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