2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * Luc van OostenRyck for numerous patches.
10 * Nick Bane for numerous patches.
11 * Nick Bane for 2.5/2.6 integration.
12 * Andras Toth for mknod rdev issue.
13 * Michael Fischer for finding the problem with inode inconsistency.
14 * Some code bodily lifted from JFFS
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
23 * This is the file system front-end to YAFFS that hooks it up to
27 * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
29 * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
31 * >> inode->u.generic_ip points to the associated struct yaffs_obj.
35 * 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(3, 6, 0))
66 #define YAFFS_HAS_WRITE_SUPER
70 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
71 #include <linux/config.h>
74 #include <linux/kernel.h>
75 #include <linux/module.h>
76 #include <linux/slab.h>
77 #include <linux/init.h>
79 #include <linux/proc_fs.h>
80 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
81 #include <linux/smp_lock.h>
83 #include <linux/pagemap.h>
84 #include <linux/mtd/mtd.h>
85 #include <linux/interrupt.h>
86 #include <linux/string.h>
87 #include <linux/ctype.h>
89 #if (YAFFS_NEW_FOLLOW_LINK == 1)
90 #include <linux/namei.h>
93 #ifdef YAFFS_COMPILE_EXPORTFS
94 #include <linux/exportfs.h>
97 #ifdef YAFFS_COMPILE_BACKGROUND
98 #include <linux/kthread.h>
99 #include <linux/delay.h>
101 #ifdef YAFFS_COMPILE_FREEZER
102 #include <linux/freezer.h>
105 #include <asm/div64.h>
107 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
109 #include <linux/statfs.h>
111 #define UnlockPage(p) unlock_page(p)
112 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
114 /* FIXME: use sb->s_id instead ? */
115 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
119 #include <linux/locks.h>
120 #define BDEVNAME_SIZE 0
121 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
123 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
124 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
130 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
131 #define YPROC_ROOT (&proc_root)
133 #define YPROC_ROOT NULL
136 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
137 #define Y_INIT_TIMER(a) init_timer(a)
139 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
142 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
143 #define YAFFS_USE_WRITE_BEGIN_END 1
145 #define YAFFS_USE_WRITE_BEGIN_END 0
148 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
149 #define YAFFS_SUPER_HAS_DIRTY
153 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
154 #define set_nlink(inode, count) do { (inode)->i_nlink = (count); } while(0)
157 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
158 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
160 uint64_t result = partition_size;
161 do_div(result, block_size);
162 return (uint32_t) result;
165 #define YCALCBLOCKS(s, b) ((s)/(b))
168 #include <linux/uaccess.h>
169 #include <linux/mtd/mtd.h>
171 #include "yportenv.h"
172 #include "yaffs_trace.h"
173 #include "yaffs_guts.h"
174 #include "yaffs_attribs.h"
176 #include "yaffs_linux.h"
178 #include "yaffs_mtdif.h"
180 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
181 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
182 unsigned int yaffs_auto_checkpoint = 1;
183 unsigned int yaffs_gc_control = 1;
184 unsigned int yaffs_bg_enable = 1;
185 unsigned int yaffs_auto_select = 1;
186 /* Module Parameters */
187 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
188 module_param(yaffs_trace_mask, uint, 0644);
189 module_param(yaffs_wr_attempts, uint, 0644);
190 module_param(yaffs_auto_checkpoint, uint, 0644);
191 module_param(yaffs_gc_control, uint, 0644);
192 module_param(yaffs_bg_enable, uint, 0644);
194 MODULE_PARM(yaffs_trace_mask, "i");
195 MODULE_PARM(yaffs_wr_attempts, "i");
196 MODULE_PARM(yaffs_auto_checkpoint, "i");
197 MODULE_PARM(yaffs_gc_control, "i");
200 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
201 /* use iget and read_inode */
202 #define Y_IGET(sb, inum) iget((sb), (inum))
205 /* Call local equivalent */
206 #define YAFFS_USE_OWN_IGET
207 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
211 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
212 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
214 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
217 #define yaffs_inode_to_obj(iptr) \
218 ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
219 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
221 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
222 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
224 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
227 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
228 #define Y_CLEAR_INODE(i) clear_inode(i)
230 #define Y_CLEAR_INODE(i) end_writeback(i)
234 #define update_dir_time(dir) do {\
235 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
238 static void yaffs_fill_inode_from_obj(struct inode *inode,
239 struct yaffs_obj *obj);
242 static void yaffs_gross_lock(struct yaffs_dev *dev)
244 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
245 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
246 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
249 static void yaffs_gross_unlock(struct yaffs_dev *dev)
251 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
252 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
256 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
258 /* Lifted from jffs2 */
260 struct yaffs_obj *obj;
261 unsigned char *pg_buf;
263 loff_t pos = ((loff_t) pg->index) << PAGE_CACHE_SHIFT;
264 struct yaffs_dev *dev;
266 yaffs_trace(YAFFS_TRACE_OS,
267 "yaffs_readpage_nolock at %lld, size %08x",
269 (unsigned)PAGE_CACHE_SIZE);
271 obj = yaffs_dentry_to_obj(f->f_dentry);
275 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
276 BUG_ON(!PageLocked(pg));
283 /* FIXME: Can kmap fail? */
285 yaffs_gross_lock(dev);
287 ret = yaffs_file_rd(obj, pg_buf, pos, PAGE_CACHE_SIZE);
289 yaffs_gross_unlock(dev);
295 ClearPageUptodate(pg);
302 flush_dcache_page(pg);
305 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
309 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
311 int ret = yaffs_readpage_nolock(f, pg);
316 static int yaffs_readpage(struct file *f, struct page *pg)
320 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
321 ret = yaffs_readpage_unlock(f, pg);
322 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
327 static void yaffs_set_super_dirty_val(struct yaffs_dev *dev, int val)
329 struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
334 # ifdef YAFFS_SUPER_HAS_DIRTY
336 struct super_block *sb = lc->super;
345 static void yaffs_set_super_dirty(struct yaffs_dev *dev)
347 yaffs_set_super_dirty_val(dev, 1);
350 static void yaffs_clear_super_dirty(struct yaffs_dev *dev)
352 yaffs_set_super_dirty_val(dev, 0);
355 static int yaffs_check_super_dirty(struct yaffs_dev *dev)
357 struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
362 # ifdef YAFFS_SUPER_HAS_DIRTY
364 struct super_block *sb = lc->super;
366 if (sb && sb->s_dirt)
374 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
375 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
377 static int yaffs_writepage(struct page *page)
380 struct yaffs_dev *dev;
381 struct address_space *mapping = page->mapping;
383 unsigned long end_index;
385 struct yaffs_obj *obj;
392 inode = mapping->host;
395 i_size = i_size_read(inode);
397 end_index = i_size >> PAGE_CACHE_SHIFT;
399 if (page->index < end_index)
400 n_bytes = PAGE_CACHE_SIZE;
402 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
404 if (page->index > end_index || !n_bytes) {
405 yaffs_trace(YAFFS_TRACE_OS,
406 "yaffs_writepage at %lld, inode size = %lld!!",
407 ((loff_t)page->index) << PAGE_CACHE_SHIFT,
409 yaffs_trace(YAFFS_TRACE_OS,
412 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
413 set_page_writeback(page);
415 end_page_writeback(page);
420 if (n_bytes != PAGE_CACHE_SIZE)
421 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
427 obj = yaffs_inode_to_obj(inode);
429 yaffs_gross_lock(dev);
431 yaffs_trace(YAFFS_TRACE_OS,
432 "yaffs_writepage at %lld, size %08x",
433 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes);
434 yaffs_trace(YAFFS_TRACE_OS,
435 "writepag0: obj = %lld, ino = %lld",
436 obj->variant.file_variant.file_size, inode->i_size);
438 n_written = yaffs_wr_file(obj, buffer,
439 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes, 0);
441 yaffs_set_super_dirty(dev);
443 yaffs_trace(YAFFS_TRACE_OS,
444 "writepag1: obj = %lld, ino = %lld",
445 obj->variant.file_variant.file_size, inode->i_size);
447 yaffs_gross_unlock(dev);
450 set_page_writeback(page);
452 end_page_writeback(page);
455 return (n_written == n_bytes) ? 0 : -ENOSPC;
458 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
459 /* For now we just assume few parallel writes and check against a small number. */
460 /* Todo: need to do this with a counter to handle parallel reads better */
462 static ssize_t yaffs_hold_space(struct file *f)
464 struct yaffs_obj *obj;
465 struct yaffs_dev *dev;
469 obj = yaffs_dentry_to_obj(f->f_dentry);
473 yaffs_gross_lock(dev);
475 n_free_chunks = yaffs_get_n_free_chunks(dev);
477 yaffs_gross_unlock(dev);
479 return (n_free_chunks > 20) ? 1 : 0;
482 static void yaffs_release_space(struct file *f)
484 struct yaffs_obj *obj;
485 struct yaffs_dev *dev;
487 obj = yaffs_dentry_to_obj(f->f_dentry);
491 yaffs_gross_lock(dev);
493 yaffs_gross_unlock(dev);
496 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
497 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
498 loff_t pos, unsigned len, unsigned flags,
499 struct page **pagep, void **fsdata)
501 struct page *pg = NULL;
502 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
508 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
509 pg = grab_cache_page_write_begin(mapping, index, flags);
511 pg = __grab_cache_page(mapping, index);
519 yaffs_trace(YAFFS_TRACE_OS,
520 "start yaffs_write_begin index %d(%x) uptodate %d",
521 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
524 space_held = yaffs_hold_space(filp);
531 /* Update page if required */
533 if (!Page_Uptodate(pg))
534 ret = yaffs_readpage_nolock(filp, pg);
539 /* Happy path return */
540 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
545 yaffs_trace(YAFFS_TRACE_OS,
546 "end yaffs_write_begin fail returning %d", ret);
548 yaffs_release_space(filp);
551 page_cache_release(pg);
558 static int yaffs_prepare_write(struct file *f, struct page *pg,
559 unsigned offset, unsigned to)
561 yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
563 if (!Page_Uptodate(pg))
564 return yaffs_readpage_nolock(f, pg);
570 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
573 struct yaffs_obj *obj;
577 struct yaffs_dev *dev;
579 obj = yaffs_dentry_to_obj(f->f_dentry);
582 yaffs_trace(YAFFS_TRACE_OS,
583 "yaffs_file_write: hey obj is null!");
589 yaffs_gross_lock(dev);
591 inode = f->f_dentry->d_inode;
593 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
594 ipos = inode->i_size;
598 yaffs_trace(YAFFS_TRACE_OS,
599 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %lld",
600 (unsigned)n, (unsigned)n, obj->obj_id, ipos);
602 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
604 yaffs_set_super_dirty(dev);
606 yaffs_trace(YAFFS_TRACE_OS,
607 "yaffs_file_write: %d(%x) bytes written",
608 (unsigned)n, (unsigned)n);
613 if (ipos > inode->i_size) {
614 inode->i_size = ipos;
615 inode->i_blocks = (ipos + 511) >> 9;
617 yaffs_trace(YAFFS_TRACE_OS,
618 "yaffs_file_write size updated to %lld bytes, %d blocks",
619 ipos, (int)(inode->i_blocks));
623 yaffs_gross_unlock(dev);
624 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
628 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
629 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
630 loff_t pos, unsigned len, unsigned copied,
631 struct page *pg, void *fsdadata)
635 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
638 addr = kva + offset_into_page;
640 yaffs_trace(YAFFS_TRACE_OS,
641 "yaffs_write_end addr %p pos %lld n_bytes %d",
644 ret = yaffs_file_write(filp, addr, copied, &pos);
647 yaffs_trace(YAFFS_TRACE_OS,
648 "yaffs_write_end not same size ret %d copied %d",
655 yaffs_release_space(filp);
657 page_cache_release(pg);
662 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
667 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
668 int n_bytes = to - offset;
674 yaffs_trace(YAFFS_TRACE_OS,
675 "yaffs_commit_write addr %p pos %lld n_bytes %d",
678 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
680 if (n_written != n_bytes) {
681 yaffs_trace(YAFFS_TRACE_OS,
682 "yaffs_commit_write not same size n_written %d n_bytes %d",
688 yaffs_trace(YAFFS_TRACE_OS,
689 "yaffs_commit_write returning %d",
690 n_written == n_bytes ? 0 : n_written);
692 return n_written == n_bytes ? 0 : n_written;
696 static struct address_space_operations yaffs_file_address_operations = {
697 .readpage = yaffs_readpage,
698 .writepage = yaffs_writepage,
699 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
700 .write_begin = yaffs_write_begin,
701 .write_end = yaffs_write_end,
703 .prepare_write = yaffs_prepare_write,
704 .commit_write = yaffs_commit_write,
709 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
710 static int yaffs_file_flush(struct file *file, fl_owner_t id)
712 static int yaffs_file_flush(struct file *file)
715 struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
717 struct yaffs_dev *dev = obj->my_dev;
719 yaffs_trace(YAFFS_TRACE_OS,
720 "yaffs_file_flush object %d (%s)",
722 obj->dirty ? "dirty" : "clean");
724 yaffs_gross_lock(dev);
726 yaffs_flush_file(obj, 1, 0);
728 yaffs_gross_unlock(dev);
734 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
735 static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync)
736 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
737 static int yaffs_sync_object(struct file *file, int datasync)
739 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
743 struct yaffs_obj *obj;
744 struct yaffs_dev *dev;
745 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
746 struct dentry *dentry = file->f_path.dentry;
749 obj = yaffs_dentry_to_obj(dentry);
753 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
754 "yaffs_sync_object");
755 yaffs_gross_lock(dev);
756 yaffs_flush_file(obj, 1, datasync);
757 yaffs_gross_unlock(dev);
762 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
763 static const struct file_operations yaffs_file_operations = {
764 .read = do_sync_read,
765 .write = do_sync_write,
766 .aio_read = generic_file_aio_read,
767 .aio_write = generic_file_aio_write,
768 .mmap = generic_file_mmap,
769 .flush = yaffs_file_flush,
770 .fsync = yaffs_sync_object,
771 .splice_read = generic_file_splice_read,
772 .splice_write = generic_file_splice_write,
773 .llseek = generic_file_llseek,
776 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
778 static const struct file_operations yaffs_file_operations = {
779 .read = do_sync_read,
780 .write = do_sync_write,
781 .aio_read = generic_file_aio_read,
782 .aio_write = generic_file_aio_write,
783 .mmap = generic_file_mmap,
784 .flush = yaffs_file_flush,
785 .fsync = yaffs_sync_object,
786 .sendfile = generic_file_sendfile,
791 static const struct file_operations yaffs_file_operations = {
792 .read = generic_file_read,
793 .write = generic_file_write,
794 .mmap = generic_file_mmap,
795 .flush = yaffs_file_flush,
796 .fsync = yaffs_sync_object,
797 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
798 .sendfile = generic_file_sendfile,
806 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
807 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
809 void *kaddr = kmap_atomic(page, KM_USER0);
810 memset(kaddr + start, 0, end - start);
811 kunmap_atomic(kaddr, KM_USER0);
812 flush_dcache_page(page);
817 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
819 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
820 truncate_setsize(inode, newsize);
823 truncate_inode_pages(&inode->i_data, newsize);
830 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
832 #ifdef YAFFS_USE_SETATTR_COPY
833 setattr_copy(inode, attr);
836 return inode_setattr(inode, attr);
841 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
843 struct inode *inode = dentry->d_inode;
845 struct yaffs_dev *dev;
847 yaffs_trace(YAFFS_TRACE_OS,
848 "yaffs_setattr of object %d",
849 yaffs_inode_to_obj(inode)->obj_id);
851 /* Fail if a requested resize >= 2GB */
852 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
857 error = inode_change_ok(inode, attr);
861 error = yaffs_vfs_setattr(inode, attr);
862 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
863 if (attr->ia_valid & ATTR_SIZE) {
864 yaffs_vfs_setsize(inode, attr->ia_size);
865 inode->i_blocks = (inode->i_size + 511) >> 9;
868 dev = yaffs_inode_to_obj(inode)->my_dev;
869 if (attr->ia_valid & ATTR_SIZE) {
870 yaffs_trace(YAFFS_TRACE_OS,
872 (int)(attr->ia_size),
873 (int)(attr->ia_size));
875 yaffs_gross_lock(dev);
876 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
877 if (result == YAFFS_OK) {
882 yaffs_gross_unlock(dev);
886 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
891 static int yaffs_setxattr(struct dentry *dentry, const char *name,
892 const void *value, size_t size, int flags)
894 struct inode *inode = dentry->d_inode;
896 struct yaffs_dev *dev;
897 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
899 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
904 yaffs_gross_lock(dev);
905 result = yaffs_set_xattrib(obj, name, value, size, flags);
906 if (result == YAFFS_OK)
910 yaffs_gross_unlock(dev);
913 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
918 static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
919 void *buff, size_t size)
921 struct inode *inode = dentry->d_inode;
923 struct yaffs_dev *dev;
924 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
926 yaffs_trace(YAFFS_TRACE_OS,
927 "yaffs_getxattr \"%s\" from object %d",
932 yaffs_gross_lock(dev);
933 error = yaffs_get_xattrib(obj, name, buff, size);
934 yaffs_gross_unlock(dev);
937 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
942 static int yaffs_removexattr(struct dentry *dentry, const char *name)
944 struct inode *inode = dentry->d_inode;
946 struct yaffs_dev *dev;
947 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
949 yaffs_trace(YAFFS_TRACE_OS,
950 "yaffs_removexattr of object %d", obj->obj_id);
955 yaffs_gross_lock(dev);
956 result = yaffs_remove_xattrib(obj, name);
957 if (result == YAFFS_OK)
961 yaffs_gross_unlock(dev);
964 yaffs_trace(YAFFS_TRACE_OS,
965 "yaffs_removexattr done returning %d", error);
970 static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
972 struct inode *inode = dentry->d_inode;
974 struct yaffs_dev *dev;
975 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
977 yaffs_trace(YAFFS_TRACE_OS,
978 "yaffs_listxattr of object %d", obj->obj_id);
982 yaffs_gross_lock(dev);
983 error = yaffs_list_xattrib(obj, buff, size);
984 yaffs_gross_unlock(dev);
987 yaffs_trace(YAFFS_TRACE_OS,
988 "yaffs_listxattr done returning %d", error);
994 static const struct inode_operations yaffs_file_inode_operations = {
995 .setattr = yaffs_setattr,
996 .setxattr = yaffs_setxattr,
997 .getxattr = yaffs_getxattr,
998 .listxattr = yaffs_listxattr,
999 .removexattr = yaffs_removexattr,
1003 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1006 unsigned char *alias;
1009 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1011 yaffs_gross_lock(dev);
1013 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1015 yaffs_gross_unlock(dev);
1020 ret = vfs_readlink(dentry, buffer, buflen, alias);
1025 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1026 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1030 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1034 unsigned char *alias;
1036 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1038 yaffs_gross_lock(dev);
1040 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1041 yaffs_gross_unlock(dev);
1047 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1048 nd_set_link(nd, alias);
1052 ret = ERR_PTR(ret_int);
1055 ret = vfs_follow_link(nd, alias);
1065 #ifdef YAFFS_HAS_PUT_INODE
1067 /* For now put inode is just for debugging
1068 * Put inode is called when the inode **structure** is put.
1070 static void yaffs_put_inode(struct inode *inode)
1072 yaffs_trace(YAFFS_TRACE_OS,
1073 "yaffs_put_inode: ino %d, count %d"),
1074 (int)inode->i_ino, atomic_read(&inode->i_count);
1079 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1080 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
1086 static const struct inode_operations yaffs_symlink_inode_operations = {
1087 .readlink = yaffs_readlink,
1088 .follow_link = yaffs_follow_link,
1089 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1090 .put_link = yaffs_put_link,
1092 .setattr = yaffs_setattr,
1093 .setxattr = yaffs_setxattr,
1094 .getxattr = yaffs_getxattr,
1095 .listxattr = yaffs_listxattr,
1096 .removexattr = yaffs_removexattr,
1099 #ifdef YAFFS_USE_OWN_IGET
1101 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1103 struct inode *inode;
1104 struct yaffs_obj *obj;
1105 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1107 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
1109 inode = iget_locked(sb, ino);
1111 return ERR_PTR(-ENOMEM);
1112 if (!(inode->i_state & I_NEW))
1115 /* NB This is called as a side effect of other functions, but
1116 * we had to release the lock to prevent deadlocks, so
1117 * need to lock again.
1120 yaffs_gross_lock(dev);
1122 obj = yaffs_find_by_number(dev, inode->i_ino);
1124 yaffs_fill_inode_from_obj(inode, obj);
1126 yaffs_gross_unlock(dev);
1128 unlock_new_inode(inode);
1134 static void yaffs_read_inode(struct inode *inode)
1136 /* NB This is called as a side effect of other functions, but
1137 * we had to release the lock to prevent deadlocks, so
1138 * need to lock again.
1141 struct yaffs_obj *obj;
1142 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
1144 yaffs_trace(YAFFS_TRACE_OS,
1145 "yaffs_read_inode for %d", (int)inode->i_ino);
1147 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1148 yaffs_gross_lock(dev);
1150 obj = yaffs_find_by_number(dev, inode->i_ino);
1152 yaffs_fill_inode_from_obj(inode, obj);
1154 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1155 yaffs_gross_unlock(dev);
1162 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1163 struct yaffs_obj *obj)
1165 struct inode *inode;
1168 yaffs_trace(YAFFS_TRACE_OS,
1169 "yaffs_get_inode for NULL super_block!!");
1175 yaffs_trace(YAFFS_TRACE_OS,
1176 "yaffs_get_inode for NULL object!!");
1181 yaffs_trace(YAFFS_TRACE_OS,
1182 "yaffs_get_inode for object %d", obj->obj_id);
1184 inode = Y_IGET(sb, obj->obj_id);
1188 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1189 /* iget also increments the inode's i_count */
1190 /* NB You can't be holding gross_lock or deadlock will happen! */
1197 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1200 #define YCRED(x) (x->cred)
1203 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1204 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1206 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1207 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1210 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1214 struct inode *inode;
1216 struct yaffs_obj *obj = NULL;
1217 struct yaffs_dev *dev;
1219 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1221 int error = -ENOSPC;
1222 uid_t uid = YCRED(current)->fsuid;
1224 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1226 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1230 yaffs_trace(YAFFS_TRACE_OS,
1231 "yaffs_mknod: parent object %d type %d",
1232 parent->obj_id, parent->variant_type);
1234 yaffs_trace(YAFFS_TRACE_OS,
1235 "yaffs_mknod: could not get parent object");
1239 yaffs_trace(YAFFS_TRACE_OS,
1240 "yaffs_mknod: making oject for %s, mode %x dev %x",
1241 dentry->d_name.name, mode, rdev);
1243 dev = parent->my_dev;
1245 yaffs_gross_lock(dev);
1247 switch (mode & S_IFMT) {
1249 /* Special (socket, fifo, device...) */
1250 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
1251 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1253 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1254 gid, old_encode_dev(rdev));
1257 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1261 case S_IFREG: /* file */
1262 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
1263 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1266 case S_IFDIR: /* directory */
1267 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
1268 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1271 case S_IFLNK: /* symlink */
1272 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
1273 obj = NULL; /* Do we ever get here? */
1277 /* Can not call yaffs_get_inode() with gross lock held */
1278 yaffs_gross_unlock(dev);
1281 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1282 d_instantiate(dentry, inode);
1283 update_dir_time(dir);
1284 yaffs_trace(YAFFS_TRACE_OS,
1285 "yaffs_mknod created object %d count = %d",
1286 obj->obj_id, atomic_read(&inode->i_count));
1288 yaffs_fill_inode_from_obj(dir, parent);
1290 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
1297 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1298 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1300 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1304 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
1305 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1310 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
1311 static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1313 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1314 static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1315 struct nameidata *n)
1316 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1317 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1318 struct nameidata *n)
1320 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1323 yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
1324 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1327 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
1328 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1330 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1331 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1332 struct nameidata *n)
1334 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1337 struct yaffs_obj *obj;
1338 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1340 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
1342 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1343 yaffs_gross_lock(dev);
1345 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
1346 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
1348 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
1350 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
1352 /* Can't hold gross lock when calling yaffs_get_inode() */
1353 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1354 yaffs_gross_unlock(dev);
1357 yaffs_trace(YAFFS_TRACE_OS,
1358 "yaffs_lookup found %d", obj->obj_id);
1360 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1362 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
1366 /* added NCB for 2.5/6 compatability - forces add even if inode is
1367 * NULL which creates dentry hash */
1368 d_add(dentry, inode);
1376 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1377 struct dentry *dentry)
1379 struct inode *inode = old_dentry->d_inode;
1380 struct yaffs_obj *obj = NULL;
1381 struct yaffs_obj *link = NULL;
1382 struct yaffs_dev *dev;
1384 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
1386 obj = yaffs_inode_to_obj(inode);
1389 yaffs_gross_lock(dev);
1391 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1393 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1397 set_nlink(old_dentry->d_inode, yaffs_get_obj_link_count(obj));
1398 d_instantiate(dentry, old_dentry->d_inode);
1399 atomic_inc(&old_dentry->d_inode->i_count);
1400 yaffs_trace(YAFFS_TRACE_OS,
1401 "yaffs_link link count %d i_count %d",
1402 old_dentry->d_inode->i_nlink,
1403 atomic_read(&old_dentry->d_inode->i_count));
1406 yaffs_gross_unlock(dev);
1409 update_dir_time(dir);
1416 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1417 const char *symname)
1419 struct yaffs_obj *obj;
1420 struct yaffs_dev *dev;
1421 uid_t uid = YCRED(current)->fsuid;
1423 (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1425 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
1427 if (strnlen(dentry->d_name.name, YAFFS_MAX_NAME_LENGTH + 1) >
1428 YAFFS_MAX_NAME_LENGTH)
1429 return -ENAMETOOLONG;
1431 if (strnlen(symname, YAFFS_MAX_ALIAS_LENGTH + 1) >
1432 YAFFS_MAX_ALIAS_LENGTH)
1433 return -ENAMETOOLONG;
1435 dev = yaffs_inode_to_obj(dir)->my_dev;
1436 yaffs_gross_lock(dev);
1437 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1438 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1439 yaffs_gross_unlock(dev);
1442 struct inode *inode;
1444 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1445 d_instantiate(dentry, inode);
1446 update_dir_time(dir);
1447 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
1450 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
1457 * The VFS layer already does all the dentry stuff for rename.
1459 * NB: POSIX says you can rename an object over an old object of the same name
1461 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1462 struct inode *new_dir, struct dentry *new_dentry)
1464 struct yaffs_dev *dev;
1465 int ret_val = YAFFS_FAIL;
1466 struct yaffs_obj *target;
1468 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
1469 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1471 yaffs_gross_lock(dev);
1473 /* Check if the target is an existing directory that is not empty. */
1474 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1475 new_dentry->d_name.name);
1477 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1478 !list_empty(&target->variant.dir_variant.children)) {
1480 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
1482 ret_val = YAFFS_FAIL;
1484 /* Now does unlinking internally using shadowing mechanism */
1485 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
1487 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1488 old_dentry->d_name.name,
1489 yaffs_inode_to_obj(new_dir),
1490 new_dentry->d_name.name);
1492 yaffs_gross_unlock(dev);
1494 if (ret_val == YAFFS_OK) {
1496 inode_dec_link_count(new_dentry->d_inode);
1498 update_dir_time(old_dir);
1499 if (old_dir != new_dir)
1500 update_dir_time(new_dir);
1510 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1514 struct yaffs_dev *dev;
1515 struct yaffs_obj *obj;
1517 yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
1518 (int)(dir->i_ino), dentry->d_name.name);
1519 obj = yaffs_inode_to_obj(dir);
1522 yaffs_gross_lock(dev);
1524 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1526 if (ret_val == YAFFS_OK) {
1527 inode_dec_link_count(dentry->d_inode);
1529 yaffs_gross_unlock(dev);
1530 update_dir_time(dir);
1533 yaffs_gross_unlock(dev);
1539 static const struct inode_operations yaffs_dir_inode_operations = {
1540 .create = yaffs_create,
1541 .lookup = yaffs_lookup,
1543 .unlink = yaffs_unlink,
1544 .symlink = yaffs_symlink,
1545 .mkdir = yaffs_mkdir,
1546 .rmdir = yaffs_unlink,
1547 .mknod = yaffs_mknod,
1548 .rename = yaffs_rename,
1549 .setattr = yaffs_setattr,
1550 .setxattr = yaffs_setxattr,
1551 .getxattr = yaffs_getxattr,
1552 .listxattr = yaffs_listxattr,
1553 .removexattr = yaffs_removexattr,
1556 /*-----------------------------------------------------------------*/
1557 /* Directory search context allows us to unlock access to yaffs during
1558 * filldir without causing problems with the directory being modified.
1559 * This is similar to the tried and tested mechanism used in yaffs direct.
1561 * A search context iterates along a doubly linked list of siblings in the
1562 * directory. If the iterating object is deleted then this would corrupt
1563 * the list iteration, likely causing a crash. The search context avoids
1564 * this by using the remove_obj_fn to move the search context to the
1565 * next object before the object is deleted.
1567 * Many readdirs (and thus seach conexts) may be alive simulateously so
1568 * each struct yaffs_dev has a list of these.
1570 * A seach context lives for the duration of a readdir.
1572 * All these functions must be called while yaffs is locked.
1575 struct yaffs_search_context {
1576 struct yaffs_dev *dev;
1577 struct yaffs_obj *dir_obj;
1578 struct yaffs_obj *next_return;
1579 struct list_head others;
1583 * yaffs_new_search() creates a new search context, initialises it and
1584 * adds it to the device's search context list.
1586 * Called at start of readdir.
1588 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
1590 struct yaffs_dev *dev = dir->my_dev;
1591 struct yaffs_search_context *sc =
1592 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
1596 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
1597 sc->next_return = NULL;
1600 list_entry(dir->variant.dir_variant.children.next,
1601 struct yaffs_obj, siblings);
1602 INIT_LIST_HEAD(&sc->others);
1603 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
1609 * yaffs_search_end() disposes of a search context and cleans up.
1611 static void yaffs_search_end(struct yaffs_search_context *sc)
1614 list_del(&sc->others);
1620 * yaffs_search_advance() moves a search context to the next object.
1621 * Called when the search iterates or when an object removal causes
1622 * the search context to be moved to the next object.
1624 static void yaffs_search_advance(struct yaffs_search_context *sc)
1629 if (sc->next_return == NULL ||
1630 list_empty(&sc->dir_obj->variant.dir_variant.children))
1631 sc->next_return = NULL;
1633 struct list_head *next = sc->next_return->siblings.next;
1635 if (next == &sc->dir_obj->variant.dir_variant.children)
1636 sc->next_return = NULL; /* end of list */
1639 list_entry(next, struct yaffs_obj, siblings);
1644 * yaffs_remove_obj_callback() is called when an object is unlinked.
1645 * We check open search contexts and advance any which are currently
1646 * on the object being iterated.
1648 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
1651 struct list_head *i;
1652 struct yaffs_search_context *sc;
1653 struct list_head *search_contexts =
1654 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
1656 /* Iterate through the directory search contexts.
1657 * If any are currently on the object being removed, then advance
1658 * the search context to the next object to prevent a hanging pointer.
1660 list_for_each(i, search_contexts) {
1661 sc = list_entry(i, struct yaffs_search_context, others);
1662 if (sc->next_return == obj)
1663 yaffs_search_advance(sc);
1669 /*-----------------------------------------------------------------*/
1671 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1673 struct yaffs_obj *obj;
1674 struct yaffs_dev *dev;
1675 struct yaffs_search_context *sc;
1676 struct inode *inode = f->f_dentry->d_inode;
1677 unsigned long offset, curoffs;
1678 struct yaffs_obj *l;
1681 char name[YAFFS_MAX_NAME_LENGTH + 1];
1683 obj = yaffs_dentry_to_obj(f->f_dentry);
1686 yaffs_gross_lock(dev);
1688 yaffs_dev_to_lc(dev)->readdir_process = current;
1692 sc = yaffs_new_search(obj);
1698 yaffs_trace(YAFFS_TRACE_OS,
1699 "yaffs_readdir: starting at %d", (int)offset);
1702 yaffs_trace(YAFFS_TRACE_OS,
1703 "yaffs_readdir: entry . ino %d",
1705 yaffs_gross_unlock(dev);
1706 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1707 yaffs_gross_lock(dev);
1710 yaffs_gross_lock(dev);
1715 yaffs_trace(YAFFS_TRACE_OS,
1716 "yaffs_readdir: entry .. ino %d",
1717 (int)f->f_dentry->d_parent->d_inode->i_ino);
1718 yaffs_gross_unlock(dev);
1719 if (filldir(dirent, "..", 2, offset,
1720 f->f_dentry->d_parent->d_inode->i_ino,
1722 yaffs_gross_lock(dev);
1725 yaffs_gross_lock(dev);
1732 /* If the directory has changed since the open or last call to
1733 readdir, rewind to after the 2 canned entries. */
1734 if (f->f_version != inode->i_version) {
1737 f->f_version = inode->i_version;
1740 while (sc->next_return) {
1742 l = sc->next_return;
1743 if (curoffs >= offset) {
1744 int this_inode = yaffs_get_obj_inode(l);
1745 int this_type = yaffs_get_obj_type(l);
1747 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1748 yaffs_trace(YAFFS_TRACE_OS,
1749 "yaffs_readdir: %s inode %d",
1750 name, yaffs_get_obj_inode(l));
1752 yaffs_gross_unlock(dev);
1757 offset, this_inode, this_type) < 0) {
1758 yaffs_gross_lock(dev);
1762 yaffs_gross_lock(dev);
1767 yaffs_search_advance(sc);
1771 yaffs_search_end(sc);
1772 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1773 yaffs_gross_unlock(dev);
1778 static const struct file_operations yaffs_dir_operations = {
1779 .read = generic_read_dir,
1780 .readdir = yaffs_readdir,
1781 .fsync = yaffs_sync_object,
1782 .llseek = generic_file_llseek,
1785 static void yaffs_fill_inode_from_obj(struct inode *inode,
1786 struct yaffs_obj *obj)
1790 /* Check mode against the variant type and attempt to repair if broken. */
1791 u32 mode = obj->yst_mode;
1792 switch (obj->variant_type) {
1793 case YAFFS_OBJECT_TYPE_FILE:
1794 if (!S_ISREG(mode)) {
1795 obj->yst_mode &= ~S_IFMT;
1796 obj->yst_mode |= S_IFREG;
1800 case YAFFS_OBJECT_TYPE_SYMLINK:
1801 if (!S_ISLNK(mode)) {
1802 obj->yst_mode &= ~S_IFMT;
1803 obj->yst_mode |= S_IFLNK;
1807 case YAFFS_OBJECT_TYPE_DIRECTORY:
1808 if (!S_ISDIR(mode)) {
1809 obj->yst_mode &= ~S_IFMT;
1810 obj->yst_mode |= S_IFDIR;
1814 case YAFFS_OBJECT_TYPE_UNKNOWN:
1815 case YAFFS_OBJECT_TYPE_HARDLINK:
1816 case YAFFS_OBJECT_TYPE_SPECIAL:
1822 inode->i_flags |= S_NOATIME;
1824 inode->i_ino = obj->obj_id;
1825 inode->i_mode = obj->yst_mode;
1826 inode->i_uid = obj->yst_uid;
1827 inode->i_gid = obj->yst_gid;
1828 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1829 inode->i_blksize = inode->i_sb->s_blocksize;
1831 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1833 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1834 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1835 inode->i_atime.tv_nsec = 0;
1836 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1837 inode->i_mtime.tv_nsec = 0;
1838 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1839 inode->i_ctime.tv_nsec = 0;
1841 inode->i_rdev = obj->yst_rdev;
1842 inode->i_atime = obj->yst_atime;
1843 inode->i_mtime = obj->yst_mtime;
1844 inode->i_ctime = obj->yst_ctime;
1846 inode->i_size = yaffs_get_obj_length(obj);
1847 inode->i_blocks = (inode->i_size + 511) >> 9;
1849 set_nlink(inode, yaffs_get_obj_link_count(obj));
1851 yaffs_trace(YAFFS_TRACE_OS,
1852 "yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
1853 inode->i_mode, inode->i_uid, inode->i_gid,
1854 inode->i_size, atomic_read(&inode->i_count));
1856 switch (obj->yst_mode & S_IFMT) {
1857 default: /* fifo, device or socket */
1858 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1859 init_special_inode(inode, obj->yst_mode,
1860 old_decode_dev(obj->yst_rdev));
1862 init_special_inode(inode, obj->yst_mode,
1863 (dev_t) (obj->yst_rdev));
1866 case S_IFREG: /* file */
1867 inode->i_op = &yaffs_file_inode_operations;
1868 inode->i_fop = &yaffs_file_operations;
1869 inode->i_mapping->a_ops =
1870 &yaffs_file_address_operations;
1872 case S_IFDIR: /* directory */
1873 inode->i_op = &yaffs_dir_inode_operations;
1874 inode->i_fop = &yaffs_dir_operations;
1876 case S_IFLNK: /* symlink */
1877 inode->i_op = &yaffs_symlink_inode_operations;
1881 yaffs_inode_to_obj_lv(inode) = obj;
1883 obj->my_inode = inode;
1886 yaffs_trace(YAFFS_TRACE_OS,
1887 "yaffs_fill_inode invalid parameters");
1895 * yaffs background thread functions .
1896 * yaffs_bg_thread_fn() the thread function
1897 * yaffs_bg_start() launches the background thread.
1898 * yaffs_bg_stop() cleans up the background thread.
1901 * The thread should only run after the yaffs is initialised
1902 * The thread should be stopped before yaffs is unmounted.
1903 * The thread should not do any writing while the fs is in read only.
1906 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
1908 unsigned erased_chunks =
1909 dev->n_erased_blocks * dev->param.chunks_per_block;
1910 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1911 unsigned scattered = 0; /* Free chunks not in an erased block */
1913 if (erased_chunks < dev->n_free_chunks)
1914 scattered = (dev->n_free_chunks - erased_chunks);
1916 if (!context->bg_running)
1918 else if (scattered < (dev->param.chunks_per_block * 2))
1920 else if (erased_chunks > dev->n_free_chunks / 2)
1922 else if (erased_chunks > dev->n_free_chunks / 4)
1928 #ifdef YAFFS_COMPILE_BACKGROUND
1930 void yaffs_background_waker(unsigned long data)
1932 wake_up_process((struct task_struct *)data);
1935 static int yaffs_bg_thread_fn(void *data)
1937 struct yaffs_dev *dev = (struct yaffs_dev *)data;
1938 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
1939 unsigned long now = jiffies;
1940 unsigned long next_dir_update = now;
1941 unsigned long next_gc = now;
1942 unsigned long expires;
1943 unsigned int urgency;
1946 struct timer_list timer;
1948 yaffs_trace(YAFFS_TRACE_BACKGROUND,
1949 "yaffs_background starting for dev %p", (void *)dev);
1951 #ifdef YAFFS_COMPILE_FREEZER
1954 while (context->bg_running) {
1955 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
1957 if (kthread_should_stop())
1960 #ifdef YAFFS_COMPILE_FREEZER
1961 if (try_to_freeze())
1964 yaffs_gross_lock(dev);
1968 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
1969 yaffs_update_dirty_dirs(dev);
1970 next_dir_update = now + HZ;
1973 if (time_after(now, next_gc) && yaffs_bg_enable) {
1974 if (!dev->is_checkpointed) {
1975 urgency = yaffs_bg_gc_urgency(dev);
1976 gc_result = yaffs_bg_gc(dev, urgency);
1978 next_gc = now + HZ / 20 + 1;
1979 else if (urgency > 0)
1980 next_gc = now + HZ / 10 + 1;
1982 next_gc = now + HZ * 2;
1985 * gc not running so set to next_dir_update
1986 * to cut down on wake ups
1988 next_gc = next_dir_update;
1991 yaffs_gross_unlock(dev);
1993 expires = next_dir_update;
1994 if (time_before(next_gc, expires))
1996 if (time_before(expires, now))
1999 Y_INIT_TIMER(&timer);
2000 timer.expires = expires + 1;
2001 timer.data = (unsigned long)current;
2002 timer.function = yaffs_background_waker;
2004 set_current_state(TASK_INTERRUPTIBLE);
2007 del_timer_sync(&timer);
2016 static int yaffs_bg_start(struct yaffs_dev *dev)
2019 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2024 context->bg_running = 1;
2026 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2027 (void *)dev, "yaffs-bg-%d",
2030 if (IS_ERR(context->bg_thread)) {
2031 retval = PTR_ERR(context->bg_thread);
2032 context->bg_thread = NULL;
2033 context->bg_running = 0;
2038 static void yaffs_bg_stop(struct yaffs_dev *dev)
2040 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2042 ctxt->bg_running = 0;
2044 if (ctxt->bg_thread) {
2045 kthread_stop(ctxt->bg_thread);
2046 ctxt->bg_thread = NULL;
2050 static int yaffs_bg_thread_fn(void *data)
2055 static int yaffs_bg_start(struct yaffs_dev *dev)
2060 static void yaffs_bg_stop(struct yaffs_dev *dev)
2066 static void yaffs_flush_inodes(struct super_block *sb)
2069 struct yaffs_obj *obj;
2071 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
2072 obj = yaffs_inode_to_obj(iptr);
2074 yaffs_trace(YAFFS_TRACE_OS,
2077 yaffs_flush_file(obj, 1, 0);
2082 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2084 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2088 yaffs_flush_inodes(sb);
2089 yaffs_update_dirty_dirs(dev);
2090 yaffs_flush_whole_cache(dev);
2092 yaffs_checkpoint_save(dev);
2095 static LIST_HEAD(yaffs_context_list);
2096 struct mutex yaffs_context_lock;
2098 static void yaffs_put_super(struct super_block *sb)
2100 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2101 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
2103 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2106 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2107 "Shutting down yaffs background thread");
2109 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2110 "yaffs background thread shut down");
2112 yaffs_gross_lock(dev);
2114 yaffs_flush_super(sb, 1);
2116 yaffs_deinitialise(dev);
2118 yaffs_gross_unlock(dev);
2120 mutex_lock(&yaffs_context_lock);
2121 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2122 mutex_unlock(&yaffs_context_lock);
2124 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2125 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
2126 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2131 yaffs_put_mtd_device(mtd);
2133 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2134 "yaffs_put_super done");
2138 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
2140 return yaffs_gc_control;
2144 #ifdef YAFFS_COMPILE_EXPORTFS
2146 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
2147 uint32_t generation)
2149 return Y_IGET(sb, ino);
2152 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
2153 struct fid *fid, int fh_len,
2156 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2157 yaffs2_nfs_get_inode);
2160 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
2161 struct fid *fid, int fh_len,
2164 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2165 yaffs2_nfs_get_inode);
2168 struct dentry *yaffs2_get_parent(struct dentry *dentry)
2171 struct super_block *sb = dentry->d_inode->i_sb;
2172 struct dentry *parent = ERR_PTR(-ENOENT);
2173 struct inode *inode;
2174 unsigned long parent_ino;
2175 struct yaffs_obj *d_obj;
2176 struct yaffs_obj *parent_obj;
2178 d_obj = yaffs_inode_to_obj(dentry->d_inode);
2181 parent_obj = d_obj->parent;
2183 parent_ino = yaffs_get_obj_inode(parent_obj);
2184 inode = Y_IGET(sb, parent_ino);
2186 if (IS_ERR(inode)) {
2187 parent = ERR_CAST(inode);
2189 parent = d_obtain_alias(inode);
2190 if (!IS_ERR(parent)) {
2191 parent = ERR_PTR(-ENOMEM);
2201 /* Just declare a zero structure as a NULL value implies
2202 * using the default functions of exportfs.
2205 static struct export_operations yaffs_export_ops = {
2206 .fh_to_dentry = yaffs2_fh_to_dentry,
2207 .fh_to_parent = yaffs2_fh_to_parent,
2208 .get_parent = yaffs2_get_parent,
2213 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
2215 /* Clear the association between the inode and
2216 * the struct yaffs_obj.
2218 obj->my_inode = NULL;
2219 yaffs_inode_to_obj_lv(inode) = NULL;
2221 /* If the object freeing was deferred, then the real
2223 * This should fix the inode inconsistency problem.
2225 yaffs_handle_defered_free(obj);
2228 #ifdef YAFFS_HAS_EVICT_INODE
2229 /* yaffs_evict_inode combines into one operation what was previously done in
2230 * yaffs_clear_inode() and yaffs_delete_inode()
2233 static void yaffs_evict_inode(struct inode *inode)
2235 struct yaffs_obj *obj;
2236 struct yaffs_dev *dev;
2239 obj = yaffs_inode_to_obj(inode);
2241 yaffs_trace(YAFFS_TRACE_OS,
2242 "yaffs_evict_inode: ino %d, count %d %s",
2243 (int)inode->i_ino, atomic_read(&inode->i_count),
2244 obj ? "object exists" : "null object");
2246 if (!inode->i_nlink && !is_bad_inode(inode))
2248 truncate_inode_pages(&inode->i_data, 0);
2249 Y_CLEAR_INODE(inode);
2251 if (deleteme && obj) {
2253 yaffs_gross_lock(dev);
2255 yaffs_gross_unlock(dev);
2259 yaffs_gross_lock(dev);
2260 yaffs_unstitch_obj(inode, obj);
2261 yaffs_gross_unlock(dev);
2266 /* clear is called to tell the fs to release any per-inode data it holds.
2267 * The object might still exist on disk and is just being thrown out of the cache
2268 * or else the object has actually been deleted and we're being called via
2270 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
2273 static void yaffs_clear_inode(struct inode *inode)
2275 struct yaffs_obj *obj;
2276 struct yaffs_dev *dev;
2278 obj = yaffs_inode_to_obj(inode);
2280 yaffs_trace(YAFFS_TRACE_OS,
2281 "yaffs_clear_inode: ino %d, count %d %s",
2282 (int)inode->i_ino, atomic_read(&inode->i_count),
2283 obj ? "object exists" : "null object");
2287 yaffs_gross_lock(dev);
2288 yaffs_unstitch_obj(inode, obj);
2289 yaffs_gross_unlock(dev);
2294 /* delete is called when the link count is zero and the inode
2295 * is put (ie. nobody wants to know about it anymore, time to
2297 * NB Must call clear_inode()
2299 static void yaffs_delete_inode(struct inode *inode)
2301 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2302 struct yaffs_dev *dev;
2304 yaffs_trace(YAFFS_TRACE_OS,
2305 "yaffs_delete_inode: ino %d, count %d %s",
2306 (int)inode->i_ino, atomic_read(&inode->i_count),
2307 obj ? "object exists" : "null object");
2311 yaffs_gross_lock(dev);
2313 yaffs_gross_unlock(dev);
2315 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
2316 truncate_inode_pages(&inode->i_data, 0);
2325 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2326 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2328 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2329 struct super_block *sb = dentry->d_sb;
2330 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2331 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2333 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2335 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2337 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2340 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
2342 yaffs_gross_lock(dev);
2344 buf->f_type = YAFFS_MAGIC;
2345 buf->f_bsize = sb->s_blocksize;
2346 buf->f_namelen = 255;
2348 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2349 /* Do this if chunk size is not a power of 2 */
2351 uint64_t bytes_in_dev;
2352 uint64_t bytes_free;
2356 ((dev->param.end_block - dev->param.start_block +
2357 1))) * ((uint64_t) (dev->param.chunks_per_block *
2358 dev->data_bytes_per_chunk));
2360 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2361 buf->f_blocks = bytes_in_dev;
2363 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
2364 ((uint64_t) (dev->data_bytes_per_chunk));
2366 do_div(bytes_free, sb->s_blocksize);
2368 buf->f_bfree = bytes_free;
2370 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2373 (dev->param.end_block - dev->param.start_block + 1) *
2374 dev->param.chunks_per_block /
2375 (sb->s_blocksize / dev->data_bytes_per_chunk);
2377 yaffs_get_n_free_chunks(dev) /
2378 (sb->s_blocksize / dev->data_bytes_per_chunk);
2381 (dev->param.end_block - dev->param.start_block + 1) *
2382 dev->param.chunks_per_block *
2383 (dev->data_bytes_per_chunk / sb->s_blocksize);
2386 yaffs_get_n_free_chunks(dev) *
2387 (dev->data_bytes_per_chunk / sb->s_blocksize);
2392 buf->f_bavail = buf->f_bfree;
2394 yaffs_gross_unlock(dev);
2400 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
2403 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2404 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2405 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2407 int dirty = yaffs_check_super_dirty(dev);
2409 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2410 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
2412 dirty ? "dirty" : "clean",
2413 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2414 oneshot_checkpoint ? " one-shot" : "");
2416 yaffs_gross_lock(dev);
2417 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2418 oneshot_checkpoint) && !dev->is_checkpointed;
2420 if (dirty || do_checkpoint) {
2421 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2422 yaffs_clear_super_dirty(dev);
2423 if (oneshot_checkpoint)
2424 yaffs_auto_checkpoint &= ~4;
2426 yaffs_gross_unlock(dev);
2432 #ifdef YAFFS_HAS_WRITE_SUPER
2433 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2434 static void yaffs_write_super(struct super_block *sb)
2436 static int yaffs_write_super(struct super_block *sb)
2439 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2441 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2442 "yaffs_write_super %s",
2443 request_checkpoint ? " checkpt" : "");
2445 yaffs_do_sync_fs(sb, request_checkpoint);
2447 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2453 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2454 static int yaffs_sync_fs(struct super_block *sb, int wait)
2456 static int yaffs_sync_fs(struct super_block *sb)
2459 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2461 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2462 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
2464 yaffs_do_sync_fs(sb, request_checkpoint);
2471 static const struct super_operations yaffs_super_ops = {
2472 .statfs = yaffs_statfs,
2474 #ifndef YAFFS_USE_OWN_IGET
2475 .read_inode = yaffs_read_inode,
2477 #ifdef YAFFS_HAS_PUT_INODE
2478 .put_inode = yaffs_put_inode,
2480 .put_super = yaffs_put_super,
2481 #ifdef YAFFS_HAS_EVICT_INODE
2482 .evict_inode = yaffs_evict_inode,
2484 .delete_inode = yaffs_delete_inode,
2485 .clear_inode = yaffs_clear_inode,
2487 .sync_fs = yaffs_sync_fs,
2488 #ifdef YAFFS_HAS_WRITE_SUPER
2489 .write_super = yaffs_write_super,
2493 struct yaffs_options {
2495 int skip_checkpoint_read;
2496 int skip_checkpoint_write;
2499 int tags_ecc_overridden;
2500 int lazy_loading_enabled;
2501 int lazy_loading_overridden;
2502 int empty_lost_and_found;
2503 int empty_lost_and_found_overridden;
2504 int disable_summary;
2507 #define MAX_OPT_LEN 30
2508 static int yaffs_parse_options(struct yaffs_options *options,
2509 const char *options_str)
2511 char cur_opt[MAX_OPT_LEN + 1];
2515 /* Parse through the options which is a comma seperated list */
2517 while (options_str && *options_str && !error) {
2518 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2521 while (*options_str == ',')
2524 while (*options_str && *options_str != ',') {
2525 if (p < MAX_OPT_LEN) {
2526 cur_opt[p] = *options_str;
2532 if (!strcmp(cur_opt, "inband-tags")) {
2533 options->inband_tags = 1;
2534 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2535 options->tags_ecc_on = 0;
2536 options->tags_ecc_overridden = 1;
2537 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2538 options->tags_ecc_on = 1;
2539 options->tags_ecc_overridden = 1;
2540 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2541 options->lazy_loading_enabled = 0;
2542 options->lazy_loading_overridden = 1;
2543 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2544 options->lazy_loading_enabled = 1;
2545 options->lazy_loading_overridden = 1;
2546 } else if (!strcmp(cur_opt, "disable-summary")) {
2547 options->disable_summary = 1;
2548 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2549 options->empty_lost_and_found = 0;
2550 options->empty_lost_and_found_overridden = 1;
2551 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2552 options->empty_lost_and_found = 1;
2553 options->empty_lost_and_found_overridden = 1;
2554 } else if (!strcmp(cur_opt, "no-cache")) {
2555 options->no_cache = 1;
2556 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2557 options->skip_checkpoint_read = 1;
2558 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2559 options->skip_checkpoint_write = 1;
2560 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2561 options->skip_checkpoint_read = 1;
2562 options->skip_checkpoint_write = 1;
2564 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2574 static struct dentry *yaffs_make_root(struct inode *inode)
2576 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
2577 struct dentry *root = d_alloc_root(inode);
2584 return d_make_root(inode);
2591 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2592 struct super_block *sb,
2593 void *data, int silent)
2596 struct inode *inode = NULL;
2597 struct dentry *root;
2598 struct yaffs_dev *dev = 0;
2599 char devname_buf[BDEVNAME_SIZE + 1];
2600 struct mtd_info *mtd;
2602 char *data_str = (char *)data;
2603 struct yaffs_linux_context *context = NULL;
2604 struct yaffs_param *param;
2608 struct yaffs_options options;
2612 struct yaffs_linux_context *context_iterator;
2613 struct list_head *l;
2616 printk(KERN_INFO "yaffs: sb is NULL\n");
2620 sb->s_magic = YAFFS_MAGIC;
2621 sb->s_op = &yaffs_super_ops;
2622 sb->s_flags |= MS_NOATIME;
2624 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2626 #ifdef YAFFS_COMPILE_EXPORTFS
2627 sb->s_export_op = &yaffs_export_ops;
2631 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2632 else if (!yaffs_devname(sb, devname_buf))
2633 printk(KERN_INFO "yaffs: devname is NULL\n");
2635 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2637 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2642 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2644 memset(&options, 0, sizeof(options));
2646 if (yaffs_parse_options(&options, data_str)) {
2647 /* Option parsing failed */
2651 sb->s_blocksize = PAGE_CACHE_SIZE;
2652 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2654 yaffs_trace(YAFFS_TRACE_OS,
2655 "yaffs_read_super: Using yaffs%d", yaffs_version);
2656 yaffs_trace(YAFFS_TRACE_OS,
2657 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2659 yaffs_trace(YAFFS_TRACE_ALWAYS,
2660 "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
2661 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2662 yaffs_devname(sb, devname_buf));
2664 /* Get the device */
2665 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2667 yaffs_trace(YAFFS_TRACE_ALWAYS,
2668 "yaffs: MTD device %u either not valid or unavailable",
2673 if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2674 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2678 /* Added NCB 26/5/2006 for completeness */
2679 if (yaffs_version == 2 && !options.inband_tags
2680 && WRITE_SIZE(mtd) == 512) {
2681 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2685 if(yaffs_verify_mtd(mtd, yaffs_version, options.inband_tags) < 0)
2688 /* OK, so if we got here, we have an MTD that's NAND and looks
2689 * like it has the right capabilities
2690 * Set the struct yaffs_dev up for mtd
2693 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2696 "yaffs: mtd is read only, setting superblock read only\n"
2698 sb->s_flags |= MS_RDONLY;
2701 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2702 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2704 if (!dev || !context) {
2714 /* Deep shit could not allocate device structure */
2715 yaffs_trace(YAFFS_TRACE_ALWAYS,
2716 "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
2720 memset(dev, 0, sizeof(struct yaffs_dev));
2721 param = &(dev->param);
2723 memset(context, 0, sizeof(struct yaffs_linux_context));
2724 dev->os_context = context;
2725 INIT_LIST_HEAD(&(context->context_list));
2727 context->super = sb;
2729 dev->read_only = read_only;
2731 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2732 sb->s_fs_info = dev;
2734 sb->u.generic_sbp = dev;
2738 dev->driver_context = mtd;
2739 param->name = mtd->name;
2741 /* Set up the memory size parameters.... */
2744 param->n_reserved_blocks = 5;
2745 param->n_caches = (options.no_cache) ? 0 : 10;
2746 param->inband_tags = options.inband_tags;
2748 param->enable_xattr = 1;
2749 if (options.lazy_loading_overridden)
2750 param->disable_lazy_load = !options.lazy_loading_enabled;
2752 param->defered_dir_update = 1;
2754 if (options.tags_ecc_overridden)
2755 param->no_tags_ecc = !options.tags_ecc_on;
2757 param->empty_lost_n_found = 1;
2758 param->refresh_period = 500;
2759 param->disable_summary = options.disable_summary;
2761 if (options.empty_lost_and_found_overridden)
2762 param->empty_lost_n_found = options.empty_lost_and_found;
2764 /* ... and the functions. */
2765 if (yaffs_version == 2) {
2766 param->is_yaffs2 = 1;
2767 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2768 param->total_bytes_per_chunk = mtd->writesize;
2769 param->chunks_per_block = mtd->erasesize / mtd->writesize;
2771 param->total_bytes_per_chunk = mtd->oobblock;
2772 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
2774 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2776 param->start_block = 0;
2777 param->end_block = n_blocks - 1;
2779 param->is_yaffs2 = 0;
2780 n_blocks = YCALCBLOCKS(mtd->size,
2781 YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
2783 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
2784 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
2787 param->start_block = 0;
2788 param->end_block = n_blocks - 1;
2790 yaffs_mtd_drv_install(dev);
2792 param->sb_dirty_fn = yaffs_set_super_dirty;
2793 param->gc_control_fn = yaffs_gc_control_callback;
2795 yaffs_dev_to_lc(dev)->super = sb;
2797 param->use_nand_ecc = 1;
2799 param->skip_checkpt_rd = options.skip_checkpoint_read;
2800 param->skip_checkpt_wr = options.skip_checkpoint_write;
2802 mutex_lock(&yaffs_context_lock);
2803 /* Get a mount id */
2805 for (mount_id = 0; !found; mount_id++) {
2807 list_for_each(l, &yaffs_context_list) {
2809 list_entry(l, struct yaffs_linux_context,
2811 if (context_iterator->mount_id == mount_id)
2815 context->mount_id = mount_id;
2817 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
2818 &yaffs_context_list);
2819 mutex_unlock(&yaffs_context_lock);
2821 /* Directory search handling... */
2822 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
2823 param->remove_obj_fn = yaffs_remove_obj_callback;
2825 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
2827 yaffs_gross_lock(dev);
2829 err = yaffs_guts_initialise(dev);
2831 yaffs_trace(YAFFS_TRACE_OS,
2832 "yaffs_read_super: guts initialised %s",
2833 (err == YAFFS_OK) ? "OK" : "FAILED");
2835 if (err == YAFFS_OK)
2836 yaffs_bg_start(dev);
2838 if (!context->bg_thread)
2839 param->defered_dir_update = 0;
2841 sb->s_maxbytes = yaffs_max_file_size(dev);
2843 /* Release lock before yaffs_get_inode() */
2844 yaffs_gross_unlock(dev);
2846 /* Create root inode */
2847 if (err == YAFFS_OK)
2848 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
2853 inode->i_op = &yaffs_dir_inode_operations;
2854 inode->i_fop = &yaffs_dir_operations;
2856 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
2858 root = yaffs_make_root(inode);
2864 if(!dev->is_checkpointed)
2865 yaffs_set_super_dirty(dev);
2867 yaffs_trace(YAFFS_TRACE_ALWAYS,
2868 "yaffs_read_super: is_checkpointed %d",
2869 dev->is_checkpointed);
2871 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
2875 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2876 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2879 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2882 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
2883 static struct dentry *yaffs_mount(struct file_system_type *fs_type, int flags,
2884 const char *dev_name, void *data)
2886 return mount_bdev(fs_type, flags, dev_name, data, yaffs_internal_read_super_mtd);
2888 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2889 static int yaffs_read_super(struct file_system_type *fs,
2890 int flags, const char *dev_name,
2891 void *data, struct vfsmount *mnt)
2894 return get_sb_bdev(fs, flags, dev_name, data,
2895 yaffs_internal_read_super_mtd, mnt);
2898 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2899 int flags, const char *dev_name,
2903 return get_sb_bdev(fs, flags, dev_name, data,
2904 yaffs_internal_read_super_mtd);
2908 static struct file_system_type yaffs_fs_type = {
2909 .owner = THIS_MODULE,
2911 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
2912 .mount = yaffs_mount,
2914 .get_sb = yaffs_read_super,
2916 .kill_sb = kill_block_super,
2917 .fs_flags = FS_REQUIRES_DEV,
2920 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2923 return yaffs_internal_read_super(1, sb, data, silent);
2926 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2931 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2932 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2935 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2938 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
2939 static struct dentry *yaffs2_mount(struct file_system_type *fs_type, int flags,
2940 const char *dev_name, void *data)
2942 return mount_bdev(fs_type, flags, dev_name, data, yaffs2_internal_read_super_mtd);
2944 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2945 static int yaffs2_read_super(struct file_system_type *fs,
2946 int flags, const char *dev_name, void *data,
2947 struct vfsmount *mnt)
2949 return get_sb_bdev(fs, flags, dev_name, data,
2950 yaffs2_internal_read_super_mtd, mnt);
2953 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2954 int flags, const char *dev_name,
2958 return get_sb_bdev(fs, flags, dev_name, data,
2959 yaffs2_internal_read_super_mtd);
2963 static struct file_system_type yaffs2_fs_type = {
2964 .owner = THIS_MODULE,
2966 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
2967 .mount = yaffs2_mount,
2969 .get_sb = yaffs2_read_super,
2971 .kill_sb = kill_block_super,
2972 .fs_flags = FS_REQUIRES_DEV,
2975 static struct super_block *yaffs2_read_super(struct super_block *sb,
2976 void *data, int silent)
2978 return yaffs_internal_read_super(2, sb, data, silent);
2981 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2986 static struct proc_dir_entry *my_proc_entry;
2988 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
2990 struct yaffs_param *param = &dev->param;
2992 buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
2993 buf += sprintf(buf, "end_block............ %d\n", param->end_block);
2994 buf += sprintf(buf, "total_bytes_per_chunk %d\n",
2995 param->total_bytes_per_chunk);
2996 buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
2997 buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
2998 buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
2999 buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
3000 buf += sprintf(buf, "empty_lost_n_found... %d\n",
3001 param->empty_lost_n_found);
3002 buf += sprintf(buf, "disable_lazy_load.... %d\n",
3003 param->disable_lazy_load);
3004 buf += sprintf(buf, "refresh_period....... %d\n",
3005 param->refresh_period);
3006 buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
3007 buf += sprintf(buf, "n_reserved_blocks.... %d\n",
3008 param->n_reserved_blocks);
3009 buf += sprintf(buf, "always_check_erased.. %d\n",
3010 param->always_check_erased);
3011 buf += sprintf(buf, "\n");
3016 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3018 buf += sprintf(buf, "max file size....... %lld\n",
3019 (long long) yaffs_max_file_size(dev));
3020 buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
3021 dev->data_bytes_per_chunk);
3022 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3023 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3024 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3025 buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
3026 dev->blocks_in_checkpt);
3027 buf += sprintf(buf, "\n");
3028 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3029 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3030 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3031 buf += sprintf(buf, "\n");
3032 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3033 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3034 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3035 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3036 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3037 buf += sprintf(buf, "passive_gc_count..... %u\n",
3038 dev->passive_gc_count);
3039 buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
3040 dev->oldest_dirty_gc_count);
3041 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3042 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3043 buf += sprintf(buf, "n_retried_writes..... %u\n",
3044 dev->n_retried_writes);
3045 buf += sprintf(buf, "n_retired_blocks..... %u\n",
3046 dev->n_retired_blocks);
3047 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3048 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3049 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
3050 dev->n_tags_ecc_fixed);
3051 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
3052 dev->n_tags_ecc_unfixed);
3053 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3054 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3055 buf += sprintf(buf, "n_unlinked_files..... %u\n",
3056 dev->n_unlinked_files);
3057 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3058 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3059 buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
3060 buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
3065 static int yaffs_proc_read(char *page,
3067 off_t offset, int count, int *eof, void *data)
3069 struct list_head *item;
3074 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3075 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3076 * This also assumes the user has posted a read buffer large
3077 * enough to hold the complete output; but that's life in /proc.
3082 /* Print header first */
3086 "Multi-version YAFFS built:" __DATE__ " " __TIME__
3089 buf += sprintf(buf, "\n");
3093 mutex_lock(&yaffs_context_lock);
3095 /* Locate and print the Nth entry. Order N-squared but N is small. */
3096 list_for_each(item, &yaffs_context_list) {
3097 struct yaffs_linux_context *dc =
3098 list_entry(item, struct yaffs_linux_context,
3100 struct yaffs_dev *dev = dc->dev;
3102 if (n < (step & ~1)) {
3106 if ((step & 1) == 0) {
3108 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3110 buf = yaffs_dump_dev_part0(buf, dev);
3112 buf = yaffs_dump_dev_part1(buf, dev);
3117 mutex_unlock(&yaffs_context_lock);
3120 return buf - page < count ? buf - page : count;
3124 * Set the verbosity of the warnings and error messages.
3126 * Note that the names can only be a..z or _ with the current code.
3131 unsigned mask_bitfield;
3133 {"allocate", YAFFS_TRACE_ALLOCATE},
3134 {"always", YAFFS_TRACE_ALWAYS},
3135 {"background", YAFFS_TRACE_BACKGROUND},
3136 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3137 {"buffers", YAFFS_TRACE_BUFFERS},
3138 {"bug", YAFFS_TRACE_BUG},
3139 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3140 {"deletion", YAFFS_TRACE_DELETION},
3141 {"erase", YAFFS_TRACE_ERASE},
3142 {"error", YAFFS_TRACE_ERROR},
3143 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3144 {"gc", YAFFS_TRACE_GC},
3145 {"lock", YAFFS_TRACE_LOCK},
3146 {"mtd", YAFFS_TRACE_MTD},
3147 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3148 {"os", YAFFS_TRACE_OS},
3149 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3150 {"scan", YAFFS_TRACE_SCAN},
3151 {"mount", YAFFS_TRACE_MOUNT},
3152 {"tracing", YAFFS_TRACE_TRACING},
3153 {"sync", YAFFS_TRACE_SYNC},
3154 {"write", YAFFS_TRACE_WRITE},
3155 {"verify", YAFFS_TRACE_VERIFY},
3156 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3157 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3158 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3159 {"all", 0xffffffff},
3164 #define MAX_MASK_NAME_LENGTH 40
3165 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3166 unsigned long count, void *data)
3168 unsigned rg = 0, mask_bitfield;
3172 char substring[MAX_MASK_NAME_LENGTH + 1];
3178 rg = yaffs_trace_mask;
3180 while (!done && (pos < count)) {
3182 while ((pos < count) && isspace(buf[pos]))
3199 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3201 if (end > buf + pos) {
3202 mask_name = "numeral";
3203 len = end - (buf + pos);
3207 for (x = buf + pos, i = 0;
3208 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3209 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3211 substring[i] = '\0';
3213 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3214 if (strcmp(substring, mask_flags[i].mask_name)
3216 mask_name = mask_flags[i].mask_name;
3218 mask_flags[i].mask_bitfield;
3225 if (mask_name != NULL) {
3229 rg &= ~mask_bitfield;
3232 rg |= mask_bitfield;
3238 rg |= mask_bitfield;
3244 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3246 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3248 if (rg & YAFFS_TRACE_ALWAYS) {
3249 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3251 flag = ((rg & mask_flags[i].mask_bitfield) ==
3252 mask_flags[i].mask_bitfield) ? '+' : '-';
3253 printk(KERN_DEBUG "%c%s\n", flag,
3254 mask_flags[i].mask_name);
3261 static int yaffs_proc_write(struct file *file, const char *buf,
3262 unsigned long count, void *data)
3264 return yaffs_proc_write_trace_options(file, buf, count, data);
3267 /* Stuff to handle installation of file systems */
3268 struct file_system_to_install {
3269 struct file_system_type *fst;
3273 static struct file_system_to_install fs_to_install[] = {
3274 {&yaffs_fs_type, 0},
3275 {&yaffs2_fs_type, 0},
3279 static int __init init_yaffs_fs(void)
3282 struct file_system_to_install *fsinst;
3284 yaffs_trace(YAFFS_TRACE_ALWAYS,
3285 "yaffs built " __DATE__ " " __TIME__ " Installing.");
3287 mutex_init(&yaffs_context_lock);
3289 /* Install the proc_fs entries */
3290 my_proc_entry = create_proc_entry("yaffs",
3291 S_IRUGO | S_IFREG, YPROC_ROOT);
3293 if (my_proc_entry) {
3294 my_proc_entry->write_proc = yaffs_proc_write;
3295 my_proc_entry->read_proc = yaffs_proc_read;
3296 my_proc_entry->data = NULL;
3301 /* Now add the file system entries */
3303 fsinst = fs_to_install;
3305 while (fsinst->fst && !error) {
3306 error = register_filesystem(fsinst->fst);
3308 fsinst->installed = 1;
3312 /* Any errors? uninstall */
3314 fsinst = fs_to_install;
3316 while (fsinst->fst) {
3317 if (fsinst->installed) {
3318 unregister_filesystem(fsinst->fst);
3319 fsinst->installed = 0;
3328 static void __exit exit_yaffs_fs(void)
3331 struct file_system_to_install *fsinst;
3333 yaffs_trace(YAFFS_TRACE_ALWAYS,
3334 "yaffs built " __DATE__ " " __TIME__ " removing.");
3336 remove_proc_entry("yaffs", YPROC_ROOT);
3338 fsinst = fs_to_install;
3340 while (fsinst->fst) {
3341 if (fsinst->installed) {
3342 unregister_filesystem(fsinst->fst);
3343 fsinst->installed = 0;
3349 module_init(init_yaffs_fs)
3350 module_exit(exit_yaffs_fs)
3352 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3353 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
3354 MODULE_LICENSE("GPL");