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/smp_lock.h>
46 #include <linux/pagemap.h>
47 #include <linux/mtd/mtd.h>
48 #include <linux/interrupt.h>
49 #include <linux/string.h>
50 #include <linux/ctype.h>
51 #include <linux/namei.h>
52 #include <linux/exportfs.h>
53 #include <linux/kthread.h>
54 #include <linux/delay.h>
55 #include <linux/freezer.h>
56 #include <asm/div64.h>
57 #include <linux/statfs.h>
58 #include <linux/uaccess.h>
59 #include <linux/mtd/mtd.h>
62 #include "yaffs_trace.h"
63 #include "yaffs_guts.h"
64 #include "yaffs_attribs.h"
65 #include "yaffs_linux.h"
66 #include "yaffs_mtdif.h"
67 #include "yaffs_mtdif1.h"
68 #include "yaffs_mtdif2.h"
70 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
71 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
72 unsigned int yaffs_auto_checkpoint = 1;
73 unsigned int yaffs_gc_control = 1;
74 unsigned int yaffs_bg_enable = 1;
75 unsigned int yaffs_auto_select = 1;
77 /* Module Parameters */
78 module_param(yaffs_trace_mask, uint, 0644);
79 module_param(yaffs_wr_attempts, uint, 0644);
80 module_param(yaffs_auto_checkpoint, uint, 0644);
81 module_param(yaffs_gc_control, uint, 0644);
82 module_param(yaffs_bg_enable, uint, 0644);
83 module_param(yaffs_auto_select, uint, 0644);
85 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
87 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
89 uint64_t result = partition_size;
91 do_div(result, block_size);
92 return (uint32_t) result;
95 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
96 #define yaffs_inode_to_obj(iptr)\
97 ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
98 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
99 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
101 #define update_dir_time(dir) do {\
102 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
106 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
108 return yaffs_gc_control;
111 static void yaffs_gross_lock(struct yaffs_dev *dev)
113 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
114 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
115 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
118 static void yaffs_gross_unlock(struct yaffs_dev *dev)
120 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
121 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
124 static void yaffs_fill_inode_from_obj(struct inode *inode,
125 struct yaffs_obj *obj);
127 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
130 struct yaffs_obj *obj;
131 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
133 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
135 inode = iget_locked(sb, ino);
137 return ERR_PTR(-ENOMEM);
138 if (!(inode->i_state & I_NEW))
141 /* NB This is called as a side effect of other functions, but
142 * we had to release the lock to prevent deadlocks, so
143 * need to lock again.
146 yaffs_gross_lock(dev);
148 obj = yaffs_find_by_number(dev, inode->i_ino);
149 yaffs_fill_inode_from_obj(inode, obj);
151 yaffs_gross_unlock(dev);
153 unlock_new_inode(inode);
157 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
158 struct yaffs_obj *obj)
163 yaffs_trace(YAFFS_TRACE_OS,
164 "yaffs_get_inode for NULL super_block!!");
170 yaffs_trace(YAFFS_TRACE_OS,
171 "yaffs_get_inode for NULL object!!");
176 yaffs_trace(YAFFS_TRACE_OS,
177 "yaffs_get_inode for object %d",
180 inode = yaffs_iget(sb, obj->obj_id);
184 /* NB Side effect: iget calls back to yaffs_read_inode(). */
185 /* iget also increments the inode's i_count */
186 /* NB You can't be holding gross_lock or deadlock will happen! */
191 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
195 struct yaffs_obj *obj = NULL;
196 struct yaffs_dev *dev;
197 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
199 uid_t uid = current->cred->fsuid;
201 (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
203 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
207 yaffs_trace(YAFFS_TRACE_OS,
208 "yaffs_mknod: could not get parent object");
212 yaffs_trace(YAFFS_TRACE_OS,
213 "yaffs_mknod: parent object %d type %d",
214 parent->obj_id, parent->variant_type);
216 yaffs_trace(YAFFS_TRACE_OS,
217 "yaffs_mknod: making oject for %s, mode %x dev %x",
218 dentry->d_name.name, mode, rdev);
220 dev = parent->my_dev;
222 yaffs_gross_lock(dev);
224 if (yaffs_get_n_free_chunks(dev) < 1) {
229 switch (mode & S_IFMT) {
231 /* Special (socket, fifo, device...) */
232 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
234 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
235 gid, old_encode_dev(rdev));
237 case S_IFREG: /* file */
238 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
239 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
242 case S_IFDIR: /* directory */
243 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
244 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
247 case S_IFLNK: /* symlink */
248 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
249 obj = NULL; /* Do we ever get here? */
258 /* Can not call yaffs_get_inode() with gross lock held */
259 yaffs_gross_unlock(dev);
262 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
263 d_instantiate(dentry, inode);
264 update_dir_time(dir);
265 yaffs_trace(YAFFS_TRACE_OS,
266 "yaffs_mknod created object %d count = %d",
267 obj->obj_id, atomic_read(&inode->i_count));
268 yaffs_fill_inode_from_obj(dir, parent);
272 yaffs_gross_unlock(dev);
273 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod error %d", error);
278 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
280 return yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
283 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
286 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
289 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
290 struct dentry *dentry)
292 struct inode *inode = old_dentry->d_inode;
293 struct yaffs_obj *obj = NULL;
294 struct yaffs_obj *link = NULL;
295 struct yaffs_dev *dev;
297 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
299 obj = yaffs_inode_to_obj(inode);
302 yaffs_gross_lock(dev);
304 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
306 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
310 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
311 d_instantiate(dentry, old_dentry->d_inode);
312 atomic_inc(&old_dentry->d_inode->i_count);
313 yaffs_trace(YAFFS_TRACE_OS,
314 "yaffs_link link count %d i_count %d",
315 old_dentry->d_inode->i_nlink,
316 atomic_read(&old_dentry->d_inode->i_count));
319 yaffs_gross_unlock(dev);
322 update_dir_time(dir);
329 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
332 struct yaffs_obj *obj;
333 struct yaffs_dev *dev;
335 uid_t uid = current->cred->fsuid;
337 (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid;
339 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
341 dev = yaffs_inode_to_obj(dir)->my_dev;
342 yaffs_gross_lock(dev);
343 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
344 S_IFLNK | S_IRWXUGO, uid, gid, symname);
345 yaffs_gross_unlock(dev);
348 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
352 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
353 d_instantiate(dentry, inode);
354 update_dir_time(dir);
355 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
360 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
363 struct yaffs_obj *obj;
364 struct inode *inode = NULL;
365 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
367 if (current != yaffs_dev_to_lc(dev)->readdir_process)
368 yaffs_gross_lock(dev);
370 yaffs_trace(YAFFS_TRACE_OS,
371 "yaffs_lookup for %d:%s",
372 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
374 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
376 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
378 /* Can't hold gross lock when calling yaffs_get_inode() */
379 if (current != yaffs_dev_to_lc(dev)->readdir_process)
380 yaffs_gross_unlock(dev);
383 yaffs_trace(YAFFS_TRACE_OS,
384 "yaffs_lookup found %d", obj->obj_id);
386 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
389 yaffs_trace(YAFFS_TRACE_OS, "yaffs_loookup dentry");
390 d_add(dentry, inode);
396 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
400 d_add(dentry, inode);
405 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
408 struct yaffs_dev *dev;
409 struct yaffs_obj *obj;
411 yaffs_trace(YAFFS_TRACE_OS,
412 "yaffs_unlink %d:%s",
413 (int)(dir->i_ino), dentry->d_name.name);
414 obj = yaffs_inode_to_obj(dir);
417 yaffs_gross_lock(dev);
419 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
421 if (ret_val == YAFFS_OK) {
422 dentry->d_inode->i_nlink--;
424 yaffs_gross_unlock(dev);
425 mark_inode_dirty(dentry->d_inode);
426 update_dir_time(dir);
429 yaffs_gross_unlock(dev);
433 static int yaffs_sync_object(struct file *file, int datasync)
436 struct yaffs_obj *obj;
437 struct yaffs_dev *dev;
438 struct dentry *dentry = file->f_path.dentry;
440 obj = yaffs_dentry_to_obj(dentry);
444 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, "yaffs_sync_object");
445 yaffs_gross_lock(dev);
446 yaffs_flush_file(obj, 1, datasync);
447 yaffs_gross_unlock(dev);
451 * The VFS layer already does all the dentry stuff for rename.
453 * NB: POSIX says you can rename an object over an old object of the same name
455 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
456 struct inode *new_dir, struct dentry *new_dentry)
458 struct yaffs_dev *dev;
459 int ret_val = YAFFS_FAIL;
460 struct yaffs_obj *target;
462 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
463 dev = yaffs_inode_to_obj(old_dir)->my_dev;
465 yaffs_gross_lock(dev);
467 /* Check if the target is an existing directory that is not empty. */
468 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
469 new_dentry->d_name.name);
471 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
472 !list_empty(&target->variant.dir_variant.children)) {
474 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
476 ret_val = YAFFS_FAIL;
478 /* Now does unlinking internally using shadowing mechanism */
479 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
481 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
482 old_dentry->d_name.name,
483 yaffs_inode_to_obj(new_dir),
484 new_dentry->d_name.name);
486 yaffs_gross_unlock(dev);
488 if (ret_val == YAFFS_OK) {
490 new_dentry->d_inode->i_nlink--;
491 mark_inode_dirty(new_dentry->d_inode);
494 update_dir_time(old_dir);
495 if (old_dir != new_dir)
496 update_dir_time(new_dir);
503 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
505 struct inode *inode = dentry->d_inode;
507 struct yaffs_dev *dev;
510 yaffs_trace(YAFFS_TRACE_OS,
511 "yaffs_setattr of object %d",
512 yaffs_inode_to_obj(inode)->obj_id);
514 /* Fail if a requested resize >= 2GB */
515 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
519 error = inode_change_ok(inode, attr);
522 setattr_copy(inode, attr);
523 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
524 if (attr->ia_valid & ATTR_SIZE) {
525 truncate_setsize(inode, attr->ia_size);
526 inode->i_blocks = (inode->i_size + 511) >> 9;
528 dev = yaffs_inode_to_obj(inode)->my_dev;
529 if (attr->ia_valid & ATTR_SIZE) {
530 yaffs_trace(YAFFS_TRACE_OS, "resize to %d(%x)",
531 (int)(attr->ia_size),
532 (int)(attr->ia_size));
535 yaffs_gross_lock(dev);
536 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
537 if (result != YAFFS_OK)
539 yaffs_gross_unlock(dev);
542 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
547 static int yaffs_setxattr(struct dentry *dentry, const char *name,
548 const void *value, size_t size, int flags)
550 struct inode *inode = dentry->d_inode;
552 struct yaffs_dev *dev;
553 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
555 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
558 yaffs_gross_lock(dev);
559 error = yaffs_set_xattrib(obj, name, value, size, flags);
560 yaffs_gross_unlock(dev);
562 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
567 static ssize_t yaffs_getxattr(struct dentry *dentry, const char *name,
568 void *buff, size_t size)
570 struct inode *inode = dentry->d_inode;
572 struct yaffs_dev *dev;
573 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
575 yaffs_trace(YAFFS_TRACE_OS,
576 "yaffs_getxattr \"%s\" from object %d",
580 yaffs_gross_lock(dev);
581 error = yaffs_get_xattrib(obj, name, buff, size);
582 yaffs_gross_unlock(dev);
584 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
589 static int yaffs_removexattr(struct dentry *dentry, const char *name)
591 struct inode *inode = dentry->d_inode;
593 struct yaffs_dev *dev;
594 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
596 yaffs_trace(YAFFS_TRACE_OS,
597 "yaffs_removexattr of object %d", obj->obj_id);
600 yaffs_gross_lock(dev);
601 error = yaffs_remove_xattrib(obj, name);
602 yaffs_gross_unlock(dev);
604 yaffs_trace(YAFFS_TRACE_OS,
605 "yaffs_removexattr done returning %d", error);
610 static ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
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_listxattr of object %d", obj->obj_id);
621 yaffs_gross_lock(dev);
622 error = yaffs_list_xattrib(obj, buff, size);
623 yaffs_gross_unlock(dev);
625 yaffs_trace(YAFFS_TRACE_OS,
626 "yaffs_listxattr done returning %d", error);
631 static const struct inode_operations yaffs_dir_inode_operations = {
632 .create = yaffs_create,
633 .lookup = yaffs_lookup,
635 .unlink = yaffs_unlink,
636 .symlink = yaffs_symlink,
637 .mkdir = yaffs_mkdir,
638 .rmdir = yaffs_unlink,
639 .mknod = yaffs_mknod,
640 .rename = yaffs_rename,
641 .setattr = yaffs_setattr,
642 .setxattr = yaffs_setxattr,
643 .getxattr = yaffs_getxattr,
644 .listxattr = yaffs_listxattr,
645 .removexattr = yaffs_removexattr,
647 /*-----------------------------------------------------------------*/
648 /* Directory search context allows us to unlock access to yaffs during
649 * filldir without causing problems with the directory being modified.
650 * This is similar to the tried and tested mechanism used in yaffs direct.
652 * A search context iterates along a doubly linked list of siblings in the
653 * directory. If the iterating object is deleted then this would corrupt
654 * the list iteration, likely causing a crash. The search context avoids
655 * this by using the remove_obj_fn to move the search context to the
656 * next object before the object is deleted.
658 * Many readdirs (and thus seach conexts) may be alive simulateously so
659 * each struct yaffs_dev has a list of these.
661 * A seach context lives for the duration of a readdir.
663 * All these functions must be called while yaffs is locked.
666 struct yaffs_search_context {
667 struct yaffs_dev *dev;
668 struct yaffs_obj *dir_obj;
669 struct yaffs_obj *next_return;
670 struct list_head others;
674 * yaffs_new_search() creates a new search context, initialises it and
675 * adds it to the device's search context list.
677 * Called at start of readdir.
679 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
681 struct yaffs_dev *dev = dir->my_dev;
682 struct yaffs_search_context *sc =
683 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
690 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
691 sc->next_return = NULL;
694 list_entry(dir->variant.dir_variant.children.next,
695 struct yaffs_obj, siblings);
696 INIT_LIST_HEAD(&sc->others);
697 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
703 * yaffs_search_end() disposes of a search context and cleans up.
705 static void yaffs_search_end(struct yaffs_search_context *sc)
708 list_del(&sc->others);
714 * yaffs_search_advance() moves a search context to the next object.
715 * Called when the search iterates or when an object removal causes
716 * the search context to be moved to the next object.
718 static void yaffs_search_advance(struct yaffs_search_context *sc)
723 if (sc->next_return == NULL ||
724 list_empty(&sc->dir_obj->variant.dir_variant.children))
725 sc->next_return = NULL;
727 struct list_head *next = sc->next_return->siblings.next;
729 if (next == &sc->dir_obj->variant.dir_variant.children)
730 sc->next_return = NULL; /* end of list */
733 list_entry(next, struct yaffs_obj, siblings);
738 * yaffs_remove_obj_callback() is called when an object is unlinked.
739 * We check open search contexts and advance any which are currently
740 * on the object being iterated.
742 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
746 struct yaffs_search_context *sc;
747 struct list_head *search_contexts =
748 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
750 /* Iterate through the directory search contexts.
751 * If any are currently on the object being removed, then advance
752 * the search context to the next object to prevent a hanging pointer.
754 list_for_each(i, search_contexts) {
755 sc = list_entry(i, struct yaffs_search_context, others);
756 if (sc->next_return == obj)
757 yaffs_search_advance(sc);
762 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
764 struct yaffs_obj *obj;
765 struct yaffs_dev *dev;
766 struct yaffs_search_context *sc;
767 struct inode *inode = f->f_dentry->d_inode;
768 unsigned long offset, curoffs;
771 char name[YAFFS_MAX_NAME_LENGTH + 1];
773 obj = yaffs_dentry_to_obj(f->f_dentry);
776 yaffs_gross_lock(dev);
778 yaffs_dev_to_lc(dev)->readdir_process = current;
782 sc = yaffs_new_search(obj);
788 yaffs_trace(YAFFS_TRACE_OS,
789 "yaffs_readdir: starting at %d", (int)offset);
792 yaffs_trace(YAFFS_TRACE_OS,
793 "yaffs_readdir: entry . ino %d",
795 yaffs_gross_unlock(dev);
796 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
797 yaffs_gross_lock(dev);
800 yaffs_gross_lock(dev);
805 yaffs_trace(YAFFS_TRACE_OS,
806 "yaffs_readdir: entry .. ino %d",
807 (int)f->f_dentry->d_parent->d_inode->i_ino);
808 yaffs_gross_unlock(dev);
809 if (filldir(dirent, "..", 2, offset,
810 f->f_dentry->d_parent->d_inode->i_ino,
812 yaffs_gross_lock(dev);
815 yaffs_gross_lock(dev);
822 /* If the directory has changed since the open or last call to
823 readdir, rewind to after the 2 canned entries. */
824 if (f->f_version != inode->i_version) {
827 f->f_version = inode->i_version;
830 while (sc->next_return) {
833 if (curoffs >= offset) {
834 int this_inode = yaffs_get_obj_inode(l);
835 int this_type = yaffs_get_obj_type(l);
837 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
838 yaffs_trace(YAFFS_TRACE_OS,
839 "yaffs_readdir: %s inode %d",
840 name, yaffs_get_obj_inode(l));
842 yaffs_gross_unlock(dev);
847 offset, this_inode, this_type) < 0) {
848 yaffs_gross_lock(dev);
852 yaffs_gross_lock(dev);
857 yaffs_search_advance(sc);
861 yaffs_search_end(sc);
862 yaffs_dev_to_lc(dev)->readdir_process = NULL;
863 yaffs_gross_unlock(dev);
868 static const struct file_operations yaffs_dir_operations = {
869 .read = generic_read_dir,
870 .readdir = yaffs_readdir,
871 .fsync = yaffs_sync_object,
872 .llseek = generic_file_llseek,
877 static int yaffs_file_flush(struct file *file, fl_owner_t id)
879 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
880 struct yaffs_dev *dev = obj->my_dev;
882 yaffs_trace(YAFFS_TRACE_OS,
883 "yaffs_file_flush object %d (%s)",
884 obj->obj_id, obj->dirty ? "dirty" : "clean");
886 yaffs_gross_lock(dev);
888 yaffs_flush_file(obj, 1, 0);
890 yaffs_gross_unlock(dev);
895 static const struct file_operations yaffs_file_operations = {
896 .read = do_sync_read,
897 .write = do_sync_write,
898 .aio_read = generic_file_aio_read,
899 .aio_write = generic_file_aio_write,
900 .mmap = generic_file_mmap,
901 .flush = yaffs_file_flush,
902 .fsync = yaffs_sync_object,
903 .splice_read = generic_file_splice_read,
904 .splice_write = generic_file_splice_write,
905 .llseek = generic_file_llseek,
909 /* ExportFS support */
910 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
913 return yaffs_iget(sb, ino);
916 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
917 struct fid *fid, int fh_len,
920 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
921 yaffs2_nfs_get_inode);
924 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
925 struct fid *fid, int fh_len,
928 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
929 yaffs2_nfs_get_inode);
932 struct dentry *yaffs2_get_parent(struct dentry *dentry)
935 struct super_block *sb = dentry->d_inode->i_sb;
936 struct dentry *parent = ERR_PTR(-ENOENT);
938 unsigned long parent_ino;
939 struct yaffs_obj *d_obj;
940 struct yaffs_obj *parent_obj;
942 d_obj = yaffs_inode_to_obj(dentry->d_inode);
945 parent_obj = d_obj->parent;
947 parent_ino = yaffs_get_obj_inode(parent_obj);
948 inode = yaffs_iget(sb, parent_ino);
951 parent = ERR_CAST(inode);
953 parent = d_obtain_alias(inode);
954 if (!IS_ERR(parent)) {
955 parent = ERR_PTR(-ENOMEM);
964 /* Just declare a zero structure as a NULL value implies
965 * using the default functions of exportfs.
968 static struct export_operations yaffs_export_ops = {
969 .fh_to_dentry = yaffs2_fh_to_dentry,
970 .fh_to_parent = yaffs2_fh_to_parent,
971 .get_parent = yaffs2_get_parent,
975 /*-----------------------------------------------------------------*/
977 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
980 unsigned char *alias;
982 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
984 yaffs_gross_lock(dev);
986 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
988 yaffs_gross_unlock(dev);
993 ret = vfs_readlink(dentry, buffer, buflen, alias);
998 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1000 unsigned char *alias;
1002 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1004 yaffs_gross_lock(dev);
1006 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1007 yaffs_gross_unlock(dev);
1010 ret = ERR_PTR(-ENOMEM);
1014 nd_set_link(nd, alias);
1015 ret = (void *)alias;
1020 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
1026 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
1028 /* Clear the association between the inode and
1029 * the struct yaffs_obj.
1031 obj->my_inode = NULL;
1032 yaffs_inode_to_obj_lv(inode) = NULL;
1034 /* If the object freeing was deferred, then the real
1036 * This should fix the inode inconsistency problem.
1038 yaffs_handle_defered_free(obj);
1041 /* yaffs_evict_inode combines into one operation what was previously done in
1042 * yaffs_clear_inode() and yaffs_delete_inode()
1045 static void yaffs_evict_inode(struct inode *inode)
1047 struct yaffs_obj *obj;
1048 struct yaffs_dev *dev;
1051 obj = yaffs_inode_to_obj(inode);
1053 yaffs_trace(YAFFS_TRACE_OS,
1054 "yaffs_evict_inode: ino %d, count %d %s",
1056 atomic_read(&inode->i_count),
1057 obj ? "object exists" : "null object");
1059 if (!inode->i_nlink && !is_bad_inode(inode))
1061 truncate_inode_pages(&inode->i_data, 0);
1062 end_writeback(inode);
1064 if (deleteme && obj) {
1066 yaffs_gross_lock(dev);
1068 yaffs_gross_unlock(dev);
1072 yaffs_gross_lock(dev);
1073 yaffs_unstitch_obj(inode, obj);
1074 yaffs_gross_unlock(dev);
1079 static void yaffs_touch_super(struct yaffs_dev *dev)
1081 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
1083 yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb);
1088 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1090 /* Lifted from jffs2 */
1092 struct yaffs_obj *obj;
1093 unsigned char *pg_buf;
1095 struct yaffs_dev *dev;
1097 yaffs_trace(YAFFS_TRACE_OS,
1098 "yaffs_readpage_nolock at %08x, size %08x",
1099 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1100 (unsigned)PAGE_CACHE_SIZE);
1102 obj = yaffs_dentry_to_obj(f->f_dentry);
1106 BUG_ON(!PageLocked(pg));
1109 /* FIXME: Can kmap fail? */
1111 yaffs_gross_lock(dev);
1113 ret = yaffs_file_rd(obj, pg_buf,
1114 pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
1116 yaffs_gross_unlock(dev);
1122 ClearPageUptodate(pg);
1125 SetPageUptodate(pg);
1129 flush_dcache_page(pg);
1132 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
1136 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1138 int ret = yaffs_readpage_nolock(f, pg);
1144 static int yaffs_readpage(struct file *f, struct page *pg)
1148 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
1149 ret = yaffs_readpage_unlock(f, pg);
1150 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
1154 /* writepage inspired by/stolen from smbfs */
1156 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1158 struct yaffs_dev *dev;
1159 struct address_space *mapping = page->mapping;
1160 struct inode *inode;
1161 unsigned long end_index;
1163 struct yaffs_obj *obj;
1170 inode = mapping->host;
1173 i_size = i_size_read(inode);
1175 end_index = i_size >> PAGE_CACHE_SHIFT;
1177 if (page->index < end_index)
1178 n_bytes = PAGE_CACHE_SIZE;
1180 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
1182 if (page->index > end_index || !n_bytes) {
1183 yaffs_trace(YAFFS_TRACE_OS,
1184 "yaffs_writepage at %08x, inode size = %08x!!!",
1185 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1186 (unsigned)inode->i_size);
1187 yaffs_trace(YAFFS_TRACE_OS,
1188 " -> don't care!!");
1190 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1191 set_page_writeback(page);
1193 end_page_writeback(page);
1198 if (n_bytes != PAGE_CACHE_SIZE)
1199 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
1203 buffer = kmap(page);
1205 obj = yaffs_inode_to_obj(inode);
1207 yaffs_gross_lock(dev);
1209 yaffs_trace(YAFFS_TRACE_OS,
1210 "yaffs_writepage at %08x, size %08x",
1211 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes);
1212 yaffs_trace(YAFFS_TRACE_OS,
1213 "writepag0: obj = %05x, ino = %05x",
1214 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1216 n_written = yaffs_wr_file(obj, buffer,
1217 page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
1219 yaffs_touch_super(dev);
1221 yaffs_trace(YAFFS_TRACE_OS,
1222 "writepag1: obj = %05x, ino = %05x",
1223 (int)obj->variant.file_variant.file_size, (int)inode->i_size);
1225 yaffs_gross_unlock(dev);
1228 set_page_writeback(page);
1230 end_page_writeback(page);
1233 return (n_written == n_bytes) ? 0 : -ENOSPC;
1236 /* Space holding and freeing is done to ensure we have space available for
1238 * For now we just assume few parallel writes and check against a small
1240 * Todo: need to do this with a counter to handle parallel reads better
1243 static ssize_t yaffs_hold_space(struct file *f)
1245 struct yaffs_obj *obj;
1246 struct yaffs_dev *dev;
1249 obj = yaffs_dentry_to_obj(f->f_dentry);
1253 yaffs_gross_lock(dev);
1255 n_free_chunks = yaffs_get_n_free_chunks(dev);
1257 yaffs_gross_unlock(dev);
1259 return (n_free_chunks > 20) ? 1 : 0;
1262 static void yaffs_release_space(struct file *f)
1264 struct yaffs_obj *obj;
1265 struct yaffs_dev *dev;
1267 obj = yaffs_dentry_to_obj(f->f_dentry);
1271 yaffs_gross_lock(dev);
1273 yaffs_gross_unlock(dev);
1276 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1277 loff_t pos, unsigned len, unsigned flags,
1278 struct page **pagep, void **fsdata)
1280 struct page *pg = NULL;
1281 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1286 pg = grab_cache_page_write_begin(mapping, index, flags);
1293 yaffs_trace(YAFFS_TRACE_OS,
1294 "start yaffs_write_begin index %d(%x) uptodate %d",
1295 (int)index, (int)index, PageUptodate(pg) ? 1 : 0);
1298 space_held = yaffs_hold_space(filp);
1305 /* Update page if required */
1307 if (!PageUptodate(pg))
1308 ret = yaffs_readpage_nolock(filp, pg);
1313 /* Happy path return */
1314 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
1319 yaffs_trace(YAFFS_TRACE_OS,
1320 "end yaffs_write_begin fail returning %d", ret);
1322 yaffs_release_space(filp);
1325 page_cache_release(pg);
1330 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1333 struct yaffs_obj *obj;
1334 int n_written, ipos;
1335 struct inode *inode;
1336 struct yaffs_dev *dev;
1338 obj = yaffs_dentry_to_obj(f->f_dentry);
1341 /* This should not happen */
1342 yaffs_trace(YAFFS_TRACE_OS,
1343 "yaffs_file_write: hey obj is null!");
1349 yaffs_gross_lock(dev);
1351 inode = f->f_dentry->d_inode;
1353 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1354 ipos = inode->i_size;
1358 yaffs_trace(YAFFS_TRACE_OS,
1359 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %d(%x)",
1360 (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos);
1362 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1364 yaffs_touch_super(dev);
1366 yaffs_trace(YAFFS_TRACE_OS,
1367 "yaffs_file_write: %d(%x) bytes written",
1368 (unsigned)n, (unsigned)n);
1370 if (n_written > 0) {
1373 if (ipos > inode->i_size) {
1374 inode->i_size = ipos;
1375 inode->i_blocks = (ipos + 511) >> 9;
1377 yaffs_trace(YAFFS_TRACE_OS,
1378 "yaffs_file_write size updated to %d bytes, %d blocks",
1379 ipos, (int)(inode->i_blocks));
1382 yaffs_gross_unlock(dev);
1383 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1386 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1387 loff_t pos, unsigned len, unsigned copied,
1388 struct page *pg, void *fsdadata)
1392 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1395 addr = kva + offset_into_page;
1397 yaffs_trace(YAFFS_TRACE_OS,
1398 "yaffs_write_end addr %p pos %x n_bytes %d",
1399 addr, (unsigned)pos, copied);
1401 ret = yaffs_file_write(filp, addr, copied, &pos);
1403 if (ret != copied) {
1404 yaffs_trace(YAFFS_TRACE_OS,
1405 "yaffs_write_end not same size ret %d copied %d",
1412 yaffs_release_space(filp);
1414 page_cache_release(pg);
1418 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1420 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1421 struct super_block *sb = dentry->d_sb;
1423 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
1425 yaffs_gross_lock(dev);
1427 buf->f_type = YAFFS_MAGIC;
1428 buf->f_bsize = sb->s_blocksize;
1429 buf->f_namelen = 255;
1431 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
1432 /* Do this if chunk size is not a power of 2 */
1434 uint64_t bytes_in_dev;
1435 uint64_t bytes_free;
1439 ((dev->param.end_block - dev->param.start_block + 1))) *
1440 ((uint64_t) (dev->param.chunks_per_block *
1441 dev->data_bytes_per_chunk));
1443 do_div(bytes_in_dev, sb->s_blocksize);
1444 /* bytes_in_dev becomes the number of blocks */
1445 buf->f_blocks = bytes_in_dev;
1447 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
1448 ((uint64_t) (dev->data_bytes_per_chunk));
1450 do_div(bytes_free, sb->s_blocksize);
1452 buf->f_bfree = bytes_free;
1454 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
1457 (dev->param.end_block - dev->param.start_block + 1) *
1458 dev->param.chunks_per_block /
1459 (sb->s_blocksize / dev->data_bytes_per_chunk);
1461 yaffs_get_n_free_chunks(dev) /
1462 (sb->s_blocksize / dev->data_bytes_per_chunk);
1465 (dev->param.end_block - dev->param.start_block + 1) *
1466 dev->param.chunks_per_block *
1467 (dev->data_bytes_per_chunk / sb->s_blocksize);
1470 yaffs_get_n_free_chunks(dev) *
1471 (dev->data_bytes_per_chunk / sb->s_blocksize);
1476 buf->f_bavail = buf->f_bfree;
1478 yaffs_gross_unlock(dev);
1482 static void yaffs_flush_inodes(struct super_block *sb)
1485 struct yaffs_obj *obj;
1487 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
1488 obj = yaffs_inode_to_obj(iptr);
1490 yaffs_trace(YAFFS_TRACE_OS,
1491 "flushing obj %d", obj->obj_id);
1492 yaffs_flush_file(obj, 1, 0);
1497 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
1499 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1504 yaffs_flush_inodes(sb);
1505 yaffs_update_dirty_dirs(dev);
1506 yaffs_flush_whole_cache(dev);
1508 yaffs_checkpoint_save(dev);
1511 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
1513 unsigned erased_chunks =
1514 dev->n_erased_blocks * dev->param.chunks_per_block;
1515 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1516 unsigned scattered = 0; /* Free chunks not in an erased block */
1518 if (erased_chunks < dev->n_free_chunks)
1519 scattered = (dev->n_free_chunks - erased_chunks);
1521 if (!context->bg_running)
1523 else if (scattered < (dev->param.chunks_per_block * 2))
1525 else if (erased_chunks > dev->n_free_chunks / 2)
1527 else if (erased_chunks > dev->n_free_chunks / 4)
1533 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
1536 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1537 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
1538 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
1541 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1542 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
1544 sb->s_dirt ? "dirty" : "clean",
1545 request_checkpoint ? "checkpoint requested" : "no checkpoint",
1546 oneshot_checkpoint ? " one-shot" : "");
1548 yaffs_gross_lock(dev);
1549 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
1550 oneshot_checkpoint) && !dev->is_checkpointed;
1552 if (sb->s_dirt || do_checkpoint) {
1553 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
1555 if (oneshot_checkpoint)
1556 yaffs_auto_checkpoint &= ~4;
1558 yaffs_gross_unlock(dev);
1564 * yaffs background thread functions .
1565 * yaffs_bg_thread_fn() the thread function
1566 * yaffs_bg_start() launches the background thread.
1567 * yaffs_bg_stop() cleans up the background thread.
1570 * The thread should only run after the yaffs is initialised
1571 * The thread should be stopped before yaffs is unmounted.
1572 * The thread should not do any writing while the fs is in read only.
1575 void yaffs_background_waker(unsigned long data)
1577 wake_up_process((struct task_struct *)data);
1580 static int yaffs_bg_thread_fn(void *data)
1582 struct yaffs_dev *dev = (struct yaffs_dev *)data;
1583 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1584 unsigned long now = jiffies;
1585 unsigned long next_dir_update = now;
1586 unsigned long next_gc = now;
1587 unsigned long expires;
1588 unsigned int urgency;
1590 struct timer_list timer;
1592 yaffs_trace(YAFFS_TRACE_BACKGROUND,
1593 "yaffs_background starting for dev %p", (void *)dev);
1596 while (context->bg_running) {
1597 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
1599 if (kthread_should_stop())
1602 if (try_to_freeze())
1605 yaffs_gross_lock(dev);
1609 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
1610 yaffs_update_dirty_dirs(dev);
1611 next_dir_update = now + HZ;
1614 if (time_after(now, next_gc) && yaffs_bg_enable) {
1615 if (!dev->is_checkpointed) {
1616 urgency = yaffs_bg_gc_urgency(dev);
1617 gc_result = yaffs_bg_gc(dev, urgency);
1619 next_gc = now + HZ / 20 + 1;
1620 else if (urgency > 0)
1621 next_gc = now + HZ / 10 + 1;
1623 next_gc = now + HZ * 2;
1626 * gc not running so set to next_dir_update
1627 * to cut down on wake ups
1629 next_gc = next_dir_update;
1632 yaffs_gross_unlock(dev);
1633 expires = next_dir_update;
1634 if (time_before(next_gc, expires))
1636 if (time_before(expires, now))
1639 init_timer_on_stack(&timer);
1640 timer.expires = expires + 1;
1641 timer.data = (unsigned long)current;
1642 timer.function = yaffs_background_waker;
1644 set_current_state(TASK_INTERRUPTIBLE);
1647 del_timer_sync(&timer);
1653 static int yaffs_bg_start(struct yaffs_dev *dev)
1656 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1661 context->bg_running = 1;
1663 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
1664 (void *)dev, "yaffs-bg-%d",
1667 if (IS_ERR(context->bg_thread)) {
1668 retval = PTR_ERR(context->bg_thread);
1669 context->bg_thread = NULL;
1670 context->bg_running = 0;
1675 static void yaffs_bg_stop(struct yaffs_dev *dev)
1677 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
1679 ctxt->bg_running = 0;
1681 if (ctxt->bg_thread) {
1682 kthread_stop(ctxt->bg_thread);
1683 ctxt->bg_thread = NULL;
1687 static void yaffs_write_super(struct super_block *sb)
1689 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
1691 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1692 "yaffs_write_super%s",
1693 request_checkpoint ? " checkpt" : "");
1695 yaffs_do_sync_fs(sb, request_checkpoint);
1699 static int yaffs_sync_fs(struct super_block *sb, int wait)
1701 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
1703 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1704 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
1706 yaffs_do_sync_fs(sb, request_checkpoint);
1711 static LIST_HEAD(yaffs_context_list);
1712 struct mutex yaffs_context_lock;
1714 struct yaffs_options {
1716 int skip_checkpoint_read;
1717 int skip_checkpoint_write;
1720 int tags_ecc_overridden;
1721 int lazy_loading_enabled;
1722 int lazy_loading_overridden;
1723 int empty_lost_and_found;
1724 int empty_lost_and_found_overridden;
1727 #define MAX_OPT_LEN 30
1728 static int yaffs_parse_options(struct yaffs_options *options,
1729 const char *options_str)
1731 char cur_opt[MAX_OPT_LEN + 1];
1735 /* Parse through the options which is a comma seperated list */
1737 while (options_str && *options_str && !error) {
1738 memset(cur_opt, 0, MAX_OPT_LEN + 1);
1741 while (*options_str == ',')
1744 while (*options_str && *options_str != ',') {
1745 if (p < MAX_OPT_LEN) {
1746 cur_opt[p] = *options_str;
1752 if (!strcmp(cur_opt, "inband-tags")) {
1753 options->inband_tags = 1;
1754 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
1755 options->tags_ecc_on = 0;
1756 options->tags_ecc_overridden = 1;
1757 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
1758 options->tags_ecc_on = 1;
1759 options->tags_ecc_overridden = 1;
1760 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
1761 options->lazy_loading_enabled = 0;
1762 options->lazy_loading_overridden = 1;
1763 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
1764 options->lazy_loading_enabled = 1;
1765 options->lazy_loading_overridden = 1;
1766 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
1767 options->empty_lost_and_found = 0;
1768 options->empty_lost_and_found_overridden = 1;
1769 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
1770 options->empty_lost_and_found = 1;
1771 options->empty_lost_and_found_overridden = 1;
1772 } else if (!strcmp(cur_opt, "no-cache")) {
1773 options->no_cache = 1;
1774 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
1775 options->skip_checkpoint_read = 1;
1776 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
1777 options->skip_checkpoint_write = 1;
1778 } else if (!strcmp(cur_opt, "no-checkpoint")) {
1779 options->skip_checkpoint_read = 1;
1780 options->skip_checkpoint_write = 1;
1782 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
1790 static const struct address_space_operations yaffs_file_address_operations = {
1791 .readpage = yaffs_readpage,
1792 .writepage = yaffs_writepage,
1793 .write_begin = yaffs_write_begin,
1794 .write_end = yaffs_write_end,
1799 static const struct inode_operations yaffs_file_inode_operations = {
1800 .setattr = yaffs_setattr,
1801 .setxattr = yaffs_setxattr,
1802 .getxattr = yaffs_getxattr,
1803 .listxattr = yaffs_listxattr,
1804 .removexattr = yaffs_removexattr,
1807 static const struct inode_operations yaffs_symlink_inode_operations = {
1808 .readlink = yaffs_readlink,
1809 .follow_link = yaffs_follow_link,
1810 .put_link = yaffs_put_link,
1811 .setattr = yaffs_setattr,
1812 .setxattr = yaffs_setxattr,
1813 .getxattr = yaffs_getxattr,
1814 .listxattr = yaffs_listxattr,
1815 .removexattr = yaffs_removexattr,
1818 static void yaffs_fill_inode_from_obj(struct inode *inode,
1819 struct yaffs_obj *obj)
1823 if (!inode || !obj) {
1824 yaffs_trace(YAFFS_TRACE_OS,
1825 "yaffs_fill_inode invalid parameters");
1829 /* Check mode against the variant type
1830 * and attempt to repair if broken. */
1831 mode = obj->yst_mode;
1833 switch (obj->variant_type) {
1834 case YAFFS_OBJECT_TYPE_FILE:
1835 if (!S_ISREG(mode)) {
1836 obj->yst_mode &= ~S_IFMT;
1837 obj->yst_mode |= S_IFREG;
1840 case YAFFS_OBJECT_TYPE_SYMLINK:
1841 if (!S_ISLNK(mode)) {
1842 obj->yst_mode &= ~S_IFMT;
1843 obj->yst_mode |= S_IFLNK;
1846 case YAFFS_OBJECT_TYPE_DIRECTORY:
1847 if (!S_ISDIR(mode)) {
1848 obj->yst_mode &= ~S_IFMT;
1849 obj->yst_mode |= S_IFDIR;
1852 case YAFFS_OBJECT_TYPE_UNKNOWN:
1853 case YAFFS_OBJECT_TYPE_HARDLINK:
1854 case YAFFS_OBJECT_TYPE_SPECIAL:
1860 inode->i_flags |= S_NOATIME;
1861 inode->i_ino = obj->obj_id;
1862 inode->i_mode = obj->yst_mode;
1863 inode->i_uid = obj->yst_uid;
1864 inode->i_gid = obj->yst_gid;
1866 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1868 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1869 inode->i_atime.tv_nsec = 0;
1870 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1871 inode->i_mtime.tv_nsec = 0;
1872 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1873 inode->i_ctime.tv_nsec = 0;
1874 inode->i_size = yaffs_get_obj_length(obj);
1875 inode->i_blocks = (inode->i_size + 511) >> 9;
1876 inode->i_nlink = yaffs_get_obj_link_count(obj);
1877 yaffs_trace(YAFFS_TRACE_OS,
1878 "yaffs_fill_inode mode %x uid %d gid %d size %d count %d",
1879 inode->i_mode, inode->i_uid, inode->i_gid,
1880 (int)inode->i_size, atomic_read(&inode->i_count));
1882 switch (obj->yst_mode & S_IFMT) {
1883 default: /* fifo, device or socket */
1884 init_special_inode(inode, obj->yst_mode,
1885 old_decode_dev(obj->yst_rdev));
1887 case S_IFREG: /* file */
1888 inode->i_op = &yaffs_file_inode_operations;
1889 inode->i_fop = &yaffs_file_operations;
1890 inode->i_mapping->a_ops = &yaffs_file_address_operations;
1892 case S_IFDIR: /* directory */
1893 inode->i_op = &yaffs_dir_inode_operations;
1894 inode->i_fop = &yaffs_dir_operations;
1896 case S_IFLNK: /* symlink */
1897 inode->i_op = &yaffs_symlink_inode_operations;
1901 yaffs_inode_to_obj_lv(inode) = obj;
1902 obj->my_inode = inode;
1905 static void yaffs_put_super(struct super_block *sb)
1907 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1909 yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super");
1911 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1912 "Shutting down yaffs background thread");
1914 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
1915 "yaffs background thread shut down");
1917 yaffs_gross_lock(dev);
1919 yaffs_flush_super(sb, 1);
1921 if (yaffs_dev_to_lc(dev)->put_super_fn)
1922 yaffs_dev_to_lc(dev)->put_super_fn(sb);
1924 yaffs_deinitialise(dev);
1926 yaffs_gross_unlock(dev);
1927 mutex_lock(&yaffs_context_lock);
1928 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
1929 mutex_unlock(&yaffs_context_lock);
1931 if (yaffs_dev_to_lc(dev)->spare_buffer) {
1932 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
1933 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
1939 static void yaffs_mtd_put_super(struct super_block *sb)
1941 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
1946 put_mtd_device(mtd);
1949 static const struct super_operations yaffs_super_ops = {
1950 .statfs = yaffs_statfs,
1951 .put_super = yaffs_put_super,
1952 .evict_inode = yaffs_evict_inode,
1953 .sync_fs = yaffs_sync_fs,
1954 .write_super = yaffs_write_super,
1957 static struct super_block *yaffs_internal_read_super(int yaffs_version,
1958 struct super_block *sb,
1959 void *data, int silent)
1962 struct inode *inode = NULL;
1963 struct dentry *root;
1964 struct yaffs_dev *dev = 0;
1965 char devname_buf[BDEVNAME_SIZE + 1];
1966 struct mtd_info *mtd;
1968 char *data_str = (char *)data;
1969 struct yaffs_linux_context *context = NULL;
1970 struct yaffs_param *param;
1972 struct yaffs_options options;
1975 struct yaffs_linux_context *context_iterator;
1976 struct list_head *l;
1979 printk(KERN_INFO "yaffs: sb is NULL\n");
1983 sb->s_magic = YAFFS_MAGIC;
1984 sb->s_op = &yaffs_super_ops;
1985 sb->s_flags |= MS_NOATIME;
1987 read_only = ((sb->s_flags & MS_RDONLY) != 0);
1989 sb->s_export_op = &yaffs_export_ops;
1992 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1993 else if (!yaffs_devname(sb, devname_buf))
1994 printk(KERN_INFO "yaffs: devname is NULL\n");
1996 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
1998 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2003 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2005 memset(&options, 0, sizeof(options));
2007 if (yaffs_parse_options(&options, data_str))
2008 /* Option parsing failed */
2011 sb->s_blocksize = PAGE_CACHE_SIZE;
2012 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2014 yaffs_trace(YAFFS_TRACE_OS,
2015 "yaffs_read_super: Using yaffs%d", yaffs_version);
2016 yaffs_trace(YAFFS_TRACE_OS,
2017 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2019 yaffs_trace(YAFFS_TRACE_ALWAYS,
2020 "Attempting MTD mount of %u.%u,\"%s\"",
2021 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2022 yaffs_devname(sb, devname_buf));
2024 /* Check it's an mtd device..... */
2025 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2026 return NULL; /* This isn't an mtd device */
2028 /* Get the device */
2029 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2031 yaffs_trace(YAFFS_TRACE_ALWAYS,
2032 "MTD device #%u doesn't appear to exist",
2036 /* Check it's NAND */
2037 if (mtd->type != MTD_NANDFLASH) {
2038 yaffs_trace(YAFFS_TRACE_ALWAYS,
2039 "MTD device is not NAND it's type %d",
2044 yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase);
2045 yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read);
2046 yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write);
2047 yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob);
2048 yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob);
2049 yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad);
2050 yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad);
2051 yaffs_trace(YAFFS_TRACE_OS, " writesize %d", mtd->writesize);
2052 yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
2053 yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
2054 yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
2056 if (yaffs_auto_select && yaffs_version == 1 && mtd->writesize >= 2048) {
2057 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2061 if (yaffs_auto_select && yaffs_version == 2 && !options.inband_tags &&
2062 mtd->writesize == 512) {
2063 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2067 if (yaffs_version == 2) {
2068 /* Check for version 2 style functions */
2070 !mtd->block_isbad ||
2071 !mtd->block_markbad ||
2073 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2074 yaffs_trace(YAFFS_TRACE_ALWAYS,
2075 "MTD device does not support required functions");
2079 if ((mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2080 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2081 !options.inband_tags) {
2082 yaffs_trace(YAFFS_TRACE_ALWAYS,
2083 "MTD device does not have the right page sizes");
2087 /* Check for V1 style functions */
2090 !mtd->write || !mtd->read_oob || !mtd->write_oob) {
2091 yaffs_trace(YAFFS_TRACE_ALWAYS,
2092 "MTD device does not support required functions");
2096 if (mtd->writesize < YAFFS_BYTES_PER_CHUNK ||
2097 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2098 yaffs_trace(YAFFS_TRACE_ALWAYS,
2099 "MTD device does not support have the right page sizes");
2104 /* OK, so if we got here, we have an MTD that's NAND and looks
2105 * like it has the right capabilities
2106 * Set the struct yaffs_dev up for mtd
2109 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2112 "yaffs: mtd is read only, setting superblock read only");
2113 sb->s_flags |= MS_RDONLY;
2116 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2117 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2119 if (!dev || !context) {
2127 /* Deep shit could not allocate device structure */
2128 yaffs_trace(YAFFS_TRACE_ALWAYS,
2129 "yaffs_read_super failed trying to allocate yaffs_dev");
2132 memset(dev, 0, sizeof(struct yaffs_dev));
2133 param = &(dev->param);
2135 memset(context, 0, sizeof(struct yaffs_linux_context));
2136 dev->os_context = context;
2137 INIT_LIST_HEAD(&(context->context_list));
2139 context->super = sb;
2141 dev->read_only = read_only;
2143 sb->s_fs_info = dev;
2145 dev->driver_context = mtd;
2146 param->name = mtd->name;
2148 /* Set up the memory size parameters.... */
2151 YCALCBLOCKS(mtd->size,
2152 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2154 param->start_block = 0;
2155 param->end_block = n_blocks - 1;
2156 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2157 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2158 param->n_reserved_blocks = 5;
2159 param->n_caches = (options.no_cache) ? 0 : 10;
2160 param->inband_tags = options.inband_tags;
2162 param->disable_lazy_load = 1;
2163 param->enable_xattr = 1;
2164 if (options.lazy_loading_overridden)
2165 param->disable_lazy_load = !options.lazy_loading_enabled;
2167 param->defered_dir_update = 1;
2169 if (options.tags_ecc_overridden)
2170 param->no_tags_ecc = !options.tags_ecc_on;
2172 param->empty_lost_n_found = 1;
2174 param->refresh_period = 500;
2176 if (options.empty_lost_and_found_overridden)
2177 param->empty_lost_n_found = options.empty_lost_and_found;
2179 /* ... and the functions. */
2180 if (yaffs_version == 2) {
2181 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2182 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2183 param->bad_block_fn = nandmtd2_mark_block_bad;
2184 param->query_block_fn = nandmtd2_query_block;
2185 yaffs_dev_to_lc(dev)->spare_buffer =
2186 kmalloc(mtd->oobsize, GFP_NOFS);
2187 param->is_yaffs2 = 1;
2188 param->total_bytes_per_chunk = mtd->writesize;
2189 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2190 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2192 param->start_block = 0;
2193 param->end_block = n_blocks - 1;
2195 /* use the MTD interface in yaffs_mtdif1.c */
2196 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2197 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2198 param->bad_block_fn = nandmtd1_mark_block_bad;
2199 param->query_block_fn = nandmtd1_query_block;
2200 param->is_yaffs2 = 0;
2202 /* ... and common functions */
2203 param->erase_fn = nandmtd_erase_block;
2204 param->initialise_flash_fn = nandmtd_initialise;
2206 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2208 param->sb_dirty_fn = yaffs_touch_super;
2209 param->gc_control = yaffs_gc_control_callback;
2211 yaffs_dev_to_lc(dev)->super = sb;
2213 param->use_nand_ecc = 1;
2215 param->skip_checkpt_rd = options.skip_checkpoint_read;
2216 param->skip_checkpt_wr = options.skip_checkpoint_write;
2218 mutex_lock(&yaffs_context_lock);
2219 /* Get a mount id */
2220 for (mount_id = 0, found = 0; !found; mount_id++) {
2222 list_for_each(l, &yaffs_context_list) {
2224 list_entry(l, struct yaffs_linux_context,
2226 if (context_iterator->mount_id == mount_id)
2230 context->mount_id = mount_id;
2232 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2233 &yaffs_context_list);
2234 mutex_unlock(&yaffs_context_lock);
2236 /* Directory search handling... */
2237 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2238 param->remove_obj_fn = yaffs_remove_obj_callback;
2240 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2242 yaffs_gross_lock(dev);
2244 err = yaffs_guts_initialise(dev);
2246 yaffs_trace(YAFFS_TRACE_OS,
2247 "yaffs_read_super: guts initialised %s",
2248 (err == YAFFS_OK) ? "OK" : "FAILED");
2250 if (err == YAFFS_OK)
2251 yaffs_bg_start(dev);
2253 if (!context->bg_thread)
2254 param->defered_dir_update = 0;
2256 /* Release lock before yaffs_get_inode() */
2257 yaffs_gross_unlock(dev);
2259 /* Create root inode */
2260 if (err == YAFFS_OK)
2261 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2266 inode->i_op = &yaffs_dir_inode_operations;
2267 inode->i_fop = &yaffs_dir_operations;
2269 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2271 root = d_alloc_root(inode);
2273 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done");
2280 sb->s_dirt = !dev->is_checkpointed;
2281 yaffs_trace(YAFFS_TRACE_ALWAYS,
2282 "yaffs_read_super: is_checkpointed %d",
2283 dev->is_checkpointed);
2285 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
2289 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2292 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2295 static int yaffs_read_super(struct file_system_type *fs,
2296 int flags, const char *dev_name,
2297 void *data, struct vfsmount *mnt)
2300 return get_sb_bdev(fs, flags, dev_name, data,
2301 yaffs_internal_read_super_mtd, mnt);
2304 static struct file_system_type yaffs_fs_type = {
2305 .owner = THIS_MODULE,
2307 .get_sb = yaffs_read_super,
2308 .kill_sb = kill_block_super,
2309 .fs_flags = FS_REQUIRES_DEV,
2312 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2315 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2318 static int yaffs2_read_super(struct file_system_type *fs,
2319 int flags, const char *dev_name, void *data,
2320 struct vfsmount *mnt)
2322 return get_sb_bdev(fs, flags, dev_name, data,
2323 yaffs2_internal_read_super_mtd, mnt);
2326 static struct file_system_type yaffs2_fs_type = {
2327 .owner = THIS_MODULE,
2329 .get_sb = yaffs2_read_super,
2330 .kill_sb = kill_block_super,
2331 .fs_flags = FS_REQUIRES_DEV,
2335 static struct proc_dir_entry *my_proc_entry;
2337 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
2339 struct yaffs_param *param = &dev->param;
2341 buf += sprintf(buf, "start_block........... %d\n", param->start_block);
2342 buf += sprintf(buf, "end_block............. %d\n", param->end_block);
2343 buf += sprintf(buf, "total_bytes_per_chunk. %d\n",
2344 param->total_bytes_per_chunk);
2345 buf += sprintf(buf, "use_nand_ecc.......... %d\n",
2346 param->use_nand_ecc);
2347 buf += sprintf(buf, "no_tags_ecc........... %d\n", param->no_tags_ecc);
2348 buf += sprintf(buf, "is_yaffs2............. %d\n", param->is_yaffs2);
2349 buf += sprintf(buf, "inband_tags........... %d\n", param->inband_tags);
2350 buf += sprintf(buf, "empty_lost_n_found.... %d\n",
2351 param->empty_lost_n_found);
2352 buf += sprintf(buf, "disable_lazy_load..... %d\n",
2353 param->disable_lazy_load);
2354 buf += sprintf(buf, "refresh_period........ %d\n",
2355 param->refresh_period);
2356 buf += sprintf(buf, "n_caches.............. %d\n", param->n_caches);
2357 buf += sprintf(buf, "n_reserved_blocks..... %d\n",
2358 param->n_reserved_blocks);
2359 buf += sprintf(buf, "always_check_erased... %d\n",
2360 param->always_check_erased);
2365 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
2368 sprintf(buf, "data_bytes_per_chunk.. %d\n",
2369 dev->data_bytes_per_chunk);
2370 buf += sprintf(buf, "chunk_grp_bits........ %d\n", dev->chunk_grp_bits);
2371 buf += sprintf(buf, "chunk_grp_size........ %d\n", dev->chunk_grp_size);
2373 sprintf(buf, "n_erased_blocks....... %d\n", dev->n_erased_blocks);
2375 sprintf(buf, "blocks_in_checkpt..... %d\n", dev->blocks_in_checkpt);
2376 buf += sprintf(buf, "\n");
2377 buf += sprintf(buf, "n_tnodes.............. %d\n", dev->n_tnodes);
2378 buf += sprintf(buf, "n_obj................. %d\n", dev->n_obj);
2379 buf += sprintf(buf, "n_free_chunks......... %d\n", dev->n_free_chunks);
2380 buf += sprintf(buf, "\n");
2381 buf += sprintf(buf, "n_page_writes......... %u\n", dev->n_page_writes);
2382 buf += sprintf(buf, "n_page_reads.......... %u\n", dev->n_page_reads);
2383 buf += sprintf(buf, "n_erasures............ %u\n", dev->n_erasures);
2384 buf += sprintf(buf, "n_gc_copies........... %u\n", dev->n_gc_copies);
2385 buf += sprintf(buf, "all_gcs............... %u\n", dev->all_gcs);
2387 sprintf(buf, "passive_gc_count...... %u\n", dev->passive_gc_count);
2389 sprintf(buf, "oldest_dirty_gc_count. %u\n",
2390 dev->oldest_dirty_gc_count);
2391 buf += sprintf(buf, "n_gc_blocks........... %u\n", dev->n_gc_blocks);
2392 buf += sprintf(buf, "bg_gcs................ %u\n", dev->bg_gcs);
2394 sprintf(buf, "n_retired_writes...... %u\n", dev->n_retired_writes);
2396 sprintf(buf, "n_retired_blocks...... %u\n", dev->n_retired_blocks);
2397 buf += sprintf(buf, "n_ecc_fixed........... %u\n", dev->n_ecc_fixed);
2398 buf += sprintf(buf, "n_ecc_unfixed......... %u\n", dev->n_ecc_unfixed);
2400 sprintf(buf, "n_tags_ecc_fixed...... %u\n", dev->n_tags_ecc_fixed);
2402 sprintf(buf, "n_tags_ecc_unfixed.... %u\n",
2403 dev->n_tags_ecc_unfixed);
2404 buf += sprintf(buf, "cache_hits............ %u\n", dev->cache_hits);
2406 sprintf(buf, "n_deleted_files....... %u\n", dev->n_deleted_files);
2408 sprintf(buf, "n_unlinked_files...... %u\n", dev->n_unlinked_files);
2409 buf += sprintf(buf, "refresh_count......... %u\n", dev->refresh_count);
2410 buf += sprintf(buf, "n_bg_deletions........ %u\n", dev->n_bg_deletions);
2415 static int yaffs_proc_read(char *page,
2417 off_t offset, int count, int *eof, void *data)
2419 struct list_head *item;
2424 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2425 * We use 'offset' (*ppos) to indicate where we are in dev_list.
2426 * This also assumes the user has posted a read buffer large
2427 * enough to hold the complete output; but that's life in /proc.
2432 /* Print header first */
2434 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__ "\n");
2436 buf += sprintf(buf, "\n");
2440 mutex_lock(&yaffs_context_lock);
2442 /* Locate and print the Nth entry.
2443 * Order N-squared but N is small. */
2444 list_for_each(item, &yaffs_context_list) {
2445 struct yaffs_linux_context *dc =
2446 list_entry(item, struct yaffs_linux_context,
2448 struct yaffs_dev *dev = dc->dev;
2450 if (n < (step & ~1)) {
2454 if ((step & 1) == 0) {
2456 sprintf(buf, "\nDevice %d \"%s\"\n", n,
2458 buf = yaffs_dump_dev_part0(buf, dev);
2460 buf = yaffs_dump_dev_part1(buf, dev);
2465 mutex_unlock(&yaffs_context_lock);
2468 return buf - page < count ? buf - page : count;
2473 /* Stuff to handle installation of file systems */
2474 struct file_system_to_install {
2475 struct file_system_type *fst;
2479 static struct file_system_to_install fs_to_install[] = {
2480 {&yaffs_fs_type, 0},
2481 {&yaffs2_fs_type, 0},
2485 static int __init init_yaffs_fs(void)
2488 struct file_system_to_install *fsinst;
2490 yaffs_trace(YAFFS_TRACE_ALWAYS,
2491 "yaffs built " __DATE__ " " __TIME__ " Installing.");
2493 mutex_init(&yaffs_context_lock);
2495 /* Install the proc_fs entries */
2496 my_proc_entry = create_proc_entry("yaffs",
2497 S_IRUGO | S_IFREG, NULL);
2499 if (my_proc_entry) {
2500 my_proc_entry->write_proc = NULL;
2501 my_proc_entry->read_proc = yaffs_proc_read;
2502 my_proc_entry->data = NULL;
2508 /* Now add the file system entries */
2510 fsinst = fs_to_install;
2512 while (fsinst->fst && !error) {
2513 error = register_filesystem(fsinst->fst);
2515 fsinst->installed = 1;
2519 /* Any errors? uninstall */
2521 fsinst = fs_to_install;
2523 while (fsinst->fst) {
2524 if (fsinst->installed) {
2525 unregister_filesystem(fsinst->fst);
2526 fsinst->installed = 0;
2535 static void __exit exit_yaffs_fs(void)
2538 struct file_system_to_install *fsinst;
2540 yaffs_trace(YAFFS_TRACE_ALWAYS,
2541 "yaffs built " __DATE__ " " __TIME__ " removing.");
2543 remove_proc_entry("yaffs", NULL);
2545 fsinst = fs_to_install;
2547 while (fsinst->fst) {
2548 if (fsinst->installed) {
2549 unregister_filesystem(fsinst->fst);
2550 fsinst->installed = 0;
2556 module_init(init_yaffs_fs)
2557 module_exit(exit_yaffs_fs)
2559 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2560 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
2561 MODULE_LICENSE("GPL");