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