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