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