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