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)
223 #define update_dir_time(dir) do {\
224 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
227 static void yaffs_put_super(struct super_block *sb);
229 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
231 static ssize_t yaffs_hold_space(struct file *f);
232 static void yaffs_release_space(struct file *f);
234 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
235 static int yaffs_file_flush(struct file *file, fl_owner_t id);
237 static int yaffs_file_flush(struct file *file);
240 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
241 static int yaffs_sync_object(struct file *file, int datasync);
243 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
247 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
249 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
250 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
251 struct nameidata *n);
252 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
253 struct nameidata *n);
255 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
256 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
258 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
259 struct dentry *dentry);
260 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
261 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
262 const char *symname);
263 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
265 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
266 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
269 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
272 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
273 struct inode *new_dir, struct dentry *new_dentry);
274 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
276 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
277 static int yaffs_sync_fs(struct super_block *sb, int wait);
278 static void yaffs_write_super(struct super_block *sb);
280 static int yaffs_sync_fs(struct super_block *sb);
281 static int yaffs_write_super(struct super_block *sb);
284 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
285 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
286 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
287 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
289 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
292 #ifdef YAFFS_HAS_PUT_INODE
293 static void yaffs_put_inode(struct inode *inode);
296 #ifdef YAFFS_HAS_EVICT_INODE
297 static void yaffs_evict_inode(struct inode *);
299 static void yaffs_delete_inode(struct inode *);
300 static void yaffs_clear_inode(struct inode *);
303 static int yaffs_readpage(struct file *file, struct page *page);
304 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
305 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
307 static int yaffs_writepage(struct page *page);
310 #ifdef CONFIG_YAFFS_XATTR
311 int yaffs_setxattr(struct dentry *dentry, const char *name,
312 const void *value, size_t size, int flags);
313 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
315 int yaffs_removexattr(struct dentry *dentry, const char *name);
316 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
320 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
321 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
322 loff_t pos, unsigned len, unsigned flags,
323 struct page **pagep, void **fsdata);
324 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
325 loff_t pos, unsigned len, unsigned copied,
326 struct page *pg, void *fsdadata);
328 static int yaffs_prepare_write(struct file *f, struct page *pg,
329 unsigned offset, unsigned to);
330 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
335 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
337 #if (YAFFS_NEW_FOLLOW_LINK == 1)
338 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
339 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
341 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
344 static void yaffs_touch_super(struct yaffs_dev *dev);
346 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
348 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
351 static struct address_space_operations yaffs_file_address_operations = {
352 .readpage = yaffs_readpage,
353 .writepage = yaffs_writepage,
354 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
355 .write_begin = yaffs_write_begin,
356 .write_end = yaffs_write_end,
358 .prepare_write = yaffs_prepare_write,
359 .commit_write = yaffs_commit_write,
364 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
365 static const struct file_operations yaffs_file_operations = {
366 .read = do_sync_read,
367 .write = do_sync_write,
368 .aio_read = generic_file_aio_read,
369 .aio_write = generic_file_aio_write,
370 .mmap = generic_file_mmap,
371 .flush = yaffs_file_flush,
372 .fsync = yaffs_sync_object,
373 .splice_read = generic_file_splice_read,
374 .splice_write = generic_file_splice_write,
375 .llseek = generic_file_llseek,
378 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
380 static const struct file_operations yaffs_file_operations = {
381 .read = do_sync_read,
382 .write = do_sync_write,
383 .aio_read = generic_file_aio_read,
384 .aio_write = generic_file_aio_write,
385 .mmap = generic_file_mmap,
386 .flush = yaffs_file_flush,
387 .fsync = yaffs_sync_object,
388 .sendfile = generic_file_sendfile,
393 static const struct file_operations yaffs_file_operations = {
394 .read = generic_file_read,
395 .write = generic_file_write,
396 .mmap = generic_file_mmap,
397 .flush = yaffs_file_flush,
398 .fsync = yaffs_sync_object,
399 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
400 .sendfile = generic_file_sendfile,
405 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
406 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
408 void * kaddr = kmap_atomic(page, KM_USER0);
409 memset(kaddr + start, 0, end - start);
410 kunmap_atomic(kaddr, KM_USER0);
411 flush_dcache_page(page);
416 static const struct inode_operations yaffs_file_inode_operations = {
417 .setattr = yaffs_setattr,
418 #ifdef CONFIG_YAFFS_XATTR
419 .setxattr = yaffs_setxattr,
420 .getxattr = yaffs_getxattr,
421 .listxattr = yaffs_listxattr,
422 .removexattr = yaffs_removexattr,
426 static const struct inode_operations yaffs_symlink_inode_operations = {
427 .readlink = yaffs_readlink,
428 .follow_link = yaffs_follow_link,
429 #if (YAFFS_NEW_FOLLOW_LINK == 1)
430 .put_link = yaffs_put_link,
432 .setattr = yaffs_setattr,
433 #ifdef CONFIG_YAFFS_XATTR
434 .setxattr = yaffs_setxattr,
435 .getxattr = yaffs_getxattr,
436 .listxattr = yaffs_listxattr,
437 .removexattr = yaffs_removexattr,
441 static const struct inode_operations yaffs_dir_inode_operations = {
442 .create = yaffs_create,
443 .lookup = yaffs_lookup,
445 .unlink = yaffs_unlink,
446 .symlink = yaffs_symlink,
447 .mkdir = yaffs_mkdir,
448 .rmdir = yaffs_unlink,
449 .mknod = yaffs_mknod,
450 .rename = yaffs_rename,
451 .setattr = yaffs_setattr,
452 #ifdef CONFIG_YAFFS_XATTR
453 .setxattr = yaffs_setxattr,
454 .getxattr = yaffs_getxattr,
455 .listxattr = yaffs_listxattr,
456 .removexattr = yaffs_removexattr,
460 static const struct file_operations yaffs_dir_operations = {
461 .read = generic_read_dir,
462 .readdir = yaffs_readdir,
463 .fsync = yaffs_sync_object,
464 .llseek = yaffs_dir_llseek,
467 static const struct super_operations yaffs_super_ops = {
468 .statfs = yaffs_statfs,
470 #ifndef YAFFS_USE_OWN_IGET
471 .read_inode = yaffs_read_inode,
473 #ifdef YAFFS_HAS_PUT_INODE
474 .put_inode = yaffs_put_inode,
476 .put_super = yaffs_put_super,
477 #ifdef YAFFS_HAS_EVICT_INODE
478 .evict_inode = yaffs_evict_inode,
480 .delete_inode = yaffs_delete_inode,
481 .clear_inode = yaffs_clear_inode,
483 .sync_fs = yaffs_sync_fs,
484 .write_super = yaffs_write_super,
488 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
490 #ifdef YAFFS_USE_SETATTR_COPY
491 setattr_copy(inode,attr);
494 return inode_setattr(inode, attr);
499 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
501 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
502 truncate_setsize(inode,newsize);
505 truncate_inode_pages(&inode->i_data,newsize);
511 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
513 return yaffs_gc_control;
516 static void yaffs_gross_lock(struct yaffs_dev *dev)
518 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
519 down(&(yaffs_dev_to_lc(dev)->gross_lock));
520 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
523 static void yaffs_gross_unlock(struct yaffs_dev *dev)
525 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
526 up(&(yaffs_dev_to_lc(dev)->gross_lock));
529 #ifdef YAFFS_COMPILE_EXPORTFS
531 static struct inode *
532 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
534 return Y_IGET(sb, ino);
537 static struct dentry *
538 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
540 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
543 static struct dentry *
544 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
546 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
549 struct dentry *yaffs2_get_parent(struct dentry *dentry)
552 struct super_block *sb = dentry->d_inode->i_sb;
553 struct dentry *parent = ERR_PTR(-ENOENT);
555 unsigned long parent_ino;
556 struct yaffs_obj *d_obj;
557 struct yaffs_obj *parent_obj;
559 d_obj = yaffs_inode_to_obj(dentry->d_inode);
562 parent_obj = d_obj->parent;
564 parent_ino = yaffs_get_obj_inode(parent_obj);
565 inode = Y_IGET(sb, parent_ino);
568 parent = ERR_CAST(inode);
570 parent = d_obtain_alias(inode);
571 if (!IS_ERR(parent)) {
572 parent = ERR_PTR(-ENOMEM);
582 /* Just declare a zero structure as a NULL value implies
583 * using the default functions of exportfs.
586 static struct export_operations yaffs_export_ops =
588 .fh_to_dentry = yaffs2_fh_to_dentry,
589 .fh_to_parent = yaffs2_fh_to_parent,
590 .get_parent = yaffs2_get_parent,
595 /*-----------------------------------------------------------------*/
596 /* Directory search context allows us to unlock access to yaffs during
597 * filldir without causing problems with the directory being modified.
598 * This is similar to the tried and tested mechanism used in yaffs direct.
600 * A search context iterates along a doubly linked list of siblings in the
601 * directory. If the iterating object is deleted then this would corrupt
602 * the list iteration, likely causing a crash. The search context avoids
603 * this by using the remove_obj_fn to move the search context to the
604 * next object before the object is deleted.
606 * Many readdirs (and thus seach conexts) may be alive simulateously so
607 * each struct yaffs_dev has a list of these.
609 * A seach context lives for the duration of a readdir.
611 * All these functions must be called while yaffs is locked.
614 struct yaffs_search_context {
615 struct yaffs_dev *dev;
616 struct yaffs_obj *dir_obj;
617 struct yaffs_obj *next_return;
618 struct list_head others;
622 * yaffs_new_search() creates a new search context, initialises it and
623 * adds it to the device's search context list.
625 * Called at start of readdir.
627 static struct yaffs_search_context * yaffs_new_search(struct yaffs_obj *dir)
629 struct yaffs_dev *dev = dir->my_dev;
630 struct yaffs_search_context *sc = YMALLOC(sizeof(struct yaffs_search_context));
634 if( list_empty(&sc->dir_obj->variant.dir_variant.children))
635 sc->next_return = NULL;
637 sc->next_return = list_entry(
638 dir->variant.dir_variant.children.next,
639 struct yaffs_obj,siblings);
640 INIT_LIST_HEAD(&sc->others);
641 list_add(&sc->others,&(yaffs_dev_to_lc(dev)->search_contexts));
647 * yaffs_search_end() disposes of a search context and cleans up.
649 static void yaffs_search_end(struct yaffs_search_context * sc)
652 list_del(&sc->others);
658 * yaffs_search_advance() moves a search context to the next object.
659 * Called when the search iterates or when an object removal causes
660 * the search context to be moved to the next object.
662 static void yaffs_search_advance(struct yaffs_search_context *sc)
667 if( sc->next_return == NULL ||
668 list_empty(&sc->dir_obj->variant.dir_variant.children))
669 sc->next_return = NULL;
671 struct list_head *next = sc->next_return->siblings.next;
673 if( next == &sc->dir_obj->variant.dir_variant.children)
674 sc->next_return = NULL; /* end of list */
676 sc->next_return = list_entry(next,struct yaffs_obj,siblings);
681 * yaffs_remove_obj_callback() is called when an object is unlinked.
682 * We check open search contexts and advance any which are currently
683 * on the object being iterated.
685 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
689 struct yaffs_search_context *sc;
690 struct list_head *search_contexts = &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
693 /* Iterate through the directory search contexts.
694 * If any are currently on the object being removed, then advance
695 * the search context to the next object to prevent a hanging pointer.
697 list_for_each(i, search_contexts) {
699 sc = list_entry(i, struct yaffs_search_context,others);
700 if(sc->next_return == obj)
701 yaffs_search_advance(sc);
708 /*-----------------------------------------------------------------*/
710 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
713 unsigned char *alias;
716 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
718 yaffs_gross_lock(dev);
720 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
722 yaffs_gross_unlock(dev);
727 ret = vfs_readlink(dentry, buffer, buflen, alias);
732 #if (YAFFS_NEW_FOLLOW_LINK == 1)
733 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
737 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
741 unsigned char *alias;
743 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
745 yaffs_gross_lock(dev);
747 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
748 yaffs_gross_unlock(dev);
755 #if (YAFFS_NEW_FOLLOW_LINK == 1)
756 nd_set_link(nd, alias);
760 ret = ERR_PTR(ret_int);
763 ret = vfs_follow_link(nd, alias);
772 #if (YAFFS_NEW_FOLLOW_LINK == 1)
773 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) {
778 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
779 struct yaffs_obj *obj);
782 * Lookup is used to find objects in the fs
784 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
786 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
789 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
792 struct yaffs_obj *obj;
793 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
795 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
797 if(current != yaffs_dev_to_lc(dev)->readdir_process)
798 yaffs_gross_lock(dev);
801 (TSTR("yaffs_lookup for %d:%s\n"),
802 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name));
804 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir),
805 dentry->d_name.name);
807 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
809 /* Can't hold gross lock when calling yaffs_get_inode() */
810 if(current != yaffs_dev_to_lc(dev)->readdir_process)
811 yaffs_gross_unlock(dev);
815 (TSTR("yaffs_lookup found %d\n"), obj->obj_id));
817 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
821 (TSTR("yaffs_loookup dentry \n")));
822 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
823 * d_add even if NULL inode */
825 /*dget(dentry); // try to solve directory bug */
826 d_add(dentry, inode);
834 T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
838 /* added NCB for 2.5/6 compatability - forces add even if inode is
839 * NULL which creates dentry hash */
840 d_add(dentry, inode);
846 #ifdef YAFFS_HAS_PUT_INODE
848 /* For now put inode is just for debugging
849 * Put inode is called when the inode **structure** is put.
851 static void yaffs_put_inode(struct inode *inode)
854 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
855 atomic_read(&inode->i_count)));
861 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
863 /* Clear the association between the inode and
864 * the struct yaffs_obj.
866 obj->my_inode = NULL;
867 yaffs_inode_to_obj_lv(inode) = NULL;
869 /* If the object freeing was deferred, then the real
871 * This should fix the inode inconsistency problem.
873 yaffs_handle_defered_free(obj);
876 #ifdef YAFFS_HAS_EVICT_INODE
877 /* yaffs_evict_inode combines into one operation what was previously done in
878 * yaffs_clear_inode() and yaffs_delete_inode()
881 static void yaffs_evict_inode( struct inode *inode)
883 struct yaffs_obj *obj;
884 struct yaffs_dev *dev;
887 obj = yaffs_inode_to_obj(inode);
890 (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
891 atomic_read(&inode->i_count),
892 obj ? "object exists" : "null object"));
894 if (!inode->i_nlink && !is_bad_inode(inode))
896 truncate_inode_pages(&inode->i_data,0);
897 end_writeback(inode);
901 yaffs_gross_lock(dev);
903 yaffs_gross_unlock(dev);
907 yaffs_gross_lock(dev);
908 yaffs_unstitch_obj(inode,obj);
909 yaffs_gross_unlock(dev);
916 /* clear is called to tell the fs to release any per-inode data it holds.
917 * The object might still exist on disk and is just being thrown out of the cache
918 * or else the object has actually been deleted and we're being called via
920 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
923 static void yaffs_clear_inode(struct inode *inode)
925 struct yaffs_obj *obj;
926 struct yaffs_dev *dev;
928 obj = yaffs_inode_to_obj(inode);
931 (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
932 atomic_read(&inode->i_count),
933 obj ? "object exists" : "null object"));
937 yaffs_gross_lock(dev);
938 yaffs_unstitch_obj(inode,obj);
939 yaffs_gross_unlock(dev);
944 /* delete is called when the link count is zero and the inode
945 * is put (ie. nobody wants to know about it anymore, time to
947 * NB Must call clear_inode()
949 static void yaffs_delete_inode(struct inode *inode)
951 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
952 struct yaffs_dev *dev;
955 (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
956 atomic_read(&inode->i_count),
957 obj ? "object exists" : "null object"));
961 yaffs_gross_lock(dev);
963 yaffs_gross_unlock(dev);
965 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
966 truncate_inode_pages(&inode->i_data, 0);
973 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
974 static int yaffs_file_flush(struct file *file, fl_owner_t id)
976 static int yaffs_file_flush(struct file *file)
979 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
981 struct yaffs_dev *dev = obj->my_dev;
984 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->obj_id,
985 obj->dirty ? "dirty" : "clean"));
987 yaffs_gross_lock(dev);
989 yaffs_flush_file(obj, 1, 0);
991 yaffs_gross_unlock(dev);
996 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
998 /* Lifted from jffs2 */
1000 struct yaffs_obj *obj;
1001 unsigned char *pg_buf;
1004 struct yaffs_dev *dev;
1007 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
1008 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1009 (unsigned)PAGE_CACHE_SIZE));
1011 obj = yaffs_dentry_to_obj(f->f_dentry);
1015 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1016 BUG_ON(!PageLocked(pg));
1018 if (!PageLocked(pg))
1023 /* FIXME: Can kmap fail? */
1025 yaffs_gross_lock(dev);
1027 ret = yaffs_file_rd(obj, pg_buf,
1028 pg->index << PAGE_CACHE_SHIFT,
1031 yaffs_gross_unlock(dev);
1037 ClearPageUptodate(pg);
1040 SetPageUptodate(pg);
1044 flush_dcache_page(pg);
1047 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
1051 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1053 int ret = yaffs_readpage_nolock(f, pg);
1058 static int yaffs_readpage(struct file *f, struct page *pg)
1062 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
1063 ret=yaffs_readpage_unlock(f, pg);
1064 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
1068 /* writepage inspired by/stolen from smbfs */
1070 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1071 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1073 static int yaffs_writepage(struct page *page)
1076 struct yaffs_dev *dev;
1077 struct address_space *mapping = page->mapping;
1078 struct inode *inode;
1079 unsigned long end_index;
1081 struct yaffs_obj *obj;
1088 inode = mapping->host;
1091 i_size = i_size_read(inode);
1093 end_index = i_size >> PAGE_CACHE_SHIFT;
1095 if(page->index < end_index)
1096 n_bytes = PAGE_CACHE_SIZE;
1098 n_bytes = i_size & (PAGE_CACHE_SIZE -1);
1100 if (page->index > end_index || !n_bytes) {
1102 (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
1103 (unsigned)(page->index << PAGE_CACHE_SHIFT),
1104 (unsigned)inode->i_size));
1106 (TSTR(" -> don't care!!\n")));
1108 zero_user_segment(page,0,PAGE_CACHE_SIZE);
1109 set_page_writeback(page);
1111 end_page_writeback(page);
1116 if(n_bytes != PAGE_CACHE_SIZE)
1117 zero_user_segment(page,n_bytes,PAGE_CACHE_SIZE);
1121 buffer = kmap(page);
1123 obj = yaffs_inode_to_obj(inode);
1125 yaffs_gross_lock(dev);
1128 (TSTR("yaffs_writepage at %08x, size %08x\n"),
1129 (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes));
1131 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
1132 (int)obj->variant.file_variant.file_size, (int)inode->i_size));
1134 n_written = yaffs_wr_file(obj, buffer,
1135 page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
1137 yaffs_touch_super(dev);
1140 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
1141 (int)obj->variant.file_variant.file_size, (int)inode->i_size));
1143 yaffs_gross_unlock(dev);
1146 set_page_writeback(page);
1148 end_page_writeback(page);
1151 return (n_written == n_bytes) ? 0 : -ENOSPC;
1155 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1156 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1157 loff_t pos, unsigned len, unsigned flags,
1158 struct page **pagep, void **fsdata)
1160 struct page *pg = NULL;
1161 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1167 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
1168 pg = grab_cache_page_write_begin(mapping, index, flags);
1170 pg = __grab_cache_page(mapping, index);
1179 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
1180 (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
1183 space_held = yaffs_hold_space(filp);
1190 /* Update page if required */
1192 if (!Page_Uptodate(pg))
1193 ret = yaffs_readpage_nolock(filp, pg);
1198 /* Happy path return */
1199 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
1205 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
1207 yaffs_release_space(filp);
1210 page_cache_release(pg);
1217 static int yaffs_prepare_write(struct file *f, struct page *pg,
1218 unsigned offset, unsigned to)
1220 T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
1222 if (!Page_Uptodate(pg))
1223 return yaffs_readpage_nolock(f, pg);
1228 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
1229 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1230 loff_t pos, unsigned len, unsigned copied,
1231 struct page *pg, void *fsdadata)
1235 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
1238 addr = kva + offset_into_page;
1241 ("yaffs_write_end addr %p pos %x n_bytes %d\n",
1242 addr,(unsigned)pos, copied));
1244 ret = yaffs_file_write(filp, addr, copied, &pos);
1246 if (ret != copied) {
1248 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
1257 yaffs_release_space(filp);
1259 page_cache_release(pg);
1264 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1269 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1270 int n_bytes = to - offset;
1273 unsigned spos = pos;
1277 addr = kva + offset;
1279 saddr = (unsigned) addr;
1282 (TSTR("yaffs_commit_write addr %x pos %x n_bytes %d\n"),
1283 saddr, spos, n_bytes));
1285 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
1287 if (n_written != n_bytes) {
1289 (TSTR("yaffs_commit_write not same size n_written %d n_bytes %d\n"),
1290 n_written, n_bytes));
1299 (TSTR("yaffs_commit_write returning %d\n"),
1300 n_written == n_bytes ? 0 : n_written));
1302 return n_written == n_bytes ? 0 : n_written;
1307 static void yaffs_fill_inode_from_obj(struct inode *inode, struct yaffs_obj *obj)
1312 /* Check mode against the variant type and attempt to repair if broken. */
1313 u32 mode = obj->yst_mode;
1314 switch (obj->variant_type) {
1315 case YAFFS_OBJECT_TYPE_FILE:
1316 if (!S_ISREG(mode)) {
1317 obj->yst_mode &= ~S_IFMT;
1318 obj->yst_mode |= S_IFREG;
1322 case YAFFS_OBJECT_TYPE_SYMLINK:
1323 if (!S_ISLNK(mode)) {
1324 obj->yst_mode &= ~S_IFMT;
1325 obj->yst_mode |= S_IFLNK;
1329 case YAFFS_OBJECT_TYPE_DIRECTORY:
1330 if (!S_ISDIR(mode)) {
1331 obj->yst_mode &= ~S_IFMT;
1332 obj->yst_mode |= S_IFDIR;
1336 case YAFFS_OBJECT_TYPE_UNKNOWN:
1337 case YAFFS_OBJECT_TYPE_HARDLINK:
1338 case YAFFS_OBJECT_TYPE_SPECIAL:
1344 inode->i_flags |= S_NOATIME;
1346 inode->i_ino = obj->obj_id;
1347 inode->i_mode = obj->yst_mode;
1348 inode->i_uid = obj->yst_uid;
1349 inode->i_gid = obj->yst_gid;
1350 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1351 inode->i_blksize = inode->i_sb->s_blocksize;
1353 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1355 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1356 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1357 inode->i_atime.tv_nsec = 0;
1358 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1359 inode->i_mtime.tv_nsec = 0;
1360 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1361 inode->i_ctime.tv_nsec = 0;
1363 inode->i_rdev = obj->yst_rdev;
1364 inode->i_atime = obj->yst_atime;
1365 inode->i_mtime = obj->yst_mtime;
1366 inode->i_ctime = obj->yst_ctime;
1368 inode->i_size = yaffs_get_obj_length(obj);
1369 inode->i_blocks = (inode->i_size + 511) >> 9;
1371 inode->i_nlink = yaffs_get_obj_link_count(obj);
1374 (TSTR("yaffs_fill_inode mode %x uid %d gid %d size %d count %d\n"),
1375 inode->i_mode, inode->i_uid, inode->i_gid,
1376 (int)inode->i_size, atomic_read(&inode->i_count)));
1378 switch (obj->yst_mode & S_IFMT) {
1379 default: /* fifo, device or socket */
1380 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1381 init_special_inode(inode, obj->yst_mode,
1382 old_decode_dev(obj->yst_rdev));
1384 init_special_inode(inode, obj->yst_mode,
1385 (dev_t) (obj->yst_rdev));
1388 case S_IFREG: /* file */
1389 inode->i_op = &yaffs_file_inode_operations;
1390 inode->i_fop = &yaffs_file_operations;
1391 inode->i_mapping->a_ops =
1392 &yaffs_file_address_operations;
1394 case S_IFDIR: /* directory */
1395 inode->i_op = &yaffs_dir_inode_operations;
1396 inode->i_fop = &yaffs_dir_operations;
1398 case S_IFLNK: /* symlink */
1399 inode->i_op = &yaffs_symlink_inode_operations;
1403 yaffs_inode_to_obj_lv(inode) = obj;
1405 obj->my_inode = inode;
1409 (TSTR("yaffs_fill_inode invalid parameters\n")));
1414 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1415 struct yaffs_obj *obj)
1417 struct inode *inode;
1421 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1428 (TSTR("yaffs_get_inode for NULL object!!\n")));
1434 (TSTR("yaffs_get_inode for object %d\n"), obj->obj_id));
1436 inode = Y_IGET(sb, obj->obj_id);
1440 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1441 /* iget also increments the inode's i_count */
1442 /* NB You can't be holding gross_lock or deadlock will happen! */
1447 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1450 struct yaffs_obj *obj;
1451 int n_written, ipos;
1452 struct inode *inode;
1453 struct yaffs_dev *dev;
1455 obj = yaffs_dentry_to_obj(f->f_dentry);
1459 yaffs_gross_lock(dev);
1461 inode = f->f_dentry->d_inode;
1463 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1464 ipos = inode->i_size;
1470 (TSTR("yaffs_file_write: hey obj is null!\n")));
1473 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1474 "to object %d at %d(%x)\n"),
1475 (unsigned) n, (unsigned) n, obj->obj_id, ipos,ipos));
1477 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
1479 yaffs_touch_super(dev);
1482 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1483 (unsigned )n,(unsigned)n));
1485 if (n_written > 0) {
1488 if (ipos > inode->i_size) {
1489 inode->i_size = ipos;
1490 inode->i_blocks = (ipos + 511) >> 9;
1493 (TSTR("yaffs_file_write size updated to %d bytes, "
1495 ipos, (int)(inode->i_blocks)));
1499 yaffs_gross_unlock(dev);
1500 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
1503 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1504 /* For now we just assume few parallel writes and check against a small number. */
1505 /* Todo: need to do this with a counter to handle parallel reads better */
1507 static ssize_t yaffs_hold_space(struct file *f)
1509 struct yaffs_obj *obj;
1510 struct yaffs_dev *dev;
1515 obj = yaffs_dentry_to_obj(f->f_dentry);
1519 yaffs_gross_lock(dev);
1521 n_free_chunks = yaffs_get_n_free_chunks(dev);
1523 yaffs_gross_unlock(dev);
1525 return (n_free_chunks > 20) ? 1 : 0;
1528 static void yaffs_release_space(struct file *f)
1530 struct yaffs_obj *obj;
1531 struct yaffs_dev *dev;
1534 obj = yaffs_dentry_to_obj(f->f_dentry);
1538 yaffs_gross_lock(dev);
1541 yaffs_gross_unlock(dev);
1545 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1553 offset += i_size_read(file->f_path.dentry->d_inode);
1556 offset += file->f_pos;
1561 if (offset != file->f_pos)
1562 file->f_pos = offset;
1571 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1573 struct yaffs_obj *obj;
1574 struct yaffs_dev *dev;
1575 struct yaffs_search_context *sc;
1576 struct inode *inode = f->f_dentry->d_inode;
1577 unsigned long offset, curoffs;
1578 struct yaffs_obj *l;
1581 char name[YAFFS_MAX_NAME_LENGTH + 1];
1583 obj = yaffs_dentry_to_obj(f->f_dentry);
1586 yaffs_gross_lock(dev);
1588 yaffs_dev_to_lc(dev)->readdir_process = current;
1592 sc = yaffs_new_search(obj);
1598 T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1602 (TSTR("yaffs_readdir: entry . ino %d \n"),
1603 (int)inode->i_ino));
1604 yaffs_gross_unlock(dev);
1605 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0){
1606 yaffs_gross_lock(dev);
1609 yaffs_gross_lock(dev);
1615 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1616 (int)f->f_dentry->d_parent->d_inode->i_ino));
1617 yaffs_gross_unlock(dev);
1618 if (filldir(dirent, "..", 2, offset,
1619 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
1620 yaffs_gross_lock(dev);
1623 yaffs_gross_lock(dev);
1630 /* If the directory has changed since the open or last call to
1631 readdir, rewind to after the 2 canned entries. */
1632 if (f->f_version != inode->i_version) {
1635 f->f_version = inode->i_version;
1638 while(sc->next_return){
1640 l = sc->next_return;
1641 if (curoffs >= offset) {
1642 int this_inode = yaffs_get_obj_inode(l);
1643 int this_type = yaffs_get_obj_type(l);
1645 yaffs_get_obj_name(l, name,
1646 YAFFS_MAX_NAME_LENGTH + 1);
1648 (TSTR("yaffs_readdir: %s inode %d\n"),
1649 name, yaffs_get_obj_inode(l)));
1651 yaffs_gross_unlock(dev);
1659 yaffs_gross_lock(dev);
1663 yaffs_gross_lock(dev);
1668 yaffs_search_advance(sc);
1672 yaffs_search_end(sc);
1673 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1674 yaffs_gross_unlock(dev);
1682 * File creation. Allocate an inode, and we're done..
1685 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1688 #define YCRED(x) (x->cred)
1691 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1692 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1695 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1699 struct inode *inode;
1701 struct yaffs_obj *obj = NULL;
1702 struct yaffs_dev *dev;
1704 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1706 int error = -ENOSPC;
1707 uid_t uid = YCRED(current)->fsuid;
1708 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1710 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1715 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1716 parent->obj_id, parent->variant_type));
1719 (TSTR("yaffs_mknod: could not get parent object\n")));
1723 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1724 "mode %x dev %x\n"),
1725 dentry->d_name.name, mode, rdev));
1727 dev = parent->my_dev;
1729 yaffs_gross_lock(dev);
1731 switch (mode & S_IFMT) {
1733 /* Special (socket, fifo, device...) */
1734 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1735 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1736 obj = yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1737 gid, old_encode_dev(rdev));
1739 obj = yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1743 case S_IFREG: /* file */
1744 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1745 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1748 case S_IFDIR: /* directory */
1750 (TSTR("yaffs_mknod: making directory\n")));
1751 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1754 case S_IFLNK: /* symlink */
1755 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1756 obj = NULL; /* Do we ever get here? */
1760 /* Can not call yaffs_get_inode() with gross lock held */
1761 yaffs_gross_unlock(dev);
1764 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1765 d_instantiate(dentry, inode);
1766 update_dir_time(dir);
1768 (TSTR("yaffs_mknod created object %d count = %d\n"),
1769 obj->obj_id, atomic_read(&inode->i_count)));
1771 yaffs_fill_inode_from_obj(dir,parent);
1774 (TSTR("yaffs_mknod failed making object\n")));
1781 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1784 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1785 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1789 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1790 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1791 struct nameidata *n)
1793 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1796 T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1797 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1800 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1804 struct yaffs_dev *dev;
1805 struct yaffs_obj *obj;
1808 (TSTR("yaffs_unlink %d:%s\n"),
1810 dentry->d_name.name));
1811 obj = yaffs_inode_to_obj(dir);
1814 yaffs_gross_lock(dev);
1816 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1818 if (ret_val == YAFFS_OK) {
1819 dentry->d_inode->i_nlink--;
1821 yaffs_gross_unlock(dev);
1822 mark_inode_dirty(dentry->d_inode);
1823 update_dir_time(dir);
1826 yaffs_gross_unlock(dev);
1833 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1834 struct dentry *dentry)
1836 struct inode *inode = old_dentry->d_inode;
1837 struct yaffs_obj *obj = NULL;
1838 struct yaffs_obj *link = NULL;
1839 struct yaffs_dev *dev;
1841 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1843 obj = yaffs_inode_to_obj(inode);
1846 yaffs_gross_lock(dev);
1848 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1849 link = yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1853 old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
1854 d_instantiate(dentry, old_dentry->d_inode);
1855 atomic_inc(&old_dentry->d_inode->i_count);
1857 (TSTR("yaffs_link link count %d i_count %d\n"),
1858 old_dentry->d_inode->i_nlink,
1859 atomic_read(&old_dentry->d_inode->i_count)));
1862 yaffs_gross_unlock(dev);
1865 update_dir_time(dir);
1872 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1873 const char *symname)
1875 struct yaffs_obj *obj;
1876 struct yaffs_dev *dev;
1877 uid_t uid = YCRED(current)->fsuid;
1878 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1880 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1882 dev = yaffs_inode_to_obj(dir)->my_dev;
1883 yaffs_gross_lock(dev);
1884 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1885 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1886 yaffs_gross_unlock(dev);
1889 struct inode *inode;
1891 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1892 d_instantiate(dentry, inode);
1893 update_dir_time(dir);
1894 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1897 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1903 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1904 static int yaffs_sync_object(struct file *file, int datasync)
1906 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1911 struct yaffs_obj *obj;
1912 struct yaffs_dev *dev;
1913 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
1914 struct dentry *dentry = file->f_path.dentry;
1917 obj = yaffs_dentry_to_obj(dentry);
1921 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1922 (TSTR("yaffs_sync_object\n")));
1923 yaffs_gross_lock(dev);
1924 yaffs_flush_file(obj, 1, datasync);
1925 yaffs_gross_unlock(dev);
1930 * The VFS layer already does all the dentry stuff for rename.
1932 * NB: POSIX says you can rename an object over an old object of the same name
1934 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1935 struct inode *new_dir, struct dentry *new_dentry)
1937 struct yaffs_dev *dev;
1938 int ret_val = YAFFS_FAIL;
1939 struct yaffs_obj *target;
1941 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1942 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1944 yaffs_gross_lock(dev);
1946 /* Check if the target is an existing directory that is not empty. */
1947 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1948 new_dentry->d_name.name);
1952 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1953 !list_empty(&target->variant.dir_variant.children)) {
1955 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1957 ret_val = YAFFS_FAIL;
1959 /* Now does unlinking internally using shadowing mechanism */
1960 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_rename_obj\n")));
1962 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1963 old_dentry->d_name.name,
1964 yaffs_inode_to_obj(new_dir),
1965 new_dentry->d_name.name);
1967 yaffs_gross_unlock(dev);
1969 if (ret_val == YAFFS_OK) {
1971 new_dentry->d_inode->i_nlink--;
1972 mark_inode_dirty(new_dentry->d_inode);
1975 update_dir_time(old_dir);
1976 if(old_dir != new_dir)
1977 update_dir_time(new_dir);
1984 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1986 struct inode *inode = dentry->d_inode;
1988 struct yaffs_dev *dev;
1991 (TSTR("yaffs_setattr of object %d\n"),
1992 yaffs_inode_to_obj(inode)->obj_id));
1994 /* Fail if a requested resize >= 2GB */
1995 if (attr->ia_valid & ATTR_SIZE &&
1996 (attr->ia_size >> 31))
2000 error = inode_change_ok(inode, attr);
2004 error = yaffs_vfs_setattr(inode, attr);
2005 T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
2006 if (attr->ia_valid & ATTR_SIZE){
2007 yaffs_vfs_setsize(inode,attr->ia_size);
2008 inode->i_blocks = (inode->i_size + 511) >> 9;
2011 dev = yaffs_inode_to_obj(inode)->my_dev;
2012 if (attr->ia_valid & ATTR_SIZE){
2013 T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
2014 (int)(attr->ia_size),(int)(attr->ia_size)));
2016 yaffs_gross_lock(dev);
2017 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
2018 if(result == YAFFS_OK) {
2023 yaffs_gross_unlock(dev);
2028 (TSTR("yaffs_setattr done returning %d\n"),error));
2033 #ifdef CONFIG_YAFFS_XATTR
2034 int yaffs_setxattr(struct dentry *dentry, const char *name,
2035 const void *value, size_t size, int flags)
2037 struct inode *inode = dentry->d_inode;
2039 struct yaffs_dev *dev;
2040 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2043 (TSTR("yaffs_setxattr of object %d\n"),
2050 yaffs_gross_lock(dev);
2051 result = yaffs_set_xattrib(obj, name, value, size, flags);
2052 if(result == YAFFS_OK)
2056 yaffs_gross_unlock(dev);
2060 (TSTR("yaffs_setxattr done returning %d\n"),error));
2066 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
2069 struct inode *inode = dentry->d_inode;
2071 struct yaffs_dev *dev;
2072 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2075 (TSTR("yaffs_getxattr \"%s\" from object %d\n"),
2076 name, obj->obj_id));
2080 yaffs_gross_lock(dev);
2081 error = yaffs_get_xattrib(obj, name, buff, size);
2082 yaffs_gross_unlock(dev);
2086 (TSTR("yaffs_getxattr done returning %d\n"),error));
2091 int yaffs_removexattr(struct dentry *dentry, const char *name)
2093 struct inode *inode = dentry->d_inode;
2095 struct yaffs_dev *dev;
2096 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2099 (TSTR("yaffs_removexattr of object %d\n"),
2106 yaffs_gross_lock(dev);
2107 result = yaffs_remove_xattrib(obj, name);
2108 if(result == YAFFS_OK)
2112 yaffs_gross_unlock(dev);
2116 (TSTR("yaffs_removexattr done returning %d\n"),error));
2121 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
2123 struct inode *inode = dentry->d_inode;
2125 struct yaffs_dev *dev;
2126 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2129 (TSTR("yaffs_listxattr of object %d\n"),
2135 yaffs_gross_lock(dev);
2136 error = yaffs_list_xattrib(obj, buff, size);
2137 yaffs_gross_unlock(dev);
2141 (TSTR("yaffs_listxattr done returning %d\n"),error));
2149 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2150 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2152 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2153 struct super_block *sb = dentry->d_sb;
2154 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2155 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2157 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2159 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2161 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2164 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
2166 yaffs_gross_lock(dev);
2168 buf->f_type = YAFFS_MAGIC;
2169 buf->f_bsize = sb->s_blocksize;
2170 buf->f_namelen = 255;
2172 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2173 /* Do this if chunk size is not a power of 2 */
2175 uint64_t bytes_in_dev;
2176 uint64_t bytes_free;
2178 bytes_in_dev = ((uint64_t)((dev->param.end_block - dev->param.start_block + 1))) *
2179 ((uint64_t)(dev->param.chunks_per_block * dev->data_bytes_per_chunk));
2181 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2182 buf->f_blocks = bytes_in_dev;
2184 bytes_free = ((uint64_t)(yaffs_get_n_free_chunks(dev))) *
2185 ((uint64_t)(dev->data_bytes_per_chunk));
2187 do_div(bytes_free, sb->s_blocksize);
2189 buf->f_bfree = bytes_free;
2191 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2194 (dev->param.end_block - dev->param.start_block + 1) *
2195 dev->param.chunks_per_block /
2196 (sb->s_blocksize / dev->data_bytes_per_chunk);
2198 yaffs_get_n_free_chunks(dev) /
2199 (sb->s_blocksize / dev->data_bytes_per_chunk);
2202 (dev->param.end_block - dev->param.start_block + 1) *
2203 dev->param.chunks_per_block *
2204 (dev->data_bytes_per_chunk / sb->s_blocksize);
2207 yaffs_get_n_free_chunks(dev) *
2208 (dev->data_bytes_per_chunk / sb->s_blocksize);
2213 buf->f_bavail = buf->f_bfree;
2215 yaffs_gross_unlock(dev);
2221 static void yaffs_flush_inodes(struct super_block *sb)
2224 struct yaffs_obj *obj;
2226 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
2227 obj = yaffs_inode_to_obj(iptr);
2229 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
2231 yaffs_flush_file(obj,1,0);
2237 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2239 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2243 yaffs_flush_inodes(sb);
2244 yaffs_update_dirty_dirs(dev);
2245 yaffs_flush_whole_cache(dev);
2247 yaffs_checkpoint_save(dev);
2251 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2253 unsigned erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
2254 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2255 unsigned scattered = 0; /* Free chunks not in an erased block */
2257 if(erased_chunks < dev->n_free_chunks)
2258 scattered = (dev->n_free_chunks - erased_chunks);
2260 if(!context->bg_running)
2262 else if(scattered < (dev->param.chunks_per_block * 2))
2264 else if(erased_chunks > dev->n_free_chunks/2)
2266 else if(erased_chunks > dev->n_free_chunks/4)
2272 static int yaffs_do_sync_fs(struct super_block *sb,
2273 int request_checkpoint)
2276 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2277 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2278 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2281 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2282 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
2284 sb->s_dirt ? "dirty" : "clean",
2285 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2286 oneshot_checkpoint ? " one-shot" : "" ));
2288 yaffs_gross_lock(dev);
2289 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2290 oneshot_checkpoint) &&
2291 !dev->is_checkpointed;
2293 if (sb->s_dirt || do_checkpoint) {
2294 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2296 if(oneshot_checkpoint)
2297 yaffs_auto_checkpoint &= ~4;
2299 yaffs_gross_unlock(dev);
2305 * yaffs background thread functions .
2306 * yaffs_bg_thread_fn() the thread function
2307 * yaffs_bg_start() launches the background thread.
2308 * yaffs_bg_stop() cleans up the background thread.
2311 * The thread should only run after the yaffs is initialised
2312 * The thread should be stopped before yaffs is unmounted.
2313 * The thread should not do any writing while the fs is in read only.
2316 #ifdef YAFFS_COMPILE_BACKGROUND
2318 void yaffs_background_waker(unsigned long data)
2320 wake_up_process((struct task_struct *)data);
2323 static int yaffs_bg_thread_fn(void *data)
2325 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2326 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2327 unsigned long now = jiffies;
2328 unsigned long next_dir_update = now;
2329 unsigned long next_gc = now;
2330 unsigned long expires;
2331 unsigned int urgency;
2334 struct timer_list timer;
2336 T(YAFFS_TRACE_BACKGROUND,
2337 (TSTR("yaffs_background starting for dev %p\n"),
2340 #ifdef YAFFS_COMPILE_FREEZER
2343 while(context->bg_running){
2344 T(YAFFS_TRACE_BACKGROUND,
2345 (TSTR("yaffs_background\n")));
2347 if(kthread_should_stop())
2350 #ifdef YAFFS_COMPILE_FREEZER
2354 yaffs_gross_lock(dev);
2358 if(time_after(now, next_dir_update) && yaffs_bg_enable){
2359 yaffs_update_dirty_dirs(dev);
2360 next_dir_update = now + HZ;
2363 if(time_after(now,next_gc) && yaffs_bg_enable){
2364 if(!dev->is_checkpointed){
2365 urgency = yaffs_bg_gc_urgency(dev);
2366 gc_result = yaffs_bg_gc(dev, urgency);
2368 next_gc = now + HZ/20+1;
2369 else if(urgency > 0)
2370 next_gc = now + HZ/10+1;
2372 next_gc = now + HZ * 2;
2374 * gc not running so set to next_dir_update
2375 * to cut down on wake ups
2377 next_gc = next_dir_update;
2379 yaffs_gross_unlock(dev);
2381 expires = next_dir_update;
2382 if (time_before(next_gc,expires))
2384 if(time_before(expires,now))
2387 Y_INIT_TIMER(&timer);
2388 timer.expires = expires+1;
2389 timer.data = (unsigned long) current;
2390 timer.function = yaffs_background_waker;
2392 set_current_state(TASK_INTERRUPTIBLE);
2395 del_timer_sync(&timer);
2404 static int yaffs_bg_start(struct yaffs_dev *dev)
2407 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2412 context->bg_running = 1;
2414 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2415 (void *)dev,"yaffs-bg-%d",context->mount_id);
2417 if(IS_ERR(context->bg_thread)){
2418 retval = PTR_ERR(context->bg_thread);
2419 context->bg_thread = NULL;
2420 context->bg_running = 0;
2425 static void yaffs_bg_stop(struct yaffs_dev *dev)
2427 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2429 ctxt->bg_running = 0;
2431 if( ctxt->bg_thread){
2432 kthread_stop(ctxt->bg_thread);
2433 ctxt->bg_thread = NULL;
2437 static int yaffs_bg_thread_fn(void *data)
2442 static int yaffs_bg_start(struct yaffs_dev *dev)
2447 static void yaffs_bg_stop(struct yaffs_dev *dev)
2453 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2454 static void yaffs_write_super(struct super_block *sb)
2456 static int yaffs_write_super(struct super_block *sb)
2459 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2461 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2462 (TSTR("yaffs_write_super%s\n"),
2463 request_checkpoint ? " checkpt" : ""));
2465 yaffs_do_sync_fs(sb, request_checkpoint);
2467 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2473 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2474 static int yaffs_sync_fs(struct super_block *sb, int wait)
2476 static int yaffs_sync_fs(struct super_block *sb)
2479 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2481 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2482 (TSTR("yaffs_sync_fs%s\n"),
2483 request_checkpoint ? " checkpt" : ""));
2485 yaffs_do_sync_fs(sb, request_checkpoint);
2490 #ifdef YAFFS_USE_OWN_IGET
2492 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2494 struct inode *inode;
2495 struct yaffs_obj *obj;
2496 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2499 (TSTR("yaffs_iget for %lu\n"), ino));
2501 inode = iget_locked(sb, ino);
2503 return ERR_PTR(-ENOMEM);
2504 if (!(inode->i_state & I_NEW))
2507 /* NB This is called as a side effect of other functions, but
2508 * we had to release the lock to prevent deadlocks, so
2509 * need to lock again.
2512 yaffs_gross_lock(dev);
2514 obj = yaffs_find_by_number(dev, inode->i_ino);
2516 yaffs_fill_inode_from_obj(inode, obj);
2518 yaffs_gross_unlock(dev);
2520 unlock_new_inode(inode);
2526 static void yaffs_read_inode(struct inode *inode)
2528 /* NB This is called as a side effect of other functions, but
2529 * we had to release the lock to prevent deadlocks, so
2530 * need to lock again.
2533 struct yaffs_obj *obj;
2534 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
2537 (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
2539 if(current != yaffs_dev_to_lc(dev)->readdir_process)
2540 yaffs_gross_lock(dev);
2542 obj = yaffs_find_by_number(dev, inode->i_ino);
2544 yaffs_fill_inode_from_obj(inode, obj);
2546 if(current != yaffs_dev_to_lc(dev)->readdir_process)
2547 yaffs_gross_unlock(dev);
2552 static LIST_HEAD(yaffs_context_list);
2553 struct semaphore yaffs_context_lock;
2555 static void yaffs_put_super(struct super_block *sb)
2557 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2559 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2561 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2562 (TSTR("Shutting down yaffs background thread\n")));
2564 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2565 (TSTR("yaffs background thread shut down\n")));
2567 yaffs_gross_lock(dev);
2569 yaffs_flush_super(sb,1);
2571 if (yaffs_dev_to_lc(dev)->put_super_fn)
2572 yaffs_dev_to_lc(dev)->put_super_fn(sb);
2575 yaffs_deinitialise(dev);
2577 yaffs_gross_unlock(dev);
2579 down(&yaffs_context_lock);
2580 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2581 up(&yaffs_context_lock);
2583 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2584 YFREE(yaffs_dev_to_lc(dev)->spare_buffer);
2585 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2592 static void yaffs_mtd_put_super(struct super_block *sb)
2594 struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
2599 put_mtd_device(mtd);
2603 static void yaffs_touch_super(struct yaffs_dev *dev)
2605 struct super_block *sb = yaffs_dev_to_lc(dev)->super;
2607 T(YAFFS_TRACE_OS, (TSTR("yaffs_touch_super() sb = %p\n"), sb));
2612 struct yaffs_options {
2614 int skip_checkpoint_read;
2615 int skip_checkpoint_write;
2618 int tags_ecc_overridden;
2619 int lazy_loading_enabled;
2620 int lazy_loading_overridden;
2621 int empty_lost_and_found;
2622 int empty_lost_and_found_overridden;
2625 #define MAX_OPT_LEN 30
2626 static int yaffs_parse_options(struct yaffs_options *options, const char *options_str)
2628 char cur_opt[MAX_OPT_LEN + 1];
2632 /* Parse through the options which is a comma seperated list */
2634 while (options_str && *options_str && !error) {
2635 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2638 while(*options_str == ',')
2641 while (*options_str && *options_str != ',') {
2642 if (p < MAX_OPT_LEN) {
2643 cur_opt[p] = *options_str;
2649 if (!strcmp(cur_opt, "inband-tags"))
2650 options->inband_tags = 1;
2651 else if (!strcmp(cur_opt, "tags-ecc-off")){
2652 options->tags_ecc_on = 0;
2653 options->tags_ecc_overridden=1;
2654 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2655 options->tags_ecc_on = 1;
2656 options->tags_ecc_overridden = 1;
2657 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2658 options->lazy_loading_enabled = 0;
2659 options->lazy_loading_overridden=1;
2660 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2661 options->lazy_loading_enabled = 1;
2662 options->lazy_loading_overridden = 1;
2663 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2664 options->empty_lost_and_found = 0;
2665 options->empty_lost_and_found_overridden=1;
2666 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2667 options->empty_lost_and_found = 1;
2668 options->empty_lost_and_found_overridden=1;
2669 } else if (!strcmp(cur_opt, "no-cache"))
2670 options->no_cache = 1;
2671 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2672 options->skip_checkpoint_read = 1;
2673 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2674 options->skip_checkpoint_write = 1;
2675 else if (!strcmp(cur_opt, "no-checkpoint")) {
2676 options->skip_checkpoint_read = 1;
2677 options->skip_checkpoint_write = 1;
2679 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2688 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2689 struct super_block *sb,
2690 void *data, int silent)
2693 struct inode *inode = NULL;
2694 struct dentry *root;
2695 struct yaffs_dev *dev = 0;
2696 char devname_buf[BDEVNAME_SIZE + 1];
2697 struct mtd_info *mtd;
2699 char *data_str = (char *)data;
2700 struct yaffs_linux_context *context = NULL;
2701 struct yaffs_param *param;
2705 struct yaffs_options options;
2709 struct yaffs_linux_context *context_iterator;
2710 struct list_head *l;
2712 sb->s_magic = YAFFS_MAGIC;
2713 sb->s_op = &yaffs_super_ops;
2714 sb->s_flags |= MS_NOATIME;
2716 read_only =((sb->s_flags & MS_RDONLY) != 0);
2719 #ifdef YAFFS_COMPILE_EXPORTFS
2720 sb->s_export_op = &yaffs_export_ops;
2724 printk(KERN_INFO "yaffs: sb is NULL\n");
2725 else if (!sb->s_dev)
2726 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2727 else if (!yaffs_devname(sb, devname_buf))
2728 printk(KERN_INFO "yaffs: devname is NULL\n");
2730 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2732 yaffs_devname(sb, devname_buf),
2733 read_only ? "ro" : "rw");
2738 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2740 memset(&options, 0, sizeof(options));
2742 if (yaffs_parse_options(&options, data_str)) {
2743 /* Option parsing failed */
2748 sb->s_blocksize = PAGE_CACHE_SIZE;
2749 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2752 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffs_version));
2754 (TSTR("yaffs_read_super: block size %d\n"),
2755 (int)(sb->s_blocksize)));
2757 T(YAFFS_TRACE_ALWAYS,
2758 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2759 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2760 yaffs_devname(sb, devname_buf)));
2762 /* Check it's an mtd device..... */
2763 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2764 return NULL; /* This isn't an mtd device */
2766 /* Get the device */
2767 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2769 T(YAFFS_TRACE_ALWAYS,
2770 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2774 /* Check it's NAND */
2775 if (mtd->type != MTD_NANDFLASH) {
2776 T(YAFFS_TRACE_ALWAYS,
2777 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2782 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2783 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2784 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2785 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2786 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2787 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2788 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2789 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2790 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2791 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2792 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2793 T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
2795 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2798 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2800 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2801 T(YAFFS_TRACE_ALWAYS,
2802 (TSTR("yaffs: auto selecting yaffs2\n")));
2806 /* Added NCB 26/5/2006 for completeness */
2807 if (yaffs_version == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2808 T(YAFFS_TRACE_ALWAYS,
2809 (TSTR("yaffs: auto selecting yaffs1\n")));
2815 if (yaffs_version == 2) {
2816 /* Check for version 2 style functions */
2818 !mtd->block_isbad ||
2819 !mtd->block_markbad ||
2822 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2823 !mtd->read_oob || !mtd->write_oob) {
2826 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2828 T(YAFFS_TRACE_ALWAYS,
2829 (TSTR("yaffs: MTD device does not support required "
2834 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2835 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2836 !options.inband_tags) {
2837 T(YAFFS_TRACE_ALWAYS,
2838 (TSTR("yaffs: MTD device does not have the "
2839 "right page sizes\n")));
2843 /* Check for V1 style functions */
2847 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2848 !mtd->read_oob || !mtd->write_oob) {
2851 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2853 T(YAFFS_TRACE_ALWAYS,
2854 (TSTR("yaffs: MTD device does not support required "
2859 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2860 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2861 T(YAFFS_TRACE_ALWAYS,
2862 (TSTR("yaffs: MTD device does not support have the "
2863 "right page sizes\n")));
2868 /* OK, so if we got here, we have an MTD that's NAND and looks
2869 * like it has the right capabilities
2870 * Set the struct yaffs_dev up for mtd
2873 if (!read_only && !(mtd->flags & MTD_WRITEABLE)){
2875 printk(KERN_INFO "yaffs: mtd is read only, setting superblock read only");
2876 sb->s_flags |= MS_RDONLY;
2879 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2880 context = kmalloc(sizeof(struct yaffs_linux_context),GFP_KERNEL);
2882 if(!dev || !context ){
2892 /* Deep shit could not allocate device structure */
2893 T(YAFFS_TRACE_ALWAYS,
2894 (TSTR("yaffs_read_super: Failed trying to allocate "
2895 "struct yaffs_dev. \n")));
2898 memset(dev, 0, sizeof(struct yaffs_dev));
2899 param = &(dev->param);
2901 memset(context,0,sizeof(struct yaffs_linux_context));
2902 dev->os_context = context;
2903 INIT_LIST_HEAD(&(context->context_list));
2905 context->super = sb;
2907 dev->read_only = read_only;
2909 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2910 sb->s_fs_info = dev;
2912 sb->u.generic_sbp = dev;
2915 dev->driver_context = mtd;
2916 param->name = mtd->name;
2918 /* Set up the memory size parameters.... */
2920 n_blocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2922 param->start_block = 0;
2923 param->end_block = n_blocks - 1;
2924 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2925 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2926 param->n_reserved_blocks = 5;
2927 param->n_caches = (options.no_cache) ? 0 : 10;
2928 param->inband_tags = options.inband_tags;
2930 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2931 param->disable_lazy_load = 1;
2933 #ifdef CONFIG_YAFFS_XATTR
2934 param->enable_xattr = 1;
2936 if(options.lazy_loading_overridden)
2937 param->disable_lazy_load = !options.lazy_loading_enabled;
2939 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2940 param->no_tags_ecc = 1;
2943 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2945 param->defered_dir_update = 1;
2948 if(options.tags_ecc_overridden)
2949 param->no_tags_ecc = !options.tags_ecc_on;
2951 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2952 param->empty_lost_n_found = 1;
2955 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2956 param->refresh_period = 0;
2958 param->refresh_period = 500;
2961 #ifdef CONFIG_YAFFS__ALWAYS_CHECK_CHUNK_ERASED
2962 param->always_check_erased = 1;
2965 if(options.empty_lost_and_found_overridden)
2966 param->empty_lost_n_found = options.empty_lost_and_found;
2968 /* ... and the functions. */
2969 if (yaffs_version == 2) {
2970 param->write_chunk_tags_fn =
2971 nandmtd2_write_chunk_tags;
2972 param->read_chunk_tags_fn =
2973 nandmtd2_read_chunk_tags;
2974 param->bad_block_fn = nandmtd2_mark_block_bad;
2975 param->query_block_fn = nandmtd2_query_block;
2976 yaffs_dev_to_lc(dev)->spare_buffer = YMALLOC(mtd->oobsize);
2977 param->is_yaffs2 = 1;
2978 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2979 param->total_bytes_per_chunk = mtd->writesize;
2980 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2982 param->total_bytes_per_chunk = mtd->oobblock;
2983 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
2985 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2987 param->start_block = 0;
2988 param->end_block = n_blocks - 1;
2990 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2991 /* use the MTD interface in yaffs_mtdif1.c */
2992 param->write_chunk_tags_fn =
2993 nandmtd1_write_chunk_tags;
2994 param->read_chunk_tags_fn =
2995 nandmtd1_read_chunk_tags;
2996 param->bad_block_fn = nandmtd1_mark_block_bad;
2997 param->query_block_fn = nandmtd1_query_block;
2999 param->write_chunk_fn = nandmtd_write_chunk;
3000 param->read_chunk_fn = nandmtd_read_chunk;
3002 param->is_yaffs2 = 0;
3004 /* ... and common functions */
3005 param->erase_fn = nandmtd_erase_block;
3006 param->initialise_flash_fn = nandmtd_initialise;
3008 yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
3010 param->sb_dirty_fn = yaffs_touch_super;
3011 param->gc_control = yaffs_gc_control_callback;
3013 yaffs_dev_to_lc(dev)->super= sb;
3016 #ifndef CONFIG_YAFFS_DOES_ECC
3017 param->use_nand_ecc = 1;
3020 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3021 param->wide_tnodes_disabled = 1;
3024 param->skip_checkpt_rd = options.skip_checkpoint_read;
3025 param->skip_checkpt_wr = options.skip_checkpoint_write;
3027 down(&yaffs_context_lock);
3028 /* Get a mount id */
3030 for(mount_id=0; ! found; mount_id++){
3032 list_for_each(l,&yaffs_context_list){
3033 context_iterator = list_entry(l,struct yaffs_linux_context,context_list);
3034 if(context_iterator->mount_id == mount_id)
3038 context->mount_id = mount_id;
3040 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list), &yaffs_context_list);
3041 up(&yaffs_context_lock);
3043 /* Directory search handling...*/
3044 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
3045 param->remove_obj_fn = yaffs_remove_obj_callback;
3047 init_MUTEX(&(yaffs_dev_to_lc(dev)->gross_lock));
3049 yaffs_gross_lock(dev);
3051 err = yaffs_guts_initialise(dev);
3054 (TSTR("yaffs_read_super: guts initialised %s\n"),
3055 (err == YAFFS_OK) ? "OK" : "FAILED"));
3058 yaffs_bg_start(dev);
3060 if(!context->bg_thread)
3061 param->defered_dir_update = 0;
3064 /* Release lock before yaffs_get_inode() */
3065 yaffs_gross_unlock(dev);
3067 /* Create root inode */
3068 if (err == YAFFS_OK)
3069 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3075 inode->i_op = &yaffs_dir_inode_operations;
3076 inode->i_fop = &yaffs_dir_operations;
3078 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
3080 root = d_alloc_root(inode);
3082 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
3089 sb->s_dirt = !dev->is_checkpointed;
3090 T(YAFFS_TRACE_ALWAYS,
3091 (TSTR("yaffs_read_super: is_checkpointed %d\n"),
3092 dev->is_checkpointed));
3094 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
3099 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3100 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3103 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3106 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3107 static int yaffs_read_super(struct file_system_type *fs,
3108 int flags, const char *dev_name,
3109 void *data, struct vfsmount *mnt)
3112 return get_sb_bdev(fs, flags, dev_name, data,
3113 yaffs_internal_read_super_mtd, mnt);
3116 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3117 int flags, const char *dev_name,
3121 return get_sb_bdev(fs, flags, dev_name, data,
3122 yaffs_internal_read_super_mtd);
3126 static struct file_system_type yaffs_fs_type = {
3127 .owner = THIS_MODULE,
3129 .get_sb = yaffs_read_super,
3130 .kill_sb = kill_block_super,
3131 .fs_flags = FS_REQUIRES_DEV,
3134 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3137 return yaffs_internal_read_super(1, sb, data, silent);
3140 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3145 #ifdef CONFIG_YAFFS_YAFFS2
3147 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3148 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3151 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3154 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3155 static int yaffs2_read_super(struct file_system_type *fs,
3156 int flags, const char *dev_name, void *data,
3157 struct vfsmount *mnt)
3159 return get_sb_bdev(fs, flags, dev_name, data,
3160 yaffs2_internal_read_super_mtd, mnt);
3163 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3164 int flags, const char *dev_name,
3168 return get_sb_bdev(fs, flags, dev_name, data,
3169 yaffs2_internal_read_super_mtd);
3173 static struct file_system_type yaffs2_fs_type = {
3174 .owner = THIS_MODULE,
3176 .get_sb = yaffs2_read_super,
3177 .kill_sb = kill_block_super,
3178 .fs_flags = FS_REQUIRES_DEV,
3181 static struct super_block *yaffs2_read_super(struct super_block *sb,
3182 void *data, int silent)
3184 return yaffs_internal_read_super(2, sb, data, silent);
3187 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3191 #endif /* CONFIG_YAFFS_YAFFS2 */
3193 static struct proc_dir_entry *my_proc_entry;
3194 static struct proc_dir_entry *debug_proc_entry;
3196 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev * dev)
3198 buf += sprintf(buf, "start_block.......... %d\n", dev->param.start_block);
3199 buf += sprintf(buf, "end_block............ %d\n", dev->param.end_block);
3200 buf += sprintf(buf, "total_bytes_per_chunk %d\n", dev->param.total_bytes_per_chunk);
3201 buf += sprintf(buf, "use_nand_ecc......... %d\n", dev->param.use_nand_ecc);
3202 buf += sprintf(buf, "no_tags_ecc.......... %d\n", dev->param.no_tags_ecc);
3203 buf += sprintf(buf, "is_yaffs2............ %d\n", dev->param.is_yaffs2);
3204 buf += sprintf(buf, "inband_tags.......... %d\n", dev->param.inband_tags);
3205 buf += sprintf(buf, "empty_lost_n_found... %d\n", dev->param.empty_lost_n_found);
3206 buf += sprintf(buf, "disable_lazy_load.... %d\n", dev->param.disable_lazy_load);
3207 buf += sprintf(buf, "refresh_period....... %d\n", dev->param.refresh_period);
3208 buf += sprintf(buf, "n_caches............. %d\n", dev->param.n_caches);
3209 buf += sprintf(buf, "n_reserved_blocks.... %d\n", dev->param.n_reserved_blocks);
3210 buf += sprintf(buf, "always_check_erased.. %d\n", dev->param.always_check_erased);
3212 buf += sprintf(buf, "\n");
3218 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev * dev)
3220 buf += sprintf(buf, "data_bytes_per_chunk. %d\n", dev->data_bytes_per_chunk);
3221 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3222 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3223 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3224 buf += sprintf(buf, "blocks_in_checkpt.... %d\n", dev->blocks_in_checkpt);
3225 buf += sprintf(buf, "\n");
3226 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3227 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3228 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3229 buf += sprintf(buf, "\n");
3230 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3231 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3232 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3233 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3234 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3235 buf += sprintf(buf, "passive_gc_count..... %u\n", dev->passive_gc_count);
3236 buf += sprintf(buf, "oldest_dirty_gc_count %u\n", dev->oldest_dirty_gc_count);
3237 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3238 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3239 buf += sprintf(buf, "n_retired_writes..... %u\n", dev->n_retired_writes);
3240 buf += sprintf(buf, "n_retired_blocks..... %u\n", dev->n_retired_blocks);
3241 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3242 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3243 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n", dev->n_tags_ecc_fixed);
3244 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n", dev->n_tags_ecc_unfixed);
3245 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3246 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3247 buf += sprintf(buf, "n_unlinked_files..... %u\n", dev->n_unlinked_files);
3248 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3249 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3254 static int yaffs_proc_read(char *page,
3256 off_t offset, int count, int *eof, void *data)
3258 struct list_head *item;
3263 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3264 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3265 * This also assumes the user has posted a read buffer large
3266 * enough to hold the complete output; but that's life in /proc.
3271 /* Print header first */
3273 buf += sprintf(buf, "Multi-version YAFFS built:" __DATE__ " " __TIME__"\n");
3275 buf += sprintf(buf,"\n");
3279 down(&yaffs_context_lock);
3281 /* Locate and print the Nth entry. Order N-squared but N is small. */
3282 list_for_each(item, &yaffs_context_list) {
3283 struct yaffs_linux_context *dc = list_entry(item, struct yaffs_linux_context, context_list);
3284 struct yaffs_dev *dev = dc->dev;
3286 if (n < (step & ~1)) {
3291 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
3292 buf = yaffs_dump_dev_part0(buf, dev);
3294 buf = yaffs_dump_dev_part1(buf, dev);
3298 up(&yaffs_context_lock);
3301 return buf - page < count ? buf - page : count;
3304 static int yaffs_stats_proc_read(char *page,
3306 off_t offset, int count, int *eof, void *data)
3308 struct list_head *item;
3312 down(&yaffs_context_lock);
3314 /* Locate and print the Nth entry. Order N-squared but N is small. */
3315 list_for_each(item, &yaffs_context_list) {
3316 struct yaffs_linux_context *dc = list_entry(item, struct yaffs_linux_context, context_list);
3317 struct yaffs_dev *dev = dc->dev;
3321 erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
3323 buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
3324 n, dev->n_free_chunks, erased_chunks,
3325 dev->bg_gcs, dev->oldest_dirty_gc_count,
3326 dev->n_obj, dev->n_tnodes);
3328 up(&yaffs_context_lock);
3331 return buf - page < count ? buf - page : count;
3335 * Set the verbosity of the warnings and error messages.
3337 * Note that the names can only be a..z or _ with the current code.
3342 unsigned mask_bitfield;
3344 {"allocate", YAFFS_TRACE_ALLOCATE},
3345 {"always", YAFFS_TRACE_ALWAYS},
3346 {"background", YAFFS_TRACE_BACKGROUND},
3347 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3348 {"buffers", YAFFS_TRACE_BUFFERS},
3349 {"bug", YAFFS_TRACE_BUG},
3350 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3351 {"deletion", YAFFS_TRACE_DELETION},
3352 {"erase", YAFFS_TRACE_ERASE},
3353 {"error", YAFFS_TRACE_ERROR},
3354 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3355 {"gc", YAFFS_TRACE_GC},
3356 {"lock", YAFFS_TRACE_LOCK},
3357 {"mtd", YAFFS_TRACE_MTD},
3358 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3359 {"os", YAFFS_TRACE_OS},
3360 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3361 {"scan", YAFFS_TRACE_SCAN},
3362 {"tracing", YAFFS_TRACE_TRACING},
3363 {"sync", YAFFS_TRACE_SYNC},
3364 {"write", YAFFS_TRACE_WRITE},
3366 {"verify", YAFFS_TRACE_VERIFY},
3367 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3368 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3369 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3371 {"all", 0xffffffff},
3376 #define MAX_MASK_NAME_LENGTH 40
3377 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3378 unsigned long count, void *data)
3380 unsigned rg = 0, mask_bitfield;
3384 char substring[MAX_MASK_NAME_LENGTH + 1];
3390 rg = yaffs_trace_mask;
3392 while (!done && (pos < count)) {
3394 while ((pos < count) && isspace(buf[pos]))
3411 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3413 if (end > buf + pos) {
3414 mask_name = "numeral";
3415 len = end - (buf + pos);
3419 for (x = buf + pos, i = 0;
3420 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3421 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3423 substring[i] = '\0';
3425 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3426 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3427 mask_name = mask_flags[i].mask_name;
3428 mask_bitfield = mask_flags[i].mask_bitfield;
3435 if (mask_name != NULL) {
3439 rg &= ~mask_bitfield;
3442 rg |= mask_bitfield;
3448 rg |= mask_bitfield;
3454 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3456 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3458 if (rg & YAFFS_TRACE_ALWAYS) {
3459 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3461 flag = ((rg & mask_flags[i].mask_bitfield) ==
3462 mask_flags[i].mask_bitfield) ? '+' : '-';
3463 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3471 static int yaffs_proc_write(struct file *file, const char *buf,
3472 unsigned long count, void *data)
3474 return yaffs_proc_write_trace_options(file, buf, count, data);
3477 /* Stuff to handle installation of file systems */
3478 struct file_system_to_install {
3479 struct file_system_type *fst;
3483 static struct file_system_to_install fs_to_install[] = {
3484 {&yaffs_fs_type, 0},
3485 {&yaffs2_fs_type, 0},
3489 static int __init init_yaffs_fs(void)
3492 struct file_system_to_install *fsinst;
3494 T(YAFFS_TRACE_ALWAYS,
3495 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
3497 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3498 T(YAFFS_TRACE_ALWAYS,
3499 (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
3505 init_MUTEX(&yaffs_context_lock);
3507 /* Install the proc_fs entries */
3508 my_proc_entry = create_proc_entry("yaffs",
3512 if (my_proc_entry) {
3513 my_proc_entry->write_proc = yaffs_proc_write;
3514 my_proc_entry->read_proc = yaffs_proc_read;
3515 my_proc_entry->data = NULL;
3519 debug_proc_entry = create_proc_entry("yaffs_stats",
3523 if (debug_proc_entry) {
3524 debug_proc_entry->write_proc = NULL;
3525 debug_proc_entry->read_proc = yaffs_stats_proc_read;
3526 debug_proc_entry->data = NULL;
3530 /* Now add the file system entries */
3532 fsinst = fs_to_install;
3534 while (fsinst->fst && !error) {
3535 error = register_filesystem(fsinst->fst);
3537 fsinst->installed = 1;
3541 /* Any errors? uninstall */
3543 fsinst = fs_to_install;
3545 while (fsinst->fst) {
3546 if (fsinst->installed) {
3547 unregister_filesystem(fsinst->fst);
3548 fsinst->installed = 0;
3557 static void __exit exit_yaffs_fs(void)
3560 struct file_system_to_install *fsinst;
3562 T(YAFFS_TRACE_ALWAYS,
3563 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3565 remove_proc_entry("yaffs", YPROC_ROOT);
3566 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3568 fsinst = fs_to_install;
3570 while (fsinst->fst) {
3571 if (fsinst->installed) {
3572 unregister_filesystem(fsinst->fst);
3573 fsinst->installed = 0;
3579 module_init(init_yaffs_fs)
3580 module_exit(exit_yaffs_fs)
3582 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3583 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3584 MODULE_LICENSE("GPL");