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