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