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"
167 #include "yaffs_linux.h"
169 #include "yaffs_mtdif.h"
170 #include "yaffs_mtdif1.h"
171 #include "yaffs_mtdif2.h"
173 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
174 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
175 unsigned int yaffs_auto_checkpoint = 1;
176 unsigned int yaffs_gc_control = 1;
177 unsigned int yaffs_bg_enable = 1;
179 /* Module Parameters */
180 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
181 module_param(yaffs_trace_mask, uint, 0644);
182 module_param(yaffs_wr_attempts, uint, 0644);
183 module_param(yaffs_auto_checkpoint, uint, 0644);
184 module_param(yaffs_gc_control, uint, 0644);
185 module_param(yaffs_bg_enable, uint, 0644);
187 MODULE_PARM(yaffs_trace_mask, "i");
188 MODULE_PARM(yaffs_wr_attempts, "i");
189 MODULE_PARM(yaffs_auto_checkpoint, "i");
190 MODULE_PARM(yaffs_gc_control, "i");
193 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
194 /* use iget and read_inode */
195 #define Y_IGET(sb, inum) iget((sb), (inum))
196 static void yaffs_read_inode(struct inode *inode);
199 /* Call local equivalent */
200 #define YAFFS_USE_OWN_IGET
201 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
203 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
206 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
207 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
209 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
212 #define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
213 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
215 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
216 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
218 #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);
319 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
320 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
321 loff_t pos, unsigned len, unsigned flags,
322 struct page **pagep, void **fsdata);
323 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
324 loff_t pos, unsigned len, unsigned copied,
325 struct page *pg, void *fsdadata);
327 static int yaffs_prepare_write(struct file *f, struct page *pg,
328 unsigned offset, unsigned to);
329 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
334 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
336 #if (YAFFS_NEW_FOLLOW_LINK == 1)
337 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
338 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
340 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
343 static void yaffs_touch_super(struct yaffs_dev *dev);
345 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
347 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
350 static struct address_space_operations yaffs_file_address_operations = {
351 .readpage = yaffs_readpage,
352 .writepage = yaffs_writepage,
353 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
354 .write_begin = yaffs_write_begin,
355 .write_end = yaffs_write_end,
357 .prepare_write = yaffs_prepare_write,
358 .commit_write = yaffs_commit_write,
363 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
364 static const struct file_operations yaffs_file_operations = {
365 .read = do_sync_read,
366 .write = do_sync_write,
367 .aio_read = generic_file_aio_read,
368 .aio_write = generic_file_aio_write,
369 .mmap = generic_file_mmap,
370 .flush = yaffs_file_flush,
371 .fsync = yaffs_sync_object,
372 .splice_read = generic_file_splice_read,
373 .splice_write = generic_file_splice_write,
374 .llseek = generic_file_llseek,
377 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
379 static const struct file_operations yaffs_file_operations = {
380 .read = do_sync_read,
381 .write = do_sync_write,
382 .aio_read = generic_file_aio_read,
383 .aio_write = generic_file_aio_write,
384 .mmap = generic_file_mmap,
385 .flush = yaffs_file_flush,
386 .fsync = yaffs_sync_object,
387 .sendfile = generic_file_sendfile,
392 static const struct file_operations yaffs_file_operations = {
393 .read = generic_file_read,
394 .write = generic_file_write,
395 .mmap = generic_file_mmap,
396 .flush = yaffs_file_flush,
397 .fsync = yaffs_sync_object,
398 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
399 .sendfile = generic_file_sendfile,
404 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
405 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
407 void * kaddr = kmap_atomic(page, KM_USER0);
408 memset(kaddr + start, 0, end - start);
409 kunmap_atomic(kaddr, KM_USER0);
410 flush_dcache_page(page);
415 static const struct inode_operations yaffs_file_inode_operations = {
416 .setattr = yaffs_setattr,
417 #ifdef CONFIG_YAFFS_XATTR
418 .setxattr = yaffs_setxattr,
419 .getxattr = yaffs_getxattr,
420 .listxattr = yaffs_listxattr,
421 .removexattr = yaffs_removexattr,
425 static const struct inode_operations yaffs_symlink_inode_operations = {
426 .readlink = yaffs_readlink,
427 .follow_link = yaffs_follow_link,
428 #if (YAFFS_NEW_FOLLOW_LINK == 1)
429 .put_link = yaffs_put_link,
431 .setattr = yaffs_setattr,
432 #ifdef CONFIG_YAFFS_XATTR
433 .setxattr = yaffs_setxattr,
434 .getxattr = yaffs_getxattr,
435 .listxattr = yaffs_listxattr,
436 .removexattr = yaffs_removexattr,
440 static const struct inode_operations yaffs_dir_inode_operations = {
441 .create = yaffs_create,
442 .lookup = yaffs_lookup,
444 .unlink = yaffs_unlink,
445 .symlink = yaffs_symlink,
446 .mkdir = yaffs_mkdir,
447 .rmdir = yaffs_unlink,
448 .mknod = yaffs_mknod,
449 .rename = yaffs_rename,
450 .setattr = yaffs_setattr,
451 #ifdef CONFIG_YAFFS_XATTR
452 .setxattr = yaffs_setxattr,
453 .getxattr = yaffs_getxattr,
454 .listxattr = yaffs_listxattr,
455 .removexattr = yaffs_removexattr,
459 static const struct file_operations yaffs_dir_operations = {
460 .read = generic_read_dir,
461 .readdir = yaffs_readdir,
462 .fsync = yaffs_sync_object,
463 .llseek = yaffs_dir_llseek,
466 static const struct super_operations yaffs_super_ops = {
467 .statfs = yaffs_statfs,
469 #ifndef YAFFS_USE_OWN_IGET
470 .read_inode = yaffs_read_inode,
472 #ifdef YAFFS_HAS_PUT_INODE
473 .put_inode = yaffs_put_inode,
475 .put_super = yaffs_put_super,
476 #ifdef YAFFS_HAS_EVICT_INODE
477 .evict_inode = yaffs_evict_inode,
479 .delete_inode = yaffs_delete_inode,
480 .clear_inode = yaffs_clear_inode,
482 .sync_fs = yaffs_sync_fs,
483 .write_super = yaffs_write_super,
487 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
489 #ifdef YAFFS_USE_SETATTR_COPY
490 setattr_copy(inode,attr);
493 return inode_setattr(inode, attr);
498 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
500 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
501 truncate_setsize(inode,newsize);
504 truncate_inode_pages(&inode->i_data,newsize);
510 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
512 return yaffs_gc_control;
515 static void yaffs_gross_lock(struct yaffs_dev *dev)
517 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
518 down(&(yaffs_dev_to_lc(dev)->gross_lock));
519 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
522 static void yaffs_gross_unlock(struct yaffs_dev *dev)
524 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
525 up(&(yaffs_dev_to_lc(dev)->gross_lock));
528 #ifdef YAFFS_COMPILE_EXPORTFS
530 static struct inode *
531 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
533 return Y_IGET(sb, ino);
536 static struct dentry *
537 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
539 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
542 static struct dentry *
543 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
545 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
548 struct dentry *yaffs2_get_parent(struct dentry *dentry)
551 struct super_block *sb = dentry->d_inode->i_sb;
552 struct dentry *parent = ERR_PTR(-ENOENT);
554 unsigned long parent_ino;
555 struct yaffs_obj *d_obj;
556 struct yaffs_obj *parent_obj;
558 d_obj = yaffs_inode_to_obj(dentry->d_inode);
561 parent_obj = d_obj->parent;
563 parent_ino = yaffs_get_obj_inode(parent_obj);
564 inode = Y_IGET(sb, parent_ino);
567 parent = ERR_CAST(inode);
569 parent = d_obtain_alias(inode);
570 if (!IS_ERR(parent)) {
571 parent = ERR_PTR(-ENOMEM);
581 /* Just declare a zero structure as a NULL value implies
582 * using the default functions of exportfs.
585 static struct export_operations yaffs_export_ops =
587 .fh_to_dentry = yaffs2_fh_to_dentry,
588 .fh_to_parent = yaffs2_fh_to_parent,
589 .get_parent = yaffs2_get_parent,
594 /*-----------------------------------------------------------------*/
595 /* Directory search context allows us to unlock access to yaffs during
596 * filldir without causing problems with the directory being modified.
597 * This is similar to the tried and tested mechanism used in yaffs direct.
599 * A search context iterates along a doubly linked list of siblings in the
600 * directory. If the iterating object is deleted then this would corrupt
601 * the list iteration, likely causing a crash. The search context avoids
602 * this by using the remove_obj_fn to move the search context to the
603 * next object before the object is deleted.
605 * Many readdirs (and thus seach conexts) may be alive simulateously so
606 * each struct yaffs_dev has a list of these.
608 * A seach context lives for the duration of a readdir.
610 * All these functions must be called while yaffs is locked.
613 struct yaffs_search_context {
614 struct yaffs_dev *dev;
615 struct yaffs_obj *dir_obj;
616 struct yaffs_obj *next_return;
617 struct ylist_head others;
621 * yaffs_new_search() creates a new search context, initialises it and
622 * adds it to the device's search context list.
624 * Called at start of readdir.
626 static struct yaffs_search_context * yaffs_new_search(struct yaffs_obj *dir)
628 struct yaffs_dev *dev = dir->my_dev;
629 struct yaffs_search_context *sc = YMALLOC(sizeof(struct yaffs_search_context));
633 if( ylist_empty(&sc->dir_obj->variant.dir_variant.children))
634 sc->next_return = NULL;
636 sc->next_return = ylist_entry(
637 dir->variant.dir_variant.children.next,
638 struct yaffs_obj,siblings);
639 YINIT_LIST_HEAD(&sc->others);
640 ylist_add(&sc->others,&(yaffs_dev_to_lc(dev)->search_contexts));
646 * yaffs_search_end() disposes of a search context and cleans up.
648 static void yaffs_search_end(struct yaffs_search_context * sc)
651 ylist_del(&sc->others);
657 * yaffs_search_advance() moves a search context to the next object.
658 * Called when the search iterates or when an object removal causes
659 * the search context to be moved to the next object.
661 static void yaffs_search_advance(struct yaffs_search_context *sc)
666 if( sc->next_return == NULL ||
667 ylist_empty(&sc->dir_obj->variant.dir_variant.children))
668 sc->next_return = NULL;
670 struct ylist_head *next = sc->next_return->siblings.next;
672 if( next == &sc->dir_obj->variant.dir_variant.children)
673 sc->next_return = NULL; /* end of list */
675 sc->next_return = ylist_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)
687 struct ylist_head *i;
688 struct yaffs_search_context *sc;
689 struct ylist_head *search_contexts = &(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 ylist_for_each(i, search_contexts) {
698 sc = ylist_entry(i, struct yaffs_search_context,others);
699 if(sc->next_return == obj)
700 yaffs_search_advance(sc);
707 /*-----------------------------------------------------------------*/
709 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
712 unsigned char *alias;
715 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
717 yaffs_gross_lock(dev);
719 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
721 yaffs_gross_unlock(dev);
726 ret = vfs_readlink(dentry, buffer, buflen, alias);
731 #if (YAFFS_NEW_FOLLOW_LINK == 1)
732 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
736 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
740 unsigned char *alias;
742 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
744 yaffs_gross_lock(dev);
746 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
747 yaffs_gross_unlock(dev);
754 #if (YAFFS_NEW_FOLLOW_LINK == 1)
755 nd_set_link(nd, alias);
759 ret = ERR_PTR(ret_int);
762 ret = vfs_follow_link(nd, alias);
771 #if (YAFFS_NEW_FOLLOW_LINK == 1)
772 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) {
777 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
778 struct yaffs_obj *obj);
781 * Lookup is used to find objects in the fs
783 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
785 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
788 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
791 struct yaffs_obj *obj;
792 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
794 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
796 if(current != yaffs_dev_to_lc(dev)->readdir_process)
797 yaffs_gross_lock(dev);
800 (TSTR("yaffs_lookup for %d:%s\n"),
801 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name));
803 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir),
804 dentry->d_name.name);
806 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
808 /* Can't hold gross lock when calling yaffs_get_inode() */
809 if(current != yaffs_dev_to_lc(dev)->readdir_process)
810 yaffs_gross_unlock(dev);
814 (TSTR("yaffs_lookup found %d\n"), obj->obj_id));
816 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
820 (TSTR("yaffs_loookup dentry \n")));
821 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
822 * d_add even if NULL inode */
824 /*dget(dentry); // try to solve directory bug */
825 d_add(dentry, inode);
833 T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
837 /* added NCB for 2.5/6 compatability - forces add even if inode is
838 * NULL which creates dentry hash */
839 d_add(dentry, inode);
845 #ifdef YAFFS_HAS_PUT_INODE
847 /* For now put inode is just for debugging
848 * Put inode is called when the inode **structure** is put.
850 static void yaffs_put_inode(struct inode *inode)
853 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
854 atomic_read(&inode->i_count)));
860 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
862 /* Clear the association between the inode and
863 * the struct yaffs_obj.
865 obj->my_inode = NULL;
866 yaffs_inode_to_obj_lv(inode) = NULL;
868 /* If the object freeing was deferred, then the real
870 * This should fix the inode inconsistency problem.
872 yaffs_handle_defered_free(obj);
875 #ifdef YAFFS_HAS_EVICT_INODE
876 /* yaffs_evict_inode combines into one operation what was previously done in
877 * yaffs_clear_inode() and yaffs_delete_inode()
880 static void yaffs_evict_inode( struct inode *inode)
882 struct yaffs_obj *obj;
883 struct yaffs_dev *dev;
886 obj = yaffs_inode_to_obj(inode);
889 (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
890 atomic_read(&inode->i_count),
891 obj ? "object exists" : "null object"));
893 if (!inode->i_nlink && !is_bad_inode(inode))
895 truncate_inode_pages(&inode->i_data,0);
896 end_writeback(inode);
900 yaffs_gross_lock(dev);
902 yaffs_gross_unlock(dev);
906 yaffs_gross_lock(dev);
907 yaffs_unstitch_obj(inode,obj);
908 yaffs_gross_unlock(dev);
915 /* clear is called to tell the fs to release any per-inode data it holds.
916 * The object might still exist on disk and is just being thrown out of the cache
917 * or else the object has actually been deleted and we're being called via
919 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
922 static void yaffs_clear_inode(struct inode *inode)
924 struct yaffs_obj *obj;
925 struct yaffs_dev *dev;
927 obj = yaffs_inode_to_obj(inode);
930 (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
931 atomic_read(&inode->i_count),
932 obj ? "object exists" : "null object"));
936 yaffs_gross_lock(dev);
937 yaffs_unstitch_obj(inode,obj);
938 yaffs_gross_unlock(dev);
943 /* delete is called when the link count is zero and the inode
944 * is put (ie. nobody wants to know about it anymore, time to
946 * NB Must call clear_inode()
948 static void yaffs_delete_inode(struct inode *inode)
950 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
951 struct yaffs_dev *dev;
954 (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
955 atomic_read(&inode->i_count),
956 obj ? "object exists" : "null object"));
960 yaffs_gross_lock(dev);
962 yaffs_gross_unlock(dev);
964 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
965 truncate_inode_pages(&inode->i_data, 0);
972 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
973 static int yaffs_file_flush(struct file *file, fl_owner_t id)
975 static int yaffs_file_flush(struct file *file)
978 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
980 struct yaffs_dev *dev = obj->my_dev;
983 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->obj_id,
984 obj->dirty ? "dirty" : "clean"));
986 yaffs_gross_lock(dev);
988 yaffs_flush_file(obj, 1, 0);
990 yaffs_gross_unlock(dev);
995 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
997 /* Lifted from jffs2 */
999 struct yaffs_obj *obj;
1000 unsigned char *pg_buf;
1003 struct yaffs_dev *dev;
1006 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
1007 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1008 (unsigned)PAGE_CACHE_SIZE));
1010 obj = yaffs_dentry_to_obj(f->f_dentry);
1014 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1015 BUG_ON(!PageLocked(pg));
1017 if (!PageLocked(pg))
1022 /* FIXME: Can kmap fail? */
1024 yaffs_gross_lock(dev);
1026 ret = yaffs_file_rd(obj, pg_buf,
1027 pg->index << PAGE_CACHE_SHIFT,
1030 yaffs_gross_unlock(dev);
1036 ClearPageUptodate(pg);
1039 SetPageUptodate(pg);
1043 flush_dcache_page(pg);
1046 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
1050 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1052 int ret = yaffs_readpage_nolock(f, pg);
1057 static int yaffs_readpage(struct file *f, struct page *pg)
1061 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
1062 ret=yaffs_readpage_unlock(f, pg);
1063 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
1067 /* writepage inspired by/stolen from smbfs */
1069 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1070 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1072 static int yaffs_writepage(struct page *page)
1075 struct yaffs_dev *dev;
1076 struct address_space *mapping = page->mapping;
1077 struct inode *inode;
1078 unsigned long end_index;
1080 struct yaffs_obj *obj;
1087 inode = mapping->host;
1090 i_size = i_size_read(inode);
1092 end_index = i_size >> PAGE_CACHE_SHIFT;
1094 if(page->index < end_index)
1095 n_bytes = PAGE_CACHE_SIZE;
1097 n_bytes = i_size & (PAGE_CACHE_SIZE -1);
1099 if (page->index > end_index || !n_bytes) {
1101 (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
1102 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1103 (unsigned)inode->i_size));
1105 (TSTR(" -> don't care!!\n")));
1107 zero_user_segment(page,0,PAGE_CACHE_SIZE);
1108 set_page_writeback(page);
1110 end_page_writeback(page);
1115 if(n_bytes != PAGE_CACHE_SIZE)
1116 zero_user_segment(page,n_bytes,PAGE_CACHE_SIZE);
1120 buffer = kmap(page);
1122 obj = yaffs_inode_to_obj(inode);
1124 yaffs_gross_lock(dev);
1127 (TSTR("yaffs_writepage at %08x, size %08x\n"),
1128 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes));
1130 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
1131 (int)obj->variant.file_variant.file_size, (int)inode->i_size));
1133 n_written = yaffs_wr_file(obj, buffer,
1134 page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
1136 yaffs_touch_super(dev);
1139 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
1140 (int)obj->variant.file_variant.file_size, (int)inode->i_size));
1142 yaffs_gross_unlock(dev);
1145 set_page_writeback(page);
1147 end_page_writeback(page);
1150 return (n_written == n_bytes) ? 0 : -ENOSPC;
1154 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1155 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1156 loff_t pos, unsigned len, unsigned flags,
1157 struct page **pagep, void **fsdata)
1159 struct page *pg = NULL;
1160 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1166 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1167 pg = grab_cache_page_write_begin(mapping, index, flags);
1169 pg = __grab_cache_page(mapping, index);
1178 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
1179 (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
1182 space_held = yaffs_hold_space(filp);
1189 /* Update page if required */
1191 if (!Page_Uptodate(pg))
1192 ret = yaffs_readpage_nolock(filp, pg);
1197 /* Happy path return */
1198 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
1204 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
1206 yaffs_release_space(filp);
1209 page_cache_release(pg);
1216 static int yaffs_prepare_write(struct file *f, struct page *pg,
1217 unsigned offset, unsigned to)
1219 T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
1221 if (!Page_Uptodate(pg))
1222 return yaffs_readpage_nolock(f, pg);
1227 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1228 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1229 loff_t pos, unsigned len, unsigned copied,
1230 struct page *pg, void *fsdadata)
1234 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1237 addr = kva + offset_into_page;
1240 ("yaffs_write_end addr %p pos %x n_bytes %d\n",
1241 addr,(unsigned)pos, copied));
1243 ret = yaffs_file_write(filp, addr, copied, &pos);
1245 if (ret != copied) {
1247 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
1256 yaffs_release_space(filp);
1258 page_cache_release(pg);
1263 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1268 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1269 int n_bytes = to - offset;
1272 unsigned spos = pos;
1276 addr = kva + offset;
1278 saddr = (unsigned) addr;
1281 (TSTR("yaffs_commit_write addr %x pos %x n_bytes %d\n"),
1282 saddr, spos, n_bytes));
1284 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
1286 if (n_written != n_bytes) {
1288 (TSTR("yaffs_commit_write not same size n_written %d n_bytes %d\n"),
1289 n_written, n_bytes));
1298 (TSTR("yaffs_commit_write returning %d\n"),
1299 n_written == n_bytes ? 0 : n_written));
1301 return n_written == n_bytes ? 0 : n_written;
1306 static void yaffs_fill_inode_from_obj(struct inode *inode, struct yaffs_obj *obj)
1311 /* Check mode against the variant type and attempt to repair if broken. */
1312 u32 mode = obj->yst_mode;
1313 switch (obj->variant_type) {
1314 case YAFFS_OBJECT_TYPE_FILE:
1315 if (!S_ISREG(mode)) {
1316 obj->yst_mode &= ~S_IFMT;
1317 obj->yst_mode |= S_IFREG;
1321 case YAFFS_OBJECT_TYPE_SYMLINK:
1322 if (!S_ISLNK(mode)) {
1323 obj->yst_mode &= ~S_IFMT;
1324 obj->yst_mode |= S_IFLNK;
1328 case YAFFS_OBJECT_TYPE_DIRECTORY:
1329 if (!S_ISDIR(mode)) {
1330 obj->yst_mode &= ~S_IFMT;
1331 obj->yst_mode |= S_IFDIR;
1335 case YAFFS_OBJECT_TYPE_UNKNOWN:
1336 case YAFFS_OBJECT_TYPE_HARDLINK:
1337 case YAFFS_OBJECT_TYPE_SPECIAL:
1343 inode->i_flags |= S_NOATIME;
1345 inode->i_ino = obj->obj_id;
1346 inode->i_mode = obj->yst_mode;
1347 inode->i_uid = obj->yst_uid;
1348 inode->i_gid = obj->yst_gid;
1349 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1350 inode->i_blksize = inode->i_sb->s_blocksize;
1352 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1354 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1355 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1356 inode->i_atime.tv_nsec = 0;
1357 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1358 inode->i_mtime.tv_nsec = 0;
1359 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1360 inode->i_ctime.tv_nsec = 0;
1362 inode->i_rdev = obj->yst_rdev;
1363 inode->i_atime = obj->yst_atime;
1364 inode->i_mtime = obj->yst_mtime;
1365 inode->i_ctime = obj->yst_ctime;
1367 inode->i_size = yaffs_get_obj_length(obj);
1368 inode->i_blocks = (inode->i_size + 511) >> 9;
1370 inode->i_nlink = yaffs_get_obj_link_count(obj);
1373 (TSTR("yaffs_fill_inode mode %x uid %d gid %d size %d count %d\n"),
1374 inode->i_mode, inode->i_uid, inode->i_gid,
1375 (int)inode->i_size, atomic_read(&inode->i_count)));
1377 switch (obj->yst_mode & S_IFMT) {
1378 default: /* fifo, device or socket */
1379 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1380 init_special_inode(inode, obj->yst_mode,
1381 old_decode_dev(obj->yst_rdev));
1383 init_special_inode(inode, obj->yst_mode,
1384 (dev_t) (obj->yst_rdev));
1387 case S_IFREG: /* file */
1388 inode->i_op = &yaffs_file_inode_operations;
1389 inode->i_fop = &yaffs_file_operations;
1390 inode->i_mapping->a_ops =
1391 &yaffs_file_address_operations;
1393 case S_IFDIR: /* directory */
1394 inode->i_op = &yaffs_dir_inode_operations;
1395 inode->i_fop = &yaffs_dir_operations;
1397 case S_IFLNK: /* symlink */
1398 inode->i_op = &yaffs_symlink_inode_operations;
1402 yaffs_inode_to_obj_lv(inode) = obj;
1404 obj->my_inode = inode;
1408 (TSTR("yaffs_fill_inode invalid parameters\n")));
1413 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1414 struct yaffs_obj *obj)
1416 struct inode *inode;
1420 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1427 (TSTR("yaffs_get_inode for NULL object!!\n")));
1433 (TSTR("yaffs_get_inode for object %d\n"), obj->obj_id));
1435 inode = Y_IGET(sb, obj->obj_id);
1439 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1440 /* iget also increments the inode's i_count */
1441 /* NB You can't be holding gross_lock or deadlock will happen! */
1446 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1449 struct yaffs_obj *obj;
1450 int n_written, ipos;
1451 struct inode *inode;
1452 struct yaffs_dev *dev;
1454 obj = yaffs_dentry_to_obj(f->f_dentry);
1458 yaffs_gross_lock(dev);
1460 inode = f->f_dentry->d_inode;
1462 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1463 ipos = inode->i_size;
1469 (TSTR("yaffs_file_write: hey obj is null!\n")));
1472 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1473 "to object %d at %d(%x)\n"),
1474 (unsigned) n, (unsigned) n, obj->obj_id, ipos,ipos));
1476 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1478 yaffs_touch_super(dev);
1481 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1482 (unsigned )n,(unsigned)n));
1484 if (n_written > 0) {
1487 if (ipos > inode->i_size) {
1488 inode->i_size = ipos;
1489 inode->i_blocks = (ipos + 511) >> 9;
1492 (TSTR("yaffs_file_write size updated to %d bytes, "
1494 ipos, (int)(inode->i_blocks)));
1498 yaffs_gross_unlock(dev);
1499 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1502 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1503 /* For now we just assume few parallel writes and check against a small number. */
1504 /* Todo: need to do this with a counter to handle parallel reads better */
1506 static ssize_t yaffs_hold_space(struct file *f)
1508 struct yaffs_obj *obj;
1509 struct yaffs_dev *dev;
1514 obj = yaffs_dentry_to_obj(f->f_dentry);
1518 yaffs_gross_lock(dev);
1520 n_free_chunks = yaffs_get_n_free_chunks(dev);
1522 yaffs_gross_unlock(dev);
1524 return (n_free_chunks > 20) ? 1 : 0;
1527 static void yaffs_release_space(struct file *f)
1529 struct yaffs_obj *obj;
1530 struct yaffs_dev *dev;
1533 obj = yaffs_dentry_to_obj(f->f_dentry);
1537 yaffs_gross_lock(dev);
1540 yaffs_gross_unlock(dev);
1544 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1552 offset += i_size_read(file->f_path.dentry->d_inode);
1555 offset += file->f_pos;
1560 if (offset != file->f_pos)
1561 file->f_pos = offset;
1570 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1572 struct yaffs_obj *obj;
1573 struct yaffs_dev *dev;
1574 struct yaffs_search_context *sc;
1575 struct inode *inode = f->f_dentry->d_inode;
1576 unsigned long offset, curoffs;
1577 struct yaffs_obj *l;
1580 char name[YAFFS_MAX_NAME_LENGTH + 1];
1582 obj = yaffs_dentry_to_obj(f->f_dentry);
1585 yaffs_gross_lock(dev);
1587 yaffs_dev_to_lc(dev)->readdir_process = current;
1591 sc = yaffs_new_search(obj);
1597 T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1601 (TSTR("yaffs_readdir: entry . ino %d \n"),
1602 (int)inode->i_ino));
1603 yaffs_gross_unlock(dev);
1604 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0){
1605 yaffs_gross_lock(dev);
1608 yaffs_gross_lock(dev);
1614 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1615 (int)f->f_dentry->d_parent->d_inode->i_ino));
1616 yaffs_gross_unlock(dev);
1617 if (filldir(dirent, "..", 2, offset,
1618 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
1619 yaffs_gross_lock(dev);
1622 yaffs_gross_lock(dev);
1629 /* If the directory has changed since the open or last call to
1630 readdir, rewind to after the 2 canned entries. */
1631 if (f->f_version != inode->i_version) {
1634 f->f_version = inode->i_version;
1637 while(sc->next_return){
1639 l = sc->next_return;
1640 if (curoffs >= offset) {
1641 int this_inode = yaffs_get_obj_inode(l);
1642 int this_type = yaffs_get_obj_type(l);
1644 yaffs_get_obj_name(l, name,
1645 YAFFS_MAX_NAME_LENGTH + 1);
1647 (TSTR("yaffs_readdir: %s inode %d\n"),
1648 name, yaffs_get_obj_inode(l)));
1650 yaffs_gross_unlock(dev);
1658 yaffs_gross_lock(dev);
1662 yaffs_gross_lock(dev);
1667 yaffs_search_advance(sc);
1671 yaffs_search_end(sc);
1672 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1673 yaffs_gross_unlock(dev);
1681 * File creation. Allocate an inode, and we're done..
1684 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1687 #define YCRED(x) (x->cred)
1690 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1691 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1694 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1698 struct inode *inode;
1700 struct yaffs_obj *obj = NULL;
1701 struct yaffs_dev *dev;
1703 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1705 int error = -ENOSPC;
1706 uid_t uid = YCRED(current)->fsuid;
1707 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1709 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1714 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1715 parent->obj_id, parent->variant_type));
1718 (TSTR("yaffs_mknod: could not get parent object\n")));
1722 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1723 "mode %x dev %x\n"),
1724 dentry->d_name.name, mode, rdev));
1726 dev = parent->my_dev;
1728 yaffs_gross_lock(dev);
1730 switch (mode & S_IFMT) {
1732 /* Special (socket, fifo, device...) */
1733 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1734 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1735 obj = yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1736 gid, old_encode_dev(rdev));
1738 obj = yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1742 case S_IFREG: /* file */
1743 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1744 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1747 case S_IFDIR: /* directory */
1749 (TSTR("yaffs_mknod: making directory\n")));
1750 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1753 case S_IFLNK: /* symlink */
1754 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1755 obj = NULL; /* Do we ever get here? */
1759 /* Can not call yaffs_get_inode() with gross lock held */
1760 yaffs_gross_unlock(dev);
1763 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1764 d_instantiate(dentry, inode);
1765 update_dir_time(dir);
1767 (TSTR("yaffs_mknod created object %d count = %d\n"),
1768 obj->obj_id, atomic_read(&inode->i_count)));
1770 yaffs_fill_inode_from_obj(dir,parent);
1773 (TSTR("yaffs_mknod failed making object\n")));
1780 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1783 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1784 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1788 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1789 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1790 struct nameidata *n)
1792 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1795 T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1796 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1799 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1803 struct yaffs_dev *dev;
1804 struct yaffs_obj *obj;
1807 (TSTR("yaffs_unlink %d:%s\n"),
1809 dentry->d_name.name));
1810 obj = yaffs_inode_to_obj(dir);
1813 yaffs_gross_lock(dev);
1815 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1817 if (ret_val == YAFFS_OK) {
1818 dentry->d_inode->i_nlink--;
1820 yaffs_gross_unlock(dev);
1821 mark_inode_dirty(dentry->d_inode);
1822 update_dir_time(dir);
1825 yaffs_gross_unlock(dev);
1832 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1833 struct dentry *dentry)
1835 struct inode *inode = old_dentry->d_inode;
1836 struct yaffs_obj *obj = NULL;
1837 struct yaffs_obj *link = NULL;
1838 struct yaffs_dev *dev;
1840 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1842 obj = yaffs_inode_to_obj(inode);
1845 yaffs_gross_lock(dev);
1847 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1848 link = yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1852 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
1853 d_instantiate(dentry, old_dentry->d_inode);
1854 atomic_inc(&old_dentry->d_inode->i_count);
1856 (TSTR("yaffs_link link count %d i_count %d\n"),
1857 old_dentry->d_inode->i_nlink,
1858 atomic_read(&old_dentry->d_inode->i_count)));
1861 yaffs_gross_unlock(dev);
1864 update_dir_time(dir);
1871 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1872 const char *symname)
1874 struct yaffs_obj *obj;
1875 struct yaffs_dev *dev;
1876 uid_t uid = YCRED(current)->fsuid;
1877 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1879 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1881 dev = yaffs_inode_to_obj(dir)->my_dev;
1882 yaffs_gross_lock(dev);
1883 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1884 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1885 yaffs_gross_unlock(dev);
1888 struct inode *inode;
1890 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1891 d_instantiate(dentry, inode);
1892 update_dir_time(dir);
1893 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1896 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1902 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1903 static int yaffs_sync_object(struct file *file, int datasync)
1905 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1910 struct yaffs_obj *obj;
1911 struct yaffs_dev *dev;
1912 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1913 struct dentry *dentry = file->f_path.dentry;
1916 obj = yaffs_dentry_to_obj(dentry);
1920 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1921 (TSTR("yaffs_sync_object\n")));
1922 yaffs_gross_lock(dev);
1923 yaffs_flush_file(obj, 1, datasync);
1924 yaffs_gross_unlock(dev);
1929 * The VFS layer already does all the dentry stuff for rename.
1931 * NB: POSIX says you can rename an object over an old object of the same name
1933 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1934 struct inode *new_dir, struct dentry *new_dentry)
1936 struct yaffs_dev *dev;
1937 int ret_val = YAFFS_FAIL;
1938 struct yaffs_obj *target;
1940 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1941 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1943 yaffs_gross_lock(dev);
1945 /* Check if the target is an existing directory that is not empty. */
1946 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1947 new_dentry->d_name.name);
1951 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1952 !ylist_empty(&target->variant.dir_variant.children)) {
1954 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1956 ret_val = YAFFS_FAIL;
1958 /* Now does unlinking internally using shadowing mechanism */
1959 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_rename_obj\n")));
1961 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1962 old_dentry->d_name.name,
1963 yaffs_inode_to_obj(new_dir),
1964 new_dentry->d_name.name);
1966 yaffs_gross_unlock(dev);
1968 if (ret_val == YAFFS_OK) {
1970 new_dentry->d_inode->i_nlink--;
1971 mark_inode_dirty(new_dentry->d_inode);
1974 update_dir_time(old_dir);
1975 if(old_dir != new_dir)
1976 update_dir_time(new_dir);
1983 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1985 struct inode *inode = dentry->d_inode;
1987 struct yaffs_dev *dev;
1990 (TSTR("yaffs_setattr of object %d\n"),
1991 yaffs_inode_to_obj(inode)->obj_id));
1993 /* Fail if a requested resize >= 2GB */
1994 if (attr->ia_valid & ATTR_SIZE &&
1995 (attr->ia_size >> 31))
1999 error = inode_change_ok(inode, attr);
2003 error = yaffs_vfs_setattr(inode, attr);
2004 T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
2005 if (attr->ia_valid & ATTR_SIZE){
2006 yaffs_vfs_setsize(inode,attr->ia_size);
2007 inode->i_blocks = (inode->i_size + 511) >> 9;
2010 dev = yaffs_inode_to_obj(inode)->my_dev;
2011 if (attr->ia_valid & ATTR_SIZE){
2012 T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
2013 (int)(attr->ia_size),(int)(attr->ia_size)));
2015 yaffs_gross_lock(dev);
2016 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
2017 if(result == YAFFS_OK) {
2022 yaffs_gross_unlock(dev);
2027 (TSTR("yaffs_setattr done returning %d\n"),error));
2032 #ifdef CONFIG_YAFFS_XATTR
2033 int yaffs_setxattr(struct dentry *dentry, const char *name,
2034 const void *value, size_t size, int flags)
2036 struct inode *inode = dentry->d_inode;
2038 struct yaffs_dev *dev;
2039 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2042 (TSTR("yaffs_setxattr of object %d\n"),
2049 yaffs_gross_lock(dev);
2050 result = yaffs_set_xattrib(obj, name, value, size, flags);
2051 if(result == YAFFS_OK)
2055 yaffs_gross_unlock(dev);
2059 (TSTR("yaffs_setxattr done returning %d\n"),error));
2065 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
2068 struct inode *inode = dentry->d_inode;
2070 struct yaffs_dev *dev;
2071 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2074 (TSTR("yaffs_getxattr \"%s\" from object %d\n"),
2075 name, obj->obj_id));
2079 yaffs_gross_lock(dev);
2080 error = yaffs_get_xattrib(obj, name, buff, size);
2081 yaffs_gross_unlock(dev);
2085 (TSTR("yaffs_getxattr done returning %d\n"),error));
2090 int yaffs_removexattr(struct dentry *dentry, const char *name)
2092 struct inode *inode = dentry->d_inode;
2094 struct yaffs_dev *dev;
2095 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2098 (TSTR("yaffs_removexattr of object %d\n"),
2105 yaffs_gross_lock(dev);
2106 result = yaffs_remove_xattrib(obj, name);
2107 if(result == YAFFS_OK)
2111 yaffs_gross_unlock(dev);
2115 (TSTR("yaffs_removexattr done returning %d\n"),error));
2120 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
2122 struct inode *inode = dentry->d_inode;
2124 struct yaffs_dev *dev;
2125 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2128 (TSTR("yaffs_listxattr of object %d\n"),
2134 yaffs_gross_lock(dev);
2135 error = yaffs_list_xattrib(obj, buff, size);
2136 yaffs_gross_unlock(dev);
2140 (TSTR("yaffs_listxattr done returning %d\n"),error));
2148 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2149 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2151 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2152 struct super_block *sb = dentry->d_sb;
2153 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2154 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2156 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2158 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2160 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2163 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
2165 yaffs_gross_lock(dev);
2167 buf->f_type = YAFFS_MAGIC;
2168 buf->f_bsize = sb->s_blocksize;
2169 buf->f_namelen = 255;
2171 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2172 /* Do this if chunk size is not a power of 2 */
2174 uint64_t bytes_in_dev;
2175 uint64_t bytes_free;
2177 bytes_in_dev = ((uint64_t)((dev->param.end_block - dev->param.start_block + 1))) *
2178 ((uint64_t)(dev->param.chunks_per_block * dev->data_bytes_per_chunk));
2180 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2181 buf->f_blocks = bytes_in_dev;
2183 bytes_free = ((uint64_t)(yaffs_get_n_free_chunks(dev))) *
2184 ((uint64_t)(dev->data_bytes_per_chunk));
2186 do_div(bytes_free, sb->s_blocksize);
2188 buf->f_bfree = bytes_free;
2190 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2193 (dev->param.end_block - dev->param.start_block + 1) *
2194 dev->param.chunks_per_block /
2195 (sb->s_blocksize / dev->data_bytes_per_chunk);
2197 yaffs_get_n_free_chunks(dev) /
2198 (sb->s_blocksize / dev->data_bytes_per_chunk);
2201 (dev->param.end_block - dev->param.start_block + 1) *
2202 dev->param.chunks_per_block *
2203 (dev->data_bytes_per_chunk / sb->s_blocksize);
2206 yaffs_get_n_free_chunks(dev) *
2207 (dev->data_bytes_per_chunk / sb->s_blocksize);
2212 buf->f_bavail = buf->f_bfree;
2214 yaffs_gross_unlock(dev);
2220 static void yaffs_flush_inodes(struct super_block *sb)
2223 struct yaffs_obj *obj;
2225 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
2226 obj = yaffs_inode_to_obj(iptr);
2228 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
2230 yaffs_flush_file(obj,1,0);
2236 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2238 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2242 yaffs_flush_inodes(sb);
2243 yaffs_update_dirty_dirs(dev);
2244 yaffs_flush_whole_cache(dev);
2246 yaffs_checkpoint_save(dev);
2250 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2252 unsigned erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2253 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2254 unsigned scattered = 0; /* Free chunks not in an erased block */
2256 if(erased_chunks < dev->n_free_chunks)
2257 scattered = (dev->n_free_chunks - erased_chunks);
2259 if(!context->bg_running)
2261 else if(scattered < (dev->param.chunks_per_block * 2))
2263 else if(erased_chunks > dev->n_free_chunks/2)
2265 else if(erased_chunks > dev->n_free_chunks/4)
2271 static int yaffs_do_sync_fs(struct super_block *sb,
2272 int request_checkpoint)
2275 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2276 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2277 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2280 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2281 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
2283 sb->s_dirt ? "dirty" : "clean",
2284 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2285 oneshot_checkpoint ? " one-shot" : "" ));
2287 yaffs_gross_lock(dev);
2288 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2289 oneshot_checkpoint) &&
2290 !dev->is_checkpointed;
2292 if (sb->s_dirt || do_checkpoint) {
2293 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2295 if(oneshot_checkpoint)
2296 yaffs_auto_checkpoint &= ~4;
2298 yaffs_gross_unlock(dev);
2304 * yaffs background thread functions .
2305 * yaffs_bg_thread_fn() the thread function
2306 * yaffs_bg_start() launches the background thread.
2307 * yaffs_bg_stop() cleans up the background thread.
2310 * The thread should only run after the yaffs is initialised
2311 * The thread should be stopped before yaffs is unmounted.
2312 * The thread should not do any writing while the fs is in read only.
2315 #ifdef YAFFS_COMPILE_BACKGROUND
2317 void yaffs_background_waker(unsigned long data)
2319 wake_up_process((struct task_struct *)data);
2322 static int yaffs_bg_thread_fn(void *data)
2324 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2325 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2326 unsigned long now = jiffies;
2327 unsigned long next_dir_update = now;
2328 unsigned long next_gc = now;
2329 unsigned long expires;
2330 unsigned int urgency;
2333 struct timer_list timer;
2335 T(YAFFS_TRACE_BACKGROUND,
2336 (TSTR("yaffs_background starting for dev %p\n"),
2339 #ifdef YAFFS_COMPILE_FREEZER
2342 while(context->bg_running){
2343 T(YAFFS_TRACE_BACKGROUND,
2344 (TSTR("yaffs_background\n")));
2346 if(kthread_should_stop())
2349 #ifdef YAFFS_COMPILE_FREEZER
2353 yaffs_gross_lock(dev);
2357 if(time_after(now, next_dir_update) && yaffs_bg_enable){
2358 yaffs_update_dirty_dirs(dev);
2359 next_dir_update = now + HZ;
2362 if(time_after(now,next_gc) && yaffs_bg_enable){
2363 if(!dev->is_checkpointed){
2364 urgency = yaffs_bg_gc_urgency(dev);
2365 gc_result = yaffs_bg_gc(dev, urgency);
2367 next_gc = now + HZ/20+1;
2368 else if(urgency > 0)
2369 next_gc = now + HZ/10+1;
2371 next_gc = now + HZ * 2;
2373 * gc not running so set to next_dir_update
2374 * to cut down on wake ups
2376 next_gc = next_dir_update;
2378 yaffs_gross_unlock(dev);
2380 expires = next_dir_update;
2381 if (time_before(next_gc,expires))
2383 if(time_before(expires,now))
2386 Y_INIT_TIMER(&timer);
2387 timer.expires = expires+1;
2388 timer.data = (unsigned long) current;
2389 timer.function = yaffs_background_waker;
2391 set_current_state(TASK_INTERRUPTIBLE);
2394 del_timer_sync(&timer);
2403 static int yaffs_bg_start(struct yaffs_dev *dev)
2406 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2411 context->bg_running = 1;
2413 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2414 (void *)dev,"yaffs-bg-%d",context->mount_id);
2416 if(IS_ERR(context->bg_thread)){
2417 retval = PTR_ERR(context->bg_thread);
2418 context->bg_thread = NULL;
2419 context->bg_running = 0;
2424 static void yaffs_bg_stop(struct yaffs_dev *dev)
2426 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2428 ctxt->bg_running = 0;
2430 if( ctxt->bg_thread){
2431 kthread_stop(ctxt->bg_thread);
2432 ctxt->bg_thread = NULL;
2436 static int yaffs_bg_thread_fn(void *data)
2441 static int yaffs_bg_start(struct yaffs_dev *dev)
2446 static void yaffs_bg_stop(struct yaffs_dev *dev)
2452 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2453 static void yaffs_write_super(struct super_block *sb)
2455 static int yaffs_write_super(struct super_block *sb)
2458 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2460 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2461 (TSTR("yaffs_write_super%s\n"),
2462 request_checkpoint ? " checkpt" : ""));
2464 yaffs_do_sync_fs(sb, request_checkpoint);
2466 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2472 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2473 static int yaffs_sync_fs(struct super_block *sb, int wait)
2475 static int yaffs_sync_fs(struct super_block *sb)
2478 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2480 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2481 (TSTR("yaffs_sync_fs%s\n"),
2482 request_checkpoint ? " checkpt" : ""));
2484 yaffs_do_sync_fs(sb, request_checkpoint);
2489 #ifdef YAFFS_USE_OWN_IGET
2491 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2493 struct inode *inode;
2494 struct yaffs_obj *obj;
2495 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2498 (TSTR("yaffs_iget for %lu\n"), ino));
2500 inode = iget_locked(sb, ino);
2502 return ERR_PTR(-ENOMEM);
2503 if (!(inode->i_state & I_NEW))
2506 /* NB This is called as a side effect of other functions, but
2507 * we had to release the lock to prevent deadlocks, so
2508 * need to lock again.
2511 yaffs_gross_lock(dev);
2513 obj = yaffs_find_by_number(dev, inode->i_ino);
2515 yaffs_fill_inode_from_obj(inode, obj);
2517 yaffs_gross_unlock(dev);
2519 unlock_new_inode(inode);
2525 static void yaffs_read_inode(struct inode *inode)
2527 /* NB This is called as a side effect of other functions, but
2528 * we had to release the lock to prevent deadlocks, so
2529 * need to lock again.
2532 struct yaffs_obj *obj;
2533 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
2536 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2538 if(current != yaffs_dev_to_lc(dev)->readdir_process)
2539 yaffs_gross_lock(dev);
2541 obj = yaffs_find_by_number(dev, inode->i_ino);
2543 yaffs_fill_inode_from_obj(inode, obj);
2545 if(current != yaffs_dev_to_lc(dev)->readdir_process)
2546 yaffs_gross_unlock(dev);
2551 static YLIST_HEAD(yaffs_context_list);
2552 struct semaphore yaffs_context_lock;
2554 static void yaffs_put_super(struct super_block *sb)
2556 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2558 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2560 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2561 (TSTR("Shutting down yaffs background thread\n")));
2563 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2564 (TSTR("yaffs background thread shut down\n")));
2566 yaffs_gross_lock(dev);
2568 yaffs_flush_super(sb,1);
2570 if (yaffs_dev_to_lc(dev)->put_super_fn)
2571 yaffs_dev_to_lc(dev)->put_super_fn(sb);
2574 yaffs_deinitialise(dev);
2576 yaffs_gross_unlock(dev);
2578 down(&yaffs_context_lock);
2579 ylist_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2580 up(&yaffs_context_lock);
2582 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2583 YFREE(yaffs_dev_to_lc(dev)->spare_buffer);
2584 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2591 static void yaffs_mtd_put_super(struct super_block *sb)
2593 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
2598 put_mtd_device(mtd);
2602 static void yaffs_touch_super(struct yaffs_dev *dev)
2604 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
2606 T(YAFFS_TRACE_OS, (TSTR("yaffs_touch_super() sb = %p\n"), sb));
2613 int skip_checkpoint_read;
2614 int skip_checkpoint_write;
2617 int tags_ecc_overridden;
2618 int lazy_loading_enabled;
2619 int lazy_loading_overridden;
2620 int empty_lost_and_found;
2621 int empty_lost_and_found_overridden;
2624 #define MAX_OPT_LEN 30
2625 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2627 char cur_opt[MAX_OPT_LEN + 1];
2631 /* Parse through the options which is a comma seperated list */
2633 while (options_str && *options_str && !error) {
2634 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2637 while(*options_str == ',')
2640 while (*options_str && *options_str != ',') {
2641 if (p < MAX_OPT_LEN) {
2642 cur_opt[p] = *options_str;
2648 if (!strcmp(cur_opt, "inband-tags"))
2649 options->inband_tags = 1;
2650 else if (!strcmp(cur_opt, "tags-ecc-off")){
2651 options->tags_ecc_on = 0;
2652 options->tags_ecc_overridden=1;
2653 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2654 options->tags_ecc_on = 1;
2655 options->tags_ecc_overridden = 1;
2656 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2657 options->lazy_loading_enabled = 0;
2658 options->lazy_loading_overridden=1;
2659 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2660 options->lazy_loading_enabled = 1;
2661 options->lazy_loading_overridden = 1;
2662 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2663 options->empty_lost_and_found = 0;
2664 options->empty_lost_and_found_overridden=1;
2665 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2666 options->empty_lost_and_found = 1;
2667 options->empty_lost_and_found_overridden=1;
2668 } else if (!strcmp(cur_opt, "no-cache"))
2669 options->no_cache = 1;
2670 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2671 options->skip_checkpoint_read = 1;
2672 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2673 options->skip_checkpoint_write = 1;
2674 else if (!strcmp(cur_opt, "no-checkpoint")) {
2675 options->skip_checkpoint_read = 1;
2676 options->skip_checkpoint_write = 1;
2678 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2687 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2688 struct super_block *sb,
2689 void *data, int silent)
2692 struct inode *inode = NULL;
2693 struct dentry *root;
2694 struct yaffs_dev *dev = 0;
2695 char devname_buf[BDEVNAME_SIZE + 1];
2696 struct mtd_info *mtd;
2698 char *data_str = (char *)data;
2699 struct yaffs_linux_context *context = NULL;
2700 struct yaffs_param *param;
2704 yaffs_options options;
2708 struct yaffs_linux_context *context_iterator;
2709 struct ylist_head *l;
2711 sb->s_magic = YAFFS_MAGIC;
2712 sb->s_op = &yaffs_super_ops;
2713 sb->s_flags |= MS_NOATIME;
2715 read_only =((sb->s_flags & MS_RDONLY) != 0);
2718 #ifdef YAFFS_COMPILE_EXPORTFS
2719 sb->s_export_op = &yaffs_export_ops;
2723 printk(KERN_INFO "yaffs: sb is NULL\n");
2724 else if (!sb->s_dev)
2725 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2726 else if (!yaffs_devname(sb, devname_buf))
2727 printk(KERN_INFO "yaffs: devname is NULL\n");
2729 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2731 yaffs_devname(sb, devname_buf),
2732 read_only ? "ro" : "rw");
2737 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2739 memset(&options, 0, sizeof(options));
2741 if (yaffs_parse_options(&options, data_str)) {
2742 /* Option parsing failed */
2747 sb->s_blocksize = PAGE_CACHE_SIZE;
2748 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2751 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffs_version));
2753 (TSTR("yaffs_read_super: block size %d\n"),
2754 (int)(sb->s_blocksize)));
2756 T(YAFFS_TRACE_ALWAYS,
2757 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2758 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2759 yaffs_devname(sb, devname_buf)));
2761 /* Check it's an mtd device..... */
2762 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2763 return NULL; /* This isn't an mtd device */
2765 /* Get the device */
2766 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2768 T(YAFFS_TRACE_ALWAYS,
2769 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2773 /* Check it's NAND */
2774 if (mtd->type != MTD_NANDFLASH) {
2775 T(YAFFS_TRACE_ALWAYS,
2776 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2781 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2782 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2783 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2784 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2785 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2786 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2787 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2788 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2789 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2790 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2791 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2792 T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2794 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2797 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2799 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2800 T(YAFFS_TRACE_ALWAYS,
2801 (TSTR("yaffs: auto selecting yaffs2\n")));
2805 /* Added NCB 26/5/2006 for completeness */
2806 if (yaffs_version == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2807 T(YAFFS_TRACE_ALWAYS,
2808 (TSTR("yaffs: auto selecting yaffs1\n")));
2814 if (yaffs_version == 2) {
2815 /* Check for version 2 style functions */
2817 !mtd->block_isbad ||
2818 !mtd->block_markbad ||
2821 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2822 !mtd->read_oob || !mtd->write_oob) {
2825 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2827 T(YAFFS_TRACE_ALWAYS,
2828 (TSTR("yaffs: MTD device does not support required "
2833 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2834 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2835 !options.inband_tags) {
2836 T(YAFFS_TRACE_ALWAYS,
2837 (TSTR("yaffs: MTD device does not have the "
2838 "right page sizes\n")));
2842 /* Check for V1 style functions */
2846 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2847 !mtd->read_oob || !mtd->write_oob) {
2850 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2852 T(YAFFS_TRACE_ALWAYS,
2853 (TSTR("yaffs: MTD device does not support required "
2858 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2859 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2860 T(YAFFS_TRACE_ALWAYS,
2861 (TSTR("yaffs: MTD device does not support have the "
2862 "right page sizes\n")));
2867 /* OK, so if we got here, we have an MTD that's NAND and looks
2868 * like it has the right capabilities
2869 * Set the struct yaffs_dev up for mtd
2872 if (!read_only && !(mtd->flags & MTD_WRITEABLE)){
2874 printk(KERN_INFO "yaffs: mtd is read only, setting superblock read only");
2875 sb->s_flags |= MS_RDONLY;
2878 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2879 context = kmalloc(sizeof(struct yaffs_linux_context),GFP_KERNEL);
2881 if(!dev || !context ){
2891 /* Deep shit could not allocate device structure */
2892 T(YAFFS_TRACE_ALWAYS,
2893 (TSTR("yaffs_read_super: Failed trying to allocate "
2894 "struct yaffs_dev. \n")));
2897 memset(dev, 0, sizeof(struct yaffs_dev));
2898 param = &(dev->param);
2900 memset(context,0,sizeof(struct yaffs_linux_context));
2901 dev->os_context = context;
2902 YINIT_LIST_HEAD(&(context->context_list));
2904 context->super = sb;
2906 dev->read_only = read_only;
2908 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2909 sb->s_fs_info = dev;
2911 sb->u.generic_sbp = dev;
2914 dev->driver_context = mtd;
2915 param->name = mtd->name;
2917 /* Set up the memory size parameters.... */
2919 n_blocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2921 param->start_block = 0;
2922 param->end_block = n_blocks - 1;
2923 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2924 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2925 param->n_reserved_blocks = 5;
2926 param->n_caches = (options.no_cache) ? 0 : 10;
2927 param->inband_tags = options.inband_tags;
2929 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2930 param->disable_lazy_load = 1;
2932 #ifdef CONFIG_YAFFS_XATTR
2933 param->enable_xattr = 1;
2935 if(options.lazy_loading_overridden)
2936 param->disable_lazy_load = !options.lazy_loading_enabled;
2938 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2939 param->no_tags_ecc = 1;
2942 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2944 param->defered_dir_update = 1;
2947 if(options.tags_ecc_overridden)
2948 param->no_tags_ecc = !options.tags_ecc_on;
2950 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2951 param->empty_lost_n_found = 1;
2954 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2955 param->refresh_period = 0;
2957 param->refresh_period = 500;
2960 #ifdef CONFIG_YAFFS__ALWAYS_CHECK_CHUNK_ERASED
2961 param->always_check_erased = 1;
2964 if(options.empty_lost_and_found_overridden)
2965 param->empty_lost_n_found = options.empty_lost_and_found;
2967 /* ... and the functions. */
2968 if (yaffs_version == 2) {
2969 param->write_chunk_tags_fn =
2970 nandmtd2_write_chunk_tags;
2971 param->read_chunk_tags_fn =
2972 nandmtd2_read_chunk_tags;
2973 param->bad_block_fn = nandmtd2_mark_block_bad;
2974 param->query_block_fn = nandmtd2_query_block;
2975 yaffs_dev_to_lc(dev)->spare_buffer = YMALLOC(mtd->oobsize);
2976 param->is_yaffs2 = 1;
2977 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2978 param->total_bytes_per_chunk = mtd->writesize;
2979 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2981 param->total_bytes_per_chunk = mtd->oobblock;
2982 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
2984 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2986 param->start_block = 0;
2987 param->end_block = n_blocks - 1;
2989 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2990 /* use the MTD interface in yaffs_mtdif1.c */
2991 param->write_chunk_tags_fn =
2992 nandmtd1_write_chunk_tags;
2993 param->read_chunk_tags_fn =
2994 nandmtd1_read_chunk_tags;
2995 param->bad_block_fn = nandmtd1_mark_block_bad;
2996 param->query_block_fn = nandmtd1_query_block;
2998 param->write_chunk_fn = nandmtd_write_chunk;
2999 param->read_chunk_fn = nandmtd_read_chunk;
3001 param->is_yaffs2 = 0;
3003 /* ... and common functions */
3004 param->erase_fn = nandmtd_erase_block;
3005 param->initialise_flash_fn = nandmtd_initialise;
3007 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
3009 param->sb_dirty_fn = yaffs_touch_super;
3010 param->gc_control = yaffs_gc_control_callback;
3012 yaffs_dev_to_lc(dev)->super= sb;
3015 #ifndef CONFIG_YAFFS_DOES_ECC
3016 param->use_nand_ecc = 1;
3019 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3020 param->wide_tnodes_disabled = 1;
3023 param->skip_checkpt_rd = options.skip_checkpoint_read;
3024 param->skip_checkpt_wr = options.skip_checkpoint_write;
3026 down(&yaffs_context_lock);
3027 /* Get a mount id */
3029 for(mount_id=0; ! found; mount_id++){
3031 ylist_for_each(l,&yaffs_context_list){
3032 context_iterator = ylist_entry(l,struct yaffs_linux_context,context_list);
3033 if(context_iterator->mount_id == mount_id)
3037 context->mount_id = mount_id;
3039 ylist_add_tail(&(yaffs_dev_to_lc(dev)->context_list), &yaffs_context_list);
3040 up(&yaffs_context_lock);
3042 /* Directory search handling...*/
3043 YINIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
3044 param->remove_obj_fn = yaffs_remove_obj_callback;
3046 init_MUTEX(&(yaffs_dev_to_lc(dev)->gross_lock));
3048 yaffs_gross_lock(dev);
3050 err = yaffs_guts_initialise(dev);
3053 (TSTR("yaffs_read_super: guts initialised %s\n"),
3054 (err == YAFFS_OK) ? "OK" : "FAILED"));
3057 yaffs_bg_start(dev);
3059 if(!context->bg_thread)
3060 param->defered_dir_update = 0;
3063 /* Release lock before yaffs_get_inode() */
3064 yaffs_gross_unlock(dev);
3066 /* Create root inode */
3067 if (err == YAFFS_OK)
3068 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3074 inode->i_op = &yaffs_dir_inode_operations;
3075 inode->i_fop = &yaffs_dir_operations;
3077 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
3079 root = d_alloc_root(inode);
3081 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
3088 sb->s_dirt = !dev->is_checkpointed;
3089 T(YAFFS_TRACE_ALWAYS,
3090 (TSTR("yaffs_read_super: is_checkpointed %d\n"),
3091 dev->is_checkpointed));
3093 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
3098 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3099 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3102 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3105 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3106 static int yaffs_read_super(struct file_system_type *fs,
3107 int flags, const char *dev_name,
3108 void *data, struct vfsmount *mnt)
3111 return get_sb_bdev(fs, flags, dev_name, data,
3112 yaffs_internal_read_super_mtd, mnt);
3115 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3116 int flags, const char *dev_name,
3120 return get_sb_bdev(fs, flags, dev_name, data,
3121 yaffs_internal_read_super_mtd);
3125 static struct file_system_type yaffs_fs_type = {
3126 .owner = THIS_MODULE,
3128 .get_sb = yaffs_read_super,
3129 .kill_sb = kill_block_super,
3130 .fs_flags = FS_REQUIRES_DEV,
3133 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3136 return yaffs_internal_read_super(1, sb, data, silent);
3139 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3144 #ifdef CONFIG_YAFFS_YAFFS2
3146 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3147 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3150 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3153 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3154 static int yaffs2_read_super(struct file_system_type *fs,
3155 int flags, const char *dev_name, void *data,
3156 struct vfsmount *mnt)
3158 return get_sb_bdev(fs, flags, dev_name, data,
3159 yaffs2_internal_read_super_mtd, mnt);
3162 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3163 int flags, const char *dev_name,
3167 return get_sb_bdev(fs, flags, dev_name, data,
3168 yaffs2_internal_read_super_mtd);
3172 static struct file_system_type yaffs2_fs_type = {
3173 .owner = THIS_MODULE,
3175 .get_sb = yaffs2_read_super,
3176 .kill_sb = kill_block_super,
3177 .fs_flags = FS_REQUIRES_DEV,
3180 static struct super_block *yaffs2_read_super(struct super_block *sb,
3181 void *data, int silent)
3183 return yaffs_internal_read_super(2, sb, data, silent);
3186 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3190 #endif /* CONFIG_YAFFS_YAFFS2 */
3192 static struct proc_dir_entry *my_proc_entry;
3193 static struct proc_dir_entry *debug_proc_entry;
3195 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev * dev)
3197 buf += sprintf(buf, "start_block.......... %d\n", dev->param.start_block);
3198 buf += sprintf(buf, "end_block............ %d\n", dev->param.end_block);
3199 buf += sprintf(buf, "total_bytes_per_chunk %d\n", dev->param.total_bytes_per_chunk);
3200 buf += sprintf(buf, "use_nand_ecc......... %d\n", dev->param.use_nand_ecc);
3201 buf += sprintf(buf, "no_tags_ecc.......... %d\n", dev->param.no_tags_ecc);
3202 buf += sprintf(buf, "is_yaffs2............ %d\n", dev->param.is_yaffs2);
3203 buf += sprintf(buf, "inband_tags.......... %d\n", dev->param.inband_tags);
3204 buf += sprintf(buf, "empty_lost_n_found... %d\n", dev->param.empty_lost_n_found);
3205 buf += sprintf(buf, "disable_lazy_load.... %d\n", dev->param.disable_lazy_load);
3206 buf += sprintf(buf, "refresh_period....... %d\n", dev->param.refresh_period);
3207 buf += sprintf(buf, "n_caches............. %d\n", dev->param.n_caches);
3208 buf += sprintf(buf, "n_reserved_blocks.... %d\n", dev->param.n_reserved_blocks);
3209 buf += sprintf(buf, "always_check_erased.. %d\n", dev->param.always_check_erased);
3211 buf += sprintf(buf, "\n");
3217 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev * dev)
3219 buf += sprintf(buf, "data_bytes_per_chunk. %d\n", dev->data_bytes_per_chunk);
3220 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3221 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3222 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3223 buf += sprintf(buf, "blocks_in_checkpt.... %d\n", dev->blocks_in_checkpt);
3224 buf += sprintf(buf, "\n");
3225 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3226 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3227 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3228 buf += sprintf(buf, "\n");
3229 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3230 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3231 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3232 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3233 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3234 buf += sprintf(buf, "passive_gc_count..... %u\n", dev->passive_gc_count);
3235 buf += sprintf(buf, "oldest_dirty_gc_count %u\n", dev->oldest_dirty_gc_count);
3236 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3237 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3238 buf += sprintf(buf, "n_retired_writes..... %u\n", dev->n_retired_writes);
3239 buf += sprintf(buf, "n_retired_blocks..... %u\n", dev->n_retired_blocks);
3240 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3241 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3242 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n", dev->n_tags_ecc_fixed);
3243 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n", dev->n_tags_ecc_unfixed);
3244 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3245 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3246 buf += sprintf(buf, "n_unlinked_files..... %u\n", dev->n_unlinked_files);
3247 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3248 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3253 static int yaffs_proc_read(char *page,
3255 off_t offset, int count, int *eof, void *data)
3257 struct ylist_head *item;
3262 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3263 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3264 * This also assumes the user has posted a read buffer large
3265 * enough to hold the complete output; but that's life in /proc.
3270 /* Print header first */
3272 buf += sprintf(buf, "Multi-version YAFFS built:" __DATE__ " " __TIME__"\n");
3274 buf += sprintf(buf,"\n");
3278 down(&yaffs_context_lock);
3280 /* Locate and print the Nth entry. Order N-squared but N is small. */
3281 ylist_for_each(item, &yaffs_context_list) {
3282 struct yaffs_linux_context *dc = ylist_entry(item, struct yaffs_linux_context, context_list);
3283 struct yaffs_dev *dev = dc->dev;
3285 if (n < (step & ~1)) {
3290 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
3291 buf = yaffs_dump_dev_part0(buf, dev);
3293 buf = yaffs_dump_dev_part1(buf, dev);
3297 up(&yaffs_context_lock);
3300 return buf - page < count ? buf - page : count;
3303 static int yaffs_stats_proc_read(char *page,
3305 off_t offset, int count, int *eof, void *data)
3307 struct ylist_head *item;
3311 down(&yaffs_context_lock);
3313 /* Locate and print the Nth entry. Order N-squared but N is small. */
3314 ylist_for_each(item, &yaffs_context_list) {
3315 struct yaffs_linux_context *dc = ylist_entry(item, struct yaffs_linux_context, context_list);
3316 struct yaffs_dev *dev = dc->dev;
3320 erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
3322 buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
3323 n, dev->n_free_chunks, erased_chunks,
3324 dev->bg_gcs, dev->oldest_dirty_gc_count,
3325 dev->n_obj, dev->n_tnodes);
3327 up(&yaffs_context_lock);
3330 return buf - page < count ? buf - page : count;
3334 * Set the verbosity of the warnings and error messages.
3336 * Note that the names can only be a..z or _ with the current code.
3341 unsigned mask_bitfield;
3343 {"allocate", YAFFS_TRACE_ALLOCATE},
3344 {"always", YAFFS_TRACE_ALWAYS},
3345 {"background", YAFFS_TRACE_BACKGROUND},
3346 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3347 {"buffers", YAFFS_TRACE_BUFFERS},
3348 {"bug", YAFFS_TRACE_BUG},
3349 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3350 {"deletion", YAFFS_TRACE_DELETION},
3351 {"erase", YAFFS_TRACE_ERASE},
3352 {"error", YAFFS_TRACE_ERROR},
3353 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3354 {"gc", YAFFS_TRACE_GC},
3355 {"lock", YAFFS_TRACE_LOCK},
3356 {"mtd", YAFFS_TRACE_MTD},
3357 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3358 {"os", YAFFS_TRACE_OS},
3359 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3360 {"scan", YAFFS_TRACE_SCAN},
3361 {"tracing", YAFFS_TRACE_TRACING},
3362 {"sync", YAFFS_TRACE_SYNC},
3363 {"write", YAFFS_TRACE_WRITE},
3365 {"verify", YAFFS_TRACE_VERIFY},
3366 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3367 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3368 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3370 {"all", 0xffffffff},
3375 #define MAX_MASK_NAME_LENGTH 40
3376 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3377 unsigned long count, void *data)
3379 unsigned rg = 0, mask_bitfield;
3383 char substring[MAX_MASK_NAME_LENGTH + 1];
3389 rg = yaffs_trace_mask;
3391 while (!done && (pos < count)) {
3393 while ((pos < count) && isspace(buf[pos]))
3410 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3412 if (end > buf + pos) {
3413 mask_name = "numeral";
3414 len = end - (buf + pos);
3418 for (x = buf + pos, i = 0;
3419 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3420 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3422 substring[i] = '\0';
3424 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3425 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3426 mask_name = mask_flags[i].mask_name;
3427 mask_bitfield = mask_flags[i].mask_bitfield;
3434 if (mask_name != NULL) {
3438 rg &= ~mask_bitfield;
3441 rg |= mask_bitfield;
3447 rg |= mask_bitfield;
3453 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3455 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3457 if (rg & YAFFS_TRACE_ALWAYS) {
3458 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3460 flag = ((rg & mask_flags[i].mask_bitfield) ==
3461 mask_flags[i].mask_bitfield) ? '+' : '-';
3462 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3470 static int yaffs_proc_write(struct file *file, const char *buf,
3471 unsigned long count, void *data)
3473 return yaffs_proc_write_trace_options(file, buf, count, data);
3476 /* Stuff to handle installation of file systems */
3477 struct file_system_to_install {
3478 struct file_system_type *fst;
3482 static struct file_system_to_install fs_to_install[] = {
3483 {&yaffs_fs_type, 0},
3484 {&yaffs2_fs_type, 0},
3488 static int __init init_yaffs_fs(void)
3491 struct file_system_to_install *fsinst;
3493 T(YAFFS_TRACE_ALWAYS,
3494 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3496 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3497 T(YAFFS_TRACE_ALWAYS,
3498 (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3504 init_MUTEX(&yaffs_context_lock);
3506 /* Install the proc_fs entries */
3507 my_proc_entry = create_proc_entry("yaffs",
3511 if (my_proc_entry) {
3512 my_proc_entry->write_proc = yaffs_proc_write;
3513 my_proc_entry->read_proc = yaffs_proc_read;
3514 my_proc_entry->data = NULL;
3518 debug_proc_entry = create_proc_entry("yaffs_stats",
3522 if (debug_proc_entry) {
3523 debug_proc_entry->write_proc = NULL;
3524 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3525 debug_proc_entry->data = NULL;
3529 /* Now add the file system entries */
3531 fsinst = fs_to_install;
3533 while (fsinst->fst && !error) {
3534 error = register_filesystem(fsinst->fst);
3536 fsinst->installed = 1;
3540 /* Any errors? uninstall */
3542 fsinst = fs_to_install;
3544 while (fsinst->fst) {
3545 if (fsinst->installed) {
3546 unregister_filesystem(fsinst->fst);
3547 fsinst->installed = 0;
3556 static void __exit exit_yaffs_fs(void)
3559 struct file_system_to_install *fsinst;
3561 T(YAFFS_TRACE_ALWAYS,
3562 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3564 remove_proc_entry("yaffs", YPROC_ROOT);
3565 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3567 fsinst = fs_to_install;
3569 while (fsinst->fst) {
3570 if (fsinst->installed) {
3571 unregister_filesystem(fsinst->fst);
3572 fsinst->installed = 0;
3578 module_init(init_yaffs_fs)
3579 module_exit(exit_yaffs_fs)
3581 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3582 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3583 MODULE_LICENSE("GPL");