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