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>
60 #include <linux/statfs.h>
62 #define UnlockPage(p) unlock_page(p)
63 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
65 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
67 #define YPROC_ROOT NULL
69 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
71 #define WRITE_SIZE_STR "writesize"
72 #define WRITE_SIZE(mtd) ((mtd)->writesize)
74 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
76 uint64_t result = partition_size;
77 do_div(result, block_size);
78 return (uint32_t) result;
81 #include <linux/uaccess.h>
82 #include <linux/mtd/mtd.h>
85 #include "yaffs_trace.h"
86 #include "yaffs_guts.h"
87 #include "yaffs_attribs.h"
89 #include "yaffs_linux.h"
91 #include "yaffs_mtdif.h"
92 #include "yaffs_mtdif1.h"
93 #include "yaffs_mtdif2.h"
95 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
96 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
97 unsigned int yaffs_auto_checkpoint = 1;
98 unsigned int yaffs_gc_control = 1;
99 unsigned int yaffs_bg_enable = 1;
101 /* Module Parameters */
102 module_param(yaffs_trace_mask, uint, 0644);
103 module_param(yaffs_wr_attempts, uint, 0644);
104 module_param(yaffs_auto_checkpoint, uint, 0644);
105 module_param(yaffs_gc_control, uint, 0644);
106 module_param(yaffs_bg_enable, uint, 0644);
108 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
110 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
112 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
113 #define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
114 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
115 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
117 #define update_dir_time(dir) do {\
118 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
121 static void yaffs_put_super(struct super_block *sb);
123 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
125 static ssize_t yaffs_hold_space(struct file *f);
126 static void yaffs_release_space(struct file *f);
128 static int yaffs_file_flush(struct file *file, fl_owner_t id);
130 static int yaffs_sync_object(struct file *file, int datasync);
132 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
134 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
135 struct nameidata *n);
136 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
137 struct nameidata *n);
138 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
139 struct dentry *dentry);
140 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
141 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
142 const char *symname);
143 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
145 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
147 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
148 struct inode *new_dir, struct dentry *new_dentry);
149 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
151 static int yaffs_sync_fs(struct super_block *sb, int wait);
152 static void yaffs_write_super(struct super_block *sb);
154 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
156 #ifdef YAFFS_HAS_PUT_INODE
157 static void yaffs_put_inode(struct inode *inode);
160 static void yaffs_evict_inode(struct inode *);
162 static int yaffs_readpage(struct file *file, struct page *page);
163 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
165 #ifdef CONFIG_YAFFS_XATTR
166 static int yaffs_setxattr(struct dentry *dentry, const char *name,
167 const void *value, size_t size, int flags);
168 static ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
170 static int yaffs_removexattr(struct dentry *dentry, const char *name);
171 static ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
174 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
175 loff_t pos, unsigned len, unsigned flags,
176 struct page **pagep, void **fsdata);
177 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
178 loff_t pos, unsigned len, unsigned copied,
179 struct page *pg, void *fsdadata);
181 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
183 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
184 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
186 static void yaffs_touch_super(struct yaffs_dev *dev);
188 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
190 static struct address_space_operations yaffs_file_address_operations = {
191 .readpage = yaffs_readpage,
192 .writepage = yaffs_writepage,
193 .write_begin = yaffs_write_begin,
194 .write_end = yaffs_write_end,
197 static const struct file_operations yaffs_file_operations = {
198 .read = do_sync_read,
199 .write = do_sync_write,
200 .aio_read = generic_file_aio_read,
201 .aio_write = generic_file_aio_write,
202 .mmap = generic_file_mmap,
203 .flush = yaffs_file_flush,
204 .fsync = yaffs_sync_object,
205 .splice_read = generic_file_splice_read,
206 .splice_write = generic_file_splice_write,
207 .llseek = generic_file_llseek,
210 static const struct inode_operations yaffs_file_inode_operations = {
211 .setattr = yaffs_setattr,
212 #ifdef CONFIG_YAFFS_XATTR
213 .setxattr = yaffs_setxattr,
214 .getxattr = yaffs_getxattr,
215 .listxattr = yaffs_listxattr,
216 .removexattr = yaffs_removexattr,
220 static const struct inode_operations yaffs_symlink_inode_operations = {
221 .readlink = yaffs_readlink,
222 .follow_link = yaffs_follow_link,
223 .put_link = yaffs_put_link,
224 .setattr = yaffs_setattr,
225 #ifdef CONFIG_YAFFS_XATTR
226 .setxattr = yaffs_setxattr,
227 .getxattr = yaffs_getxattr,
228 .listxattr = yaffs_listxattr,
229 .removexattr = yaffs_removexattr,
233 static const struct inode_operations yaffs_dir_inode_operations = {
234 .create = yaffs_create,
235 .lookup = yaffs_lookup,
237 .unlink = yaffs_unlink,
238 .symlink = yaffs_symlink,
239 .mkdir = yaffs_mkdir,
240 .rmdir = yaffs_unlink,
241 .mknod = yaffs_mknod,
242 .rename = yaffs_rename,
243 .setattr = yaffs_setattr,
244 #ifdef CONFIG_YAFFS_XATTR
245 .setxattr = yaffs_setxattr,
246 .getxattr = yaffs_getxattr,
247 .listxattr = yaffs_listxattr,
248 .removexattr = yaffs_removexattr,
252 static const struct file_operations yaffs_dir_operations = {
253 .read = generic_read_dir,
254 .readdir = yaffs_readdir,
255 .fsync = yaffs_sync_object,
256 .llseek = generic_file_llseek,
259 static const struct super_operations yaffs_super_ops = {
260 .statfs = yaffs_statfs,
261 #ifdef YAFFS_HAS_PUT_INODE
262 .put_inode = yaffs_put_inode,
264 .put_super = yaffs_put_super,
265 .evict_inode = yaffs_evict_inode,
266 .sync_fs = yaffs_sync_fs,
267 .write_super = yaffs_write_super,
270 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
272 setattr_copy(inode, attr);
276 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
278 truncate_setsize(inode, newsize);
282 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
284 return yaffs_gc_control;
287 static void yaffs_gross_lock(struct yaffs_dev *dev)
289 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
290 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
291 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
294 static void yaffs_gross_unlock(struct yaffs_dev *dev)
296 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
297 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
300 /* ExportFS support */
301 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
304 return Y_IGET(sb, ino);
307 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
308 struct fid *fid, int fh_len,
311 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
312 yaffs2_nfs_get_inode);
315 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
316 struct fid *fid, int fh_len,
319 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
320 yaffs2_nfs_get_inode);
323 struct dentry *yaffs2_get_parent(struct dentry *dentry)
326 struct super_block *sb = dentry->d_inode->i_sb;
327 struct dentry *parent = ERR_PTR(-ENOENT);
329 unsigned long parent_ino;
330 struct yaffs_obj *d_obj;
331 struct yaffs_obj *parent_obj;
333 d_obj = yaffs_inode_to_obj(dentry->d_inode);
336 parent_obj = d_obj->parent;
338 parent_ino = yaffs_get_obj_inode(parent_obj);
339 inode = Y_IGET(sb, parent_ino);
342 parent = ERR_CAST(inode);
344 parent = d_obtain_alias(inode);
345 if (!IS_ERR(parent)) {
346 parent = ERR_PTR(-ENOMEM);
356 /* Just declare a zero structure as a NULL value implies
357 * using the default functions of exportfs.
360 static struct export_operations yaffs_export_ops = {
361 .fh_to_dentry = yaffs2_fh_to_dentry,
362 .fh_to_parent = yaffs2_fh_to_parent,
363 .get_parent = yaffs2_get_parent,
366 /*-----------------------------------------------------------------*/
367 /* Directory search context allows us to unlock access to yaffs during
368 * filldir without causing problems with the directory being modified.
369 * This is similar to the tried and tested mechanism used in yaffs direct.
371 * A search context iterates along a doubly linked list of siblings in the
372 * directory. If the iterating object is deleted then this would corrupt
373 * the list iteration, likely causing a crash. The search context avoids
374 * this by using the remove_obj_fn to move the search context to the
375 * next object before the object is deleted.
377 * Many readdirs (and thus seach conexts) may be alive simulateously so
378 * each struct yaffs_dev has a list of these.
380 * A seach context lives for the duration of a readdir.
382 * All these functions must be called while yaffs is locked.
385 struct yaffs_search_context {
386 struct yaffs_dev *dev;
387 struct yaffs_obj *dir_obj;
388 struct yaffs_obj *next_return;
389 struct list_head others;
393 * yaffs_new_search() creates a new search context, initialises it and
394 * adds it to the device's search context list.
396 * Called at start of readdir.
398 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
400 struct yaffs_dev *dev = dir->my_dev;
401 struct yaffs_search_context *sc =
402 YMALLOC(sizeof(struct yaffs_search_context));
406 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
407 sc->next_return = NULL;
410 list_entry(dir->variant.dir_variant.children.next,
411 struct yaffs_obj, siblings);
412 INIT_LIST_HEAD(&sc->others);
413 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
419 * yaffs_search_end() disposes of a search context and cleans up.
421 static void yaffs_search_end(struct yaffs_search_context *sc)
424 list_del(&sc->others);
430 * yaffs_search_advance() moves a search context to the next object.
431 * Called when the search iterates or when an object removal causes
432 * the search context to be moved to the next object.
434 static void yaffs_search_advance(struct yaffs_search_context *sc)
439 if (sc->next_return == NULL ||
440 list_empty(&sc->dir_obj->variant.dir_variant.children))
441 sc->next_return = NULL;
443 struct list_head *next = sc->next_return->siblings.next;
445 if (next == &sc->dir_obj->variant.dir_variant.children)
446 sc->next_return = NULL; /* end of list */
449 list_entry(next, struct yaffs_obj, siblings);
454 * yaffs_remove_obj_callback() is called when an object is unlinked.
455 * We check open search contexts and advance any which are currently
456 * on the object being iterated.
458 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
462 struct yaffs_search_context *sc;
463 struct list_head *search_contexts =
464 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
466 /* Iterate through the directory search contexts.
467 * If any are currently on the object being removed, then advance
468 * the search context to the next object to prevent a hanging pointer.
470 list_for_each(i, search_contexts) {
472 sc = list_entry(i, struct yaffs_search_context, others);
473 if (sc->next_return == obj)
474 yaffs_search_advance(sc);
480 /*-----------------------------------------------------------------*/
482 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
485 unsigned char *alias;
488 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
490 yaffs_gross_lock(dev);
492 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
494 yaffs_gross_unlock(dev);
499 ret = vfs_readlink(dentry, buffer, buflen, alias);
504 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
506 unsigned char *alias;
508 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
510 yaffs_gross_lock(dev);
512 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
513 yaffs_gross_unlock(dev);
516 ret = ERR_PTR(-ENOMEM);
520 nd_set_link(nd, alias);
526 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
531 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
532 struct yaffs_obj *obj);
535 * Lookup is used to find objects in the fs
538 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
541 struct yaffs_obj *obj;
542 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
544 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
546 if (current != yaffs_dev_to_lc(dev)->readdir_process)
547 yaffs_gross_lock(dev);
550 (TSTR("yaffs_lookup for %d:%s\n"),
551 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name));
553 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
555 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
557 /* Can't hold gross lock when calling yaffs_get_inode() */
558 if (current != yaffs_dev_to_lc(dev)->readdir_process)
559 yaffs_gross_unlock(dev);
563 (TSTR("yaffs_lookup found %d\n"), obj->obj_id));
565 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
568 T(YAFFS_TRACE_OS, (TSTR("yaffs_loookup dentry \n")));
569 d_add(dentry, inode);
575 T(YAFFS_TRACE_OS, (TSTR("yaffs_lookup not found\n")));
579 d_add(dentry, inode);
584 #ifdef YAFFS_HAS_PUT_INODE
586 /* For now put inode is just for debugging
587 * Put inode is called when the inode **structure** is put.
589 static void yaffs_put_inode(struct inode *inode)
592 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
593 atomic_read(&inode->i_count)));
598 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
600 /* Clear the association between the inode and
601 * the struct yaffs_obj.
603 obj->my_inode = NULL;
604 yaffs_inode_to_obj_lv(inode) = NULL;
606 /* If the object freeing was deferred, then the real
608 * This should fix the inode inconsistency problem.
610 yaffs_handle_defered_free(obj);
613 /* yaffs_evict_inode combines into one operation what was previously done in
614 * yaffs_clear_inode() and yaffs_delete_inode()
617 static void yaffs_evict_inode(struct inode *inode)
619 struct yaffs_obj *obj;
620 struct yaffs_dev *dev;
623 obj = yaffs_inode_to_obj(inode);
626 (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
627 atomic_read(&inode->i_count),
628 obj ? "object exists" : "null object"));
630 if (!inode->i_nlink && !is_bad_inode(inode))
632 truncate_inode_pages(&inode->i_data, 0);
633 end_writeback(inode);
635 if (deleteme && obj) {
637 yaffs_gross_lock(dev);
639 yaffs_gross_unlock(dev);
643 yaffs_gross_lock(dev);
644 yaffs_unstitch_obj(inode, obj);
645 yaffs_gross_unlock(dev);
650 static int yaffs_file_flush(struct file *file, fl_owner_t id)
652 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
654 struct yaffs_dev *dev = obj->my_dev;
657 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->obj_id,
658 obj->dirty ? "dirty" : "clean"));
660 yaffs_gross_lock(dev);
662 yaffs_flush_file(obj, 1, 0);
664 yaffs_gross_unlock(dev);
669 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
671 /* Lifted from jffs2 */
673 struct yaffs_obj *obj;
674 unsigned char *pg_buf;
677 struct yaffs_dev *dev;
680 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
681 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
682 (unsigned)PAGE_CACHE_SIZE));
684 obj = yaffs_dentry_to_obj(f->f_dentry);
688 BUG_ON(!PageLocked(pg));
691 /* FIXME: Can kmap fail? */
693 yaffs_gross_lock(dev);
695 ret = yaffs_file_rd(obj, pg_buf,
696 pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
698 yaffs_gross_unlock(dev);
704 ClearPageUptodate(pg);
711 flush_dcache_page(pg);
714 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
718 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
720 int ret = yaffs_readpage_nolock(f, pg);
725 static int yaffs_readpage(struct file *f, struct page *pg)
729 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
730 ret = yaffs_readpage_unlock(f, pg);
731 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
735 /* writepage inspired by/stolen from smbfs */
737 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
739 struct yaffs_dev *dev;
740 struct address_space *mapping = page->mapping;
742 unsigned long end_index;
744 struct yaffs_obj *obj;
751 inode = mapping->host;
754 i_size = i_size_read(inode);
756 end_index = i_size >> PAGE_CACHE_SHIFT;
758 if (page->index < end_index)
759 n_bytes = PAGE_CACHE_SIZE;
761 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
763 if (page->index > end_index || !n_bytes) {
766 ("yaffs_writepage at %08x, inode size = %08x!!!\n"),
767 (unsigned)(page->index << PAGE_CACHE_SHIFT),
768 (unsigned)inode->i_size));
770 (TSTR(" -> don't care!!\n")));
772 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
773 set_page_writeback(page);
775 end_page_writeback(page);
780 if (n_bytes != PAGE_CACHE_SIZE)
781 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
787 obj = yaffs_inode_to_obj(inode);
789 yaffs_gross_lock(dev);
792 (TSTR("yaffs_writepage at %08x, size %08x\n"),
793 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes));
795 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
796 (int)obj->variant.file_variant.file_size, (int)inode->i_size));
798 n_written = yaffs_wr_file(obj, buffer,
799 page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
801 yaffs_touch_super(dev);
804 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
805 (int)obj->variant.file_variant.file_size, (int)inode->i_size));
807 yaffs_gross_unlock(dev);
810 set_page_writeback(page);
812 end_page_writeback(page);
815 return (n_written == n_bytes) ? 0 : -ENOSPC;
818 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
819 loff_t pos, unsigned len, unsigned flags,
820 struct page **pagep, void **fsdata)
822 struct page *pg = NULL;
823 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
829 pg = grab_cache_page_write_begin(mapping, index, flags);
837 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
838 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0));
841 space_held = yaffs_hold_space(filp);
848 /* Update page if required */
850 if (!Page_Uptodate(pg))
851 ret = yaffs_readpage_nolock(filp, pg);
856 /* Happy path return */
857 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
863 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
865 yaffs_release_space(filp);
868 page_cache_release(pg);
873 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
874 loff_t pos, unsigned len, unsigned copied,
875 struct page *pg, void *fsdadata)
879 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
882 addr = kva + offset_into_page;
885 ("yaffs_write_end addr %p pos %x n_bytes %d\n",
886 addr, (unsigned)pos, copied));
888 ret = yaffs_file_write(filp, addr, copied, &pos);
892 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
901 yaffs_release_space(filp);
903 page_cache_release(pg);
907 static void yaffs_fill_inode_from_obj(struct inode *inode,
908 struct yaffs_obj *obj)
912 /* Check mode against the variant type and attempt to repair if broken. */
913 u32 mode = obj->yst_mode;
914 switch (obj->variant_type) {
915 case YAFFS_OBJECT_TYPE_FILE:
916 if (!S_ISREG(mode)) {
917 obj->yst_mode &= ~S_IFMT;
918 obj->yst_mode |= S_IFREG;
922 case YAFFS_OBJECT_TYPE_SYMLINK:
923 if (!S_ISLNK(mode)) {
924 obj->yst_mode &= ~S_IFMT;
925 obj->yst_mode |= S_IFLNK;
929 case YAFFS_OBJECT_TYPE_DIRECTORY:
930 if (!S_ISDIR(mode)) {
931 obj->yst_mode &= ~S_IFMT;
932 obj->yst_mode |= S_IFDIR;
936 case YAFFS_OBJECT_TYPE_UNKNOWN:
937 case YAFFS_OBJECT_TYPE_HARDLINK:
938 case YAFFS_OBJECT_TYPE_SPECIAL:
944 inode->i_flags |= S_NOATIME;
946 inode->i_ino = obj->obj_id;
947 inode->i_mode = obj->yst_mode;
948 inode->i_uid = obj->yst_uid;
949 inode->i_gid = obj->yst_gid;
951 inode->i_rdev = old_decode_dev(obj->yst_rdev);
953 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
954 inode->i_atime.tv_nsec = 0;
955 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
956 inode->i_mtime.tv_nsec = 0;
957 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
958 inode->i_ctime.tv_nsec = 0;
959 inode->i_size = yaffs_get_obj_length(obj);
960 inode->i_blocks = (inode->i_size + 511) >> 9;
962 inode->i_nlink = yaffs_get_obj_link_count(obj);
966 ("yaffs_fill_inode mode %x uid %d gid %d size %d count %d\n"),
967 inode->i_mode, inode->i_uid, inode->i_gid,
968 (int)inode->i_size, atomic_read(&inode->i_count)));
970 switch (obj->yst_mode & S_IFMT) {
971 default: /* fifo, device or socket */
972 init_special_inode(inode, obj->yst_mode,
973 old_decode_dev(obj->yst_rdev));
975 case S_IFREG: /* file */
976 inode->i_op = &yaffs_file_inode_operations;
977 inode->i_fop = &yaffs_file_operations;
978 inode->i_mapping->a_ops =
979 &yaffs_file_address_operations;
981 case S_IFDIR: /* directory */
982 inode->i_op = &yaffs_dir_inode_operations;
983 inode->i_fop = &yaffs_dir_operations;
985 case S_IFLNK: /* symlink */
986 inode->i_op = &yaffs_symlink_inode_operations;
990 yaffs_inode_to_obj_lv(inode) = obj;
992 obj->my_inode = inode;
996 (TSTR("yaffs_fill_inode invalid parameters\n")));
1001 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1002 struct yaffs_obj *obj)
1004 struct inode *inode;
1008 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1015 (TSTR("yaffs_get_inode for NULL object!!\n")));
1021 (TSTR("yaffs_get_inode for object %d\n"), obj->obj_id));
1023 inode = Y_IGET(sb, obj->obj_id);
1027 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1028 /* iget also increments the inode's i_count */
1029 /* NB You can't be holding gross_lock or deadlock will happen! */
1034 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1037 struct yaffs_obj *obj;
1038 int n_written, ipos;
1039 struct inode *inode;
1040 struct yaffs_dev *dev;
1042 obj = yaffs_dentry_to_obj(f->f_dentry);
1046 yaffs_gross_lock(dev);
1048 inode = f->f_dentry->d_inode;
1050 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1051 ipos = inode->i_size;
1057 (TSTR("yaffs_file_write: hey obj is null!\n")));
1060 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1061 "to object %d at %d(%x)\n"),
1062 (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos));
1064 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1066 yaffs_touch_super(dev);
1069 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1070 (unsigned)n, (unsigned)n));
1072 if (n_written > 0) {
1075 if (ipos > inode->i_size) {
1076 inode->i_size = ipos;
1077 inode->i_blocks = (ipos + 511) >> 9;
1080 (TSTR("yaffs_file_write size updated to %d bytes, "
1081 "%d blocks\n"), ipos, (int)(inode->i_blocks)));
1085 yaffs_gross_unlock(dev);
1086 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1089 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1090 /* For now we just assume few parallel writes and check against a small number. */
1091 /* Todo: need to do this with a counter to handle parallel reads better */
1093 static ssize_t yaffs_hold_space(struct file *f)
1095 struct yaffs_obj *obj;
1096 struct yaffs_dev *dev;
1100 obj = yaffs_dentry_to_obj(f->f_dentry);
1104 yaffs_gross_lock(dev);
1106 n_free_chunks = yaffs_get_n_free_chunks(dev);
1108 yaffs_gross_unlock(dev);
1110 return (n_free_chunks > 20) ? 1 : 0;
1113 static void yaffs_release_space(struct file *f)
1115 struct yaffs_obj *obj;
1116 struct yaffs_dev *dev;
1118 obj = yaffs_dentry_to_obj(f->f_dentry);
1122 yaffs_gross_lock(dev);
1124 yaffs_gross_unlock(dev);
1127 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1129 struct yaffs_obj *obj;
1130 struct yaffs_dev *dev;
1131 struct yaffs_search_context *sc;
1132 struct inode *inode = f->f_dentry->d_inode;
1133 unsigned long offset, curoffs;
1134 struct yaffs_obj *l;
1137 char name[YAFFS_MAX_NAME_LENGTH + 1];
1139 obj = yaffs_dentry_to_obj(f->f_dentry);
1142 yaffs_gross_lock(dev);
1144 yaffs_dev_to_lc(dev)->readdir_process = current;
1148 sc = yaffs_new_search(obj);
1155 (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1159 (TSTR("yaffs_readdir: entry . ino %d \n"),
1160 (int)inode->i_ino));
1161 yaffs_gross_unlock(dev);
1162 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1163 yaffs_gross_lock(dev);
1166 yaffs_gross_lock(dev);
1172 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1173 (int)f->f_dentry->d_parent->d_inode->i_ino));
1174 yaffs_gross_unlock(dev);
1175 if (filldir(dirent, "..", 2, offset,
1176 f->f_dentry->d_parent->d_inode->i_ino,
1178 yaffs_gross_lock(dev);
1181 yaffs_gross_lock(dev);
1188 /* If the directory has changed since the open or last call to
1189 readdir, rewind to after the 2 canned entries. */
1190 if (f->f_version != inode->i_version) {
1193 f->f_version = inode->i_version;
1196 while (sc->next_return) {
1198 l = sc->next_return;
1199 if (curoffs >= offset) {
1200 int this_inode = yaffs_get_obj_inode(l);
1201 int this_type = yaffs_get_obj_type(l);
1203 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1205 (TSTR("yaffs_readdir: %s inode %d\n"),
1206 name, yaffs_get_obj_inode(l)));
1208 yaffs_gross_unlock(dev);
1213 offset, this_inode, this_type) < 0) {
1214 yaffs_gross_lock(dev);
1218 yaffs_gross_lock(dev);
1223 yaffs_search_advance(sc);
1227 yaffs_search_end(sc);
1228 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1229 yaffs_gross_unlock(dev);
1235 * File creation. Allocate an inode, and we're done..
1238 #define YCRED(x) (x->cred)
1240 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1243 struct inode *inode;
1245 struct yaffs_obj *obj = NULL;
1246 struct yaffs_dev *dev;
1248 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1250 int error = -ENOSPC;
1251 uid_t uid = YCRED(current)->fsuid;
1253 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1255 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1260 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1261 parent->obj_id, parent->variant_type));
1264 (TSTR("yaffs_mknod: could not get parent object\n")));
1268 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1269 "mode %x dev %x\n"),
1270 dentry->d_name.name, mode, rdev));
1272 dev = parent->my_dev;
1274 yaffs_gross_lock(dev);
1276 switch (mode & S_IFMT) {
1278 /* Special (socket, fifo, device...) */
1279 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1281 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1282 gid, old_encode_dev(rdev));
1284 case S_IFREG: /* file */
1285 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1286 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1289 case S_IFDIR: /* directory */
1290 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making directory\n")));
1291 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1294 case S_IFLNK: /* symlink */
1295 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1296 obj = NULL; /* Do we ever get here? */
1300 /* Can not call yaffs_get_inode() with gross lock held */
1301 yaffs_gross_unlock(dev);
1304 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1305 d_instantiate(dentry, inode);
1306 update_dir_time(dir);
1308 (TSTR("yaffs_mknod created object %d count = %d\n"),
1309 obj->obj_id, atomic_read(&inode->i_count)));
1311 yaffs_fill_inode_from_obj(dir, parent);
1313 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod failed making object\n")));
1320 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1323 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1324 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1328 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1329 struct nameidata *n)
1331 T(YAFFS_TRACE_OS, (TSTR("yaffs_create\n")));
1332 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1335 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1339 struct yaffs_dev *dev;
1340 struct yaffs_obj *obj;
1343 (TSTR("yaffs_unlink %d:%s\n"),
1344 (int)(dir->i_ino), dentry->d_name.name));
1345 obj = yaffs_inode_to_obj(dir);
1348 yaffs_gross_lock(dev);
1350 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1352 if (ret_val == YAFFS_OK) {
1353 dentry->d_inode->i_nlink--;
1355 yaffs_gross_unlock(dev);
1356 mark_inode_dirty(dentry->d_inode);
1357 update_dir_time(dir);
1360 yaffs_gross_unlock(dev);
1367 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1368 struct dentry *dentry)
1370 struct inode *inode = old_dentry->d_inode;
1371 struct yaffs_obj *obj = NULL;
1372 struct yaffs_obj *link = NULL;
1373 struct yaffs_dev *dev;
1375 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1377 obj = yaffs_inode_to_obj(inode);
1380 yaffs_gross_lock(dev);
1382 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1384 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1388 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
1389 d_instantiate(dentry, old_dentry->d_inode);
1390 atomic_inc(&old_dentry->d_inode->i_count);
1392 (TSTR("yaffs_link link count %d i_count %d\n"),
1393 old_dentry->d_inode->i_nlink,
1394 atomic_read(&old_dentry->d_inode->i_count)));
1397 yaffs_gross_unlock(dev);
1400 update_dir_time(dir);
1407 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1408 const char *symname)
1410 struct yaffs_obj *obj;
1411 struct yaffs_dev *dev;
1412 uid_t uid = YCRED(current)->fsuid;
1414 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1416 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1418 dev = yaffs_inode_to_obj(dir)->my_dev;
1419 yaffs_gross_lock(dev);
1420 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1421 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1422 yaffs_gross_unlock(dev);
1425 struct inode *inode;
1427 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1428 d_instantiate(dentry, inode);
1429 update_dir_time(dir);
1430 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1433 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1439 static int yaffs_sync_object(struct file *file, int datasync)
1442 struct yaffs_obj *obj;
1443 struct yaffs_dev *dev;
1444 struct dentry *dentry = file->f_path.dentry;
1446 obj = yaffs_dentry_to_obj(dentry);
1450 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, (TSTR("yaffs_sync_object\n")));
1451 yaffs_gross_lock(dev);
1452 yaffs_flush_file(obj, 1, datasync);
1453 yaffs_gross_unlock(dev);
1458 * The VFS layer already does all the dentry stuff for rename.
1460 * NB: POSIX says you can rename an object over an old object of the same name
1462 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1463 struct inode *new_dir, struct dentry *new_dentry)
1465 struct yaffs_dev *dev;
1466 int ret_val = YAFFS_FAIL;
1467 struct yaffs_obj *target;
1469 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1470 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1472 yaffs_gross_lock(dev);
1474 /* Check if the target is an existing directory that is not empty. */
1475 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1476 new_dentry->d_name.name);
1478 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1479 !list_empty(&target->variant.dir_variant.children)) {
1481 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1483 ret_val = YAFFS_FAIL;
1485 /* Now does unlinking internally using shadowing mechanism */
1486 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_rename_obj\n")));
1488 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1489 old_dentry->d_name.name,
1490 yaffs_inode_to_obj(new_dir),
1491 new_dentry->d_name.name);
1493 yaffs_gross_unlock(dev);
1495 if (ret_val == YAFFS_OK) {
1497 new_dentry->d_inode->i_nlink--;
1498 mark_inode_dirty(new_dentry->d_inode);
1501 update_dir_time(old_dir);
1502 if (old_dir != new_dir)
1503 update_dir_time(new_dir);
1510 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1512 struct inode *inode = dentry->d_inode;
1514 struct yaffs_dev *dev;
1517 (TSTR("yaffs_setattr of object %d\n"),
1518 yaffs_inode_to_obj(inode)->obj_id));
1520 /* Fail if a requested resize >= 2GB */
1521 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
1525 error = inode_change_ok(inode, attr);
1529 error = yaffs_vfs_setattr(inode, attr);
1530 T(YAFFS_TRACE_OS, (TSTR("inode_setattr called\n")));
1531 if (attr->ia_valid & ATTR_SIZE) {
1532 yaffs_vfs_setsize(inode, attr->ia_size);
1533 inode->i_blocks = (inode->i_size + 511) >> 9;
1536 dev = yaffs_inode_to_obj(inode)->my_dev;
1537 if (attr->ia_valid & ATTR_SIZE) {
1538 T(YAFFS_TRACE_OS, (TSTR("resize to %d(%x)\n"),
1539 (int)(attr->ia_size),
1540 (int)(attr->ia_size)));
1542 yaffs_gross_lock(dev);
1543 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
1544 if (result == YAFFS_OK) {
1549 yaffs_gross_unlock(dev);
1553 T(YAFFS_TRACE_OS, (TSTR("yaffs_setattr done returning %d\n"), error));
1558 #ifdef CONFIG_YAFFS_XATTR
1559 static int yaffs_setxattr(struct dentry *dentry, const char *name,
1560 const void *value, size_t size, int flags)
1562 struct inode *inode = dentry->d_inode;
1564 struct yaffs_dev *dev;
1565 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1567 T(YAFFS_TRACE_OS, (TSTR("yaffs_setxattr of object %d\n"), obj->obj_id));
1572 yaffs_gross_lock(dev);
1573 result = yaffs_set_xattrib(obj, name, value, size, flags);
1574 if (result == YAFFS_OK)
1576 else if (result < 0)
1578 yaffs_gross_unlock(dev);
1581 T(YAFFS_TRACE_OS, (TSTR("yaffs_setxattr done returning %d\n"), error));
1586 static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, void *buff,
1589 struct inode *inode = dentry->d_inode;
1591 struct yaffs_dev *dev;
1592 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1595 (TSTR("yaffs_getxattr \"%s\" from object %d\n"), name, obj->obj_id));
1599 yaffs_gross_lock(dev);
1600 error = yaffs_get_xattrib(obj, name, buff, size);
1601 yaffs_gross_unlock(dev);
1604 T(YAFFS_TRACE_OS, (TSTR("yaffs_getxattr done returning %d\n"), error));
1609 static int yaffs_removexattr(struct dentry *dentry, const char *name)
1611 struct inode *inode = dentry->d_inode;
1613 struct yaffs_dev *dev;
1614 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1617 (TSTR("yaffs_removexattr of object %d\n"), obj->obj_id));
1622 yaffs_gross_lock(dev);
1623 result = yaffs_remove_xattrib(obj, name);
1624 if (result == YAFFS_OK)
1626 else if (result < 0)
1628 yaffs_gross_unlock(dev);
1632 (TSTR("yaffs_removexattr done returning %d\n"), error));
1637 static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
1639 struct inode *inode = dentry->d_inode;
1641 struct yaffs_dev *dev;
1642 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1645 (TSTR("yaffs_listxattr of object %d\n"), obj->obj_id));
1649 yaffs_gross_lock(dev);
1650 error = yaffs_list_xattrib(obj, buff, size);
1651 yaffs_gross_unlock(dev);
1654 T(YAFFS_TRACE_OS, (TSTR("yaffs_listxattr done returning %d\n"), error));
1661 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1663 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1664 struct super_block *sb = dentry->d_sb;
1666 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
1668 yaffs_gross_lock(dev);
1670 buf->f_type = YAFFS_MAGIC;
1671 buf->f_bsize = sb->s_blocksize;
1672 buf->f_namelen = 255;
1674 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
1675 /* Do this if chunk size is not a power of 2 */
1677 uint64_t bytes_in_dev;
1678 uint64_t bytes_free;
1682 ((dev->param.end_block - dev->param.start_block +
1683 1))) * ((uint64_t) (dev->param.chunks_per_block *
1684 dev->data_bytes_per_chunk));
1686 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
1687 buf->f_blocks = bytes_in_dev;
1689 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
1690 ((uint64_t) (dev->data_bytes_per_chunk));
1692 do_div(bytes_free, sb->s_blocksize);
1694 buf->f_bfree = bytes_free;
1696 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
1699 (dev->param.end_block - dev->param.start_block + 1) *
1700 dev->param.chunks_per_block /
1701 (sb->s_blocksize / dev->data_bytes_per_chunk);
1703 yaffs_get_n_free_chunks(dev) /
1704 (sb->s_blocksize / dev->data_bytes_per_chunk);
1707 (dev->param.end_block - dev->param.start_block + 1) *
1708 dev->param.chunks_per_block *
1709 (dev->data_bytes_per_chunk / sb->s_blocksize);
1712 yaffs_get_n_free_chunks(dev) *
1713 (dev->data_bytes_per_chunk / sb->s_blocksize);
1718 buf->f_bavail = buf->f_bfree;
1720 yaffs_gross_unlock(dev);
1724 static void yaffs_flush_inodes(struct super_block *sb)
1727 struct yaffs_obj *obj;
1729 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
1730 obj = yaffs_inode_to_obj(iptr);
1732 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
1734 yaffs_flush_file(obj, 1, 0);
1739 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
1741 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1745 yaffs_flush_inodes(sb);
1746 yaffs_update_dirty_dirs(dev);
1747 yaffs_flush_whole_cache(dev);
1749 yaffs_checkpoint_save(dev);
1752 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
1754 unsigned erased_chunks =
1755 dev->n_erased_blocks * dev->param.chunks_per_block;
1756 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1757 unsigned scattered = 0; /* Free chunks not in an erased block */
1759 if (erased_chunks < dev->n_free_chunks)
1760 scattered = (dev->n_free_chunks - erased_chunks);
1762 if (!context->bg_running)
1764 else if (scattered < (dev->param.chunks_per_block * 2))
1766 else if (erased_chunks > dev->n_free_chunks / 2)
1768 else if (erased_chunks > dev->n_free_chunks / 4)
1774 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
1777 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1778 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
1779 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
1782 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1783 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
1785 sb->s_dirt ? "dirty" : "clean",
1786 request_checkpoint ? "checkpoint requested" : "no checkpoint",
1787 oneshot_checkpoint ? " one-shot" : ""));
1789 yaffs_gross_lock(dev);
1790 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
1791 oneshot_checkpoint) && !dev->is_checkpointed;
1793 if (sb->s_dirt || do_checkpoint) {
1794 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
1796 if (oneshot_checkpoint)
1797 yaffs_auto_checkpoint &= ~4;
1799 yaffs_gross_unlock(dev);
1805 * yaffs background thread functions .
1806 * yaffs_bg_thread_fn() the thread function
1807 * yaffs_bg_start() launches the background thread.
1808 * yaffs_bg_stop() cleans up the background thread.
1811 * The thread should only run after the yaffs is initialised
1812 * The thread should be stopped before yaffs is unmounted.
1813 * The thread should not do any writing while the fs is in read only.
1816 void yaffs_background_waker(unsigned long data)
1818 wake_up_process((struct task_struct *)data);
1821 static int yaffs_bg_thread_fn(void *data)
1823 struct yaffs_dev *dev = (struct yaffs_dev *)data;
1824 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1825 unsigned long now = jiffies;
1826 unsigned long next_dir_update = now;
1827 unsigned long next_gc = now;
1828 unsigned long expires;
1829 unsigned int urgency;
1832 struct timer_list timer;
1834 T(YAFFS_TRACE_BACKGROUND,
1835 (TSTR("yaffs_background starting for dev %p\n"), (void *)dev));
1838 while (context->bg_running) {
1839 T(YAFFS_TRACE_BACKGROUND, (TSTR("yaffs_background\n")));
1841 if (kthread_should_stop())
1844 if (try_to_freeze())
1847 yaffs_gross_lock(dev);
1851 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
1852 yaffs_update_dirty_dirs(dev);
1853 next_dir_update = now + HZ;
1856 if (time_after(now, next_gc) && yaffs_bg_enable) {
1857 if (!dev->is_checkpointed) {
1858 urgency = yaffs_bg_gc_urgency(dev);
1859 gc_result = yaffs_bg_gc(dev, urgency);
1861 next_gc = now + HZ / 20 + 1;
1862 else if (urgency > 0)
1863 next_gc = now + HZ / 10 + 1;
1865 next_gc = now + HZ * 2;
1868 * gc not running so set to next_dir_update
1869 * to cut down on wake ups
1871 next_gc = next_dir_update;
1874 yaffs_gross_unlock(dev);
1875 expires = next_dir_update;
1876 if (time_before(next_gc, expires))
1878 if (time_before(expires, now))
1881 Y_INIT_TIMER(&timer);
1882 timer.expires = expires + 1;
1883 timer.data = (unsigned long)current;
1884 timer.function = yaffs_background_waker;
1886 set_current_state(TASK_INTERRUPTIBLE);
1889 del_timer_sync(&timer);
1895 static int yaffs_bg_start(struct yaffs_dev *dev)
1898 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1903 context->bg_running = 1;
1905 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
1906 (void *)dev, "yaffs-bg-%d",
1909 if (IS_ERR(context->bg_thread)) {
1910 retval = PTR_ERR(context->bg_thread);
1911 context->bg_thread = NULL;
1912 context->bg_running = 0;
1917 static void yaffs_bg_stop(struct yaffs_dev *dev)
1919 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
1921 ctxt->bg_running = 0;
1923 if (ctxt->bg_thread) {
1924 kthread_stop(ctxt->bg_thread);
1925 ctxt->bg_thread = NULL;
1929 static void yaffs_write_super(struct super_block *sb)
1931 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
1933 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1934 (TSTR("yaffs_write_super%s\n"),
1935 request_checkpoint ? " checkpt" : ""));
1937 yaffs_do_sync_fs(sb, request_checkpoint);
1941 static int yaffs_sync_fs(struct super_block *sb, int wait)
1943 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
1945 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1946 (TSTR("yaffs_sync_fs%s\n"), request_checkpoint ? " checkpt" : ""));
1948 yaffs_do_sync_fs(sb, request_checkpoint);
1953 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1955 struct inode *inode;
1956 struct yaffs_obj *obj;
1957 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1959 T(YAFFS_TRACE_OS, (TSTR("yaffs_iget for %lu\n"), ino));
1961 inode = iget_locked(sb, ino);
1963 return ERR_PTR(-ENOMEM);
1964 if (!(inode->i_state & I_NEW))
1967 /* NB This is called as a side effect of other functions, but
1968 * we had to release the lock to prevent deadlocks, so
1969 * need to lock again.
1972 yaffs_gross_lock(dev);
1974 obj = yaffs_find_by_number(dev, inode->i_ino);
1976 yaffs_fill_inode_from_obj(inode, obj);
1978 yaffs_gross_unlock(dev);
1980 unlock_new_inode(inode);
1984 static LIST_HEAD(yaffs_context_list);
1985 struct mutex yaffs_context_lock;
1987 static void yaffs_put_super(struct super_block *sb)
1989 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1991 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
1993 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1994 (TSTR("Shutting down yaffs background thread\n")));
1996 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1997 (TSTR("yaffs background thread shut down\n")));
1999 yaffs_gross_lock(dev);
2001 yaffs_flush_super(sb, 1);
2003 if (yaffs_dev_to_lc(dev)->put_super_fn)
2004 yaffs_dev_to_lc(dev)->put_super_fn(sb);
2006 yaffs_deinitialise(dev);
2008 yaffs_gross_unlock(dev);
2009 mutex_lock(&yaffs_context_lock);
2010 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2011 mutex_unlock(&yaffs_context_lock);
2013 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2014 YFREE(yaffs_dev_to_lc(dev)->spare_buffer);
2015 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2021 static void yaffs_mtd_put_super(struct super_block *sb)
2023 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
2028 put_mtd_device(mtd);
2031 static void yaffs_touch_super(struct yaffs_dev *dev)
2033 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
2035 T(YAFFS_TRACE_OS, (TSTR("yaffs_touch_super() sb = %p\n"), sb));
2040 struct yaffs_options {
2042 int skip_checkpoint_read;
2043 int skip_checkpoint_write;
2046 int tags_ecc_overridden;
2047 int lazy_loading_enabled;
2048 int lazy_loading_overridden;
2049 int empty_lost_and_found;
2050 int empty_lost_and_found_overridden;
2053 #define MAX_OPT_LEN 30
2054 static int yaffs_parse_options(struct yaffs_options *options,
2055 const char *options_str)
2057 char cur_opt[MAX_OPT_LEN + 1];
2061 /* Parse through the options which is a comma seperated list */
2063 while (options_str && *options_str && !error) {
2064 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2067 while (*options_str == ',')
2070 while (*options_str && *options_str != ',') {
2071 if (p < MAX_OPT_LEN) {
2072 cur_opt[p] = *options_str;
2078 if (!strcmp(cur_opt, "inband-tags")) {
2079 options->inband_tags = 1;
2080 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2081 options->tags_ecc_on = 0;
2082 options->tags_ecc_overridden = 1;
2083 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2084 options->tags_ecc_on = 1;
2085 options->tags_ecc_overridden = 1;
2086 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2087 options->lazy_loading_enabled = 0;
2088 options->lazy_loading_overridden = 1;
2089 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2090 options->lazy_loading_enabled = 1;
2091 options->lazy_loading_overridden = 1;
2092 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2093 options->empty_lost_and_found = 0;
2094 options->empty_lost_and_found_overridden = 1;
2095 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2096 options->empty_lost_and_found = 1;
2097 options->empty_lost_and_found_overridden = 1;
2098 } else if (!strcmp(cur_opt, "no-cache")) {
2099 options->no_cache = 1;
2100 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2101 options->skip_checkpoint_read = 1;
2102 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2103 options->skip_checkpoint_write = 1;
2104 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2105 options->skip_checkpoint_read = 1;
2106 options->skip_checkpoint_write = 1;
2108 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2117 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2118 struct super_block *sb,
2119 void *data, int silent)
2122 struct inode *inode = NULL;
2123 struct dentry *root;
2124 struct yaffs_dev *dev = 0;
2125 char devname_buf[BDEVNAME_SIZE + 1];
2126 struct mtd_info *mtd;
2128 char *data_str = (char *)data;
2129 struct yaffs_linux_context *context = NULL;
2130 struct yaffs_param *param;
2134 struct yaffs_options options;
2138 struct yaffs_linux_context *context_iterator;
2139 struct list_head *l;
2141 sb->s_magic = YAFFS_MAGIC;
2142 sb->s_op = &yaffs_super_ops;
2143 sb->s_flags |= MS_NOATIME;
2145 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2147 sb->s_export_op = &yaffs_export_ops;
2150 printk(KERN_INFO "yaffs: sb is NULL\n");
2151 else if (!sb->s_dev)
2152 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2153 else if (!yaffs_devname(sb, devname_buf))
2154 printk(KERN_INFO "yaffs: devname is NULL\n");
2156 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2158 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2163 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2165 memset(&options, 0, sizeof(options));
2167 if (yaffs_parse_options(&options, data_str)) {
2168 /* Option parsing failed */
2172 sb->s_blocksize = PAGE_CACHE_SIZE;
2173 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2176 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffs_version));
2178 (TSTR("yaffs_read_super: block size %d\n"), (int)(sb->s_blocksize)));
2180 T(YAFFS_TRACE_ALWAYS,
2181 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2182 MAJOR(sb->s_dev), MINOR(sb->s_dev), yaffs_devname(sb, devname_buf)));
2184 /* Check it's an mtd device..... */
2185 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2186 return NULL; /* This isn't an mtd device */
2188 /* Get the device */
2189 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2191 T(YAFFS_TRACE_ALWAYS,
2192 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2196 /* Check it's NAND */
2197 if (mtd->type != MTD_NANDFLASH) {
2198 T(YAFFS_TRACE_ALWAYS,
2199 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2204 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2205 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2206 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2207 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2208 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2209 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2210 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2211 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2212 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2213 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2214 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2216 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2218 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2219 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs2\n")));
2223 /* Added NCB 26/5/2006 for completeness */
2224 if (yaffs_version == 2 && !options.inband_tags
2225 && WRITE_SIZE(mtd) == 512) {
2226 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs1\n")));
2231 if (yaffs_version == 2) {
2232 /* Check for version 2 style functions */
2234 !mtd->block_isbad ||
2235 !mtd->block_markbad ||
2237 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2238 T(YAFFS_TRACE_ALWAYS,
2239 (TSTR("yaffs: MTD device does not support required "
2244 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2245 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2246 !options.inband_tags) {
2247 T(YAFFS_TRACE_ALWAYS,
2248 (TSTR("yaffs: MTD device does not have the "
2249 "right page sizes\n")));
2253 /* Check for V1 style functions */
2256 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2257 T(YAFFS_TRACE_ALWAYS,
2258 (TSTR("yaffs: MTD device does not support required "
2263 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2264 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2265 T(YAFFS_TRACE_ALWAYS,
2266 (TSTR("yaffs: MTD device does not support have the "
2267 "right page sizes\n")));
2272 /* OK, so if we got here, we have an MTD that's NAND and looks
2273 * like it has the right capabilities
2274 * Set the struct yaffs_dev up for mtd
2277 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2280 "yaffs: mtd is read only, setting superblock read only");
2281 sb->s_flags |= MS_RDONLY;
2284 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2285 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2287 if (!dev || !context) {
2297 /* Deep shit could not allocate device structure */
2298 T(YAFFS_TRACE_ALWAYS,
2299 (TSTR("yaffs_read_super: Failed trying to allocate "
2300 "struct yaffs_dev. \n")));
2303 memset(dev, 0, sizeof(struct yaffs_dev));
2304 param = &(dev->param);
2306 memset(context, 0, sizeof(struct yaffs_linux_context));
2307 dev->os_context = context;
2308 INIT_LIST_HEAD(&(context->context_list));
2310 context->super = sb;
2312 dev->read_only = read_only;
2314 sb->s_fs_info = dev;
2316 dev->driver_context = mtd;
2317 param->name = mtd->name;
2319 /* Set up the memory size parameters.... */
2322 YCALCBLOCKS(mtd->size,
2323 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2325 param->start_block = 0;
2326 param->end_block = n_blocks - 1;
2327 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2328 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2329 param->n_reserved_blocks = 5;
2330 param->n_caches = (options.no_cache) ? 0 : 10;
2331 param->inband_tags = options.inband_tags;
2333 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2334 param->disable_lazy_load = 1;
2336 #ifdef CONFIG_YAFFS_XATTR
2337 param->enable_xattr = 1;
2339 if (options.lazy_loading_overridden)
2340 param->disable_lazy_load = !options.lazy_loading_enabled;
2342 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2343 param->no_tags_ecc = 1;
2346 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2348 param->defered_dir_update = 1;
2351 if (options.tags_ecc_overridden)
2352 param->no_tags_ecc = !options.tags_ecc_on;
2354 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2355 param->empty_lost_n_found = 1;
2358 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2359 param->refresh_period = 0;
2361 param->refresh_period = 500;
2364 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2365 param->always_check_erased = 1;
2368 if (options.empty_lost_and_found_overridden)
2369 param->empty_lost_n_found = options.empty_lost_and_found;
2371 /* ... and the functions. */
2372 if (yaffs_version == 2) {
2373 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2374 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2375 param->bad_block_fn = nandmtd2_mark_block_bad;
2376 param->query_block_fn = nandmtd2_query_block;
2377 yaffs_dev_to_lc(dev)->spare_buffer = YMALLOC(mtd->oobsize);
2378 param->is_yaffs2 = 1;
2379 param->total_bytes_per_chunk = mtd->writesize;
2380 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2381 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2383 param->start_block = 0;
2384 param->end_block = n_blocks - 1;
2386 /* use the MTD interface in yaffs_mtdif1.c */
2387 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2388 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2389 param->bad_block_fn = nandmtd1_mark_block_bad;
2390 param->query_block_fn = nandmtd1_query_block;
2391 param->is_yaffs2 = 0;
2393 /* ... and common functions */
2394 param->erase_fn = nandmtd_erase_block;
2395 param->initialise_flash_fn = nandmtd_initialise;
2397 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2399 param->sb_dirty_fn = yaffs_touch_super;
2400 param->gc_control = yaffs_gc_control_callback;
2402 yaffs_dev_to_lc(dev)->super = sb;
2404 #ifndef CONFIG_YAFFS_DOES_ECC
2405 param->use_nand_ecc = 1;
2408 param->skip_checkpt_rd = options.skip_checkpoint_read;
2409 param->skip_checkpt_wr = options.skip_checkpoint_write;
2411 mutex_lock(&yaffs_context_lock);
2412 /* Get a mount id */
2414 for (mount_id = 0; !found; mount_id++) {
2416 list_for_each(l, &yaffs_context_list) {
2418 list_entry(l, struct yaffs_linux_context,
2420 if (context_iterator->mount_id == mount_id)
2424 context->mount_id = mount_id;
2426 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2427 &yaffs_context_list);
2428 mutex_unlock(&yaffs_context_lock);
2430 /* Directory search handling... */
2431 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2432 param->remove_obj_fn = yaffs_remove_obj_callback;
2434 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2436 yaffs_gross_lock(dev);
2438 err = yaffs_guts_initialise(dev);
2441 (TSTR("yaffs_read_super: guts initialised %s\n"),
2442 (err == YAFFS_OK) ? "OK" : "FAILED"));
2444 if (err == YAFFS_OK)
2445 yaffs_bg_start(dev);
2447 if (!context->bg_thread)
2448 param->defered_dir_update = 0;
2450 /* Release lock before yaffs_get_inode() */
2451 yaffs_gross_unlock(dev);
2453 /* Create root inode */
2454 if (err == YAFFS_OK)
2455 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2460 inode->i_op = &yaffs_dir_inode_operations;
2461 inode->i_fop = &yaffs_dir_operations;
2463 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
2465 root = d_alloc_root(inode);
2467 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
2474 sb->s_dirt = !dev->is_checkpointed;
2475 T(YAFFS_TRACE_ALWAYS,
2476 (TSTR("yaffs_read_super: is_checkpointed %d\n"),
2477 dev->is_checkpointed));
2479 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
2483 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2486 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2489 static int yaffs_read_super(struct file_system_type *fs,
2490 int flags, const char *dev_name,
2491 void *data, struct vfsmount *mnt)
2494 return get_sb_bdev(fs, flags, dev_name, data,
2495 yaffs_internal_read_super_mtd, mnt);
2498 static struct file_system_type yaffs_fs_type = {
2499 .owner = THIS_MODULE,
2501 .get_sb = yaffs_read_super,
2502 .kill_sb = kill_block_super,
2503 .fs_flags = FS_REQUIRES_DEV,
2506 #ifdef CONFIG_YAFFS_YAFFS2
2508 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2511 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2514 static int yaffs2_read_super(struct file_system_type *fs,
2515 int flags, const char *dev_name, void *data,
2516 struct vfsmount *mnt)
2518 return get_sb_bdev(fs, flags, dev_name, data,
2519 yaffs2_internal_read_super_mtd, mnt);
2522 static struct file_system_type yaffs2_fs_type = {
2523 .owner = THIS_MODULE,
2525 .get_sb = yaffs2_read_super,
2526 .kill_sb = kill_block_super,
2527 .fs_flags = FS_REQUIRES_DEV,
2529 #endif /* CONFIG_YAFFS_YAFFS2 */
2531 static struct proc_dir_entry *my_proc_entry;
2533 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
2536 sprintf(buf, "start_block........... %d\n", dev->param.start_block);
2538 sprintf(buf, "end_block............. %d\n", dev->param.end_block);
2540 sprintf(buf, "total_bytes_per_chunk. %d\n",
2541 dev->param.total_bytes_per_chunk);
2543 sprintf(buf, "use_nand_ecc.......... %d\n",
2544 dev->param.use_nand_ecc);
2546 sprintf(buf, "no_tags_ecc........... %d\n", dev->param.no_tags_ecc);
2548 sprintf(buf, "is_yaffs2............. %d\n", dev->param.is_yaffs2);
2550 sprintf(buf, "inband_tags........... %d\n", dev->param.inband_tags);
2552 sprintf(buf, "empty_lost_n_found.... %d\n",
2553 dev->param.empty_lost_n_found);
2555 sprintf(buf, "disable_lazy_load..... %d\n",
2556 dev->param.disable_lazy_load);
2558 sprintf(buf, "refresh_period........ %d\n",
2559 dev->param.refresh_period);
2560 buf += sprintf(buf, "n_caches.............. %d\n", dev->param.n_caches);
2562 sprintf(buf, "n_reserved_blocks..... %d\n",
2563 dev->param.n_reserved_blocks);
2565 sprintf(buf, "always_check_erased... %d\n",
2566 dev->param.always_check_erased);
2568 buf += sprintf(buf, "\n");
2573 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
2576 sprintf(buf, "data_bytes_per_chunk.. %d\n",
2577 dev->data_bytes_per_chunk);
2578 buf += sprintf(buf, "chunk_grp_bits........ %d\n", dev->chunk_grp_bits);
2579 buf += sprintf(buf, "chunk_grp_size........ %d\n", dev->chunk_grp_size);
2581 sprintf(buf, "n_erased_blocks....... %d\n", dev->n_erased_blocks);
2583 sprintf(buf, "blocks_in_checkpt..... %d\n", dev->blocks_in_checkpt);
2584 buf += sprintf(buf, "\n");
2585 buf += sprintf(buf, "n_tnodes.............. %d\n", dev->n_tnodes);
2586 buf += sprintf(buf, "n_obj................. %d\n", dev->n_obj);
2587 buf += sprintf(buf, "n_free_chunks......... %d\n", dev->n_free_chunks);
2588 buf += sprintf(buf, "\n");
2589 buf += sprintf(buf, "n_page_writes......... %u\n", dev->n_page_writes);
2590 buf += sprintf(buf, "n_page_reads.......... %u\n", dev->n_page_reads);
2591 buf += sprintf(buf, "n_erasures............ %u\n", dev->n_erasures);
2592 buf += sprintf(buf, "n_gc_copies........... %u\n", dev->n_gc_copies);
2593 buf += sprintf(buf, "all_gcs............... %u\n", dev->all_gcs);
2595 sprintf(buf, "passive_gc_count...... %u\n", dev->passive_gc_count);
2597 sprintf(buf, "oldest_dirty_gc_count. %u\n",
2598 dev->oldest_dirty_gc_count);
2599 buf += sprintf(buf, "n_gc_blocks........... %u\n", dev->n_gc_blocks);
2600 buf += sprintf(buf, "bg_gcs................ %u\n", dev->bg_gcs);
2602 sprintf(buf, "n_retired_writes...... %u\n", dev->n_retired_writes);
2604 sprintf(buf, "n_retired_blocks...... %u\n", dev->n_retired_blocks);
2605 buf += sprintf(buf, "n_ecc_fixed........... %u\n", dev->n_ecc_fixed);
2606 buf += sprintf(buf, "n_ecc_unfixed......... %u\n", dev->n_ecc_unfixed);
2608 sprintf(buf, "n_tags_ecc_fixed...... %u\n", dev->n_tags_ecc_fixed);
2610 sprintf(buf, "n_tags_ecc_unfixed.... %u\n",
2611 dev->n_tags_ecc_unfixed);
2612 buf += sprintf(buf, "cache_hits............ %u\n", dev->cache_hits);
2614 sprintf(buf, "n_deleted_files....... %u\n", dev->n_deleted_files);
2616 sprintf(buf, "n_unlinked_files...... %u\n", dev->n_unlinked_files);
2617 buf += sprintf(buf, "refresh_count......... %u\n", dev->refresh_count);
2618 buf += sprintf(buf, "n_bg_deletions........ %u\n", dev->n_bg_deletions);
2623 static int yaffs_proc_read(char *page,
2625 off_t offset, int count, int *eof, void *data)
2627 struct list_head *item;
2632 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2633 * We use 'offset' (*ppos) to indicate where we are in dev_list.
2634 * This also assumes the user has posted a read buffer large
2635 * enough to hold the complete output; but that's life in /proc.
2640 /* Print header first */
2642 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__ "\n");
2644 buf += sprintf(buf, "\n");
2648 mutex_lock(&yaffs_context_lock);
2650 /* Locate and print the Nth entry. Order N-squared but N is small. */
2651 list_for_each(item, &yaffs_context_list) {
2652 struct yaffs_linux_context *dc =
2653 list_entry(item, struct yaffs_linux_context,
2655 struct yaffs_dev *dev = dc->dev;
2657 if (n < (step & ~1)) {
2661 if ((step & 1) == 0) {
2663 sprintf(buf, "\nDevice %d \"%s\"\n", n,
2665 buf = yaffs_dump_dev_part0(buf, dev);
2667 buf = yaffs_dump_dev_part1(buf, dev);
2672 mutex_unlock(&yaffs_context_lock);
2675 return buf - page < count ? buf - page : count;
2680 * Set the verbosity of the warnings and error messages.
2682 * Note that the names can only be a..z or _ with the current code.
2687 unsigned mask_bitfield;
2689 {"allocate", YAFFS_TRACE_ALLOCATE},
2690 {"always", YAFFS_TRACE_ALWAYS},
2691 {"background", YAFFS_TRACE_BACKGROUND},
2692 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2693 {"buffers", YAFFS_TRACE_BUFFERS},
2694 {"bug", YAFFS_TRACE_BUG},
2695 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2696 {"deletion", YAFFS_TRACE_DELETION},
2697 {"erase", YAFFS_TRACE_ERASE},
2698 {"error", YAFFS_TRACE_ERROR},
2699 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2700 {"gc", YAFFS_TRACE_GC},
2701 {"lock", YAFFS_TRACE_LOCK},
2702 {"mtd", YAFFS_TRACE_MTD},
2703 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2704 {"os", YAFFS_TRACE_OS},
2705 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2706 {"scan", YAFFS_TRACE_SCAN},
2707 {"mount", YAFFS_TRACE_MOUNT},
2708 {"tracing", YAFFS_TRACE_TRACING},
2709 {"sync", YAFFS_TRACE_SYNC},
2710 {"write", YAFFS_TRACE_WRITE},
2711 {"verify", YAFFS_TRACE_VERIFY},
2712 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2713 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2714 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2715 {"all", 0xffffffff},
2720 #define MAX_MASK_NAME_LENGTH 40
2721 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2722 unsigned long count, void *data)
2724 unsigned rg = 0, mask_bitfield;
2728 char substring[MAX_MASK_NAME_LENGTH + 1];
2734 rg = yaffs_trace_mask;
2736 while (!done && (pos < count)) {
2738 while ((pos < count) && isspace(buf[pos]))
2755 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2757 if (end > buf + pos) {
2758 mask_name = "numeral";
2759 len = end - (buf + pos);
2763 for (x = buf + pos, i = 0;
2764 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2765 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2767 substring[i] = '\0';
2769 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2770 if (strcmp(substring, mask_flags[i].mask_name)
2772 mask_name = mask_flags[i].mask_name;
2774 mask_flags[i].mask_bitfield;
2781 if (mask_name != NULL) {
2785 rg &= ~mask_bitfield;
2788 rg |= mask_bitfield;
2794 rg |= mask_bitfield;
2800 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
2802 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
2804 if (rg & YAFFS_TRACE_ALWAYS) {
2805 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2807 flag = ((rg & mask_flags[i].mask_bitfield) ==
2808 mask_flags[i].mask_bitfield) ? '+' : '-';
2809 printk(KERN_DEBUG "%c%s\n", flag,
2810 mask_flags[i].mask_name);
2817 static int yaffs_proc_write(struct file *file, const char *buf,
2818 unsigned long count, void *data)
2820 return yaffs_proc_write_trace_options(file, buf, count, data);
2823 /* Stuff to handle installation of file systems */
2824 struct file_system_to_install {
2825 struct file_system_type *fst;
2829 static struct file_system_to_install fs_to_install[] = {
2830 {&yaffs_fs_type, 0},
2831 {&yaffs2_fs_type, 0},
2835 static int __init init_yaffs_fs(void)
2838 struct file_system_to_install *fsinst;
2840 T(YAFFS_TRACE_ALWAYS,
2841 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
2843 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2844 T(YAFFS_TRACE_ALWAYS,
2846 (" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
2849 mutex_init(&yaffs_context_lock);
2851 /* Install the proc_fs entries */
2852 my_proc_entry = create_proc_entry("yaffs",
2853 S_IRUGO | S_IFREG, YPROC_ROOT);
2855 if (my_proc_entry) {
2856 my_proc_entry->write_proc = yaffs_proc_write;
2857 my_proc_entry->read_proc = yaffs_proc_read;
2858 my_proc_entry->data = NULL;
2864 /* Now add the file system entries */
2866 fsinst = fs_to_install;
2868 while (fsinst->fst && !error) {
2869 error = register_filesystem(fsinst->fst);
2871 fsinst->installed = 1;
2875 /* Any errors? uninstall */
2877 fsinst = fs_to_install;
2879 while (fsinst->fst) {
2880 if (fsinst->installed) {
2881 unregister_filesystem(fsinst->fst);
2882 fsinst->installed = 0;
2891 static void __exit exit_yaffs_fs(void)
2894 struct file_system_to_install *fsinst;
2896 T(YAFFS_TRACE_ALWAYS,
2897 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
2899 remove_proc_entry("yaffs", YPROC_ROOT);
2901 fsinst = fs_to_install;
2903 while (fsinst->fst) {
2904 if (fsinst->installed) {
2905 unregister_filesystem(fsinst->fst);
2906 fsinst->installed = 0;
2912 module_init(init_yaffs_fs)
2913 module_exit(exit_yaffs_fs)
2915 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2916 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
2917 MODULE_LICENSE("GPL");