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