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