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