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