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