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