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