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