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