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 yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
64 #define YPROC_ROOT NULL
66 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
68 #define WRITE_SIZE_STR "writesize"
69 #define WRITE_SIZE(mtd) ((mtd)->writesize)
71 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
73 uint64_t result = partition_size;
74 do_div(result, block_size);
75 return (uint32_t) result;
78 #include <linux/uaccess.h>
79 #include <linux/mtd/mtd.h>
82 #include "yaffs_trace.h"
83 #include "yaffs_guts.h"
84 #include "yaffs_attribs.h"
86 #include "yaffs_linux.h"
88 #include "yaffs_mtdif.h"
89 #include "yaffs_mtdif1.h"
90 #include "yaffs_mtdif2.h"
92 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
93 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
94 unsigned int yaffs_auto_checkpoint = 1;
95 unsigned int yaffs_gc_control = 1;
96 unsigned int yaffs_bg_enable = 1;
98 /* Module Parameters */
99 module_param(yaffs_trace_mask, uint, 0644);
100 module_param(yaffs_wr_attempts, uint, 0644);
101 module_param(yaffs_auto_checkpoint, uint, 0644);
102 module_param(yaffs_gc_control, uint, 0644);
103 module_param(yaffs_bg_enable, uint, 0644);
106 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
107 #define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
108 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
109 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
111 #define update_dir_time(dir) do {\
112 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
116 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
118 return yaffs_gc_control;
121 static void yaffs_gross_lock(struct yaffs_dev *dev)
123 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
124 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
125 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
128 static void yaffs_gross_unlock(struct yaffs_dev *dev)
130 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
131 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
134 static void yaffs_fill_inode_from_obj(struct inode *inode,
135 struct yaffs_obj *obj);
137 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
140 struct yaffs_obj *obj;
141 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
143 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
145 inode = iget_locked(sb, ino);
147 return ERR_PTR(-ENOMEM);
148 if (!(inode->i_state & I_NEW))
151 /* NB This is called as a side effect of other functions, but
152 * we had to release the lock to prevent deadlocks, so
153 * need to lock again.
156 yaffs_gross_lock(dev);
158 obj = yaffs_find_by_number(dev, inode->i_ino);
160 yaffs_fill_inode_from_obj(inode, obj);
162 yaffs_gross_unlock(dev);
164 unlock_new_inode(inode);
168 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
169 struct yaffs_obj *obj)
174 yaffs_trace(YAFFS_TRACE_OS,
175 "yaffs_get_inode for NULL super_block!!");
181 yaffs_trace(YAFFS_TRACE_OS,
182 "yaffs_get_inode for NULL object!!");
187 yaffs_trace(YAFFS_TRACE_OS,
188 "yaffs_get_inode for object %d",
191 inode = yaffs_iget(sb, obj->obj_id);
195 /* NB Side effect: iget calls back to yaffs_read_inode(). */
196 /* iget also increments the inode's i_count */
197 /* NB You can't be holding gross_lock or deadlock will happen! */
202 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
207 struct yaffs_obj *obj = NULL;
208 struct yaffs_dev *dev;
210 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
213 uid_t uid = current->cred->fsuid;
215 (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
217 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
221 yaffs_trace(YAFFS_TRACE_OS,
222 "yaffs_mknod: parent object %d type %d",
223 parent->obj_id, parent->variant_type);
225 yaffs_trace(YAFFS_TRACE_OS,
226 "yaffs_mknod: could not get parent object");
230 yaffs_trace(YAFFS_TRACE_OS,
231 "yaffs_mknod: making oject for %s, mode %x dev %x",
232 dentry->d_name.name, mode, rdev);
234 dev = parent->my_dev;
236 yaffs_gross_lock(dev);
238 switch (mode & S_IFMT) {
240 /* Special (socket, fifo, device...) */
241 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
243 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
244 gid, old_encode_dev(rdev));
246 case S_IFREG: /* file */
247 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
248 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
251 case S_IFDIR: /* directory */
252 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
253 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
256 case S_IFLNK: /* symlink */
257 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
258 obj = NULL; /* Do we ever get here? */
262 /* Can not call yaffs_get_inode() with gross lock held */
263 yaffs_gross_unlock(dev);
266 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
267 d_instantiate(dentry, inode);
268 update_dir_time(dir);
269 yaffs_trace(YAFFS_TRACE_OS,
270 "yaffs_mknod created object %d count = %d",
271 obj->obj_id, atomic_read(&inode->i_count));
273 yaffs_fill_inode_from_obj(dir, parent);
275 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
282 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
284 return yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
287 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
290 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
293 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
294 struct dentry *dentry)
296 struct inode *inode = old_dentry->d_inode;
297 struct yaffs_obj *obj = NULL;
298 struct yaffs_obj *link = NULL;
299 struct yaffs_dev *dev;
301 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
303 obj = yaffs_inode_to_obj(inode);
306 yaffs_gross_lock(dev);
308 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
310 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
314 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
315 d_instantiate(dentry, old_dentry->d_inode);
316 atomic_inc(&old_dentry->d_inode->i_count);
317 yaffs_trace(YAFFS_TRACE_OS,
318 "yaffs_link link count %d i_count %d",
319 old_dentry->d_inode->i_nlink,
320 atomic_read(&old_dentry->d_inode->i_count));
323 yaffs_gross_unlock(dev);
326 update_dir_time(dir);
333 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
336 struct yaffs_obj *obj;
337 struct yaffs_dev *dev;
338 uid_t uid = current->cred->fsuid;
340 (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
342 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
344 dev = yaffs_inode_to_obj(dir)->my_dev;
345 yaffs_gross_lock(dev);
346 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
347 S_IFLNK | S_IRWXUGO, uid, gid, symname);
348 yaffs_gross_unlock(dev);
353 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
354 d_instantiate(dentry, inode);
355 update_dir_time(dir);
356 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
359 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
365 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
368 struct yaffs_obj *obj;
369 struct inode *inode = NULL;
371 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
373 if (current != yaffs_dev_to_lc(dev)->readdir_process)
374 yaffs_gross_lock(dev);
376 yaffs_trace(YAFFS_TRACE_OS,
377 "yaffs_lookup for %d:%s",
378 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
380 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
382 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
384 /* Can't hold gross lock when calling yaffs_get_inode() */
385 if (current != yaffs_dev_to_lc(dev)->readdir_process)
386 yaffs_gross_unlock(dev);
389 yaffs_trace(YAFFS_TRACE_OS,
390 "yaffs_lookup found %d", obj->obj_id);
392 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
395 yaffs_trace(YAFFS_TRACE_OS, "yaffs_loookup dentry");
396 d_add(dentry, inode);
402 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
406 d_add(dentry, inode);
411 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
415 struct yaffs_dev *dev;
416 struct yaffs_obj *obj;
418 yaffs_trace(YAFFS_TRACE_OS,
419 "yaffs_unlink %d:%s",
420 (int)(dir->i_ino), dentry->d_name.name);
421 obj = yaffs_inode_to_obj(dir);
424 yaffs_gross_lock(dev);
426 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
428 if (ret_val == YAFFS_OK) {
429 dentry->d_inode->i_nlink--;
431 yaffs_gross_unlock(dev);
432 mark_inode_dirty(dentry->d_inode);
433 update_dir_time(dir);
436 yaffs_gross_unlock(dev);
440 static int yaffs_sync_object(struct file *file, int datasync)
443 struct yaffs_obj *obj;
444 struct yaffs_dev *dev;
445 struct dentry *dentry = file->f_path.dentry;
447 obj = yaffs_dentry_to_obj(dentry);
451 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, "yaffs_sync_object");
452 yaffs_gross_lock(dev);
453 yaffs_flush_file(obj, 1, datasync);
454 yaffs_gross_unlock(dev);
458 * The VFS layer already does all the dentry stuff for rename.
460 * NB: POSIX says you can rename an object over an old object of the same name
462 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
463 struct inode *new_dir, struct dentry *new_dentry)
465 struct yaffs_dev *dev;
466 int ret_val = YAFFS_FAIL;
467 struct yaffs_obj *target;
469 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
470 dev = yaffs_inode_to_obj(old_dir)->my_dev;
472 yaffs_gross_lock(dev);
474 /* Check if the target is an existing directory that is not empty. */
475 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
476 new_dentry->d_name.name);
478 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
479 !list_empty(&target->variant.dir_variant.children)) {
481 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
483 ret_val = YAFFS_FAIL;
485 /* Now does unlinking internally using shadowing mechanism */
486 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
488 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
489 old_dentry->d_name.name,
490 yaffs_inode_to_obj(new_dir),
491 new_dentry->d_name.name);
493 yaffs_gross_unlock(dev);
495 if (ret_val == YAFFS_OK) {
497 new_dentry->d_inode->i_nlink--;
498 mark_inode_dirty(new_dentry->d_inode);
501 update_dir_time(old_dir);
502 if (old_dir != new_dir)
503 update_dir_time(new_dir);
510 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
512 struct inode *inode = dentry->d_inode;
514 struct yaffs_dev *dev;
516 yaffs_trace(YAFFS_TRACE_OS,
517 "yaffs_setattr of object %d",
518 yaffs_inode_to_obj(inode)->obj_id);
520 /* Fail if a requested resize >= 2GB */
521 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
525 error = inode_change_ok(inode, attr);
529 setattr_copy(inode, attr);
530 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
531 if (attr->ia_valid & ATTR_SIZE) {
532 truncate_setsize(inode, attr->ia_size);
533 inode->i_blocks = (inode->i_size + 511) >> 9;
536 dev = yaffs_inode_to_obj(inode)->my_dev;
537 if (attr->ia_valid & ATTR_SIZE) {
538 yaffs_trace(YAFFS_TRACE_OS, "resize to %d(%x)",
539 (int)(attr->ia_size),
540 (int)(attr->ia_size));
542 yaffs_gross_lock(dev);
543 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
544 if (result == YAFFS_OK) {
549 yaffs_gross_unlock(dev);
553 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
558 #ifdef CONFIG_YAFFS_XATTR
559 static int yaffs_setxattr(struct dentry *dentry, const char *name,
560 const void *value, size_t size, int flags)
562 struct inode *inode = dentry->d_inode;
564 struct yaffs_dev *dev;
565 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
567 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
572 yaffs_gross_lock(dev);
573 result = yaffs_set_xattrib(obj, name, value, size, flags);
574 if (result == YAFFS_OK)
578 yaffs_gross_unlock(dev);
581 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
586 static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, void *buff,
589 struct inode *inode = dentry->d_inode;
591 struct yaffs_dev *dev;
592 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
594 yaffs_trace(YAFFS_TRACE_OS,
595 "yaffs_getxattr \"%s\" from object %d",
600 yaffs_gross_lock(dev);
601 error = yaffs_get_xattrib(obj, name, buff, size);
602 yaffs_gross_unlock(dev);
605 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
610 static int yaffs_removexattr(struct dentry *dentry, const char *name)
612 struct inode *inode = dentry->d_inode;
614 struct yaffs_dev *dev;
615 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
617 yaffs_trace(YAFFS_TRACE_OS,
618 "yaffs_removexattr of object %d", obj->obj_id);
623 yaffs_gross_lock(dev);
624 result = yaffs_remove_xattrib(obj, name);
625 if (result == YAFFS_OK)
629 yaffs_gross_unlock(dev);
632 yaffs_trace(YAFFS_TRACE_OS,
633 "yaffs_removexattr done returning %d", error);
638 static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
640 struct inode *inode = dentry->d_inode;
642 struct yaffs_dev *dev;
643 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
645 yaffs_trace(YAFFS_TRACE_OS,
646 "yaffs_listxattr of object %d", obj->obj_id);
650 yaffs_gross_lock(dev);
651 error = yaffs_list_xattrib(obj, buff, size);
652 yaffs_gross_unlock(dev);
655 yaffs_trace(YAFFS_TRACE_OS,
656 "yaffs_listxattr done returning %d", error);
663 static const struct inode_operations yaffs_dir_inode_operations = {
664 .create = yaffs_create,
665 .lookup = yaffs_lookup,
667 .unlink = yaffs_unlink,
668 .symlink = yaffs_symlink,
669 .mkdir = yaffs_mkdir,
670 .rmdir = yaffs_unlink,
671 .mknod = yaffs_mknod,
672 .rename = yaffs_rename,
673 .setattr = yaffs_setattr,
674 #ifdef CONFIG_YAFFS_XATTR
675 .setxattr = yaffs_setxattr,
676 .getxattr = yaffs_getxattr,
677 .listxattr = yaffs_listxattr,
678 .removexattr = yaffs_removexattr,
681 /*-----------------------------------------------------------------*/
682 /* Directory search context allows us to unlock access to yaffs during
683 * filldir without causing problems with the directory being modified.
684 * This is similar to the tried and tested mechanism used in yaffs direct.
686 * A search context iterates along a doubly linked list of siblings in the
687 * directory. If the iterating object is deleted then this would corrupt
688 * the list iteration, likely causing a crash. The search context avoids
689 * this by using the remove_obj_fn to move the search context to the
690 * next object before the object is deleted.
692 * Many readdirs (and thus seach conexts) may be alive simulateously so
693 * each struct yaffs_dev has a list of these.
695 * A seach context lives for the duration of a readdir.
697 * All these functions must be called while yaffs is locked.
700 struct yaffs_search_context {
701 struct yaffs_dev *dev;
702 struct yaffs_obj *dir_obj;
703 struct yaffs_obj *next_return;
704 struct list_head others;
708 * yaffs_new_search() creates a new search context, initialises it and
709 * adds it to the device's search context list.
711 * Called at start of readdir.
713 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
715 struct yaffs_dev *dev = dir->my_dev;
716 struct yaffs_search_context *sc =
717 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
721 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
722 sc->next_return = NULL;
725 list_entry(dir->variant.dir_variant.children.next,
726 struct yaffs_obj, siblings);
727 INIT_LIST_HEAD(&sc->others);
728 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
734 * yaffs_search_end() disposes of a search context and cleans up.
736 static void yaffs_search_end(struct yaffs_search_context *sc)
739 list_del(&sc->others);
745 * yaffs_search_advance() moves a search context to the next object.
746 * Called when the search iterates or when an object removal causes
747 * the search context to be moved to the next object.
749 static void yaffs_search_advance(struct yaffs_search_context *sc)
754 if (sc->next_return == NULL ||
755 list_empty(&sc->dir_obj->variant.dir_variant.children))
756 sc->next_return = NULL;
758 struct list_head *next = sc->next_return->siblings.next;
760 if (next == &sc->dir_obj->variant.dir_variant.children)
761 sc->next_return = NULL; /* end of list */
764 list_entry(next, struct yaffs_obj, siblings);
769 * yaffs_remove_obj_callback() is called when an object is unlinked.
770 * We check open search contexts and advance any which are currently
771 * on the object being iterated.
773 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
777 struct yaffs_search_context *sc;
778 struct list_head *search_contexts =
779 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
781 /* Iterate through the directory search contexts.
782 * If any are currently on the object being removed, then advance
783 * the search context to the next object to prevent a hanging pointer.
785 list_for_each(i, search_contexts) {
787 sc = list_entry(i, struct yaffs_search_context, others);
788 if (sc->next_return == obj)
789 yaffs_search_advance(sc);
795 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
797 struct yaffs_obj *obj;
798 struct yaffs_dev *dev;
799 struct yaffs_search_context *sc;
800 struct inode *inode = f->f_dentry->d_inode;
801 unsigned long offset, curoffs;
805 char name[YAFFS_MAX_NAME_LENGTH + 1];
807 obj = yaffs_dentry_to_obj(f->f_dentry);
810 yaffs_gross_lock(dev);
812 yaffs_dev_to_lc(dev)->readdir_process = current;
816 sc = yaffs_new_search(obj);
822 yaffs_trace(YAFFS_TRACE_OS,
823 "yaffs_readdir: starting at %d", (int)offset);
826 yaffs_trace(YAFFS_TRACE_OS,
827 "yaffs_readdir: entry . ino %d",
829 yaffs_gross_unlock(dev);
830 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
831 yaffs_gross_lock(dev);
834 yaffs_gross_lock(dev);
839 yaffs_trace(YAFFS_TRACE_OS,
840 "yaffs_readdir: entry .. ino %d",
841 (int)f->f_dentry->d_parent->d_inode->i_ino);
842 yaffs_gross_unlock(dev);
843 if (filldir(dirent, "..", 2, offset,
844 f->f_dentry->d_parent->d_inode->i_ino,
846 yaffs_gross_lock(dev);
849 yaffs_gross_lock(dev);
856 /* If the directory has changed since the open or last call to
857 readdir, rewind to after the 2 canned entries. */
858 if (f->f_version != inode->i_version) {
861 f->f_version = inode->i_version;
864 while (sc->next_return) {
867 if (curoffs >= offset) {
868 int this_inode = yaffs_get_obj_inode(l);
869 int this_type = yaffs_get_obj_type(l);
871 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
872 yaffs_trace(YAFFS_TRACE_OS,
873 "yaffs_readdir: %s inode %d",
874 name, yaffs_get_obj_inode(l));
876 yaffs_gross_unlock(dev);
881 offset, this_inode, this_type) < 0) {
882 yaffs_gross_lock(dev);
886 yaffs_gross_lock(dev);
891 yaffs_search_advance(sc);
895 yaffs_search_end(sc);
896 yaffs_dev_to_lc(dev)->readdir_process = NULL;
897 yaffs_gross_unlock(dev);
902 static const struct file_operations yaffs_dir_operations = {
903 .read = generic_read_dir,
904 .readdir = yaffs_readdir,
905 .fsync = yaffs_sync_object,
906 .llseek = generic_file_llseek,
911 static int yaffs_file_flush(struct file *file, fl_owner_t id)
913 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
915 struct yaffs_dev *dev = obj->my_dev;
917 yaffs_trace(YAFFS_TRACE_OS,
918 "yaffs_file_flush object %d (%s)",
919 obj->obj_id, obj->dirty ? "dirty" : "clean");
921 yaffs_gross_lock(dev);
923 yaffs_flush_file(obj, 1, 0);
925 yaffs_gross_unlock(dev);
930 static const struct file_operations yaffs_file_operations = {
931 .read = do_sync_read,
932 .write = do_sync_write,
933 .aio_read = generic_file_aio_read,
934 .aio_write = generic_file_aio_write,
935 .mmap = generic_file_mmap,
936 .flush = yaffs_file_flush,
937 .fsync = yaffs_sync_object,
938 .splice_read = generic_file_splice_read,
939 .splice_write = generic_file_splice_write,
940 .llseek = generic_file_llseek,
944 /* ExportFS support */
945 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
948 return yaffs_iget(sb, ino);
951 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
952 struct fid *fid, int fh_len,
955 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
956 yaffs2_nfs_get_inode);
959 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
960 struct fid *fid, int fh_len,
963 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
964 yaffs2_nfs_get_inode);
967 struct dentry *yaffs2_get_parent(struct dentry *dentry)
970 struct super_block *sb = dentry->d_inode->i_sb;
971 struct dentry *parent = ERR_PTR(-ENOENT);
973 unsigned long parent_ino;
974 struct yaffs_obj *d_obj;
975 struct yaffs_obj *parent_obj;
977 d_obj = yaffs_inode_to_obj(dentry->d_inode);
980 parent_obj = d_obj->parent;
982 parent_ino = yaffs_get_obj_inode(parent_obj);
983 inode = yaffs_iget(sb, parent_ino);
986 parent = ERR_CAST(inode);
988 parent = d_obtain_alias(inode);
989 if (!IS_ERR(parent)) {
990 parent = ERR_PTR(-ENOMEM);
1000 /* Just declare a zero structure as a NULL value implies
1001 * using the default functions of exportfs.
1004 static struct export_operations yaffs_export_ops = {
1005 .fh_to_dentry = yaffs2_fh_to_dentry,
1006 .fh_to_parent = yaffs2_fh_to_parent,
1007 .get_parent = yaffs2_get_parent,
1011 /*-----------------------------------------------------------------*/
1013 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1016 unsigned char *alias;
1019 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1021 yaffs_gross_lock(dev);
1023 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1025 yaffs_gross_unlock(dev);
1030 ret = vfs_readlink(dentry, buffer, buflen, alias);
1035 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1037 unsigned char *alias;
1039 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1041 yaffs_gross_lock(dev);
1043 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1044 yaffs_gross_unlock(dev);
1047 ret = ERR_PTR(-ENOMEM);
1051 nd_set_link(nd, alias);
1052 ret = (void *)alias;
1057 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
1063 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
1065 /* Clear the association between the inode and
1066 * the struct yaffs_obj.
1068 obj->my_inode = NULL;
1069 yaffs_inode_to_obj_lv(inode) = NULL;
1071 /* If the object freeing was deferred, then the real
1073 * This should fix the inode inconsistency problem.
1075 yaffs_handle_defered_free(obj);
1078 /* yaffs_evict_inode combines into one operation what was previously done in
1079 * yaffs_clear_inode() and yaffs_delete_inode()
1082 static void yaffs_evict_inode(struct inode *inode)
1084 struct yaffs_obj *obj;
1085 struct yaffs_dev *dev;
1088 obj = yaffs_inode_to_obj(inode);
1090 yaffs_trace(YAFFS_TRACE_OS,
1091 "yaffs_evict_inode: ino %d, count %d %s",
1093 atomic_read(&inode->i_count),
1094 obj ? "object exists" : "null object");
1096 if (!inode->i_nlink && !is_bad_inode(inode))
1098 truncate_inode_pages(&inode->i_data, 0);
1099 end_writeback(inode);
1101 if (deleteme && obj) {
1103 yaffs_gross_lock(dev);
1105 yaffs_gross_unlock(dev);
1109 yaffs_gross_lock(dev);
1110 yaffs_unstitch_obj(inode, obj);
1111 yaffs_gross_unlock(dev);
1116 static void yaffs_touch_super(struct yaffs_dev *dev)
1118 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
1120 yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb);
1125 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1127 /* Lifted from jffs2 */
1129 struct yaffs_obj *obj;
1130 unsigned char *pg_buf;
1133 struct yaffs_dev *dev;
1135 yaffs_trace(YAFFS_TRACE_OS,
1136 "yaffs_readpage_nolock at %08x, size %08x",
1137 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1138 (unsigned)PAGE_CACHE_SIZE);
1140 obj = yaffs_dentry_to_obj(f->f_dentry);
1144 BUG_ON(!PageLocked(pg));
1147 /* FIXME: Can kmap fail? */
1149 yaffs_gross_lock(dev);
1151 ret = yaffs_file_rd(obj, pg_buf,
1152 pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
1154 yaffs_gross_unlock(dev);
1160 ClearPageUptodate(pg);
1163 SetPageUptodate(pg);
1167 flush_dcache_page(pg);
1170 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
1174 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1176 int ret = yaffs_readpage_nolock(f, pg);
1181 static int yaffs_readpage(struct file *f, struct page *pg)
1185 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
1186 ret = yaffs_readpage_unlock(f, pg);
1187 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
1191 /* writepage inspired by/stolen from smbfs */
1193 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1195 struct yaffs_dev *dev;
1196 struct address_space *mapping = page->mapping;
1197 struct inode *inode;
1198 unsigned long end_index;
1200 struct yaffs_obj *obj;
1207 inode = mapping->host;
1210 i_size = i_size_read(inode);
1212 end_index = i_size >> PAGE_CACHE_SHIFT;
1214 if (page->index < end_index)
1215 n_bytes = PAGE_CACHE_SIZE;
1217 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
1219 if (page->index > end_index || !n_bytes) {
1220 yaffs_trace(YAFFS_TRACE_OS,
1221 "yaffs_writepage at %08x, inode size = %08x!!!",
1222 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1223 (unsigned)inode->i_size);
1224 yaffs_trace(YAFFS_TRACE_OS,
1225 " -> don't care!!");
1227 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1228 set_page_writeback(page);
1230 end_page_writeback(page);
1235 if (n_bytes != PAGE_CACHE_SIZE)
1236 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
1240 buffer = kmap(page);
1242 obj = yaffs_inode_to_obj(inode);
1244 yaffs_gross_lock(dev);
1246 yaffs_trace(YAFFS_TRACE_OS,
1247 "yaffs_writepage at %08x, size %08x",
1248 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes);
1249 yaffs_trace(YAFFS_TRACE_OS,
1250 "writepag0: obj = %05x, ino = %05x",
1251 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1253 n_written = yaffs_wr_file(obj, buffer,
1254 page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
1256 yaffs_touch_super(dev);
1258 yaffs_trace(YAFFS_TRACE_OS,
1259 "writepag1: obj = %05x, ino = %05x",
1260 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1262 yaffs_gross_unlock(dev);
1265 set_page_writeback(page);
1267 end_page_writeback(page);
1270 return (n_written == n_bytes) ? 0 : -ENOSPC;
1273 /* Space holding and freeing is done to ensure we have space available for
1275 * For now we just assume few parallel writes and check against a small
1277 * Todo: need to do this with a counter to handle parallel reads better.
1280 static ssize_t yaffs_hold_space(struct file *f)
1282 struct yaffs_obj *obj;
1283 struct yaffs_dev *dev;
1287 obj = yaffs_dentry_to_obj(f->f_dentry);
1291 yaffs_gross_lock(dev);
1293 n_free_chunks = yaffs_get_n_free_chunks(dev);
1295 yaffs_gross_unlock(dev);
1297 return (n_free_chunks > 20) ? 1 : 0;
1300 static void yaffs_release_space(struct file *f)
1302 struct yaffs_obj *obj;
1303 struct yaffs_dev *dev;
1305 obj = yaffs_dentry_to_obj(f->f_dentry);
1309 yaffs_gross_lock(dev);
1311 yaffs_gross_unlock(dev);
1314 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1315 loff_t pos, unsigned len, unsigned flags,
1316 struct page **pagep, void **fsdata)
1318 struct page *pg = NULL;
1319 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1325 pg = grab_cache_page_write_begin(mapping, index, flags);
1332 yaffs_trace(YAFFS_TRACE_OS,
1333 "start yaffs_write_begin index %d(%x) uptodate %d",
1334 (int)index, (int)index, PageUptodate(pg) ? 1 : 0);
1337 space_held = yaffs_hold_space(filp);
1344 /* Update page if required */
1346 if (!PageUptodate(pg))
1347 ret = yaffs_readpage_nolock(filp, pg);
1352 /* Happy path return */
1353 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
1358 yaffs_trace(YAFFS_TRACE_OS,
1359 "end yaffs_write_begin fail returning %d", ret);
1361 yaffs_release_space(filp);
1364 page_cache_release(pg);
1369 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1372 struct yaffs_obj *obj;
1373 int n_written, ipos;
1374 struct inode *inode;
1375 struct yaffs_dev *dev;
1377 obj = yaffs_dentry_to_obj(f->f_dentry);
1381 yaffs_gross_lock(dev);
1383 inode = f->f_dentry->d_inode;
1385 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1386 ipos = inode->i_size;
1391 yaffs_trace(YAFFS_TRACE_OS,
1392 "yaffs_file_write: hey obj is null!");
1394 yaffs_trace(YAFFS_TRACE_OS,
1395 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %d(%x)",
1396 (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos);
1398 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1400 yaffs_touch_super(dev);
1402 yaffs_trace(YAFFS_TRACE_OS,
1403 "yaffs_file_write: %d(%x) bytes written",
1404 (unsigned)n, (unsigned)n);
1406 if (n_written > 0) {
1409 if (ipos > inode->i_size) {
1410 inode->i_size = ipos;
1411 inode->i_blocks = (ipos + 511) >> 9;
1413 yaffs_trace(YAFFS_TRACE_OS,
1414 "yaffs_file_write size updated to %d bytes, %d blocks",
1415 ipos, (int)(inode->i_blocks));
1419 yaffs_gross_unlock(dev);
1420 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1423 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1424 loff_t pos, unsigned len, unsigned copied,
1425 struct page *pg, void *fsdadata)
1429 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1432 addr = kva + offset_into_page;
1434 yaffs_trace(YAFFS_TRACE_OS,
1435 "yaffs_write_end addr %p pos %x n_bytes %d",
1436 addr, (unsigned)pos, copied);
1438 ret = yaffs_file_write(filp, addr, copied, &pos);
1440 if (ret != copied) {
1441 yaffs_trace(YAFFS_TRACE_OS,
1442 "yaffs_write_end not same size ret %d copied %d",
1449 yaffs_release_space(filp);
1451 page_cache_release(pg);
1455 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1457 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1458 struct super_block *sb = dentry->d_sb;
1460 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
1462 yaffs_gross_lock(dev);
1464 buf->f_type = YAFFS_MAGIC;
1465 buf->f_bsize = sb->s_blocksize;
1466 buf->f_namelen = 255;
1468 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
1469 /* Do this if chunk size is not a power of 2 */
1471 uint64_t bytes_in_dev;
1472 uint64_t bytes_free;
1476 ((dev->param.end_block - dev->param.start_block +
1477 1))) * ((uint64_t) (dev->param.chunks_per_block *
1478 dev->data_bytes_per_chunk));
1480 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
1481 buf->f_blocks = bytes_in_dev;
1483 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
1484 ((uint64_t) (dev->data_bytes_per_chunk));
1486 do_div(bytes_free, sb->s_blocksize);
1488 buf->f_bfree = bytes_free;
1490 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
1493 (dev->param.end_block - dev->param.start_block + 1) *
1494 dev->param.chunks_per_block /
1495 (sb->s_blocksize / dev->data_bytes_per_chunk);
1497 yaffs_get_n_free_chunks(dev) /
1498 (sb->s_blocksize / dev->data_bytes_per_chunk);
1501 (dev->param.end_block - dev->param.start_block + 1) *
1502 dev->param.chunks_per_block *
1503 (dev->data_bytes_per_chunk / sb->s_blocksize);
1506 yaffs_get_n_free_chunks(dev) *
1507 (dev->data_bytes_per_chunk / sb->s_blocksize);
1512 buf->f_bavail = buf->f_bfree;
1514 yaffs_gross_unlock(dev);
1518 static void yaffs_flush_inodes(struct super_block *sb)
1521 struct yaffs_obj *obj;
1523 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
1524 obj = yaffs_inode_to_obj(iptr);
1526 yaffs_trace(YAFFS_TRACE_OS,
1527 "flushing obj %d", obj->obj_id);
1528 yaffs_flush_file(obj, 1, 0);
1533 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
1535 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1539 yaffs_flush_inodes(sb);
1540 yaffs_update_dirty_dirs(dev);
1541 yaffs_flush_whole_cache(dev);
1543 yaffs_checkpoint_save(dev);
1546 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
1548 unsigned erased_chunks =
1549 dev->n_erased_blocks * dev->param.chunks_per_block;
1550 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1551 unsigned scattered = 0; /* Free chunks not in an erased block */
1553 if (erased_chunks < dev->n_free_chunks)
1554 scattered = (dev->n_free_chunks - erased_chunks);
1556 if (!context->bg_running)
1558 else if (scattered < (dev->param.chunks_per_block * 2))
1560 else if (erased_chunks > dev->n_free_chunks / 2)
1562 else if (erased_chunks > dev->n_free_chunks / 4)
1568 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
1571 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1572 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
1573 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
1576 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1577 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
1579 sb->s_dirt ? "dirty" : "clean",
1580 request_checkpoint ? "checkpoint requested" : "no checkpoint",
1581 oneshot_checkpoint ? " one-shot" : "");
1583 yaffs_gross_lock(dev);
1584 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
1585 oneshot_checkpoint) && !dev->is_checkpointed;
1587 if (sb->s_dirt || do_checkpoint) {
1588 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
1590 if (oneshot_checkpoint)
1591 yaffs_auto_checkpoint &= ~4;
1593 yaffs_gross_unlock(dev);
1599 * yaffs background thread functions .
1600 * yaffs_bg_thread_fn() the thread function
1601 * yaffs_bg_start() launches the background thread.
1602 * yaffs_bg_stop() cleans up the background thread.
1605 * The thread should only run after the yaffs is initialised
1606 * The thread should be stopped before yaffs is unmounted.
1607 * The thread should not do any writing while the fs is in read only.
1610 void yaffs_background_waker(unsigned long data)
1612 wake_up_process((struct task_struct *)data);
1615 static int yaffs_bg_thread_fn(void *data)
1617 struct yaffs_dev *dev = (struct yaffs_dev *)data;
1618 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1619 unsigned long now = jiffies;
1620 unsigned long next_dir_update = now;
1621 unsigned long next_gc = now;
1622 unsigned long expires;
1623 unsigned int urgency;
1626 struct timer_list timer;
1628 yaffs_trace(YAFFS_TRACE_BACKGROUND,
1629 "yaffs_background starting for dev %p", (void *)dev);
1632 while (context->bg_running) {
1633 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
1635 if (kthread_should_stop())
1638 if (try_to_freeze())
1641 yaffs_gross_lock(dev);
1645 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
1646 yaffs_update_dirty_dirs(dev);
1647 next_dir_update = now + HZ;
1650 if (time_after(now, next_gc) && yaffs_bg_enable) {
1651 if (!dev->is_checkpointed) {
1652 urgency = yaffs_bg_gc_urgency(dev);
1653 gc_result = yaffs_bg_gc(dev, urgency);
1655 next_gc = now + HZ / 20 + 1;
1656 else if (urgency > 0)
1657 next_gc = now + HZ / 10 + 1;
1659 next_gc = now + HZ * 2;
1662 * gc not running so set to next_dir_update
1663 * to cut down on wake ups
1665 next_gc = next_dir_update;
1668 yaffs_gross_unlock(dev);
1669 expires = next_dir_update;
1670 if (time_before(next_gc, expires))
1672 if (time_before(expires, now))
1675 Y_INIT_TIMER(&timer);
1676 timer.expires = expires + 1;
1677 timer.data = (unsigned long)current;
1678 timer.function = yaffs_background_waker;
1680 set_current_state(TASK_INTERRUPTIBLE);
1683 del_timer_sync(&timer);
1689 static int yaffs_bg_start(struct yaffs_dev *dev)
1692 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1697 context->bg_running = 1;
1699 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
1700 (void *)dev, "yaffs-bg-%d",
1703 if (IS_ERR(context->bg_thread)) {
1704 retval = PTR_ERR(context->bg_thread);
1705 context->bg_thread = NULL;
1706 context->bg_running = 0;
1711 static void yaffs_bg_stop(struct yaffs_dev *dev)
1713 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
1715 ctxt->bg_running = 0;
1717 if (ctxt->bg_thread) {
1718 kthread_stop(ctxt->bg_thread);
1719 ctxt->bg_thread = NULL;
1723 static void yaffs_write_super(struct super_block *sb)
1725 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
1727 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1728 "yaffs_write_super%s",
1729 request_checkpoint ? " checkpt" : "");
1731 yaffs_do_sync_fs(sb, request_checkpoint);
1735 static int yaffs_sync_fs(struct super_block *sb, int wait)
1737 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
1739 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1740 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
1742 yaffs_do_sync_fs(sb, request_checkpoint);
1748 static LIST_HEAD(yaffs_context_list);
1749 struct mutex yaffs_context_lock;
1753 struct yaffs_options {
1755 int skip_checkpoint_read;
1756 int skip_checkpoint_write;
1759 int tags_ecc_overridden;
1760 int lazy_loading_enabled;
1761 int lazy_loading_overridden;
1762 int empty_lost_and_found;
1763 int empty_lost_and_found_overridden;
1766 #define MAX_OPT_LEN 30
1767 static int yaffs_parse_options(struct yaffs_options *options,
1768 const char *options_str)
1770 char cur_opt[MAX_OPT_LEN + 1];
1774 /* Parse through the options which is a comma seperated list */
1776 while (options_str && *options_str && !error) {
1777 memset(cur_opt, 0, MAX_OPT_LEN + 1);
1780 while (*options_str == ',')
1783 while (*options_str && *options_str != ',') {
1784 if (p < MAX_OPT_LEN) {
1785 cur_opt[p] = *options_str;
1791 if (!strcmp(cur_opt, "inband-tags")) {
1792 options->inband_tags = 1;
1793 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
1794 options->tags_ecc_on = 0;
1795 options->tags_ecc_overridden = 1;
1796 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
1797 options->tags_ecc_on = 1;
1798 options->tags_ecc_overridden = 1;
1799 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
1800 options->lazy_loading_enabled = 0;
1801 options->lazy_loading_overridden = 1;
1802 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
1803 options->lazy_loading_enabled = 1;
1804 options->lazy_loading_overridden = 1;
1805 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
1806 options->empty_lost_and_found = 0;
1807 options->empty_lost_and_found_overridden = 1;
1808 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
1809 options->empty_lost_and_found = 1;
1810 options->empty_lost_and_found_overridden = 1;
1811 } else if (!strcmp(cur_opt, "no-cache")) {
1812 options->no_cache = 1;
1813 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
1814 options->skip_checkpoint_read = 1;
1815 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
1816 options->skip_checkpoint_write = 1;
1817 } else if (!strcmp(cur_opt, "no-checkpoint")) {
1818 options->skip_checkpoint_read = 1;
1819 options->skip_checkpoint_write = 1;
1821 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
1830 static struct address_space_operations yaffs_file_address_operations = {
1831 .readpage = yaffs_readpage,
1832 .writepage = yaffs_writepage,
1833 .write_begin = yaffs_write_begin,
1834 .write_end = yaffs_write_end,
1839 static const struct inode_operations yaffs_file_inode_operations = {
1840 .setattr = yaffs_setattr,
1841 #ifdef CONFIG_YAFFS_XATTR
1842 .setxattr = yaffs_setxattr,
1843 .getxattr = yaffs_getxattr,
1844 .listxattr = yaffs_listxattr,
1845 .removexattr = yaffs_removexattr,
1849 static const struct inode_operations yaffs_symlink_inode_operations = {
1850 .readlink = yaffs_readlink,
1851 .follow_link = yaffs_follow_link,
1852 .put_link = yaffs_put_link,
1853 .setattr = yaffs_setattr,
1854 #ifdef CONFIG_YAFFS_XATTR
1855 .setxattr = yaffs_setxattr,
1856 .getxattr = yaffs_getxattr,
1857 .listxattr = yaffs_listxattr,
1858 .removexattr = yaffs_removexattr,
1862 static void yaffs_fill_inode_from_obj(struct inode *inode,
1863 struct yaffs_obj *obj)
1867 /* Check mode against the variant type and attempt to repair if broken. */
1868 u32 mode = obj->yst_mode;
1869 switch (obj->variant_type) {
1870 case YAFFS_OBJECT_TYPE_FILE:
1871 if (!S_ISREG(mode)) {
1872 obj->yst_mode &= ~S_IFMT;
1873 obj->yst_mode |= S_IFREG;
1877 case YAFFS_OBJECT_TYPE_SYMLINK:
1878 if (!S_ISLNK(mode)) {
1879 obj->yst_mode &= ~S_IFMT;
1880 obj->yst_mode |= S_IFLNK;
1884 case YAFFS_OBJECT_TYPE_DIRECTORY:
1885 if (!S_ISDIR(mode)) {
1886 obj->yst_mode &= ~S_IFMT;
1887 obj->yst_mode |= S_IFDIR;
1891 case YAFFS_OBJECT_TYPE_UNKNOWN:
1892 case YAFFS_OBJECT_TYPE_HARDLINK:
1893 case YAFFS_OBJECT_TYPE_SPECIAL:
1899 inode->i_flags |= S_NOATIME;
1901 inode->i_ino = obj->obj_id;
1902 inode->i_mode = obj->yst_mode;
1903 inode->i_uid = obj->yst_uid;
1904 inode->i_gid = obj->yst_gid;
1906 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1908 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1909 inode->i_atime.tv_nsec = 0;
1910 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1911 inode->i_mtime.tv_nsec = 0;
1912 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1913 inode->i_ctime.tv_nsec = 0;
1914 inode->i_size = yaffs_get_obj_length(obj);
1915 inode->i_blocks = (inode->i_size + 511) >> 9;
1917 inode->i_nlink = yaffs_get_obj_link_count(obj);
1919 yaffs_trace(YAFFS_TRACE_OS,
1920 "yaffs_fill_inode mode %x uid %d gid %d size %d count %d",
1921 inode->i_mode, inode->i_uid, inode->i_gid,
1922 (int)inode->i_size, atomic_read(&inode->i_count));
1924 switch (obj->yst_mode & S_IFMT) {
1925 default: /* fifo, device or socket */
1926 init_special_inode(inode, obj->yst_mode,
1927 old_decode_dev(obj->yst_rdev));
1929 case S_IFREG: /* file */
1930 inode->i_op = &yaffs_file_inode_operations;
1931 inode->i_fop = &yaffs_file_operations;
1932 inode->i_mapping->a_ops =
1933 &yaffs_file_address_operations;
1935 case S_IFDIR: /* directory */
1936 inode->i_op = &yaffs_dir_inode_operations;
1937 inode->i_fop = &yaffs_dir_operations;
1939 case S_IFLNK: /* symlink */
1940 inode->i_op = &yaffs_symlink_inode_operations;
1944 yaffs_inode_to_obj_lv(inode) = obj;
1946 obj->my_inode = inode;
1949 yaffs_trace(YAFFS_TRACE_OS,
1950 "yaffs_fill_inode invalid parameters");
1954 static void yaffs_put_super(struct super_block *sb)
1956 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1958 yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
1960 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1961 "Shutting down yaffs background thread");
1963 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1964 "yaffs background thread shut down");
1966 yaffs_gross_lock(dev);
1968 yaffs_flush_super(sb, 1);
1970 if (yaffs_dev_to_lc(dev)->put_super_fn)
1971 yaffs_dev_to_lc(dev)->put_super_fn(sb);
1973 yaffs_deinitialise(dev);
1975 yaffs_gross_unlock(dev);
1976 mutex_lock(&yaffs_context_lock);
1977 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
1978 mutex_unlock(&yaffs_context_lock);
1980 if (yaffs_dev_to_lc(dev)->spare_buffer) {
1981 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
1982 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
1988 static void yaffs_mtd_put_super(struct super_block *sb)
1990 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
1995 put_mtd_device(mtd);
1998 static const struct super_operations yaffs_super_ops = {
1999 .statfs = yaffs_statfs,
2000 .put_super = yaffs_put_super,
2001 .evict_inode = yaffs_evict_inode,
2002 .sync_fs = yaffs_sync_fs,
2003 .write_super = yaffs_write_super,
2006 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2007 struct super_block *sb,
2008 void *data, int silent)
2011 struct inode *inode = NULL;
2012 struct dentry *root;
2013 struct yaffs_dev *dev = 0;
2014 char devname_buf[BDEVNAME_SIZE + 1];
2015 struct mtd_info *mtd;
2017 char *data_str = (char *)data;
2018 struct yaffs_linux_context *context = NULL;
2019 struct yaffs_param *param;
2023 struct yaffs_options options;
2027 struct yaffs_linux_context *context_iterator;
2028 struct list_head *l;
2030 sb->s_magic = YAFFS_MAGIC;
2031 sb->s_op = &yaffs_super_ops;
2032 sb->s_flags |= MS_NOATIME;
2034 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2036 sb->s_export_op = &yaffs_export_ops;
2039 printk(KERN_INFO "yaffs: sb is NULL\n");
2040 else if (!sb->s_dev)
2041 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2042 else if (!yaffs_devname(sb, devname_buf))
2043 printk(KERN_INFO "yaffs: devname is NULL\n");
2045 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2047 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2052 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2054 memset(&options, 0, sizeof(options));
2056 if (yaffs_parse_options(&options, data_str)) {
2057 /* Option parsing failed */
2061 sb->s_blocksize = PAGE_CACHE_SIZE;
2062 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2064 yaffs_trace(YAFFS_TRACE_OS,
2065 "yaffs_read_super: Using yaffs%d", yaffs_version);
2066 yaffs_trace(YAFFS_TRACE_OS,
2067 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2069 yaffs_trace(YAFFS_TRACE_ALWAYS,
2070 "Attempting MTD mount of %u.%u,\"%s\"",
2071 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2072 yaffs_devname(sb, devname_buf));
2074 /* Check it's an mtd device..... */
2075 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2076 return NULL; /* This isn't an mtd device */
2078 /* Get the device */
2079 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2081 yaffs_trace(YAFFS_TRACE_ALWAYS,
2082 "MTD device #%u doesn't appear to exist",
2086 /* Check it's NAND */
2087 if (mtd->type != MTD_NANDFLASH) {
2088 yaffs_trace(YAFFS_TRACE_ALWAYS,
2089 "MTD device is not NAND it's type %d",
2094 yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase);
2095 yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read);
2096 yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write);
2097 yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob);
2098 yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob);
2099 yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad);
2100 yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad);
2101 yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
2102 yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
2103 yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
2104 yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
2106 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2108 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2109 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2113 /* Added NCB 26/5/2006 for completeness */
2114 if (yaffs_version == 2 && !options.inband_tags
2115 && WRITE_SIZE(mtd) == 512) {
2116 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2121 if (yaffs_version == 2) {
2122 /* Check for version 2 style functions */
2124 !mtd->block_isbad ||
2125 !mtd->block_markbad ||
2127 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2128 yaffs_trace(YAFFS_TRACE_ALWAYS,
2129 "MTD device does not support required functions");
2133 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2134 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2135 !options.inband_tags) {
2136 yaffs_trace(YAFFS_TRACE_ALWAYS,
2137 "MTD device does not have the right page sizes");
2141 /* Check for V1 style functions */
2144 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2145 yaffs_trace(YAFFS_TRACE_ALWAYS,
2146 "MTD device does not support required functions");
2150 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2151 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2152 yaffs_trace(YAFFS_TRACE_ALWAYS,
2153 "MTD device does not support have the right page sizes");
2158 /* OK, so if we got here, we have an MTD that's NAND and looks
2159 * like it has the right capabilities
2160 * Set the struct yaffs_dev up for mtd
2163 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2166 "yaffs: mtd is read only, setting superblock read only");
2167 sb->s_flags |= MS_RDONLY;
2170 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2171 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2173 if (!dev || !context) {
2183 /* Deep shit could not allocate device structure */
2184 yaffs_trace(YAFFS_TRACE_ALWAYS,
2185 "yaffs_read_super failed trying to allocate yaffs_dev");
2188 memset(dev, 0, sizeof(struct yaffs_dev));
2189 param = &(dev->param);
2191 memset(context, 0, sizeof(struct yaffs_linux_context));
2192 dev->os_context = context;
2193 INIT_LIST_HEAD(&(context->context_list));
2195 context->super = sb;
2197 dev->read_only = read_only;
2199 sb->s_fs_info = dev;
2201 dev->driver_context = mtd;
2202 param->name = mtd->name;
2204 /* Set up the memory size parameters.... */
2207 YCALCBLOCKS(mtd->size,
2208 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2210 param->start_block = 0;
2211 param->end_block = n_blocks - 1;
2212 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2213 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2214 param->n_reserved_blocks = 5;
2215 param->n_caches = (options.no_cache) ? 0 : 10;
2216 param->inband_tags = options.inband_tags;
2218 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2219 param->disable_lazy_load = 1;
2221 #ifdef CONFIG_YAFFS_XATTR
2222 param->enable_xattr = 1;
2224 if (options.lazy_loading_overridden)
2225 param->disable_lazy_load = !options.lazy_loading_enabled;
2227 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2228 param->no_tags_ecc = 1;
2231 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2233 param->defered_dir_update = 1;
2236 if (options.tags_ecc_overridden)
2237 param->no_tags_ecc = !options.tags_ecc_on;
2239 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2240 param->empty_lost_n_found = 1;
2243 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2244 param->refresh_period = 0;
2246 param->refresh_period = 500;
2249 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2250 param->always_check_erased = 1;
2253 if (options.empty_lost_and_found_overridden)
2254 param->empty_lost_n_found = options.empty_lost_and_found;
2256 /* ... and the functions. */
2257 if (yaffs_version == 2) {
2258 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2259 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2260 param->bad_block_fn = nandmtd2_mark_block_bad;
2261 param->query_block_fn = nandmtd2_query_block;
2262 yaffs_dev_to_lc(dev)->spare_buffer =
2263 kmalloc(mtd->oobsize, GFP_NOFS);
2264 param->is_yaffs2 = 1;
2265 param->total_bytes_per_chunk = mtd->writesize;
2266 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2267 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2269 param->start_block = 0;
2270 param->end_block = n_blocks - 1;
2272 /* use the MTD interface in yaffs_mtdif1.c */
2273 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2274 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2275 param->bad_block_fn = nandmtd1_mark_block_bad;
2276 param->query_block_fn = nandmtd1_query_block;
2277 param->is_yaffs2 = 0;
2279 /* ... and common functions */
2280 param->erase_fn = nandmtd_erase_block;
2281 param->initialise_flash_fn = nandmtd_initialise;
2283 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2285 param->sb_dirty_fn = yaffs_touch_super;
2286 param->gc_control = yaffs_gc_control_callback;
2288 yaffs_dev_to_lc(dev)->super = sb;
2290 #ifndef CONFIG_YAFFS_DOES_ECC
2291 param->use_nand_ecc = 1;
2294 param->skip_checkpt_rd = options.skip_checkpoint_read;
2295 param->skip_checkpt_wr = options.skip_checkpoint_write;
2297 mutex_lock(&yaffs_context_lock);
2298 /* Get a mount id */
2300 for (mount_id = 0; !found; mount_id++) {
2302 list_for_each(l, &yaffs_context_list) {
2304 list_entry(l, struct yaffs_linux_context,
2306 if (context_iterator->mount_id == mount_id)
2310 context->mount_id = mount_id;
2312 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2313 &yaffs_context_list);
2314 mutex_unlock(&yaffs_context_lock);
2316 /* Directory search handling... */
2317 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2318 param->remove_obj_fn = yaffs_remove_obj_callback;
2320 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2322 yaffs_gross_lock(dev);
2324 err = yaffs_guts_initialise(dev);
2326 yaffs_trace(YAFFS_TRACE_OS,
2327 "yaffs_read_super: guts initialised %s",
2328 (err == YAFFS_OK) ? "OK" : "FAILED");
2330 if (err == YAFFS_OK)
2331 yaffs_bg_start(dev);
2333 if (!context->bg_thread)
2334 param->defered_dir_update = 0;
2336 /* Release lock before yaffs_get_inode() */
2337 yaffs_gross_unlock(dev);
2339 /* Create root inode */
2340 if (err == YAFFS_OK)
2341 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2346 inode->i_op = &yaffs_dir_inode_operations;
2347 inode->i_fop = &yaffs_dir_operations;
2349 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2351 root = d_alloc_root(inode);
2353 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
2360 sb->s_dirt = !dev->is_checkpointed;
2361 yaffs_trace(YAFFS_TRACE_ALWAYS,
2362 "yaffs_read_super: is_checkpointed %d",
2363 dev->is_checkpointed);
2365 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
2369 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2372 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2375 static int yaffs_read_super(struct file_system_type *fs,
2376 int flags, const char *dev_name,
2377 void *data, struct vfsmount *mnt)
2380 return get_sb_bdev(fs, flags, dev_name, data,
2381 yaffs_internal_read_super_mtd, mnt);
2384 static struct file_system_type yaffs_fs_type = {
2385 .owner = THIS_MODULE,
2387 .get_sb = yaffs_read_super,
2388 .kill_sb = kill_block_super,
2389 .fs_flags = FS_REQUIRES_DEV,
2392 #ifdef CONFIG_YAFFS_YAFFS2
2394 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2397 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2400 static int yaffs2_read_super(struct file_system_type *fs,
2401 int flags, const char *dev_name, void *data,
2402 struct vfsmount *mnt)
2404 return get_sb_bdev(fs, flags, dev_name, data,
2405 yaffs2_internal_read_super_mtd, mnt);
2408 static struct file_system_type yaffs2_fs_type = {
2409 .owner = THIS_MODULE,
2411 .get_sb = yaffs2_read_super,
2412 .kill_sb = kill_block_super,
2413 .fs_flags = FS_REQUIRES_DEV,
2415 #endif /* CONFIG_YAFFS_YAFFS2 */
2417 static struct proc_dir_entry *my_proc_entry;
2419 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
2421 struct yaffs_param *param = &dev->param;
2422 buf += sprintf(buf, "start_block........... %d\n", param->start_block);
2423 buf += sprintf(buf, "end_block............. %d\n", param->end_block);
2424 buf += sprintf(buf, "total_bytes_per_chunk. %d\n",
2425 param->total_bytes_per_chunk);
2426 buf += sprintf(buf, "use_nand_ecc.......... %d\n",
2427 param->use_nand_ecc);
2428 buf += sprintf(buf, "no_tags_ecc........... %d\n", param->no_tags_ecc);
2429 buf += sprintf(buf, "is_yaffs2............. %d\n", param->is_yaffs2);
2430 buf += sprintf(buf, "inband_tags........... %d\n", param->inband_tags);
2431 buf += sprintf(buf, "empty_lost_n_found.... %d\n",
2432 param->empty_lost_n_found);
2433 buf += sprintf(buf, "disable_lazy_load..... %d\n",
2434 param->disable_lazy_load);
2435 buf += sprintf(buf, "refresh_period........ %d\n",
2436 param->refresh_period);
2437 buf += sprintf(buf, "n_caches.............. %d\n", param->n_caches);
2438 buf += sprintf(buf, "n_reserved_blocks..... %d\n",
2439 param->n_reserved_blocks);
2440 buf += sprintf(buf, "always_check_erased... %d\n",
2441 param->always_check_erased);
2446 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
2449 sprintf(buf, "data_bytes_per_chunk.. %d\n",
2450 dev->data_bytes_per_chunk);
2451 buf += sprintf(buf, "chunk_grp_bits........ %d\n", dev->chunk_grp_bits);
2452 buf += sprintf(buf, "chunk_grp_size........ %d\n", dev->chunk_grp_size);
2454 sprintf(buf, "n_erased_blocks....... %d\n", dev->n_erased_blocks);
2456 sprintf(buf, "blocks_in_checkpt..... %d\n", dev->blocks_in_checkpt);
2457 buf += sprintf(buf, "\n");
2458 buf += sprintf(buf, "n_tnodes.............. %d\n", dev->n_tnodes);
2459 buf += sprintf(buf, "n_obj................. %d\n", dev->n_obj);
2460 buf += sprintf(buf, "n_free_chunks......... %d\n", dev->n_free_chunks);
2461 buf += sprintf(buf, "\n");
2462 buf += sprintf(buf, "n_page_writes......... %u\n", dev->n_page_writes);
2463 buf += sprintf(buf, "n_page_reads.......... %u\n", dev->n_page_reads);
2464 buf += sprintf(buf, "n_erasures............ %u\n", dev->n_erasures);
2465 buf += sprintf(buf, "n_gc_copies........... %u\n", dev->n_gc_copies);
2466 buf += sprintf(buf, "all_gcs............... %u\n", dev->all_gcs);
2468 sprintf(buf, "passive_gc_count...... %u\n", dev->passive_gc_count);
2470 sprintf(buf, "oldest_dirty_gc_count. %u\n",
2471 dev->oldest_dirty_gc_count);
2472 buf += sprintf(buf, "n_gc_blocks........... %u\n", dev->n_gc_blocks);
2473 buf += sprintf(buf, "bg_gcs................ %u\n", dev->bg_gcs);
2475 sprintf(buf, "n_retired_writes...... %u\n", dev->n_retired_writes);
2477 sprintf(buf, "n_retired_blocks...... %u\n", dev->n_retired_blocks);
2478 buf += sprintf(buf, "n_ecc_fixed........... %u\n", dev->n_ecc_fixed);
2479 buf += sprintf(buf, "n_ecc_unfixed......... %u\n", dev->n_ecc_unfixed);
2481 sprintf(buf, "n_tags_ecc_fixed...... %u\n", dev->n_tags_ecc_fixed);
2483 sprintf(buf, "n_tags_ecc_unfixed.... %u\n",
2484 dev->n_tags_ecc_unfixed);
2485 buf += sprintf(buf, "cache_hits............ %u\n", dev->cache_hits);
2487 sprintf(buf, "n_deleted_files....... %u\n", dev->n_deleted_files);
2489 sprintf(buf, "n_unlinked_files...... %u\n", dev->n_unlinked_files);
2490 buf += sprintf(buf, "refresh_count......... %u\n", dev->refresh_count);
2491 buf += sprintf(buf, "n_bg_deletions........ %u\n", dev->n_bg_deletions);
2496 static int yaffs_proc_read(char *page,
2498 off_t offset, int count, int *eof, void *data)
2500 struct list_head *item;
2505 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2506 * We use 'offset' (*ppos) to indicate where we are in dev_list.
2507 * This also assumes the user has posted a read buffer large
2508 * enough to hold the complete output; but that's life in /proc.
2513 /* Print header first */
2515 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__ "\n");
2517 buf += sprintf(buf, "\n");
2521 mutex_lock(&yaffs_context_lock);
2523 /* Locate and print the Nth entry. Order N-squared but N is small. */
2524 list_for_each(item, &yaffs_context_list) {
2525 struct yaffs_linux_context *dc =
2526 list_entry(item, struct yaffs_linux_context,
2528 struct yaffs_dev *dev = dc->dev;
2530 if (n < (step & ~1)) {
2534 if ((step & 1) == 0) {
2536 sprintf(buf, "\nDevice %d \"%s\"\n", n,
2538 buf = yaffs_dump_dev_part0(buf, dev);
2540 buf = yaffs_dump_dev_part1(buf, dev);
2545 mutex_unlock(&yaffs_context_lock);
2548 return buf - page < count ? buf - page : count;
2553 * Set the verbosity of the warnings and error messages.
2555 * Note that the names can only be a..z or _ with the current code.
2560 unsigned mask_bitfield;
2562 {"allocate", YAFFS_TRACE_ALLOCATE},
2563 {"always", YAFFS_TRACE_ALWAYS},
2564 {"background", YAFFS_TRACE_BACKGROUND},
2565 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2566 {"buffers", YAFFS_TRACE_BUFFERS},
2567 {"bug", YAFFS_TRACE_BUG},
2568 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2569 {"deletion", YAFFS_TRACE_DELETION},
2570 {"erase", YAFFS_TRACE_ERASE},
2571 {"error", YAFFS_TRACE_ERROR},
2572 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2573 {"gc", YAFFS_TRACE_GC},
2574 {"lock", YAFFS_TRACE_LOCK},
2575 {"mtd", YAFFS_TRACE_MTD},
2576 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2577 {"os", YAFFS_TRACE_OS},
2578 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2579 {"scan", YAFFS_TRACE_SCAN},
2580 {"mount", YAFFS_TRACE_MOUNT},
2581 {"tracing", YAFFS_TRACE_TRACING},
2582 {"sync", YAFFS_TRACE_SYNC},
2583 {"write", YAFFS_TRACE_WRITE},
2584 {"verify", YAFFS_TRACE_VERIFY},
2585 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2586 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2587 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2588 {"all", 0xffffffff},
2593 #define MAX_MASK_NAME_LENGTH 40
2594 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2595 unsigned long count, void *data)
2597 unsigned rg = 0, mask_bitfield;
2601 char substring[MAX_MASK_NAME_LENGTH + 1];
2607 rg = yaffs_trace_mask;
2609 while (!done && (pos < count)) {
2611 while ((pos < count) && isspace(buf[pos]))
2628 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2630 if (end > buf + pos) {
2631 mask_name = "numeral";
2632 len = end - (buf + pos);
2636 for (x = buf + pos, i = 0;
2637 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2638 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2640 substring[i] = '\0';
2642 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2643 if (strcmp(substring, mask_flags[i].mask_name)
2645 mask_name = mask_flags[i].mask_name;
2647 mask_flags[i].mask_bitfield;
2654 if (mask_name != NULL) {
2658 rg &= ~mask_bitfield;
2661 rg |= mask_bitfield;
2667 rg |= mask_bitfield;
2673 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
2675 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
2677 if (rg & YAFFS_TRACE_ALWAYS) {
2678 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2680 flag = ((rg & mask_flags[i].mask_bitfield) ==
2681 mask_flags[i].mask_bitfield) ? '+' : '-';
2682 printk(KERN_DEBUG "%c%s\n", flag,
2683 mask_flags[i].mask_name);
2690 static int yaffs_proc_write(struct file *file, const char *buf,
2691 unsigned long count, void *data)
2693 return yaffs_proc_write_trace_options(file, buf, count, data);
2696 /* Stuff to handle installation of file systems */
2697 struct file_system_to_install {
2698 struct file_system_type *fst;
2702 static struct file_system_to_install fs_to_install[] = {
2703 {&yaffs_fs_type, 0},
2704 {&yaffs2_fs_type, 0},
2708 static int __init init_yaffs_fs(void)
2711 struct file_system_to_install *fsinst;
2713 yaffs_trace(YAFFS_TRACE_ALWAYS,
2714 "yaffs built " __DATE__ " " __TIME__ " Installing.");
2716 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2717 yaffs_trace(YAFFS_TRACE_ALWAYS,
2718 "\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n");
2721 mutex_init(&yaffs_context_lock);
2723 /* Install the proc_fs entries */
2724 my_proc_entry = create_proc_entry("yaffs",
2725 S_IRUGO | S_IFREG, YPROC_ROOT);
2727 if (my_proc_entry) {
2728 my_proc_entry->write_proc = yaffs_proc_write;
2729 my_proc_entry->read_proc = yaffs_proc_read;
2730 my_proc_entry->data = NULL;
2736 /* Now add the file system entries */
2738 fsinst = fs_to_install;
2740 while (fsinst->fst && !error) {
2741 error = register_filesystem(fsinst->fst);
2743 fsinst->installed = 1;
2747 /* Any errors? uninstall */
2749 fsinst = fs_to_install;
2751 while (fsinst->fst) {
2752 if (fsinst->installed) {
2753 unregister_filesystem(fsinst->fst);
2754 fsinst->installed = 0;
2763 static void __exit exit_yaffs_fs(void)
2766 struct file_system_to_install *fsinst;
2768 yaffs_trace(YAFFS_TRACE_ALWAYS,
2769 "yaffs built " __DATE__ " " __TIME__ " removing.");
2771 remove_proc_entry("yaffs", YPROC_ROOT);
2773 fsinst = fs_to_install;
2775 while (fsinst->fst) {
2776 if (fsinst->installed) {
2777 unregister_filesystem(fsinst->fst);
2778 fsinst->installed = 0;
2784 module_init(init_yaffs_fs)
2785 module_exit(exit_yaffs_fs)
2787 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2788 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
2789 MODULE_LICENSE("GPL");