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