Fix dependencies in Kconfig
[yaffs2.git] / yaffs_fs.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2007 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  * Acknowledgements:
9  * Luc van OostenRyck for numerous patches.
10  * Nick Bane for numerous patches.
11  * Nick Bane for 2.5/2.6 integration.
12  * Andras Toth for mknod rdev issue.
13  * Michael Fischer for finding the problem with inode inconsistency.
14  * Some code bodily lifted from JFFS
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License version 2 as
18  * published by the Free Software Foundation.
19  */
20
21 /*
22  *
23  * This is the file system front-end to YAFFS that hooks it up to
24  * the VFS.
25  *
26  * Special notes:
27  * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
28  *         this superblock
29  * >> 2.6: sb->s_fs_info  points to the yaffs_Device associated with this
30  *         superblock
31  * >> inode->u.generic_ip points to the associated yaffs_Object.
32  */
33
34 const char *yaffs_fs_c_version =
35     "$Id: yaffs_fs.c,v 1.71 2009-01-22 00:45:54 charles Exp $";
36 extern const char *yaffs_guts_c_version;
37
38 #include <linux/version.h>
39 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
40 #include <linux/config.h>
41 #endif
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/init.h>
46 #include <linux/fs.h>
47 #include <linux/proc_fs.h>
48 #include <linux/smp_lock.h>
49 #include <linux/pagemap.h>
50 #include <linux/mtd/mtd.h>
51 #include <linux/interrupt.h>
52 #include <linux/string.h>
53 #include <linux/ctype.h>
54
55 #include "asm/div64.h"
56
57 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
58
59 #include <linux/statfs.h>       /* Added NCB 15-8-2003 */
60 #include <asm/statfs.h>
61 #define UnlockPage(p) unlock_page(p)
62 #define Page_Uptodate(page)     test_bit(PG_uptodate, &(page)->flags)
63
64 /* FIXME: use sb->s_id instead ? */
65 #define yaffs_devname(sb, buf)  bdevname(sb->s_bdev, buf)
66
67 #else
68
69 #include <linux/locks.h>
70 #define BDEVNAME_SIZE           0
71 #define yaffs_devname(sb, buf)  kdevname(sb->s_dev)
72
73 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
74 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
75 #define __user
76 #endif
77
78 #endif
79
80 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
81 #define YPROC_ROOT  &proc_root
82 #else
83 #define YPROC_ROOT  NULL
84 #endif
85
86 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
87 #define WRITE_SIZE_STR "writesize"
88 #define WRITE_SIZE(mtd) (mtd)->writesize
89 #else
90 #define WRITE_SIZE_STR "oobblock"
91 #define WRITE_SIZE(mtd) (mtd)->oobblock
92 #endif
93
94 #if(LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27))
95 #define YAFFS_USE_WRITE_BEGIN_END 1
96 #else
97 #define YAFFS_USE_WRITE_BEGIN_END 0
98 #endif
99
100
101 #include <asm/uaccess.h>
102
103 #include "yportenv.h"
104 #include "yaffs_guts.h"
105
106 #include <linux/mtd/mtd.h>
107 #include "yaffs_mtdif.h"
108 #include "yaffs_mtdif1.h"
109 #include "yaffs_mtdif2.h"
110
111 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
112 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
113 unsigned int yaffs_auto_checkpoint = 1;
114
115 /* Module Parameters */
116 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
117 module_param(yaffs_traceMask,uint,0644);
118 module_param(yaffs_wr_attempts,uint,0644);
119 module_param(yaffs_auto_checkpoint,uint,0644);
120 #else
121 MODULE_PARM(yaffs_traceMask,"i");
122 MODULE_PARM(yaffs_wr_attempts,"i");
123 MODULE_PARM(yaffs_auto_checkpoint,"i");
124 #endif
125
126 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
127 /* use iget and read_inode */
128 #define Y_IGET(sb,inum) iget((sb),(inum))
129 static void yaffs_read_inode(struct inode *inode);
130
131 #else
132 /* Call local equivalent */
133 #define YAFFS_USE_OWN_IGET
134 #define Y_IGET(sb,inum) yaffs_iget((sb),(inum))
135
136 static struct inode * yaffs_iget(struct super_block *sb, unsigned long ino);
137 #endif
138
139 /*#define T(x) printk x */
140
141 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
142 #define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
143 #else
144 #define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
145 #endif
146
147 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
148 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
149
150 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
151 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
152 #else
153 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
154 #endif
155
156 static void yaffs_put_super(struct super_block *sb);
157
158 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
159                                 loff_t * pos);
160 static ssize_t yaffs_hold_space(struct file *f);
161 static void yaffs_release_space(struct file *f);
162
163 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
164 static int yaffs_file_flush(struct file *file, fl_owner_t id);
165 #else
166 static int yaffs_file_flush(struct file *file);
167 #endif
168
169 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
170                              int datasync);
171
172 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
173
174 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
175 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
176                         struct nameidata *n);
177 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
178                                    struct nameidata *n);
179 #else
180 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
181 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
182 #endif
183 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
184                       struct dentry *dentry);
185 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
186 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
187                          const char *symname);
188 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
189
190 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
191 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
192                        dev_t dev);
193 #else
194 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
195                        int dev);
196 #endif
197 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
198                         struct inode *new_dir, struct dentry *new_dentry);
199 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
200
201 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
202 static int yaffs_sync_fs(struct super_block *sb, int wait);
203 static void yaffs_write_super(struct super_block *sb);
204 #else
205 static int yaffs_sync_fs(struct super_block *sb);
206 static int yaffs_write_super(struct super_block *sb);
207 #endif
208
209 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
210 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
211 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
212 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
213 #else
214 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
215 #endif
216
217 #ifdef YAFFS_HAS_PUT_INODE
218 static void yaffs_put_inode(struct inode *inode);
219 #endif
220
221 static void yaffs_delete_inode(struct inode *);
222 static void yaffs_clear_inode(struct inode *);
223
224 static int yaffs_readpage(struct file *file, struct page *page);
225 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
226 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
227 #else
228 static int yaffs_writepage(struct page *page);
229 #endif
230
231
232 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
233 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
234                              loff_t pos, unsigned len, unsigned flags,
235                           struct page **pagep, void **fsdata);
236 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
237                            loff_t pos, unsigned len, unsigned copied,
238                            struct page *pg, void *fsdadata);
239 #else
240 static int yaffs_prepare_write(struct file *f, struct page *pg,
241                                unsigned offset, unsigned to);
242 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
243                               unsigned to);
244                                                                                                 
245 #endif
246
247 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
248                           int buflen);
249 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
250 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
251 #else
252 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
253 #endif
254
255 static struct address_space_operations yaffs_file_address_operations = {
256         .readpage = yaffs_readpage,
257         .writepage = yaffs_writepage,
258 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
259         .write_begin = yaffs_write_begin,
260         .write_end = yaffs_write_end,
261 #else
262         .prepare_write = yaffs_prepare_write,
263         .commit_write = yaffs_commit_write,
264 #endif
265 };
266
267 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,22))
268 static struct file_operations yaffs_file_operations = {
269         .read = do_sync_read,
270         .write = do_sync_write,
271         .aio_read = generic_file_aio_read,
272         .aio_write = generic_file_aio_write,
273         .mmap = generic_file_mmap,
274         .flush = yaffs_file_flush,
275         .fsync = yaffs_sync_object,
276         .splice_read = generic_file_splice_read,
277         .splice_write = generic_file_splice_write,
278         .llseek = generic_file_llseek,
279 };
280
281 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
282
283 static struct file_operations yaffs_file_operations = {
284         .read = do_sync_read,
285         .write = do_sync_write,
286         .aio_read = generic_file_aio_read,
287         .aio_write = generic_file_aio_write,
288         .mmap = generic_file_mmap,
289         .flush = yaffs_file_flush,
290         .fsync = yaffs_sync_object,
291         .sendfile = generic_file_sendfile,
292 };
293
294 #else
295
296 static struct file_operations yaffs_file_operations = {
297         .read = generic_file_read,
298         .write = generic_file_write,
299         .mmap = generic_file_mmap,
300         .flush = yaffs_file_flush,
301         .fsync = yaffs_sync_object,
302 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
303         .sendfile = generic_file_sendfile,
304 #endif
305 };
306 #endif
307
308 static struct inode_operations yaffs_file_inode_operations = {
309         .setattr = yaffs_setattr,
310 };
311
312 static struct inode_operations yaffs_symlink_inode_operations = {
313         .readlink = yaffs_readlink,
314         .follow_link = yaffs_follow_link,
315         .setattr = yaffs_setattr,
316 };
317
318 static struct inode_operations yaffs_dir_inode_operations = {
319         .create = yaffs_create,
320         .lookup = yaffs_lookup,
321         .link = yaffs_link,
322         .unlink = yaffs_unlink,
323         .symlink = yaffs_symlink,
324         .mkdir = yaffs_mkdir,
325         .rmdir = yaffs_unlink,
326         .mknod = yaffs_mknod,
327         .rename = yaffs_rename,
328         .setattr = yaffs_setattr,
329 };
330
331 static struct file_operations yaffs_dir_operations = {
332         .read = generic_read_dir,
333         .readdir = yaffs_readdir,
334         .fsync = yaffs_sync_object,
335 };
336
337 static struct super_operations yaffs_super_ops = {
338         .statfs = yaffs_statfs,
339
340 #ifndef YAFFS_USE_OWN_IGET
341         .read_inode = yaffs_read_inode,
342 #endif
343 #ifdef YAFFS_HAS_PUT_INODE
344         .put_inode = yaffs_put_inode,
345 #endif
346         .put_super = yaffs_put_super,
347         .delete_inode = yaffs_delete_inode,
348         .clear_inode = yaffs_clear_inode,
349         .sync_fs = yaffs_sync_fs,
350         .write_super = yaffs_write_super,
351 };
352
353 static void yaffs_GrossLock(yaffs_Device * dev)
354 {
355         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
356
357         down(&dev->grossLock);
358 }
359
360 static void yaffs_GrossUnlock(yaffs_Device * dev)
361 {
362         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
363         up(&dev->grossLock);
364
365 }
366
367 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
368                           int buflen)
369 {
370         unsigned char *alias;
371         int ret;
372
373         yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
374
375         yaffs_GrossLock(dev);
376
377         alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
378
379         yaffs_GrossUnlock(dev);
380
381         if (!alias)
382                 return -ENOMEM;
383
384         ret = vfs_readlink(dentry, buffer, buflen, alias);
385         kfree(alias);
386         return ret;
387 }
388
389 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
390 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
391 #else
392 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
393 #endif
394 {
395         unsigned char *alias;
396         int ret;
397         yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
398
399         yaffs_GrossLock(dev);
400
401         alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
402
403         yaffs_GrossUnlock(dev);
404
405         if (!alias)
406         {
407                 ret = -ENOMEM;
408                 goto out;
409         }
410
411         ret = vfs_follow_link(nd, alias);
412         kfree(alias);
413 out:
414 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
415         return ERR_PTR (ret);
416 #else
417         return ret;
418 #endif
419 }
420
421 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
422                               yaffs_Object * obj);
423
424 /*
425  * Lookup is used to find objects in the fs
426  */
427 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
428
429 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
430                                    struct nameidata *n)
431 #else
432 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
433 #endif
434 {
435         yaffs_Object *obj;
436         struct inode *inode = NULL;     /* NCB 2.5/2.6 needs NULL here */
437
438         yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
439
440         yaffs_GrossLock(dev);
441
442         T(YAFFS_TRACE_OS,
443           (KERN_DEBUG "yaffs_lookup for %d:%s\n",
444            yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
445
446         obj =
447             yaffs_FindObjectByName(yaffs_InodeToObject(dir),
448                                    dentry->d_name.name);
449
450         obj = yaffs_GetEquivalentObject(obj);   /* in case it was a hardlink */
451
452         /* Can't hold gross lock when calling yaffs_get_inode() */
453         yaffs_GrossUnlock(dev);
454
455         if (obj) {
456                 T(YAFFS_TRACE_OS,
457                   (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
458
459                 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
460
461                 if (inode) {
462                         T(YAFFS_TRACE_OS,
463                           (KERN_DEBUG "yaffs_loookup dentry \n"));
464 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
465  * d_add even if NULL inode */
466 #if 0
467                         /*dget(dentry); // try to solve directory bug */
468                         d_add(dentry, inode);
469
470                         /* return dentry; */
471                         return NULL;
472 #endif
473                 }
474
475         } else {
476                 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
477
478         }
479
480 /* added NCB for 2.5/6 compatability - forces add even if inode is
481  * NULL which creates dentry hash */
482         d_add(dentry, inode);
483
484         return NULL;
485         /*      return (ERR_PTR(-EIO)); */
486
487 }
488
489
490 #ifdef YAFFS_HAS_PUT_INODE
491
492 /* For now put inode is just for debugging
493  * Put inode is called when the inode **structure** is put.
494  */
495 static void yaffs_put_inode(struct inode *inode)
496 {
497         T(YAFFS_TRACE_OS,
498           ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
499            atomic_read(&inode->i_count)));
500
501 }
502 #endif
503
504 /* clear is called to tell the fs to release any per-inode data it holds */
505 static void yaffs_clear_inode(struct inode *inode)
506 {
507         yaffs_Object *obj;
508         yaffs_Device *dev;
509
510         obj = yaffs_InodeToObject(inode);
511
512         T(YAFFS_TRACE_OS,
513           ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
514            atomic_read(&inode->i_count),
515            obj ? "object exists" : "null object"));
516
517         if (obj) {
518                 dev = obj->myDev;
519                 yaffs_GrossLock(dev);
520
521                 /* Clear the association between the inode and
522                  * the yaffs_Object.
523                  */
524                 obj->myInode = NULL;
525                 yaffs_InodeToObjectLV(inode) = NULL;
526
527                 /* If the object freeing was deferred, then the real
528                  * free happens now.
529                  * This should fix the inode inconsistency problem.
530                  */
531
532                 yaffs_HandleDeferedFree(obj);
533
534                 yaffs_GrossUnlock(dev);
535         }
536
537 }
538
539 /* delete is called when the link count is zero and the inode
540  * is put (ie. nobody wants to know about it anymore, time to
541  * delete the file).
542  * NB Must call clear_inode()
543  */
544 static void yaffs_delete_inode(struct inode *inode)
545 {
546         yaffs_Object *obj = yaffs_InodeToObject(inode);
547         yaffs_Device *dev;
548
549         T(YAFFS_TRACE_OS,
550           ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
551            atomic_read(&inode->i_count),
552            obj ? "object exists" : "null object"));
553
554         if (obj) {
555                 dev = obj->myDev;
556                 yaffs_GrossLock(dev);
557                 yaffs_DeleteFile(obj);
558                 yaffs_GrossUnlock(dev);
559         }
560 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
561         truncate_inode_pages (&inode->i_data, 0);
562 #endif
563         clear_inode(inode);
564 }
565
566 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
567 static int yaffs_file_flush(struct file *file, fl_owner_t id)
568 #else
569 static int yaffs_file_flush(struct file *file)
570 #endif
571 {
572         yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
573
574         yaffs_Device *dev = obj->myDev;
575
576         T(YAFFS_TRACE_OS,
577           (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
578            obj->dirty ? "dirty" : "clean"));
579
580         yaffs_GrossLock(dev);
581
582         yaffs_FlushFile(obj, 1);
583
584         yaffs_GrossUnlock(dev);
585
586         return 0;
587 }
588
589 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
590 {
591         /* Lifted from jffs2 */
592
593         yaffs_Object *obj;
594         unsigned char *pg_buf;
595         int ret;
596
597         yaffs_Device *dev;
598
599         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
600                            (unsigned)(pg->index << PAGE_CACHE_SHIFT),
601                            (unsigned)PAGE_CACHE_SIZE));
602
603         obj = yaffs_DentryToObject(f->f_dentry);
604
605         dev = obj->myDev;
606
607 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
608         BUG_ON(!PageLocked(pg));
609 #else
610         if (!PageLocked(pg))
611                 PAGE_BUG(pg);
612 #endif
613
614         pg_buf = kmap(pg);
615         /* FIXME: Can kmap fail? */
616
617         yaffs_GrossLock(dev);
618
619         ret =
620             yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
621                                    PAGE_CACHE_SIZE);
622
623         yaffs_GrossUnlock(dev);
624
625         if (ret >= 0)
626                 ret = 0;
627
628         if (ret) {
629                 ClearPageUptodate(pg);
630                 SetPageError(pg);
631         } else {
632                 SetPageUptodate(pg);
633                 ClearPageError(pg);
634         }
635
636         flush_dcache_page(pg);
637         kunmap(pg);
638
639         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
640         return ret;
641 }
642
643 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
644 {
645         int ret = yaffs_readpage_nolock(f, pg);
646         UnlockPage(pg);
647         return ret;
648 }
649
650 static int yaffs_readpage(struct file *f, struct page *pg)
651 {
652         return yaffs_readpage_unlock(f, pg);
653 }
654
655 /* writepage inspired by/stolen from smbfs */
656
657 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
658 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
659 #else
660 static int yaffs_writepage(struct page *page)
661 #endif
662 {
663         struct address_space *mapping = page->mapping;
664         loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
665         struct inode *inode;
666         unsigned long end_index;
667         char *buffer;
668         yaffs_Object *obj;
669         int nWritten = 0;
670         unsigned nBytes;
671
672         if (!mapping)
673                 BUG();
674         inode = mapping->host;
675         if (!inode)
676                 BUG();
677
678         if (offset > inode->i_size) {
679                 T(YAFFS_TRACE_OS,
680                   (KERN_DEBUG
681                    "yaffs_writepage at %08x, inode size = %08x!!!\n",
682                    (unsigned)(page->index << PAGE_CACHE_SHIFT),
683                    (unsigned)inode->i_size));
684                 T(YAFFS_TRACE_OS,
685                   (KERN_DEBUG "                -> don't care!!\n"));
686                 unlock_page(page);
687                 return 0;
688         }
689
690         end_index = inode->i_size >> PAGE_CACHE_SHIFT;
691
692         /* easy case */
693         if (page->index < end_index) {
694                 nBytes = PAGE_CACHE_SIZE;
695         } else {
696                 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
697         }
698
699         get_page(page);
700
701         buffer = kmap(page);
702
703         obj = yaffs_InodeToObject(inode);
704         yaffs_GrossLock(obj->myDev);
705
706         T(YAFFS_TRACE_OS,
707           (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
708            (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
709         T(YAFFS_TRACE_OS,
710           (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
711            (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
712
713         nWritten =
714             yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
715                                   nBytes, 0);
716
717         T(YAFFS_TRACE_OS,
718           (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
719            (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
720
721         yaffs_GrossUnlock(obj->myDev);
722
723         kunmap(page);
724         SetPageUptodate(page);
725         UnlockPage(page);
726         put_page(page);
727
728         return (nWritten == nBytes) ? 0 : -ENOSPC;
729 }
730
731
732 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
733 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
734                              loff_t pos, unsigned len, unsigned flags,
735                           struct page **pagep, void **fsdata)
736
737 {
738         struct page *pg = NULL;
739         pgoff_t index = pos >> PAGE_CACHE_SHIFT;
740         uint32_t offset = pos & (PAGE_CACHE_SIZE - 1);
741         uint32_t to = offset + len;
742         
743         int ret = 0;
744         int space_held = 0;
745         
746         T(YAFFS_TRACE_OS, (KERN_DEBUG "start yaffs_write_begin\n"));
747         /* Get a page */
748         pg = __grab_cache_page(mapping,index);
749         *pagep = pg;    
750         if(!pg){
751                 ret =  -ENOMEM;
752                 goto out;
753         }
754         /* Get fs space */
755         space_held = yaffs_hold_space(filp);
756         
757         if(!space_held){
758                 ret = -ENOSPC;
759                 goto out;
760         }
761                 
762         /* Update page if required */
763         
764         if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
765                 ret = yaffs_readpage_nolock(filp, pg);
766         
767         if(ret)
768                 goto out;
769
770         /* Happy path return */
771         T(YAFFS_TRACE_OS, (KERN_DEBUG "end yaffs_write_begin - ok\n"));
772                 
773         return 0;
774                 
775 out:
776         T(YAFFS_TRACE_OS, (KERN_DEBUG "end yaffs_write_begin fail returning %d\n",ret));
777         if(space_held){
778                 yaffs_release_space(filp);
779         }
780         if(pg) {
781                 unlock_page(pg);
782                 page_cache_release(pg);
783         }
784         return ret;
785 }
786
787 #else
788
789 static int yaffs_prepare_write(struct file *f, struct page *pg,
790                                unsigned offset, unsigned to)
791 {
792
793         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
794         if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
795                 return yaffs_readpage_nolock(f, pg);
796         return 0;
797
798 }
799 #endif
800
801 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
802 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
803                            loff_t pos, unsigned len, unsigned copied,
804                            struct page *pg, void *fsdadata)
805 {
806         int ret = 0;
807         void *addr, *kva;
808         pgoff_t index = pos >> PAGE_CACHE_SHIFT;
809         uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE -1); 
810
811
812         
813         kva=kmap(pg);
814         addr = kva + offset_into_page;
815
816         T(YAFFS_TRACE_OS,
817           (KERN_DEBUG "yaffs_write_end addr %x pos %x nBytes %d\n", (unsigned) addr,
818            (int)pos, copied));
819
820         ret = yaffs_file_write(filp, addr, copied, &pos);
821
822         if (ret != copied) {
823                 T(YAFFS_TRACE_OS,
824                   (KERN_DEBUG
825                    "yaffs_write_end not same size ret %d  copied %d\n",
826                    ret, copied ));
827                 SetPageError(pg);
828                 ClearPageUptodate(pg);
829         } else {
830                 SetPageUptodate(pg);
831         }
832
833         kunmap(pg);
834
835         yaffs_release_space(filp);
836         unlock_page(pg);
837         page_cache_release(pg);
838         return ret;
839 }
840 #else
841
842 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
843                               unsigned to)
844 {
845
846         void *addr, *kva;
847         
848         loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
849         int nBytes = to - offset;
850         int nWritten;
851
852         unsigned spos = pos;
853         unsigned saddr;
854         
855         kva=kmap(pg);
856         addr = kva + offset;
857
858         saddr = (unsigned) addr;
859
860         T(YAFFS_TRACE_OS,
861           (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
862            spos, nBytes));
863
864         nWritten = yaffs_file_write(f, addr, nBytes, &pos);
865
866         if (nWritten != nBytes) {
867                 T(YAFFS_TRACE_OS,
868                   (KERN_DEBUG
869                    "yaffs_commit_write not same size nWritten %d  nBytes %d\n",
870                    nWritten, nBytes));
871                 SetPageError(pg);
872                 ClearPageUptodate(pg);
873         } else {
874                 SetPageUptodate(pg);
875         }
876
877         kunmap(pg);
878
879         T(YAFFS_TRACE_OS,
880           (KERN_DEBUG "yaffs_commit_write returning %d\n",
881            nWritten == nBytes ? 0 : nWritten));
882
883         return nWritten == nBytes ? 0 : nWritten;
884
885 }
886 #endif
887
888
889 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
890 {
891         if (inode && obj) {
892
893
894                 /* Check mode against the variant type and attempt to repair if broken. */
895                 __u32 mode = obj->yst_mode;
896                 switch( obj->variantType ){
897                 case YAFFS_OBJECT_TYPE_FILE :
898                         if( ! S_ISREG(mode) ){
899                                 obj->yst_mode &= ~S_IFMT;
900                                 obj->yst_mode |= S_IFREG;
901                         }
902
903                         break;
904                 case YAFFS_OBJECT_TYPE_SYMLINK :
905                         if( ! S_ISLNK(mode) ){
906                                 obj->yst_mode &= ~S_IFMT;
907                                 obj->yst_mode |= S_IFLNK;
908                         }
909
910                         break;
911                 case YAFFS_OBJECT_TYPE_DIRECTORY :
912                         if( ! S_ISDIR(mode) ){
913                                 obj->yst_mode &= ~S_IFMT;
914                                 obj->yst_mode |= S_IFDIR;
915                         }
916
917                         break;
918                 case YAFFS_OBJECT_TYPE_UNKNOWN :
919                 case YAFFS_OBJECT_TYPE_HARDLINK :
920                 case YAFFS_OBJECT_TYPE_SPECIAL :
921                 default:
922                         /* TODO? */
923                         break;
924                 }
925
926                 inode->i_flags |= S_NOATIME;
927                 
928                 inode->i_ino = obj->objectId;
929                 inode->i_mode = obj->yst_mode;
930                 inode->i_uid = obj->yst_uid;
931                 inode->i_gid = obj->yst_gid;
932 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
933                 inode->i_blksize = inode->i_sb->s_blocksize;
934 #endif
935 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
936
937                 inode->i_rdev = old_decode_dev(obj->yst_rdev);
938                 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
939                 inode->i_atime.tv_nsec = 0;
940                 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
941                 inode->i_mtime.tv_nsec = 0;
942                 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
943                 inode->i_ctime.tv_nsec = 0;
944 #else
945                 inode->i_rdev = obj->yst_rdev;
946                 inode->i_atime = obj->yst_atime;
947                 inode->i_mtime = obj->yst_mtime;
948                 inode->i_ctime = obj->yst_ctime;
949 #endif
950                 inode->i_size = yaffs_GetObjectFileLength(obj);
951                 inode->i_blocks = (inode->i_size + 511) >> 9;
952
953                 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
954
955                 T(YAFFS_TRACE_OS,
956                   (KERN_DEBUG
957                    "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
958                    inode->i_mode, inode->i_uid, inode->i_gid,
959                    (int)inode->i_size, atomic_read(&inode->i_count)));
960
961                 switch (obj->yst_mode & S_IFMT) {
962                 default:        /* fifo, device or socket */
963 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
964                         init_special_inode(inode, obj->yst_mode,
965                                            old_decode_dev(obj->yst_rdev));
966 #else
967                         init_special_inode(inode, obj->yst_mode,
968                                            (dev_t) (obj->yst_rdev));
969 #endif
970                         break;
971                 case S_IFREG:   /* file */
972                         inode->i_op = &yaffs_file_inode_operations;
973                         inode->i_fop = &yaffs_file_operations;
974                         inode->i_mapping->a_ops =
975                             &yaffs_file_address_operations;
976                         break;
977                 case S_IFDIR:   /* directory */
978                         inode->i_op = &yaffs_dir_inode_operations;
979                         inode->i_fop = &yaffs_dir_operations;
980                         break;
981                 case S_IFLNK:   /* symlink */
982                         inode->i_op = &yaffs_symlink_inode_operations;
983                         break;
984                 }
985
986                 yaffs_InodeToObjectLV(inode) = obj;
987
988                 obj->myInode = inode;
989
990         } else {
991                 T(YAFFS_TRACE_OS,
992                   (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
993         }
994
995 }
996
997 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
998                               yaffs_Object * obj)
999 {
1000         struct inode *inode;
1001
1002         if (!sb) {
1003                 T(YAFFS_TRACE_OS,
1004                   (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
1005                 return NULL;
1006
1007         }
1008
1009         if (!obj) {
1010                 T(YAFFS_TRACE_OS,
1011                   (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
1012                 return NULL;
1013
1014         }
1015
1016         T(YAFFS_TRACE_OS,
1017           (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
1018
1019         inode = Y_IGET(sb, obj->objectId);
1020         if(IS_ERR(inode))
1021           return NULL;
1022
1023         /* NB Side effect: iget calls back to yaffs_read_inode(). */
1024         /* iget also increments the inode's i_count */
1025         /* NB You can't be holding grossLock or deadlock will happen! */
1026
1027         return inode;
1028 }
1029
1030 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1031                                 loff_t * pos)
1032 {
1033         yaffs_Object *obj;
1034         int nWritten, ipos;
1035         struct inode *inode;
1036         yaffs_Device *dev;
1037
1038         obj = yaffs_DentryToObject(f->f_dentry);
1039
1040         dev = obj->myDev;
1041
1042         yaffs_GrossLock(dev);
1043
1044         inode = f->f_dentry->d_inode;
1045
1046         if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
1047                 ipos = inode->i_size;
1048         } else {
1049                 ipos = *pos;
1050         }
1051
1052         if (!obj) {
1053                 T(YAFFS_TRACE_OS,
1054                   (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
1055         } else {
1056                 T(YAFFS_TRACE_OS,
1057                   (KERN_DEBUG
1058                    "yaffs_file_write about to write writing %d bytes"
1059                    "to object %d at %d\n",
1060                    n, obj->objectId, ipos));
1061         }
1062
1063         nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1064
1065         T(YAFFS_TRACE_OS,
1066           (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
1067            n, nWritten, ipos));
1068         if (nWritten > 0) {
1069                 ipos += nWritten;
1070                 *pos = ipos;
1071                 if (ipos > inode->i_size) {
1072                         inode->i_size = ipos;
1073                         inode->i_blocks = (ipos + 511) >> 9;
1074
1075                         T(YAFFS_TRACE_OS,
1076                           (KERN_DEBUG
1077                            "yaffs_file_write size updated to %d bytes, "
1078                            "%d blocks\n",
1079                            ipos, (int)(inode->i_blocks)));
1080                 }
1081
1082         }
1083         yaffs_GrossUnlock(dev);
1084         return nWritten == 0 ? -ENOSPC : nWritten;
1085 }
1086
1087 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1088 /* For now we just assume few parallel writes and check against a small number. */
1089 /* Todo: need to do this with a counter to handle parallel reads better */
1090
1091 static ssize_t yaffs_hold_space(struct file *f)
1092 {
1093         yaffs_Object *obj;
1094         yaffs_Device *dev;
1095         
1096         int nFreeChunks;
1097
1098         
1099         obj = yaffs_DentryToObject(f->f_dentry);
1100
1101         dev = obj->myDev;
1102
1103         yaffs_GrossLock(dev);
1104
1105         nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1106         
1107         yaffs_GrossUnlock(dev);
1108
1109         return (nFreeChunks > 20) ? 1 : 0;
1110 }
1111
1112 static void yaffs_release_space(struct file *f)
1113 {
1114         yaffs_Object *obj;
1115         yaffs_Device *dev;
1116         
1117         
1118         obj = yaffs_DentryToObject(f->f_dentry);
1119
1120         dev = obj->myDev;
1121
1122         yaffs_GrossLock(dev);
1123
1124         
1125         yaffs_GrossUnlock(dev);
1126
1127 }
1128
1129 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1130 {
1131         yaffs_Object *obj;
1132         yaffs_Device *dev;
1133         struct inode *inode = f->f_dentry->d_inode;
1134         unsigned long offset, curoffs;
1135         struct ylist_head *i;
1136         yaffs_Object *l;
1137
1138         char name[YAFFS_MAX_NAME_LENGTH + 1];
1139
1140         obj = yaffs_DentryToObject(f->f_dentry);
1141         dev = obj->myDev;
1142
1143         yaffs_GrossLock(dev);
1144
1145         offset = f->f_pos;
1146
1147         T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
1148
1149         if (offset == 0) {
1150                 T(YAFFS_TRACE_OS,
1151                   (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
1152                    (int)inode->i_ino));
1153                 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
1154                     < 0) {
1155                         goto out;
1156                 }
1157                 offset++;
1158                 f->f_pos++;
1159         }
1160         if (offset == 1) {
1161                 T(YAFFS_TRACE_OS,
1162                   (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
1163                    (int)f->f_dentry->d_parent->d_inode->i_ino));
1164                 if (filldir
1165                     (dirent, "..", 2, offset,
1166                      f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
1167                         goto out;
1168                 }
1169                 offset++;
1170                 f->f_pos++;
1171         }
1172
1173         curoffs = 1;
1174
1175         /* If the directory has changed since the open or last call to
1176            readdir, rewind to after the 2 canned entries. */
1177
1178         if (f->f_version != inode->i_version) {
1179                 offset = 2;
1180                 f->f_pos = offset;
1181                 f->f_version = inode->i_version;
1182         }
1183
1184         ylist_for_each(i, &obj->variant.directoryVariant.children) {
1185                 curoffs++;
1186                 if (curoffs >= offset) {
1187                         l = ylist_entry(i, yaffs_Object, siblings);
1188
1189                         yaffs_GetObjectName(l, name,
1190                                             YAFFS_MAX_NAME_LENGTH + 1);
1191                         T(YAFFS_TRACE_OS,
1192                           (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
1193                            yaffs_GetObjectInode(l)));
1194
1195                         if (filldir(dirent,
1196                                     name,
1197                                     strlen(name),
1198                                     offset,
1199                                     yaffs_GetObjectInode(l),
1200                                     yaffs_GetObjectType(l))
1201                             < 0) {
1202                                 goto up_and_out;
1203                         }
1204
1205                         offset++;
1206                         f->f_pos++;
1207                 }
1208         }
1209
1210       up_and_out:
1211       out:
1212
1213         yaffs_GrossUnlock(dev);
1214
1215         return 0;
1216 }
1217
1218 /*
1219  * File creation. Allocate an inode, and we're done..
1220  */
1221 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1222 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1223                        dev_t rdev)
1224 #else
1225 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1226                        int rdev)
1227 #endif
1228 {
1229         struct inode *inode;
1230
1231         yaffs_Object *obj = NULL;
1232         yaffs_Device *dev;
1233
1234         yaffs_Object *parent = yaffs_InodeToObject(dir);
1235
1236         int error = -ENOSPC;
1237         uid_t uid = current->fsuid;
1238         gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1239
1240         if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1241                 mode |= S_ISGID;
1242
1243         if (parent) {
1244                 T(YAFFS_TRACE_OS,
1245                   (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
1246                    parent->objectId, parent->variantType));
1247         } else {
1248                 T(YAFFS_TRACE_OS,
1249                   (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
1250                 return -EPERM;
1251         }
1252
1253         T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1254                            "mode %x dev %x\n",
1255                            dentry->d_name.name, mode, rdev));
1256
1257         dev = parent->myDev;
1258
1259         yaffs_GrossLock(dev);
1260
1261         switch (mode & S_IFMT) {
1262         default:
1263                 /* Special (socket, fifo, device...) */
1264                 T(YAFFS_TRACE_OS, (KERN_DEBUG
1265                                    "yaffs_mknod: making special\n"));
1266 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1267                 obj =
1268                     yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1269                                        gid, old_encode_dev(rdev));
1270 #else
1271                 obj =
1272                     yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1273                                        gid, rdev);
1274 #endif
1275                 break;
1276         case S_IFREG:           /* file          */
1277                 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1278                 obj =
1279                     yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1280                                     gid);
1281                 break;
1282         case S_IFDIR:           /* directory */
1283                 T(YAFFS_TRACE_OS,
1284                   (KERN_DEBUG "yaffs_mknod: making directory\n"));
1285                 obj =
1286                     yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1287                                          uid, gid);
1288                 break;
1289         case S_IFLNK:           /* symlink */
1290                 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1291                 obj = NULL;     /* Do we ever get here? */
1292                 break;
1293         }
1294
1295         /* Can not call yaffs_get_inode() with gross lock held */
1296         yaffs_GrossUnlock(dev);
1297
1298         if (obj) {
1299                 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1300                 d_instantiate(dentry, inode);
1301                 T(YAFFS_TRACE_OS,
1302                   (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
1303                    obj->objectId, atomic_read(&inode->i_count)));
1304                 error = 0;
1305         } else {
1306                 T(YAFFS_TRACE_OS,
1307                   (KERN_DEBUG "yaffs_mknod failed making object\n"));
1308                 error = -ENOMEM;
1309         }
1310
1311         return error;
1312 }
1313
1314 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1315 {
1316         int retVal;
1317         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
1318         retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1319 #if 0
1320         /* attempt to fix dir bug - didn't work */
1321         if (!retVal) {
1322                 dget(dentry);
1323         }
1324 #endif
1325         return retVal;
1326 }
1327
1328 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1329 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1330                         struct nameidata *n)
1331 #else
1332 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1333 #endif
1334 {
1335         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1336         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1337 }
1338
1339 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1340 {
1341         int retVal;
1342
1343         yaffs_Device *dev;
1344
1345         T(YAFFS_TRACE_OS,
1346           (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1347            dentry->d_name.name));
1348
1349         dev = yaffs_InodeToObject(dir)->myDev;
1350
1351         yaffs_GrossLock(dev);
1352
1353         retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1354
1355         if (retVal == YAFFS_OK) {
1356                 dentry->d_inode->i_nlink--;
1357                 dir->i_version++;
1358                 yaffs_GrossUnlock(dev);
1359                 mark_inode_dirty(dentry->d_inode);
1360                 return 0;
1361         }
1362         yaffs_GrossUnlock(dev);
1363         return -ENOTEMPTY;
1364 }
1365
1366 /*
1367  * Create a link...
1368  */
1369 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1370                       struct dentry *dentry)
1371 {
1372         struct inode *inode = old_dentry->d_inode;
1373         yaffs_Object *obj = NULL;
1374         yaffs_Object *link = NULL;
1375         yaffs_Device *dev;
1376
1377         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1378
1379         obj = yaffs_InodeToObject(inode);
1380         dev = obj->myDev;
1381
1382         yaffs_GrossLock(dev);
1383
1384         if (!S_ISDIR(inode->i_mode))    /* Don't link directories */
1385         {
1386                 link =
1387                     yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1388                                obj);
1389         }
1390
1391         if (link) {
1392                 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1393                 d_instantiate(dentry, old_dentry->d_inode);
1394                 atomic_inc(&old_dentry->d_inode->i_count);
1395                 T(YAFFS_TRACE_OS,
1396                   (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1397                    old_dentry->d_inode->i_nlink,
1398                    atomic_read(&old_dentry->d_inode->i_count)));
1399
1400         }
1401
1402         yaffs_GrossUnlock(dev);
1403
1404         if (link) {
1405
1406                 return 0;
1407         }
1408
1409         return -EPERM;
1410 }
1411
1412 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1413                          const char *symname)
1414 {
1415         yaffs_Object *obj;
1416         yaffs_Device *dev;
1417         uid_t uid = current->fsuid;
1418         gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1419
1420         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1421
1422         dev = yaffs_InodeToObject(dir)->myDev;
1423         yaffs_GrossLock(dev);
1424         obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1425                                  S_IFLNK | S_IRWXUGO, uid, gid, symname);
1426         yaffs_GrossUnlock(dev);
1427
1428         if (obj) {
1429
1430                 struct inode *inode;
1431
1432                 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1433                 d_instantiate(dentry, inode);
1434                 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1435                 return 0;
1436         } else {
1437                 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1438
1439         }
1440
1441         return -ENOMEM;
1442 }
1443
1444 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1445                              int datasync)
1446 {
1447
1448         yaffs_Object *obj;
1449         yaffs_Device *dev;
1450
1451         obj = yaffs_DentryToObject(dentry);
1452
1453         dev = obj->myDev;
1454
1455         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1456         yaffs_GrossLock(dev);
1457         yaffs_FlushFile(obj, 1);
1458         yaffs_GrossUnlock(dev);
1459         return 0;
1460 }
1461
1462 /*
1463  * The VFS layer already does all the dentry stuff for rename.
1464  *
1465  * NB: POSIX says you can rename an object over an old object of the same name
1466  */
1467 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1468                         struct inode *new_dir, struct dentry *new_dentry)
1469 {
1470         yaffs_Device *dev;
1471         int retVal = YAFFS_FAIL;
1472         yaffs_Object *target;
1473
1474         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1475         dev = yaffs_InodeToObject(old_dir)->myDev;
1476
1477         yaffs_GrossLock(dev);
1478
1479         /* Check if the target is an existing directory that is not empty. */
1480         target =
1481             yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1482                                    new_dentry->d_name.name);
1483
1484
1485
1486         if (target &&
1487             target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1488             !ylist_empty(&target->variant.directoryVariant.children)) {
1489
1490                 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1491
1492                 retVal = YAFFS_FAIL;
1493         } else {
1494
1495                 /* Now does unlinking internally using shadowing mechanism */
1496                 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1497
1498                 retVal =
1499                     yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1500                                        old_dentry->d_name.name,
1501                                        yaffs_InodeToObject(new_dir),
1502                                        new_dentry->d_name.name);
1503
1504         }
1505         yaffs_GrossUnlock(dev);
1506
1507         if (retVal == YAFFS_OK) {
1508                 if(target) {
1509                         new_dentry->d_inode->i_nlink--;
1510                         mark_inode_dirty(new_dentry->d_inode);
1511                 }
1512
1513                 return 0;
1514         } else {
1515                 return -ENOTEMPTY;
1516         }
1517
1518 }
1519
1520 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1521 {
1522         struct inode *inode = dentry->d_inode;
1523         int error;
1524         yaffs_Device *dev;
1525
1526         T(YAFFS_TRACE_OS,
1527           (KERN_DEBUG "yaffs_setattr of object %d\n",
1528            yaffs_InodeToObject(inode)->objectId));
1529
1530         if ((error = inode_change_ok(inode, attr)) == 0) {
1531
1532                 dev = yaffs_InodeToObject(inode)->myDev;
1533                 yaffs_GrossLock(dev);
1534                 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1535                     YAFFS_OK) {
1536                         error = 0;
1537                 } else {
1538                         error = -EPERM;
1539                 }
1540                 yaffs_GrossUnlock(dev);
1541                 if (!error)
1542                         error = inode_setattr(inode, attr);
1543         }
1544         return error;
1545 }
1546
1547 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1548 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1549 {
1550         yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1551         struct super_block *sb = dentry->d_sb;
1552 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1553 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1554 {
1555         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1556 #else
1557 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1558 {
1559         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1560 #endif
1561
1562         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1563
1564         yaffs_GrossLock(dev);
1565
1566         buf->f_type = YAFFS_MAGIC;
1567         buf->f_bsize = sb->s_blocksize;
1568         buf->f_namelen = 255;
1569         
1570         if(dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)){
1571                 /* Do this if chunk size is not a power of 2 */
1572                 
1573                 uint64_t bytesInDev;
1574                 uint64_t bytesFree;
1575
1576                 bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock +1))) *
1577                              ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
1578         
1579                 do_div(bytesInDev,sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1580                 buf->f_blocks = bytesInDev;
1581
1582                 bytesFree  = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1583                              ((uint64_t)(dev->nDataBytesPerChunk));
1584         
1585                 do_div(bytesFree,sb->s_blocksize);
1586         
1587                 buf->f_bfree = bytesFree;
1588         
1589         } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1590         
1591                 buf->f_blocks =
1592                            (dev->endBlock - dev->startBlock + 1) * 
1593                             dev->nChunksPerBlock / 
1594                             (sb->s_blocksize / dev->nDataBytesPerChunk);
1595                 buf->f_bfree =
1596                            yaffs_GetNumberOfFreeChunks(dev) / 
1597                            (sb->s_blocksize / dev->nDataBytesPerChunk);
1598         } else {
1599                buf->f_blocks =
1600                            (dev->endBlock - dev->startBlock + 1) * 
1601                            dev->nChunksPerBlock * 
1602                            (dev->nDataBytesPerChunk / sb->s_blocksize);
1603                            
1604                        buf->f_bfree =
1605                            yaffs_GetNumberOfFreeChunks(dev) * 
1606                            (dev->nDataBytesPerChunk / sb->s_blocksize);
1607         }
1608         
1609         
1610         buf->f_files = 0;
1611         buf->f_ffree = 0;
1612         buf->f_bavail = buf->f_bfree;
1613
1614         yaffs_GrossUnlock(dev);
1615         return 0;
1616 }
1617
1618
1619 static int yaffs_do_sync_fs(struct super_block *sb)
1620 {
1621
1622         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1623         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
1624
1625         if(sb->s_dirt) {
1626                 yaffs_GrossLock(dev);
1627
1628                 if(dev){
1629                         yaffs_FlushEntireDeviceCache(dev);
1630                         yaffs_CheckpointSave(dev);
1631                 }
1632
1633                 yaffs_GrossUnlock(dev);
1634
1635                 sb->s_dirt = 0;
1636         }
1637         return 0;
1638 }
1639
1640
1641 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1642 static void yaffs_write_super(struct super_block *sb)
1643 #else
1644 static int yaffs_write_super(struct super_block *sb)
1645 #endif
1646 {
1647
1648         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
1649         if (yaffs_auto_checkpoint >= 2)
1650                 yaffs_do_sync_fs(sb);
1651 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
1652         return 0; 
1653 #endif
1654 }
1655
1656
1657 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1658 static int yaffs_sync_fs(struct super_block *sb, int wait)
1659 #else
1660 static int yaffs_sync_fs(struct super_block *sb)
1661 #endif
1662 {
1663
1664         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
1665
1666         if (yaffs_auto_checkpoint >= 1)
1667                 yaffs_do_sync_fs(sb);
1668         
1669         return 0; 
1670
1671 }
1672
1673 #ifdef YAFFS_USE_OWN_IGET
1674
1675 static struct inode * yaffs_iget(struct super_block *sb, unsigned long ino)
1676 {
1677         struct inode *inode;
1678         yaffs_Object *obj;
1679         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1680
1681         T(YAFFS_TRACE_OS,
1682           (KERN_DEBUG "yaffs_iget for %lu\n", ino));
1683
1684         inode = iget_locked(sb, ino);
1685         if (!inode)
1686                 return ERR_PTR(-ENOMEM);
1687         if (!(inode->i_state & I_NEW))
1688                 return inode;
1689
1690        /* NB This is called as a side effect of other functions, but
1691         * we had to release the lock to prevent deadlocks, so
1692         * need to lock again.
1693         */
1694     
1695         yaffs_GrossLock(dev);
1696
1697         obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1698
1699         yaffs_FillInodeFromObject(inode, obj);
1700
1701         yaffs_GrossUnlock(dev);
1702         
1703         unlock_new_inode(inode);
1704         return inode;
1705 }
1706
1707 #else
1708
1709 static void yaffs_read_inode(struct inode *inode)
1710 {
1711         /* NB This is called as a side effect of other functions, but
1712          * we had to release the lock to prevent deadlocks, so
1713          * need to lock again.
1714          */
1715
1716         yaffs_Object *obj;
1717         yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1718
1719         T(YAFFS_TRACE_OS,
1720           (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1721
1722         yaffs_GrossLock(dev);
1723
1724         obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1725
1726         yaffs_FillInodeFromObject(inode, obj);
1727
1728         yaffs_GrossUnlock(dev);
1729 }
1730
1731 #endif
1732
1733 static YLIST_HEAD(yaffs_dev_list);
1734
1735 #if 0 // not used
1736 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1737 {
1738         yaffs_Device    *dev = yaffs_SuperToDevice(sb);
1739
1740         if( *flags & MS_RDONLY ) {
1741                 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1742
1743                 T(YAFFS_TRACE_OS,
1744                         (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
1745
1746                 yaffs_GrossLock(dev);
1747
1748                 yaffs_FlushEntireDeviceCache(dev);
1749
1750                 yaffs_CheckpointSave(dev);
1751
1752                 if (mtd->sync)
1753                         mtd->sync(mtd);
1754
1755                 yaffs_GrossUnlock(dev);
1756         }
1757         else {
1758                 T(YAFFS_TRACE_OS,
1759                         (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
1760         }
1761
1762         return 0;
1763 }
1764 #endif
1765
1766 static void yaffs_put_super(struct super_block *sb)
1767 {
1768         yaffs_Device *dev = yaffs_SuperToDevice(sb);
1769
1770         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
1771
1772         yaffs_GrossLock(dev);
1773
1774         yaffs_FlushEntireDeviceCache(dev);
1775
1776         yaffs_CheckpointSave(dev);
1777
1778         if (dev->putSuperFunc) {
1779                 dev->putSuperFunc(sb);
1780         }
1781
1782         yaffs_Deinitialise(dev);
1783
1784         yaffs_GrossUnlock(dev);
1785
1786         /* we assume this is protected by lock_kernel() in mount/umount */
1787         ylist_del(&dev->devList);
1788
1789         if(dev->spareBuffer){
1790                 YFREE(dev->spareBuffer);
1791                 dev->spareBuffer = NULL;
1792         }
1793
1794         kfree(dev);
1795 }
1796
1797
1798 static void yaffs_MTDPutSuper(struct super_block *sb)
1799 {
1800
1801         struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1802
1803         if (mtd->sync) {
1804                 mtd->sync(mtd);
1805         }
1806
1807         put_mtd_device(mtd);
1808 }
1809
1810
1811 static void yaffs_MarkSuperBlockDirty(void *vsb)
1812 {
1813         struct super_block *sb = (struct super_block *)vsb;
1814
1815         T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
1816         if(sb)
1817                 sb->s_dirt = 1;
1818 }
1819
1820 typedef struct {
1821         int inband_tags;
1822         int skip_checkpoint_read;
1823         int skip_checkpoint_write;
1824         int no_cache;
1825 } yaffs_options;
1826
1827 #define MAX_OPT_LEN 20
1828 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
1829 {
1830         char cur_opt[MAX_OPT_LEN+1];
1831         int p;
1832         int error = 0;
1833
1834         /* Parse through the options which is a comma seperated list */
1835
1836         while(options_str && *options_str && !error){
1837                 memset(cur_opt,0,MAX_OPT_LEN+1);
1838                 p = 0;
1839
1840                 while(*options_str && *options_str != ','){
1841                         if(p < MAX_OPT_LEN){
1842                                 cur_opt[p] = *options_str;
1843                                 p++;
1844                         }
1845                         options_str++;
1846                 }
1847
1848                 if(!strcmp(cur_opt,"inband-tags"))
1849                         options->inband_tags = 1;
1850                 else if(!strcmp(cur_opt,"no-cache"))
1851                         options->no_cache = 1;
1852                 else if(!strcmp(cur_opt,"no-checkpoint-read"))
1853                         options->skip_checkpoint_read = 1;
1854                 else if(!strcmp(cur_opt,"no-checkpoint-write"))
1855                         options->skip_checkpoint_write = 1;
1856                 else if(!strcmp(cur_opt,"no-checkpoint")){
1857                         options->skip_checkpoint_read = 1;
1858                         options->skip_checkpoint_write = 1;
1859                 } else {
1860                         printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
1861                         error = 1;
1862                 }
1863
1864         }
1865
1866         return error;
1867 }
1868
1869 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1870                                                      struct super_block *sb,
1871                                                      void *data, int silent)
1872 {
1873         int nBlocks;
1874         struct inode *inode = NULL;
1875         struct dentry *root;
1876         yaffs_Device *dev = 0;
1877         char devname_buf[BDEVNAME_SIZE + 1];
1878         struct mtd_info *mtd;
1879         int err;
1880         char *data_str = (char *)data;
1881
1882         yaffs_options options;
1883
1884         sb->s_magic = YAFFS_MAGIC;
1885         sb->s_op = &yaffs_super_ops;
1886         sb->s_flags |= MS_NOATIME;
1887
1888         if (!sb)
1889                 printk(KERN_INFO "yaffs: sb is NULL\n");
1890         else if (!sb->s_dev)
1891                 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1892         else if (!yaffs_devname(sb, devname_buf))
1893                 printk(KERN_INFO "yaffs: devname is NULL\n");
1894         else
1895                 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1896                        sb->s_dev,
1897                        yaffs_devname(sb, devname_buf));
1898
1899         if(!data_str)
1900                 data_str = "";
1901
1902         printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
1903
1904         memset(&options,0,sizeof(options));
1905
1906         if(yaffs_parse_options(&options,data_str)){
1907                 /* Option parsing failed */
1908                 return NULL;
1909         }
1910
1911
1912         sb->s_blocksize = PAGE_CACHE_SIZE;
1913         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1914         T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1915         T(YAFFS_TRACE_OS,
1916           ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1917
1918 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1919         T(YAFFS_TRACE_OS,
1920           ("yaffs: Write verification disabled. All guarantees "
1921            "null and void\n"));
1922 #endif
1923
1924         T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1925                                "\"%s\"\n",
1926                                MAJOR(sb->s_dev), MINOR(sb->s_dev),
1927                                yaffs_devname(sb, devname_buf)));
1928
1929         /* Check it's an mtd device..... */
1930         if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1931                 return NULL;    /* This isn't an mtd device */
1932         }
1933         /* Get the device */
1934         mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1935         if (!mtd) {
1936                 T(YAFFS_TRACE_ALWAYS,
1937                   ("yaffs: MTD device #%u doesn't appear to exist\n",
1938                    MINOR(sb->s_dev)));
1939                 return NULL;
1940         }
1941         /* Check it's NAND */
1942         if (mtd->type != MTD_NANDFLASH) {
1943                 T(YAFFS_TRACE_ALWAYS,
1944                   ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1945                 return NULL;
1946         }
1947
1948         T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1949         T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1950         T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1951         T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1952         T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1953         T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1954         T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1955         T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
1956         T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1957         T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1958         T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1959
1960 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1961
1962         if (yaffsVersion == 1 &&
1963             WRITE_SIZE(mtd) >= 2048) {
1964             T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1965             yaffsVersion = 2;
1966         }
1967
1968         /* Added NCB 26/5/2006 for completeness */
1969         if (yaffsVersion == 2 && 
1970             !options.inband_tags &&
1971             WRITE_SIZE(mtd) == 512){
1972             T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
1973             yaffsVersion = 1;
1974         }
1975
1976 #endif
1977
1978         if (yaffsVersion == 2) {
1979                 /* Check for version 2 style functions */
1980                 if (!mtd->erase ||
1981                     !mtd->block_isbad ||
1982                     !mtd->block_markbad ||
1983                     !mtd->read ||
1984                     !mtd->write ||
1985 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1986                     !mtd->read_oob || !mtd->write_oob) {
1987 #else
1988                     !mtd->write_ecc ||
1989                     !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1990 #endif
1991                         T(YAFFS_TRACE_ALWAYS,
1992                           ("yaffs: MTD device does not support required "
1993                            "functions\n"));;
1994                         return NULL;
1995                 }
1996
1997                 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1998                     mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
1999                     !options.inband_tags) {
2000                         T(YAFFS_TRACE_ALWAYS,
2001                           ("yaffs: MTD device does not have the "
2002                            "right page sizes\n"));
2003                         return NULL;
2004                 }
2005         } else {
2006                 /* Check for V1 style functions */
2007                 if (!mtd->erase ||
2008                     !mtd->read ||
2009                     !mtd->write ||
2010 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2011                     !mtd->read_oob || !mtd->write_oob) {
2012 #else
2013                     !mtd->write_ecc ||
2014                     !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2015 #endif
2016                         T(YAFFS_TRACE_ALWAYS,
2017                           ("yaffs: MTD device does not support required "
2018                            "functions\n"));;
2019                         return NULL;
2020                 }
2021
2022                 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2023                     mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2024                         T(YAFFS_TRACE_ALWAYS,
2025                           ("yaffs: MTD device does not support have the "
2026                            "right page sizes\n"));
2027                         return NULL;
2028                 }
2029         }
2030
2031         /* OK, so if we got here, we have an MTD that's NAND and looks
2032          * like it has the right capabilities
2033          * Set the yaffs_Device up for mtd
2034          */
2035
2036 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2037         sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2038 #else
2039         sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2040 #endif
2041         if (!dev) {
2042                 /* Deep shit could not allocate device structure */
2043                 T(YAFFS_TRACE_ALWAYS,
2044                   ("yaffs_read_super: Failed trying to allocate "
2045                    "yaffs_Device. \n"));
2046                 return NULL;
2047         }
2048
2049         memset(dev, 0, sizeof(yaffs_Device));
2050         dev->genericDevice = mtd;
2051         dev->name = mtd->name;
2052
2053         /* Set up the memory size parameters.... */
2054
2055         nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
2056         dev->startBlock = 0;
2057         dev->endBlock = nBlocks - 1;
2058         dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2059         dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2060         dev->nReservedBlocks = 5;
2061         dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
2062         dev->inbandTags = options.inband_tags;
2063
2064         /* ... and the functions. */
2065         if (yaffsVersion == 2) {
2066                 dev->writeChunkWithTagsToNAND =
2067                     nandmtd2_WriteChunkWithTagsToNAND;
2068                 dev->readChunkWithTagsFromNAND =
2069                     nandmtd2_ReadChunkWithTagsFromNAND;
2070                 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2071                 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
2072                 dev->spareBuffer = YMALLOC(mtd->oobsize);
2073                 dev->isYaffs2 = 1;
2074 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2075                 dev->totalBytesPerChunk = mtd->writesize;
2076                 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2077 #else
2078                 dev->totalBytesPerChunk = mtd->oobblock;
2079                 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2080 #endif
2081                 nBlocks = mtd->size / mtd->erasesize;
2082
2083                 dev->startBlock = 0;
2084                 dev->endBlock = nBlocks - 1;
2085         } else {
2086 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2087                 /* use the MTD interface in yaffs_mtdif1.c */
2088                 dev->writeChunkWithTagsToNAND =
2089                         nandmtd1_WriteChunkWithTagsToNAND;
2090                 dev->readChunkWithTagsFromNAND =
2091                         nandmtd1_ReadChunkWithTagsFromNAND;
2092                 dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2093                 dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
2094 #else
2095                 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2096                 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2097 #endif
2098                 dev->isYaffs2 = 0;
2099         }
2100         /* ... and common functions */
2101         dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2102         dev->initialiseNAND = nandmtd_InitialiseNAND;
2103
2104         dev->putSuperFunc = yaffs_MTDPutSuper;
2105
2106         dev->superBlock = (void *)sb;
2107         dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2108
2109
2110 #ifndef CONFIG_YAFFS_DOES_ECC
2111         dev->useNANDECC = 1;
2112 #endif
2113
2114 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2115         dev->wideTnodesDisabled = 1;
2116 #endif
2117
2118         dev->skipCheckpointRead = options.skip_checkpoint_read;
2119         dev->skipCheckpointWrite = options.skip_checkpoint_write;
2120
2121         /* we assume this is protected by lock_kernel() in mount/umount */
2122         ylist_add_tail(&dev->devList, &yaffs_dev_list);
2123
2124         init_MUTEX(&dev->grossLock);
2125
2126         yaffs_GrossLock(dev);
2127
2128         err = yaffs_GutsInitialise(dev);
2129
2130         T(YAFFS_TRACE_OS,
2131           ("yaffs_read_super: guts initialised %s\n",
2132            (err == YAFFS_OK) ? "OK" : "FAILED"));
2133
2134         /* Release lock before yaffs_get_inode() */
2135         yaffs_GrossUnlock(dev);
2136
2137         /* Create root inode */
2138         if (err == YAFFS_OK)
2139                 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2140                                         yaffs_Root(dev));
2141
2142         if (!inode)
2143                 return NULL;
2144
2145         inode->i_op = &yaffs_dir_inode_operations;
2146         inode->i_fop = &yaffs_dir_operations;
2147
2148         T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
2149
2150         root = d_alloc_root(inode);
2151
2152         T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
2153
2154         if (!root) {
2155                 iput(inode);
2156                 return NULL;
2157         }
2158         sb->s_root = root;
2159         sb->s_dirt = !dev->isCheckpointed;
2160         T(YAFFS_TRACE_ALWAYS,
2161           ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
2162
2163         T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
2164         return sb;
2165 }
2166
2167
2168 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2169 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2170                                          int silent)
2171 {
2172         return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2173 }
2174
2175 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2176 static int yaffs_read_super(struct file_system_type *fs,
2177                             int flags, const char *dev_name,
2178                             void *data, struct vfsmount *mnt)
2179 {
2180
2181         return get_sb_bdev(fs, flags, dev_name, data,
2182                            yaffs_internal_read_super_mtd, mnt);
2183 }
2184 #else
2185 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2186                                             int flags, const char *dev_name,
2187                                             void *data)
2188 {
2189
2190         return get_sb_bdev(fs, flags, dev_name, data,
2191                            yaffs_internal_read_super_mtd);
2192 }
2193 #endif
2194
2195 static struct file_system_type yaffs_fs_type = {
2196         .owner = THIS_MODULE,
2197         .name = "yaffs",
2198         .get_sb = yaffs_read_super,
2199         .kill_sb = kill_block_super,
2200         .fs_flags = FS_REQUIRES_DEV,
2201 };
2202 #else
2203 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2204                                             int silent)
2205 {
2206         return yaffs_internal_read_super(1, sb, data, silent);
2207 }
2208
2209 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2210                       FS_REQUIRES_DEV);
2211 #endif
2212
2213
2214 #ifdef CONFIG_YAFFS_YAFFS2
2215
2216 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2217 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2218                                           int silent)
2219 {
2220         return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2221 }
2222
2223 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2224 static int yaffs2_read_super(struct file_system_type *fs,
2225                         int flags, const char *dev_name, void *data,
2226                         struct vfsmount *mnt)
2227 {
2228         return get_sb_bdev(fs, flags, dev_name, data,
2229                         yaffs2_internal_read_super_mtd, mnt);
2230 }
2231 #else
2232 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2233                                              int flags, const char *dev_name,
2234                                              void *data)
2235 {
2236
2237         return get_sb_bdev(fs, flags, dev_name, data,
2238                            yaffs2_internal_read_super_mtd);
2239 }
2240 #endif
2241
2242 static struct file_system_type yaffs2_fs_type = {
2243         .owner = THIS_MODULE,
2244         .name = "yaffs2",
2245         .get_sb = yaffs2_read_super,
2246         .kill_sb = kill_block_super,
2247         .fs_flags = FS_REQUIRES_DEV,
2248 };
2249 #else
2250 static struct super_block *yaffs2_read_super(struct super_block *sb,
2251                                              void *data, int silent)
2252 {
2253         return yaffs_internal_read_super(2, sb, data, silent);
2254 }
2255
2256 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2257                       FS_REQUIRES_DEV);
2258 #endif
2259
2260 #endif                          /* CONFIG_YAFFS_YAFFS2 */
2261
2262 static struct proc_dir_entry *my_proc_entry;
2263
2264 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
2265 {
2266         buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
2267         buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
2268         buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
2269         buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2270         buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2271         buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2272         buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2273         buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
2274         buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2275         buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2276         buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2277         buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2278         buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2279         buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2280         buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2281         buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2282         buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2283         buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2284         buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2285         buf += sprintf(buf, "passiveGCs......... %d\n",
2286                     dev->passiveGarbageCollections);
2287         buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2288         buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
2289         buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2290         buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2291         buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2292         buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2293         buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2294         buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2295         buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2296         buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2297         buf +=
2298             sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2299         buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
2300         buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
2301         buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
2302
2303         return buf;
2304 }
2305
2306 static int yaffs_proc_read(char *page,
2307                            char **start,
2308                            off_t offset, int count, int *eof, void *data)
2309 {
2310         struct ylist_head *item;
2311         char *buf = page;
2312         int step = offset;
2313         int n = 0;
2314
2315         /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2316          * We use 'offset' (*ppos) to indicate where we are in devList.
2317          * This also assumes the user has posted a read buffer large
2318          * enough to hold the complete output; but that's life in /proc.
2319          */
2320
2321         *(int *)start = 1;
2322
2323         /* Print header first */
2324         if (step == 0) {
2325                 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2326                                "\n%s\n%s\n", yaffs_fs_c_version,
2327                                yaffs_guts_c_version);
2328         }
2329
2330         /* hold lock_kernel while traversing yaffs_dev_list */
2331         lock_kernel();
2332
2333         /* Locate and print the Nth entry.  Order N-squared but N is small. */
2334         ylist_for_each(item, &yaffs_dev_list) {
2335                 yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
2336                 if (n < step) {
2337                         n++;
2338                         continue;
2339                 }
2340                 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
2341                 buf = yaffs_dump_dev(buf, dev);
2342                 break;
2343         }
2344         unlock_kernel();
2345
2346         return buf - page < count ? buf - page : count;
2347 }
2348
2349 /**
2350  * Set the verbosity of the warnings and error messages.
2351  *
2352  * Note that the names can only be a..z or _ with the current code.
2353  */
2354
2355 static struct {
2356         char *mask_name;
2357         unsigned mask_bitfield;
2358 } mask_flags[] = {
2359         {"allocate", YAFFS_TRACE_ALLOCATE},
2360         {"always", YAFFS_TRACE_ALWAYS},
2361         {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2362         {"buffers", YAFFS_TRACE_BUFFERS},
2363         {"bug", YAFFS_TRACE_BUG},
2364         {"checkpt", YAFFS_TRACE_CHECKPOINT},
2365         {"deletion", YAFFS_TRACE_DELETION},
2366         {"erase", YAFFS_TRACE_ERASE},
2367         {"error", YAFFS_TRACE_ERROR},
2368         {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2369         {"gc", YAFFS_TRACE_GC},
2370         {"mtd", YAFFS_TRACE_MTD},
2371         {"nandaccess", YAFFS_TRACE_NANDACCESS},
2372         {"os", YAFFS_TRACE_OS},
2373         {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2374         {"scan", YAFFS_TRACE_SCAN},
2375         {"tracing", YAFFS_TRACE_TRACING},
2376
2377         {"verify", YAFFS_TRACE_VERIFY},
2378         {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2379         {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2380         {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2381
2382         {"write", YAFFS_TRACE_WRITE},
2383         {"all", 0xffffffff},
2384         {"none", 0},
2385         {NULL, 0},
2386 };
2387
2388 #define MAX_MASK_NAME_LENGTH 40
2389 static int yaffs_proc_write(struct file *file, const char *buf,
2390                                          unsigned long count, void *data)
2391 {
2392         unsigned rg = 0, mask_bitfield;
2393         char *end;
2394         char *mask_name;
2395         const char *x;
2396         char substring[MAX_MASK_NAME_LENGTH+1];
2397         int i;
2398         int done = 0;
2399         int add, len = 0;
2400         int pos = 0;
2401
2402         rg = yaffs_traceMask;
2403
2404         while (!done && (pos < count)) {
2405                 done = 1;
2406                 while ((pos < count) && isspace(buf[pos])) {
2407                         pos++;
2408                 }
2409
2410                 switch (buf[pos]) {
2411                 case '+':
2412                 case '-':
2413                 case '=':
2414                         add = buf[pos];
2415                         pos++;
2416                         break;
2417
2418                 default:
2419                         add = ' ';
2420                         break;
2421                 }
2422                 mask_name = NULL;
2423
2424                 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2425                 if (end > buf + pos) {
2426                         mask_name = "numeral";
2427                         len = end - (buf + pos);
2428                         pos += len;
2429                         done = 0;
2430                 } else {
2431                         for(x = buf + pos, i = 0;
2432                             (*x == '_' || (*x >='a' && *x <= 'z')) &&
2433                             i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2434                             substring[i] = *x;
2435                         substring[i] = '\0';
2436
2437                         for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2438                                 if(strcmp(substring,mask_flags[i].mask_name) == 0){
2439                                         mask_name = mask_flags[i].mask_name;
2440                                         mask_bitfield = mask_flags[i].mask_bitfield;
2441                                         done = 0;
2442                                         break;
2443                                 }
2444                         }
2445                 }
2446
2447                 if (mask_name != NULL) {
2448                         done = 0;
2449                         switch(add) {
2450                         case '-':
2451                                 rg &= ~mask_bitfield;
2452                                 break;
2453                         case '+':
2454                                 rg |= mask_bitfield;
2455                                 break;
2456                         case '=':
2457                                 rg = mask_bitfield;
2458                                 break;
2459                         default:
2460                                 rg |= mask_bitfield;
2461                                 break;
2462                         }
2463                 }
2464         }
2465
2466         yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2467
2468         printk("new trace = 0x%08X\n",yaffs_traceMask);
2469
2470         if (rg & YAFFS_TRACE_ALWAYS) {
2471                 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2472                         char flag;
2473                         flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2474                         printk("%c%s\n", flag, mask_flags[i].mask_name);
2475                 }
2476         }
2477
2478         return count;
2479 }
2480
2481 /* Stuff to handle installation of file systems */
2482 struct file_system_to_install {
2483         struct file_system_type *fst;
2484         int installed;
2485 };
2486
2487 static struct file_system_to_install fs_to_install[] = {
2488 //#ifdef CONFIG_YAFFS_YAFFS1
2489         {&yaffs_fs_type, 0},
2490 //#endif
2491 //#ifdef CONFIG_YAFFS_YAFFS2
2492         {&yaffs2_fs_type, 0},
2493 //#endif
2494         {NULL, 0}
2495 };
2496
2497 static int __init init_yaffs_fs(void)
2498 {
2499         int error = 0;
2500         struct file_system_to_install *fsinst;
2501
2502         T(YAFFS_TRACE_ALWAYS,
2503           ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2504
2505         /* Install the proc_fs entry */
2506         my_proc_entry = create_proc_entry("yaffs",
2507                                                S_IRUGO | S_IFREG,
2508                                                YPROC_ROOT);
2509
2510         if (my_proc_entry) {
2511                 my_proc_entry->write_proc = yaffs_proc_write;
2512                 my_proc_entry->read_proc = yaffs_proc_read;
2513                 my_proc_entry->data = NULL;
2514         } else {
2515                 return -ENOMEM;
2516         }
2517
2518         /* Now add the file system entries */
2519
2520         fsinst = fs_to_install;
2521
2522         while (fsinst->fst && !error) {
2523                 error = register_filesystem(fsinst->fst);
2524                 if (!error) {
2525                         fsinst->installed = 1;
2526                 }
2527                 fsinst++;
2528         }
2529
2530         /* Any errors? uninstall  */
2531         if (error) {
2532                 fsinst = fs_to_install;
2533
2534                 while (fsinst->fst) {
2535                         if (fsinst->installed) {
2536                                 unregister_filesystem(fsinst->fst);
2537                                 fsinst->installed = 0;
2538                         }
2539                         fsinst++;
2540                 }
2541         }
2542
2543         return error;
2544 }
2545
2546 static void __exit exit_yaffs_fs(void)
2547 {
2548
2549         struct file_system_to_install *fsinst;
2550
2551         T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2552                                " removing. \n"));
2553
2554         remove_proc_entry("yaffs", YPROC_ROOT);
2555
2556         fsinst = fs_to_install;
2557
2558         while (fsinst->fst) {
2559                 if (fsinst->installed) {
2560                         unregister_filesystem(fsinst->fst);
2561                         fsinst->installed = 0;
2562                 }
2563                 fsinst++;
2564         }
2565
2566 }
2567
2568 module_init(init_yaffs_fs)
2569 module_exit(exit_yaffs_fs)
2570
2571 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2572 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2573 MODULE_LICENSE("GPL");