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 down(&(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 up(&(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;
2338 * gc not running so set to next_dir_update
2339 * to cut down on wake ups
2341 next_gc = next_dir_update;
2343 yaffs_gross_unlock(dev);
2345 expires = next_dir_update;
2346 if (time_before(next_gc, expires))
2348 if (time_before(expires, now))
2351 Y_INIT_TIMER(&timer);
2352 timer.expires = expires + 1;
2353 timer.data = (unsigned long)current;
2354 timer.function = yaffs_background_waker;
2356 set_current_state(TASK_INTERRUPTIBLE);
2359 del_timer_sync(&timer);
2368 static int yaffs_bg_start(struct yaffs_dev *dev)
2371 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2376 context->bg_running = 1;
2378 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2379 (void *)dev, "yaffs-bg-%d",
2382 if (IS_ERR(context->bg_thread)) {
2383 retval = PTR_ERR(context->bg_thread);
2384 context->bg_thread = NULL;
2385 context->bg_running = 0;
2390 static void yaffs_bg_stop(struct yaffs_dev *dev)
2392 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2394 ctxt->bg_running = 0;
2396 if (ctxt->bg_thread) {
2397 kthread_stop(ctxt->bg_thread);
2398 ctxt->bg_thread = NULL;
2402 static int yaffs_bg_thread_fn(void *data)
2407 static int yaffs_bg_start(struct yaffs_dev *dev)
2412 static void yaffs_bg_stop(struct yaffs_dev *dev)
2417 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2418 static void yaffs_write_super(struct super_block *sb)
2420 static int yaffs_write_super(struct super_block *sb)
2423 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2425 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2426 (TSTR("yaffs_write_super%s\n"),
2427 request_checkpoint ? " checkpt" : ""));
2429 yaffs_do_sync_fs(sb, request_checkpoint);
2431 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2436 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2437 static int yaffs_sync_fs(struct super_block *sb, int wait)
2439 static int yaffs_sync_fs(struct super_block *sb)
2442 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2444 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2445 (TSTR("yaffs_sync_fs%s\n"), request_checkpoint ? " checkpt" : ""));
2447 yaffs_do_sync_fs(sb, request_checkpoint);
2452 #ifdef YAFFS_USE_OWN_IGET
2454 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2456 struct inode *inode;
2457 struct yaffs_obj *obj;
2458 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2460 T(YAFFS_TRACE_OS, (TSTR("yaffs_iget for %lu\n"), ino));
2462 inode = iget_locked(sb, ino);
2464 return ERR_PTR(-ENOMEM);
2465 if (!(inode->i_state & I_NEW))
2468 /* NB This is called as a side effect of other functions, but
2469 * we had to release the lock to prevent deadlocks, so
2470 * need to lock again.
2473 yaffs_gross_lock(dev);
2475 obj = yaffs_find_by_number(dev, inode->i_ino);
2477 yaffs_fill_inode_from_obj(inode, obj);
2479 yaffs_gross_unlock(dev);
2481 unlock_new_inode(inode);
2487 static void yaffs_read_inode(struct inode *inode)
2489 /* NB This is called as a side effect of other functions, but
2490 * we had to release the lock to prevent deadlocks, so
2491 * need to lock again.
2494 struct yaffs_obj *obj;
2495 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
2498 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2500 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2501 yaffs_gross_lock(dev);
2503 obj = yaffs_find_by_number(dev, inode->i_ino);
2505 yaffs_fill_inode_from_obj(inode, obj);
2507 if (current != yaffs_dev_to_lc(dev)->readdir_process)
2508 yaffs_gross_unlock(dev);
2513 static LIST_HEAD(yaffs_context_list);
2514 struct semaphore yaffs_context_lock;
2516 static void yaffs_put_super(struct super_block *sb)
2518 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2520 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2522 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2523 (TSTR("Shutting down yaffs background thread\n")));
2525 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2526 (TSTR("yaffs background thread shut down\n")));
2528 yaffs_gross_lock(dev);
2530 yaffs_flush_super(sb, 1);
2532 if (yaffs_dev_to_lc(dev)->put_super_fn)
2533 yaffs_dev_to_lc(dev)->put_super_fn(sb);
2535 yaffs_deinitialise(dev);
2537 yaffs_gross_unlock(dev);
2539 down(&yaffs_context_lock);
2540 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2541 up(&yaffs_context_lock);
2543 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2544 YFREE(yaffs_dev_to_lc(dev)->spare_buffer);
2545 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2551 static void yaffs_mtd_put_super(struct super_block *sb)
2553 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
2558 put_mtd_device(mtd);
2561 static void yaffs_touch_super(struct yaffs_dev *dev)
2563 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
2565 T(YAFFS_TRACE_OS, (TSTR("yaffs_touch_super() sb = %p\n"), sb));
2570 struct yaffs_options {
2572 int skip_checkpoint_read;
2573 int skip_checkpoint_write;
2576 int tags_ecc_overridden;
2577 int lazy_loading_enabled;
2578 int lazy_loading_overridden;
2579 int empty_lost_and_found;
2580 int empty_lost_and_found_overridden;
2583 #define MAX_OPT_LEN 30
2584 static int yaffs_parse_options(struct yaffs_options *options,
2585 const char *options_str)
2587 char cur_opt[MAX_OPT_LEN + 1];
2591 /* Parse through the options which is a comma seperated list */
2593 while (options_str && *options_str && !error) {
2594 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2597 while (*options_str == ',')
2600 while (*options_str && *options_str != ',') {
2601 if (p < MAX_OPT_LEN) {
2602 cur_opt[p] = *options_str;
2608 if (!strcmp(cur_opt, "inband-tags"))
2609 options->inband_tags = 1;
2610 else if (!strcmp(cur_opt, "tags-ecc-off")) {
2611 options->tags_ecc_on = 0;
2612 options->tags_ecc_overridden = 1;
2613 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2614 options->tags_ecc_on = 1;
2615 options->tags_ecc_overridden = 1;
2616 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2617 options->lazy_loading_enabled = 0;
2618 options->lazy_loading_overridden = 1;
2619 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2620 options->lazy_loading_enabled = 1;
2621 options->lazy_loading_overridden = 1;
2622 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2623 options->empty_lost_and_found = 0;
2624 options->empty_lost_and_found_overridden = 1;
2625 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2626 options->empty_lost_and_found = 1;
2627 options->empty_lost_and_found_overridden = 1;
2628 } else if (!strcmp(cur_opt, "no-cache"))
2629 options->no_cache = 1;
2630 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2631 options->skip_checkpoint_read = 1;
2632 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2633 options->skip_checkpoint_write = 1;
2634 else if (!strcmp(cur_opt, "no-checkpoint")) {
2635 options->skip_checkpoint_read = 1;
2636 options->skip_checkpoint_write = 1;
2638 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2647 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2648 struct super_block *sb,
2649 void *data, int silent)
2652 struct inode *inode = NULL;
2653 struct dentry *root;
2654 struct yaffs_dev *dev = 0;
2655 char devname_buf[BDEVNAME_SIZE + 1];
2656 struct mtd_info *mtd;
2658 char *data_str = (char *)data;
2659 struct yaffs_linux_context *context = NULL;
2660 struct yaffs_param *param;
2664 struct yaffs_options options;
2668 struct yaffs_linux_context *context_iterator;
2669 struct list_head *l;
2671 sb->s_magic = YAFFS_MAGIC;
2672 sb->s_op = &yaffs_super_ops;
2673 sb->s_flags |= MS_NOATIME;
2675 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2677 #ifdef YAFFS_COMPILE_EXPORTFS
2678 sb->s_export_op = &yaffs_export_ops;
2682 printk(KERN_INFO "yaffs: sb is NULL\n");
2683 else if (!sb->s_dev)
2684 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2685 else if (!yaffs_devname(sb, devname_buf))
2686 printk(KERN_INFO "yaffs: devname is NULL\n");
2688 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2690 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2695 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2697 memset(&options, 0, sizeof(options));
2699 if (yaffs_parse_options(&options, data_str)) {
2700 /* Option parsing failed */
2704 sb->s_blocksize = PAGE_CACHE_SIZE;
2705 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2708 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffs_version));
2710 (TSTR("yaffs_read_super: block size %d\n"), (int)(sb->s_blocksize)));
2712 T(YAFFS_TRACE_ALWAYS,
2713 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2714 MAJOR(sb->s_dev), MINOR(sb->s_dev), yaffs_devname(sb, devname_buf)));
2716 /* Check it's an mtd device..... */
2717 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2718 return NULL; /* This isn't an mtd device */
2720 /* Get the device */
2721 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2723 T(YAFFS_TRACE_ALWAYS,
2724 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2728 /* Check it's NAND */
2729 if (mtd->type != MTD_NANDFLASH) {
2730 T(YAFFS_TRACE_ALWAYS,
2731 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2736 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2737 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2738 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2739 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2740 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2741 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2742 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2743 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2744 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2745 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2746 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2747 T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2749 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2752 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2754 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2755 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs2\n")));
2759 /* Added NCB 26/5/2006 for completeness */
2760 if (yaffs_version == 2 && !options.inband_tags
2761 && WRITE_SIZE(mtd) == 512) {
2762 T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs1\n")));
2767 if (yaffs_version == 2) {
2768 /* Check for version 2 style functions */
2770 !mtd->block_isbad ||
2771 !mtd->block_markbad || !mtd->read || !mtd->write ||
2772 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2773 !mtd->read_oob || !mtd->write_oob) {
2776 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2778 T(YAFFS_TRACE_ALWAYS,
2779 (TSTR("yaffs: MTD device does not support required "
2784 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2785 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2786 !options.inband_tags) {
2787 T(YAFFS_TRACE_ALWAYS,
2788 (TSTR("yaffs: MTD device does not have the "
2789 "right page sizes\n")));
2793 /* Check for V1 style functions */
2794 if (!mtd->erase || !mtd->read || !mtd->write ||
2795 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2796 !mtd->read_oob || !mtd->write_oob) {
2799 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2801 T(YAFFS_TRACE_ALWAYS,
2802 (TSTR("yaffs: MTD device does not support required "
2807 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2808 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2809 T(YAFFS_TRACE_ALWAYS,
2810 (TSTR("yaffs: MTD device does not support have the "
2811 "right page sizes\n")));
2816 /* OK, so if we got here, we have an MTD that's NAND and looks
2817 * like it has the right capabilities
2818 * Set the struct yaffs_dev up for mtd
2821 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2824 "yaffs: mtd is read only, setting superblock read only");
2825 sb->s_flags |= MS_RDONLY;
2828 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2829 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2831 if (!dev || !context) {
2841 /* Deep shit could not allocate device structure */
2842 T(YAFFS_TRACE_ALWAYS,
2843 (TSTR("yaffs_read_super: Failed trying to allocate "
2844 "struct yaffs_dev. \n")));
2847 memset(dev, 0, sizeof(struct yaffs_dev));
2848 param = &(dev->param);
2850 memset(context, 0, sizeof(struct yaffs_linux_context));
2851 dev->os_context = context;
2852 INIT_LIST_HEAD(&(context->context_list));
2854 context->super = sb;
2856 dev->read_only = read_only;
2858 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2859 sb->s_fs_info = dev;
2861 sb->u.generic_sbp = dev;
2864 dev->driver_context = mtd;
2865 param->name = mtd->name;
2867 /* Set up the memory size parameters.... */
2870 YCALCBLOCKS(mtd->size,
2871 (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2873 param->start_block = 0;
2874 param->end_block = n_blocks - 1;
2875 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2876 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2877 param->n_reserved_blocks = 5;
2878 param->n_caches = (options.no_cache) ? 0 : 10;
2879 param->inband_tags = options.inband_tags;
2881 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2882 param->disable_lazy_load = 1;
2884 #ifdef CONFIG_YAFFS_XATTR
2885 param->enable_xattr = 1;
2887 if (options.lazy_loading_overridden)
2888 param->disable_lazy_load = !options.lazy_loading_enabled;
2890 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2891 param->no_tags_ecc = 1;
2894 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2896 param->defered_dir_update = 1;
2899 if (options.tags_ecc_overridden)
2900 param->no_tags_ecc = !options.tags_ecc_on;
2902 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2903 param->empty_lost_n_found = 1;
2906 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2907 param->refresh_period = 0;
2909 param->refresh_period = 500;
2912 #ifdef CONFIG_YAFFS__ALWAYS_CHECK_CHUNK_ERASED
2913 param->always_check_erased = 1;
2916 if (options.empty_lost_and_found_overridden)
2917 param->empty_lost_n_found = options.empty_lost_and_found;
2919 /* ... and the functions. */
2920 if (yaffs_version == 2) {
2921 param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
2922 param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
2923 param->bad_block_fn = nandmtd2_mark_block_bad;
2924 param->query_block_fn = nandmtd2_query_block;
2925 yaffs_dev_to_lc(dev)->spare_buffer = YMALLOC(mtd->oobsize);
2926 param->is_yaffs2 = 1;
2927 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2928 param->total_bytes_per_chunk = mtd->writesize;
2929 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2931 param->total_bytes_per_chunk = mtd->oobblock;
2932 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
2934 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2936 param->start_block = 0;
2937 param->end_block = n_blocks - 1;
2939 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2940 /* use the MTD interface in yaffs_mtdif1.c */
2941 param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
2942 param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
2943 param->bad_block_fn = nandmtd1_mark_block_bad;
2944 param->query_block_fn = nandmtd1_query_block;
2946 param->write_chunk_fn = nandmtd_write_chunk;
2947 param->read_chunk_fn = nandmtd_read_chunk;
2949 param->is_yaffs2 = 0;
2951 /* ... and common functions */
2952 param->erase_fn = nandmtd_erase_block;
2953 param->initialise_flash_fn = nandmtd_initialise;
2955 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
2957 param->sb_dirty_fn = yaffs_touch_super;
2958 param->gc_control = yaffs_gc_control_callback;
2960 yaffs_dev_to_lc(dev)->super = sb;
2962 #ifndef CONFIG_YAFFS_DOES_ECC
2963 param->use_nand_ecc = 1;
2966 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2967 param->wide_tnodes_disabled = 1;
2970 param->skip_checkpt_rd = options.skip_checkpoint_read;
2971 param->skip_checkpt_wr = options.skip_checkpoint_write;
2973 down(&yaffs_context_lock);
2974 /* Get a mount id */
2976 for (mount_id = 0; !found; mount_id++) {
2978 list_for_each(l, &yaffs_context_list) {
2980 list_entry(l, struct yaffs_linux_context,
2982 if (context_iterator->mount_id == mount_id)
2986 context->mount_id = mount_id;
2988 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2989 &yaffs_context_list);
2990 up(&yaffs_context_lock);
2992 /* Directory search handling... */
2993 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2994 param->remove_obj_fn = yaffs_remove_obj_callback;
2996 init_MUTEX(&(yaffs_dev_to_lc(dev)->gross_lock));
2998 yaffs_gross_lock(dev);
3000 err = yaffs_guts_initialise(dev);
3003 (TSTR("yaffs_read_super: guts initialised %s\n"),
3004 (err == YAFFS_OK) ? "OK" : "FAILED"));
3006 if (err == YAFFS_OK)
3007 yaffs_bg_start(dev);
3009 if (!context->bg_thread)
3010 param->defered_dir_update = 0;
3012 /* Release lock before yaffs_get_inode() */
3013 yaffs_gross_unlock(dev);
3015 /* Create root inode */
3016 if (err == YAFFS_OK)
3017 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
3022 inode->i_op = &yaffs_dir_inode_operations;
3023 inode->i_fop = &yaffs_dir_operations;
3025 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
3027 root = d_alloc_root(inode);
3029 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
3036 sb->s_dirt = !dev->is_checkpointed;
3037 T(YAFFS_TRACE_ALWAYS,
3038 (TSTR("yaffs_read_super: is_checkpointed %d\n"),
3039 dev->is_checkpointed));
3041 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
3045 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3046 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3049 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3052 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3053 static int yaffs_read_super(struct file_system_type *fs,
3054 int flags, const char *dev_name,
3055 void *data, struct vfsmount *mnt)
3058 return get_sb_bdev(fs, flags, dev_name, data,
3059 yaffs_internal_read_super_mtd, mnt);
3062 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3063 int flags, const char *dev_name,
3067 return get_sb_bdev(fs, flags, dev_name, data,
3068 yaffs_internal_read_super_mtd);
3072 static struct file_system_type yaffs_fs_type = {
3073 .owner = THIS_MODULE,
3075 .get_sb = yaffs_read_super,
3076 .kill_sb = kill_block_super,
3077 .fs_flags = FS_REQUIRES_DEV,
3080 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3083 return yaffs_internal_read_super(1, sb, data, silent);
3086 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3090 #ifdef CONFIG_YAFFS_YAFFS2
3092 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3093 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3096 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3099 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3100 static int yaffs2_read_super(struct file_system_type *fs,
3101 int flags, const char *dev_name, void *data,
3102 struct vfsmount *mnt)
3104 return get_sb_bdev(fs, flags, dev_name, data,
3105 yaffs2_internal_read_super_mtd, mnt);
3108 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3109 int flags, const char *dev_name,
3113 return get_sb_bdev(fs, flags, dev_name, data,
3114 yaffs2_internal_read_super_mtd);
3118 static struct file_system_type yaffs2_fs_type = {
3119 .owner = THIS_MODULE,
3121 .get_sb = yaffs2_read_super,
3122 .kill_sb = kill_block_super,
3123 .fs_flags = FS_REQUIRES_DEV,
3126 static struct super_block *yaffs2_read_super(struct super_block *sb,
3127 void *data, int silent)
3129 return yaffs_internal_read_super(2, sb, data, silent);
3132 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3136 #endif /* CONFIG_YAFFS_YAFFS2 */
3138 static struct proc_dir_entry *my_proc_entry;
3139 static struct proc_dir_entry *debug_proc_entry;
3141 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3144 sprintf(buf, "start_block.......... %d\n", dev->param.start_block);
3145 buf += sprintf(buf, "end_block............ %d\n", dev->param.end_block);
3147 sprintf(buf, "total_bytes_per_chunk %d\n",
3148 dev->param.total_bytes_per_chunk);
3150 sprintf(buf, "use_nand_ecc......... %d\n", dev->param.use_nand_ecc);
3152 sprintf(buf, "no_tags_ecc.......... %d\n", dev->param.no_tags_ecc);
3153 buf += sprintf(buf, "is_yaffs2............ %d\n", dev->param.is_yaffs2);
3155 sprintf(buf, "inband_tags.......... %d\n", dev->param.inband_tags);
3157 sprintf(buf, "empty_lost_n_found... %d\n",
3158 dev->param.empty_lost_n_found);
3160 sprintf(buf, "disable_lazy_load.... %d\n",
3161 dev->param.disable_lazy_load);
3163 sprintf(buf, "refresh_period....... %d\n",
3164 dev->param.refresh_period);
3165 buf += sprintf(buf, "n_caches............. %d\n", dev->param.n_caches);
3167 sprintf(buf, "n_reserved_blocks.... %d\n",
3168 dev->param.n_reserved_blocks);
3170 sprintf(buf, "always_check_erased.. %d\n",
3171 dev->param.always_check_erased);
3173 buf += sprintf(buf, "\n");
3178 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3181 sprintf(buf, "data_bytes_per_chunk. %d\n",
3182 dev->data_bytes_per_chunk);
3183 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3184 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3185 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3187 sprintf(buf, "blocks_in_checkpt.... %d\n", dev->blocks_in_checkpt);
3188 buf += sprintf(buf, "\n");
3189 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3190 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3191 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3192 buf += sprintf(buf, "\n");
3193 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3194 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3195 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3196 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3197 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3199 sprintf(buf, "passive_gc_count..... %u\n", dev->passive_gc_count);
3201 sprintf(buf, "oldest_dirty_gc_count %u\n",
3202 dev->oldest_dirty_gc_count);
3203 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3204 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3206 sprintf(buf, "n_retired_writes..... %u\n", dev->n_retired_writes);
3208 sprintf(buf, "n_retired_blocks..... %u\n", dev->n_retired_blocks);
3209 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3210 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3212 sprintf(buf, "n_tags_ecc_fixed..... %u\n", dev->n_tags_ecc_fixed);
3214 sprintf(buf, "n_tags_ecc_unfixed... %u\n", dev->n_tags_ecc_unfixed);
3215 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3216 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3218 sprintf(buf, "n_unlinked_files..... %u\n", dev->n_unlinked_files);
3219 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3220 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3225 static int yaffs_proc_read(char *page,
3227 off_t offset, int count, int *eof, void *data)
3229 struct list_head *item;
3234 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3235 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3236 * This also assumes the user has posted a read buffer large
3237 * enough to hold the complete output; but that's life in /proc.
3242 /* Print header first */
3246 "Multi-version YAFFS built:" __DATE__ " " __TIME__
3249 buf += sprintf(buf, "\n");
3253 down(&yaffs_context_lock);
3255 /* Locate and print the Nth entry. Order N-squared but N is small. */
3256 list_for_each(item, &yaffs_context_list) {
3257 struct yaffs_linux_context *dc =
3258 list_entry(item, struct yaffs_linux_context,
3260 struct yaffs_dev *dev = dc->dev;
3262 if (n < (step & ~1)) {
3266 if ((step & 1) == 0) {
3268 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3270 buf = yaffs_dump_dev_part0(buf, dev);
3272 buf = yaffs_dump_dev_part1(buf, dev);
3276 up(&yaffs_context_lock);
3279 return buf - page < count ? buf - page : count;
3282 static int yaffs_stats_proc_read(char *page,
3284 off_t offset, int count, int *eof, void *data)
3286 struct list_head *item;
3290 down(&yaffs_context_lock);
3292 /* Locate and print the Nth entry. Order N-squared but N is small. */
3293 list_for_each(item, &yaffs_context_list) {
3294 struct yaffs_linux_context *dc =
3295 list_entry(item, struct yaffs_linux_context, context_list);
3296 struct yaffs_dev *dev = dc->dev;
3301 dev->n_erased_blocks * dev->param.chunks_per_block;
3303 buf += sprintf(buf, "%d, %d, %d, %u, %u, %u, %u\n",
3304 n, dev->n_free_chunks, erased_chunks,
3305 dev->bg_gcs, dev->oldest_dirty_gc_count,
3306 dev->n_obj, dev->n_tnodes);
3308 up(&yaffs_context_lock);
3310 return buf - page < count ? buf - page : count;
3314 * Set the verbosity of the warnings and error messages.
3316 * Note that the names can only be a..z or _ with the current code.
3321 unsigned mask_bitfield;
3324 "allocate", YAFFS_TRACE_ALLOCATE}, {
3325 "always", YAFFS_TRACE_ALWAYS}, {
3326 "background", YAFFS_TRACE_BACKGROUND}, {
3327 "bad_blocks", YAFFS_TRACE_BAD_BLOCKS}, {
3328 "buffers", YAFFS_TRACE_BUFFERS}, {
3329 "bug", YAFFS_TRACE_BUG}, {
3330 "checkpt", YAFFS_TRACE_CHECKPOINT}, {
3331 "deletion", YAFFS_TRACE_DELETION}, {
3332 "erase", YAFFS_TRACE_ERASE}, {
3333 "error", YAFFS_TRACE_ERROR}, {
3334 "gc_detail", YAFFS_TRACE_GC_DETAIL}, {
3335 "gc", YAFFS_TRACE_GC}, {
3336 "lock", YAFFS_TRACE_LOCK}, {
3337 "mtd", YAFFS_TRACE_MTD}, {
3338 "nandaccess", YAFFS_TRACE_NANDACCESS}, {
3339 "os", YAFFS_TRACE_OS}, {
3340 "scan_debug", YAFFS_TRACE_SCAN_DEBUG}, {
3341 "scan", YAFFS_TRACE_SCAN}, {
3342 "tracing", YAFFS_TRACE_TRACING}, {
3343 "sync", YAFFS_TRACE_SYNC}, {
3344 "write", YAFFS_TRACE_WRITE}, {
3345 "verify", YAFFS_TRACE_VERIFY}, {
3346 "verify_nand", YAFFS_TRACE_VERIFY_NAND}, {
3347 "verify_full", YAFFS_TRACE_VERIFY_FULL}, {
3348 "verify_all", YAFFS_TRACE_VERIFY_ALL}, {
3349 "all", 0xffffffff}, {
3353 #define MAX_MASK_NAME_LENGTH 40
3354 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3355 unsigned long count, void *data)
3357 unsigned rg = 0, mask_bitfield;
3361 char substring[MAX_MASK_NAME_LENGTH + 1];
3367 rg = yaffs_trace_mask;
3369 while (!done && (pos < count)) {
3371 while ((pos < count) && isspace(buf[pos]))
3388 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3390 if (end > buf + pos) {
3391 mask_name = "numeral";
3392 len = end - (buf + pos);
3396 for (x = buf + pos, i = 0;
3397 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3398 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3400 substring[i] = '\0';
3402 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3403 if (strcmp(substring, mask_flags[i].mask_name)
3405 mask_name = mask_flags[i].mask_name;
3407 mask_flags[i].mask_bitfield;
3414 if (mask_name != NULL) {
3418 rg &= ~mask_bitfield;
3421 rg |= mask_bitfield;
3427 rg |= mask_bitfield;
3433 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3435 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3437 if (rg & YAFFS_TRACE_ALWAYS) {
3438 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3440 flag = ((rg & mask_flags[i].mask_bitfield) ==
3441 mask_flags[i].mask_bitfield) ? '+' : '-';
3442 printk(KERN_DEBUG "%c%s\n", flag,
3443 mask_flags[i].mask_name);
3450 static int yaffs_proc_write(struct file *file, const char *buf,
3451 unsigned long count, void *data)
3453 return yaffs_proc_write_trace_options(file, buf, count, data);
3456 /* Stuff to handle installation of file systems */
3457 struct file_system_to_install {
3458 struct file_system_type *fst;
3462 static struct file_system_to_install fs_to_install[] = {
3463 {&yaffs_fs_type, 0},
3464 {&yaffs2_fs_type, 0},
3468 static int __init init_yaffs_fs(void)
3471 struct file_system_to_install *fsinst;
3473 T(YAFFS_TRACE_ALWAYS,
3474 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3476 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3477 T(YAFFS_TRACE_ALWAYS,
3479 (" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3482 init_MUTEX(&yaffs_context_lock);
3484 /* Install the proc_fs entries */
3485 my_proc_entry = create_proc_entry("yaffs",
3486 S_IRUGO | S_IFREG, YPROC_ROOT);
3488 if (my_proc_entry) {
3489 my_proc_entry->write_proc = yaffs_proc_write;
3490 my_proc_entry->read_proc = yaffs_proc_read;
3491 my_proc_entry->data = NULL;
3495 debug_proc_entry = create_proc_entry("yaffs_stats",
3496 S_IRUGO | S_IFREG, YPROC_ROOT);
3498 if (debug_proc_entry) {
3499 debug_proc_entry->write_proc = NULL;
3500 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3501 debug_proc_entry->data = NULL;
3505 /* Now add the file system entries */
3507 fsinst = fs_to_install;
3509 while (fsinst->fst && !error) {
3510 error = register_filesystem(fsinst->fst);
3512 fsinst->installed = 1;
3516 /* Any errors? uninstall */
3518 fsinst = fs_to_install;
3520 while (fsinst->fst) {
3521 if (fsinst->installed) {
3522 unregister_filesystem(fsinst->fst);
3523 fsinst->installed = 0;
3532 static void __exit exit_yaffs_fs(void)
3535 struct file_system_to_install *fsinst;
3537 T(YAFFS_TRACE_ALWAYS,
3538 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3540 remove_proc_entry("yaffs", YPROC_ROOT);
3541 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3543 fsinst = fs_to_install;
3545 while (fsinst->fst) {
3546 if (fsinst->installed) {
3547 unregister_filesystem(fsinst->fst);
3548 fsinst->installed = 0;
3554 module_init(init_yaffs_fs)
3555 module_exit(exit_yaffs_fs)
3557 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3558 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3559 MODULE_LICENSE("GPL");