2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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
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.
23 * This is the file system front-end to YAFFS that hooks it up to
27 * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
29 * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
31 * >> inode->u.generic_ip points to the associated struct yaffs_obj.
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.
38 #include <linux/version.h>
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/slab.h>
43 #include <linux/init.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>
58 #include <asm/div64.h>
61 #include <linux/statfs.h>
63 #define UnlockPage(p) unlock_page(p)
64 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
66 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
68 #define YPROC_ROOT NULL
70 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
72 #define WRITE_SIZE_STR "writesize"
73 #define WRITE_SIZE(mtd) ((mtd)->writesize)
76 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
78 uint64_t result = partition_size;
79 do_div(result, block_size);
80 return (uint32_t)result;
83 #include <linux/uaccess.h>
84 #include <linux/mtd/mtd.h>
87 #include "yaffs_trace.h"
88 #include "yaffs_guts.h"
89 #include "yaffs_attribs.h"
91 #include "yaffs_linux.h"
93 #include "yaffs_mtdif.h"
94 #include "yaffs_mtdif1.h"
95 #include "yaffs_mtdif2.h"
97 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
98 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
99 unsigned int yaffs_auto_checkpoint = 1;
100 unsigned int yaffs_gc_control = 1;
101 unsigned int yaffs_bg_enable = 1;
103 /* Module Parameters */
104 module_param(yaffs_trace_mask, uint, 0644);
105 module_param(yaffs_wr_attempts, uint, 0644);
106 module_param(yaffs_auto_checkpoint, uint, 0644);
107 module_param(yaffs_gc_control, uint, 0644);
108 module_param(yaffs_bg_enable, uint, 0644);
110 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
112 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
114 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
115 #define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
116 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
117 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
120 #define update_dir_time(dir) do {\
121 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
124 static void yaffs_put_super(struct super_block *sb);
126 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
128 static ssize_t yaffs_hold_space(struct file *f);
129 static void yaffs_release_space(struct file *f);
131 static int yaffs_file_flush(struct file *file, fl_owner_t id);
133 static int yaffs_sync_object(struct file *file, int datasync);
135 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
137 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
138 struct nameidata *n);
139 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
140 struct nameidata *n);
141 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
142 struct dentry *dentry);
143 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
144 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
145 const char *symname);
146 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
148 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
150 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
151 struct inode *new_dir, struct dentry *new_dentry);
152 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
154 static int yaffs_sync_fs(struct super_block *sb, int wait);
155 static void yaffs_write_super(struct super_block *sb);
157 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
159 #ifdef YAFFS_HAS_PUT_INODE
160 static void yaffs_put_inode(struct inode *inode);
163 static void yaffs_evict_inode(struct inode *);
165 static int yaffs_readpage(struct file *file, struct page *page);
166 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
168 #ifdef CONFIG_YAFFS_XATTR
169 int yaffs_setxattr(struct dentry *dentry, const char *name,
170 const void *value, size_t size, int flags);
171 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
173 int yaffs_removexattr(struct dentry *dentry, const char *name);
174 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
178 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
179 loff_t pos, unsigned len, unsigned flags,
180 struct page **pagep, void **fsdata);
181 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
182 loff_t pos, unsigned len, unsigned copied,
183 struct page *pg, void *fsdadata);
185 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
187 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
188 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
190 static void yaffs_touch_super(struct yaffs_dev *dev);
192 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
194 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
197 static struct address_space_operations yaffs_file_address_operations = {
198 .readpage = yaffs_readpage,
199 .writepage = yaffs_writepage,
200 .write_begin = yaffs_write_begin,
201 .write_end = yaffs_write_end,
205 static const struct file_operations yaffs_file_operations = {
206 .read = do_sync_read,
207 .write = do_sync_write,
208 .aio_read = generic_file_aio_read,
209 .aio_write = generic_file_aio_write,
210 .mmap = generic_file_mmap,
211 .flush = yaffs_file_flush,
212 .fsync = yaffs_sync_object,
213 .splice_read = generic_file_splice_read,
214 .splice_write = generic_file_splice_write,
215 .llseek = generic_file_llseek,
220 static const struct inode_operations yaffs_file_inode_operations = {
221 .setattr = yaffs_setattr,
222 #ifdef CONFIG_YAFFS_XATTR
223 .setxattr = yaffs_setxattr,
224 .getxattr = yaffs_getxattr,
225 .listxattr = yaffs_listxattr,
226 .removexattr = yaffs_removexattr,
230 static const struct inode_operations yaffs_symlink_inode_operations = {
231 .readlink = yaffs_readlink,
232 .follow_link = yaffs_follow_link,
233 .put_link = yaffs_put_link,
234 .setattr = yaffs_setattr,
235 #ifdef CONFIG_YAFFS_XATTR
236 .setxattr = yaffs_setxattr,
237 .getxattr = yaffs_getxattr,
238 .listxattr = yaffs_listxattr,
239 .removexattr = yaffs_removexattr,
243 static const struct inode_operations yaffs_dir_inode_operations = {
244 .create = yaffs_create,
245 .lookup = yaffs_lookup,
247 .unlink = yaffs_unlink,
248 .symlink = yaffs_symlink,
249 .mkdir = yaffs_mkdir,
250 .rmdir = yaffs_unlink,
251 .mknod = yaffs_mknod,
252 .rename = yaffs_rename,
253 .setattr = yaffs_setattr,
254 #ifdef CONFIG_YAFFS_XATTR
255 .setxattr = yaffs_setxattr,
256 .getxattr = yaffs_getxattr,
257 .listxattr = yaffs_listxattr,
258 .removexattr = yaffs_removexattr,
262 static const struct file_operations yaffs_dir_operations = {
263 .read = generic_read_dir,
264 .readdir = yaffs_readdir,
265 .fsync = yaffs_sync_object,
266 .llseek = yaffs_dir_llseek,
269 static const struct super_operations yaffs_super_ops = {
270 .statfs = yaffs_statfs,
271 #ifdef YAFFS_HAS_PUT_INODE
272 .put_inode = yaffs_put_inode,
274 .put_super = yaffs_put_super,
275 .evict_inode = yaffs_evict_inode,
276 .sync_fs = yaffs_sync_fs,
277 .write_super = yaffs_write_super,
281 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
283 setattr_copy(inode,attr);
287 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
289 truncate_setsize(inode,newsize);
293 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
295 return yaffs_gc_control;
298 static void yaffs_gross_lock(struct yaffs_dev *dev)
300 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
301 down(&(yaffs_dev_to_lc(dev)->gross_lock));
302 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
305 static void yaffs_gross_unlock(struct yaffs_dev *dev)
307 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
308 up(&(yaffs_dev_to_lc(dev)->gross_lock));
313 /* ExportFS support */
314 static struct inode *
315 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
317 return Y_IGET(sb, ino);
320 static struct dentry *
321 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
323 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
326 static struct dentry *
327 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
329 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
332 struct dentry *yaffs2_get_parent(struct dentry *dentry)
335 struct super_block *sb = dentry->d_inode->i_sb;
336 struct dentry *parent = ERR_PTR(-ENOENT);
338 unsigned long parent_ino;
339 struct yaffs_obj *d_obj;
340 struct yaffs_obj *parent_obj;
342 d_obj = yaffs_inode_to_obj(dentry->d_inode);
345 parent_obj = d_obj->parent;
347 parent_ino = yaffs_get_obj_inode(parent_obj);
348 inode = Y_IGET(sb, parent_ino);
351 parent = ERR_CAST(inode);
353 parent = d_obtain_alias(inode);
354 if (!IS_ERR(parent)) {
355 parent = ERR_PTR(-ENOMEM);
365 /* Just declare a zero structure as a NULL value implies
366 * using the default functions of exportfs.
369 static struct export_operations yaffs_export_ops =
371 .fh_to_dentry = yaffs2_fh_to_dentry,
372 .fh_to_parent = yaffs2_fh_to_parent,
373 .get_parent = yaffs2_get_parent,
376 /*-----------------------------------------------------------------*/
377 /* Directory search context allows us to unlock access to yaffs during
378 * filldir without causing problems with the directory being modified.
379 * This is similar to the tried and tested mechanism used in yaffs direct.
381 * A search context iterates along a doubly linked list of siblings in the
382 * directory. If the iterating object is deleted then this would corrupt
383 * the list iteration, likely causing a crash. The search context avoids
384 * this by using the remove_obj_fn to move the search context to the
385 * next object before the object is deleted.
387 * Many readdirs (and thus seach conexts) may be alive simulateously so
388 * each struct yaffs_dev has a list of these.
390 * A seach context lives for the duration of a readdir.
392 * All these functions must be called while yaffs is locked.
395 struct yaffs_search_context {
396 struct yaffs_dev *dev;
397 struct yaffs_obj *dir_obj;
398 struct yaffs_obj *next_return;
399 struct list_head others;
403 * yaffs_new_search() creates a new search context, initialises it and
404 * adds it to the device's search context list.
406 * Called at start of readdir.
408 static struct yaffs_search_context * yaffs_new_search(struct yaffs_obj *dir)
410 struct yaffs_dev *dev = dir->my_dev;
411 struct yaffs_search_context *sc = YMALLOC(sizeof(struct yaffs_search_context));
415 if( list_empty(&sc->dir_obj->variant.dir_variant.children))
416 sc->next_return = NULL;
418 sc->next_return = list_entry(
419 dir->variant.dir_variant.children.next,
420 struct yaffs_obj,siblings);
421 INIT_LIST_HEAD(&sc->others);
422 list_add(&sc->others,&(yaffs_dev_to_lc(dev)->search_contexts));
428 * yaffs_search_end() disposes of a search context and cleans up.
430 static void yaffs_search_end(struct yaffs_search_context * sc)
433 list_del(&sc->others);
439 * yaffs_search_advance() moves a search context to the next object.
440 * Called when the search iterates or when an object removal causes
441 * the search context to be moved to the next object.
443 static void yaffs_search_advance(struct yaffs_search_context *sc)
448 if( sc->next_return == NULL ||
449 list_empty(&sc->dir_obj->variant.dir_variant.children))
450 sc->next_return = NULL;
452 struct list_head *next = sc->next_return->siblings.next;
454 if( next == &sc->dir_obj->variant.dir_variant.children)
455 sc->next_return = NULL; /* end of list */
457 sc->next_return = list_entry(next,struct yaffs_obj,siblings);
462 * yaffs_remove_obj_callback() is called when an object is unlinked.
463 * We check open search contexts and advance any which are currently
464 * on the object being iterated.
466 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
470 struct yaffs_search_context *sc;
471 struct list_head *search_contexts = &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
474 /* Iterate through the directory search contexts.
475 * If any are currently on the object being removed, then advance
476 * the search context to the next object to prevent a hanging pointer.
478 list_for_each(i, search_contexts) {
480 sc = list_entry(i, struct yaffs_search_context,others);
481 if(sc->next_return == obj)
482 yaffs_search_advance(sc);
489 /*-----------------------------------------------------------------*/
491 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
494 unsigned char *alias;
497 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
499 yaffs_gross_lock(dev);
501 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
503 yaffs_gross_unlock(dev);
508 ret = vfs_readlink(dentry, buffer, buflen, alias);
513 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
515 unsigned char *alias;
517 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
519 yaffs_gross_lock(dev);
521 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
522 yaffs_gross_unlock(dev);
525 ret = ERR_PTR(-ENOMEM);
529 nd_set_link(nd, alias);
535 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) {
539 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
540 struct yaffs_obj *obj);
543 * Lookup is used to find objects in the fs
546 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
549 struct yaffs_obj *obj;
550 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
552 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
554 if(current != yaffs_dev_to_lc(dev)->readdir_process)
555 yaffs_gross_lock(dev);
558 (TSTR("yaffs_lookup for %d:%s\n"),
559 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name));
561 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir),
562 dentry->d_name.name);
564 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
566 /* Can't hold gross lock when calling yaffs_get_inode() */
567 if(current != yaffs_dev_to_lc(dev)->readdir_process)
568 yaffs_gross_unlock(dev);
572 (TSTR("yaffs_lookup found %d\n"), obj->obj_id));
574 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
578 (TSTR("yaffs_loookup dentry \n")));
579 d_add(dentry, inode);
585 T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
589 d_add(dentry, inode);
595 #ifdef YAFFS_HAS_PUT_INODE
597 /* For now put inode is just for debugging
598 * Put inode is called when the inode **structure** is put.
600 static void yaffs_put_inode(struct inode *inode)
603 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
604 atomic_read(&inode->i_count)));
610 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
612 /* Clear the association between the inode and
613 * the struct yaffs_obj.
615 obj->my_inode = NULL;
616 yaffs_inode_to_obj_lv(inode) = NULL;
618 /* If the object freeing was deferred, then the real
620 * This should fix the inode inconsistency problem.
622 yaffs_handle_defered_free(obj);
625 /* yaffs_evict_inode combines into one operation what was previously done in
626 * yaffs_clear_inode() and yaffs_delete_inode()
629 static void yaffs_evict_inode( struct inode *inode)
631 struct yaffs_obj *obj;
632 struct yaffs_dev *dev;
635 obj = yaffs_inode_to_obj(inode);
638 (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
639 atomic_read(&inode->i_count),
640 obj ? "object exists" : "null object"));
642 if (!inode->i_nlink && !is_bad_inode(inode))
644 truncate_inode_pages(&inode->i_data,0);
645 end_writeback(inode);
649 yaffs_gross_lock(dev);
651 yaffs_gross_unlock(dev);
655 yaffs_gross_lock(dev);
656 yaffs_unstitch_obj(inode,obj);
657 yaffs_gross_unlock(dev);
664 static int yaffs_file_flush(struct file *file, fl_owner_t id)
666 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
668 struct yaffs_dev *dev = obj->my_dev;
671 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->obj_id,
672 obj->dirty ? "dirty" : "clean"));
674 yaffs_gross_lock(dev);
676 yaffs_flush_file(obj, 1, 0);
678 yaffs_gross_unlock(dev);
683 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
685 /* Lifted from jffs2 */
687 struct yaffs_obj *obj;
688 unsigned char *pg_buf;
691 struct yaffs_dev *dev;
694 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
695 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
696 (unsigned)PAGE_CACHE_SIZE));
698 obj = yaffs_dentry_to_obj(f->f_dentry);
702 BUG_ON(!PageLocked(pg));
705 /* FIXME: Can kmap fail? */
707 yaffs_gross_lock(dev);
709 ret = yaffs_file_rd(obj, pg_buf,
710 pg->index << PAGE_CACHE_SHIFT,
713 yaffs_gross_unlock(dev);
719 ClearPageUptodate(pg);
726 flush_dcache_page(pg);
729 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
733 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
735 int ret = yaffs_readpage_nolock(f, pg);
740 static int yaffs_readpage(struct file *f, struct page *pg)
744 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
745 ret=yaffs_readpage_unlock(f, pg);
746 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
750 /* writepage inspired by/stolen from smbfs */
752 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
754 struct yaffs_dev *dev;
755 struct address_space *mapping = page->mapping;
757 unsigned long end_index;
759 struct yaffs_obj *obj;
766 inode = mapping->host;
769 i_size = i_size_read(inode);
771 end_index = i_size >> PAGE_CACHE_SHIFT;
773 if(page->index < end_index)
774 n_bytes = PAGE_CACHE_SIZE;
776 n_bytes = i_size & (PAGE_CACHE_SIZE -1);
778 if (page->index > end_index || !n_bytes) {
780 (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
781 (unsigned)(page->index << PAGE_CACHE_SHIFT),
782 (unsigned)inode->i_size));
784 (TSTR(" -> don't care!!\n")));
786 zero_user_segment(page,0,PAGE_CACHE_SIZE);
787 set_page_writeback(page);
789 end_page_writeback(page);
794 if(n_bytes != PAGE_CACHE_SIZE)
795 zero_user_segment(page,n_bytes,PAGE_CACHE_SIZE);
801 obj = yaffs_inode_to_obj(inode);
803 yaffs_gross_lock(dev);
806 (TSTR("yaffs_writepage at %08x, size %08x\n"),
807 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes));
809 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
810 (int)obj->variant.file_variant.file_size, (int)inode->i_size));
812 n_written = yaffs_wr_file(obj, buffer,
813 page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
815 yaffs_touch_super(dev);
818 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
819 (int)obj->variant.file_variant.file_size, (int)inode->i_size));
821 yaffs_gross_unlock(dev);
824 set_page_writeback(page);
826 end_page_writeback(page);
829 return (n_written == n_bytes) ? 0 : -ENOSPC;
833 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
834 loff_t pos, unsigned len, unsigned flags,
835 struct page **pagep, void **fsdata)
837 struct page *pg = NULL;
838 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
844 pg = grab_cache_page_write_begin(mapping, index, flags);
852 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
853 (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
856 space_held = yaffs_hold_space(filp);
863 /* Update page if required */
865 if (!Page_Uptodate(pg))
866 ret = yaffs_readpage_nolock(filp, pg);
871 /* Happy path return */
872 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
878 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
880 yaffs_release_space(filp);
883 page_cache_release(pg);
889 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
890 loff_t pos, unsigned len, unsigned copied,
891 struct page *pg, void *fsdadata)
895 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
898 addr = kva + offset_into_page;
901 ("yaffs_write_end addr %p pos %x n_bytes %d\n",
902 addr,(unsigned)pos, copied));
904 ret = yaffs_file_write(filp, addr, copied, &pos);
908 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
917 yaffs_release_space(filp);
919 page_cache_release(pg);
924 static void yaffs_fill_inode_from_obj(struct inode *inode, struct yaffs_obj *obj)
929 /* Check mode against the variant type and attempt to repair if broken. */
930 u32 mode = obj->yst_mode;
931 switch (obj->variant_type) {
932 case YAFFS_OBJECT_TYPE_FILE:
933 if (!S_ISREG(mode)) {
934 obj->yst_mode &= ~S_IFMT;
935 obj->yst_mode |= S_IFREG;
939 case YAFFS_OBJECT_TYPE_SYMLINK:
940 if (!S_ISLNK(mode)) {
941 obj->yst_mode &= ~S_IFMT;
942 obj->yst_mode |= S_IFLNK;
946 case YAFFS_OBJECT_TYPE_DIRECTORY:
947 if (!S_ISDIR(mode)) {
948 obj->yst_mode &= ~S_IFMT;
949 obj->yst_mode |= S_IFDIR;
953 case YAFFS_OBJECT_TYPE_UNKNOWN:
954 case YAFFS_OBJECT_TYPE_HARDLINK:
955 case YAFFS_OBJECT_TYPE_SPECIAL:
961 inode->i_flags |= S_NOATIME;
963 inode->i_ino = obj->obj_id;
964 inode->i_mode = obj->yst_mode;
965 inode->i_uid = obj->yst_uid;
966 inode->i_gid = obj->yst_gid;
968 inode->i_rdev = old_decode_dev(obj->yst_rdev);
970 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
971 inode->i_atime.tv_nsec = 0;
972 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
973 inode->i_mtime.tv_nsec = 0;
974 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
975 inode->i_ctime.tv_nsec = 0;
976 inode->i_size = yaffs_get_obj_length(obj);
977 inode->i_blocks = (inode->i_size + 511) >> 9;
979 inode->i_nlink = yaffs_get_obj_link_count(obj);
982 (TSTR("yaffs_fill_inode mode %x uid %d gid %d size %d count %d\n"),
983 inode->i_mode, inode->i_uid, inode->i_gid,
984 (int)inode->i_size, atomic_read(&inode->i_count)));
986 switch (obj->yst_mode & S_IFMT) {
987 default: /* fifo, device or socket */
988 init_special_inode(inode, obj->yst_mode,
989 old_decode_dev(obj->yst_rdev));
991 case S_IFREG: /* file */
992 inode->i_op = &yaffs_file_inode_operations;
993 inode->i_fop = &yaffs_file_operations;
994 inode->i_mapping->a_ops =
995 &yaffs_file_address_operations;
997 case S_IFDIR: /* directory */
998 inode->i_op = &yaffs_dir_inode_operations;
999 inode->i_fop = &yaffs_dir_operations;
1001 case S_IFLNK: /* symlink */
1002 inode->i_op = &yaffs_symlink_inode_operations;
1006 yaffs_inode_to_obj_lv(inode) = obj;
1008 obj->my_inode = inode;
1012 (TSTR("yaffs_fill_inode invalid parameters\n")));
1017 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1018 struct yaffs_obj *obj)
1020 struct inode *inode;
1024 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1031 (TSTR("yaffs_get_inode for NULL object!!\n")));
1037 (TSTR("yaffs_get_inode for object %d\n"), obj->obj_id));
1039 inode = Y_IGET(sb, obj->obj_id);
1043 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1044 /* iget also increments the inode's i_count */
1045 /* NB You can't be holding gross_lock or deadlock will happen! */
1050 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1053 struct yaffs_obj *obj;
1054 int n_written, ipos;
1055 struct inode *inode;
1056 struct yaffs_dev *dev;
1058 obj = yaffs_dentry_to_obj(f->f_dentry);
1062 yaffs_gross_lock(dev);
1064 inode = f->f_dentry->d_inode;
1066 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1067 ipos = inode->i_size;
1073 (TSTR("yaffs_file_write: hey obj is null!\n")));
1076 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1077 "to object %d at %d(%x)\n"),
1078 (unsigned) n, (unsigned) n, obj->obj_id, ipos,ipos));
1080 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1082 yaffs_touch_super(dev);
1085 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1086 (unsigned )n,(unsigned)n));
1088 if (n_written > 0) {
1091 if (ipos > inode->i_size) {
1092 inode->i_size = ipos;
1093 inode->i_blocks = (ipos + 511) >> 9;
1096 (TSTR("yaffs_file_write size updated to %d bytes, "
1098 ipos, (int)(inode->i_blocks)));
1102 yaffs_gross_unlock(dev);
1103 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1106 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1107 /* For now we just assume few parallel writes and check against a small number. */
1108 /* Todo: need to do this with a counter to handle parallel reads better */
1110 static ssize_t yaffs_hold_space(struct file *f)
1112 struct yaffs_obj *obj;
1113 struct yaffs_dev *dev;
1118 obj = yaffs_dentry_to_obj(f->f_dentry);
1122 yaffs_gross_lock(dev);
1124 n_free_chunks = yaffs_get_n_free_chunks(dev);
1126 yaffs_gross_unlock(dev);
1128 return (n_free_chunks > 20) ? 1 : 0;
1131 static void yaffs_release_space(struct file *f)
1133 struct yaffs_obj *obj;
1134 struct yaffs_dev *dev;
1137 obj = yaffs_dentry_to_obj(f->f_dentry);
1141 yaffs_gross_lock(dev);
1144 yaffs_gross_unlock(dev);
1148 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1156 offset += i_size_read(file->f_path.dentry->d_inode);
1159 offset += file->f_pos;
1164 if (offset != file->f_pos)
1165 file->f_pos = offset;
1174 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1176 struct yaffs_obj *obj;
1177 struct yaffs_dev *dev;
1178 struct yaffs_search_context *sc;
1179 struct inode *inode = f->f_dentry->d_inode;
1180 unsigned long offset, curoffs;
1181 struct yaffs_obj *l;
1184 char name[YAFFS_MAX_NAME_LENGTH + 1];
1186 obj = yaffs_dentry_to_obj(f->f_dentry);
1189 yaffs_gross_lock(dev);
1191 yaffs_dev_to_lc(dev)->readdir_process = current;
1195 sc = yaffs_new_search(obj);
1201 T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1205 (TSTR("yaffs_readdir: entry . ino %d \n"),
1206 (int)inode->i_ino));
1207 yaffs_gross_unlock(dev);
1208 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0){
1209 yaffs_gross_lock(dev);
1212 yaffs_gross_lock(dev);
1218 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1219 (int)f->f_dentry->d_parent->d_inode->i_ino));
1220 yaffs_gross_unlock(dev);
1221 if (filldir(dirent, "..", 2, offset,
1222 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
1223 yaffs_gross_lock(dev);
1226 yaffs_gross_lock(dev);
1233 /* If the directory has changed since the open or last call to
1234 readdir, rewind to after the 2 canned entries. */
1235 if (f->f_version != inode->i_version) {
1238 f->f_version = inode->i_version;
1241 while(sc->next_return){
1243 l = sc->next_return;
1244 if (curoffs >= offset) {
1245 int this_inode = yaffs_get_obj_inode(l);
1246 int this_type = yaffs_get_obj_type(l);
1248 yaffs_get_obj_name(l, name,
1249 YAFFS_MAX_NAME_LENGTH + 1);
1251 (TSTR("yaffs_readdir: %s inode %d\n"),
1252 name, yaffs_get_obj_inode(l)));
1254 yaffs_gross_unlock(dev);
1262 yaffs_gross_lock(dev);
1266 yaffs_gross_lock(dev);
1271 yaffs_search_advance(sc);
1275 yaffs_search_end(sc);
1276 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1277 yaffs_gross_unlock(dev);
1285 * File creation. Allocate an inode, and we're done..
1288 #define YCRED(x) (x->cred)
1290 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1293 struct inode *inode;
1295 struct yaffs_obj *obj = NULL;
1296 struct yaffs_dev *dev;
1298 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1300 int error = -ENOSPC;
1301 uid_t uid = YCRED(current)->fsuid;
1302 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1304 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1309 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1310 parent->obj_id, parent->variant_type));
1313 (TSTR("yaffs_mknod: could not get parent object\n")));
1317 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1318 "mode %x dev %x\n"),
1319 dentry->d_name.name, mode, rdev));
1321 dev = parent->my_dev;
1323 yaffs_gross_lock(dev);
1325 switch (mode & S_IFMT) {
1327 /* Special (socket, fifo, device...) */
1328 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1329 obj = yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1330 gid, old_encode_dev(rdev));
1332 case S_IFREG: /* file */
1333 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1334 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1337 case S_IFDIR: /* directory */
1339 (TSTR("yaffs_mknod: making directory\n")));
1340 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1343 case S_IFLNK: /* symlink */
1344 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1345 obj = NULL; /* Do we ever get here? */
1349 /* Can not call yaffs_get_inode() with gross lock held */
1350 yaffs_gross_unlock(dev);
1353 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1354 d_instantiate(dentry, inode);
1355 update_dir_time(dir);
1357 (TSTR("yaffs_mknod created object %d count = %d\n"),
1358 obj->obj_id, atomic_read(&inode->i_count)));
1360 yaffs_fill_inode_from_obj(dir,parent);
1363 (TSTR("yaffs_mknod failed making object\n")));
1370 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1373 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1374 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1378 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1379 struct nameidata *n)
1381 T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1382 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1385 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1389 struct yaffs_dev *dev;
1390 struct yaffs_obj *obj;
1393 (TSTR("yaffs_unlink %d:%s\n"),
1395 dentry->d_name.name));
1396 obj = yaffs_inode_to_obj(dir);
1399 yaffs_gross_lock(dev);
1401 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1403 if (ret_val == YAFFS_OK) {
1404 dentry->d_inode->i_nlink--;
1406 yaffs_gross_unlock(dev);
1407 mark_inode_dirty(dentry->d_inode);
1408 update_dir_time(dir);
1411 yaffs_gross_unlock(dev);
1418 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1419 struct dentry *dentry)
1421 struct inode *inode = old_dentry->d_inode;
1422 struct yaffs_obj *obj = NULL;
1423 struct yaffs_obj *link = NULL;
1424 struct yaffs_dev *dev;
1426 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1428 obj = yaffs_inode_to_obj(inode);
1431 yaffs_gross_lock(dev);
1433 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1434 link = yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1438 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
1439 d_instantiate(dentry, old_dentry->d_inode);
1440 atomic_inc(&old_dentry->d_inode->i_count);
1442 (TSTR("yaffs_link link count %d i_count %d\n"),
1443 old_dentry->d_inode->i_nlink,
1444 atomic_read(&old_dentry->d_inode->i_count)));
1447 yaffs_gross_unlock(dev);
1450 update_dir_time(dir);
1457 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1458 const char *symname)
1460 struct yaffs_obj *obj;
1461 struct yaffs_dev *dev;
1462 uid_t uid = YCRED(current)->fsuid;
1463 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1465 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1467 dev = yaffs_inode_to_obj(dir)->my_dev;
1468 yaffs_gross_lock(dev);
1469 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1470 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1471 yaffs_gross_unlock(dev);
1474 struct inode *inode;
1476 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1477 d_instantiate(dentry, inode);
1478 update_dir_time(dir);
1479 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1482 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1488 static int yaffs_sync_object(struct file *file, int datasync)
1491 struct yaffs_obj *obj;
1492 struct yaffs_dev *dev;
1493 struct dentry *dentry = file->f_path.dentry;
1495 obj = yaffs_dentry_to_obj(dentry);
1499 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1500 (TSTR("yaffs_sync_object\n")));
1501 yaffs_gross_lock(dev);
1502 yaffs_flush_file(obj, 1, datasync);
1503 yaffs_gross_unlock(dev);
1508 * The VFS layer already does all the dentry stuff for rename.
1510 * NB: POSIX says you can rename an object over an old object of the same name
1512 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1513 struct inode *new_dir, struct dentry *new_dentry)
1515 struct yaffs_dev *dev;
1516 int ret_val = YAFFS_FAIL;
1517 struct yaffs_obj *target;
1519 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1520 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1522 yaffs_gross_lock(dev);
1524 /* Check if the target is an existing directory that is not empty. */
1525 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1526 new_dentry->d_name.name);
1530 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1531 !list_empty(&target->variant.dir_variant.children)) {
1533 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1535 ret_val = YAFFS_FAIL;
1537 /* Now does unlinking internally using shadowing mechanism */
1538 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_rename_obj\n")));
1540 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1541 old_dentry->d_name.name,
1542 yaffs_inode_to_obj(new_dir),
1543 new_dentry->d_name.name);
1545 yaffs_gross_unlock(dev);
1547 if (ret_val == YAFFS_OK) {
1549 new_dentry->d_inode->i_nlink--;
1550 mark_inode_dirty(new_dentry->d_inode);
1553 update_dir_time(old_dir);
1554 if(old_dir != new_dir)
1555 update_dir_time(new_dir);
1562 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1564 struct inode *inode = dentry->d_inode;
1566 struct yaffs_dev *dev;
1569 (TSTR("yaffs_setattr of object %d\n"),
1570 yaffs_inode_to_obj(inode)->obj_id));
1572 /* Fail if a requested resize >= 2GB */
1573 if (attr->ia_valid & ATTR_SIZE &&
1574 (attr->ia_size >> 31))
1578 error = inode_change_ok(inode, attr);
1582 error = yaffs_vfs_setattr(inode, attr);
1583 T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
1584 if (attr->ia_valid & ATTR_SIZE){
1585 yaffs_vfs_setsize(inode,attr->ia_size);
1586 inode->i_blocks = (inode->i_size + 511) >> 9;
1589 dev = yaffs_inode_to_obj(inode)->my_dev;
1590 if (attr->ia_valid & ATTR_SIZE){
1591 T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
1592 (int)(attr->ia_size),(int)(attr->ia_size)));
1594 yaffs_gross_lock(dev);
1595 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
1596 if(result == YAFFS_OK) {
1601 yaffs_gross_unlock(dev);
1606 (TSTR("yaffs_setattr done returning %d\n"),error));
1611 #ifdef CONFIG_YAFFS_XATTR
1612 int yaffs_setxattr(struct dentry *dentry, const char *name,
1613 const void *value, size_t size, int flags)
1615 struct inode *inode = dentry->d_inode;
1617 struct yaffs_dev *dev;
1618 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1621 (TSTR("yaffs_setxattr of object %d\n"),
1628 yaffs_gross_lock(dev);
1629 result = yaffs_set_xattrib(obj, name, value, size, flags);
1630 if(result == YAFFS_OK)
1634 yaffs_gross_unlock(dev);
1638 (TSTR("yaffs_setxattr done returning %d\n"),error));
1644 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
1647 struct inode *inode = dentry->d_inode;
1649 struct yaffs_dev *dev;
1650 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1653 (TSTR("yaffs_getxattr \"%s\" from object %d\n"),
1654 name, obj->obj_id));
1658 yaffs_gross_lock(dev);
1659 error = yaffs_get_xattrib(obj, name, buff, size);
1660 yaffs_gross_unlock(dev);
1664 (TSTR("yaffs_getxattr done returning %d\n"),error));
1669 int yaffs_removexattr(struct dentry *dentry, const char *name)
1671 struct inode *inode = dentry->d_inode;
1673 struct yaffs_dev *dev;
1674 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1677 (TSTR("yaffs_removexattr of object %d\n"),
1684 yaffs_gross_lock(dev);
1685 result = yaffs_remove_xattrib(obj, name);
1686 if(result == YAFFS_OK)
1690 yaffs_gross_unlock(dev);
1694 (TSTR("yaffs_removexattr done returning %d\n"),error));
1699 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
1701 struct inode *inode = dentry->d_inode;
1703 struct yaffs_dev *dev;
1704 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1707 (TSTR("yaffs_listxattr of object %d\n"),
1713 yaffs_gross_lock(dev);
1714 error = yaffs_list_xattrib(obj, buff, size);
1715 yaffs_gross_unlock(dev);
1719 (TSTR("yaffs_listxattr done returning %d\n"),error));
1727 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1729 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1730 struct super_block *sb = dentry->d_sb;
1732 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
1734 yaffs_gross_lock(dev);
1736 buf->f_type = YAFFS_MAGIC;
1737 buf->f_bsize = sb->s_blocksize;
1738 buf->f_namelen = 255;
1740 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
1741 /* Do this if chunk size is not a power of 2 */
1743 uint64_t bytes_in_dev;
1744 uint64_t bytes_free;
1746 bytes_in_dev = ((uint64_t)((dev->param.end_block - dev->param.start_block + 1))) *
1747 ((uint64_t)(dev->param.chunks_per_block * dev->data_bytes_per_chunk));
1749 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
1750 buf->f_blocks = bytes_in_dev;
1752 bytes_free = ((uint64_t)(yaffs_get_n_free_chunks(dev))) *
1753 ((uint64_t)(dev->data_bytes_per_chunk));
1755 do_div(bytes_free, sb->s_blocksize);
1757 buf->f_bfree = bytes_free;
1759 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
1762 (dev->param.end_block - dev->param.start_block + 1) *
1763 dev->param.chunks_per_block /
1764 (sb->s_blocksize / dev->data_bytes_per_chunk);
1766 yaffs_get_n_free_chunks(dev) /
1767 (sb->s_blocksize / dev->data_bytes_per_chunk);
1770 (dev->param.end_block - dev->param.start_block + 1) *
1771 dev->param.chunks_per_block *
1772 (dev->data_bytes_per_chunk / sb->s_blocksize);
1775 yaffs_get_n_free_chunks(dev) *
1776 (dev->data_bytes_per_chunk / sb->s_blocksize);
1781 buf->f_bavail = buf->f_bfree;
1783 yaffs_gross_unlock(dev);
1789 static void yaffs_flush_inodes(struct super_block *sb)
1792 struct yaffs_obj *obj;
1794 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1795 obj = yaffs_inode_to_obj(iptr);
1797 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
1799 yaffs_flush_file(obj,1,0);
1805 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
1807 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1811 yaffs_flush_inodes(sb);
1812 yaffs_update_dirty_dirs(dev);
1813 yaffs_flush_whole_cache(dev);
1815 yaffs_checkpoint_save(dev);
1819 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
1821 unsigned erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
1822 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1823 unsigned scattered = 0; /* Free chunks not in an erased block */
1825 if(erased_chunks < dev->n_free_chunks)
1826 scattered = (dev->n_free_chunks - erased_chunks);
1828 if(!context->bg_running)
1830 else if(scattered < (dev->param.chunks_per_block * 2))
1832 else if(erased_chunks > dev->n_free_chunks/2)
1834 else if(erased_chunks > dev->n_free_chunks/4)
1840 static int yaffs_do_sync_fs(struct super_block *sb,
1841 int request_checkpoint)
1844 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1845 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
1846 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
1849 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1850 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
1852 sb->s_dirt ? "dirty" : "clean",
1853 request_checkpoint ? "checkpoint requested" : "no checkpoint",
1854 oneshot_checkpoint ? " one-shot" : "" ));
1856 yaffs_gross_lock(dev);
1857 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
1858 oneshot_checkpoint) &&
1859 !dev->is_checkpointed;
1861 if (sb->s_dirt || do_checkpoint) {
1862 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
1864 if(oneshot_checkpoint)
1865 yaffs_auto_checkpoint &= ~4;
1867 yaffs_gross_unlock(dev);
1873 * yaffs background thread functions .
1874 * yaffs_bg_thread_fn() the thread function
1875 * yaffs_bg_start() launches the background thread.
1876 * yaffs_bg_stop() cleans up the background thread.
1879 * The thread should only run after the yaffs is initialised
1880 * The thread should be stopped before yaffs is unmounted.
1881 * The thread should not do any writing while the fs is in read only.
1885 void yaffs_background_waker(unsigned long data)
1887 wake_up_process((struct task_struct *)data);
1890 static int yaffs_bg_thread_fn(void *data)
1892 struct yaffs_dev *dev = (struct yaffs_dev *)data;
1893 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1894 unsigned long now = jiffies;
1895 unsigned long next_dir_update = now;
1896 unsigned long next_gc = now;
1897 unsigned long expires;
1898 unsigned int urgency;
1901 struct timer_list timer;
1903 T(YAFFS_TRACE_BACKGROUND,
1904 (TSTR("yaffs_background starting for dev %p\n"),
1908 while(context->bg_running){
1909 T(YAFFS_TRACE_BACKGROUND,
1910 (TSTR("yaffs_background\n")));
1912 if(kthread_should_stop())
1918 yaffs_gross_lock(dev);
1922 if(time_after(now, next_dir_update) && yaffs_bg_enable){
1923 yaffs_update_dirty_dirs(dev);
1924 next_dir_update = now + HZ;
1927 if(time_after(now,next_gc) && yaffs_bg_enable){
1928 if(!dev->is_checkpointed){
1929 urgency = yaffs_bg_gc_urgency(dev);
1930 gc_result = yaffs_bg_gc(dev, urgency);
1932 next_gc = now + HZ/20+1;
1933 else if(urgency > 0)
1934 next_gc = now + HZ/10+1;
1936 next_gc = now + HZ * 2;
1938 * gc not running so set to next_dir_update
1939 * to cut down on wake ups
1941 next_gc = next_dir_update;
1943 yaffs_gross_unlock(dev);
1944 expires = next_dir_update;
1945 if (time_before(next_gc,expires))
1947 if(time_before(expires,now))
1950 Y_INIT_TIMER(&timer);
1951 timer.expires = expires+1;
1952 timer.data = (unsigned long) current;
1953 timer.function = yaffs_background_waker;
1955 set_current_state(TASK_INTERRUPTIBLE);
1958 del_timer_sync(&timer);
1964 static int yaffs_bg_start(struct yaffs_dev *dev)
1967 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1972 context->bg_running = 1;
1974 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
1975 (void *)dev,"yaffs-bg-%d",context->mount_id);
1977 if(IS_ERR(context->bg_thread)){
1978 retval = PTR_ERR(context->bg_thread);
1979 context->bg_thread = NULL;
1980 context->bg_running = 0;
1985 static void yaffs_bg_stop(struct yaffs_dev *dev)
1987 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
1989 ctxt->bg_running = 0;
1991 if( ctxt->bg_thread){
1992 kthread_stop(ctxt->bg_thread);
1993 ctxt->bg_thread = NULL;
1998 static void yaffs_write_super(struct super_block *sb)
2000 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2002 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2003 (TSTR("yaffs_write_super%s\n"),
2004 request_checkpoint ? " checkpt" : ""));
2006 yaffs_do_sync_fs(sb, request_checkpoint);
2011 static int yaffs_sync_fs(struct super_block *sb, int wait)
2013 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2015 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2016 (TSTR("yaffs_sync_fs%s\n"),
2017 request_checkpoint ? " checkpt" : ""));
2019 yaffs_do_sync_fs(sb, request_checkpoint);
2024 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2026 struct inode *inode;
2027 struct yaffs_obj *obj;
2028 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2031 (TSTR("yaffs_iget for %lu\n"), ino));
2033 inode = iget_locked(sb, ino);
2035 return ERR_PTR(-ENOMEM);
2036 if (!(inode->i_state & I_NEW))
2039 /* NB This is called as a side effect of other functions, but
2040 * we had to release the lock to prevent deadlocks, so
2041 * need to lock again.
2044 yaffs_gross_lock(dev);
2046 obj = yaffs_find_by_number(dev, inode->i_ino);
2048 yaffs_fill_inode_from_obj(inode, obj);
2050 yaffs_gross_unlock(dev);
2052 unlock_new_inode(inode);
2056 static LIST_HEAD(yaffs_context_list);
2057 struct semaphore yaffs_context_lock;
2059 static void yaffs_put_super(struct super_block *sb)
2061 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2063 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2065 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2066 (TSTR("Shutting down yaffs background thread\n")));
2068 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2069 (TSTR("yaffs background thread shut down\n")));
2071 yaffs_gross_lock(dev);
2073 yaffs_flush_super(sb,1);
2075 if (yaffs_dev_to_lc(dev)->put_super_fn)
2076 yaffs_dev_to_lc(dev)->put_super_fn(sb);
2079 yaffs_deinitialise(dev);
2081 yaffs_gross_unlock(dev);
2083 down(&yaffs_context_lock);
2084 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2085 up(&yaffs_context_lock);
2087 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2088 YFREE(yaffs_dev_to_lc(dev)->spare_buffer);
2089 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2096 static void yaffs_mtd_put_super(struct super_block *sb)
2098 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
2103 put_mtd_device(mtd);
2107 static void yaffs_touch_super(struct yaffs_dev *dev)
2109 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
2111 T(YAFFS_TRACE_OS, (TSTR("yaffs_touch_super() sb = %p\n"), sb));
2116 struct yaffs_options {
2118 int skip_checkpoint_read;
2119 int skip_checkpoint_write;
2122 int tags_ecc_overridden;
2123 int lazy_loading_enabled;
2124 int lazy_loading_overridden;
2125 int empty_lost_and_found;
2126 int empty_lost_and_found_overridden;
2129 #define MAX_OPT_LEN 30
2130 static int yaffs_parse_options(struct yaffs_options *options, const char *options_str)
2132 char cur_opt[MAX_OPT_LEN + 1];
2136 /* Parse through the options which is a comma seperated list */
2138 while (options_str && *options_str && !error) {
2139 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2142 while(*options_str == ',')
2145 while (*options_str && *options_str != ',') {
2146 if (p < MAX_OPT_LEN) {
2147 cur_opt[p] = *options_str;
2153 if (!strcmp(cur_opt, "inband-tags"))
2154 options->inband_tags = 1;
2155 else if (!strcmp(cur_opt, "tags-ecc-off")){
2156 options->tags_ecc_on = 0;
2157 options->tags_ecc_overridden=1;
2158 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2159 options->tags_ecc_on = 1;
2160 options->tags_ecc_overridden = 1;
2161 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2162 options->lazy_loading_enabled = 0;
2163 options->lazy_loading_overridden=1;
2164 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2165 options->lazy_loading_enabled = 1;
2166 options->lazy_loading_overridden = 1;
2167 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2168 options->empty_lost_and_found = 0;
2169 options->empty_lost_and_found_overridden=1;
2170 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2171 options->empty_lost_and_found = 1;
2172 options->empty_lost_and_found_overridden=1;
2173 } else if (!strcmp(cur_opt, "no-cache"))
2174 options->no_cache = 1;
2175 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2176 options->skip_checkpoint_read = 1;
2177 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2178 options->skip_checkpoint_write = 1;
2179 else if (!strcmp(cur_opt, "no-checkpoint")) {
2180 options->skip_checkpoint_read = 1;
2181 options->skip_checkpoint_write = 1;
2183 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2192 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2193 struct super_block *sb,
2194 void *data, int silent)
2197 struct inode *inode = NULL;
2198 struct dentry *root;
2199 struct yaffs_dev *dev = 0;
2200 char devname_buf[BDEVNAME_SIZE + 1];
2201 struct mtd_info *mtd;
2203 char *data_str = (char *)data;
2204 struct yaffs_linux_context *context = NULL;
2205 struct yaffs_param *param;
2209 struct yaffs_options options;
2213 struct yaffs_linux_context *context_iterator;
2214 struct list_head *l;
2216 sb->s_magic = YAFFS_MAGIC;
2217 sb->s_op = &yaffs_super_ops;
2218 sb->s_flags |= MS_NOATIME;
2220 read_only =((sb->s_flags & MS_RDONLY) != 0);
2222 sb->s_export_op = &yaffs_export_ops;
2225 printk(KERN_INFO "yaffs: sb is NULL\n");
2226 else if (!sb->s_dev)
2227 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2228 else if (!yaffs_devname(sb, devname_buf))
2229 printk(KERN_INFO "yaffs: devname is NULL\n");
2231 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2233 yaffs_devname(sb, devname_buf),
2234 read_only ? "ro" : "rw");
2239 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2241 memset(&options, 0, sizeof(options));
2243 if (yaffs_parse_options(&options, data_str)) {
2244 /* Option parsing failed */
2249 sb->s_blocksize = PAGE_CACHE_SIZE;
2250 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2253 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffs_version));
2255 (TSTR("yaffs_read_super: block size %d\n"),
2256 (int)(sb->s_blocksize)));
2258 T(YAFFS_TRACE_ALWAYS,
2259 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2260 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2261 yaffs_devname(sb, devname_buf)));
2263 /* Check it's an mtd device..... */
2264 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2265 return NULL; /* This isn't an mtd device */
2267 /* Get the device */
2268 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2270 T(YAFFS_TRACE_ALWAYS,
2271 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2275 /* Check it's NAND */
2276 if (mtd->type != MTD_NANDFLASH) {
2277 T(YAFFS_TRACE_ALWAYS,
2278 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2283 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2284 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2285 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2286 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2287 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2288 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2289 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2290 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2291 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2292 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2293 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2295 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2297 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2298 T(YAFFS_TRACE_ALWAYS,
2299 (TSTR("yaffs: auto selecting yaffs2\n")));
2303 /* Added NCB 26/5/2006 for completeness */
2304 if (yaffs_version == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2305 T(YAFFS_TRACE_ALWAYS,
2306 (TSTR("yaffs: auto selecting yaffs1\n")));
2312 if (yaffs_version == 2) {
2313 /* Check for version 2 style functions */
2315 !mtd->block_isbad ||
2316 !mtd->block_markbad ||
2319 !mtd->read_oob || !mtd->write_oob) {
2320 T(YAFFS_TRACE_ALWAYS,
2321 (TSTR("yaffs: MTD device does not support required "
2326 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2327 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2328 !options.inband_tags) {
2329 T(YAFFS_TRACE_ALWAYS,
2330 (TSTR("yaffs: MTD device does not have the "
2331 "right page sizes\n")));
2335 /* Check for V1 style functions */
2339 !mtd->read_oob || !mtd->write_oob) {
2340 T(YAFFS_TRACE_ALWAYS,
2341 (TSTR("yaffs: MTD device does not support required "
2346 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2347 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2348 T(YAFFS_TRACE_ALWAYS,
2349 (TSTR("yaffs: MTD device does not support have the "
2350 "right page sizes\n")));
2355 /* OK, so if we got here, we have an MTD that's NAND and looks
2356 * like it has the right capabilities
2357 * Set the struct yaffs_dev up for mtd
2360 if (!read_only && !(mtd->flags & MTD_WRITEABLE)){
2362 printk(KERN_INFO "yaffs: mtd is read only, setting superblock read only");
2363 sb->s_flags |= MS_RDONLY;
2366 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2367 context = kmalloc(sizeof(struct yaffs_linux_context),GFP_KERNEL);
2369 if(!dev || !context ){
2379 /* Deep shit could not allocate device structure */
2380 T(YAFFS_TRACE_ALWAYS,
2381 (TSTR("yaffs_read_super: Failed trying to allocate "
2382 "struct yaffs_dev. \n")));
2385 memset(dev, 0, sizeof(struct yaffs_dev));
2386 param = &(dev->param);
2388 memset(context,0,sizeof(struct yaffs_linux_context));
2389 dev->os_context = context;
2390 INIT_LIST_HEAD(&(context->context_list));
2392 context->super = sb;
2394 dev->read_only = read_only;
2396 sb->s_fs_info = dev;
2398 dev->driver_context = mtd;
2399 param->name = mtd->name;
2401 /* Set up the memory size parameters.... */
2403 n_blocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2405 param->start_block = 0;
2406 param->end_block = n_blocks - 1;
2407 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2408 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2409 param->n_reserved_blocks = 5;
2410 param->n_caches = (options.no_cache) ? 0 : 10;
2411 param->inband_tags = options.inband_tags;
2413 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2414 param->disable_lazy_load = 1;
2416 #ifdef CONFIG_YAFFS_XATTR
2417 param->enable_xattr = 1;
2419 if(options.lazy_loading_overridden)
2420 param->disable_lazy_load = !options.lazy_loading_enabled;
2422 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2423 param->no_tags_ecc = 1;
2426 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2428 param->defered_dir_update = 1;
2431 if(options.tags_ecc_overridden)
2432 param->no_tags_ecc = !options.tags_ecc_on;
2434 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2435 param->empty_lost_n_found = 1;
2438 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2439 param->refresh_period = 0;
2441 param->refresh_period = 500;
2444 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2445 param->always_check_erased = 1;
2448 if(options.empty_lost_and_found_overridden)
2449 param->empty_lost_n_found = options.empty_lost_and_found;
2451 /* ... and the functions. */
2452 if (yaffs_version == 2) {
2453 param->write_chunk_tags_fn =
2454 nandmtd2_write_chunk_tags;
2455 param->read_chunk_tags_fn =
2456 nandmtd2_read_chunk_tags;
2457 param->bad_block_fn = nandmtd2_mark_block_bad;
2458 param->query_block_fn = nandmtd2_query_block;
2459 yaffs_dev_to_lc(dev)->spare_buffer = YMALLOC(mtd->oobsize);
2460 param->is_yaffs2 = 1;
2461 param->total_bytes_per_chunk = mtd->writesize;
2462 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2463 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2465 param->start_block = 0;
2466 param->end_block = n_blocks - 1;
2468 /* use the MTD interface in yaffs_mtdif1.c */
2469 param->write_chunk_tags_fn =
2470 nandmtd1_write_chunk_tags;
2471 param->read_chunk_tags_fn =
2472 nandmtd1_read_chunk_tags;
2473 param->bad_block_fn = nandmtd1_mark_block_bad;
2474 param->query_block_fn = nandmtd1_query_block;
2475 param->is_yaffs2 = 0;
2477 /* ... and common functions */
2478 param->erase_fn = nandmtd_erase_block;
2479 param->initialise_flash_fn = nandmtd_initialise;
2481 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2483 param->sb_dirty_fn = yaffs_touch_super;
2484 param->gc_control = yaffs_gc_control_callback;
2486 yaffs_dev_to_lc(dev)->super= sb;
2489 #ifndef CONFIG_YAFFS_DOES_ECC
2490 param->use_nand_ecc = 1;
2493 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2494 param->wide_tnodes_disabled = 1;
2497 param->skip_checkpt_rd = options.skip_checkpoint_read;
2498 param->skip_checkpt_wr = options.skip_checkpoint_write;
2500 down(&yaffs_context_lock);
2501 /* Get a mount id */
2503 for(mount_id=0; ! found; mount_id++){
2505 list_for_each(l,&yaffs_context_list){
2506 context_iterator = list_entry(l,struct yaffs_linux_context,context_list);
2507 if(context_iterator->mount_id == mount_id)
2511 context->mount_id = mount_id;
2513 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list), &yaffs_context_list);
2514 up(&yaffs_context_lock);
2516 /* Directory search handling...*/
2517 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2518 param->remove_obj_fn = yaffs_remove_obj_callback;
2520 init_MUTEX(&(yaffs_dev_to_lc(dev)->gross_lock));
2522 yaffs_gross_lock(dev);
2524 err = yaffs_guts_initialise(dev);
2527 (TSTR("yaffs_read_super: guts initialised %s\n"),
2528 (err == YAFFS_OK) ? "OK" : "FAILED"));
2531 yaffs_bg_start(dev);
2533 if(!context->bg_thread)
2534 param->defered_dir_update = 0;
2537 /* Release lock before yaffs_get_inode() */
2538 yaffs_gross_unlock(dev);
2540 /* Create root inode */
2541 if (err == YAFFS_OK)
2542 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2548 inode->i_op = &yaffs_dir_inode_operations;
2549 inode->i_fop = &yaffs_dir_operations;
2551 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
2553 root = d_alloc_root(inode);
2555 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
2562 sb->s_dirt = !dev->is_checkpointed;
2563 T(YAFFS_TRACE_ALWAYS,
2564 (TSTR("yaffs_read_super: is_checkpointed %d\n"),
2565 dev->is_checkpointed));
2567 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
2572 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2575 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2578 static int yaffs_read_super(struct file_system_type *fs,
2579 int flags, const char *dev_name,
2580 void *data, struct vfsmount *mnt)
2583 return get_sb_bdev(fs, flags, dev_name, data,
2584 yaffs_internal_read_super_mtd, mnt);
2587 static struct file_system_type yaffs_fs_type = {
2588 .owner = THIS_MODULE,
2590 .get_sb = yaffs_read_super,
2591 .kill_sb = kill_block_super,
2592 .fs_flags = FS_REQUIRES_DEV,
2596 #ifdef CONFIG_YAFFS_YAFFS2
2598 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2601 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2604 static int yaffs2_read_super(struct file_system_type *fs,
2605 int flags, const char *dev_name, void *data,
2606 struct vfsmount *mnt)
2608 return get_sb_bdev(fs, flags, dev_name, data,
2609 yaffs2_internal_read_super_mtd, mnt);
2612 static struct file_system_type yaffs2_fs_type = {
2613 .owner = THIS_MODULE,
2615 .get_sb = yaffs2_read_super,
2616 .kill_sb = kill_block_super,
2617 .fs_flags = FS_REQUIRES_DEV,
2619 #endif /* CONFIG_YAFFS_YAFFS2 */
2621 static struct proc_dir_entry *my_proc_entry;
2622 static struct proc_dir_entry *debug_proc_entry;
2624 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev * dev)
2626 buf += sprintf(buf, "start_block........... %d\n", dev->param.start_block);
2627 buf += sprintf(buf, "end_block............. %d\n", dev->param.end_block);
2628 buf += sprintf(buf, "total_bytes_per_chunk. %d\n", dev->param.total_bytes_per_chunk);
2629 buf += sprintf(buf, "use_nand_ecc.......... %d\n", dev->param.use_nand_ecc);
2630 buf += sprintf(buf, "no_tags_ecc........... %d\n", dev->param.no_tags_ecc);
2631 buf += sprintf(buf, "is_yaffs2............. %d\n", dev->param.is_yaffs2);
2632 buf += sprintf(buf, "inband_tags........... %d\n", dev->param.inband_tags);
2633 buf += sprintf(buf, "empty_lost_n_found.... %d\n", dev->param.empty_lost_n_found);
2634 buf += sprintf(buf, "disable_lazy_load..... %d\n", dev->param.disable_lazy_load);
2635 buf += sprintf(buf, "refresh_period........ %d\n", dev->param.refresh_period);
2636 buf += sprintf(buf, "n_caches.............. %d\n", dev->param.n_caches);
2637 buf += sprintf(buf, "n_reserved_blocks..... %d\n", dev->param.n_reserved_blocks);
2638 buf += sprintf(buf, "always_check_erased... %d\n", dev->param.always_check_erased);
2640 buf += sprintf(buf, "\n");
2646 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev * dev)
2648 buf += sprintf(buf, "data_bytes_per_chunk.. %d\n", dev->data_bytes_per_chunk);
2649 buf += sprintf(buf, "chunk_grp_bits........ %d\n", dev->chunk_grp_bits);
2650 buf += sprintf(buf, "chunk_grp_size........ %d\n", dev->chunk_grp_size);
2651 buf += sprintf(buf, "n_erased_blocks....... %d\n", dev->n_erased_blocks);
2652 buf += sprintf(buf, "blocks_in_checkpt..... %d\n", dev->blocks_in_checkpt);
2653 buf += sprintf(buf, "\n");
2654 buf += sprintf(buf, "n_tnodes.............. %d\n", dev->n_tnodes);
2655 buf += sprintf(buf, "n_obj................. %d\n", dev->n_obj);
2656 buf += sprintf(buf, "n_free_chunks......... %d\n", dev->n_free_chunks);
2657 buf += sprintf(buf, "\n");
2658 buf += sprintf(buf, "n_page_writes......... %u\n", dev->n_page_writes);
2659 buf += sprintf(buf, "n_page_reads.......... %u\n", dev->n_page_reads);
2660 buf += sprintf(buf, "n_erasures............ %u\n", dev->n_erasures);
2661 buf += sprintf(buf, "n_gc_copies........... %u\n", dev->n_gc_copies);
2662 buf += sprintf(buf, "all_gcs............... %u\n", dev->all_gcs);
2663 buf += sprintf(buf, "passive_gc_count...... %u\n", dev->passive_gc_count);
2664 buf += sprintf(buf, "oldest_dirty_gc_count. %u\n", dev->oldest_dirty_gc_count);
2665 buf += sprintf(buf, "n_gc_blocks........... %u\n", dev->n_gc_blocks);
2666 buf += sprintf(buf, "bg_gcs................ %u\n", dev->bg_gcs);
2667 buf += sprintf(buf, "n_retired_writes...... %u\n", dev->n_retired_writes);
2668 buf += sprintf(buf, "n_retired_blocks...... %u\n", dev->n_retired_blocks);
2669 buf += sprintf(buf, "n_ecc_fixed........... %u\n", dev->n_ecc_fixed);
2670 buf += sprintf(buf, "n_ecc_unfixed......... %u\n", dev->n_ecc_unfixed);
2671 buf += sprintf(buf, "n_tags_ecc_fixed...... %u\n", dev->n_tags_ecc_fixed);
2672 buf += sprintf(buf, "n_tags_ecc_unfixed.... %u\n", dev->n_tags_ecc_unfixed);
2673 buf += sprintf(buf, "cache_hits............ %u\n", dev->cache_hits);
2674 buf += sprintf(buf, "n_deleted_files....... %u\n", dev->n_deleted_files);
2675 buf += sprintf(buf, "n_unlinked_files...... %u\n", dev->n_unlinked_files);
2676 buf += sprintf(buf, "refresh_count......... %u\n", dev->refresh_count);
2677 buf += sprintf(buf, "n_bg_deletions........ %u\n", dev->n_bg_deletions);
2682 static int yaffs_proc_read(char *page,
2684 off_t offset, int count, int *eof, void *data)
2686 struct list_head *item;
2691 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2692 * We use 'offset' (*ppos) to indicate where we are in dev_list.
2693 * This also assumes the user has posted a read buffer large
2694 * enough to hold the complete output; but that's life in /proc.
2699 /* Print header first */
2701 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__"\n");
2703 buf += sprintf(buf,"\n");
2707 down(&yaffs_context_lock);
2709 /* Locate and print the Nth entry. Order N-squared but N is small. */
2710 list_for_each(item, &yaffs_context_list) {
2711 struct yaffs_linux_context *dc = list_entry(item, struct yaffs_linux_context, context_list);
2712 struct yaffs_dev *dev = dc->dev;
2714 if (n < (step & ~1)) {
2719 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
2720 buf = yaffs_dump_dev_part0(buf, dev);
2722 buf = yaffs_dump_dev_part1(buf, dev);
2726 up(&yaffs_context_lock);
2729 return buf - page < count ? buf - page : count;
2732 static int yaffs_stats_proc_read(char *page,
2734 off_t offset, int count, int *eof, void *data)
2736 struct list_head *item;
2740 down(&yaffs_context_lock);
2742 /* Locate and print the Nth entry. Order N-squared but N is small. */
2743 list_for_each(item, &yaffs_context_list) {
2744 struct yaffs_linux_context *dc = list_entry(item, struct yaffs_linux_context, context_list);
2745 struct yaffs_dev *dev = dc->dev;
2749 erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2751 buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
2752 n, dev->n_free_chunks, erased_chunks,
2753 dev->bg_gcs, dev->oldest_dirty_gc_count,
2754 dev->n_obj, dev->n_tnodes);
2756 up(&yaffs_context_lock);
2759 return buf - page < count ? buf - page : count;
2763 * Set the verbosity of the warnings and error messages.
2765 * Note that the names can only be a..z or _ with the current code.
2770 unsigned mask_bitfield;
2772 {"allocate", YAFFS_TRACE_ALLOCATE},
2773 {"always", YAFFS_TRACE_ALWAYS},
2774 {"background", YAFFS_TRACE_BACKGROUND},
2775 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2776 {"buffers", YAFFS_TRACE_BUFFERS},
2777 {"bug", YAFFS_TRACE_BUG},
2778 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2779 {"deletion", YAFFS_TRACE_DELETION},
2780 {"erase", YAFFS_TRACE_ERASE},
2781 {"error", YAFFS_TRACE_ERROR},
2782 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2783 {"gc", YAFFS_TRACE_GC},
2784 {"lock", YAFFS_TRACE_LOCK},
2785 {"mtd", YAFFS_TRACE_MTD},
2786 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2787 {"os", YAFFS_TRACE_OS},
2788 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2789 {"scan", YAFFS_TRACE_SCAN},
2790 {"tracing", YAFFS_TRACE_TRACING},
2791 {"sync", YAFFS_TRACE_SYNC},
2792 {"write", YAFFS_TRACE_WRITE},
2794 {"verify", YAFFS_TRACE_VERIFY},
2795 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2796 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2797 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2799 {"all", 0xffffffff},
2804 #define MAX_MASK_NAME_LENGTH 40
2805 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2806 unsigned long count, void *data)
2808 unsigned rg = 0, mask_bitfield;
2812 char substring[MAX_MASK_NAME_LENGTH + 1];
2818 rg = yaffs_trace_mask;
2820 while (!done && (pos < count)) {
2822 while ((pos < count) && isspace(buf[pos]))
2839 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2841 if (end > buf + pos) {
2842 mask_name = "numeral";
2843 len = end - (buf + pos);
2847 for (x = buf + pos, i = 0;
2848 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2849 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2851 substring[i] = '\0';
2853 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2854 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
2855 mask_name = mask_flags[i].mask_name;
2856 mask_bitfield = mask_flags[i].mask_bitfield;
2863 if (mask_name != NULL) {
2867 rg &= ~mask_bitfield;
2870 rg |= mask_bitfield;
2876 rg |= mask_bitfield;
2882 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
2884 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
2886 if (rg & YAFFS_TRACE_ALWAYS) {
2887 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2889 flag = ((rg & mask_flags[i].mask_bitfield) ==
2890 mask_flags[i].mask_bitfield) ? '+' : '-';
2891 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
2899 static int yaffs_proc_write(struct file *file, const char *buf,
2900 unsigned long count, void *data)
2902 return yaffs_proc_write_trace_options(file, buf, count, data);
2905 /* Stuff to handle installation of file systems */
2906 struct file_system_to_install {
2907 struct file_system_type *fst;
2911 static struct file_system_to_install fs_to_install[] = {
2912 {&yaffs_fs_type, 0},
2913 {&yaffs2_fs_type, 0},
2917 static int __init init_yaffs_fs(void)
2920 struct file_system_to_install *fsinst;
2922 T(YAFFS_TRACE_ALWAYS,
2923 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
2925 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2926 T(YAFFS_TRACE_ALWAYS,
2927 (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
2933 init_MUTEX(&yaffs_context_lock);
2935 /* Install the proc_fs entries */
2936 my_proc_entry = create_proc_entry("yaffs",
2940 if (my_proc_entry) {
2941 my_proc_entry->write_proc = yaffs_proc_write;
2942 my_proc_entry->read_proc = yaffs_proc_read;
2943 my_proc_entry->data = NULL;
2947 debug_proc_entry = create_proc_entry("yaffs_stats",
2951 if (debug_proc_entry) {
2952 debug_proc_entry->write_proc = NULL;
2953 debug_proc_entry->read_proc = yaffs_stats_proc_read;
2954 debug_proc_entry->data = NULL;
2958 /* Now add the file system entries */
2960 fsinst = fs_to_install;
2962 while (fsinst->fst && !error) {
2963 error = register_filesystem(fsinst->fst);
2965 fsinst->installed = 1;
2969 /* Any errors? uninstall */
2971 fsinst = fs_to_install;
2973 while (fsinst->fst) {
2974 if (fsinst->installed) {
2975 unregister_filesystem(fsinst->fst);
2976 fsinst->installed = 0;
2985 static void __exit exit_yaffs_fs(void)
2988 struct file_system_to_install *fsinst;
2990 T(YAFFS_TRACE_ALWAYS,
2991 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
2993 remove_proc_entry("yaffs", YPROC_ROOT);
2994 remove_proc_entry("yaffs_stats", YPROC_ROOT);
2996 fsinst = fs_to_install;
2998 while (fsinst->fst) {
2999 if (fsinst->installed) {
3000 unregister_filesystem(fsinst->fst);
3001 fsinst->installed = 0;
3007 module_init(init_yaffs_fs)
3008 module_exit(exit_yaffs_fs)
3010 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3011 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3012 MODULE_LICENSE("GPL");