yaffs Move yaffscfg.h to where it should be
[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_DeviceToLC(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_DeviceToLC(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_DeviceToLC(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_DeviceToLC(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_DeviceToLC(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_DeviceToLC(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_DeviceToLC(dev)->readdirProcess = current;
1439
1440         offset = f->f_pos;
1441
1442         sc = yaffs_NewSearch(obj);
1443         if(!sc){
1444                 retVal = -ENOMEM;
1445                 goto 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                         yaffs_GrossLock(dev);
1457                         goto out;
1458                 }
1459                 yaffs_GrossLock(dev);
1460                 offset++;
1461                 f->f_pos++;
1462         }
1463         if (offset == 1) {
1464                 T(YAFFS_TRACE_OS,
1465                         (TSTR("yaffs_readdir: entry .. ino %d \n"),
1466                         (int)f->f_dentry->d_parent->d_inode->i_ino));
1467                 yaffs_GrossUnlock(dev);
1468                 if (filldir(dirent, "..", 2, offset,
1469                         f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
1470                         yaffs_GrossLock(dev);
1471                         goto out;
1472                 }
1473                 yaffs_GrossLock(dev);
1474                 offset++;
1475                 f->f_pos++;
1476         }
1477
1478         curoffs = 1;
1479
1480         /* If the directory has changed since the open or last call to
1481            readdir, rewind to after the 2 canned entries. */
1482         if (f->f_version != inode->i_version) {
1483                 offset = 2;
1484                 f->f_pos = offset;
1485                 f->f_version = inode->i_version;
1486         }
1487
1488         while(sc->nextReturn){
1489                 curoffs++;
1490                 l = sc->nextReturn;
1491                 if (curoffs >= offset) {
1492                         int this_inode = yaffs_GetObjectInode(l);
1493                         int this_type = yaffs_GetObjectType(l);
1494
1495                         yaffs_GetObjectName(l, name,
1496                                             YAFFS_MAX_NAME_LENGTH + 1);
1497                         T(YAFFS_TRACE_OS,
1498                           (TSTR("yaffs_readdir: %s inode %d\n"),
1499                           name, yaffs_GetObjectInode(l)));
1500
1501                         yaffs_GrossUnlock(dev);
1502
1503                         if (filldir(dirent,
1504                                         name,
1505                                         strlen(name),
1506                                         offset,
1507                                         this_inode,
1508                                         this_type) < 0){
1509                                 yaffs_GrossLock(dev);
1510                                 goto out;
1511                         }
1512
1513                         yaffs_GrossLock(dev);
1514
1515                         offset++;
1516                         f->f_pos++;
1517                 }
1518                 yaffs_SearchAdvance(sc);
1519         }
1520
1521 out:
1522         yaffs_EndSearch(sc);
1523         yaffs_DeviceToLC(dev)->readdirProcess = NULL;
1524         yaffs_GrossUnlock(dev);
1525
1526         return retVal;
1527 }
1528
1529
1530
1531 /*
1532  * File creation. Allocate an inode, and we're done..
1533  */
1534
1535 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1536 #define YCRED(x) x
1537 #else
1538 #define YCRED(x) (x->cred)
1539 #endif
1540
1541 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1542 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1543                         dev_t rdev)
1544 #else
1545 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1546                         int rdev)
1547 #endif
1548 {
1549         struct inode *inode;
1550
1551         yaffs_Object *obj = NULL;
1552         yaffs_Device *dev;
1553
1554         yaffs_Object *parent = yaffs_InodeToObject(dir);
1555
1556         int error = -ENOSPC;
1557         uid_t uid = YCRED(current)->fsuid;
1558         gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1559
1560         if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1561                 mode |= S_ISGID;
1562
1563         if (parent) {
1564                 T(YAFFS_TRACE_OS,
1565                         (TSTR("yaffs_mknod: parent object %d type %d\n"),
1566                         parent->objectId, parent->variantType));
1567         } else {
1568                 T(YAFFS_TRACE_OS,
1569                         (TSTR("yaffs_mknod: could not get parent object\n")));
1570                 return -EPERM;
1571         }
1572
1573         T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1574                         "mode %x dev %x\n"),
1575                         dentry->d_name.name, mode, rdev));
1576
1577         dev = parent->myDev;
1578
1579         yaffs_GrossLock(dev);
1580
1581         switch (mode & S_IFMT) {
1582         default:
1583                 /* Special (socket, fifo, device...) */
1584                 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1585 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1586                 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1587                                 gid, old_encode_dev(rdev));
1588 #else
1589                 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1590                                 gid, rdev);
1591 #endif
1592                 break;
1593         case S_IFREG:           /* file          */
1594                 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1595                 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1596                                 gid);
1597                 break;
1598         case S_IFDIR:           /* directory */
1599                 T(YAFFS_TRACE_OS,
1600                         (TSTR("yaffs_mknod: making directory\n")));
1601                 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1602                                         uid, gid);
1603                 break;
1604         case S_IFLNK:           /* symlink */
1605                 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1606                 obj = NULL;     /* Do we ever get here? */
1607                 break;
1608         }
1609
1610         /* Can not call yaffs_get_inode() with gross lock held */
1611         yaffs_GrossUnlock(dev);
1612
1613         if (obj) {
1614                 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1615                 d_instantiate(dentry, inode);
1616                 update_dir_time(dir);
1617                 T(YAFFS_TRACE_OS,
1618                         (TSTR("yaffs_mknod created object %d count = %d\n"),
1619                         obj->objectId, atomic_read(&inode->i_count)));
1620                 error = 0;
1621                 yaffs_FillInodeFromObject(dir,parent);
1622         } else {
1623                 T(YAFFS_TRACE_OS,
1624                         (TSTR("yaffs_mknod failed making object\n")));
1625                 error = -ENOMEM;
1626         }
1627
1628         return error;
1629 }
1630
1631 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1632 {
1633         int retVal;
1634         T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1635         retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1636         return retVal;
1637 }
1638
1639 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1640 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1641                         struct nameidata *n)
1642 #else
1643 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1644 #endif
1645 {
1646         T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1647         return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1648 }
1649
1650 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1651 {
1652         int retVal;
1653
1654         yaffs_Device *dev;
1655         yaffs_Object *obj;
1656
1657         T(YAFFS_TRACE_OS,
1658                 (TSTR("yaffs_unlink %d:%s\n"),
1659                 (int)(dir->i_ino),
1660                 dentry->d_name.name));
1661         obj = yaffs_InodeToObject(dir);
1662         dev = obj->myDev;
1663
1664         yaffs_GrossLock(dev);
1665
1666         retVal = yaffs_Unlink(obj, dentry->d_name.name);
1667
1668         if (retVal == YAFFS_OK) {
1669                 dentry->d_inode->i_nlink--;
1670                 dir->i_version++;
1671                 yaffs_GrossUnlock(dev);
1672                 mark_inode_dirty(dentry->d_inode);
1673                 update_dir_time(dir);
1674                 return 0;
1675         }
1676         yaffs_GrossUnlock(dev);
1677         return -ENOTEMPTY;
1678 }
1679
1680 /*
1681  * Create a link...
1682  */
1683 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1684                         struct dentry *dentry)
1685 {
1686         struct inode *inode = old_dentry->d_inode;
1687         yaffs_Object *obj = NULL;
1688         yaffs_Object *link = NULL;
1689         yaffs_Device *dev;
1690
1691         T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1692
1693         obj = yaffs_InodeToObject(inode);
1694         dev = obj->myDev;
1695
1696         yaffs_GrossLock(dev);
1697
1698         if (!S_ISDIR(inode->i_mode))            /* Don't link directories */
1699                 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1700                         obj);
1701
1702         if (link) {
1703                 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1704                 d_instantiate(dentry, old_dentry->d_inode);
1705                 atomic_inc(&old_dentry->d_inode->i_count);
1706                 T(YAFFS_TRACE_OS,
1707                         (TSTR("yaffs_link link count %d i_count %d\n"),
1708                         old_dentry->d_inode->i_nlink,
1709                         atomic_read(&old_dentry->d_inode->i_count)));
1710         }
1711
1712         yaffs_GrossUnlock(dev);
1713
1714         if (link){
1715                 update_dir_time(dir);
1716                 return 0;
1717         }
1718
1719         return -EPERM;
1720 }
1721
1722 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1723                                 const char *symname)
1724 {
1725         yaffs_Object *obj;
1726         yaffs_Device *dev;
1727         uid_t uid = YCRED(current)->fsuid;
1728         gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1729
1730         T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1731
1732         dev = yaffs_InodeToObject(dir)->myDev;
1733         yaffs_GrossLock(dev);
1734         obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1735                                 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1736         yaffs_GrossUnlock(dev);
1737
1738         if (obj) {
1739                 struct inode *inode;
1740
1741                 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1742                 d_instantiate(dentry, inode);
1743                 update_dir_time(dir);
1744                 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1745                 return 0;
1746         } else {
1747                 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1748         }
1749
1750         return -ENOMEM;
1751 }
1752
1753 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1754 static int yaffs_sync_object(struct file *file, int datasync)
1755 #else
1756 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1757                                 int datasync)
1758 #endif
1759 {
1760
1761         yaffs_Object *obj;
1762         yaffs_Device *dev;
1763 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1764         struct dentry *dentry = file->f_path.dentry;
1765 #endif
1766
1767         obj = yaffs_DentryToObject(dentry);
1768
1769         dev = obj->myDev;
1770
1771         T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1772                 (TSTR("yaffs_sync_object\n")));
1773         yaffs_GrossLock(dev);
1774         yaffs_FlushFile(obj, 1, datasync);
1775         yaffs_GrossUnlock(dev);
1776         return 0;
1777 }
1778
1779 /*
1780  * The VFS layer already does all the dentry stuff for rename.
1781  *
1782  * NB: POSIX says you can rename an object over an old object of the same name
1783  */
1784 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1785                         struct inode *new_dir, struct dentry *new_dentry)
1786 {
1787         yaffs_Device *dev;
1788         int retVal = YAFFS_FAIL;
1789         yaffs_Object *target;
1790
1791         T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1792         dev = yaffs_InodeToObject(old_dir)->myDev;
1793
1794         yaffs_GrossLock(dev);
1795
1796         /* Check if the target is an existing directory that is not empty. */
1797         target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1798                                 new_dentry->d_name.name);
1799
1800
1801
1802         if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1803                 !ylist_empty(&target->variant.directoryVariant.children)) {
1804
1805                 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1806
1807                 retVal = YAFFS_FAIL;
1808         } else {
1809                 /* Now does unlinking internally using shadowing mechanism */
1810                 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_RenameObject\n")));
1811
1812                 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1813                                 old_dentry->d_name.name,
1814                                 yaffs_InodeToObject(new_dir),
1815                                 new_dentry->d_name.name);
1816         }
1817         yaffs_GrossUnlock(dev);
1818
1819         if (retVal == YAFFS_OK) {
1820                 if (target) {
1821                         new_dentry->d_inode->i_nlink--;
1822                         mark_inode_dirty(new_dentry->d_inode);
1823                 }
1824                 
1825                 update_dir_time(old_dir);
1826                 if(old_dir != new_dir)
1827                         update_dir_time(new_dir);
1828                 return 0;
1829         } else {
1830                 return -ENOTEMPTY;
1831         }
1832 }
1833
1834 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1835 {
1836         struct inode *inode = dentry->d_inode;
1837         int error = 0;
1838         yaffs_Device *dev;
1839
1840         T(YAFFS_TRACE_OS,
1841                 (TSTR("yaffs_setattr of object %d\n"),
1842                 yaffs_InodeToObject(inode)->objectId));
1843
1844         /* Fail if a requested resize >= 2GB */         
1845         if (attr->ia_valid & ATTR_SIZE &&
1846                 (attr->ia_size >> 31))
1847                 error = -EINVAL;
1848
1849         if (error == 0)
1850                 error = inode_change_ok(inode, attr);
1851         if (error == 0) {
1852                 int result;
1853                 if (!error){
1854                         error = inode_setattr(inode, attr);
1855                         T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
1856                         if (attr->ia_valid & ATTR_SIZE)
1857                                 truncate_inode_pages(&inode->i_data,attr->ia_size);
1858                 }
1859                 dev = yaffs_InodeToObject(inode)->myDev;
1860                 if (attr->ia_valid & ATTR_SIZE){
1861                         T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
1862                                 (int)(attr->ia_size),(int)(attr->ia_size)));
1863                 }
1864                 yaffs_GrossLock(dev);
1865                 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1866                 if(result == YAFFS_OK) {
1867                         error = 0;
1868                 } else {
1869                         error = -EPERM;
1870                 }
1871                 yaffs_GrossUnlock(dev);
1872
1873         }
1874
1875         T(YAFFS_TRACE_OS,
1876                 (TSTR("yaffs_setattr done returning %d\n"),error));
1877
1878         return error;
1879 }
1880
1881 #ifdef CONFIG_YAFFS_XATTR
1882 int yaffs_setxattr(struct dentry *dentry, const char *name,
1883                         const void *value, size_t size, int flags)
1884 {
1885         struct inode *inode = dentry->d_inode;
1886         int error = 0;
1887         yaffs_Device *dev;
1888         yaffs_Object *obj = yaffs_InodeToObject(inode);
1889
1890         T(YAFFS_TRACE_OS,
1891                 (TSTR("yaffs_setxattr of object %d\n"),
1892                 obj->objectId));
1893
1894
1895         if (error == 0) {
1896                 int result;
1897                 dev = obj->myDev;
1898                 yaffs_GrossLock(dev);
1899                 result = yaffs_SetXAttribute(obj, name, value, size, flags);
1900                 if(result == YAFFS_OK)
1901                         error = 0;
1902                 else if(result < 0)
1903                         error = result;
1904                 yaffs_GrossUnlock(dev);
1905
1906         }
1907         T(YAFFS_TRACE_OS,
1908                 (TSTR("yaffs_setxattr done returning %d\n"),error));
1909
1910         return error;
1911 }
1912
1913
1914 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
1915                         size_t size)
1916 {
1917         struct inode *inode = dentry->d_inode;
1918         int error = 0;
1919         yaffs_Device *dev;
1920         yaffs_Object *obj = yaffs_InodeToObject(inode);
1921
1922         T(YAFFS_TRACE_OS,
1923                 (TSTR("yaffs_getxattr of object %d\n"),
1924                 obj->objectId));
1925
1926
1927         if (error == 0) {
1928                 dev = obj->myDev;
1929                 yaffs_GrossLock(dev);
1930                 error = yaffs_GetXAttribute(obj, name, buff, size);
1931                 yaffs_GrossUnlock(dev);
1932
1933         }
1934         T(YAFFS_TRACE_OS,
1935                 (TSTR("yaffs_getxattr done returning %d\n"),error));
1936
1937         return error;
1938 }
1939
1940 int yaffs_removexattr(struct dentry *dentry, const char *name)
1941 {
1942         struct inode *inode = dentry->d_inode;
1943         int error = 0;
1944         yaffs_Device *dev;
1945         yaffs_Object *obj = yaffs_InodeToObject(inode);
1946
1947         T(YAFFS_TRACE_OS,
1948                 (TSTR("yaffs_removexattr of object %d\n"),
1949                 obj->objectId));
1950
1951
1952         if (error == 0) {
1953                 int result;
1954                 dev = obj->myDev;
1955                 yaffs_GrossLock(dev);
1956                 result = yaffs_RemoveXAttribute(obj, name);
1957                 if(result == YAFFS_OK)
1958                         error = 0;
1959                 else if(result < 0)
1960                         error = result;
1961                 yaffs_GrossUnlock(dev);
1962
1963         }
1964         T(YAFFS_TRACE_OS,
1965                 (TSTR("yaffs_removexattr done returning %d\n"),error));
1966
1967         return error;
1968 }
1969
1970 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
1971 {
1972         struct inode *inode = dentry->d_inode;
1973         int error = 0;
1974         yaffs_Device *dev;
1975         yaffs_Object *obj = yaffs_InodeToObject(inode);
1976
1977         T(YAFFS_TRACE_OS,
1978                 (TSTR("yaffs_listxattr of object %d\n"),
1979                 obj->objectId));
1980
1981
1982         if (error == 0) {
1983                 dev = obj->myDev;
1984                 yaffs_GrossLock(dev);
1985                 error = yaffs_ListXAttributes(obj, buff, size);
1986                 yaffs_GrossUnlock(dev);
1987
1988         }
1989         T(YAFFS_TRACE_OS,
1990                 (TSTR("yaffs_listxattr done returning %d\n"),error));
1991
1992         return error;
1993 }
1994
1995 #endif
1996
1997
1998 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1999 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2000 {
2001         yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2002         struct super_block *sb = dentry->d_sb;
2003 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2004 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2005 {
2006         yaffs_Device *dev = yaffs_SuperToDevice(sb);
2007 #else
2008 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2009 {
2010         yaffs_Device *dev = yaffs_SuperToDevice(sb);
2011 #endif
2012
2013         T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
2014
2015         yaffs_GrossLock(dev);
2016
2017         buf->f_type = YAFFS_MAGIC;
2018         buf->f_bsize = sb->s_blocksize;
2019         buf->f_namelen = 255;
2020
2021         if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
2022                 /* Do this if chunk size is not a power of 2 */
2023
2024                 uint64_t bytesInDev;
2025                 uint64_t bytesFree;
2026
2027                 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
2028                         ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
2029
2030                 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
2031                 buf->f_blocks = bytesInDev;
2032
2033                 bytesFree  = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
2034                         ((uint64_t)(dev->nDataBytesPerChunk));
2035
2036                 do_div(bytesFree, sb->s_blocksize);
2037
2038                 buf->f_bfree = bytesFree;
2039
2040         } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2041
2042                 buf->f_blocks =
2043                         (dev->param.endBlock - dev->param.startBlock + 1) *
2044                         dev->param.nChunksPerBlock /
2045                         (sb->s_blocksize / dev->nDataBytesPerChunk);
2046                 buf->f_bfree =
2047                         yaffs_GetNumberOfFreeChunks(dev) /
2048                         (sb->s_blocksize / dev->nDataBytesPerChunk);
2049         } else {
2050                 buf->f_blocks =
2051                         (dev->param.endBlock - dev->param.startBlock + 1) *
2052                         dev->param.nChunksPerBlock *
2053                         (dev->nDataBytesPerChunk / sb->s_blocksize);
2054
2055                 buf->f_bfree =
2056                         yaffs_GetNumberOfFreeChunks(dev) *
2057                         (dev->nDataBytesPerChunk / sb->s_blocksize);
2058         }
2059
2060         buf->f_files = 0;
2061         buf->f_ffree = 0;
2062         buf->f_bavail = buf->f_bfree;
2063
2064         yaffs_GrossUnlock(dev);
2065         return 0;
2066 }
2067
2068
2069
2070 static void yaffs_FlushInodes(struct super_block *sb)
2071 {
2072         struct inode *iptr;
2073         yaffs_Object *obj;
2074         
2075         list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
2076                 obj = yaffs_InodeToObject(iptr);
2077                 if(obj){
2078                         T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
2079                                 obj->objectId));
2080                         yaffs_FlushFile(obj,1,0);
2081                 }
2082         }
2083 }
2084
2085
2086 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
2087 {
2088         yaffs_Device *dev = yaffs_SuperToDevice(sb);    
2089         if(!dev)
2090                 return;
2091         
2092         yaffs_FlushInodes(sb);
2093         yaffs_UpdateDirtyDirectories(dev);
2094         yaffs_FlushEntireDeviceCache(dev);
2095         if(do_checkpoint)
2096                 yaffs_CheckpointSave(dev);
2097 }
2098
2099
2100 static unsigned yaffs_bg_gc_urgency(yaffs_Device *dev)
2101 {
2102         unsigned erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2103         struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2104         unsigned scatteredFree = 0; /* Free chunks not in an erased block */
2105
2106         if(erasedChunks < dev->nFreeChunks)
2107                 scatteredFree = (dev->nFreeChunks - erasedChunks);
2108
2109         if(!context->bgRunning)
2110                 return 0;
2111         else if(scatteredFree < (dev->param.nChunksPerBlock * 2))
2112                 return 0;
2113         else if(erasedChunks > dev->nFreeChunks/2)
2114                 return 0;
2115         else if(erasedChunks > dev->nFreeChunks/4)
2116                 return 1;
2117         else
2118                 return 2;
2119 }
2120
2121 static int yaffs_do_sync_fs(struct super_block *sb,
2122                                 int request_checkpoint)
2123 {
2124
2125         yaffs_Device *dev = yaffs_SuperToDevice(sb);
2126         unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2127         unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2128         int do_checkpoint;
2129
2130         T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2131                 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
2132                 gc_urgent,
2133                 sb->s_dirt ? "dirty" : "clean",
2134                 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2135                 oneshot_checkpoint ? " one-shot" : "" ));
2136
2137         yaffs_GrossLock(dev);
2138         do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2139                         oneshot_checkpoint) &&
2140                         !dev->isCheckpointed;
2141
2142         if (sb->s_dirt || do_checkpoint) {
2143                 yaffs_FlushSuperBlock(sb, !dev->isCheckpointed && do_checkpoint);
2144                 sb->s_dirt = 0;
2145                 if(oneshot_checkpoint)
2146                         yaffs_auto_checkpoint &= ~4;
2147         }
2148         yaffs_GrossUnlock(dev);
2149
2150         return 0;
2151 }
2152
2153 /*
2154  * yaffs background thread functions .
2155  * yaffs_BackgroundThread() the thread function
2156  * yaffs_BackgroundStart() launches the background thread.
2157  * yaffs_BackgroundStop() cleans up the background thread.
2158  *
2159  * NB: 
2160  * The thread should only run after the yaffs is initialised
2161  * The thread should be stopped before yaffs is unmounted.
2162  * The thread should not do any writing while the fs is in read only.
2163  */
2164
2165 #ifdef YAFFS_COMPILE_BACKGROUND
2166
2167 void yaffs_background_waker(unsigned long data)
2168 {
2169         wake_up_process((struct task_struct *)data);
2170 }
2171
2172 static int yaffs_BackgroundThread(void *data)
2173 {
2174         yaffs_Device *dev = (yaffs_Device *)data;
2175         struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2176         unsigned long now = jiffies;
2177         unsigned long next_dir_update = now;
2178         unsigned long next_gc = now;
2179         unsigned long expires;
2180         unsigned int urgency;
2181
2182         int gcResult;
2183         struct timer_list timer;
2184
2185         T(YAFFS_TRACE_BACKGROUND,
2186                 (TSTR("yaffs_background starting for dev %p\n"),
2187                 (void *)dev));
2188
2189         set_freezable();
2190
2191         while(context->bgRunning){
2192                 T(YAFFS_TRACE_BACKGROUND,
2193                         (TSTR("yaffs_background\n")));
2194
2195                 if(kthread_should_stop())
2196                         break;
2197
2198                 if(try_to_freeze())
2199                         continue;
2200
2201                 yaffs_GrossLock(dev);
2202
2203                 now = jiffies;
2204
2205                 if(time_after(now, next_dir_update)){
2206                         yaffs_UpdateDirtyDirectories(dev);
2207                         next_dir_update = now + HZ;
2208                 }
2209
2210                 if(time_after(now,next_gc)){
2211                         if(!dev->isCheckpointed){
2212                                 urgency = yaffs_bg_gc_urgency(dev);
2213                                 gcResult = yaffs_BackgroundGarbageCollect(dev, urgency);
2214                                 if(urgency > 1)
2215                                         next_gc = now + HZ/20+1;
2216                                 else if(urgency > 0)
2217                                         next_gc = now + HZ/10+1;
2218                                 else
2219                                         next_gc = now + HZ * 2;
2220                         } else /*
2221                                 * gc not running so set to next_dir_update
2222                                 * to cut down on wake ups
2223                                 */
2224                                 next_gc = next_dir_update;
2225                 }
2226                 yaffs_GrossUnlock(dev);
2227 #if 1
2228                 expires = next_dir_update;
2229                 if (time_before(next_gc,expires))
2230                         expires = next_gc;
2231                 if(time_before(expires,now))
2232                         expires = now + HZ;
2233
2234                 init_timer_on_stack(&timer);
2235                 timer.expires = expires+1;
2236                 timer.data = (unsigned long) current;
2237                 timer.function = yaffs_background_waker;
2238
2239                 set_current_state(TASK_INTERRUPTIBLE);
2240                 add_timer(&timer);
2241                 schedule();
2242                 del_timer_sync(&timer);
2243 #else
2244                 msleep(10);
2245 #endif
2246         }
2247
2248         return 0;
2249 }
2250
2251 static int yaffs_BackgroundStart(yaffs_Device *dev)
2252 {
2253         int retval = 0;
2254         struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
2255
2256         context->bgRunning = 1;
2257
2258         context->bgThread = kthread_run(yaffs_BackgroundThread,
2259                                 (void *)dev,"yaffs-bg-%d",context->mount_id);
2260
2261         if(IS_ERR(context->bgThread)){
2262                 retval = PTR_ERR(context->bgThread);
2263                 context->bgThread = NULL;
2264                 context->bgRunning = 0;
2265         }
2266         return retval;
2267 }
2268
2269 static void yaffs_BackgroundStop(yaffs_Device *dev)
2270 {
2271         struct yaffs_LinuxContext *ctxt = yaffs_DeviceToLC(dev);
2272
2273         ctxt->bgRunning = 0;
2274
2275         if( ctxt->bgThread){
2276                 kthread_stop(ctxt->bgThread);
2277                 ctxt->bgThread = NULL;
2278         }
2279 }
2280 #else
2281 static int yaffs_BackgroundThread(void *data)
2282 {
2283         return 0;
2284 }
2285
2286 static int yaffs_BackgroundStart(yaffs_Device *dev)
2287 {
2288         return 0;
2289 }
2290
2291 static void yaffs_BackgroundStop(yaffs_Device *dev)
2292 {
2293 }
2294 #endif
2295
2296
2297 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2298 static void yaffs_write_super(struct super_block *sb)
2299 #else
2300 static int yaffs_write_super(struct super_block *sb)
2301 #endif
2302 {
2303         unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2304
2305         T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2306                 (TSTR("yaffs_write_super%s\n"),
2307                 request_checkpoint ? " checkpt" : ""));
2308
2309         yaffs_do_sync_fs(sb, request_checkpoint);
2310
2311 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2312         return 0;
2313 #endif
2314 }
2315
2316
2317 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2318 static int yaffs_sync_fs(struct super_block *sb, int wait)
2319 #else
2320 static int yaffs_sync_fs(struct super_block *sb)
2321 #endif
2322 {
2323         unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2324
2325         T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2326                 (TSTR("yaffs_sync_fs%s\n"),
2327                 request_checkpoint ? " checkpt" : ""));
2328
2329         yaffs_do_sync_fs(sb, request_checkpoint);
2330
2331         return 0;
2332 }
2333
2334 #ifdef YAFFS_USE_OWN_IGET
2335
2336 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2337 {
2338         struct inode *inode;
2339         yaffs_Object *obj;
2340         yaffs_Device *dev = yaffs_SuperToDevice(sb);
2341
2342         T(YAFFS_TRACE_OS,
2343                 (TSTR("yaffs_iget for %lu\n"), ino));
2344
2345         inode = iget_locked(sb, ino);
2346         if (!inode)
2347                 return ERR_PTR(-ENOMEM);
2348         if (!(inode->i_state & I_NEW))
2349                 return inode;
2350
2351         /* NB This is called as a side effect of other functions, but
2352          * we had to release the lock to prevent deadlocks, so
2353          * need to lock again.
2354          */
2355
2356         yaffs_GrossLock(dev);
2357
2358         obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2359
2360         yaffs_FillInodeFromObject(inode, obj);
2361
2362         yaffs_GrossUnlock(dev);
2363
2364         unlock_new_inode(inode);
2365         return inode;
2366 }
2367
2368 #else
2369
2370 static void yaffs_read_inode(struct inode *inode)
2371 {
2372         /* NB This is called as a side effect of other functions, but
2373          * we had to release the lock to prevent deadlocks, so
2374          * need to lock again.
2375          */
2376
2377         yaffs_Object *obj;
2378         yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2379
2380         T(YAFFS_TRACE_OS,
2381                 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2382
2383         if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2384                 yaffs_GrossLock(dev);
2385
2386         obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2387
2388         yaffs_FillInodeFromObject(inode, obj);
2389
2390         if(current != yaffs_DeviceToLC(dev)->readdirProcess)
2391                 yaffs_GrossUnlock(dev);
2392 }
2393
2394 #endif
2395
2396 static YLIST_HEAD(yaffs_context_list);
2397 struct semaphore yaffs_context_lock;
2398
2399 static void yaffs_put_super(struct super_block *sb)
2400 {
2401         yaffs_Device *dev = yaffs_SuperToDevice(sb);
2402
2403         T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2404
2405         T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2406                 (TSTR("Shutting down yaffs background thread\n")));
2407         yaffs_BackgroundStop(dev);
2408         T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2409                 (TSTR("yaffs background thread shut down\n")));
2410
2411         yaffs_GrossLock(dev);
2412
2413         yaffs_FlushSuperBlock(sb,1);
2414
2415         if (yaffs_DeviceToLC(dev)->putSuperFunc)
2416                 yaffs_DeviceToLC(dev)->putSuperFunc(sb);
2417
2418
2419         yaffs_Deinitialise(dev);
2420
2421         yaffs_GrossUnlock(dev);
2422
2423         down(&yaffs_context_lock);
2424         ylist_del_init(&(yaffs_DeviceToLC(dev)->contextList));
2425         up(&yaffs_context_lock);
2426
2427         if (yaffs_DeviceToLC(dev)->spareBuffer) {
2428                 YFREE(yaffs_DeviceToLC(dev)->spareBuffer);
2429                 yaffs_DeviceToLC(dev)->spareBuffer = NULL;
2430         }
2431
2432         kfree(dev);
2433 }
2434
2435
2436 static void yaffs_MTDPutSuper(struct super_block *sb)
2437 {
2438         struct mtd_info *mtd = yaffs_DeviceToMtd(yaffs_SuperToDevice(sb));
2439
2440         if (mtd->sync)
2441                 mtd->sync(mtd);
2442
2443         put_mtd_device(mtd);
2444 }
2445
2446
2447 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2448 {
2449         struct super_block *sb = yaffs_DeviceToLC(dev)->superBlock;
2450
2451         T(YAFFS_TRACE_OS, (TSTR("yaffs_MarkSuperBlockDirty() sb = %p\n"), sb));
2452         if (sb)
2453                 sb->s_dirt = 1;
2454 }
2455
2456 typedef struct {
2457         int inband_tags;
2458         int skip_checkpoint_read;
2459         int skip_checkpoint_write;
2460         int no_cache;
2461         int tags_ecc_on;
2462         int tags_ecc_overridden;
2463         int lazy_loading_enabled;
2464         int lazy_loading_overridden;
2465         int empty_lost_and_found;
2466         int empty_lost_and_found_overridden;
2467 } yaffs_options;
2468
2469 #define MAX_OPT_LEN 30
2470 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2471 {
2472         char cur_opt[MAX_OPT_LEN + 1];
2473         int p;
2474         int error = 0;
2475
2476         /* Parse through the options which is a comma seperated list */
2477
2478         while (options_str && *options_str && !error) {
2479                 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2480                 p = 0;
2481
2482                 while(*options_str == ',')
2483                         options_str++;
2484
2485                 while (*options_str && *options_str != ',') {
2486                         if (p < MAX_OPT_LEN) {
2487                                 cur_opt[p] = *options_str;
2488                                 p++;
2489                         }
2490                         options_str++;
2491                 }
2492
2493                 if (!strcmp(cur_opt, "inband-tags"))
2494                         options->inband_tags = 1;
2495                 else if (!strcmp(cur_opt, "tags-ecc-off")){
2496                         options->tags_ecc_on = 0;
2497                         options->tags_ecc_overridden=1;
2498                 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2499                         options->tags_ecc_on = 1;
2500                         options->tags_ecc_overridden = 1;
2501                 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2502                         options->lazy_loading_enabled = 0;
2503                         options->lazy_loading_overridden=1;
2504                 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2505                         options->lazy_loading_enabled = 1;
2506                         options->lazy_loading_overridden = 1;
2507                 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2508                         options->empty_lost_and_found = 0;
2509                         options->empty_lost_and_found_overridden=1;
2510                 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2511                         options->empty_lost_and_found = 1;
2512                         options->empty_lost_and_found_overridden=1;
2513                 } else if (!strcmp(cur_opt, "no-cache"))
2514                         options->no_cache = 1;
2515                 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2516                         options->skip_checkpoint_read = 1;
2517                 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2518                         options->skip_checkpoint_write = 1;
2519                 else if (!strcmp(cur_opt, "no-checkpoint")) {
2520                         options->skip_checkpoint_read = 1;
2521                         options->skip_checkpoint_write = 1;
2522                 } else {
2523                         printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2524                                         cur_opt);
2525                         error = 1;
2526                 }
2527         }
2528
2529         return error;
2530 }
2531
2532 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2533                                                 struct super_block *sb,
2534                                                 void *data, int silent)
2535 {
2536         int nBlocks;
2537         struct inode *inode = NULL;
2538         struct dentry *root;
2539         yaffs_Device *dev = 0;
2540         char devname_buf[BDEVNAME_SIZE + 1];
2541         struct mtd_info *mtd;
2542         int err;
2543         char *data_str = (char *)data;
2544         struct yaffs_LinuxContext *context = NULL;
2545         yaffs_DeviceParam *param;
2546
2547         yaffs_options options;
2548
2549         unsigned mount_id;
2550         int found;
2551         struct yaffs_LinuxContext *context_iterator;
2552         struct ylist_head *l;
2553
2554         sb->s_magic = YAFFS_MAGIC;
2555         sb->s_op = &yaffs_super_ops;
2556         sb->s_flags |= MS_NOATIME;
2557
2558 #ifdef YAFFS_COMPILE_EXPORTFS
2559         sb->s_export_op = &yaffs_export_ops;
2560 #endif
2561
2562         if (!sb)
2563                 printk(KERN_INFO "yaffs: sb is NULL\n");
2564         else if (!sb->s_dev)
2565                 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2566         else if (!yaffs_devname(sb, devname_buf))
2567                 printk(KERN_INFO "yaffs: devname is NULL\n");
2568         else
2569                 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2570                        sb->s_dev,
2571                        yaffs_devname(sb, devname_buf));
2572
2573         if (!data_str)
2574                 data_str = "";
2575
2576         printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2577
2578         memset(&options, 0, sizeof(options));
2579
2580         if (yaffs_parse_options(&options, data_str)) {
2581                 /* Option parsing failed */
2582                 return NULL;
2583         }
2584
2585
2586         sb->s_blocksize = PAGE_CACHE_SIZE;
2587         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2588
2589         T(YAFFS_TRACE_OS,
2590                 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffsVersion));
2591         T(YAFFS_TRACE_OS,
2592                 (TSTR("yaffs_read_super: block size %d\n"),
2593                 (int)(sb->s_blocksize)));
2594
2595         T(YAFFS_TRACE_ALWAYS,
2596                 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2597                MAJOR(sb->s_dev), MINOR(sb->s_dev),
2598                yaffs_devname(sb, devname_buf)));
2599
2600         /* Check it's an mtd device..... */
2601         if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2602                 return NULL;    /* This isn't an mtd device */
2603
2604         /* Get the device */
2605         mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2606         if (!mtd) {
2607                 T(YAFFS_TRACE_ALWAYS,
2608                         (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2609                         MINOR(sb->s_dev)));
2610                 return NULL;
2611         }
2612         /* Check it's NAND */
2613         if (mtd->type != MTD_NANDFLASH) {
2614                 T(YAFFS_TRACE_ALWAYS,
2615                         (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2616                         mtd->type));
2617                 return NULL;
2618         }
2619
2620         T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2621         T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2622         T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2623         T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2624         T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2625         T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2626         T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2627         T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2628         T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2629         T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2630 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2631         T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2632 #else
2633         T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2634 #endif
2635
2636 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2637
2638         if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2639                 T(YAFFS_TRACE_ALWAYS,
2640                         (TSTR("yaffs: auto selecting yaffs2\n")));
2641                 yaffsVersion = 2;
2642         }
2643
2644         /* Added NCB 26/5/2006 for completeness */
2645         if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2646                 T(YAFFS_TRACE_ALWAYS,
2647                         (TSTR("yaffs: auto selecting yaffs1\n")));
2648                 yaffsVersion = 1;
2649         }
2650
2651 #endif
2652
2653         if (yaffsVersion == 2) {
2654                 /* Check for version 2 style functions */
2655                 if (!mtd->erase ||
2656                     !mtd->block_isbad ||
2657                     !mtd->block_markbad ||
2658                     !mtd->read ||
2659                     !mtd->write ||
2660 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2661                     !mtd->read_oob || !mtd->write_oob) {
2662 #else
2663                     !mtd->write_ecc ||
2664                     !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2665 #endif
2666                         T(YAFFS_TRACE_ALWAYS,
2667                           (TSTR("yaffs: MTD device does not support required "
2668                            "functions\n")));
2669                         return NULL;
2670                 }
2671
2672                 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2673                     mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2674                     !options.inband_tags) {
2675                         T(YAFFS_TRACE_ALWAYS,
2676                           (TSTR("yaffs: MTD device does not have the "
2677                            "right page sizes\n")));
2678                         return NULL;
2679                 }
2680         } else {
2681                 /* Check for V1 style functions */
2682                 if (!mtd->erase ||
2683                     !mtd->read ||
2684                     !mtd->write ||
2685 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2686                     !mtd->read_oob || !mtd->write_oob) {
2687 #else
2688                     !mtd->write_ecc ||
2689                     !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2690 #endif
2691                         T(YAFFS_TRACE_ALWAYS,
2692                           (TSTR("yaffs: MTD device does not support required "
2693                            "functions\n")));
2694                         return NULL;
2695                 }
2696
2697                 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2698                     mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2699                         T(YAFFS_TRACE_ALWAYS,
2700                           (TSTR("yaffs: MTD device does not support have the "
2701                            "right page sizes\n")));
2702                         return NULL;
2703                 }
2704         }
2705
2706         /* OK, so if we got here, we have an MTD that's NAND and looks
2707          * like it has the right capabilities
2708          * Set the yaffs_Device up for mtd
2709          */
2710
2711         dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2712         context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2713         
2714         if(!dev || !context ){
2715                 if(dev)
2716                         kfree(dev);
2717                 if(context)
2718                         kfree(context);
2719                 dev = NULL;
2720                 context = NULL;
2721         }
2722
2723         if (!dev) {
2724                 /* Deep shit could not allocate device structure */
2725                 T(YAFFS_TRACE_ALWAYS,
2726                   (TSTR("yaffs_read_super: Failed trying to allocate "
2727                    "yaffs_Device. \n")));
2728                 return NULL;
2729         }
2730         memset(dev, 0, sizeof(yaffs_Device));
2731         param = &(dev->param);
2732
2733         memset(context,0,sizeof(struct yaffs_LinuxContext));
2734         dev->osContext = context;
2735         YINIT_LIST_HEAD(&(context->contextList));
2736         context->dev = dev;
2737         context->superBlock = sb;
2738
2739         
2740
2741 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2742         sb->s_fs_info = dev;
2743 #else
2744         sb->u.generic_sbp = dev;
2745 #endif
2746         
2747         dev->driverContext = mtd;
2748         param->name = mtd->name;
2749
2750         /* Set up the memory size parameters.... */
2751
2752         nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2753
2754         param->startBlock = 0;
2755         param->endBlock = nBlocks - 1;
2756         param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2757         param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2758         param->nReservedBlocks = 5;
2759         param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2760         param->inbandTags = options.inband_tags;
2761
2762 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2763         param->disableLazyLoad = 1;
2764 #endif
2765 #ifdef CONFIG_YAFFS_XATTR
2766         param->enableXattr = 1;
2767 #endif
2768         if(options.lazy_loading_overridden)
2769                 param->disableLazyLoad = !options.lazy_loading_enabled;
2770
2771 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2772         param->noTagsECC = 1;
2773 #endif
2774
2775 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2776 #else
2777         param->deferDirectoryUpdate = 1;
2778 #endif
2779
2780         if(options.tags_ecc_overridden)
2781                 param->noTagsECC = !options.tags_ecc_on;
2782
2783 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2784         param->emptyLostAndFound = 1;
2785 #endif
2786
2787 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2788         param->refreshPeriod = 0;
2789 #else
2790         param->refreshPeriod = 500;
2791 #endif
2792
2793         if(options.empty_lost_and_found_overridden)
2794                 param->emptyLostAndFound = options.empty_lost_and_found;
2795
2796         /* ... and the functions. */
2797         if (yaffsVersion == 2) {
2798                 param->writeChunkWithTagsToNAND =
2799                     nandmtd2_WriteChunkWithTagsToNAND;
2800                 param->readChunkWithTagsFromNAND =
2801                     nandmtd2_ReadChunkWithTagsFromNAND;
2802                 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2803                 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2804                 yaffs_DeviceToLC(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2805                 param->isYaffs2 = 1;
2806 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2807                 param->totalBytesPerChunk = mtd->writesize;
2808                 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2809 #else
2810                 param->totalBytesPerChunk = mtd->oobblock;
2811                 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2812 #endif
2813                 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2814
2815                 param->startBlock = 0;
2816                 param->endBlock = nBlocks - 1;
2817         } else {
2818 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2819                 /* use the MTD interface in yaffs_mtdif1.c */
2820                 param->writeChunkWithTagsToNAND =
2821                         nandmtd1_WriteChunkWithTagsToNAND;
2822                 param->readChunkWithTagsFromNAND =
2823                         nandmtd1_ReadChunkWithTagsFromNAND;
2824                 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2825                 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2826 #else
2827                 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2828                 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2829 #endif
2830                 param->isYaffs2 = 0;
2831         }
2832         /* ... and common functions */
2833         param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2834         param->initialiseNAND = nandmtd_InitialiseNAND;
2835
2836         yaffs_DeviceToLC(dev)->putSuperFunc = yaffs_MTDPutSuper;
2837
2838         param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2839         param->gcControl = yaffs_gc_control_callback;
2840
2841         yaffs_DeviceToLC(dev)->superBlock= sb;
2842         
2843
2844 #ifndef CONFIG_YAFFS_DOES_ECC
2845         param->useNANDECC = 1;
2846 #endif
2847
2848 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2849         param->wideTnodesDisabled = 1;
2850 #endif
2851
2852         param->skipCheckpointRead = options.skip_checkpoint_read;
2853         param->skipCheckpointWrite = options.skip_checkpoint_write;
2854
2855         down(&yaffs_context_lock);
2856         /* Get a mount id */
2857         found = 0;
2858         for(mount_id=0; ! found; mount_id++){
2859                 found = 1;
2860                 ylist_for_each(l,&yaffs_context_list){
2861                         context_iterator = ylist_entry(l,struct yaffs_LinuxContext,contextList);
2862                         if(context_iterator->mount_id == mount_id)
2863                                 found = 0;
2864                 }
2865         }
2866         context->mount_id = mount_id;
2867
2868         ylist_add_tail(&(yaffs_DeviceToLC(dev)->contextList), &yaffs_context_list);
2869         up(&yaffs_context_lock);
2870
2871         /* Directory search handling...*/
2872         YINIT_LIST_HEAD(&(yaffs_DeviceToLC(dev)->searchContexts));
2873         param->removeObjectCallback = yaffs_RemoveObjectCallback;
2874
2875         init_MUTEX(&(yaffs_DeviceToLC(dev)->grossLock));
2876
2877         yaffs_GrossLock(dev);
2878
2879         err = yaffs_GutsInitialise(dev);
2880
2881         T(YAFFS_TRACE_OS,
2882           (TSTR("yaffs_read_super: guts initialised %s\n"),
2883            (err == YAFFS_OK) ? "OK" : "FAILED"));
2884            
2885         if(err == YAFFS_OK)
2886                 yaffs_BackgroundStart(dev);
2887                 
2888         if(!context->bgThread)
2889                 param->deferDirectoryUpdate = 0;
2890
2891
2892         /* Release lock before yaffs_get_inode() */
2893         yaffs_GrossUnlock(dev);
2894
2895         /* Create root inode */
2896         if (err == YAFFS_OK)
2897                 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2898                                         yaffs_Root(dev));
2899
2900         if (!inode)
2901                 return NULL;
2902
2903         inode->i_op = &yaffs_dir_inode_operations;
2904         inode->i_fop = &yaffs_dir_operations;
2905
2906         T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
2907
2908         root = d_alloc_root(inode);
2909
2910         T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
2911
2912         if (!root) {
2913                 iput(inode);
2914                 return NULL;
2915         }
2916         sb->s_root = root;
2917         sb->s_dirt = !dev->isCheckpointed;
2918         T(YAFFS_TRACE_ALWAYS,
2919                 (TSTR("yaffs_read_super: isCheckpointed %d\n"),
2920                 dev->isCheckpointed));
2921
2922         T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
2923         return sb;
2924 }
2925
2926
2927 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2928 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2929                                          int silent)
2930 {
2931         return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2932 }
2933
2934 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2935 static int yaffs_read_super(struct file_system_type *fs,
2936                             int flags, const char *dev_name,
2937                             void *data, struct vfsmount *mnt)
2938 {
2939
2940         return get_sb_bdev(fs, flags, dev_name, data,
2941                            yaffs_internal_read_super_mtd, mnt);
2942 }
2943 #else
2944 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2945                                             int flags, const char *dev_name,
2946                                             void *data)
2947 {
2948
2949         return get_sb_bdev(fs, flags, dev_name, data,
2950                            yaffs_internal_read_super_mtd);
2951 }
2952 #endif
2953
2954 static struct file_system_type yaffs_fs_type = {
2955         .owner = THIS_MODULE,
2956         .name = "yaffs",
2957         .get_sb = yaffs_read_super,
2958         .kill_sb = kill_block_super,
2959         .fs_flags = FS_REQUIRES_DEV,
2960 };
2961 #else
2962 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2963                                             int silent)
2964 {
2965         return yaffs_internal_read_super(1, sb, data, silent);
2966 }
2967
2968 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2969                       FS_REQUIRES_DEV);
2970 #endif
2971
2972
2973 #ifdef CONFIG_YAFFS_YAFFS2
2974
2975 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2976 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2977                                           int silent)
2978 {
2979         return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2980 }
2981
2982 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2983 static int yaffs2_read_super(struct file_system_type *fs,
2984                         int flags, const char *dev_name, void *data,
2985                         struct vfsmount *mnt)
2986 {
2987         return get_sb_bdev(fs, flags, dev_name, data,
2988                         yaffs2_internal_read_super_mtd, mnt);
2989 }
2990 #else
2991 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2992                                              int flags, const char *dev_name,
2993                                              void *data)
2994 {
2995
2996         return get_sb_bdev(fs, flags, dev_name, data,
2997                            yaffs2_internal_read_super_mtd);
2998 }
2999 #endif
3000
3001 static struct file_system_type yaffs2_fs_type = {
3002         .owner = THIS_MODULE,
3003         .name = "yaffs2",
3004         .get_sb = yaffs2_read_super,
3005         .kill_sb = kill_block_super,
3006         .fs_flags = FS_REQUIRES_DEV,
3007 };
3008 #else
3009 static struct super_block *yaffs2_read_super(struct super_block *sb,
3010                                              void *data, int silent)
3011 {
3012         return yaffs_internal_read_super(2, sb, data, silent);
3013 }
3014
3015 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3016                       FS_REQUIRES_DEV);
3017 #endif
3018
3019 #endif                          /* CONFIG_YAFFS_YAFFS2 */
3020
3021 static struct proc_dir_entry *my_proc_entry;
3022 static struct proc_dir_entry *debug_proc_entry;
3023
3024 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
3025 {
3026         buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
3027         buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
3028         buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
3029         buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
3030         buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
3031         buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
3032         buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
3033         buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
3034         buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
3035         buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
3036         buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
3037         buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
3038
3039         buf += sprintf(buf, "\n");
3040
3041         return buf;
3042 }
3043
3044
3045 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
3046 {
3047         buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3048         buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3049         buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3050         buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3051         buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3052         buf += sprintf(buf, "\n");
3053         buf += sprintf(buf, "nTnodes............ %d\n", dev->nTnodes);
3054         buf += sprintf(buf, "nObjects........... %d\n", dev->nObjects);
3055         buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3056         buf += sprintf(buf, "\n");
3057         buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
3058         buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
3059         buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
3060         buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
3061         buf += sprintf(buf, "allGCs............. %u\n", dev->allGCs);
3062         buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGCs);
3063         buf += sprintf(buf, "oldestDirtyGCs..... %u\n", dev->oldestDirtyGCs);
3064         buf += sprintf(buf, "backgroundGCs...... %u\n", dev->backgroundGCs);
3065         buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
3066         buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
3067         buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
3068         buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
3069         buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
3070         buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
3071         buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
3072         buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
3073         buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
3074         buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
3075         buf +=
3076             sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
3077
3078         return buf;
3079 }
3080
3081 static int yaffs_proc_read(char *page,
3082                            char **start,
3083                            off_t offset, int count, int *eof, void *data)
3084 {
3085         struct ylist_head *item;
3086         char *buf = page;
3087         int step = offset;
3088         int n = 0;
3089
3090         /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3091          * We use 'offset' (*ppos) to indicate where we are in devList.
3092          * This also assumes the user has posted a read buffer large
3093          * enough to hold the complete output; but that's life in /proc.
3094          */
3095
3096         *(int *)start = 1;
3097
3098         /* Print header first */
3099         if (step == 0)
3100                 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__"\n");
3101         else if (step == 1)
3102                 buf += sprintf(buf,"\n");
3103         else {
3104                 step-=2;
3105                 
3106                 down(&yaffs_context_lock);
3107
3108                 /* Locate and print the Nth entry.  Order N-squared but N is small. */
3109                 ylist_for_each(item, &yaffs_context_list) {
3110                         struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3111                         yaffs_Device *dev = dc->dev;
3112
3113                         if (n < (step & ~1)) {
3114                                 n+=2;
3115                                 continue;
3116                         }
3117                         if((step & 1)==0){
3118                                 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
3119                                 buf = yaffs_dump_dev_part0(buf, dev);
3120                         } else
3121                                 buf = yaffs_dump_dev_part1(buf, dev);
3122                         
3123                         break;
3124                 }
3125                 up(&yaffs_context_lock);
3126         }
3127
3128         return buf - page < count ? buf - page : count;
3129 }
3130
3131 static int yaffs_stats_proc_read(char *page,
3132                                 char **start,
3133                                 off_t offset, int count, int *eof, void *data)
3134 {
3135         struct ylist_head *item;
3136         char *buf = page;
3137         int n = 0;
3138
3139         down(&yaffs_context_lock);
3140
3141         /* Locate and print the Nth entry.  Order N-squared but N is small. */
3142         ylist_for_each(item, &yaffs_context_list) {
3143                 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
3144                 yaffs_Device *dev = dc->dev;
3145
3146                 int erasedChunks;
3147
3148                 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
3149                 
3150                 buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
3151                                 n, dev->nFreeChunks, erasedChunks,
3152                                 dev->backgroundGCs, dev->oldestDirtyGCs,
3153                                 dev->nObjects, dev->nTnodes);
3154         }
3155         up(&yaffs_context_lock);
3156
3157
3158         return buf - page < count ? buf - page : count;
3159 }
3160
3161 /**
3162  * Set the verbosity of the warnings and error messages.
3163  *
3164  * Note that the names can only be a..z or _ with the current code.
3165  */
3166
3167 static struct {
3168         char *mask_name;
3169         unsigned mask_bitfield;
3170 } mask_flags[] = {
3171         {"allocate", YAFFS_TRACE_ALLOCATE},
3172         {"always", YAFFS_TRACE_ALWAYS},
3173         {"background", YAFFS_TRACE_BACKGROUND},
3174         {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3175         {"buffers", YAFFS_TRACE_BUFFERS},
3176         {"bug", YAFFS_TRACE_BUG},
3177         {"checkpt", YAFFS_TRACE_CHECKPOINT},
3178         {"deletion", YAFFS_TRACE_DELETION},
3179         {"erase", YAFFS_TRACE_ERASE},
3180         {"error", YAFFS_TRACE_ERROR},
3181         {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3182         {"gc", YAFFS_TRACE_GC},
3183         {"lock", YAFFS_TRACE_LOCK},
3184         {"mtd", YAFFS_TRACE_MTD},
3185         {"nandaccess", YAFFS_TRACE_NANDACCESS},
3186         {"os", YAFFS_TRACE_OS},
3187         {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3188         {"scan", YAFFS_TRACE_SCAN},
3189         {"tracing", YAFFS_TRACE_TRACING},
3190         {"sync", YAFFS_TRACE_SYNC},
3191         {"write", YAFFS_TRACE_WRITE},
3192
3193         {"verify", YAFFS_TRACE_VERIFY},
3194         {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3195         {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3196         {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3197
3198         {"all", 0xffffffff},
3199         {"none", 0},
3200         {NULL, 0},
3201 };
3202
3203 #define MAX_MASK_NAME_LENGTH 40
3204 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3205                                          unsigned long count, void *data)
3206 {
3207         unsigned rg = 0, mask_bitfield;
3208         char *end;
3209         char *mask_name;
3210         const char *x;
3211         char substring[MAX_MASK_NAME_LENGTH + 1];
3212         int i;
3213         int done = 0;
3214         int add, len = 0;
3215         int pos = 0;
3216
3217         rg = yaffs_traceMask;
3218
3219         while (!done && (pos < count)) {
3220                 done = 1;
3221                 while ((pos < count) && isspace(buf[pos]))
3222                         pos++;
3223
3224                 switch (buf[pos]) {
3225                 case '+':
3226                 case '-':
3227                 case '=':
3228                         add = buf[pos];
3229                         pos++;
3230                         break;
3231
3232                 default:
3233                         add = ' ';
3234                         break;
3235                 }
3236                 mask_name = NULL;
3237
3238                 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3239
3240                 if (end > buf + pos) {
3241                         mask_name = "numeral";
3242                         len = end - (buf + pos);
3243                         pos += len;
3244                         done = 0;
3245                 } else {
3246                         for (x = buf + pos, i = 0;
3247                             (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3248                             i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3249                                 substring[i] = *x;
3250                         substring[i] = '\0';
3251
3252                         for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3253                                 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3254                                         mask_name = mask_flags[i].mask_name;
3255                                         mask_bitfield = mask_flags[i].mask_bitfield;
3256                                         done = 0;
3257                                         break;
3258                                 }
3259                         }
3260                 }
3261
3262                 if (mask_name != NULL) {
3263                         done = 0;
3264                         switch (add) {
3265                         case '-':
3266                                 rg &= ~mask_bitfield;
3267                                 break;
3268                         case '+':
3269                                 rg |= mask_bitfield;
3270                                 break;
3271                         case '=':
3272                                 rg = mask_bitfield;
3273                                 break;
3274                         default:
3275                                 rg |= mask_bitfield;
3276                                 break;
3277                         }
3278                 }
3279         }
3280
3281         yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3282
3283         printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
3284
3285         if (rg & YAFFS_TRACE_ALWAYS) {
3286                 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3287                         char flag;
3288                         flag = ((rg & mask_flags[i].mask_bitfield) ==
3289                                 mask_flags[i].mask_bitfield) ? '+' : '-';
3290                         printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3291                 }
3292         }
3293
3294         return count;
3295 }
3296
3297
3298 static int yaffs_proc_write(struct file *file, const char *buf,
3299                                          unsigned long count, void *data)
3300 {
3301         return yaffs_proc_write_trace_options(file, buf, count, data);
3302 }
3303
3304 /* Stuff to handle installation of file systems */
3305 struct file_system_to_install {
3306         struct file_system_type *fst;
3307         int installed;
3308 };
3309
3310 static struct file_system_to_install fs_to_install[] = {
3311         {&yaffs_fs_type, 0},
3312         {&yaffs2_fs_type, 0},
3313         {NULL, 0}
3314 };
3315
3316 static int __init init_yaffs_fs(void)
3317 {
3318         int error = 0;
3319         struct file_system_to_install *fsinst;
3320
3321         T(YAFFS_TRACE_ALWAYS,
3322           (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3323
3324 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3325         T(YAFFS_TRACE_ALWAYS,
3326           (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3327 #endif
3328
3329
3330
3331
3332         init_MUTEX(&yaffs_context_lock);
3333
3334         /* Install the proc_fs entries */
3335         my_proc_entry = create_proc_entry("yaffs",
3336                                                S_IRUGO | S_IFREG,
3337                                                YPROC_ROOT);
3338
3339         if (my_proc_entry) {
3340                 my_proc_entry->write_proc = yaffs_proc_write;
3341                 my_proc_entry->read_proc = yaffs_proc_read;
3342                 my_proc_entry->data = NULL;
3343         } else
3344                 return -ENOMEM;
3345
3346         debug_proc_entry = create_proc_entry("yaffs_stats",
3347                                                S_IRUGO | S_IFREG,
3348                                                YPROC_ROOT);
3349
3350         if (debug_proc_entry) {
3351                 debug_proc_entry->write_proc = NULL;
3352                 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3353                 debug_proc_entry->data = NULL;
3354         } else
3355                 return -ENOMEM;
3356
3357         /* Now add the file system entries */
3358
3359         fsinst = fs_to_install;
3360
3361         while (fsinst->fst && !error) {
3362                 error = register_filesystem(fsinst->fst);
3363                 if (!error)
3364                         fsinst->installed = 1;
3365                 fsinst++;
3366         }
3367
3368         /* Any errors? uninstall  */
3369         if (error) {
3370                 fsinst = fs_to_install;
3371
3372                 while (fsinst->fst) {
3373                         if (fsinst->installed) {
3374                                 unregister_filesystem(fsinst->fst);
3375                                 fsinst->installed = 0;
3376                         }
3377                         fsinst++;
3378                 }
3379         }
3380
3381         return error;
3382 }
3383
3384 static void __exit exit_yaffs_fs(void)
3385 {
3386
3387         struct file_system_to_install *fsinst;
3388
3389         T(YAFFS_TRACE_ALWAYS,
3390                 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3391
3392         remove_proc_entry("yaffs", YPROC_ROOT);
3393         remove_proc_entry("yaffs_stats", YPROC_ROOT);
3394
3395         fsinst = fs_to_install;
3396
3397         while (fsinst->fst) {
3398                 if (fsinst->installed) {
3399                         unregister_filesystem(fsinst->fst);
3400                         fsinst->installed = 0;
3401                 }
3402                 fsinst++;
3403         }
3404 }
3405
3406 module_init(init_yaffs_fs)
3407 module_exit(exit_yaffs_fs)
3408
3409 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3410 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3411 MODULE_LICENSE("GPL");