yaffs: Refactor bit counting code
[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                                 /*
2339                                  * gc not running so set to next_dir_update
2340                                  * to cut down on wake ups
2341                                  */
2342                                 next_gc = next_dir_update;
2343                         }
2344                 }
2345                 yaffs_gross_unlock(dev);
2346 #if 1
2347                 expires = next_dir_update;
2348                 if (time_before(next_gc, expires))
2349                         expires = next_gc;
2350                 if (time_before(expires, now))
2351                         expires = now + HZ;
2352
2353                 Y_INIT_TIMER(&timer);
2354                 timer.expires = expires + 1;
2355                 timer.data = (unsigned long)current;
2356                 timer.function = yaffs_background_waker;
2357
2358                 set_current_state(TASK_INTERRUPTIBLE);
2359                 add_timer(&timer);
2360                 schedule();
2361                 del_timer_sync(&timer);
2362 #else
2363                 msleep(10);
2364 #endif
2365         }
2366
2367         return 0;
2368 }
2369
2370 static int yaffs_bg_start(struct yaffs_dev *dev)
2371 {
2372         int retval = 0;
2373         struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2374
2375         if (dev->read_only)
2376                 return -1;
2377
2378         context->bg_running = 1;
2379
2380         context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2381                                          (void *)dev, "yaffs-bg-%d",
2382                                          context->mount_id);
2383
2384         if (IS_ERR(context->bg_thread)) {
2385                 retval = PTR_ERR(context->bg_thread);
2386                 context->bg_thread = NULL;
2387                 context->bg_running = 0;
2388         }
2389         return retval;
2390 }
2391
2392 static void yaffs_bg_stop(struct yaffs_dev *dev)
2393 {
2394         struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2395
2396         ctxt->bg_running = 0;
2397
2398         if (ctxt->bg_thread) {
2399                 kthread_stop(ctxt->bg_thread);
2400                 ctxt->bg_thread = NULL;
2401         }
2402 }
2403 #else
2404 static int yaffs_bg_thread_fn(void *data)
2405 {
2406         return 0;
2407 }
2408
2409 static int yaffs_bg_start(struct yaffs_dev *dev)
2410 {
2411         return 0;
2412 }
2413
2414 static void yaffs_bg_stop(struct yaffs_dev *dev)
2415 {
2416 }
2417 #endif
2418
2419 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2420 static void yaffs_write_super(struct super_block *sb)
2421 #else
2422 static int yaffs_write_super(struct super_block *sb)
2423 #endif
2424 {
2425         unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2426
2427         T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2428           (TSTR("yaffs_write_super%s\n"),
2429            request_checkpoint ? " checkpt" : ""));
2430
2431         yaffs_do_sync_fs(sb, request_checkpoint);
2432
2433 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2434         return 0;
2435 #endif
2436 }
2437
2438 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2439 static int yaffs_sync_fs(struct super_block *sb, int wait)
2440 #else
2441 static int yaffs_sync_fs(struct super_block *sb)
2442 #endif
2443 {
2444         unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2445
2446         T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2447           (TSTR("yaffs_sync_fs%s\n"), request_checkpoint ? " checkpt" : ""));
2448
2449         yaffs_do_sync_fs(sb, request_checkpoint);
2450
2451         return 0;
2452 }
2453
2454 #ifdef YAFFS_USE_OWN_IGET
2455
2456 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2457 {
2458         struct inode *inode;
2459         struct yaffs_obj *obj;
2460         struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2461
2462         T(YAFFS_TRACE_OS, (TSTR("yaffs_iget for %lu\n"), ino));
2463
2464         inode = iget_locked(sb, ino);
2465         if (!inode)
2466                 return ERR_PTR(-ENOMEM);
2467         if (!(inode->i_state & I_NEW))
2468                 return inode;
2469
2470         /* NB This is called as a side effect of other functions, but
2471          * we had to release the lock to prevent deadlocks, so
2472          * need to lock again.
2473          */
2474
2475         yaffs_gross_lock(dev);
2476
2477         obj = yaffs_find_by_number(dev, inode->i_ino);
2478
2479         yaffs_fill_inode_from_obj(inode, obj);
2480
2481         yaffs_gross_unlock(dev);
2482
2483         unlock_new_inode(inode);
2484         return inode;
2485 }
2486
2487 #else
2488
2489 static void yaffs_read_inode(struct inode *inode)
2490 {
2491         /* NB This is called as a side effect of other functions, but
2492          * we had to release the lock to prevent deadlocks, so
2493          * need to lock again.
2494          */
2495
2496         struct yaffs_obj *obj;
2497         struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
2498
2499         T(YAFFS_TRACE_OS,
2500           (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2501
2502         if (current != yaffs_dev_to_lc(dev)->readdir_process)
2503                 yaffs_gross_lock(dev);
2504
2505         obj = yaffs_find_by_number(dev, inode->i_ino);
2506
2507         yaffs_fill_inode_from_obj(inode, obj);
2508
2509         if (current != yaffs_dev_to_lc(dev)->readdir_process)
2510                 yaffs_gross_unlock(dev);
2511 }
2512
2513 #endif
2514
2515 static LIST_HEAD(yaffs_context_list);
2516 struct mutex yaffs_context_lock;
2517
2518 static void yaffs_put_super(struct super_block *sb)
2519 {
2520         struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2521
2522         T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2523
2524         T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2525           (TSTR("Shutting down yaffs background thread\n")));
2526         yaffs_bg_stop(dev);
2527         T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2528           (TSTR("yaffs background thread shut down\n")));
2529
2530         yaffs_gross_lock(dev);
2531
2532         yaffs_flush_super(sb, 1);
2533
2534         if (yaffs_dev_to_lc(dev)->put_super_fn)
2535                 yaffs_dev_to_lc(dev)->put_super_fn(sb);
2536
2537         yaffs_deinitialise(dev);
2538
2539         yaffs_gross_unlock(dev);
2540
2541         mutex_lock(&yaffs_context_lock);
2542         list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2543         mutex_unlock(&yaffs_context_lock);
2544
2545         if (yaffs_dev_to_lc(dev)->spare_buffer) {
2546                 YFREE(yaffs_dev_to_lc(dev)->spare_buffer);
2547                 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2548         }
2549
2550         kfree(dev);
2551 }
2552
2553 static void yaffs_mtd_put_super(struct super_block *sb)
2554 {
2555         struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
2556
2557         if (mtd->sync)
2558                 mtd->sync(mtd);
2559
2560         put_mtd_device(mtd);
2561 }
2562
2563 static void yaffs_touch_super(struct yaffs_dev *dev)
2564 {
2565         struct super_block *sb = yaffs_dev_to_lc(dev)->super;
2566
2567         T(YAFFS_TRACE_OS, (TSTR("yaffs_touch_super() sb = %p\n"), sb));
2568         if (sb)
2569                 sb->s_dirt = 1;
2570 }
2571
2572 struct yaffs_options {
2573         int inband_tags;
2574         int skip_checkpoint_read;
2575         int skip_checkpoint_write;
2576         int no_cache;
2577         int tags_ecc_on;
2578         int tags_ecc_overridden;
2579         int lazy_loading_enabled;
2580         int lazy_loading_overridden;
2581         int empty_lost_and_found;
2582         int empty_lost_and_found_overridden;
2583 };
2584
2585 #define MAX_OPT_LEN 30
2586 static int yaffs_parse_options(struct yaffs_options *options,
2587                                const char *options_str)
2588 {
2589         char cur_opt[MAX_OPT_LEN + 1];
2590         int p;
2591         int error = 0;
2592
2593         /* Parse through the options which is a comma seperated list */
2594
2595         while (options_str && *options_str && !error) {
2596                 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2597                 p = 0;
2598
2599                 while (*options_str == ',')
2600                         options_str++;
2601
2602                 while (*options_str && *options_str != ',') {
2603                         if (p < MAX_OPT_LEN) {
2604                                 cur_opt[p] = *options_str;
2605                                 p++;
2606                         }
2607                         options_str++;
2608                 }
2609
2610                 if (!strcmp(cur_opt, "inband-tags")) {
2611                         options->inband_tags = 1;
2612                 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2613                         options->tags_ecc_on = 0;
2614                         options->tags_ecc_overridden = 1;
2615                 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2616                         options->tags_ecc_on = 1;
2617                         options->tags_ecc_overridden = 1;
2618                 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2619                         options->lazy_loading_enabled = 0;
2620                         options->lazy_loading_overridden = 1;
2621                 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2622                         options->lazy_loading_enabled = 1;
2623                         options->lazy_loading_overridden = 1;
2624                 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2625                         options->empty_lost_and_found = 0;
2626                         options->empty_lost_and_found_overridden = 1;
2627                 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2628                         options->empty_lost_and_found = 1;
2629                         options->empty_lost_and_found_overridden = 1;
2630                 } else if (!strcmp(cur_opt, "no-cache")) {
2631                         options->no_cache = 1;
2632                 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2633                         options->skip_checkpoint_read = 1;
2634                 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2635                         options->skip_checkpoint_write = 1;
2636                 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2637                         options->skip_checkpoint_read = 1;
2638                         options->skip_checkpoint_write = 1;
2639                 } else {
2640                         printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2641                                cur_opt);
2642                         error = 1;
2643                 }
2644         }
2645
2646         return error;
2647 }
2648
2649 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2650                                                      struct super_block *sb,
2651                                                      void *data, int silent)
2652 {
2653         int n_blocks;
2654         struct inode *inode = NULL;
2655         struct dentry *root;
2656         struct yaffs_dev *dev = 0;
2657         char devname_buf[BDEVNAME_SIZE + 1];
2658         struct mtd_info *mtd;
2659         int err;
2660         char *data_str = (char *)data;
2661         struct yaffs_linux_context *context = NULL;
2662         struct yaffs_param *param;
2663
2664         int read_only = 0;
2665
2666         struct yaffs_options options;
2667
2668         unsigned mount_id;
2669         int found;
2670         struct yaffs_linux_context *context_iterator;
2671         struct list_head *l;
2672
2673         sb->s_magic = YAFFS_MAGIC;
2674         sb->s_op = &yaffs_super_ops;
2675         sb->s_flags |= MS_NOATIME;
2676
2677         read_only = ((sb->s_flags & MS_RDONLY) != 0);
2678
2679 #ifdef YAFFS_COMPILE_EXPORTFS
2680         sb->s_export_op = &yaffs_export_ops;
2681 #endif
2682
2683         if (!sb)
2684                 printk(KERN_INFO "yaffs: sb is NULL\n");
2685         else if (!sb->s_dev)
2686                 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2687         else if (!yaffs_devname(sb, devname_buf))
2688                 printk(KERN_INFO "yaffs: devname is NULL\n");
2689         else
2690                 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2691                        sb->s_dev,
2692                        yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2693
2694         if (!data_str)
2695                 data_str = "";
2696
2697         printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2698
2699         memset(&options, 0, sizeof(options));
2700
2701         if (yaffs_parse_options(&options, data_str)) {
2702                 /* Option parsing failed */
2703                 return NULL;
2704         }
2705
2706         sb->s_blocksize = PAGE_CACHE_SIZE;
2707         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2708
2709         T(YAFFS_TRACE_OS,
2710           (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffs_version));
2711         T(YAFFS_TRACE_OS,
2712           (TSTR("yaffs_read_super: block size %d\n"), (int)(sb->s_blocksize)));
2713
2714         T(YAFFS_TRACE_ALWAYS,
2715           (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2716            MAJOR(sb->s_dev), MINOR(sb->s_dev), yaffs_devname(sb, devname_buf)));
2717
2718         /* Check it's an mtd device..... */
2719         if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2720                 return NULL;    /* This isn't an mtd device */
2721
2722         /* Get the device */
2723         mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2724         if (!mtd) {
2725                 T(YAFFS_TRACE_ALWAYS,
2726                   (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2727                    MINOR(sb->s_dev)));
2728                 return NULL;
2729         }
2730         /* Check it's NAND */
2731         if (mtd->type != MTD_NANDFLASH) {
2732                 T(YAFFS_TRACE_ALWAYS,
2733                   (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2734                    mtd->type));
2735                 return NULL;
2736         }
2737
2738         T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2739         T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2740         T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2741         T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2742         T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2743         T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2744         T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2745         T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2746         T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2747         T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2748 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2749         T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2750 #else
2751         T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2752 #endif
2753
2754 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2755
2756         if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2757                 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs2\n")));
2758                 yaffs_version = 2;
2759         }
2760
2761         /* Added NCB 26/5/2006 for completeness */
2762         if (yaffs_version == 2 && !options.inband_tags
2763             && WRITE_SIZE(mtd) == 512) {
2764                 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs1\n")));
2765                 yaffs_version = 1;
2766         }
2767 #endif
2768
2769         if (yaffs_version == 2) {
2770                 /* Check for version 2 style functions */
2771                 if (!mtd->erase ||
2772                     !mtd->block_isbad ||
2773                     !mtd->block_markbad || !mtd->read || !mtd->write ||
2774 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2775                     !mtd->read_oob || !mtd->write_oob) {
2776 #else
2777                     !mtd->write_ecc ||
2778                     !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2779 #endif
2780                         T(YAFFS_TRACE_ALWAYS,
2781                           (TSTR("yaffs: MTD device does not support required "
2782                                 "functions\n")));
2783                         return NULL;
2784                 }
2785
2786                 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2787                      mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2788                     !options.inband_tags) {
2789                         T(YAFFS_TRACE_ALWAYS,
2790                           (TSTR("yaffs: MTD device does not have the "
2791                                 "right page sizes\n")));
2792                         return NULL;
2793                 }
2794         } else {
2795                 /* Check for V1 style functions */
2796                 if (!mtd->erase || !mtd->read || !mtd->write ||
2797 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2798                     !mtd->read_oob || !mtd->write_oob) {
2799 #else
2800                     !mtd->write_ecc ||
2801                     !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2802 #endif
2803                         T(YAFFS_TRACE_ALWAYS,
2804                           (TSTR("yaffs: MTD device does not support required "
2805                                 "functions\n")));
2806                         return NULL;
2807                 }
2808
2809                 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2810                     mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2811                         T(YAFFS_TRACE_ALWAYS,
2812                           (TSTR("yaffs: MTD device does not support have the "
2813                                 "right page sizes\n")));
2814                         return NULL;
2815                 }
2816         }
2817
2818         /* OK, so if we got here, we have an MTD that's NAND and looks
2819          * like it has the right capabilities
2820          * Set the struct yaffs_dev up for mtd
2821          */
2822
2823         if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2824                 read_only = 1;
2825                 printk(KERN_INFO
2826                        "yaffs: mtd is read only, setting superblock read only");
2827                 sb->s_flags |= MS_RDONLY;
2828         }
2829
2830         dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2831         context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2832
2833         if (!dev || !context) {
2834                 if (dev)
2835                         kfree(dev);
2836                 if (context)
2837                         kfree(context);
2838                 dev = NULL;
2839                 context = NULL;
2840         }
2841
2842         if (!dev) {
2843                 /* Deep shit could not allocate device structure */
2844                 T(YAFFS_TRACE_ALWAYS,
2845                   (TSTR("yaffs_read_super: Failed trying to allocate "
2846                         "struct yaffs_dev. \n")));
2847                 return NULL;
2848         }
2849         memset(dev, 0, sizeof(struct yaffs_dev));
2850         param = &(dev->param);
2851
2852         memset(context, 0, sizeof(struct yaffs_linux_context));
2853         dev->os_context = context;
2854         INIT_LIST_HEAD(&(context->context_list));
2855         context->dev = dev;
2856         context->super = sb;
2857
2858         dev->read_only = read_only;
2859
2860 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2861         sb->s_fs_info = dev;
2862 #else
2863         sb->u.generic_sbp = dev;
2864 #endif
2865
2866         dev->driver_context = mtd;
2867         param->name = mtd->name;
2868
2869         /* Set up the memory size parameters.... */
2870
2871         n_blocks =
2872             YCALCBLOCKS(mtd->size,
2873                         (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2874
2875         param->start_block = 0;
2876         param->end_block = n_blocks - 1;
2877         param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2878         param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2879         param->n_reserved_blocks = 5;
2880         param->n_caches = (options.no_cache) ? 0 : 10;
2881         param->inband_tags = options.inband_tags;
2882
2883 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2884         param->disable_lazy_load = 1;
2885 #endif
2886 #ifdef CONFIG_YAFFS_XATTR
2887         param->enable_xattr = 1;
2888 #endif
2889         if (options.lazy_loading_overridden)
2890                 param->disable_lazy_load = !options.lazy_loading_enabled;
2891
2892 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2893         param->no_tags_ecc = 1;
2894 #endif
2895
2896 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2897 #else
2898         param->defered_dir_update = 1;
2899 #endif
2900
2901         if (options.tags_ecc_overridden)
2902                 param->no_tags_ecc = !options.tags_ecc_on;
2903
2904 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2905         param->empty_lost_n_found = 1;
2906 #endif
2907
2908 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2909         param->refresh_period = 0;
2910 #else
2911         param->refresh_period = 500;
2912 #endif
2913
2914 #ifdef CONFIG_YAFFS__ALWAYS_CHECK_CHUNK_ERASED
2915         param->always_check_erased = 1;
2916 #endif
2917
2918         if (options.empty_lost_and_found_overridden)
2919                 param->empty_lost_n_found = options.empty_lost_and_found;
2920
2921         /* ... and the functions. */
2922         if (yaffs_version == 2) {
2923                 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2924                 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2925                 param->bad_block_fn = nandmtd2_mark_block_bad;
2926                 param->query_block_fn = nandmtd2_query_block;
2927                 yaffs_dev_to_lc(dev)->spare_buffer = YMALLOC(mtd->oobsize);
2928                 param->is_yaffs2 = 1;
2929 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2930                 param->total_bytes_per_chunk = mtd->writesize;
2931                 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2932 #else
2933                 param->total_bytes_per_chunk = mtd->oobblock;
2934                 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
2935 #endif
2936                 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2937
2938                 param->start_block = 0;
2939                 param->end_block = n_blocks - 1;
2940         } else {
2941 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2942                 /* use the MTD interface in yaffs_mtdif1.c */
2943                 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2944                 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2945                 param->bad_block_fn = nandmtd1_mark_block_bad;
2946                 param->query_block_fn = nandmtd1_query_block;
2947 #else
2948                 param->write_chunk_fn = nandmtd_write_chunk;
2949                 param->read_chunk_fn = nandmtd_read_chunk;
2950 #endif
2951                 param->is_yaffs2 = 0;
2952         }
2953         /* ... and common functions */
2954         param->erase_fn = nandmtd_erase_block;
2955         param->initialise_flash_fn = nandmtd_initialise;
2956
2957         yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2958
2959         param->sb_dirty_fn = yaffs_touch_super;
2960         param->gc_control = yaffs_gc_control_callback;
2961
2962         yaffs_dev_to_lc(dev)->super = sb;
2963
2964 #ifndef CONFIG_YAFFS_DOES_ECC
2965         param->use_nand_ecc = 1;
2966 #endif
2967
2968 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2969         param->wide_tnodes_disabled = 1;
2970 #endif
2971
2972         param->skip_checkpt_rd = options.skip_checkpoint_read;
2973         param->skip_checkpt_wr = options.skip_checkpoint_write;
2974
2975         mutex_lock(&yaffs_context_lock);
2976         /* Get a mount id */
2977         found = 0;
2978         for (mount_id = 0; !found; mount_id++) {
2979                 found = 1;
2980                 list_for_each(l, &yaffs_context_list) {
2981                         context_iterator =
2982                             list_entry(l, struct yaffs_linux_context,
2983                                        context_list);
2984                         if (context_iterator->mount_id == mount_id)
2985                                 found = 0;
2986                 }
2987         }
2988         context->mount_id = mount_id;
2989
2990         list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2991                       &yaffs_context_list);
2992         mutex_unlock(&yaffs_context_lock);
2993
2994         /* Directory search handling... */
2995         INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2996         param->remove_obj_fn = yaffs_remove_obj_callback;
2997
2998         mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2999
3000         yaffs_gross_lock(dev);
3001
3002         err = yaffs_guts_initialise(dev);
3003
3004         T(YAFFS_TRACE_OS,
3005           (TSTR("yaffs_read_super: guts initialised %s\n"),
3006            (err == YAFFS_OK) ? "OK" : "FAILED"));
3007
3008         if (err == YAFFS_OK)
3009                 yaffs_bg_start(dev);
3010
3011         if (!context->bg_thread)
3012                 param->defered_dir_update = 0;
3013
3014         /* Release lock before yaffs_get_inode() */
3015         yaffs_gross_unlock(dev);
3016
3017         /* Create root inode */
3018         if (err == YAFFS_OK)
3019                 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
3020
3021         if (!inode)
3022                 return NULL;
3023
3024         inode->i_op = &yaffs_dir_inode_operations;
3025         inode->i_fop = &yaffs_dir_operations;
3026
3027         T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
3028
3029         root = d_alloc_root(inode);
3030
3031         T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
3032
3033         if (!root) {
3034                 iput(inode);
3035                 return NULL;
3036         }
3037         sb->s_root = root;
3038         sb->s_dirt = !dev->is_checkpointed;
3039         T(YAFFS_TRACE_ALWAYS,
3040           (TSTR("yaffs_read_super: is_checkpointed %d\n"),
3041            dev->is_checkpointed));
3042
3043         T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
3044         return sb;
3045 }
3046
3047 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3048 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3049                                          int silent)
3050 {
3051         return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3052 }
3053
3054 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3055 static int yaffs_read_super(struct file_system_type *fs,
3056                             int flags, const char *dev_name,
3057                             void *data, struct vfsmount *mnt)
3058 {
3059
3060         return get_sb_bdev(fs, flags, dev_name, data,
3061                            yaffs_internal_read_super_mtd, mnt);
3062 }
3063 #else
3064 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3065                                             int flags, const char *dev_name,
3066                                             void *data)
3067 {
3068
3069         return get_sb_bdev(fs, flags, dev_name, data,
3070                            yaffs_internal_read_super_mtd);
3071 }
3072 #endif
3073
3074 static struct file_system_type yaffs_fs_type = {
3075         .owner = THIS_MODULE,
3076         .name = "yaffs",
3077         .get_sb = yaffs_read_super,
3078         .kill_sb = kill_block_super,
3079         .fs_flags = FS_REQUIRES_DEV,
3080 };
3081 #else
3082 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3083                                             int silent)
3084 {
3085         return yaffs_internal_read_super(1, sb, data, silent);
3086 }
3087
3088 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3089                       FS_REQUIRES_DEV);
3090 #endif
3091
3092 #ifdef CONFIG_YAFFS_YAFFS2
3093
3094 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3095 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3096                                           int silent)
3097 {
3098         return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3099 }
3100
3101 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3102 static int yaffs2_read_super(struct file_system_type *fs,
3103                              int flags, const char *dev_name, void *data,
3104                              struct vfsmount *mnt)
3105 {
3106         return get_sb_bdev(fs, flags, dev_name, data,
3107                            yaffs2_internal_read_super_mtd, mnt);
3108 }
3109 #else
3110 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3111                                              int flags, const char *dev_name,
3112                                              void *data)
3113 {
3114
3115         return get_sb_bdev(fs, flags, dev_name, data,
3116                            yaffs2_internal_read_super_mtd);
3117 }
3118 #endif
3119
3120 static struct file_system_type yaffs2_fs_type = {
3121         .owner = THIS_MODULE,
3122         .name = "yaffs2",
3123         .get_sb = yaffs2_read_super,
3124         .kill_sb = kill_block_super,
3125         .fs_flags = FS_REQUIRES_DEV,
3126 };
3127 #else
3128 static struct super_block *yaffs2_read_super(struct super_block *sb,
3129                                              void *data, int silent)
3130 {
3131         return yaffs_internal_read_super(2, sb, data, silent);
3132 }
3133
3134 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3135                       FS_REQUIRES_DEV);
3136 #endif
3137
3138 #endif /* CONFIG_YAFFS_YAFFS2 */
3139
3140 static struct proc_dir_entry *my_proc_entry;
3141 static struct proc_dir_entry *debug_proc_entry;
3142
3143 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3144 {
3145         buf +=
3146             sprintf(buf, "start_block.......... %d\n", dev->param.start_block);
3147         buf += sprintf(buf, "end_block............ %d\n", dev->param.end_block);
3148         buf +=
3149             sprintf(buf, "total_bytes_per_chunk %d\n",
3150                     dev->param.total_bytes_per_chunk);
3151         buf +=
3152             sprintf(buf, "use_nand_ecc......... %d\n", dev->param.use_nand_ecc);
3153         buf +=
3154             sprintf(buf, "no_tags_ecc.......... %d\n", dev->param.no_tags_ecc);
3155         buf += sprintf(buf, "is_yaffs2............ %d\n", dev->param.is_yaffs2);
3156         buf +=
3157             sprintf(buf, "inband_tags.......... %d\n", dev->param.inband_tags);
3158         buf +=
3159             sprintf(buf, "empty_lost_n_found... %d\n",
3160                     dev->param.empty_lost_n_found);
3161         buf +=
3162             sprintf(buf, "disable_lazy_load.... %d\n",
3163                     dev->param.disable_lazy_load);
3164         buf +=
3165             sprintf(buf, "refresh_period....... %d\n",
3166                     dev->param.refresh_period);
3167         buf += sprintf(buf, "n_caches............. %d\n", dev->param.n_caches);
3168         buf +=
3169             sprintf(buf, "n_reserved_blocks.... %d\n",
3170                     dev->param.n_reserved_blocks);
3171         buf +=
3172             sprintf(buf, "always_check_erased.. %d\n",
3173                     dev->param.always_check_erased);
3174
3175         buf += sprintf(buf, "\n");
3176
3177         return buf;
3178 }
3179
3180 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3181 {
3182         buf +=
3183             sprintf(buf, "data_bytes_per_chunk. %d\n",
3184                     dev->data_bytes_per_chunk);
3185         buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3186         buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3187         buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3188         buf +=
3189             sprintf(buf, "blocks_in_checkpt.... %d\n", dev->blocks_in_checkpt);
3190         buf += sprintf(buf, "\n");
3191         buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3192         buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3193         buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3194         buf += sprintf(buf, "\n");
3195         buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3196         buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3197         buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3198         buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3199         buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3200         buf +=
3201             sprintf(buf, "passive_gc_count..... %u\n", dev->passive_gc_count);
3202         buf +=
3203             sprintf(buf, "oldest_dirty_gc_count %u\n",
3204                     dev->oldest_dirty_gc_count);
3205         buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3206         buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3207         buf +=
3208             sprintf(buf, "n_retired_writes..... %u\n", dev->n_retired_writes);
3209         buf +=
3210             sprintf(buf, "n_retired_blocks..... %u\n", dev->n_retired_blocks);
3211         buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3212         buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3213         buf +=
3214             sprintf(buf, "n_tags_ecc_fixed..... %u\n", dev->n_tags_ecc_fixed);
3215         buf +=
3216             sprintf(buf, "n_tags_ecc_unfixed... %u\n", dev->n_tags_ecc_unfixed);
3217         buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3218         buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3219         buf +=
3220             sprintf(buf, "n_unlinked_files..... %u\n", dev->n_unlinked_files);
3221         buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3222         buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3223
3224         return buf;
3225 }
3226
3227 static int yaffs_proc_read(char *page,
3228                            char **start,
3229                            off_t offset, int count, int *eof, void *data)
3230 {
3231         struct list_head *item;
3232         char *buf = page;
3233         int step = offset;
3234         int n = 0;
3235
3236         /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3237          * We use 'offset' (*ppos) to indicate where we are in dev_list.
3238          * This also assumes the user has posted a read buffer large
3239          * enough to hold the complete output; but that's life in /proc.
3240          */
3241
3242         *(int *)start = 1;
3243
3244         /* Print header first */
3245         if (step == 0)
3246                 buf +=
3247                     sprintf(buf,
3248                             "Multi-version YAFFS built:" __DATE__ " " __TIME__
3249                             "\n");
3250         else if (step == 1)
3251                 buf += sprintf(buf, "\n");
3252         else {
3253                 step -= 2;
3254
3255                 mutex_lock(&yaffs_context_lock);
3256
3257                 /* Locate and print the Nth entry.  Order N-squared but N is small. */
3258                 list_for_each(item, &yaffs_context_list) {
3259                         struct yaffs_linux_context *dc =
3260                             list_entry(item, struct yaffs_linux_context,
3261                                        context_list);
3262                         struct yaffs_dev *dev = dc->dev;
3263
3264                         if (n < (step & ~1)) {
3265                                 n += 2;
3266                                 continue;
3267                         }
3268                         if ((step & 1) == 0) {
3269                                 buf +=
3270                                     sprintf(buf, "\nDevice %d \"%s\"\n", n,
3271                                             dev->param.name);
3272                                 buf = yaffs_dump_dev_part0(buf, dev);
3273                         } else {
3274                                 buf = yaffs_dump_dev_part1(buf, dev);
3275                         }
3276
3277                         break;
3278                 }
3279                 mutex_unlock(&yaffs_context_lock);
3280         }
3281
3282         return buf - page < count ? buf - page : count;
3283 }
3284
3285 static int yaffs_stats_proc_read(char *page,
3286                                  char **start,
3287                                  off_t offset, int count, int *eof, void *data)
3288 {
3289         struct list_head *item;
3290         char *buf = page;
3291         int n = 0;
3292
3293         mutex_lock(&yaffs_context_lock);
3294
3295         /* Locate and print the Nth entry.  Order N-squared but N is small. */
3296         list_for_each(item, &yaffs_context_list) {
3297                 struct yaffs_linux_context *dc =
3298                     list_entry(item, struct yaffs_linux_context, context_list);
3299                 struct yaffs_dev *dev = dc->dev;
3300
3301                 int erased_chunks;
3302
3303                 erased_chunks =
3304                     dev->n_erased_blocks * dev->param.chunks_per_block;
3305
3306                 buf += sprintf(buf, "%d, %d, %d, %u, %u, %u, %u\n",
3307                                n, dev->n_free_chunks, erased_chunks,
3308                                dev->bg_gcs, dev->oldest_dirty_gc_count,
3309                                dev->n_obj, dev->n_tnodes);
3310         }
3311         mutex_unlock(&yaffs_context_lock);
3312
3313         return buf - page < count ? buf - page : count;
3314 }
3315
3316 /**
3317  * Set the verbosity of the warnings and error messages.
3318  *
3319  * Note that the names can only be a..z or _ with the current code.
3320  */
3321
3322 static struct {
3323         char *mask_name;
3324         unsigned mask_bitfield;
3325 } mask_flags[] = {
3326         {"allocate", YAFFS_TRACE_ALLOCATE},
3327         {"always", YAFFS_TRACE_ALWAYS},
3328         {"background", YAFFS_TRACE_BACKGROUND},
3329         {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3330         {"buffers", YAFFS_TRACE_BUFFERS},
3331         {"bug", YAFFS_TRACE_BUG},
3332         {"checkpt", YAFFS_TRACE_CHECKPOINT},
3333         {"deletion", YAFFS_TRACE_DELETION},
3334         {"erase", YAFFS_TRACE_ERASE},
3335         {"error", YAFFS_TRACE_ERROR},
3336         {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3337         {"gc", YAFFS_TRACE_GC},
3338         {"lock", YAFFS_TRACE_LOCK},
3339         {"mtd", YAFFS_TRACE_MTD},
3340         {"nandaccess", YAFFS_TRACE_NANDACCESS},
3341         {"os", YAFFS_TRACE_OS},
3342         {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3343         {"scan", YAFFS_TRACE_SCAN},
3344         {"mount", YAFFS_TRACE_MOUNT},
3345         {"tracing", YAFFS_TRACE_TRACING},
3346         {"sync", YAFFS_TRACE_SYNC},
3347         {"write", YAFFS_TRACE_WRITE},
3348         {"verify", YAFFS_TRACE_VERIFY},
3349         {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3350         {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3351         {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3352         {"all", 0xffffffff},
3353         {"none", 0},
3354         {NULL, 0},
3355 };
3356
3357 #define MAX_MASK_NAME_LENGTH 40
3358 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3359                                           unsigned long count, void *data)
3360 {
3361         unsigned rg = 0, mask_bitfield;
3362         char *end;
3363         char *mask_name;
3364         const char *x;
3365         char substring[MAX_MASK_NAME_LENGTH + 1];
3366         int i;
3367         int done = 0;
3368         int add, len = 0;
3369         int pos = 0;
3370
3371         rg = yaffs_trace_mask;
3372
3373         while (!done && (pos < count)) {
3374                 done = 1;
3375                 while ((pos < count) && isspace(buf[pos]))
3376                         pos++;
3377
3378                 switch (buf[pos]) {
3379                 case '+':
3380                 case '-':
3381                 case '=':
3382                         add = buf[pos];
3383                         pos++;
3384                         break;
3385
3386                 default:
3387                         add = ' ';
3388                         break;
3389                 }
3390                 mask_name = NULL;
3391
3392                 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3393
3394                 if (end > buf + pos) {
3395                         mask_name = "numeral";
3396                         len = end - (buf + pos);
3397                         pos += len;
3398                         done = 0;
3399                 } else {
3400                         for (x = buf + pos, i = 0;
3401                              (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3402                              i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3403                                 substring[i] = *x;
3404                         substring[i] = '\0';
3405
3406                         for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3407                                 if (strcmp(substring, mask_flags[i].mask_name)
3408                                     == 0) {
3409                                         mask_name = mask_flags[i].mask_name;
3410                                         mask_bitfield =
3411                                             mask_flags[i].mask_bitfield;
3412                                         done = 0;
3413                                         break;
3414                                 }
3415                         }
3416                 }
3417
3418                 if (mask_name != NULL) {
3419                         done = 0;
3420                         switch (add) {
3421                         case '-':
3422                                 rg &= ~mask_bitfield;
3423                                 break;
3424                         case '+':
3425                                 rg |= mask_bitfield;
3426                                 break;
3427                         case '=':
3428                                 rg = mask_bitfield;
3429                                 break;
3430                         default:
3431                                 rg |= mask_bitfield;
3432                                 break;
3433                         }
3434                 }
3435         }
3436
3437         yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3438
3439         printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3440
3441         if (rg & YAFFS_TRACE_ALWAYS) {
3442                 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3443                         char flag;
3444                         flag = ((rg & mask_flags[i].mask_bitfield) ==
3445                                 mask_flags[i].mask_bitfield) ? '+' : '-';
3446                         printk(KERN_DEBUG "%c%s\n", flag,
3447                                mask_flags[i].mask_name);
3448                 }
3449         }
3450
3451         return count;
3452 }
3453
3454 static int yaffs_proc_write(struct file *file, const char *buf,
3455                             unsigned long count, void *data)
3456 {
3457         return yaffs_proc_write_trace_options(file, buf, count, data);
3458 }
3459
3460 /* Stuff to handle installation of file systems */
3461 struct file_system_to_install {
3462         struct file_system_type *fst;
3463         int installed;
3464 };
3465
3466 static struct file_system_to_install fs_to_install[] = {
3467         {&yaffs_fs_type, 0},
3468         {&yaffs2_fs_type, 0},
3469         {NULL, 0}
3470 };
3471
3472 static int __init init_yaffs_fs(void)
3473 {
3474         int error = 0;
3475         struct file_system_to_install *fsinst;
3476
3477         T(YAFFS_TRACE_ALWAYS,
3478           (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3479
3480 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3481         T(YAFFS_TRACE_ALWAYS,
3482           (TSTR
3483            (" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3484 #endif
3485
3486         mutex_init(&yaffs_context_lock);
3487
3488         /* Install the proc_fs entries */
3489         my_proc_entry = create_proc_entry("yaffs",
3490                                           S_IRUGO | S_IFREG, YPROC_ROOT);
3491
3492         if (my_proc_entry) {
3493                 my_proc_entry->write_proc = yaffs_proc_write;
3494                 my_proc_entry->read_proc = yaffs_proc_read;
3495                 my_proc_entry->data = NULL;
3496         } else {
3497                 return -ENOMEM;
3498         }
3499
3500         debug_proc_entry = create_proc_entry("yaffs_stats",
3501                                              S_IRUGO | S_IFREG, YPROC_ROOT);
3502
3503         if (debug_proc_entry) {
3504                 debug_proc_entry->write_proc = NULL;
3505                 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3506                 debug_proc_entry->data = NULL;
3507         } else {
3508                 return -ENOMEM;
3509         }
3510
3511         /* Now add the file system entries */
3512
3513         fsinst = fs_to_install;
3514
3515         while (fsinst->fst && !error) {
3516                 error = register_filesystem(fsinst->fst);
3517                 if (!error)
3518                         fsinst->installed = 1;
3519                 fsinst++;
3520         }
3521
3522         /* Any errors? uninstall  */
3523         if (error) {
3524                 fsinst = fs_to_install;
3525
3526                 while (fsinst->fst) {
3527                         if (fsinst->installed) {
3528                                 unregister_filesystem(fsinst->fst);
3529                                 fsinst->installed = 0;
3530                         }
3531                         fsinst++;
3532                 }
3533         }
3534
3535         return error;
3536 }
3537
3538 static void __exit exit_yaffs_fs(void)
3539 {
3540
3541         struct file_system_to_install *fsinst;
3542
3543         T(YAFFS_TRACE_ALWAYS,
3544           (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3545
3546         remove_proc_entry("yaffs", YPROC_ROOT);
3547         remove_proc_entry("yaffs_stats", YPROC_ROOT);
3548
3549         fsinst = fs_to_install;
3550
3551         while (fsinst->fst) {
3552                 if (fsinst->installed) {
3553                         unregister_filesystem(fsinst->fst);
3554                         fsinst->installed = 0;
3555                 }
3556                 fsinst++;
3557         }
3558 }
3559
3560 module_init(init_yaffs_fs)
3561     module_exit(exit_yaffs_fs)
3562
3563     MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3564 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3565 MODULE_LICENSE("GPL");