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