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