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