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