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