2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <linux/init.h>
44 #include <linux/proc_fs.h>
45 #include <linux/pagemap.h>
46 #include <linux/mtd/mtd.h>
47 #include <linux/interrupt.h>
48 #include <linux/string.h>
49 #include <linux/ctype.h>
50 #include <linux/namei.h>
51 #include <linux/exportfs.h>
52 #include <linux/kthread.h>
53 #include <linux/delay.h>
54 #include <linux/freezer.h>
55 #include <asm/div64.h>
56 #include <linux/statfs.h>
57 #include <linux/uaccess.h>
58 #include <linux/mtd/mtd.h>
61 #include "yaffs_trace.h"
62 #include "yaffs_guts.h"
63 #include "yaffs_attribs.h"
64 #include "yaffs_linux.h"
65 #include "yaffs_mtdif.h"
66 #include "yaffs_mtdif1.h"
67 #include "yaffs_mtdif2.h"
69 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
70 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
71 unsigned int yaffs_auto_checkpoint = 1;
72 unsigned int yaffs_gc_control = 1;
73 unsigned int yaffs_bg_enable = 1;
74 unsigned int yaffs_auto_select = 1;
76 /* Module Parameters */
77 module_param(yaffs_trace_mask, uint, 0644);
78 module_param(yaffs_wr_attempts, uint, 0644);
79 module_param(yaffs_auto_checkpoint, uint, 0644);
80 module_param(yaffs_gc_control, uint, 0644);
81 module_param(yaffs_bg_enable, uint, 0644);
82 module_param(yaffs_auto_select, uint, 0644);
84 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
86 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
88 uint64_t result = partition_size;
90 do_div(result, block_size);
91 return (uint32_t) result;
94 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
95 #define yaffs_inode_to_obj(iptr)\
96 ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
97 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
98 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
100 #define update_dir_time(dir) do {\
101 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
105 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
107 return yaffs_gc_control;
110 static void yaffs_gross_lock(struct yaffs_dev *dev)
112 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
113 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
114 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
117 static void yaffs_gross_unlock(struct yaffs_dev *dev)
119 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
120 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
123 static void yaffs_fill_inode_from_obj(struct inode *inode,
124 struct yaffs_obj *obj);
126 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
129 struct yaffs_obj *obj;
130 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
132 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
134 inode = iget_locked(sb, ino);
136 return ERR_PTR(-ENOMEM);
137 if (!(inode->i_state & I_NEW))
140 /* NB This is called as a side effect of other functions, but
141 * we had to release the lock to prevent deadlocks, so
142 * need to lock again.
145 yaffs_gross_lock(dev);
147 obj = yaffs_find_by_number(dev, inode->i_ino);
148 yaffs_fill_inode_from_obj(inode, obj);
150 yaffs_gross_unlock(dev);
152 unlock_new_inode(inode);
156 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
157 struct yaffs_obj *obj)
162 yaffs_trace(YAFFS_TRACE_OS,
163 "yaffs_get_inode for NULL super_block!!");
169 yaffs_trace(YAFFS_TRACE_OS,
170 "yaffs_get_inode for NULL object!!");
175 yaffs_trace(YAFFS_TRACE_OS,
176 "yaffs_get_inode for object %d",
179 inode = yaffs_iget(sb, obj->obj_id);
183 /* NB Side effect: iget calls back to yaffs_read_inode(). */
184 /* iget also increments the inode's i_count */
185 /* NB You can't be holding gross_lock or deadlock will happen! */
190 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
194 struct yaffs_obj *obj = NULL;
195 struct yaffs_dev *dev;
196 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
198 uid_t uid = current->cred->fsuid;
200 (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
202 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
206 yaffs_trace(YAFFS_TRACE_OS,
207 "yaffs_mknod: could not get parent object");
211 yaffs_trace(YAFFS_TRACE_OS,
212 "yaffs_mknod: parent object %d type %d",
213 parent->obj_id, parent->variant_type);
215 yaffs_trace(YAFFS_TRACE_OS,
216 "yaffs_mknod: making oject for %s, mode %x dev %x",
217 dentry->d_name.name, mode, rdev);
219 dev = parent->my_dev;
221 yaffs_gross_lock(dev);
223 if (yaffs_get_n_free_chunks(dev) < 1) {
228 switch (mode & S_IFMT) {
230 /* Special (socket, fifo, device...) */
231 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
233 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
234 gid, old_encode_dev(rdev));
236 case S_IFREG: /* file */
237 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
238 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
241 case S_IFDIR: /* directory */
242 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
243 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
246 case S_IFLNK: /* symlink */
247 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
248 obj = NULL; /* Do we ever get here? */
257 /* Can not call yaffs_get_inode() with gross lock held */
258 yaffs_gross_unlock(dev);
261 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
262 d_instantiate(dentry, inode);
263 update_dir_time(dir);
264 yaffs_trace(YAFFS_TRACE_OS,
265 "yaffs_mknod created object %d count = %d",
266 obj->obj_id, atomic_read(&inode->i_count));
267 yaffs_fill_inode_from_obj(dir, parent);
271 yaffs_gross_unlock(dev);
272 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod error %d", error);
277 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
279 return yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
282 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
285 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
288 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
289 struct dentry *dentry)
291 struct inode *inode = old_dentry->d_inode;
292 struct yaffs_obj *obj = NULL;
293 struct yaffs_obj *link = NULL;
294 struct yaffs_dev *dev;
296 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
298 obj = yaffs_inode_to_obj(inode);
301 yaffs_gross_lock(dev);
303 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
305 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
309 set_nlink(old_dentry->d_inode, yaffs_get_obj_link_count(obj));
310 d_instantiate(dentry, old_dentry->d_inode);
311 atomic_inc(&old_dentry->d_inode->i_count);
312 yaffs_trace(YAFFS_TRACE_OS,
313 "yaffs_link link count %d i_count %d",
314 old_dentry->d_inode->i_nlink,
315 atomic_read(&old_dentry->d_inode->i_count));
318 yaffs_gross_unlock(dev);
321 update_dir_time(dir);
328 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
331 struct yaffs_obj *obj;
332 struct yaffs_dev *dev;
334 uid_t uid = current->cred->fsuid;
336 (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
338 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
340 if (strnlen(dentry->d_name.name, YAFFS_MAX_NAME_LENGTH + 1) >
341 YAFFS_MAX_NAME_LENGTH)
342 return -ENAMETOOLONG;
344 if (strnlen(symname, YAFFS_MAX_ALIAS_LENGTH + 1) >
345 YAFFS_MAX_ALIAS_LENGTH)
346 return -ENAMETOOLONG;
348 dev = yaffs_inode_to_obj(dir)->my_dev;
349 yaffs_gross_lock(dev);
350 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
351 S_IFLNK | S_IRWXUGO, uid, gid, symname);
352 yaffs_gross_unlock(dev);
355 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
359 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
360 d_instantiate(dentry, inode);
361 update_dir_time(dir);
362 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
367 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
370 struct yaffs_obj *obj;
371 struct inode *inode = NULL;
372 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
374 if (current != yaffs_dev_to_lc(dev)->readdir_process)
375 yaffs_gross_lock(dev);
377 yaffs_trace(YAFFS_TRACE_OS,
378 "yaffs_lookup for %d:%s",
379 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
381 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
383 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
385 /* Can't hold gross lock when calling yaffs_get_inode() */
386 if (current != yaffs_dev_to_lc(dev)->readdir_process)
387 yaffs_gross_unlock(dev);
390 yaffs_trace(YAFFS_TRACE_OS,
391 "yaffs_lookup found %d", obj->obj_id);
393 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
396 yaffs_trace(YAFFS_TRACE_OS, "yaffs_loookup dentry");
397 d_add(dentry, inode);
403 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
407 d_add(dentry, inode);
412 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 inode_dec_link_count(dentry->d_inode);
431 yaffs_gross_unlock(dev);
432 update_dir_time(dir);
435 yaffs_gross_unlock(dev);
439 static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync)
442 struct yaffs_obj *obj;
443 struct yaffs_dev *dev;
444 struct dentry *dentry = file->f_path.dentry;
446 obj = yaffs_dentry_to_obj(dentry);
450 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, "yaffs_sync_object");
451 yaffs_gross_lock(dev);
452 yaffs_flush_file(obj, 1, datasync);
453 yaffs_gross_unlock(dev);
457 * The VFS layer already does all the dentry stuff for rename.
459 * NB: POSIX says you can rename an object over an old object of the same name
461 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
462 struct inode *new_dir, struct dentry *new_dentry)
464 struct yaffs_dev *dev;
465 int ret_val = YAFFS_FAIL;
466 struct yaffs_obj *target;
468 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
469 dev = yaffs_inode_to_obj(old_dir)->my_dev;
471 yaffs_gross_lock(dev);
473 /* Check if the target is an existing directory that is not empty. */
474 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
475 new_dentry->d_name.name);
477 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
478 !list_empty(&target->variant.dir_variant.children)) {
480 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
482 ret_val = YAFFS_FAIL;
484 /* Now does unlinking internally using shadowing mechanism */
485 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
487 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
488 old_dentry->d_name.name,
489 yaffs_inode_to_obj(new_dir),
490 new_dentry->d_name.name);
492 yaffs_gross_unlock(dev);
494 if (ret_val == YAFFS_OK) {
496 inode_dec_link_count(new_dentry->d_inode);
498 update_dir_time(old_dir);
499 if (old_dir != new_dir)
500 update_dir_time(new_dir);
507 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
509 struct inode *inode = dentry->d_inode;
511 struct yaffs_dev *dev;
514 yaffs_trace(YAFFS_TRACE_OS,
515 "yaffs_setattr of object %d",
516 yaffs_inode_to_obj(inode)->obj_id);
518 /* Fail if a requested resize >= 2GB */
519 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
523 error = inode_change_ok(inode, attr);
526 setattr_copy(inode, attr);
527 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
528 if (attr->ia_valid & ATTR_SIZE) {
529 truncate_setsize(inode, attr->ia_size);
530 inode->i_blocks = (inode->i_size + 511) >> 9;
532 dev = yaffs_inode_to_obj(inode)->my_dev;
533 if (attr->ia_valid & ATTR_SIZE) {
534 yaffs_trace(YAFFS_TRACE_OS, "resize to %d(%x)",
535 (int)(attr->ia_size),
536 (int)(attr->ia_size));
539 yaffs_gross_lock(dev);
540 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
541 if (result != YAFFS_OK)
543 yaffs_gross_unlock(dev);
546 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
551 static int yaffs_setxattr(struct dentry *dentry, const char *name,
552 const void *value, size_t size, int flags)
554 struct inode *inode = dentry->d_inode;
556 struct yaffs_dev *dev;
557 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
559 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
562 yaffs_gross_lock(dev);
563 error = yaffs_set_xattrib(obj, name, value, size, flags);
564 yaffs_gross_unlock(dev);
566 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
571 static ssize_t yaffs_getxattr(struct dentry *dentry, const char *name,
572 void *buff, size_t size)
574 struct inode *inode = dentry->d_inode;
576 struct yaffs_dev *dev;
577 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
579 yaffs_trace(YAFFS_TRACE_OS,
580 "yaffs_getxattr \"%s\" from object %d",
584 yaffs_gross_lock(dev);
585 error = yaffs_get_xattrib(obj, name, buff, size);
586 yaffs_gross_unlock(dev);
588 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
593 static int yaffs_removexattr(struct dentry *dentry, const char *name)
595 struct inode *inode = dentry->d_inode;
597 struct yaffs_dev *dev;
598 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
600 yaffs_trace(YAFFS_TRACE_OS,
601 "yaffs_removexattr of object %d", obj->obj_id);
604 yaffs_gross_lock(dev);
605 error = yaffs_remove_xattrib(obj, name);
606 yaffs_gross_unlock(dev);
608 yaffs_trace(YAFFS_TRACE_OS,
609 "yaffs_removexattr done returning %d", error);
614 static ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
616 struct inode *inode = dentry->d_inode;
618 struct yaffs_dev *dev;
619 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
621 yaffs_trace(YAFFS_TRACE_OS,
622 "yaffs_listxattr of object %d", obj->obj_id);
625 yaffs_gross_lock(dev);
626 error = yaffs_list_xattrib(obj, buff, size);
627 yaffs_gross_unlock(dev);
629 yaffs_trace(YAFFS_TRACE_OS,
630 "yaffs_listxattr done returning %d", error);
635 static const struct inode_operations yaffs_dir_inode_operations = {
636 .create = yaffs_create,
637 .lookup = yaffs_lookup,
639 .unlink = yaffs_unlink,
640 .symlink = yaffs_symlink,
641 .mkdir = yaffs_mkdir,
642 .rmdir = yaffs_unlink,
643 .mknod = yaffs_mknod,
644 .rename = yaffs_rename,
645 .setattr = yaffs_setattr,
646 .setxattr = yaffs_setxattr,
647 .getxattr = yaffs_getxattr,
648 .listxattr = yaffs_listxattr,
649 .removexattr = yaffs_removexattr,
651 /*-----------------------------------------------------------------*/
652 /* Directory search context allows us to unlock access to yaffs during
653 * filldir without causing problems with the directory being modified.
654 * This is similar to the tried and tested mechanism used in yaffs direct.
656 * A search context iterates along a doubly linked list of siblings in the
657 * directory. If the iterating object is deleted then this would corrupt
658 * the list iteration, likely causing a crash. The search context avoids
659 * this by using the remove_obj_fn to move the search context to the
660 * next object before the object is deleted.
662 * Many readdirs (and thus seach conexts) may be alive simulateously so
663 * each struct yaffs_dev has a list of these.
665 * A seach context lives for the duration of a readdir.
667 * All these functions must be called while yaffs is locked.
670 struct yaffs_search_context {
671 struct yaffs_dev *dev;
672 struct yaffs_obj *dir_obj;
673 struct yaffs_obj *next_return;
674 struct list_head others;
678 * yaffs_new_search() creates a new search context, initialises it and
679 * adds it to the device's search context list.
681 * Called at start of readdir.
683 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
685 struct yaffs_dev *dev = dir->my_dev;
686 struct yaffs_search_context *sc =
687 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
694 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
695 sc->next_return = NULL;
698 list_entry(dir->variant.dir_variant.children.next,
699 struct yaffs_obj, siblings);
700 INIT_LIST_HEAD(&sc->others);
701 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
707 * yaffs_search_end() disposes of a search context and cleans up.
709 static void yaffs_search_end(struct yaffs_search_context *sc)
712 list_del(&sc->others);
718 * yaffs_search_advance() moves a search context to the next object.
719 * Called when the search iterates or when an object removal causes
720 * the search context to be moved to the next object.
722 static void yaffs_search_advance(struct yaffs_search_context *sc)
727 if (sc->next_return == NULL ||
728 list_empty(&sc->dir_obj->variant.dir_variant.children))
729 sc->next_return = NULL;
731 struct list_head *next = sc->next_return->siblings.next;
733 if (next == &sc->dir_obj->variant.dir_variant.children)
734 sc->next_return = NULL; /* end of list */
737 list_entry(next, struct yaffs_obj, siblings);
742 * yaffs_remove_obj_callback() is called when an object is unlinked.
743 * We check open search contexts and advance any which are currently
744 * on the object being iterated.
746 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
750 struct yaffs_search_context *sc;
751 struct list_head *search_contexts =
752 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
754 /* Iterate through the directory search contexts.
755 * If any are currently on the object being removed, then advance
756 * the search context to the next object to prevent a hanging pointer.
758 list_for_each(i, search_contexts) {
759 sc = list_entry(i, struct yaffs_search_context, others);
760 if (sc->next_return == obj)
761 yaffs_search_advance(sc);
766 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
768 struct yaffs_obj *obj;
769 struct yaffs_dev *dev;
770 struct yaffs_search_context *sc;
771 struct inode *inode = f->f_dentry->d_inode;
772 unsigned long offset, curoffs;
775 char name[YAFFS_MAX_NAME_LENGTH + 1];
777 obj = yaffs_dentry_to_obj(f->f_dentry);
780 yaffs_gross_lock(dev);
782 yaffs_dev_to_lc(dev)->readdir_process = current;
786 sc = yaffs_new_search(obj);
792 yaffs_trace(YAFFS_TRACE_OS,
793 "yaffs_readdir: starting at %d", (int)offset);
796 yaffs_trace(YAFFS_TRACE_OS,
797 "yaffs_readdir: entry . ino %d",
799 yaffs_gross_unlock(dev);
800 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
801 yaffs_gross_lock(dev);
804 yaffs_gross_lock(dev);
809 yaffs_trace(YAFFS_TRACE_OS,
810 "yaffs_readdir: entry .. ino %d",
811 (int)f->f_dentry->d_parent->d_inode->i_ino);
812 yaffs_gross_unlock(dev);
813 if (filldir(dirent, "..", 2, offset,
814 f->f_dentry->d_parent->d_inode->i_ino,
816 yaffs_gross_lock(dev);
819 yaffs_gross_lock(dev);
826 /* If the directory has changed since the open or last call to
827 readdir, rewind to after the 2 canned entries. */
828 if (f->f_version != inode->i_version) {
831 f->f_version = inode->i_version;
834 while (sc->next_return) {
837 if (curoffs >= offset) {
838 int this_inode = yaffs_get_obj_inode(l);
839 int this_type = yaffs_get_obj_type(l);
841 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
842 yaffs_trace(YAFFS_TRACE_OS,
843 "yaffs_readdir: %s inode %d",
844 name, yaffs_get_obj_inode(l));
846 yaffs_gross_unlock(dev);
851 offset, this_inode, this_type) < 0) {
852 yaffs_gross_lock(dev);
856 yaffs_gross_lock(dev);
861 yaffs_search_advance(sc);
865 yaffs_search_end(sc);
866 yaffs_dev_to_lc(dev)->readdir_process = NULL;
867 yaffs_gross_unlock(dev);
872 static const struct file_operations yaffs_dir_operations = {
873 .read = generic_read_dir,
874 .readdir = yaffs_readdir,
875 .fsync = yaffs_sync_object,
876 .llseek = generic_file_llseek,
881 static int yaffs_file_flush(struct file *file, fl_owner_t id)
883 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
884 struct yaffs_dev *dev = obj->my_dev;
886 yaffs_trace(YAFFS_TRACE_OS,
887 "yaffs_file_flush object %d (%s)",
888 obj->obj_id, obj->dirty ? "dirty" : "clean");
890 yaffs_gross_lock(dev);
892 yaffs_flush_file(obj, 1, 0);
894 yaffs_gross_unlock(dev);
899 static const struct file_operations yaffs_file_operations = {
900 .read = do_sync_read,
901 .write = do_sync_write,
902 .aio_read = generic_file_aio_read,
903 .aio_write = generic_file_aio_write,
904 .mmap = generic_file_mmap,
905 .flush = yaffs_file_flush,
906 .fsync = yaffs_sync_object,
907 .splice_read = generic_file_splice_read,
908 .splice_write = generic_file_splice_write,
909 .llseek = generic_file_llseek,
913 /* ExportFS support */
914 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
917 return yaffs_iget(sb, ino);
920 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
921 struct fid *fid, int fh_len,
924 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
925 yaffs2_nfs_get_inode);
928 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
929 struct fid *fid, int fh_len,
932 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
933 yaffs2_nfs_get_inode);
936 struct dentry *yaffs2_get_parent(struct dentry *dentry)
939 struct super_block *sb = dentry->d_inode->i_sb;
940 struct dentry *parent = ERR_PTR(-ENOENT);
942 unsigned long parent_ino;
943 struct yaffs_obj *d_obj;
944 struct yaffs_obj *parent_obj;
946 d_obj = yaffs_inode_to_obj(dentry->d_inode);
949 parent_obj = d_obj->parent;
951 parent_ino = yaffs_get_obj_inode(parent_obj);
952 inode = yaffs_iget(sb, parent_ino);
955 parent = ERR_CAST(inode);
957 parent = d_obtain_alias(inode);
958 if (!IS_ERR(parent)) {
959 parent = ERR_PTR(-ENOMEM);
968 /* Just declare a zero structure as a NULL value implies
969 * using the default functions of exportfs.
972 static struct export_operations yaffs_export_ops = {
973 .fh_to_dentry = yaffs2_fh_to_dentry,
974 .fh_to_parent = yaffs2_fh_to_parent,
975 .get_parent = yaffs2_get_parent,
979 /*-----------------------------------------------------------------*/
981 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
984 unsigned char *alias;
986 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
988 yaffs_gross_lock(dev);
990 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
992 yaffs_gross_unlock(dev);
997 ret = vfs_readlink(dentry, buffer, buflen, alias);
1002 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1004 unsigned char *alias;
1006 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1008 yaffs_gross_lock(dev);
1010 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1011 yaffs_gross_unlock(dev);
1014 ret = ERR_PTR(-ENOMEM);
1018 nd_set_link(nd, alias);
1019 ret = (void *)alias;
1024 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
1030 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
1032 /* Clear the association between the inode and
1033 * the struct yaffs_obj.
1035 obj->my_inode = NULL;
1036 yaffs_inode_to_obj_lv(inode) = NULL;
1038 /* If the object freeing was deferred, then the real
1040 * This should fix the inode inconsistency problem.
1042 yaffs_handle_defered_free(obj);
1045 /* yaffs_evict_inode combines into one operation what was previously done in
1046 * yaffs_clear_inode() and yaffs_delete_inode()
1049 static void yaffs_evict_inode(struct inode *inode)
1051 struct yaffs_obj *obj;
1052 struct yaffs_dev *dev;
1055 obj = yaffs_inode_to_obj(inode);
1057 yaffs_trace(YAFFS_TRACE_OS,
1058 "yaffs_evict_inode: ino %d, count %d %s",
1060 atomic_read(&inode->i_count),
1061 obj ? "object exists" : "null object");
1063 if (!inode->i_nlink && !is_bad_inode(inode))
1065 truncate_inode_pages(&inode->i_data, 0);
1066 end_writeback(inode);
1068 if (deleteme && obj) {
1070 yaffs_gross_lock(dev);
1072 yaffs_gross_unlock(dev);
1076 yaffs_gross_lock(dev);
1077 yaffs_unstitch_obj(inode, obj);
1078 yaffs_gross_unlock(dev);
1083 static void yaffs_touch_super(struct yaffs_dev *dev)
1085 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
1087 yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb);
1092 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1094 /* Lifted from jffs2 */
1096 struct yaffs_obj *obj;
1097 unsigned char *pg_buf;
1099 struct yaffs_dev *dev;
1100 loff_t pos = ((loff_t) pg->index) << PAGE_CACHE_SHIFT;
1102 yaffs_trace(YAFFS_TRACE_OS,
1103 "yaffs_readpage_nolock at %lld, size %08x",
1105 (unsigned)PAGE_CACHE_SIZE);
1108 obj = yaffs_dentry_to_obj(f->f_dentry);
1112 BUG_ON(!PageLocked(pg));
1115 /* FIXME: Can kmap fail? */
1117 yaffs_gross_lock(dev);
1119 ret = yaffs_file_rd(obj, pg_buf, pos, PAGE_CACHE_SIZE);
1121 yaffs_gross_unlock(dev);
1127 ClearPageUptodate(pg);
1130 SetPageUptodate(pg);
1134 flush_dcache_page(pg);
1137 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
1141 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1143 int ret = yaffs_readpage_nolock(f, pg);
1149 static int yaffs_readpage(struct file *f, struct page *pg)
1153 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
1154 ret = yaffs_readpage_unlock(f, pg);
1155 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
1159 /* writepage inspired by/stolen from smbfs */
1161 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1163 struct yaffs_dev *dev;
1164 struct address_space *mapping = page->mapping;
1165 struct inode *inode;
1166 unsigned long end_index;
1168 struct yaffs_obj *obj;
1175 inode = mapping->host;
1178 i_size = i_size_read(inode);
1180 end_index = i_size >> PAGE_CACHE_SHIFT;
1182 if (page->index < end_index)
1183 n_bytes = PAGE_CACHE_SIZE;
1185 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
1187 if (page->index > end_index || !n_bytes) {
1188 yaffs_trace(YAFFS_TRACE_OS,
1189 "yaffs_writepage at %08x, inode size = %08x!!!",
1190 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1191 (unsigned)inode->i_size);
1192 yaffs_trace(YAFFS_TRACE_OS,
1193 " -> don't care!!");
1195 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1196 set_page_writeback(page);
1198 end_page_writeback(page);
1203 if (n_bytes != PAGE_CACHE_SIZE)
1204 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
1208 buffer = kmap(page);
1210 obj = yaffs_inode_to_obj(inode);
1212 yaffs_gross_lock(dev);
1214 yaffs_trace(YAFFS_TRACE_OS,
1215 "yaffs_writepage at %08x, size %08x",
1216 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes);
1217 yaffs_trace(YAFFS_TRACE_OS,
1218 "writepag0: obj = %05x, ino = %05x",
1219 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1221 n_written = yaffs_wr_file(obj, buffer,
1222 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes, 0);
1224 yaffs_touch_super(dev);
1226 yaffs_trace(YAFFS_TRACE_OS,
1227 "writepag1: obj = %05x, ino = %05x",
1228 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1230 yaffs_gross_unlock(dev);
1233 set_page_writeback(page);
1235 end_page_writeback(page);
1238 return (n_written == n_bytes) ? 0 : -ENOSPC;
1241 /* Space holding and freeing is done to ensure we have space available for
1243 * For now we just assume few parallel writes and check against a small
1245 * Todo: need to do this with a counter to handle parallel reads better
1248 static ssize_t yaffs_hold_space(struct file *f)
1250 struct yaffs_obj *obj;
1251 struct yaffs_dev *dev;
1254 obj = yaffs_dentry_to_obj(f->f_dentry);
1258 yaffs_gross_lock(dev);
1260 n_free_chunks = yaffs_get_n_free_chunks(dev);
1262 yaffs_gross_unlock(dev);
1264 return (n_free_chunks > 20) ? 1 : 0;
1267 static void yaffs_release_space(struct file *f)
1269 struct yaffs_obj *obj;
1270 struct yaffs_dev *dev;
1272 obj = yaffs_dentry_to_obj(f->f_dentry);
1276 yaffs_gross_lock(dev);
1278 yaffs_gross_unlock(dev);
1281 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1282 loff_t pos, unsigned len, unsigned flags,
1283 struct page **pagep, void **fsdata)
1285 struct page *pg = NULL;
1286 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1291 pg = grab_cache_page_write_begin(mapping, index, flags);
1298 yaffs_trace(YAFFS_TRACE_OS,
1299 "start yaffs_write_begin index %d(%x) uptodate %d",
1300 (int)index, (int)index, PageUptodate(pg) ? 1 : 0);
1303 space_held = yaffs_hold_space(filp);
1310 /* Update page if required */
1312 if (!PageUptodate(pg))
1313 ret = yaffs_readpage_nolock(filp, pg);
1318 /* Happy path return */
1319 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
1324 yaffs_trace(YAFFS_TRACE_OS,
1325 "end yaffs_write_begin fail returning %d", ret);
1327 yaffs_release_space(filp);
1330 page_cache_release(pg);
1335 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1338 struct yaffs_obj *obj;
1339 int n_written, ipos;
1340 struct inode *inode;
1341 struct yaffs_dev *dev;
1343 obj = yaffs_dentry_to_obj(f->f_dentry);
1346 /* This should not happen */
1347 yaffs_trace(YAFFS_TRACE_OS,
1348 "yaffs_file_write: hey obj is null!");
1354 yaffs_gross_lock(dev);
1356 inode = f->f_dentry->d_inode;
1358 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1359 ipos = inode->i_size;
1363 yaffs_trace(YAFFS_TRACE_OS,
1364 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %d(%x)",
1365 (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos);
1367 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1369 yaffs_touch_super(dev);
1371 yaffs_trace(YAFFS_TRACE_OS,
1372 "yaffs_file_write: %d(%x) bytes written",
1373 (unsigned)n, (unsigned)n);
1375 if (n_written > 0) {
1378 if (ipos > inode->i_size) {
1379 inode->i_size = ipos;
1380 inode->i_blocks = (ipos + 511) >> 9;
1382 yaffs_trace(YAFFS_TRACE_OS,
1383 "yaffs_file_write size updated to %d bytes, %d blocks",
1384 ipos, (int)(inode->i_blocks));
1387 yaffs_gross_unlock(dev);
1388 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1391 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1392 loff_t pos, unsigned len, unsigned copied,
1393 struct page *pg, void *fsdadata)
1397 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1400 addr = kva + offset_into_page;
1402 yaffs_trace(YAFFS_TRACE_OS,
1403 "yaffs_write_end addr %p pos %x n_bytes %d",
1404 addr, (unsigned)pos, copied);
1406 ret = yaffs_file_write(filp, addr, copied, &pos);
1408 if (ret != copied) {
1409 yaffs_trace(YAFFS_TRACE_OS,
1410 "yaffs_write_end not same size ret %d copied %d",
1417 yaffs_release_space(filp);
1419 page_cache_release(pg);
1423 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1425 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1426 struct super_block *sb = dentry->d_sb;
1428 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
1430 yaffs_gross_lock(dev);
1432 buf->f_type = YAFFS_MAGIC;
1433 buf->f_bsize = sb->s_blocksize;
1434 buf->f_namelen = 255;
1436 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
1437 /* Do this if chunk size is not a power of 2 */
1439 uint64_t bytes_in_dev;
1440 uint64_t bytes_free;
1444 ((dev->param.end_block - dev->param.start_block + 1))) *
1445 ((uint64_t) (dev->param.chunks_per_block *
1446 dev->data_bytes_per_chunk));
1448 do_div(bytes_in_dev, sb->s_blocksize);
1449 /* bytes_in_dev becomes the number of blocks */
1450 buf->f_blocks = bytes_in_dev;
1452 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
1453 ((uint64_t) (dev->data_bytes_per_chunk));
1455 do_div(bytes_free, sb->s_blocksize);
1457 buf->f_bfree = bytes_free;
1459 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
1462 (dev->param.end_block - dev->param.start_block + 1) *
1463 dev->param.chunks_per_block /
1464 (sb->s_blocksize / dev->data_bytes_per_chunk);
1466 yaffs_get_n_free_chunks(dev) /
1467 (sb->s_blocksize / dev->data_bytes_per_chunk);
1470 (dev->param.end_block - dev->param.start_block + 1) *
1471 dev->param.chunks_per_block *
1472 (dev->data_bytes_per_chunk / sb->s_blocksize);
1475 yaffs_get_n_free_chunks(dev) *
1476 (dev->data_bytes_per_chunk / sb->s_blocksize);
1481 buf->f_bavail = buf->f_bfree;
1483 yaffs_gross_unlock(dev);
1487 static void yaffs_flush_inodes(struct super_block *sb)
1490 struct yaffs_obj *obj;
1492 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
1493 obj = yaffs_inode_to_obj(iptr);
1495 yaffs_trace(YAFFS_TRACE_OS,
1496 "flushing obj %d", obj->obj_id);
1497 yaffs_flush_file(obj, 1, 0);
1502 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
1504 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1509 yaffs_flush_inodes(sb);
1510 yaffs_update_dirty_dirs(dev);
1511 yaffs_flush_whole_cache(dev);
1513 yaffs_checkpoint_save(dev);
1516 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
1518 unsigned erased_chunks =
1519 dev->n_erased_blocks * dev->param.chunks_per_block;
1520 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1521 unsigned scattered = 0; /* Free chunks not in an erased block */
1523 if (erased_chunks < dev->n_free_chunks)
1524 scattered = (dev->n_free_chunks - erased_chunks);
1526 if (!context->bg_running)
1528 else if (scattered < (dev->param.chunks_per_block * 2))
1530 else if (erased_chunks > dev->n_free_chunks / 2)
1532 else if (erased_chunks > dev->n_free_chunks / 4)
1538 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
1541 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1542 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
1543 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
1546 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1547 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
1549 sb->s_dirt ? "dirty" : "clean",
1550 request_checkpoint ? "checkpoint requested" : "no checkpoint",
1551 oneshot_checkpoint ? " one-shot" : "");
1553 yaffs_gross_lock(dev);
1554 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
1555 oneshot_checkpoint) && !dev->is_checkpointed;
1557 if (sb->s_dirt || do_checkpoint) {
1558 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
1560 if (oneshot_checkpoint)
1561 yaffs_auto_checkpoint &= ~4;
1563 yaffs_gross_unlock(dev);
1569 * yaffs background thread functions .
1570 * yaffs_bg_thread_fn() the thread function
1571 * yaffs_bg_start() launches the background thread.
1572 * yaffs_bg_stop() cleans up the background thread.
1575 * The thread should only run after the yaffs is initialised
1576 * The thread should be stopped before yaffs is unmounted.
1577 * The thread should not do any writing while the fs is in read only.
1580 void yaffs_background_waker(unsigned long data)
1582 wake_up_process((struct task_struct *)data);
1585 static int yaffs_bg_thread_fn(void *data)
1587 struct yaffs_dev *dev = (struct yaffs_dev *)data;
1588 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1589 unsigned long now = jiffies;
1590 unsigned long next_dir_update = now;
1591 unsigned long next_gc = now;
1592 unsigned long expires;
1593 unsigned int urgency;
1595 struct timer_list timer;
1597 yaffs_trace(YAFFS_TRACE_BACKGROUND,
1598 "yaffs_background starting for dev %p", (void *)dev);
1601 while (context->bg_running) {
1602 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
1604 if (kthread_should_stop())
1607 if (try_to_freeze())
1610 yaffs_gross_lock(dev);
1614 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
1615 yaffs_update_dirty_dirs(dev);
1616 next_dir_update = now + HZ;
1619 if (time_after(now, next_gc) && yaffs_bg_enable) {
1620 if (!dev->is_checkpointed) {
1621 urgency = yaffs_bg_gc_urgency(dev);
1622 gc_result = yaffs_bg_gc(dev, urgency);
1624 next_gc = now + HZ / 20 + 1;
1625 else if (urgency > 0)
1626 next_gc = now + HZ / 10 + 1;
1628 next_gc = now + HZ * 2;
1631 * gc not running so set to next_dir_update
1632 * to cut down on wake ups
1634 next_gc = next_dir_update;
1637 yaffs_gross_unlock(dev);
1638 expires = next_dir_update;
1639 if (time_before(next_gc, expires))
1641 if (time_before(expires, now))
1644 init_timer_on_stack(&timer);
1645 timer.expires = expires + 1;
1646 timer.data = (unsigned long)current;
1647 timer.function = yaffs_background_waker;
1649 set_current_state(TASK_INTERRUPTIBLE);
1652 del_timer_sync(&timer);
1658 static int yaffs_bg_start(struct yaffs_dev *dev)
1661 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1666 context->bg_running = 1;
1668 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
1669 (void *)dev, "yaffs-bg-%d",
1672 if (IS_ERR(context->bg_thread)) {
1673 retval = PTR_ERR(context->bg_thread);
1674 context->bg_thread = NULL;
1675 context->bg_running = 0;
1680 static void yaffs_bg_stop(struct yaffs_dev *dev)
1682 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
1684 ctxt->bg_running = 0;
1686 if (ctxt->bg_thread) {
1687 kthread_stop(ctxt->bg_thread);
1688 ctxt->bg_thread = NULL;
1692 static void yaffs_write_super(struct super_block *sb)
1694 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
1696 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1697 "yaffs_write_super%s",
1698 request_checkpoint ? " checkpt" : "");
1700 yaffs_do_sync_fs(sb, request_checkpoint);
1704 static int yaffs_sync_fs(struct super_block *sb, int wait)
1706 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
1708 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1709 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
1711 yaffs_do_sync_fs(sb, request_checkpoint);
1716 static LIST_HEAD(yaffs_context_list);
1717 struct mutex yaffs_context_lock;
1719 struct yaffs_options {
1721 int skip_checkpoint_read;
1722 int skip_checkpoint_write;
1725 int tags_ecc_overridden;
1726 int lazy_loading_enabled;
1727 int lazy_loading_overridden;
1728 int empty_lost_and_found;
1729 int empty_lost_and_found_overridden;
1732 #define MAX_OPT_LEN 30
1733 static int yaffs_parse_options(struct yaffs_options *options,
1734 const char *options_str)
1736 char cur_opt[MAX_OPT_LEN + 1];
1740 /* Parse through the options which is a comma seperated list */
1742 while (options_str && *options_str && !error) {
1743 memset(cur_opt, 0, MAX_OPT_LEN + 1);
1746 while (*options_str == ',')
1749 while (*options_str && *options_str != ',') {
1750 if (p < MAX_OPT_LEN) {
1751 cur_opt[p] = *options_str;
1757 if (!strcmp(cur_opt, "inband-tags")) {
1758 options->inband_tags = 1;
1759 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
1760 options->tags_ecc_on = 0;
1761 options->tags_ecc_overridden = 1;
1762 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
1763 options->tags_ecc_on = 1;
1764 options->tags_ecc_overridden = 1;
1765 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
1766 options->lazy_loading_enabled = 0;
1767 options->lazy_loading_overridden = 1;
1768 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
1769 options->lazy_loading_enabled = 1;
1770 options->lazy_loading_overridden = 1;
1771 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
1772 options->empty_lost_and_found = 0;
1773 options->empty_lost_and_found_overridden = 1;
1774 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
1775 options->empty_lost_and_found = 1;
1776 options->empty_lost_and_found_overridden = 1;
1777 } else if (!strcmp(cur_opt, "no-cache")) {
1778 options->no_cache = 1;
1779 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
1780 options->skip_checkpoint_read = 1;
1781 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
1782 options->skip_checkpoint_write = 1;
1783 } else if (!strcmp(cur_opt, "no-checkpoint")) {
1784 options->skip_checkpoint_read = 1;
1785 options->skip_checkpoint_write = 1;
1787 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
1795 static const struct address_space_operations yaffs_file_address_operations = {
1796 .readpage = yaffs_readpage,
1797 .writepage = yaffs_writepage,
1798 .write_begin = yaffs_write_begin,
1799 .write_end = yaffs_write_end,
1804 static const struct inode_operations yaffs_file_inode_operations = {
1805 .setattr = yaffs_setattr,
1806 .setxattr = yaffs_setxattr,
1807 .getxattr = yaffs_getxattr,
1808 .listxattr = yaffs_listxattr,
1809 .removexattr = yaffs_removexattr,
1812 static const struct inode_operations yaffs_symlink_inode_operations = {
1813 .readlink = yaffs_readlink,
1814 .follow_link = yaffs_follow_link,
1815 .put_link = yaffs_put_link,
1816 .setattr = yaffs_setattr,
1817 .setxattr = yaffs_setxattr,
1818 .getxattr = yaffs_getxattr,
1819 .listxattr = yaffs_listxattr,
1820 .removexattr = yaffs_removexattr,
1823 static void yaffs_fill_inode_from_obj(struct inode *inode,
1824 struct yaffs_obj *obj)
1828 if (!inode || !obj) {
1829 yaffs_trace(YAFFS_TRACE_OS,
1830 "yaffs_fill_inode invalid parameters");
1834 /* Check mode against the variant type
1835 * and attempt to repair if broken. */
1836 mode = obj->yst_mode;
1838 switch (obj->variant_type) {
1839 case YAFFS_OBJECT_TYPE_FILE:
1840 if (!S_ISREG(mode)) {
1841 obj->yst_mode &= ~S_IFMT;
1842 obj->yst_mode |= S_IFREG;
1845 case YAFFS_OBJECT_TYPE_SYMLINK:
1846 if (!S_ISLNK(mode)) {
1847 obj->yst_mode &= ~S_IFMT;
1848 obj->yst_mode |= S_IFLNK;
1851 case YAFFS_OBJECT_TYPE_DIRECTORY:
1852 if (!S_ISDIR(mode)) {
1853 obj->yst_mode &= ~S_IFMT;
1854 obj->yst_mode |= S_IFDIR;
1857 case YAFFS_OBJECT_TYPE_UNKNOWN:
1858 case YAFFS_OBJECT_TYPE_HARDLINK:
1859 case YAFFS_OBJECT_TYPE_SPECIAL:
1865 inode->i_flags |= S_NOATIME;
1866 inode->i_ino = obj->obj_id;
1867 inode->i_mode = obj->yst_mode;
1868 inode->i_uid = obj->yst_uid;
1869 inode->i_gid = obj->yst_gid;
1871 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1873 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1874 inode->i_atime.tv_nsec = 0;
1875 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1876 inode->i_mtime.tv_nsec = 0;
1877 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1878 inode->i_ctime.tv_nsec = 0;
1879 inode->i_size = yaffs_get_obj_length(obj);
1880 inode->i_blocks = (inode->i_size + 511) >> 9;
1881 set_nlink(inode, yaffs_get_obj_link_count(obj));
1882 yaffs_trace(YAFFS_TRACE_OS,
1883 "yaffs_fill_inode mode %x uid %d gid %d size %d count %d",
1884 inode->i_mode, inode->i_uid, inode->i_gid,
1885 (int)inode->i_size, atomic_read(&inode->i_count));
1887 switch (obj->yst_mode & S_IFMT) {
1888 default: /* fifo, device or socket */
1889 init_special_inode(inode, obj->yst_mode,
1890 old_decode_dev(obj->yst_rdev));
1892 case S_IFREG: /* file */
1893 inode->i_op = &yaffs_file_inode_operations;
1894 inode->i_fop = &yaffs_file_operations;
1895 inode->i_mapping->a_ops = &yaffs_file_address_operations;
1897 case S_IFDIR: /* directory */
1898 inode->i_op = &yaffs_dir_inode_operations;
1899 inode->i_fop = &yaffs_dir_operations;
1901 case S_IFLNK: /* symlink */
1902 inode->i_op = &yaffs_symlink_inode_operations;
1906 yaffs_inode_to_obj_lv(inode) = obj;
1907 obj->my_inode = inode;
1910 static void yaffs_put_super(struct super_block *sb)
1912 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1914 yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
1916 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1917 "Shutting down yaffs background thread");
1919 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1920 "yaffs background thread shut down");
1922 yaffs_gross_lock(dev);
1924 yaffs_flush_super(sb, 1);
1926 if (yaffs_dev_to_lc(dev)->put_super_fn)
1927 yaffs_dev_to_lc(dev)->put_super_fn(sb);
1929 yaffs_deinitialise(dev);
1931 yaffs_gross_unlock(dev);
1932 mutex_lock(&yaffs_context_lock);
1933 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
1934 mutex_unlock(&yaffs_context_lock);
1936 if (yaffs_dev_to_lc(dev)->spare_buffer) {
1937 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
1938 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
1944 static void yaffs_mtd_put_super(struct super_block *sb)
1946 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
1951 put_mtd_device(mtd);
1954 static const struct super_operations yaffs_super_ops = {
1955 .statfs = yaffs_statfs,
1956 .put_super = yaffs_put_super,
1957 .evict_inode = yaffs_evict_inode,
1958 .sync_fs = yaffs_sync_fs,
1959 .write_super = yaffs_write_super,
1962 static struct super_block *yaffs_internal_read_super(int yaffs_version,
1963 struct super_block *sb,
1964 void *data, int silent)
1967 struct inode *inode = NULL;
1968 struct dentry *root;
1969 struct yaffs_dev *dev = 0;
1970 char devname_buf[BDEVNAME_SIZE + 1];
1971 struct mtd_info *mtd;
1973 char *data_str = (char *)data;
1974 struct yaffs_linux_context *context = NULL;
1975 struct yaffs_param *param;
1977 struct yaffs_options options;
1980 struct yaffs_linux_context *context_iterator;
1981 struct list_head *l;
1984 printk(KERN_INFO "yaffs: sb is NULL\n");
1988 sb->s_magic = YAFFS_MAGIC;
1989 sb->s_op = &yaffs_super_ops;
1990 sb->s_flags |= MS_NOATIME;
1992 read_only = ((sb->s_flags & MS_RDONLY) != 0);
1994 sb->s_export_op = &yaffs_export_ops;
1997 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1998 else if (!yaffs_devname(sb, devname_buf))
1999 printk(KERN_INFO "yaffs: devname is NULL\n");
2001 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2003 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2008 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2010 memset(&options, 0, sizeof(options));
2012 if (yaffs_parse_options(&options, data_str))
2013 /* Option parsing failed */
2016 sb->s_blocksize = PAGE_CACHE_SIZE;
2017 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2019 yaffs_trace(YAFFS_TRACE_OS,
2020 "yaffs_read_super: Using yaffs%d", yaffs_version);
2021 yaffs_trace(YAFFS_TRACE_OS,
2022 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2024 yaffs_trace(YAFFS_TRACE_ALWAYS,
2025 "Attempting MTD mount of %u.%u,\"%s\"",
2026 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2027 yaffs_devname(sb, devname_buf));
2029 /* Check it's an mtd device..... */
2030 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2031 return NULL; /* This isn't an mtd device */
2033 /* Get the device */
2034 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2036 yaffs_trace(YAFFS_TRACE_ALWAYS,
2037 "MTD device #%u doesn't appear to exist",
2041 /* Check it's NAND */
2042 if (mtd->type != MTD_NANDFLASH) {
2043 yaffs_trace(YAFFS_TRACE_ALWAYS,
2044 "MTD device is not NAND it's type %d",
2049 yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase);
2050 yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read);
2051 yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write);
2052 yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob);
2053 yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob);
2054 yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad);
2055 yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad);
2056 yaffs_trace(YAFFS_TRACE_OS, " writesize %d", mtd->writesize);
2057 yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
2058 yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
2059 yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
2061 if (yaffs_auto_select && yaffs_version == 1 && mtd->writesize >= 2048) {
2062 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2066 if (yaffs_auto_select && yaffs_version == 2 && !options.inband_tags &&
2067 mtd->writesize == 512) {
2068 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2072 if (yaffs_version == 2) {
2073 /* Check for version 2 style functions */
2075 !mtd->block_isbad ||
2076 !mtd->block_markbad ||
2078 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2079 yaffs_trace(YAFFS_TRACE_ALWAYS,
2080 "MTD device does not support required functions");
2084 if ((mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2085 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2086 !options.inband_tags) {
2087 yaffs_trace(YAFFS_TRACE_ALWAYS,
2088 "MTD device does not have the right page sizes");
2092 /* Check for V1 style functions */
2095 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2096 yaffs_trace(YAFFS_TRACE_ALWAYS,
2097 "MTD device does not support required functions");
2101 if (mtd->writesize < YAFFS_BYTES_PER_CHUNK ||
2102 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2103 yaffs_trace(YAFFS_TRACE_ALWAYS,
2104 "MTD device does not support have the right page sizes");
2109 /* OK, so if we got here, we have an MTD that's NAND and looks
2110 * like it has the right capabilities
2111 * Set the struct yaffs_dev up for mtd
2114 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2117 "yaffs: mtd is read only, setting superblock read only");
2118 sb->s_flags |= MS_RDONLY;
2121 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2122 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2124 if (!dev || !context) {
2132 /* Deep shit could not allocate device structure */
2133 yaffs_trace(YAFFS_TRACE_ALWAYS,
2134 "yaffs_read_super failed trying to allocate yaffs_dev");
2137 memset(dev, 0, sizeof(struct yaffs_dev));
2138 param = &(dev->param);
2140 memset(context, 0, sizeof(struct yaffs_linux_context));
2141 dev->os_context = context;
2142 INIT_LIST_HEAD(&(context->context_list));
2144 context->super = sb;
2146 dev->read_only = read_only;
2148 sb->s_fs_info = dev;
2150 dev->driver_context = mtd;
2151 param->name = mtd->name;
2153 /* Set up the memory size parameters.... */
2156 YCALCBLOCKS(mtd->size,
2157 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2159 param->start_block = 0;
2160 param->end_block = n_blocks - 1;
2161 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2162 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2163 param->n_reserved_blocks = 5;
2164 param->n_caches = (options.no_cache) ? 0 : 10;
2165 param->inband_tags = options.inband_tags;
2167 param->disable_lazy_load = 1;
2168 param->enable_xattr = 1;
2169 if (options.lazy_loading_overridden)
2170 param->disable_lazy_load = !options.lazy_loading_enabled;
2172 param->defered_dir_update = 1;
2174 if (options.tags_ecc_overridden)
2175 param->no_tags_ecc = !options.tags_ecc_on;
2177 param->empty_lost_n_found = 1;
2179 param->refresh_period = 500;
2181 if (options.empty_lost_and_found_overridden)
2182 param->empty_lost_n_found = options.empty_lost_and_found;
2184 /* ... and the functions. */
2185 if (yaffs_version == 2) {
2186 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2187 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2188 param->bad_block_fn = nandmtd2_mark_block_bad;
2189 param->query_block_fn = nandmtd2_query_block;
2190 yaffs_dev_to_lc(dev)->spare_buffer =
2191 kmalloc(mtd->oobsize, GFP_NOFS);
2192 param->is_yaffs2 = 1;
2193 param->total_bytes_per_chunk = mtd->writesize;
2194 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2195 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2197 param->start_block = 0;
2198 param->end_block = n_blocks - 1;
2200 /* use the MTD interface in yaffs_mtdif1.c */
2201 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2202 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2203 param->bad_block_fn = nandmtd1_mark_block_bad;
2204 param->query_block_fn = nandmtd1_query_block;
2205 param->is_yaffs2 = 0;
2207 /* ... and common functions */
2208 param->erase_fn = nandmtd_erase_block;
2209 param->initialise_flash_fn = nandmtd_initialise;
2211 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2213 param->sb_dirty_fn = yaffs_touch_super;
2214 param->gc_control = yaffs_gc_control_callback;
2216 yaffs_dev_to_lc(dev)->super = sb;
2218 param->use_nand_ecc = 1;
2220 param->skip_checkpt_rd = options.skip_checkpoint_read;
2221 param->skip_checkpt_wr = options.skip_checkpoint_write;
2223 mutex_lock(&yaffs_context_lock);
2224 /* Get a mount id */
2225 for (mount_id = 0, found = 0; !found; mount_id++) {
2227 list_for_each(l, &yaffs_context_list) {
2229 list_entry(l, struct yaffs_linux_context,
2231 if (context_iterator->mount_id == mount_id)
2235 context->mount_id = mount_id;
2237 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2238 &yaffs_context_list);
2239 mutex_unlock(&yaffs_context_lock);
2241 /* Directory search handling... */
2242 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2243 param->remove_obj_fn = yaffs_remove_obj_callback;
2245 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2247 yaffs_gross_lock(dev);
2249 err = yaffs_guts_initialise(dev);
2251 yaffs_trace(YAFFS_TRACE_OS,
2252 "yaffs_read_super: guts initialised %s",
2253 (err == YAFFS_OK) ? "OK" : "FAILED");
2255 if (err == YAFFS_OK)
2256 yaffs_bg_start(dev);
2258 if (!context->bg_thread)
2259 param->defered_dir_update = 0;
2261 sb->s_maxbytes = yaffs_max_file_size(dev);
2263 yaffs_trace(YAFFS_TRACE_OS,
2264 "yaffs_readpage_nolock at %lld, size %08x",
2266 (unsigned)PAGE_CACHE_SIZE);
2267 /* Release lock before yaffs_get_inode() */
2268 yaffs_gross_unlock(dev);
2270 /* Create root inode */
2271 if (err == YAFFS_OK)
2272 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2277 inode->i_op = &yaffs_dir_inode_operations;
2278 inode->i_fop = &yaffs_dir_operations;
2280 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2282 root = d_alloc_root(inode);
2284 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
2291 sb->s_dirt = !dev->is_checkpointed;
2292 yaffs_trace(YAFFS_TRACE_ALWAYS,
2293 "yaffs_read_super: is_checkpointed %d",
2294 dev->is_checkpointed);
2296 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
2300 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2303 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2306 static struct dentry *yaffs_mount(struct file_system_type *fs,
2307 int flags, const char *dev_name,
2310 return mount_bdev(fs, flags, dev_name, data,
2311 yaffs_internal_read_super_mtd);
2314 static struct file_system_type yaffs_fs_type = {
2315 .owner = THIS_MODULE,
2317 .mount = yaffs_mount,
2318 .kill_sb = kill_block_super,
2319 .fs_flags = FS_REQUIRES_DEV,
2322 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2325 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2328 static struct dentry *yaffs2_mount(struct file_system_type *fs,
2329 int flags, const char *dev_name, void *data)
2331 return mount_bdev(fs, flags, dev_name, data,
2332 yaffs2_internal_read_super_mtd);
2335 static struct file_system_type yaffs2_fs_type = {
2336 .owner = THIS_MODULE,
2338 .mount = yaffs2_mount,
2339 .kill_sb = kill_block_super,
2340 .fs_flags = FS_REQUIRES_DEV,
2344 static struct proc_dir_entry *my_proc_entry;
2346 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
2348 struct yaffs_param *param = &dev->param;
2350 buf += sprintf(buf, "start_block........... %d\n", param->start_block);
2351 buf += sprintf(buf, "end_block............. %d\n", param->end_block);
2352 buf += sprintf(buf, "total_bytes_per_chunk. %d\n",
2353 param->total_bytes_per_chunk);
2354 buf += sprintf(buf, "use_nand_ecc.......... %d\n",
2355 param->use_nand_ecc);
2356 buf += sprintf(buf, "no_tags_ecc........... %d\n", param->no_tags_ecc);
2357 buf += sprintf(buf, "is_yaffs2............. %d\n", param->is_yaffs2);
2358 buf += sprintf(buf, "inband_tags........... %d\n", param->inband_tags);
2359 buf += sprintf(buf, "empty_lost_n_found.... %d\n",
2360 param->empty_lost_n_found);
2361 buf += sprintf(buf, "disable_lazy_load..... %d\n",
2362 param->disable_lazy_load);
2363 buf += sprintf(buf, "refresh_period........ %d\n",
2364 param->refresh_period);
2365 buf += sprintf(buf, "n_caches.............. %d\n", param->n_caches);
2366 buf += sprintf(buf, "n_reserved_blocks..... %d\n",
2367 param->n_reserved_blocks);
2368 buf += sprintf(buf, "always_check_erased... %d\n",
2369 param->always_check_erased);
2374 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
2376 buf += sprintf(buf, "max file size......... %lld\n",
2377 (long long) yaffs_max_file_size(dev));
2378 buf += sprintf(buf, "data_bytes_per_chunk.. %d\n",
2379 dev->data_bytes_per_chunk);
2380 buf += sprintf(buf, "chunk_grp_bits........ %d\n", dev->chunk_grp_bits);
2381 buf += sprintf(buf, "chunk_grp_size........ %d\n", dev->chunk_grp_size);
2383 sprintf(buf, "n_erased_blocks....... %d\n", dev->n_erased_blocks);
2385 sprintf(buf, "blocks_in_checkpt..... %d\n", dev->blocks_in_checkpt);
2386 buf += sprintf(buf, "\n");
2387 buf += sprintf(buf, "n_tnodes.............. %d\n", dev->n_tnodes);
2388 buf += sprintf(buf, "n_obj................. %d\n", dev->n_obj);
2389 buf += sprintf(buf, "n_free_chunks......... %d\n", dev->n_free_chunks);
2390 buf += sprintf(buf, "\n");
2391 buf += sprintf(buf, "n_page_writes......... %u\n", dev->n_page_writes);
2392 buf += sprintf(buf, "n_page_reads.......... %u\n", dev->n_page_reads);
2393 buf += sprintf(buf, "n_erasures............ %u\n", dev->n_erasures);
2394 buf += sprintf(buf, "n_gc_copies........... %u\n", dev->n_gc_copies);
2395 buf += sprintf(buf, "all_gcs............... %u\n", dev->all_gcs);
2397 sprintf(buf, "passive_gc_count...... %u\n", dev->passive_gc_count);
2399 sprintf(buf, "oldest_dirty_gc_count. %u\n",
2400 dev->oldest_dirty_gc_count);
2401 buf += sprintf(buf, "n_gc_blocks........... %u\n", dev->n_gc_blocks);
2402 buf += sprintf(buf, "bg_gcs................ %u\n", dev->bg_gcs);
2404 sprintf(buf, "n_retried_writes...... %u\n", dev->n_retried_writes);
2406 sprintf(buf, "n_retired_blocks...... %u\n", dev->n_retired_blocks);
2407 buf += sprintf(buf, "n_ecc_fixed........... %u\n", dev->n_ecc_fixed);
2408 buf += sprintf(buf, "n_ecc_unfixed......... %u\n", dev->n_ecc_unfixed);
2410 sprintf(buf, "n_tags_ecc_fixed...... %u\n", dev->n_tags_ecc_fixed);
2412 sprintf(buf, "n_tags_ecc_unfixed.... %u\n",
2413 dev->n_tags_ecc_unfixed);
2414 buf += sprintf(buf, "cache_hits............ %u\n", dev->cache_hits);
2416 sprintf(buf, "n_deleted_files....... %u\n", dev->n_deleted_files);
2418 sprintf(buf, "n_unlinked_files...... %u\n", dev->n_unlinked_files);
2419 buf += sprintf(buf, "refresh_count......... %u\n", dev->refresh_count);
2420 buf += sprintf(buf, "n_bg_deletions........ %u\n", dev->n_bg_deletions);
2425 static int yaffs_proc_read(char *page,
2427 off_t offset, int count, int *eof, void *data)
2429 struct list_head *item;
2434 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2435 * We use 'offset' (*ppos) to indicate where we are in dev_list.
2436 * This also assumes the user has posted a read buffer large
2437 * enough to hold the complete output; but that's life in /proc.
2442 /* Print header first */
2444 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__ "\n");
2446 buf += sprintf(buf, "\n");
2450 mutex_lock(&yaffs_context_lock);
2452 /* Locate and print the Nth entry.
2453 * Order N-squared but N is small. */
2454 list_for_each(item, &yaffs_context_list) {
2455 struct yaffs_linux_context *dc =
2456 list_entry(item, struct yaffs_linux_context,
2458 struct yaffs_dev *dev = dc->dev;
2460 if (n < (step & ~1)) {
2464 if ((step & 1) == 0) {
2466 sprintf(buf, "\nDevice %d \"%s\"\n", n,
2468 buf = yaffs_dump_dev_part0(buf, dev);
2470 buf = yaffs_dump_dev_part1(buf, dev);
2475 mutex_unlock(&yaffs_context_lock);
2478 return buf - page < count ? buf - page : count;
2483 /* Stuff to handle installation of file systems */
2484 struct file_system_to_install {
2485 struct file_system_type *fst;
2489 static struct file_system_to_install fs_to_install[] = {
2490 {&yaffs_fs_type, 0},
2491 {&yaffs2_fs_type, 0},
2495 static int __init init_yaffs_fs(void)
2498 struct file_system_to_install *fsinst;
2500 yaffs_trace(YAFFS_TRACE_ALWAYS,
2501 "yaffs built " __DATE__ " " __TIME__ " Installing.");
2503 mutex_init(&yaffs_context_lock);
2505 /* Install the proc_fs entries */
2506 my_proc_entry = create_proc_entry("yaffs",
2507 S_IRUGO | S_IFREG, NULL);
2509 if (my_proc_entry) {
2510 my_proc_entry->write_proc = NULL;
2511 my_proc_entry->read_proc = yaffs_proc_read;
2512 my_proc_entry->data = NULL;
2518 /* Now add the file system entries */
2520 fsinst = fs_to_install;
2522 while (fsinst->fst && !error) {
2523 error = register_filesystem(fsinst->fst);
2525 fsinst->installed = 1;
2529 /* Any errors? uninstall */
2531 fsinst = fs_to_install;
2533 while (fsinst->fst) {
2534 if (fsinst->installed) {
2535 unregister_filesystem(fsinst->fst);
2536 fsinst->installed = 0;
2545 static void __exit exit_yaffs_fs(void)
2548 struct file_system_to_install *fsinst;
2550 yaffs_trace(YAFFS_TRACE_ALWAYS,
2551 "yaffs built " __DATE__ " " __TIME__ " removing.");
2553 remove_proc_entry("yaffs", NULL);
2555 fsinst = fs_to_install;
2557 while (fsinst->fst) {
2558 if (fsinst->installed) {
2559 unregister_filesystem(fsinst->fst);
2560 fsinst->installed = 0;
2566 module_init(init_yaffs_fs)
2567 module_exit(exit_yaffs_fs)
2569 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2570 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
2571 MODULE_LICENSE("GPL");