2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * Luc van OostenRyck for numerous patches.
10 * Nick Bane for numerous patches.
11 * Nick Bane for 2.5/2.6 integration.
12 * Andras Toth for mknod rdev issue.
13 * Michael Fischer for finding the problem with inode inconsistency.
14 * Some code bodily lifted from JFFS
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
23 * This is the file system front-end to YAFFS that hooks it up to
27 * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
29 * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
31 * >> inode->u.generic_ip points to the associated struct yaffs_obj.
35 * There are two variants of the VFS glue code. This variant should compile
36 * for any version of Linux.
38 #include <linux/version.h>
40 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
41 #define YAFFS_COMPILE_BACKGROUND
42 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6, 23))
43 #define YAFFS_COMPILE_FREEZER
47 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28))
48 #define YAFFS_COMPILE_EXPORTFS
51 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
52 #define YAFFS_USE_SETATTR_COPY
53 #define YAFFS_USE_TRUNCATE_SETSIZE
55 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
56 #define YAFFS_HAS_EVICT_INODE
59 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
60 #define YAFFS_NEW_FOLLOW_LINK 1
62 #define YAFFS_NEW_FOLLOW_LINK 0
65 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
66 #include <linux/config.h>
69 #include <linux/kernel.h>
70 #include <linux/module.h>
71 #include <linux/slab.h>
72 #include <linux/init.h>
74 #include <linux/proc_fs.h>
75 #include <linux/smp_lock.h>
76 #include <linux/pagemap.h>
77 #include <linux/mtd/mtd.h>
78 #include <linux/interrupt.h>
79 #include <linux/string.h>
80 #include <linux/ctype.h>
82 #if (YAFFS_NEW_FOLLOW_LINK == 1)
83 #include <linux/namei.h>
86 #ifdef YAFFS_COMPILE_EXPORTFS
87 #include <linux/exportfs.h>
90 #ifdef YAFFS_COMPILE_BACKGROUND
91 #include <linux/kthread.h>
92 #include <linux/delay.h>
94 #ifdef YAFFS_COMPILE_FREEZER
95 #include <linux/freezer.h>
98 #include <asm/div64.h>
100 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
102 #include <linux/statfs.h>
104 #define UnlockPage(p) unlock_page(p)
105 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
107 /* FIXME: use sb->s_id instead ? */
108 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
112 #include <linux/locks.h>
113 #define BDEVNAME_SIZE 0
114 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
116 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
117 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
123 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
124 #define YPROC_ROOT (&proc_root)
126 #define YPROC_ROOT NULL
129 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
130 #define Y_INIT_TIMER(a) init_timer(a)
132 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
135 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
136 #define WRITE_SIZE_STR "writesize"
137 #define WRITE_SIZE(mtd) ((mtd)->writesize)
139 #define WRITE_SIZE_STR "oobblock"
140 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
143 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
144 #define YAFFS_USE_WRITE_BEGIN_END 1
146 #define YAFFS_USE_WRITE_BEGIN_END 0
149 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
150 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
152 uint64_t result = partition_size;
153 do_div(result, block_size);
154 return (uint32_t) result;
157 #define YCALCBLOCKS(s, b) ((s)/(b))
160 #include <linux/uaccess.h>
161 #include <linux/mtd/mtd.h>
163 #include "yportenv.h"
164 #include "yaffs_trace.h"
165 #include "yaffs_guts.h"
166 #include "yaffs_attribs.h"
168 #include "yaffs_linux.h"
170 #include "yaffs_mtdif.h"
171 #include "yaffs_mtdif1.h"
172 #include "yaffs_mtdif2.h"
174 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
175 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
176 unsigned int yaffs_auto_checkpoint = 1;
177 unsigned int yaffs_gc_control = 1;
178 unsigned int yaffs_bg_enable = 1;
180 /* Module Parameters */
181 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
182 module_param(yaffs_trace_mask, uint, 0644);
183 module_param(yaffs_wr_attempts, uint, 0644);
184 module_param(yaffs_auto_checkpoint, uint, 0644);
185 module_param(yaffs_gc_control, uint, 0644);
186 module_param(yaffs_bg_enable, uint, 0644);
188 MODULE_PARM(yaffs_trace_mask, "i");
189 MODULE_PARM(yaffs_wr_attempts, "i");
190 MODULE_PARM(yaffs_auto_checkpoint, "i");
191 MODULE_PARM(yaffs_gc_control, "i");
194 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
195 /* use iget and read_inode */
196 #define Y_IGET(sb, inum) iget((sb), (inum))
197 static void yaffs_read_inode(struct inode *inode);
200 /* Call local equivalent */
201 #define YAFFS_USE_OWN_IGET
202 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
204 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
207 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
208 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
210 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
213 #define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
214 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
216 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
217 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
219 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
222 #define update_dir_time(dir) do {\
223 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
226 static void yaffs_put_super(struct super_block *sb);
228 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
230 static ssize_t yaffs_hold_space(struct file *f);
231 static void yaffs_release_space(struct file *f);
233 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
234 static int yaffs_file_flush(struct file *file, fl_owner_t id);
236 static int yaffs_file_flush(struct file *file);
239 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
240 static int yaffs_sync_object(struct file *file, int datasync);
242 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
246 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
248 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
249 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
250 struct nameidata *n);
251 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
252 struct nameidata *n);
254 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
255 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
257 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
258 struct dentry *dentry);
259 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
260 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
261 const char *symname);
262 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
264 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
265 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
268 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
271 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
272 struct inode *new_dir, struct dentry *new_dentry);
273 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
275 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
276 static int yaffs_sync_fs(struct super_block *sb, int wait);
277 static void yaffs_write_super(struct super_block *sb);
279 static int yaffs_sync_fs(struct super_block *sb);
280 static int yaffs_write_super(struct super_block *sb);
283 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
284 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
285 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
286 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
288 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
291 #ifdef YAFFS_HAS_PUT_INODE
292 static void yaffs_put_inode(struct inode *inode);
295 #ifdef YAFFS_HAS_EVICT_INODE
296 static void yaffs_evict_inode(struct inode *);
298 static void yaffs_delete_inode(struct inode *);
299 static void yaffs_clear_inode(struct inode *);
302 static int yaffs_readpage(struct file *file, struct page *page);
303 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
304 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
306 static int yaffs_writepage(struct page *page);
309 #ifdef CONFIG_YAFFS_XATTR
310 int yaffs_setxattr(struct dentry *dentry, const char *name,
311 const void *value, size_t size, int flags);
312 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
314 int yaffs_removexattr(struct dentry *dentry, const char *name);
315 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
318 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
319 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
320 loff_t pos, unsigned len, unsigned flags,
321 struct page **pagep, void **fsdata);
322 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
323 loff_t pos, unsigned len, unsigned copied,
324 struct page *pg, void *fsdadata);
326 static int yaffs_prepare_write(struct file *f, struct page *pg,
327 unsigned offset, unsigned to);
328 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
333 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
335 #if (YAFFS_NEW_FOLLOW_LINK == 1)
336 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
337 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
339 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
342 static void yaffs_touch_super(struct yaffs_dev *dev);
344 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
346 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
348 static struct address_space_operations yaffs_file_address_operations = {
349 .readpage = yaffs_readpage,
350 .writepage = yaffs_writepage,
351 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
352 .write_begin = yaffs_write_begin,
353 .write_end = yaffs_write_end,
355 .prepare_write = yaffs_prepare_write,
356 .commit_write = yaffs_commit_write,
360 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
361 static const struct file_operations yaffs_file_operations = {
362 .read = do_sync_read,
363 .write = do_sync_write,
364 .aio_read = generic_file_aio_read,
365 .aio_write = generic_file_aio_write,
366 .mmap = generic_file_mmap,
367 .flush = yaffs_file_flush,
368 .fsync = yaffs_sync_object,
369 .splice_read = generic_file_splice_read,
370 .splice_write = generic_file_splice_write,
371 .llseek = generic_file_llseek,
374 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
376 static const struct file_operations yaffs_file_operations = {
377 .read = do_sync_read,
378 .write = do_sync_write,
379 .aio_read = generic_file_aio_read,
380 .aio_write = generic_file_aio_write,
381 .mmap = generic_file_mmap,
382 .flush = yaffs_file_flush,
383 .fsync = yaffs_sync_object,
384 .sendfile = generic_file_sendfile,
389 static const struct file_operations yaffs_file_operations = {
390 .read = generic_file_read,
391 .write = generic_file_write,
392 .mmap = generic_file_mmap,
393 .flush = yaffs_file_flush,
394 .fsync = yaffs_sync_object,
395 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
396 .sendfile = generic_file_sendfile,
401 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
402 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
404 void *kaddr = kmap_atomic(page, KM_USER0);
405 memset(kaddr + start, 0, end - start);
406 kunmap_atomic(kaddr, KM_USER0);
407 flush_dcache_page(page);
411 static const struct inode_operations yaffs_file_inode_operations = {
412 .setattr = yaffs_setattr,
413 #ifdef CONFIG_YAFFS_XATTR
414 .setxattr = yaffs_setxattr,
415 .getxattr = yaffs_getxattr,
416 .listxattr = yaffs_listxattr,
417 .removexattr = yaffs_removexattr,
421 static const struct inode_operations yaffs_symlink_inode_operations = {
422 .readlink = yaffs_readlink,
423 .follow_link = yaffs_follow_link,
424 #if (YAFFS_NEW_FOLLOW_LINK == 1)
425 .put_link = yaffs_put_link,
427 .setattr = yaffs_setattr,
428 #ifdef CONFIG_YAFFS_XATTR
429 .setxattr = yaffs_setxattr,
430 .getxattr = yaffs_getxattr,
431 .listxattr = yaffs_listxattr,
432 .removexattr = yaffs_removexattr,
436 static const struct inode_operations yaffs_dir_inode_operations = {
437 .create = yaffs_create,
438 .lookup = yaffs_lookup,
440 .unlink = yaffs_unlink,
441 .symlink = yaffs_symlink,
442 .mkdir = yaffs_mkdir,
443 .rmdir = yaffs_unlink,
444 .mknod = yaffs_mknod,
445 .rename = yaffs_rename,
446 .setattr = yaffs_setattr,
447 #ifdef CONFIG_YAFFS_XATTR
448 .setxattr = yaffs_setxattr,
449 .getxattr = yaffs_getxattr,
450 .listxattr = yaffs_listxattr,
451 .removexattr = yaffs_removexattr,
455 static const struct file_operations yaffs_dir_operations = {
456 .read = generic_read_dir,
457 .readdir = yaffs_readdir,
458 .fsync = yaffs_sync_object,
459 .llseek = yaffs_dir_llseek,
462 static const struct super_operations yaffs_super_ops = {
463 .statfs = yaffs_statfs,
465 #ifndef YAFFS_USE_OWN_IGET
466 .read_inode = yaffs_read_inode,
468 #ifdef YAFFS_HAS_PUT_INODE
469 .put_inode = yaffs_put_inode,
471 .put_super = yaffs_put_super,
472 #ifdef YAFFS_HAS_EVICT_INODE
473 .evict_inode = yaffs_evict_inode,
475 .delete_inode = yaffs_delete_inode,
476 .clear_inode = yaffs_clear_inode,
478 .sync_fs = yaffs_sync_fs,
479 .write_super = yaffs_write_super,
482 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
484 #ifdef YAFFS_USE_SETATTR_COPY
485 setattr_copy(inode, attr);
488 return inode_setattr(inode, attr);
493 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
495 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
496 truncate_setsize(inode, newsize);
499 truncate_inode_pages(&inode->i_data, newsize);
505 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
507 return yaffs_gc_control;
510 static void yaffs_gross_lock(struct yaffs_dev *dev)
512 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
513 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
514 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
517 static void yaffs_gross_unlock(struct yaffs_dev *dev)
519 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
520 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
523 #ifdef YAFFS_COMPILE_EXPORTFS
525 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
528 return Y_IGET(sb, ino);
531 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
532 struct fid *fid, int fh_len,
535 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
536 yaffs2_nfs_get_inode);
539 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
540 struct fid *fid, int fh_len,
543 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
544 yaffs2_nfs_get_inode);
547 struct dentry *yaffs2_get_parent(struct dentry *dentry)
550 struct super_block *sb = dentry->d_inode->i_sb;
551 struct dentry *parent = ERR_PTR(-ENOENT);
553 unsigned long parent_ino;
554 struct yaffs_obj *d_obj;
555 struct yaffs_obj *parent_obj;
557 d_obj = yaffs_inode_to_obj(dentry->d_inode);
560 parent_obj = d_obj->parent;
562 parent_ino = yaffs_get_obj_inode(parent_obj);
563 inode = Y_IGET(sb, parent_ino);
566 parent = ERR_CAST(inode);
568 parent = d_obtain_alias(inode);
569 if (!IS_ERR(parent)) {
570 parent = ERR_PTR(-ENOMEM);
580 /* Just declare a zero structure as a NULL value implies
581 * using the default functions of exportfs.
584 static struct export_operations yaffs_export_ops = {
585 .fh_to_dentry = yaffs2_fh_to_dentry,
586 .fh_to_parent = yaffs2_fh_to_parent,
587 .get_parent = yaffs2_get_parent,
592 /*-----------------------------------------------------------------*/
593 /* Directory search context allows us to unlock access to yaffs during
594 * filldir without causing problems with the directory being modified.
595 * This is similar to the tried and tested mechanism used in yaffs direct.
597 * A search context iterates along a doubly linked list of siblings in the
598 * directory. If the iterating object is deleted then this would corrupt
599 * the list iteration, likely causing a crash. The search context avoids
600 * this by using the remove_obj_fn to move the search context to the
601 * next object before the object is deleted.
603 * Many readdirs (and thus seach conexts) may be alive simulateously so
604 * each struct yaffs_dev has a list of these.
606 * A seach context lives for the duration of a readdir.
608 * All these functions must be called while yaffs is locked.
611 struct yaffs_search_context {
612 struct yaffs_dev *dev;
613 struct yaffs_obj *dir_obj;
614 struct yaffs_obj *next_return;
615 struct list_head others;
619 * yaffs_new_search() creates a new search context, initialises it and
620 * adds it to the device's search context list.
622 * Called at start of readdir.
624 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
626 struct yaffs_dev *dev = dir->my_dev;
627 struct yaffs_search_context *sc =
628 YMALLOC(sizeof(struct yaffs_search_context));
632 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
633 sc->next_return = NULL;
636 list_entry(dir->variant.dir_variant.children.next,
637 struct yaffs_obj, siblings);
638 INIT_LIST_HEAD(&sc->others);
639 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
645 * yaffs_search_end() disposes of a search context and cleans up.
647 static void yaffs_search_end(struct yaffs_search_context *sc)
650 list_del(&sc->others);
656 * yaffs_search_advance() moves a search context to the next object.
657 * Called when the search iterates or when an object removal causes
658 * the search context to be moved to the next object.
660 static void yaffs_search_advance(struct yaffs_search_context *sc)
665 if (sc->next_return == NULL ||
666 list_empty(&sc->dir_obj->variant.dir_variant.children))
667 sc->next_return = NULL;
669 struct list_head *next = sc->next_return->siblings.next;
671 if (next == &sc->dir_obj->variant.dir_variant.children)
672 sc->next_return = NULL; /* end of list */
675 list_entry(next, struct yaffs_obj, siblings);
680 * yaffs_remove_obj_callback() is called when an object is unlinked.
681 * We check open search contexts and advance any which are currently
682 * on the object being iterated.
684 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
688 struct yaffs_search_context *sc;
689 struct list_head *search_contexts =
690 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
692 /* Iterate through the directory search contexts.
693 * If any are currently on the object being removed, then advance
694 * the search context to the next object to prevent a hanging pointer.
696 list_for_each(i, search_contexts) {
698 sc = list_entry(i, struct yaffs_search_context, others);
699 if (sc->next_return == obj)
700 yaffs_search_advance(sc);
706 /*-----------------------------------------------------------------*/
708 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
711 unsigned char *alias;
714 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
716 yaffs_gross_lock(dev);
718 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
720 yaffs_gross_unlock(dev);
725 ret = vfs_readlink(dentry, buffer, buflen, alias);
730 #if (YAFFS_NEW_FOLLOW_LINK == 1)
731 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
735 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
739 unsigned char *alias;
741 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
743 yaffs_gross_lock(dev);
745 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
746 yaffs_gross_unlock(dev);
752 #if (YAFFS_NEW_FOLLOW_LINK == 1)
753 nd_set_link(nd, alias);
757 ret = ERR_PTR(ret_int);
760 ret = vfs_follow_link(nd, alias);
769 #if (YAFFS_NEW_FOLLOW_LINK == 1)
770 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
776 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
777 struct yaffs_obj *obj);
780 * Lookup is used to find objects in the fs
782 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
784 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
787 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
790 struct yaffs_obj *obj;
791 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
793 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
795 if (current != yaffs_dev_to_lc(dev)->readdir_process)
796 yaffs_gross_lock(dev);
799 (TSTR("yaffs_lookup for %d:%s\n"),
800 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name));
802 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
804 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
806 /* Can't hold gross lock when calling yaffs_get_inode() */
807 if (current != yaffs_dev_to_lc(dev)->readdir_process)
808 yaffs_gross_unlock(dev);
812 (TSTR("yaffs_lookup found %d\n"), obj->obj_id));
814 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
817 T(YAFFS_TRACE_OS, (TSTR("yaffs_loookup dentry \n")));
818 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
819 * d_add even if NULL inode */
821 /*dget(dentry); // try to solve directory bug */
822 d_add(dentry, inode);
830 T(YAFFS_TRACE_OS, (TSTR("yaffs_lookup not found\n")));
834 /* added NCB for 2.5/6 compatability - forces add even if inode is
835 * NULL which creates dentry hash */
836 d_add(dentry, inode);
841 #ifdef YAFFS_HAS_PUT_INODE
843 /* For now put inode is just for debugging
844 * Put inode is called when the inode **structure** is put.
846 static void yaffs_put_inode(struct inode *inode)
849 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
850 atomic_read(&inode->i_count)));
855 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
857 /* Clear the association between the inode and
858 * the struct yaffs_obj.
860 obj->my_inode = NULL;
861 yaffs_inode_to_obj_lv(inode) = NULL;
863 /* If the object freeing was deferred, then the real
865 * This should fix the inode inconsistency problem.
867 yaffs_handle_defered_free(obj);
870 #ifdef YAFFS_HAS_EVICT_INODE
871 /* yaffs_evict_inode combines into one operation what was previously done in
872 * yaffs_clear_inode() and yaffs_delete_inode()
875 static void yaffs_evict_inode(struct inode *inode)
877 struct yaffs_obj *obj;
878 struct yaffs_dev *dev;
881 obj = yaffs_inode_to_obj(inode);
884 (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
885 atomic_read(&inode->i_count),
886 obj ? "object exists" : "null object"));
888 if (!inode->i_nlink && !is_bad_inode(inode))
890 truncate_inode_pages(&inode->i_data, 0);
891 end_writeback(inode);
893 if (deleteme && obj) {
895 yaffs_gross_lock(dev);
897 yaffs_gross_unlock(dev);
901 yaffs_gross_lock(dev);
902 yaffs_unstitch_obj(inode, obj);
903 yaffs_gross_unlock(dev);
909 /* clear is called to tell the fs to release any per-inode data it holds.
910 * The object might still exist on disk and is just being thrown out of the cache
911 * or else the object has actually been deleted and we're being called via
913 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
916 static void yaffs_clear_inode(struct inode *inode)
918 struct yaffs_obj *obj;
919 struct yaffs_dev *dev;
921 obj = yaffs_inode_to_obj(inode);
924 (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
925 atomic_read(&inode->i_count),
926 obj ? "object exists" : "null object"));
930 yaffs_gross_lock(dev);
931 yaffs_unstitch_obj(inode, obj);
932 yaffs_gross_unlock(dev);
937 /* delete is called when the link count is zero and the inode
938 * is put (ie. nobody wants to know about it anymore, time to
940 * NB Must call clear_inode()
942 static void yaffs_delete_inode(struct inode *inode)
944 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
945 struct yaffs_dev *dev;
948 (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
949 atomic_read(&inode->i_count),
950 obj ? "object exists" : "null object"));
954 yaffs_gross_lock(dev);
956 yaffs_gross_unlock(dev);
958 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
959 truncate_inode_pages(&inode->i_data, 0);
965 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
966 static int yaffs_file_flush(struct file *file, fl_owner_t id)
968 static int yaffs_file_flush(struct file *file)
971 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
973 struct yaffs_dev *dev = obj->my_dev;
976 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->obj_id,
977 obj->dirty ? "dirty" : "clean"));
979 yaffs_gross_lock(dev);
981 yaffs_flush_file(obj, 1, 0);
983 yaffs_gross_unlock(dev);
988 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
990 /* Lifted from jffs2 */
992 struct yaffs_obj *obj;
993 unsigned char *pg_buf;
996 struct yaffs_dev *dev;
999 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
1000 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1001 (unsigned)PAGE_CACHE_SIZE));
1003 obj = yaffs_dentry_to_obj(f->f_dentry);
1007 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1008 BUG_ON(!PageLocked(pg));
1010 if (!PageLocked(pg))
1015 /* FIXME: Can kmap fail? */
1017 yaffs_gross_lock(dev);
1019 ret = yaffs_file_rd(obj, pg_buf,
1020 pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
1022 yaffs_gross_unlock(dev);
1028 ClearPageUptodate(pg);
1031 SetPageUptodate(pg);
1035 flush_dcache_page(pg);
1038 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
1042 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1044 int ret = yaffs_readpage_nolock(f, pg);
1049 static int yaffs_readpage(struct file *f, struct page *pg)
1053 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
1054 ret = yaffs_readpage_unlock(f, pg);
1055 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
1059 /* writepage inspired by/stolen from smbfs */
1061 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1062 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1064 static int yaffs_writepage(struct page *page)
1067 struct yaffs_dev *dev;
1068 struct address_space *mapping = page->mapping;
1069 struct inode *inode;
1070 unsigned long end_index;
1072 struct yaffs_obj *obj;
1079 inode = mapping->host;
1082 i_size = i_size_read(inode);
1084 end_index = i_size >> PAGE_CACHE_SHIFT;
1086 if (page->index < end_index)
1087 n_bytes = PAGE_CACHE_SIZE;
1089 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
1091 if (page->index > end_index || !n_bytes) {
1094 ("yaffs_writepage at %08x, inode size = %08x!!!\n"),
1095 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1096 (unsigned)inode->i_size));
1098 (TSTR(" -> don't care!!\n")));
1100 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1101 set_page_writeback(page);
1103 end_page_writeback(page);
1108 if (n_bytes != PAGE_CACHE_SIZE)
1109 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
1113 buffer = kmap(page);
1115 obj = yaffs_inode_to_obj(inode);
1117 yaffs_gross_lock(dev);
1120 (TSTR("yaffs_writepage at %08x, size %08x\n"),
1121 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes));
1123 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
1124 (int)obj->variant.file_variant.file_size, (int)inode->i_size));
1126 n_written = yaffs_wr_file(obj, buffer,
1127 page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
1129 yaffs_touch_super(dev);
1132 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
1133 (int)obj->variant.file_variant.file_size, (int)inode->i_size));
1135 yaffs_gross_unlock(dev);
1138 set_page_writeback(page);
1140 end_page_writeback(page);
1143 return (n_written == n_bytes) ? 0 : -ENOSPC;
1146 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1147 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1148 loff_t pos, unsigned len, unsigned flags,
1149 struct page **pagep, void **fsdata)
1151 struct page *pg = NULL;
1152 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1158 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1159 pg = grab_cache_page_write_begin(mapping, index, flags);
1161 pg = __grab_cache_page(mapping, index);
1170 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
1171 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0));
1174 space_held = yaffs_hold_space(filp);
1181 /* Update page if required */
1183 if (!Page_Uptodate(pg))
1184 ret = yaffs_readpage_nolock(filp, pg);
1189 /* Happy path return */
1190 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
1196 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
1198 yaffs_release_space(filp);
1201 page_cache_release(pg);
1208 static int yaffs_prepare_write(struct file *f, struct page *pg,
1209 unsigned offset, unsigned to)
1211 T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
1213 if (!Page_Uptodate(pg))
1214 return yaffs_readpage_nolock(f, pg);
1219 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1220 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1221 loff_t pos, unsigned len, unsigned copied,
1222 struct page *pg, void *fsdadata)
1226 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1229 addr = kva + offset_into_page;
1232 ("yaffs_write_end addr %p pos %x n_bytes %d\n",
1233 addr, (unsigned)pos, copied));
1235 ret = yaffs_file_write(filp, addr, copied, &pos);
1237 if (ret != copied) {
1239 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
1248 yaffs_release_space(filp);
1250 page_cache_release(pg);
1255 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1260 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1261 int n_bytes = to - offset;
1264 unsigned spos = pos;
1268 addr = kva + offset;
1270 saddr = (unsigned)addr;
1273 (TSTR("yaffs_commit_write addr %x pos %x n_bytes %d\n"),
1274 saddr, spos, n_bytes));
1276 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
1278 if (n_written != n_bytes) {
1281 ("yaffs_commit_write not same size n_written %d n_bytes %d\n"),
1282 n_written, n_bytes));
1291 (TSTR("yaffs_commit_write returning %d\n"),
1292 n_written == n_bytes ? 0 : n_written));
1294 return n_written == n_bytes ? 0 : n_written;
1298 static void yaffs_fill_inode_from_obj(struct inode *inode,
1299 struct yaffs_obj *obj)
1303 /* Check mode against the variant type and attempt to repair if broken. */
1304 u32 mode = obj->yst_mode;
1305 switch (obj->variant_type) {
1306 case YAFFS_OBJECT_TYPE_FILE:
1307 if (!S_ISREG(mode)) {
1308 obj->yst_mode &= ~S_IFMT;
1309 obj->yst_mode |= S_IFREG;
1313 case YAFFS_OBJECT_TYPE_SYMLINK:
1314 if (!S_ISLNK(mode)) {
1315 obj->yst_mode &= ~S_IFMT;
1316 obj->yst_mode |= S_IFLNK;
1320 case YAFFS_OBJECT_TYPE_DIRECTORY:
1321 if (!S_ISDIR(mode)) {
1322 obj->yst_mode &= ~S_IFMT;
1323 obj->yst_mode |= S_IFDIR;
1327 case YAFFS_OBJECT_TYPE_UNKNOWN:
1328 case YAFFS_OBJECT_TYPE_HARDLINK:
1329 case YAFFS_OBJECT_TYPE_SPECIAL:
1335 inode->i_flags |= S_NOATIME;
1337 inode->i_ino = obj->obj_id;
1338 inode->i_mode = obj->yst_mode;
1339 inode->i_uid = obj->yst_uid;
1340 inode->i_gid = obj->yst_gid;
1341 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1342 inode->i_blksize = inode->i_sb->s_blocksize;
1344 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1346 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1347 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1348 inode->i_atime.tv_nsec = 0;
1349 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1350 inode->i_mtime.tv_nsec = 0;
1351 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1352 inode->i_ctime.tv_nsec = 0;
1354 inode->i_rdev = obj->yst_rdev;
1355 inode->i_atime = obj->yst_atime;
1356 inode->i_mtime = obj->yst_mtime;
1357 inode->i_ctime = obj->yst_ctime;
1359 inode->i_size = yaffs_get_obj_length(obj);
1360 inode->i_blocks = (inode->i_size + 511) >> 9;
1362 inode->i_nlink = yaffs_get_obj_link_count(obj);
1366 ("yaffs_fill_inode mode %x uid %d gid %d size %d count %d\n"),
1367 inode->i_mode, inode->i_uid, inode->i_gid,
1368 (int)inode->i_size, atomic_read(&inode->i_count)));
1370 switch (obj->yst_mode & S_IFMT) {
1371 default: /* fifo, device or socket */
1372 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1373 init_special_inode(inode, obj->yst_mode,
1374 old_decode_dev(obj->yst_rdev));
1376 init_special_inode(inode, obj->yst_mode,
1377 (dev_t) (obj->yst_rdev));
1380 case S_IFREG: /* file */
1381 inode->i_op = &yaffs_file_inode_operations;
1382 inode->i_fop = &yaffs_file_operations;
1383 inode->i_mapping->a_ops =
1384 &yaffs_file_address_operations;
1386 case S_IFDIR: /* directory */
1387 inode->i_op = &yaffs_dir_inode_operations;
1388 inode->i_fop = &yaffs_dir_operations;
1390 case S_IFLNK: /* symlink */
1391 inode->i_op = &yaffs_symlink_inode_operations;
1395 yaffs_inode_to_obj_lv(inode) = obj;
1397 obj->my_inode = inode;
1401 (TSTR("yaffs_fill_inode invalid parameters\n")));
1406 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1407 struct yaffs_obj *obj)
1409 struct inode *inode;
1413 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1420 (TSTR("yaffs_get_inode for NULL object!!\n")));
1426 (TSTR("yaffs_get_inode for object %d\n"), obj->obj_id));
1428 inode = Y_IGET(sb, obj->obj_id);
1432 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1433 /* iget also increments the inode's i_count */
1434 /* NB You can't be holding gross_lock or deadlock will happen! */
1439 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1442 struct yaffs_obj *obj;
1443 int n_written, ipos;
1444 struct inode *inode;
1445 struct yaffs_dev *dev;
1447 obj = yaffs_dentry_to_obj(f->f_dentry);
1451 yaffs_gross_lock(dev);
1453 inode = f->f_dentry->d_inode;
1455 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1456 ipos = inode->i_size;
1462 (TSTR("yaffs_file_write: hey obj is null!\n")));
1465 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1466 "to object %d at %d(%x)\n"),
1467 (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos));
1469 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1471 yaffs_touch_super(dev);
1474 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1475 (unsigned)n, (unsigned)n));
1477 if (n_written > 0) {
1480 if (ipos > inode->i_size) {
1481 inode->i_size = ipos;
1482 inode->i_blocks = (ipos + 511) >> 9;
1485 (TSTR("yaffs_file_write size updated to %d bytes, "
1486 "%d blocks\n"), ipos, (int)(inode->i_blocks)));
1490 yaffs_gross_unlock(dev);
1491 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1494 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1495 /* For now we just assume few parallel writes and check against a small number. */
1496 /* Todo: need to do this with a counter to handle parallel reads better */
1498 static ssize_t yaffs_hold_space(struct file *f)
1500 struct yaffs_obj *obj;
1501 struct yaffs_dev *dev;
1505 obj = yaffs_dentry_to_obj(f->f_dentry);
1509 yaffs_gross_lock(dev);
1511 n_free_chunks = yaffs_get_n_free_chunks(dev);
1513 yaffs_gross_unlock(dev);
1515 return (n_free_chunks > 20) ? 1 : 0;
1518 static void yaffs_release_space(struct file *f)
1520 struct yaffs_obj *obj;
1521 struct yaffs_dev *dev;
1523 obj = yaffs_dentry_to_obj(f->f_dentry);
1527 yaffs_gross_lock(dev);
1529 yaffs_gross_unlock(dev);
1532 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1540 offset += i_size_read(file->f_path.dentry->d_inode);
1543 offset += file->f_pos;
1548 if (offset != file->f_pos)
1549 file->f_pos = offset;
1557 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1559 struct yaffs_obj *obj;
1560 struct yaffs_dev *dev;
1561 struct yaffs_search_context *sc;
1562 struct inode *inode = f->f_dentry->d_inode;
1563 unsigned long offset, curoffs;
1564 struct yaffs_obj *l;
1567 char name[YAFFS_MAX_NAME_LENGTH + 1];
1569 obj = yaffs_dentry_to_obj(f->f_dentry);
1572 yaffs_gross_lock(dev);
1574 yaffs_dev_to_lc(dev)->readdir_process = current;
1578 sc = yaffs_new_search(obj);
1585 (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1589 (TSTR("yaffs_readdir: entry . ino %d \n"),
1590 (int)inode->i_ino));
1591 yaffs_gross_unlock(dev);
1592 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1593 yaffs_gross_lock(dev);
1596 yaffs_gross_lock(dev);
1602 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1603 (int)f->f_dentry->d_parent->d_inode->i_ino));
1604 yaffs_gross_unlock(dev);
1605 if (filldir(dirent, "..", 2, offset,
1606 f->f_dentry->d_parent->d_inode->i_ino,
1608 yaffs_gross_lock(dev);
1611 yaffs_gross_lock(dev);
1618 /* If the directory has changed since the open or last call to
1619 readdir, rewind to after the 2 canned entries. */
1620 if (f->f_version != inode->i_version) {
1623 f->f_version = inode->i_version;
1626 while (sc->next_return) {
1628 l = sc->next_return;
1629 if (curoffs >= offset) {
1630 int this_inode = yaffs_get_obj_inode(l);
1631 int this_type = yaffs_get_obj_type(l);
1633 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1635 (TSTR("yaffs_readdir: %s inode %d\n"),
1636 name, yaffs_get_obj_inode(l)));
1638 yaffs_gross_unlock(dev);
1643 offset, this_inode, this_type) < 0) {
1644 yaffs_gross_lock(dev);
1648 yaffs_gross_lock(dev);
1653 yaffs_search_advance(sc);
1657 yaffs_search_end(sc);
1658 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1659 yaffs_gross_unlock(dev);
1665 * File creation. Allocate an inode, and we're done..
1668 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1671 #define YCRED(x) (x->cred)
1674 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1675 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1678 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1682 struct inode *inode;
1684 struct yaffs_obj *obj = NULL;
1685 struct yaffs_dev *dev;
1687 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1689 int error = -ENOSPC;
1690 uid_t uid = YCRED(current)->fsuid;
1692 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1694 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1699 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1700 parent->obj_id, parent->variant_type));
1703 (TSTR("yaffs_mknod: could not get parent object\n")));
1707 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1708 "mode %x dev %x\n"),
1709 dentry->d_name.name, mode, rdev));
1711 dev = parent->my_dev;
1713 yaffs_gross_lock(dev);
1715 switch (mode & S_IFMT) {
1717 /* Special (socket, fifo, device...) */
1718 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1719 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1721 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1722 gid, old_encode_dev(rdev));
1725 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1729 case S_IFREG: /* file */
1730 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1731 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1734 case S_IFDIR: /* directory */
1735 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making directory\n")));
1736 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1739 case S_IFLNK: /* symlink */
1740 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1741 obj = NULL; /* Do we ever get here? */
1745 /* Can not call yaffs_get_inode() with gross lock held */
1746 yaffs_gross_unlock(dev);
1749 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1750 d_instantiate(dentry, inode);
1751 update_dir_time(dir);
1753 (TSTR("yaffs_mknod created object %d count = %d\n"),
1754 obj->obj_id, atomic_read(&inode->i_count)));
1756 yaffs_fill_inode_from_obj(dir, parent);
1758 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod failed making object\n")));
1765 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1768 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1769 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1773 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1774 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1775 struct nameidata *n)
1777 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1780 T(YAFFS_TRACE_OS, (TSTR("yaffs_create\n")));
1781 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1784 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1788 struct yaffs_dev *dev;
1789 struct yaffs_obj *obj;
1792 (TSTR("yaffs_unlink %d:%s\n"),
1793 (int)(dir->i_ino), dentry->d_name.name));
1794 obj = yaffs_inode_to_obj(dir);
1797 yaffs_gross_lock(dev);
1799 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1801 if (ret_val == YAFFS_OK) {
1802 dentry->d_inode->i_nlink--;
1804 yaffs_gross_unlock(dev);
1805 mark_inode_dirty(dentry->d_inode);
1806 update_dir_time(dir);
1809 yaffs_gross_unlock(dev);
1816 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1817 struct dentry *dentry)
1819 struct inode *inode = old_dentry->d_inode;
1820 struct yaffs_obj *obj = NULL;
1821 struct yaffs_obj *link = NULL;
1822 struct yaffs_dev *dev;
1824 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1826 obj = yaffs_inode_to_obj(inode);
1829 yaffs_gross_lock(dev);
1831 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1833 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1837 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
1838 d_instantiate(dentry, old_dentry->d_inode);
1839 atomic_inc(&old_dentry->d_inode->i_count);
1841 (TSTR("yaffs_link link count %d i_count %d\n"),
1842 old_dentry->d_inode->i_nlink,
1843 atomic_read(&old_dentry->d_inode->i_count)));
1846 yaffs_gross_unlock(dev);
1849 update_dir_time(dir);
1856 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1857 const char *symname)
1859 struct yaffs_obj *obj;
1860 struct yaffs_dev *dev;
1861 uid_t uid = YCRED(current)->fsuid;
1863 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1865 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1867 dev = yaffs_inode_to_obj(dir)->my_dev;
1868 yaffs_gross_lock(dev);
1869 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1870 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1871 yaffs_gross_unlock(dev);
1874 struct inode *inode;
1876 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1877 d_instantiate(dentry, inode);
1878 update_dir_time(dir);
1879 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1882 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1888 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1889 static int yaffs_sync_object(struct file *file, int datasync)
1891 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1896 struct yaffs_obj *obj;
1897 struct yaffs_dev *dev;
1898 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1899 struct dentry *dentry = file->f_path.dentry;
1902 obj = yaffs_dentry_to_obj(dentry);
1906 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, (TSTR("yaffs_sync_object\n")));
1907 yaffs_gross_lock(dev);
1908 yaffs_flush_file(obj, 1, datasync);
1909 yaffs_gross_unlock(dev);
1914 * The VFS layer already does all the dentry stuff for rename.
1916 * NB: POSIX says you can rename an object over an old object of the same name
1918 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1919 struct inode *new_dir, struct dentry *new_dentry)
1921 struct yaffs_dev *dev;
1922 int ret_val = YAFFS_FAIL;
1923 struct yaffs_obj *target;
1925 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1926 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1928 yaffs_gross_lock(dev);
1930 /* Check if the target is an existing directory that is not empty. */
1931 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1932 new_dentry->d_name.name);
1934 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1935 !list_empty(&target->variant.dir_variant.children)) {
1937 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1939 ret_val = YAFFS_FAIL;
1941 /* Now does unlinking internally using shadowing mechanism */
1942 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_rename_obj\n")));
1944 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1945 old_dentry->d_name.name,
1946 yaffs_inode_to_obj(new_dir),
1947 new_dentry->d_name.name);
1949 yaffs_gross_unlock(dev);
1951 if (ret_val == YAFFS_OK) {
1953 new_dentry->d_inode->i_nlink--;
1954 mark_inode_dirty(new_dentry->d_inode);
1957 update_dir_time(old_dir);
1958 if (old_dir != new_dir)
1959 update_dir_time(new_dir);
1966 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1968 struct inode *inode = dentry->d_inode;
1970 struct yaffs_dev *dev;
1973 (TSTR("yaffs_setattr of object %d\n"),
1974 yaffs_inode_to_obj(inode)->obj_id));
1976 /* Fail if a requested resize >= 2GB */
1977 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
1981 error = inode_change_ok(inode, attr);
1985 error = yaffs_vfs_setattr(inode, attr);
1986 T(YAFFS_TRACE_OS, (TSTR("inode_setattr called\n")));
1987 if (attr->ia_valid & ATTR_SIZE) {
1988 yaffs_vfs_setsize(inode, attr->ia_size);
1989 inode->i_blocks = (inode->i_size + 511) >> 9;
1992 dev = yaffs_inode_to_obj(inode)->my_dev;
1993 if (attr->ia_valid & ATTR_SIZE) {
1994 T(YAFFS_TRACE_OS, (TSTR("resize to %d(%x)\n"),
1995 (int)(attr->ia_size),
1996 (int)(attr->ia_size)));
1998 yaffs_gross_lock(dev);
1999 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
2000 if (result == YAFFS_OK) {
2005 yaffs_gross_unlock(dev);
2009 T(YAFFS_TRACE_OS, (TSTR("yaffs_setattr done returning %d\n"), error));
2014 #ifdef CONFIG_YAFFS_XATTR
2015 int yaffs_setxattr(struct dentry *dentry, const char *name,
2016 const void *value, size_t size, int flags)
2018 struct inode *inode = dentry->d_inode;
2020 struct yaffs_dev *dev;
2021 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2023 T(YAFFS_TRACE_OS, (TSTR("yaffs_setxattr of object %d\n"), obj->obj_id));
2028 yaffs_gross_lock(dev);
2029 result = yaffs_set_xattrib(obj, name, value, size, flags);
2030 if (result == YAFFS_OK)
2032 else if (result < 0)
2034 yaffs_gross_unlock(dev);
2037 T(YAFFS_TRACE_OS, (TSTR("yaffs_setxattr done returning %d\n"), error));
2042 ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, void *buff,
2045 struct inode *inode = dentry->d_inode;
2047 struct yaffs_dev *dev;
2048 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2051 (TSTR("yaffs_getxattr \"%s\" from object %d\n"), name, obj->obj_id));
2055 yaffs_gross_lock(dev);
2056 error = yaffs_get_xattrib(obj, name, buff, size);
2057 yaffs_gross_unlock(dev);
2060 T(YAFFS_TRACE_OS, (TSTR("yaffs_getxattr done returning %d\n"), error));
2065 int yaffs_removexattr(struct dentry *dentry, const char *name)
2067 struct inode *inode = dentry->d_inode;
2069 struct yaffs_dev *dev;
2070 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2073 (TSTR("yaffs_removexattr of object %d\n"), obj->obj_id));
2078 yaffs_gross_lock(dev);
2079 result = yaffs_remove_xattrib(obj, name);
2080 if (result == YAFFS_OK)
2082 else if (result < 0)
2084 yaffs_gross_unlock(dev);
2088 (TSTR("yaffs_removexattr done returning %d\n"), error));
2093 ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
2095 struct inode *inode = dentry->d_inode;
2097 struct yaffs_dev *dev;
2098 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2101 (TSTR("yaffs_listxattr of object %d\n"), obj->obj_id));
2105 yaffs_gross_lock(dev);
2106 error = yaffs_list_xattrib(obj, buff, size);
2107 yaffs_gross_unlock(dev);
2110 T(YAFFS_TRACE_OS, (TSTR("yaffs_listxattr done returning %d\n"), error));
2117 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2118 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2120 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2121 struct super_block *sb = dentry->d_sb;
2122 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2123 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2125 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2127 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2129 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2132 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
2134 yaffs_gross_lock(dev);
2136 buf->f_type = YAFFS_MAGIC;
2137 buf->f_bsize = sb->s_blocksize;
2138 buf->f_namelen = 255;
2140 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2141 /* Do this if chunk size is not a power of 2 */
2143 uint64_t bytes_in_dev;
2144 uint64_t bytes_free;
2148 ((dev->param.end_block - dev->param.start_block +
2149 1))) * ((uint64_t) (dev->param.chunks_per_block *
2150 dev->data_bytes_per_chunk));
2152 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2153 buf->f_blocks = bytes_in_dev;
2155 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
2156 ((uint64_t) (dev->data_bytes_per_chunk));
2158 do_div(bytes_free, sb->s_blocksize);
2160 buf->f_bfree = bytes_free;
2162 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2165 (dev->param.end_block - dev->param.start_block + 1) *
2166 dev->param.chunks_per_block /
2167 (sb->s_blocksize / dev->data_bytes_per_chunk);
2169 yaffs_get_n_free_chunks(dev) /
2170 (sb->s_blocksize / dev->data_bytes_per_chunk);
2173 (dev->param.end_block - dev->param.start_block + 1) *
2174 dev->param.chunks_per_block *
2175 (dev->data_bytes_per_chunk / sb->s_blocksize);
2178 yaffs_get_n_free_chunks(dev) *
2179 (dev->data_bytes_per_chunk / sb->s_blocksize);
2184 buf->f_bavail = buf->f_bfree;
2186 yaffs_gross_unlock(dev);
2190 static void yaffs_flush_inodes(struct super_block *sb)
2193 struct yaffs_obj *obj;
2195 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
2196 obj = yaffs_inode_to_obj(iptr);
2198 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
2200 yaffs_flush_file(obj, 1, 0);
2205 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2207 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2211 yaffs_flush_inodes(sb);
2212 yaffs_update_dirty_dirs(dev);
2213 yaffs_flush_whole_cache(dev);
2215 yaffs_checkpoint_save(dev);
2218 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2220 unsigned erased_chunks =
2221 dev->n_erased_blocks * dev->param.chunks_per_block;
2222 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2223 unsigned scattered = 0; /* Free chunks not in an erased block */
2225 if (erased_chunks < dev->n_free_chunks)
2226 scattered = (dev->n_free_chunks - erased_chunks);
2228 if (!context->bg_running)
2230 else if (scattered < (dev->param.chunks_per_block * 2))
2232 else if (erased_chunks > dev->n_free_chunks / 2)
2234 else if (erased_chunks > dev->n_free_chunks / 4)
2240 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
2243 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2244 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2245 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2248 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2249 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
2251 sb->s_dirt ? "dirty" : "clean",
2252 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2253 oneshot_checkpoint ? " one-shot" : ""));
2255 yaffs_gross_lock(dev);
2256 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2257 oneshot_checkpoint) && !dev->is_checkpointed;
2259 if (sb->s_dirt || do_checkpoint) {
2260 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2262 if (oneshot_checkpoint)
2263 yaffs_auto_checkpoint &= ~4;
2265 yaffs_gross_unlock(dev);
2271 * yaffs background thread functions .
2272 * yaffs_bg_thread_fn() the thread function
2273 * yaffs_bg_start() launches the background thread.
2274 * yaffs_bg_stop() cleans up the background thread.
2277 * The thread should only run after the yaffs is initialised
2278 * The thread should be stopped before yaffs is unmounted.
2279 * The thread should not do any writing while the fs is in read only.
2282 #ifdef YAFFS_COMPILE_BACKGROUND
2284 void yaffs_background_waker(unsigned long data)
2286 wake_up_process((struct task_struct *)data);
2289 static int yaffs_bg_thread_fn(void *data)
2291 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2292 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2293 unsigned long now = jiffies;
2294 unsigned long next_dir_update = now;
2295 unsigned long next_gc = now;
2296 unsigned long expires;
2297 unsigned int urgency;
2300 struct timer_list timer;
2302 T(YAFFS_TRACE_BACKGROUND,
2303 (TSTR("yaffs_background starting for dev %p\n"), (void *)dev));
2305 #ifdef YAFFS_COMPILE_FREEZER
2308 while (context->bg_running) {
2309 T(YAFFS_TRACE_BACKGROUND, (TSTR("yaffs_background\n")));
2311 if (kthread_should_stop())
2314 #ifdef YAFFS_COMPILE_FREEZER
2315 if (try_to_freeze())
2318 yaffs_gross_lock(dev);
2322 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
2323 yaffs_update_dirty_dirs(dev);
2324 next_dir_update = now + HZ;
2327 if (time_after(now, next_gc) && yaffs_bg_enable) {
2328 if (!dev->is_checkpointed) {
2329 urgency = yaffs_bg_gc_urgency(dev);
2330 gc_result = yaffs_bg_gc(dev, urgency);
2332 next_gc = now + HZ / 20 + 1;
2333 else if (urgency > 0)
2334 next_gc = now + HZ / 10 + 1;
2336 next_gc = now + HZ * 2;
2339 * gc not running so set to next_dir_update
2340 * to cut down on wake ups
2342 next_gc = next_dir_update;
2345 yaffs_gross_unlock(dev);
2347 expires = next_dir_update;
2348 if (time_before(next_gc, expires))
2350 if (time_before(expires, now))
2353 Y_INIT_TIMER(&timer);
2354 timer.expires = expires + 1;
2355 timer.data = (unsigned long)current;
2356 timer.function = yaffs_background_waker;
2358 set_current_state(TASK_INTERRUPTIBLE);
2361 del_timer_sync(&timer);
2370 static int yaffs_bg_start(struct yaffs_dev *dev)
2373 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2378 context->bg_running = 1;
2380 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2381 (void *)dev, "yaffs-bg-%d",
2384 if (IS_ERR(context->bg_thread)) {
2385 retval = PTR_ERR(context->bg_thread);
2386 context->bg_thread = NULL;
2387 context->bg_running = 0;
2392 static void yaffs_bg_stop(struct yaffs_dev *dev)
2394 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2396 ctxt->bg_running = 0;
2398 if (ctxt->bg_thread) {
2399 kthread_stop(ctxt->bg_thread);
2400 ctxt->bg_thread = NULL;
2404 static int yaffs_bg_thread_fn(void *data)
2409 static int yaffs_bg_start(struct yaffs_dev *dev)
2414 static void yaffs_bg_stop(struct yaffs_dev *dev)
2419 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2420 static void yaffs_write_super(struct super_block *sb)
2422 static int yaffs_write_super(struct super_block *sb)
2425 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2427 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2428 (TSTR("yaffs_write_super%s\n"),
2429 request_checkpoint ? " checkpt" : ""));
2431 yaffs_do_sync_fs(sb, request_checkpoint);
2433 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2438 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2439 static int yaffs_sync_fs(struct super_block *sb, int wait)
2441 static int yaffs_sync_fs(struct super_block *sb)
2444 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2446 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2447 (TSTR("yaffs_sync_fs%s\n"), request_checkpoint ? " checkpt" : ""));
2449 yaffs_do_sync_fs(sb, request_checkpoint);
2454 #ifdef YAFFS_USE_OWN_IGET
2456 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2458 struct inode *inode;
2459 struct yaffs_obj *obj;
2460 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2462 T(YAFFS_TRACE_OS, (TSTR("yaffs_iget for %lu\n"), ino));
2464 inode = iget_locked(sb, ino);
2466 return ERR_PTR(-ENOMEM);
2467 if (!(inode->i_state & I_NEW))
2470 /* NB This is called as a side effect of other functions, but
2471 * we had to release the lock to prevent deadlocks, so
2472 * need to lock again.
2475 yaffs_gross_lock(dev);
2477 obj = yaffs_find_by_number(dev, inode->i_ino);
2479 yaffs_fill_inode_from_obj(inode, obj);
2481 yaffs_gross_unlock(dev);
2483 unlock_new_inode(inode);
2489 static void yaffs_read_inode(struct inode *inode)
2491 /* NB This is called as a side effect of other functions, but
2492 * we had to release the lock to prevent deadlocks, so
2493 * need to lock again.
2496 struct yaffs_obj *obj;
2497 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
2500 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2502 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2503 yaffs_gross_lock(dev);
2505 obj = yaffs_find_by_number(dev, inode->i_ino);
2507 yaffs_fill_inode_from_obj(inode, obj);
2509 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2510 yaffs_gross_unlock(dev);
2515 static LIST_HEAD(yaffs_context_list);
2516 struct mutex yaffs_context_lock;
2518 static void yaffs_put_super(struct super_block *sb)
2520 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2522 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2524 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2525 (TSTR("Shutting down yaffs background thread\n")));
2527 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2528 (TSTR("yaffs background thread shut down\n")));
2530 yaffs_gross_lock(dev);
2532 yaffs_flush_super(sb, 1);
2534 if (yaffs_dev_to_lc(dev)->put_super_fn)
2535 yaffs_dev_to_lc(dev)->put_super_fn(sb);
2537 yaffs_deinitialise(dev);
2539 yaffs_gross_unlock(dev);
2541 mutex_lock(&yaffs_context_lock);
2542 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2543 mutex_unlock(&yaffs_context_lock);
2545 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2546 YFREE(yaffs_dev_to_lc(dev)->spare_buffer);
2547 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2553 static void yaffs_mtd_put_super(struct super_block *sb)
2555 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
2560 put_mtd_device(mtd);
2563 static void yaffs_touch_super(struct yaffs_dev *dev)
2565 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
2567 T(YAFFS_TRACE_OS, (TSTR("yaffs_touch_super() sb = %p\n"), sb));
2572 struct yaffs_options {
2574 int skip_checkpoint_read;
2575 int skip_checkpoint_write;
2578 int tags_ecc_overridden;
2579 int lazy_loading_enabled;
2580 int lazy_loading_overridden;
2581 int empty_lost_and_found;
2582 int empty_lost_and_found_overridden;
2585 #define MAX_OPT_LEN 30
2586 static int yaffs_parse_options(struct yaffs_options *options,
2587 const char *options_str)
2589 char cur_opt[MAX_OPT_LEN + 1];
2593 /* Parse through the options which is a comma seperated list */
2595 while (options_str && *options_str && !error) {
2596 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2599 while (*options_str == ',')
2602 while (*options_str && *options_str != ',') {
2603 if (p < MAX_OPT_LEN) {
2604 cur_opt[p] = *options_str;
2610 if (!strcmp(cur_opt, "inband-tags")) {
2611 options->inband_tags = 1;
2612 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2613 options->tags_ecc_on = 0;
2614 options->tags_ecc_overridden = 1;
2615 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2616 options->tags_ecc_on = 1;
2617 options->tags_ecc_overridden = 1;
2618 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2619 options->lazy_loading_enabled = 0;
2620 options->lazy_loading_overridden = 1;
2621 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2622 options->lazy_loading_enabled = 1;
2623 options->lazy_loading_overridden = 1;
2624 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2625 options->empty_lost_and_found = 0;
2626 options->empty_lost_and_found_overridden = 1;
2627 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2628 options->empty_lost_and_found = 1;
2629 options->empty_lost_and_found_overridden = 1;
2630 } else if (!strcmp(cur_opt, "no-cache")) {
2631 options->no_cache = 1;
2632 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2633 options->skip_checkpoint_read = 1;
2634 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2635 options->skip_checkpoint_write = 1;
2636 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2637 options->skip_checkpoint_read = 1;
2638 options->skip_checkpoint_write = 1;
2640 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2649 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2650 struct super_block *sb,
2651 void *data, int silent)
2654 struct inode *inode = NULL;
2655 struct dentry *root;
2656 struct yaffs_dev *dev = 0;
2657 char devname_buf[BDEVNAME_SIZE + 1];
2658 struct mtd_info *mtd;
2660 char *data_str = (char *)data;
2661 struct yaffs_linux_context *context = NULL;
2662 struct yaffs_param *param;
2666 struct yaffs_options options;
2670 struct yaffs_linux_context *context_iterator;
2671 struct list_head *l;
2673 sb->s_magic = YAFFS_MAGIC;
2674 sb->s_op = &yaffs_super_ops;
2675 sb->s_flags |= MS_NOATIME;
2677 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2679 #ifdef YAFFS_COMPILE_EXPORTFS
2680 sb->s_export_op = &yaffs_export_ops;
2684 printk(KERN_INFO "yaffs: sb is NULL\n");
2685 else if (!sb->s_dev)
2686 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2687 else if (!yaffs_devname(sb, devname_buf))
2688 printk(KERN_INFO "yaffs: devname is NULL\n");
2690 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2692 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2697 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2699 memset(&options, 0, sizeof(options));
2701 if (yaffs_parse_options(&options, data_str)) {
2702 /* Option parsing failed */
2706 sb->s_blocksize = PAGE_CACHE_SIZE;
2707 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2710 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffs_version));
2712 (TSTR("yaffs_read_super: block size %d\n"), (int)(sb->s_blocksize)));
2714 T(YAFFS_TRACE_ALWAYS,
2715 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2716 MAJOR(sb->s_dev), MINOR(sb->s_dev), yaffs_devname(sb, devname_buf)));
2718 /* Check it's an mtd device..... */
2719 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2720 return NULL; /* This isn't an mtd device */
2722 /* Get the device */
2723 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2725 T(YAFFS_TRACE_ALWAYS,
2726 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2730 /* Check it's NAND */
2731 if (mtd->type != MTD_NANDFLASH) {
2732 T(YAFFS_TRACE_ALWAYS,
2733 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2738 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2739 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2740 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2741 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2742 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2743 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2744 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2745 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2746 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2747 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2748 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2749 T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2751 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2754 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2756 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2757 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs2\n")));
2761 /* Added NCB 26/5/2006 for completeness */
2762 if (yaffs_version == 2 && !options.inband_tags
2763 && WRITE_SIZE(mtd) == 512) {
2764 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs1\n")));
2769 if (yaffs_version == 2) {
2770 /* Check for version 2 style functions */
2772 !mtd->block_isbad ||
2773 !mtd->block_markbad || !mtd->read || !mtd->write ||
2774 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2775 !mtd->read_oob || !mtd->write_oob) {
2778 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2780 T(YAFFS_TRACE_ALWAYS,
2781 (TSTR("yaffs: MTD device does not support required "
2786 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2787 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2788 !options.inband_tags) {
2789 T(YAFFS_TRACE_ALWAYS,
2790 (TSTR("yaffs: MTD device does not have the "
2791 "right page sizes\n")));
2795 /* Check for V1 style functions */
2796 if (!mtd->erase || !mtd->read || !mtd->write ||
2797 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2798 !mtd->read_oob || !mtd->write_oob) {
2801 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2803 T(YAFFS_TRACE_ALWAYS,
2804 (TSTR("yaffs: MTD device does not support required "
2809 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2810 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2811 T(YAFFS_TRACE_ALWAYS,
2812 (TSTR("yaffs: MTD device does not support have the "
2813 "right page sizes\n")));
2818 /* OK, so if we got here, we have an MTD that's NAND and looks
2819 * like it has the right capabilities
2820 * Set the struct yaffs_dev up for mtd
2823 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2826 "yaffs: mtd is read only, setting superblock read only");
2827 sb->s_flags |= MS_RDONLY;
2830 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2831 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2833 if (!dev || !context) {
2843 /* Deep shit could not allocate device structure */
2844 T(YAFFS_TRACE_ALWAYS,
2845 (TSTR("yaffs_read_super: Failed trying to allocate "
2846 "struct yaffs_dev. \n")));
2849 memset(dev, 0, sizeof(struct yaffs_dev));
2850 param = &(dev->param);
2852 memset(context, 0, sizeof(struct yaffs_linux_context));
2853 dev->os_context = context;
2854 INIT_LIST_HEAD(&(context->context_list));
2856 context->super = sb;
2858 dev->read_only = read_only;
2860 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2861 sb->s_fs_info = dev;
2863 sb->u.generic_sbp = dev;
2866 dev->driver_context = mtd;
2867 param->name = mtd->name;
2869 /* Set up the memory size parameters.... */
2872 YCALCBLOCKS(mtd->size,
2873 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2875 param->start_block = 0;
2876 param->end_block = n_blocks - 1;
2877 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2878 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2879 param->n_reserved_blocks = 5;
2880 param->n_caches = (options.no_cache) ? 0 : 10;
2881 param->inband_tags = options.inband_tags;
2883 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2884 param->disable_lazy_load = 1;
2886 #ifdef CONFIG_YAFFS_XATTR
2887 param->enable_xattr = 1;
2889 if (options.lazy_loading_overridden)
2890 param->disable_lazy_load = !options.lazy_loading_enabled;
2892 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2893 param->no_tags_ecc = 1;
2896 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2898 param->defered_dir_update = 1;
2901 if (options.tags_ecc_overridden)
2902 param->no_tags_ecc = !options.tags_ecc_on;
2904 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2905 param->empty_lost_n_found = 1;
2908 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2909 param->refresh_period = 0;
2911 param->refresh_period = 500;
2914 #ifdef CONFIG_YAFFS__ALWAYS_CHECK_CHUNK_ERASED
2915 param->always_check_erased = 1;
2918 if (options.empty_lost_and_found_overridden)
2919 param->empty_lost_n_found = options.empty_lost_and_found;
2921 /* ... and the functions. */
2922 if (yaffs_version == 2) {
2923 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2924 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2925 param->bad_block_fn = nandmtd2_mark_block_bad;
2926 param->query_block_fn = nandmtd2_query_block;
2927 yaffs_dev_to_lc(dev)->spare_buffer = YMALLOC(mtd->oobsize);
2928 param->is_yaffs2 = 1;
2929 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2930 param->total_bytes_per_chunk = mtd->writesize;
2931 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2933 param->total_bytes_per_chunk = mtd->oobblock;
2934 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
2936 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2938 param->start_block = 0;
2939 param->end_block = n_blocks - 1;
2941 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2942 /* use the MTD interface in yaffs_mtdif1.c */
2943 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2944 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2945 param->bad_block_fn = nandmtd1_mark_block_bad;
2946 param->query_block_fn = nandmtd1_query_block;
2948 param->write_chunk_fn = nandmtd_write_chunk;
2949 param->read_chunk_fn = nandmtd_read_chunk;
2951 param->is_yaffs2 = 0;
2953 /* ... and common functions */
2954 param->erase_fn = nandmtd_erase_block;
2955 param->initialise_flash_fn = nandmtd_initialise;
2957 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2959 param->sb_dirty_fn = yaffs_touch_super;
2960 param->gc_control = yaffs_gc_control_callback;
2962 yaffs_dev_to_lc(dev)->super = sb;
2964 #ifndef CONFIG_YAFFS_DOES_ECC
2965 param->use_nand_ecc = 1;
2968 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2969 param->wide_tnodes_disabled = 1;
2972 param->skip_checkpt_rd = options.skip_checkpoint_read;
2973 param->skip_checkpt_wr = options.skip_checkpoint_write;
2975 mutex_lock(&yaffs_context_lock);
2976 /* Get a mount id */
2978 for (mount_id = 0; !found; mount_id++) {
2980 list_for_each(l, &yaffs_context_list) {
2982 list_entry(l, struct yaffs_linux_context,
2984 if (context_iterator->mount_id == mount_id)
2988 context->mount_id = mount_id;
2990 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2991 &yaffs_context_list);
2992 mutex_unlock(&yaffs_context_lock);
2994 /* Directory search handling... */
2995 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2996 param->remove_obj_fn = yaffs_remove_obj_callback;
2998 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
3000 yaffs_gross_lock(dev);
3002 err = yaffs_guts_initialise(dev);
3005 (TSTR("yaffs_read_super: guts initialised %s\n"),
3006 (err == YAFFS_OK) ? "OK" : "FAILED"));
3008 if (err == YAFFS_OK)
3009 yaffs_bg_start(dev);
3011 if (!context->bg_thread)
3012 param->defered_dir_update = 0;
3014 /* Release lock before yaffs_get_inode() */
3015 yaffs_gross_unlock(dev);
3017 /* Create root inode */
3018 if (err == YAFFS_OK)
3019 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
3024 inode->i_op = &yaffs_dir_inode_operations;
3025 inode->i_fop = &yaffs_dir_operations;
3027 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
3029 root = d_alloc_root(inode);
3031 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
3038 sb->s_dirt = !dev->is_checkpointed;
3039 T(YAFFS_TRACE_ALWAYS,
3040 (TSTR("yaffs_read_super: is_checkpointed %d\n"),
3041 dev->is_checkpointed));
3043 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
3047 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3048 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3051 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3054 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3055 static int yaffs_read_super(struct file_system_type *fs,
3056 int flags, const char *dev_name,
3057 void *data, struct vfsmount *mnt)
3060 return get_sb_bdev(fs, flags, dev_name, data,
3061 yaffs_internal_read_super_mtd, mnt);
3064 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3065 int flags, const char *dev_name,
3069 return get_sb_bdev(fs, flags, dev_name, data,
3070 yaffs_internal_read_super_mtd);
3074 static struct file_system_type yaffs_fs_type = {
3075 .owner = THIS_MODULE,
3077 .get_sb = yaffs_read_super,
3078 .kill_sb = kill_block_super,
3079 .fs_flags = FS_REQUIRES_DEV,
3082 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3085 return yaffs_internal_read_super(1, sb, data, silent);
3088 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3092 #ifdef CONFIG_YAFFS_YAFFS2
3094 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3095 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3098 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3101 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3102 static int yaffs2_read_super(struct file_system_type *fs,
3103 int flags, const char *dev_name, void *data,
3104 struct vfsmount *mnt)
3106 return get_sb_bdev(fs, flags, dev_name, data,
3107 yaffs2_internal_read_super_mtd, mnt);
3110 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3111 int flags, const char *dev_name,
3115 return get_sb_bdev(fs, flags, dev_name, data,
3116 yaffs2_internal_read_super_mtd);
3120 static struct file_system_type yaffs2_fs_type = {
3121 .owner = THIS_MODULE,
3123 .get_sb = yaffs2_read_super,
3124 .kill_sb = kill_block_super,
3125 .fs_flags = FS_REQUIRES_DEV,
3128 static struct super_block *yaffs2_read_super(struct super_block *sb,
3129 void *data, int silent)
3131 return yaffs_internal_read_super(2, sb, data, silent);
3134 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3138 #endif /* CONFIG_YAFFS_YAFFS2 */
3140 static struct proc_dir_entry *my_proc_entry;
3141 static struct proc_dir_entry *debug_proc_entry;
3143 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3146 sprintf(buf, "start_block.......... %d\n", dev->param.start_block);
3147 buf += sprintf(buf, "end_block............ %d\n", dev->param.end_block);
3149 sprintf(buf, "total_bytes_per_chunk %d\n",
3150 dev->param.total_bytes_per_chunk);
3152 sprintf(buf, "use_nand_ecc......... %d\n", dev->param.use_nand_ecc);
3154 sprintf(buf, "no_tags_ecc.......... %d\n", dev->param.no_tags_ecc);
3155 buf += sprintf(buf, "is_yaffs2............ %d\n", dev->param.is_yaffs2);
3157 sprintf(buf, "inband_tags.......... %d\n", dev->param.inband_tags);
3159 sprintf(buf, "empty_lost_n_found... %d\n",
3160 dev->param.empty_lost_n_found);
3162 sprintf(buf, "disable_lazy_load.... %d\n",
3163 dev->param.disable_lazy_load);
3165 sprintf(buf, "refresh_period....... %d\n",
3166 dev->param.refresh_period);
3167 buf += sprintf(buf, "n_caches............. %d\n", dev->param.n_caches);
3169 sprintf(buf, "n_reserved_blocks.... %d\n",
3170 dev->param.n_reserved_blocks);
3172 sprintf(buf, "always_check_erased.. %d\n",
3173 dev->param.always_check_erased);
3175 buf += sprintf(buf, "\n");
3180 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3183 sprintf(buf, "data_bytes_per_chunk. %d\n",
3184 dev->data_bytes_per_chunk);
3185 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3186 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3187 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3189 sprintf(buf, "blocks_in_checkpt.... %d\n", dev->blocks_in_checkpt);
3190 buf += sprintf(buf, "\n");
3191 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3192 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3193 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3194 buf += sprintf(buf, "\n");
3195 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3196 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3197 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3198 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3199 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3201 sprintf(buf, "passive_gc_count..... %u\n", dev->passive_gc_count);
3203 sprintf(buf, "oldest_dirty_gc_count %u\n",
3204 dev->oldest_dirty_gc_count);
3205 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3206 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3208 sprintf(buf, "n_retired_writes..... %u\n", dev->n_retired_writes);
3210 sprintf(buf, "n_retired_blocks..... %u\n", dev->n_retired_blocks);
3211 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3212 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3214 sprintf(buf, "n_tags_ecc_fixed..... %u\n", dev->n_tags_ecc_fixed);
3216 sprintf(buf, "n_tags_ecc_unfixed... %u\n", dev->n_tags_ecc_unfixed);
3217 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3218 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3220 sprintf(buf, "n_unlinked_files..... %u\n", dev->n_unlinked_files);
3221 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3222 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3227 static int yaffs_proc_read(char *page,
3229 off_t offset, int count, int *eof, void *data)
3231 struct list_head *item;
3236 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3237 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3238 * This also assumes the user has posted a read buffer large
3239 * enough to hold the complete output; but that's life in /proc.
3244 /* Print header first */
3248 "Multi-version YAFFS built:" __DATE__ " " __TIME__
3251 buf += sprintf(buf, "\n");
3255 mutex_lock(&yaffs_context_lock);
3257 /* Locate and print the Nth entry. Order N-squared but N is small. */
3258 list_for_each(item, &yaffs_context_list) {
3259 struct yaffs_linux_context *dc =
3260 list_entry(item, struct yaffs_linux_context,
3262 struct yaffs_dev *dev = dc->dev;
3264 if (n < (step & ~1)) {
3268 if ((step & 1) == 0) {
3270 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3272 buf = yaffs_dump_dev_part0(buf, dev);
3274 buf = yaffs_dump_dev_part1(buf, dev);
3279 mutex_unlock(&yaffs_context_lock);
3282 return buf - page < count ? buf - page : count;
3285 static int yaffs_stats_proc_read(char *page,
3287 off_t offset, int count, int *eof, void *data)
3289 struct list_head *item;
3293 mutex_lock(&yaffs_context_lock);
3295 /* Locate and print the Nth entry. Order N-squared but N is small. */
3296 list_for_each(item, &yaffs_context_list) {
3297 struct yaffs_linux_context *dc =
3298 list_entry(item, struct yaffs_linux_context, context_list);
3299 struct yaffs_dev *dev = dc->dev;
3304 dev->n_erased_blocks * dev->param.chunks_per_block;
3306 buf += sprintf(buf, "%d, %d, %d, %u, %u, %u, %u\n",
3307 n, dev->n_free_chunks, erased_chunks,
3308 dev->bg_gcs, dev->oldest_dirty_gc_count,
3309 dev->n_obj, dev->n_tnodes);
3311 mutex_unlock(&yaffs_context_lock);
3313 return buf - page < count ? buf - page : count;
3317 * Set the verbosity of the warnings and error messages.
3319 * Note that the names can only be a..z or _ with the current code.
3324 unsigned mask_bitfield;
3326 {"allocate", YAFFS_TRACE_ALLOCATE},
3327 {"always", YAFFS_TRACE_ALWAYS},
3328 {"background", YAFFS_TRACE_BACKGROUND},
3329 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3330 {"buffers", YAFFS_TRACE_BUFFERS},
3331 {"bug", YAFFS_TRACE_BUG},
3332 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3333 {"deletion", YAFFS_TRACE_DELETION},
3334 {"erase", YAFFS_TRACE_ERASE},
3335 {"error", YAFFS_TRACE_ERROR},
3336 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3337 {"gc", YAFFS_TRACE_GC},
3338 {"lock", YAFFS_TRACE_LOCK},
3339 {"mtd", YAFFS_TRACE_MTD},
3340 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3341 {"os", YAFFS_TRACE_OS},
3342 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3343 {"scan", YAFFS_TRACE_SCAN},
3344 {"mount", YAFFS_TRACE_MOUNT},
3345 {"tracing", YAFFS_TRACE_TRACING},
3346 {"sync", YAFFS_TRACE_SYNC},
3347 {"write", YAFFS_TRACE_WRITE},
3348 {"verify", YAFFS_TRACE_VERIFY},
3349 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3350 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3351 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3352 {"all", 0xffffffff},
3357 #define MAX_MASK_NAME_LENGTH 40
3358 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3359 unsigned long count, void *data)
3361 unsigned rg = 0, mask_bitfield;
3365 char substring[MAX_MASK_NAME_LENGTH + 1];
3371 rg = yaffs_trace_mask;
3373 while (!done && (pos < count)) {
3375 while ((pos < count) && isspace(buf[pos]))
3392 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3394 if (end > buf + pos) {
3395 mask_name = "numeral";
3396 len = end - (buf + pos);
3400 for (x = buf + pos, i = 0;
3401 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3402 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3404 substring[i] = '\0';
3406 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3407 if (strcmp(substring, mask_flags[i].mask_name)
3409 mask_name = mask_flags[i].mask_name;
3411 mask_flags[i].mask_bitfield;
3418 if (mask_name != NULL) {
3422 rg &= ~mask_bitfield;
3425 rg |= mask_bitfield;
3431 rg |= mask_bitfield;
3437 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3439 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3441 if (rg & YAFFS_TRACE_ALWAYS) {
3442 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3444 flag = ((rg & mask_flags[i].mask_bitfield) ==
3445 mask_flags[i].mask_bitfield) ? '+' : '-';
3446 printk(KERN_DEBUG "%c%s\n", flag,
3447 mask_flags[i].mask_name);
3454 static int yaffs_proc_write(struct file *file, const char *buf,
3455 unsigned long count, void *data)
3457 return yaffs_proc_write_trace_options(file, buf, count, data);
3460 /* Stuff to handle installation of file systems */
3461 struct file_system_to_install {
3462 struct file_system_type *fst;
3466 static struct file_system_to_install fs_to_install[] = {
3467 {&yaffs_fs_type, 0},
3468 {&yaffs2_fs_type, 0},
3472 static int __init init_yaffs_fs(void)
3475 struct file_system_to_install *fsinst;
3477 T(YAFFS_TRACE_ALWAYS,
3478 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3480 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3481 T(YAFFS_TRACE_ALWAYS,
3483 (" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3486 mutex_init(&yaffs_context_lock);
3488 /* Install the proc_fs entries */
3489 my_proc_entry = create_proc_entry("yaffs",
3490 S_IRUGO | S_IFREG, YPROC_ROOT);
3492 if (my_proc_entry) {
3493 my_proc_entry->write_proc = yaffs_proc_write;
3494 my_proc_entry->read_proc = yaffs_proc_read;
3495 my_proc_entry->data = NULL;
3500 debug_proc_entry = create_proc_entry("yaffs_stats",
3501 S_IRUGO | S_IFREG, YPROC_ROOT);
3503 if (debug_proc_entry) {
3504 debug_proc_entry->write_proc = NULL;
3505 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3506 debug_proc_entry->data = NULL;
3511 /* Now add the file system entries */
3513 fsinst = fs_to_install;
3515 while (fsinst->fst && !error) {
3516 error = register_filesystem(fsinst->fst);
3518 fsinst->installed = 1;
3522 /* Any errors? uninstall */
3524 fsinst = fs_to_install;
3526 while (fsinst->fst) {
3527 if (fsinst->installed) {
3528 unregister_filesystem(fsinst->fst);
3529 fsinst->installed = 0;
3538 static void __exit exit_yaffs_fs(void)
3541 struct file_system_to_install *fsinst;
3543 T(YAFFS_TRACE_ALWAYS,
3544 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3546 remove_proc_entry("yaffs", YPROC_ROOT);
3547 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3549 fsinst = fs_to_install;
3551 while (fsinst->fst) {
3552 if (fsinst->installed) {
3553 unregister_filesystem(fsinst->fst);
3554 fsinst->installed = 0;
3560 module_init(init_yaffs_fs)
3561 module_exit(exit_yaffs_fs)
3563 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3564 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3565 MODULE_LICENSE("GPL");