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