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