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