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 (LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0))
61 #define YAFFS_NEW_FOLLOW_LINK 1
63 #define YAFFS_NEW_FOLLOW_LINK 0
66 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
67 #define YAFFS_HAS_WRITE_SUPER
71 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
72 #include <linux/config.h>
75 #include <linux/kernel.h>
76 #include <linux/module.h>
77 #include <linux/slab.h>
78 #include <linux/init.h>
80 #include <linux/proc_fs.h>
81 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
82 #include <linux/smp_lock.h>
84 #include <linux/pagemap.h>
85 #include <linux/mtd/mtd.h>
86 #include <linux/interrupt.h>
87 #include <linux/string.h>
88 #include <linux/ctype.h>
90 #if (YAFFS_NEW_FOLLOW_LINK == 1)
91 #include <linux/namei.h>
94 #ifdef YAFFS_COMPILE_EXPORTFS
95 #include <linux/exportfs.h>
98 #ifdef YAFFS_COMPILE_BACKGROUND
99 #include <linux/kthread.h>
100 #include <linux/delay.h>
102 #ifdef YAFFS_COMPILE_FREEZER
103 #include <linux/freezer.h>
106 #include <asm/div64.h>
108 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
110 #include <linux/statfs.h>
112 #define UnlockPage(p) unlock_page(p)
113 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
115 /* FIXME: use sb->s_id instead ? */
116 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
120 #include <linux/locks.h>
121 #define BDEVNAME_SIZE 0
122 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
124 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
125 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
131 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
132 #define YPROC_ROOT (&proc_root)
134 #define YPROC_ROOT NULL
137 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
138 #define Y_INIT_TIMER(a) init_timer(a)
140 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
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(3, 6, 0))
150 #define YAFFS_SUPER_HAS_DIRTY
154 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
155 #define set_nlink(inode, count) do { (inode)->i_nlink = (count); } while(0)
158 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
159 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
161 uint64_t result = partition_size;
162 do_div(result, block_size);
163 return (uint32_t) result;
166 #define YCALCBLOCKS(s, b) ((s)/(b))
169 #include <linux/uaccess.h>
170 #include <linux/mtd/mtd.h>
172 #include "yportenv.h"
173 #include "yaffs_trace.h"
174 #include "yaffs_guts.h"
175 #include "yaffs_attribs.h"
177 #include "yaffs_linux.h"
179 #include "yaffs_mtdif.h"
180 #include "yaffs_packedtags2.h"
181 #include "yaffs_getblockinfo.h"
183 unsigned int yaffs_trace_mask =
184 YAFFS_TRACE_BAD_BLOCKS |
188 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
189 unsigned int yaffs_auto_checkpoint = 1;
190 unsigned int yaffs_gc_control = 1;
191 unsigned int yaffs_bg_enable = 1;
192 unsigned int yaffs_auto_select = 1;
193 /* Module Parameters */
194 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
195 module_param(yaffs_trace_mask, uint, 0644);
196 module_param(yaffs_wr_attempts, uint, 0644);
197 module_param(yaffs_auto_checkpoint, uint, 0644);
198 module_param(yaffs_gc_control, uint, 0644);
199 module_param(yaffs_bg_enable, uint, 0644);
201 MODULE_PARM(yaffs_trace_mask, "i");
202 MODULE_PARM(yaffs_wr_attempts, "i");
203 MODULE_PARM(yaffs_auto_checkpoint, "i");
204 MODULE_PARM(yaffs_gc_control, "i");
207 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
208 /* use iget and read_inode */
209 #define Y_IGET(sb, inum) iget((sb), (inum))
212 /* Call local equivalent */
213 #define YAFFS_USE_OWN_IGET
214 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
218 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
219 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
221 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
224 #define yaffs_inode_to_obj(iptr) \
225 ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
226 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
228 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
229 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
231 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
234 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
235 #define Y_CLEAR_INODE(i) clear_inode(i)
237 #define Y_CLEAR_INODE(i) end_writeback(i)
240 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
241 #define YAFFS_USE_DIR_ITERATE
244 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0))
245 #define YAFFS_USE_XATTR
248 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,12,0))
249 #define YAFFS_NEW_PROCFS
250 #include <linux/seq_file.h>
253 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0))
254 #define PAGE_CACHE_SIZE PAGE_SIZE
255 #define PAGE_CACHE_SHIFT PAGE_SHIFT
256 #define Y_GET_DENTRY(f) ((f)->f_path.dentry)
257 #define page_cache_release put_page
258 #define YAFFS_NEW_XATTR 1
259 #define YAFFS_NEW_GET_LINK 1
261 #define Y_GET_DENTRY(f) ((f)->f_dentry)
262 #define YAFFS_NEW_XATTR 0
263 #define YAFFS_NEW_GET_LINK 0
266 #define update_dir_time(dir) do {\
267 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
270 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0))
271 static inline int setattr_prepare(struct dentry *dentry, struct iattr *attr)
273 return inode_change_ok(dentry->d_inode, attr);
277 static void yaffs_fill_inode_from_obj(struct inode *inode,
278 struct yaffs_obj *obj);
281 static void yaffs_gross_lock(struct yaffs_dev *dev)
283 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
284 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
285 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
288 static void yaffs_gross_unlock(struct yaffs_dev *dev)
290 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
291 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
295 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
297 /* Lifted from jffs2 */
299 struct yaffs_obj *obj;
300 unsigned char *pg_buf;
302 loff_t pos = ((loff_t) pg->index) << PAGE_SHIFT;
303 struct yaffs_dev *dev;
305 yaffs_trace(YAFFS_TRACE_OS,
306 "yaffs_readpage_nolock at %lld, size %08x",
308 (unsigned)PAGE_SIZE);
310 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
314 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
315 BUG_ON(!PageLocked(pg));
322 /* FIXME: Can kmap fail? */
324 yaffs_gross_lock(dev);
326 ret = yaffs_file_rd(obj, pg_buf, pos, PAGE_CACHE_SIZE);
328 yaffs_gross_unlock(dev);
334 ClearPageUptodate(pg);
341 flush_dcache_page(pg);
344 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
348 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
350 int ret = yaffs_readpage_nolock(f, pg);
355 static int yaffs_readpage(struct file *f, struct page *pg)
359 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
360 ret = yaffs_readpage_unlock(f, pg);
361 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
366 static void yaffs_set_super_dirty_val(struct yaffs_dev *dev, int val)
368 struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
373 # ifdef YAFFS_SUPER_HAS_DIRTY
375 struct super_block *sb = lc->super;
384 static void yaffs_set_super_dirty(struct yaffs_dev *dev)
386 yaffs_set_super_dirty_val(dev, 1);
389 static void yaffs_clear_super_dirty(struct yaffs_dev *dev)
391 yaffs_set_super_dirty_val(dev, 0);
394 static int yaffs_check_super_dirty(struct yaffs_dev *dev)
396 struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
401 # ifdef YAFFS_SUPER_HAS_DIRTY
403 struct super_block *sb = lc->super;
405 if (sb && sb->s_dirt)
413 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
414 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
416 static int yaffs_writepage(struct page *page)
419 struct yaffs_dev *dev;
420 struct address_space *mapping = page->mapping;
422 unsigned long end_index;
424 struct yaffs_obj *obj;
431 inode = mapping->host;
434 i_size = i_size_read(inode);
436 end_index = i_size >> PAGE_CACHE_SHIFT;
438 if (page->index < end_index)
439 n_bytes = PAGE_CACHE_SIZE;
441 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
443 if (page->index > end_index || !n_bytes) {
444 yaffs_trace(YAFFS_TRACE_OS,
445 "yaffs_writepage at %lld, inode size = %lld!!",
446 ((loff_t)page->index) << PAGE_CACHE_SHIFT,
448 yaffs_trace(YAFFS_TRACE_OS,
451 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
452 set_page_writeback(page);
454 end_page_writeback(page);
459 if (n_bytes != PAGE_CACHE_SIZE)
460 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
466 obj = yaffs_inode_to_obj(inode);
468 yaffs_gross_lock(dev);
470 yaffs_trace(YAFFS_TRACE_OS,
471 "yaffs_writepage at %lld, size %08x",
472 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes);
473 yaffs_trace(YAFFS_TRACE_OS,
474 "writepag0: obj = %lld, ino = %lld",
475 obj->variant.file_variant.file_size, inode->i_size);
477 n_written = yaffs_wr_file(obj, buffer,
478 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes, 0);
480 yaffs_set_super_dirty(dev);
482 yaffs_trace(YAFFS_TRACE_OS,
483 "writepag1: obj = %lld, ino = %lld",
484 obj->variant.file_variant.file_size, inode->i_size);
486 yaffs_gross_unlock(dev);
489 set_page_writeback(page);
491 end_page_writeback(page);
494 return (n_written == n_bytes) ? 0 : -ENOSPC;
497 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
498 /* For now we just assume few parallel writes and check against a small number. */
499 /* Todo: need to do this with a counter to handle parallel reads better */
501 static ssize_t yaffs_hold_space(struct file *f)
503 struct yaffs_obj *obj;
504 struct yaffs_dev *dev;
508 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
512 yaffs_gross_lock(dev);
514 n_free_chunks = yaffs_get_n_free_chunks(dev);
516 yaffs_gross_unlock(dev);
518 return (n_free_chunks > 20) ? 1 : 0;
521 static void yaffs_release_space(struct file *f)
523 struct yaffs_obj *obj;
524 struct yaffs_dev *dev;
526 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
530 yaffs_gross_lock(dev);
532 yaffs_gross_unlock(dev);
535 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
536 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
537 loff_t pos, unsigned len, unsigned flags,
538 struct page **pagep, void **fsdata)
540 struct page *pg = NULL;
541 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
547 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
548 pg = grab_cache_page_write_begin(mapping, index, flags);
550 pg = __grab_cache_page(mapping, index);
558 yaffs_trace(YAFFS_TRACE_OS,
559 "start yaffs_write_begin index %d(%x) uptodate %d",
560 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
563 space_held = yaffs_hold_space(filp);
570 /* Update page if required */
572 if (!Page_Uptodate(pg))
573 ret = yaffs_readpage_nolock(filp, pg);
578 /* Happy path return */
579 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
584 yaffs_trace(YAFFS_TRACE_OS,
585 "end yaffs_write_begin fail returning %d", ret);
587 yaffs_release_space(filp);
590 page_cache_release(pg);
597 static int yaffs_prepare_write(struct file *f, struct page *pg,
598 unsigned offset, unsigned to)
600 yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
602 if (!Page_Uptodate(pg))
603 return yaffs_readpage_nolock(f, pg);
609 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
612 struct yaffs_obj *obj;
616 struct yaffs_dev *dev;
618 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
621 yaffs_trace(YAFFS_TRACE_OS,
622 "yaffs_file_write: hey obj is null!");
628 yaffs_gross_lock(dev);
630 inode = Y_GET_DENTRY(f)->d_inode;
632 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
633 ipos = inode->i_size;
637 yaffs_trace(YAFFS_TRACE_OS,
638 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %lld",
639 (unsigned)n, (unsigned)n, obj->obj_id, ipos);
641 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
643 yaffs_set_super_dirty(dev);
645 yaffs_trace(YAFFS_TRACE_OS,
646 "yaffs_file_write: %d(%x) bytes written",
647 (unsigned)n, (unsigned)n);
652 if (ipos > inode->i_size) {
653 inode->i_size = ipos;
654 inode->i_blocks = (ipos + 511) >> 9;
656 yaffs_trace(YAFFS_TRACE_OS,
657 "yaffs_file_write size updated to %lld bytes, %d blocks",
658 ipos, (int)(inode->i_blocks));
662 yaffs_gross_unlock(dev);
663 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
667 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
668 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
669 loff_t pos, unsigned len, unsigned copied,
670 struct page *pg, void *fsdadata)
674 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
677 addr = kva + offset_into_page;
679 yaffs_trace(YAFFS_TRACE_OS,
680 "yaffs_write_end addr %p pos %lld n_bytes %d",
683 ret = yaffs_file_write(filp, addr, copied, &pos);
686 yaffs_trace(YAFFS_TRACE_OS,
687 "yaffs_write_end not same size ret %d copied %d",
694 yaffs_release_space(filp);
696 page_cache_release(pg);
701 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
706 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
707 int n_bytes = to - offset;
713 yaffs_trace(YAFFS_TRACE_OS,
714 "yaffs_commit_write addr %p pos %lld n_bytes %d",
717 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
719 if (n_written != n_bytes) {
720 yaffs_trace(YAFFS_TRACE_OS,
721 "yaffs_commit_write not same size n_written %d n_bytes %d",
727 yaffs_trace(YAFFS_TRACE_OS,
728 "yaffs_commit_write returning %d",
729 n_written == n_bytes ? 0 : n_written);
731 return n_written == n_bytes ? 0 : n_written;
735 static struct address_space_operations yaffs_file_address_operations = {
736 .readpage = yaffs_readpage,
737 .writepage = yaffs_writepage,
738 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
739 .write_begin = yaffs_write_begin,
740 .write_end = yaffs_write_end,
742 .prepare_write = yaffs_prepare_write,
743 .commit_write = yaffs_commit_write,
748 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
749 static int yaffs_file_flush(struct file *file, fl_owner_t id)
751 static int yaffs_file_flush(struct file *file)
754 struct yaffs_obj *obj = yaffs_dentry_to_obj(Y_GET_DENTRY(file));
756 struct yaffs_dev *dev = obj->my_dev;
758 yaffs_trace(YAFFS_TRACE_OS,
759 "yaffs_file_flush object %d (%s)",
761 obj->dirty ? "dirty" : "clean");
763 yaffs_gross_lock(dev);
765 yaffs_flush_file(obj, 1, 0, 0);
767 yaffs_gross_unlock(dev);
773 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
774 static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync)
775 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
776 static int yaffs_sync_object(struct file *file, int datasync)
778 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
782 struct yaffs_obj *obj;
783 struct yaffs_dev *dev;
784 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
785 struct dentry *dentry = file->f_path.dentry;
788 obj = yaffs_dentry_to_obj(dentry);
792 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
793 "yaffs_sync_object");
794 yaffs_gross_lock(dev);
795 yaffs_flush_file(obj, 1, datasync, 0);
796 yaffs_gross_unlock(dev);
801 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
802 static const struct file_operations yaffs_file_operations = {
803 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
804 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0)
805 .read = new_sync_read,
806 .write = new_sync_write,
808 .read_iter = generic_file_read_iter,
809 .write_iter = generic_file_write_iter,
811 .read = do_sync_read,
812 .write = do_sync_write,
813 .aio_read = generic_file_aio_read,
814 .aio_write = generic_file_aio_write,
816 .mmap = generic_file_mmap,
817 .flush = yaffs_file_flush,
818 .fsync = yaffs_sync_object,
819 .splice_read = generic_file_splice_read,
820 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
821 .splice_write = iter_file_splice_write,
823 .splice_write = generic_file_splice_write,
825 .llseek = generic_file_llseek,
828 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
830 static const struct file_operations yaffs_file_operations = {
831 .read = do_sync_read,
832 .write = do_sync_write,
833 .aio_read = generic_file_aio_read,
834 .aio_write = generic_file_aio_write,
835 .mmap = generic_file_mmap,
836 .flush = yaffs_file_flush,
837 .fsync = yaffs_sync_object,
838 .sendfile = generic_file_sendfile,
843 static const struct file_operations yaffs_file_operations = {
844 .read = generic_file_read,
845 .write = generic_file_write,
846 .mmap = generic_file_mmap,
847 .flush = yaffs_file_flush,
848 .fsync = yaffs_sync_object,
849 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
850 .sendfile = generic_file_sendfile,
858 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
859 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
861 void *kaddr = kmap_atomic(page, KM_USER0);
862 memset(kaddr + start, 0, end - start);
863 kunmap_atomic(kaddr, KM_USER0);
864 flush_dcache_page(page);
869 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
871 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
872 truncate_setsize(inode, newsize);
875 truncate_inode_pages(&inode->i_data, newsize);
882 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
884 #ifdef YAFFS_USE_SETATTR_COPY
885 setattr_copy(inode, attr);
888 return inode_setattr(inode, attr);
893 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
895 struct inode *inode = dentry->d_inode;
897 struct yaffs_dev *dev;
899 yaffs_trace(YAFFS_TRACE_OS,
900 "yaffs_setattr of object %d",
901 yaffs_inode_to_obj(inode)->obj_id);
903 /* Fail if a requested resize >= 2GB */
904 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
909 error = setattr_prepare(dentry, attr);
913 error = yaffs_vfs_setattr(inode, attr);
914 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
915 if (attr->ia_valid & ATTR_SIZE) {
916 yaffs_vfs_setsize(inode, attr->ia_size);
917 inode->i_blocks = (inode->i_size + 511) >> 9;
920 dev = yaffs_inode_to_obj(inode)->my_dev;
921 if (attr->ia_valid & ATTR_SIZE) {
922 yaffs_trace(YAFFS_TRACE_OS,
924 (int)(attr->ia_size),
925 (int)(attr->ia_size));
927 yaffs_gross_lock(dev);
928 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
929 if (result == YAFFS_OK) {
934 yaffs_gross_unlock(dev);
938 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
943 #ifdef YAFFS_USE_XATTR
944 #if (YAFFS_NEW_XATTR > 0)
945 static int yaffs_setxattr(struct dentry *dentry, struct inode *inode,
946 const char *name, const void *value, size_t size, int flags)
949 static int yaffs_setxattr(struct dentry *dentry, const char *name,
950 const void *value, size_t size, int flags)
952 struct inode *inode = dentry->d_inode;
955 struct yaffs_dev *dev;
956 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
958 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
963 yaffs_gross_lock(dev);
964 result = yaffs_set_xattrib(obj, name, value, size, flags);
965 if (result == YAFFS_OK)
969 yaffs_gross_unlock(dev);
972 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
977 #ifdef YAFFS_NEW_XATTR
978 static ssize_t yaffs_getxattr(struct dentry * dentry, struct inode *inode,
979 const char *name, void *buff, size_t size)
982 static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
983 void *buff, size_t size)
985 struct inode *inode = dentry->d_inode;
988 struct yaffs_dev *dev;
989 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
991 yaffs_trace(YAFFS_TRACE_OS,
992 "yaffs_getxattr \"%s\" from object %d",
997 yaffs_gross_lock(dev);
998 error = yaffs_get_xattrib(obj, name, buff, size);
999 yaffs_gross_unlock(dev);
1002 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
1007 static int yaffs_removexattr(struct dentry *dentry, const char *name)
1009 struct inode *inode = dentry->d_inode;
1011 struct yaffs_dev *dev;
1012 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1014 yaffs_trace(YAFFS_TRACE_OS,
1015 "yaffs_removexattr of object %d", obj->obj_id);
1020 yaffs_gross_lock(dev);
1021 result = yaffs_remove_xattrib(obj, name);
1022 if (result == YAFFS_OK)
1024 else if (result < 0)
1026 yaffs_gross_unlock(dev);
1029 yaffs_trace(YAFFS_TRACE_OS,
1030 "yaffs_removexattr done returning %d", error);
1036 static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
1038 struct inode *inode = dentry->d_inode;
1040 struct yaffs_dev *dev;
1041 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1043 yaffs_trace(YAFFS_TRACE_OS,
1044 "yaffs_listxattr of object %d", obj->obj_id);
1048 yaffs_gross_lock(dev);
1049 error = yaffs_list_xattrib(obj, buff, size);
1050 yaffs_gross_unlock(dev);
1053 yaffs_trace(YAFFS_TRACE_OS,
1054 "yaffs_listxattr done returning %d", error);
1060 static const struct inode_operations yaffs_file_inode_operations = {
1061 .setattr = yaffs_setattr,
1062 #ifdef YAFFS_USE_XATTR
1063 .setxattr = yaffs_setxattr,
1064 .getxattr = yaffs_getxattr,
1065 .removexattr = yaffs_removexattr,
1067 .listxattr = yaffs_listxattr,
1071 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1074 unsigned char *alias;
1077 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1079 yaffs_gross_lock(dev);
1081 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1083 yaffs_gross_unlock(dev);
1088 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0)
1089 ret = vfs_readlink(dentry, buffer, buflen, alias);
1091 ret = readlink_copy(buffer, buflen, alias);
1097 #if (YAFFS_NEW_GET_LINK == 0)
1098 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1099 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1103 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1107 unsigned char *alias;
1109 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1111 yaffs_gross_lock(dev);
1113 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1114 yaffs_gross_unlock(dev);
1120 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1121 nd_set_link(nd, alias);
1125 ret = ERR_PTR(ret_int);
1128 ret = vfs_follow_link(nd, alias);
1137 static const char *yaffs_get_link(struct dentry *dentry, struct inode *inode, struct delayed_call *done)
1139 unsigned char *alias;
1140 struct yaffs_dev *dev;
1143 return ERR_PTR(-ECHILD);
1145 dev = yaffs_dentry_to_obj(dentry)->my_dev;
1147 yaffs_gross_lock(dev);
1149 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1150 yaffs_gross_unlock(dev);
1153 return ERR_PTR(-ENOMEM);
1154 set_delayed_call(done, kfree_link, alias);
1159 #ifdef YAFFS_HAS_PUT_INODE
1161 /* For now put inode is just for debugging
1162 * Put inode is called when the inode **structure** is put.
1164 static void yaffs_put_inode(struct inode *inode)
1166 yaffs_trace(YAFFS_TRACE_OS,
1167 "yaffs_put_inode: ino %d, count %d"),
1168 (int)inode->i_ino, atomic_read(&inode->i_count);
1173 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1174 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
1180 static const struct inode_operations yaffs_symlink_inode_operations = {
1181 .readlink = yaffs_readlink,
1182 #if (YAFFS_NEW_GET_LINK == 1)
1183 .get_link = yaffs_get_link,
1185 .follow_link = yaffs_follow_link,
1187 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1188 .put_link = yaffs_put_link,
1190 .setattr = yaffs_setattr,
1191 #ifdef YAFFS_USE_XATTR
1192 .setxattr = yaffs_setxattr,
1193 .getxattr = yaffs_getxattr,
1194 .removexattr = yaffs_removexattr,
1196 .listxattr = yaffs_listxattr,
1199 #ifdef YAFFS_USE_OWN_IGET
1201 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1203 struct inode *inode;
1204 struct yaffs_obj *obj;
1205 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1207 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
1209 inode = iget_locked(sb, ino);
1211 return ERR_PTR(-ENOMEM);
1212 if (!(inode->i_state & I_NEW))
1215 /* NB This is called as a side effect of other functions, but
1216 * we had to release the lock to prevent deadlocks, so
1217 * need to lock again.
1220 yaffs_gross_lock(dev);
1222 obj = yaffs_find_by_number(dev, inode->i_ino);
1224 yaffs_fill_inode_from_obj(inode, obj);
1226 yaffs_gross_unlock(dev);
1228 unlock_new_inode(inode);
1234 static void yaffs_read_inode(struct inode *inode)
1236 /* NB This is called as a side effect of other functions, but
1237 * we had to release the lock to prevent deadlocks, so
1238 * need to lock again.
1241 struct yaffs_obj *obj;
1242 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
1244 yaffs_trace(YAFFS_TRACE_OS,
1245 "yaffs_read_inode for %d", (int)inode->i_ino);
1247 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1248 yaffs_gross_lock(dev);
1250 obj = yaffs_find_by_number(dev, inode->i_ino);
1252 yaffs_fill_inode_from_obj(inode, obj);
1254 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1255 yaffs_gross_unlock(dev);
1262 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1263 struct yaffs_obj *obj)
1265 struct inode *inode;
1268 yaffs_trace(YAFFS_TRACE_OS,
1269 "yaffs_get_inode for NULL super_block!!");
1275 yaffs_trace(YAFFS_TRACE_OS,
1276 "yaffs_get_inode for NULL object!!");
1281 yaffs_trace(YAFFS_TRACE_OS,
1282 "yaffs_get_inode for object %d", obj->obj_id);
1284 inode = Y_IGET(sb, obj->obj_id);
1288 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1289 /* iget also increments the inode's i_count */
1290 /* NB You can't be holding gross_lock or deadlock will happen! */
1297 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1300 #define YCRED(x) (x->cred)
1303 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0)
1304 #define YPROC_uid(p) (YCRED(p)->fsuid)
1305 #define YPROC_gid(p) (YCRED(p)->fsgid)
1306 #define EXTRACT_gid(x) x
1307 #define EXTRACT_uid(x) x
1308 #define MAKE_gid(x) x
1309 #define MAKE_uid(x) x
1311 #define YPROC_uid(p) from_kuid(&init_user_ns, YCRED(p)->fsuid)
1312 #define YPROC_gid(p) from_kgid(&init_user_ns, YCRED(p)->fsgid)
1313 #define EXTRACT_gid(x) from_kgid(&init_user_ns, x)
1314 #define EXTRACT_uid(x) from_kuid(&init_user_ns, x)
1315 #define MAKE_gid(x) make_kgid(&init_user_ns, x)
1316 #define MAKE_uid(x) make_kuid(&init_user_ns, x)
1320 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1321 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1323 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1324 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1327 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1331 struct inode *inode;
1333 struct yaffs_obj *obj = NULL;
1334 struct yaffs_dev *dev;
1336 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1338 int error = -ENOSPC;
1339 uid_t uid = YPROC_uid(current);
1341 (dir->i_mode & S_ISGID) ? EXTRACT_gid(dir->i_gid) : YPROC_gid(current);
1343 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1347 yaffs_trace(YAFFS_TRACE_OS,
1348 "yaffs_mknod: parent object %d type %d",
1349 parent->obj_id, parent->variant_type);
1351 yaffs_trace(YAFFS_TRACE_OS,
1352 "yaffs_mknod: could not get parent object");
1356 yaffs_trace(YAFFS_TRACE_OS,
1357 "yaffs_mknod: making oject for %s, mode %x dev %x",
1358 dentry->d_name.name, mode, rdev);
1360 dev = parent->my_dev;
1362 yaffs_gross_lock(dev);
1364 switch (mode & S_IFMT) {
1366 /* Special (socket, fifo, device...) */
1367 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
1368 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1370 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1371 gid, old_encode_dev(rdev));
1374 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1378 case S_IFREG: /* file */
1379 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
1380 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1383 case S_IFDIR: /* directory */
1384 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
1385 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1388 case S_IFLNK: /* symlink */
1389 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
1390 obj = NULL; /* Do we ever get here? */
1394 /* Can not call yaffs_get_inode() with gross lock held */
1395 yaffs_gross_unlock(dev);
1398 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1399 d_instantiate(dentry, inode);
1400 update_dir_time(dir);
1401 yaffs_trace(YAFFS_TRACE_OS,
1402 "yaffs_mknod created object %d count = %d",
1403 obj->obj_id, atomic_read(&inode->i_count));
1405 yaffs_fill_inode_from_obj(dir, parent);
1407 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
1414 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1415 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1417 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1421 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
1422 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1427 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
1428 static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1430 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1431 static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1432 struct nameidata *n)
1433 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1434 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1435 struct nameidata *n)
1437 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1440 yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
1441 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1444 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
1445 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1447 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1448 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1449 struct nameidata *n)
1451 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1454 struct yaffs_obj *obj;
1455 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1457 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
1459 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1460 yaffs_gross_lock(dev);
1462 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
1463 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
1465 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
1467 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
1469 /* Can't hold gross lock when calling yaffs_get_inode() */
1470 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1471 yaffs_gross_unlock(dev);
1474 yaffs_trace(YAFFS_TRACE_OS,
1475 "yaffs_lookup found %d", obj->obj_id);
1477 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1479 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
1483 /* added NCB for 2.5/6 compatability - forces add even if inode is
1484 * NULL which creates dentry hash */
1485 d_add(dentry, inode);
1493 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1494 struct dentry *dentry)
1496 struct inode *inode = old_dentry->d_inode;
1497 struct yaffs_obj *obj = NULL;
1498 struct yaffs_obj *link = NULL;
1499 struct yaffs_dev *dev;
1501 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
1503 obj = yaffs_inode_to_obj(inode);
1506 yaffs_gross_lock(dev);
1508 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1510 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1514 set_nlink(old_dentry->d_inode, yaffs_get_obj_link_count(obj));
1515 d_instantiate(dentry, old_dentry->d_inode);
1516 atomic_inc(&old_dentry->d_inode->i_count);
1517 yaffs_trace(YAFFS_TRACE_OS,
1518 "yaffs_link link count %d i_count %d",
1519 old_dentry->d_inode->i_nlink,
1520 atomic_read(&old_dentry->d_inode->i_count));
1523 yaffs_gross_unlock(dev);
1526 update_dir_time(dir);
1533 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1534 const char *symname)
1536 struct yaffs_obj *obj;
1537 struct yaffs_dev *dev;
1538 uid_t uid = YPROC_uid(current);
1540 (dir->i_mode & S_ISGID) ? EXTRACT_gid(dir->i_gid) : YPROC_gid(current);
1542 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
1544 if (strnlen(dentry->d_name.name, YAFFS_MAX_NAME_LENGTH + 1) >
1545 YAFFS_MAX_NAME_LENGTH)
1546 return -ENAMETOOLONG;
1548 if (strnlen(symname, YAFFS_MAX_ALIAS_LENGTH + 1) >
1549 YAFFS_MAX_ALIAS_LENGTH)
1550 return -ENAMETOOLONG;
1552 dev = yaffs_inode_to_obj(dir)->my_dev;
1553 yaffs_gross_lock(dev);
1554 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1555 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1556 yaffs_gross_unlock(dev);
1559 struct inode *inode;
1561 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1562 d_instantiate(dentry, inode);
1563 update_dir_time(dir);
1564 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
1567 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
1574 * The VFS layer already does all the dentry stuff for rename.
1576 * NB: POSIX says you can rename an object over an old object of the same name
1578 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0))
1579 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1580 struct inode *new_dir, struct dentry *new_dentry, unsigned int unused)
1582 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1583 struct inode *new_dir, struct dentry *new_dentry)
1586 struct yaffs_dev *dev;
1587 int ret_val = YAFFS_FAIL;
1588 struct yaffs_obj *target;
1590 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
1591 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1593 yaffs_gross_lock(dev);
1595 /* Check if the target is an existing directory that is not empty. */
1596 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1597 new_dentry->d_name.name);
1599 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1600 !list_empty(&target->variant.dir_variant.children)) {
1602 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
1604 ret_val = YAFFS_FAIL;
1606 /* Now does unlinking internally using shadowing mechanism */
1607 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
1609 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1610 old_dentry->d_name.name,
1611 yaffs_inode_to_obj(new_dir),
1612 new_dentry->d_name.name);
1614 yaffs_gross_unlock(dev);
1616 if (ret_val == YAFFS_OK) {
1618 inode_dec_link_count(new_dentry->d_inode);
1620 update_dir_time(old_dir);
1621 if (old_dir != new_dir)
1622 update_dir_time(new_dir);
1632 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1636 struct yaffs_dev *dev;
1637 struct yaffs_obj *obj;
1639 yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
1640 (int)(dir->i_ino), dentry->d_name.name);
1641 obj = yaffs_inode_to_obj(dir);
1644 yaffs_gross_lock(dev);
1646 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1648 if (ret_val == YAFFS_OK) {
1649 inode_dec_link_count(dentry->d_inode);
1651 yaffs_gross_unlock(dev);
1652 update_dir_time(dir);
1655 yaffs_gross_unlock(dev);
1661 static const struct inode_operations yaffs_dir_inode_operations = {
1662 .create = yaffs_create,
1663 .lookup = yaffs_lookup,
1665 .unlink = yaffs_unlink,
1666 .symlink = yaffs_symlink,
1667 .mkdir = yaffs_mkdir,
1668 .rmdir = yaffs_unlink,
1669 .mknod = yaffs_mknod,
1670 .rename = yaffs_rename,
1671 .setattr = yaffs_setattr,
1672 .listxattr = yaffs_listxattr,
1673 #ifdef YAFFS_USE_XATTR
1674 .setxattr = yaffs_setxattr,
1675 .getxattr = yaffs_getxattr,
1676 .removexattr = yaffs_removexattr,
1680 /*-----------------------------------------------------------------*/
1681 /* Directory search context allows us to unlock access to yaffs during
1682 * filldir without causing problems with the directory being modified.
1683 * This is similar to the tried and tested mechanism used in yaffs direct.
1685 * A search context iterates along a doubly linked list of siblings in the
1686 * directory. If the iterating object is deleted then this would corrupt
1687 * the list iteration, likely causing a crash. The search context avoids
1688 * this by using the remove_obj_fn to move the search context to the
1689 * next object before the object is deleted.
1691 * Many readdirs (and thus seach conexts) may be alive simulateously so
1692 * each struct yaffs_dev has a list of these.
1694 * A seach context lives for the duration of a readdir.
1696 * All these functions must be called while yaffs is locked.
1699 struct yaffs_search_context {
1700 struct yaffs_dev *dev;
1701 struct yaffs_obj *dir_obj;
1702 struct yaffs_obj *next_return;
1703 struct list_head others;
1707 * yaffs_new_search() creates a new search context, initialises it and
1708 * adds it to the device's search context list.
1710 * Called at start of readdir.
1712 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
1714 struct yaffs_dev *dev = dir->my_dev;
1715 struct yaffs_search_context *sc =
1716 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
1720 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
1721 sc->next_return = NULL;
1724 list_entry(dir->variant.dir_variant.children.next,
1725 struct yaffs_obj, siblings);
1726 INIT_LIST_HEAD(&sc->others);
1727 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
1733 * yaffs_search_end() disposes of a search context and cleans up.
1735 static void yaffs_search_end(struct yaffs_search_context *sc)
1738 list_del(&sc->others);
1744 * yaffs_search_advance() moves a search context to the next object.
1745 * Called when the search iterates or when an object removal causes
1746 * the search context to be moved to the next object.
1748 static void yaffs_search_advance(struct yaffs_search_context *sc)
1753 if (sc->next_return == NULL ||
1754 list_empty(&sc->dir_obj->variant.dir_variant.children))
1755 sc->next_return = NULL;
1757 struct list_head *next = sc->next_return->siblings.next;
1759 if (next == &sc->dir_obj->variant.dir_variant.children)
1760 sc->next_return = NULL; /* end of list */
1763 list_entry(next, struct yaffs_obj, siblings);
1768 * yaffs_remove_obj_callback() is called when an object is unlinked.
1769 * We check open search contexts and advance any which are currently
1770 * on the object being iterated.
1772 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
1775 struct list_head *i;
1776 struct yaffs_search_context *sc;
1777 struct list_head *search_contexts =
1778 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
1780 /* Iterate through the directory search contexts.
1781 * If any are currently on the object being removed, then advance
1782 * the search context to the next object to prevent a hanging pointer.
1784 list_for_each(i, search_contexts) {
1785 sc = list_entry(i, struct yaffs_search_context, others);
1786 if (sc->next_return == obj)
1787 yaffs_search_advance(sc);
1793 /*-----------------------------------------------------------------*/
1795 #ifdef YAFFS_USE_DIR_ITERATE
1796 static int yaffs_iterate(struct file *f, struct dir_context *dc)
1798 struct yaffs_obj *obj;
1799 struct yaffs_dev *dev;
1800 struct yaffs_search_context *sc;
1801 unsigned long curoffs;
1802 struct yaffs_obj *l;
1805 char name[YAFFS_MAX_NAME_LENGTH + 1];
1807 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
1810 yaffs_gross_lock(dev);
1812 yaffs_dev_to_lc(dev)->readdir_process = current;
1814 sc = yaffs_new_search(obj);
1820 if (!dir_emit_dots(f, dc))
1825 while (sc->next_return) {
1827 l = sc->next_return;
1828 if (curoffs >= dc->pos) {
1829 int this_inode = yaffs_get_obj_inode(l);
1830 int this_type = yaffs_get_obj_type(l);
1832 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1833 yaffs_trace(YAFFS_TRACE_OS,
1834 "yaffs_readdir: %s inode %d",
1835 name, yaffs_get_obj_inode(l));
1837 yaffs_gross_unlock(dev);
1844 yaffs_gross_lock(dev);
1848 yaffs_gross_lock(dev);
1853 yaffs_search_advance(sc);
1857 yaffs_search_end(sc);
1858 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1859 yaffs_gross_unlock(dev);
1866 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1868 struct yaffs_obj *obj;
1869 struct yaffs_dev *dev;
1870 struct yaffs_search_context *sc;
1871 struct inode *inode = Y_GET_DENTRY(f)->d_inode;
1872 unsigned long offset, curoffs;
1873 struct yaffs_obj *l;
1876 char name[YAFFS_MAX_NAME_LENGTH + 1];
1878 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
1881 yaffs_gross_lock(dev);
1883 yaffs_dev_to_lc(dev)->readdir_process = current;
1887 sc = yaffs_new_search(obj);
1893 yaffs_trace(YAFFS_TRACE_OS,
1894 "yaffs_readdir: starting at %d", (int)offset);
1897 yaffs_trace(YAFFS_TRACE_OS,
1898 "yaffs_readdir: entry . ino %d",
1900 yaffs_gross_unlock(dev);
1901 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1902 yaffs_gross_lock(dev);
1905 yaffs_gross_lock(dev);
1910 yaffs_trace(YAFFS_TRACE_OS,
1911 "yaffs_readdir: entry .. ino %d",
1912 (int)f->f_dentry->d_parent->d_inode->i_ino);
1913 yaffs_gross_unlock(dev);
1914 if (filldir(dirent, "..", 2, offset,
1915 f->f_dentry->d_parent->d_inode->i_ino,
1917 yaffs_gross_lock(dev);
1920 yaffs_gross_lock(dev);
1927 /* If the directory has changed since the open or last call to
1928 readdir, rewind to after the 2 canned entries. */
1929 if (f->f_version != inode->i_version) {
1932 f->f_version = inode->i_version;
1935 while (sc->next_return) {
1937 l = sc->next_return;
1938 if (curoffs >= offset) {
1939 int this_inode = yaffs_get_obj_inode(l);
1940 int this_type = yaffs_get_obj_type(l);
1942 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1943 yaffs_trace(YAFFS_TRACE_OS,
1944 "yaffs_readdir: %s inode %d",
1945 name, yaffs_get_obj_inode(l));
1947 yaffs_gross_unlock(dev);
1952 offset, this_inode, this_type) < 0) {
1953 yaffs_gross_lock(dev);
1957 yaffs_gross_lock(dev);
1962 yaffs_search_advance(sc);
1966 yaffs_search_end(sc);
1967 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1968 yaffs_gross_unlock(dev);
1975 static const struct file_operations yaffs_dir_operations = {
1976 .read = generic_read_dir,
1977 #ifdef YAFFS_USE_DIR_ITERATE
1978 .iterate = yaffs_iterate,
1980 .readdir = yaffs_readdir,
1982 .fsync = yaffs_sync_object,
1983 .llseek = generic_file_llseek,
1986 static void yaffs_fill_inode_from_obj(struct inode *inode,
1987 struct yaffs_obj *obj)
1991 /* Check mode against the variant type and attempt to repair if broken. */
1992 u32 mode = obj->yst_mode;
1993 switch (obj->variant_type) {
1994 case YAFFS_OBJECT_TYPE_FILE:
1995 if (!S_ISREG(mode)) {
1996 obj->yst_mode &= ~S_IFMT;
1997 obj->yst_mode |= S_IFREG;
2001 case YAFFS_OBJECT_TYPE_SYMLINK:
2002 if (!S_ISLNK(mode)) {
2003 obj->yst_mode &= ~S_IFMT;
2004 obj->yst_mode |= S_IFLNK;
2008 case YAFFS_OBJECT_TYPE_DIRECTORY:
2009 if (!S_ISDIR(mode)) {
2010 obj->yst_mode &= ~S_IFMT;
2011 obj->yst_mode |= S_IFDIR;
2015 case YAFFS_OBJECT_TYPE_UNKNOWN:
2016 case YAFFS_OBJECT_TYPE_HARDLINK:
2017 case YAFFS_OBJECT_TYPE_SPECIAL:
2023 inode->i_flags |= S_NOATIME;
2025 inode->i_ino = obj->obj_id;
2026 inode->i_mode = obj->yst_mode;
2027 inode->i_uid = MAKE_uid(obj->yst_uid);
2028 inode->i_gid = MAKE_gid(obj->yst_gid);
2029 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
2030 inode->i_blksize = inode->i_sb->s_blocksize;
2032 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2034 inode->i_rdev = old_decode_dev(obj->yst_rdev);
2035 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2036 inode->i_atime.tv_nsec = 0;
2037 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2038 inode->i_mtime.tv_nsec = 0;
2039 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2040 inode->i_ctime.tv_nsec = 0;
2042 inode->i_rdev = obj->yst_rdev;
2043 inode->i_atime = obj->yst_atime;
2044 inode->i_mtime = obj->yst_mtime;
2045 inode->i_ctime = obj->yst_ctime;
2047 inode->i_size = yaffs_get_obj_length(obj);
2048 inode->i_blocks = (inode->i_size + 511) >> 9;
2050 set_nlink(inode, yaffs_get_obj_link_count(obj));
2052 yaffs_trace(YAFFS_TRACE_OS,
2053 "yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
2054 inode->i_mode, obj->yst_uid, obj->yst_gid,
2055 inode->i_size, atomic_read(&inode->i_count));
2057 switch (obj->yst_mode & S_IFMT) {
2058 default: /* fifo, device or socket */
2059 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2060 init_special_inode(inode, obj->yst_mode,
2061 old_decode_dev(obj->yst_rdev));
2063 init_special_inode(inode, obj->yst_mode,
2064 (dev_t) (obj->yst_rdev));
2067 case S_IFREG: /* file */
2068 inode->i_op = &yaffs_file_inode_operations;
2069 inode->i_fop = &yaffs_file_operations;
2070 inode->i_mapping->a_ops =
2071 &yaffs_file_address_operations;
2073 case S_IFDIR: /* directory */
2074 inode->i_op = &yaffs_dir_inode_operations;
2075 inode->i_fop = &yaffs_dir_operations;
2077 case S_IFLNK: /* symlink */
2078 inode->i_op = &yaffs_symlink_inode_operations;
2082 yaffs_inode_to_obj_lv(inode) = obj;
2084 obj->my_inode = inode;
2087 yaffs_trace(YAFFS_TRACE_OS,
2088 "yaffs_fill_inode invalid parameters");
2096 * yaffs background thread functions .
2097 * yaffs_bg_thread_fn() the thread function
2098 * yaffs_bg_start() launches the background thread.
2099 * yaffs_bg_stop() cleans up the background thread.
2102 * The thread should only run after the yaffs is initialised
2103 * The thread should be stopped before yaffs is unmounted.
2104 * The thread should not do any writing while the fs is in read only.
2107 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2109 unsigned erased_chunks =
2110 dev->n_erased_blocks * dev->param.chunks_per_block;
2111 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2112 unsigned scattered = 0; /* Free chunks not in an erased block */
2114 if (erased_chunks < dev->n_free_chunks)
2115 scattered = (dev->n_free_chunks - erased_chunks);
2117 if (!context->bg_running)
2119 else if (scattered < (dev->param.chunks_per_block * 2))
2121 else if (erased_chunks > dev->n_free_chunks / 2)
2123 else if (erased_chunks > dev->n_free_chunks / 4)
2129 #ifdef YAFFS_COMPILE_BACKGROUND
2131 void yaffs_background_waker(unsigned long data)
2133 wake_up_process((struct task_struct *)data);
2136 static int yaffs_bg_thread_fn(void *data)
2138 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2139 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2140 unsigned long now = jiffies;
2141 unsigned long next_dir_update = now;
2142 unsigned long next_gc = now;
2143 unsigned long expires;
2144 unsigned int urgency;
2147 struct timer_list timer;
2149 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2150 "yaffs_background starting for dev %p", (void *)dev);
2152 #ifdef YAFFS_COMPILE_FREEZER
2155 while (context->bg_running) {
2156 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
2158 if (kthread_should_stop())
2161 #ifdef YAFFS_COMPILE_FREEZER
2162 if (try_to_freeze())
2165 yaffs_gross_lock(dev);
2169 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
2170 yaffs_update_dirty_dirs(dev);
2171 next_dir_update = now + HZ;
2174 if (time_after(now, next_gc) && yaffs_bg_enable) {
2175 if (!dev->is_checkpointed) {
2176 urgency = yaffs_bg_gc_urgency(dev);
2177 gc_result = yaffs_bg_gc(dev, urgency);
2179 next_gc = now + HZ / 20 + 1;
2180 else if (urgency > 0)
2181 next_gc = now + HZ / 10 + 1;
2183 next_gc = now + HZ * 2;
2186 * gc not running so set to next_dir_update
2187 * to cut down on wake ups
2189 next_gc = next_dir_update;
2192 yaffs_gross_unlock(dev);
2194 expires = next_dir_update;
2195 if (time_before(next_gc, expires))
2197 if (time_before(expires, now))
2200 Y_INIT_TIMER(&timer);
2201 timer.expires = expires + 1;
2202 timer.data = (unsigned long)current;
2203 timer.function = yaffs_background_waker;
2205 set_current_state(TASK_INTERRUPTIBLE);
2208 del_timer_sync(&timer);
2217 static int yaffs_bg_start(struct yaffs_dev *dev)
2220 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2225 context->bg_running = 1;
2227 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2228 (void *)dev, "yaffs-bg-%d",
2231 if (IS_ERR(context->bg_thread)) {
2232 retval = PTR_ERR(context->bg_thread);
2233 context->bg_thread = NULL;
2234 context->bg_running = 0;
2239 static void yaffs_bg_stop(struct yaffs_dev *dev)
2241 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2243 ctxt->bg_running = 0;
2245 if (ctxt->bg_thread) {
2246 kthread_stop(ctxt->bg_thread);
2247 ctxt->bg_thread = NULL;
2251 static int yaffs_bg_thread_fn(void *data)
2256 static int yaffs_bg_start(struct yaffs_dev *dev)
2261 static void yaffs_bg_stop(struct yaffs_dev *dev)
2267 static void yaffs_flush_inodes(struct super_block *sb)
2270 struct yaffs_obj *obj;
2272 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
2273 obj = yaffs_inode_to_obj(iptr);
2275 yaffs_trace(YAFFS_TRACE_OS,
2278 yaffs_flush_file(obj, 1, 0, 0);
2283 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2285 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2289 yaffs_flush_inodes(sb);
2290 yaffs_update_dirty_dirs(dev);
2291 yaffs_flush_whole_cache(dev, 1);
2293 yaffs_checkpoint_save(dev);
2296 static LIST_HEAD(yaffs_context_list);
2297 struct mutex yaffs_context_lock;
2299 static void yaffs_put_super(struct super_block *sb)
2301 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2302 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
2304 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2307 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2308 "Shutting down yaffs background thread");
2310 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2311 "yaffs background thread shut down");
2313 yaffs_gross_lock(dev);
2315 yaffs_flush_super(sb, 1);
2317 yaffs_deinitialise(dev);
2319 yaffs_gross_unlock(dev);
2321 mutex_lock(&yaffs_context_lock);
2322 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2323 mutex_unlock(&yaffs_context_lock);
2325 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2326 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
2327 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2332 yaffs_put_mtd_device(mtd);
2334 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2335 "yaffs_put_super done");
2339 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
2341 return yaffs_gc_control;
2345 #ifdef YAFFS_COMPILE_EXPORTFS
2347 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
2348 uint32_t generation)
2350 return Y_IGET(sb, ino);
2353 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
2354 struct fid *fid, int fh_len,
2357 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2358 yaffs2_nfs_get_inode);
2361 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
2362 struct fid *fid, int fh_len,
2365 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2366 yaffs2_nfs_get_inode);
2369 struct dentry *yaffs2_get_parent(struct dentry *dentry)
2372 struct super_block *sb = dentry->d_inode->i_sb;
2373 struct dentry *parent = ERR_PTR(-ENOENT);
2374 struct inode *inode;
2375 unsigned long parent_ino;
2376 struct yaffs_obj *d_obj;
2377 struct yaffs_obj *parent_obj;
2379 d_obj = yaffs_inode_to_obj(dentry->d_inode);
2382 parent_obj = d_obj->parent;
2384 parent_ino = yaffs_get_obj_inode(parent_obj);
2385 inode = Y_IGET(sb, parent_ino);
2387 if (IS_ERR(inode)) {
2388 parent = ERR_CAST(inode);
2390 parent = d_obtain_alias(inode);
2391 if (!IS_ERR(parent)) {
2392 parent = ERR_PTR(-ENOMEM);
2402 /* Just declare a zero structure as a NULL value implies
2403 * using the default functions of exportfs.
2406 static struct export_operations yaffs_export_ops = {
2407 .fh_to_dentry = yaffs2_fh_to_dentry,
2408 .fh_to_parent = yaffs2_fh_to_parent,
2409 .get_parent = yaffs2_get_parent,
2414 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
2416 /* Clear the association between the inode and
2417 * the struct yaffs_obj.
2419 obj->my_inode = NULL;
2420 yaffs_inode_to_obj_lv(inode) = NULL;
2422 /* If the object freeing was deferred, then the real
2424 * This should fix the inode inconsistency problem.
2426 yaffs_handle_defered_free(obj);
2429 #ifdef YAFFS_HAS_EVICT_INODE
2430 /* yaffs_evict_inode combines into one operation what was previously done in
2431 * yaffs_clear_inode() and yaffs_delete_inode()
2434 static void yaffs_evict_inode(struct inode *inode)
2436 struct yaffs_obj *obj;
2437 struct yaffs_dev *dev;
2440 obj = yaffs_inode_to_obj(inode);
2442 yaffs_trace(YAFFS_TRACE_OS,
2443 "yaffs_evict_inode: ino %d, count %d %s",
2444 (int)inode->i_ino, atomic_read(&inode->i_count),
2445 obj ? "object exists" : "null object");
2447 if (!inode->i_nlink && !is_bad_inode(inode))
2449 truncate_inode_pages(&inode->i_data, 0);
2450 Y_CLEAR_INODE(inode);
2452 if (deleteme && obj) {
2454 yaffs_gross_lock(dev);
2456 yaffs_gross_unlock(dev);
2460 yaffs_gross_lock(dev);
2461 yaffs_unstitch_obj(inode, obj);
2462 yaffs_gross_unlock(dev);
2467 /* clear is called to tell the fs to release any per-inode data it holds.
2468 * The object might still exist on disk and is just being thrown out of the cache
2469 * or else the object has actually been deleted and we're being called via
2471 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
2474 static void yaffs_clear_inode(struct inode *inode)
2476 struct yaffs_obj *obj;
2477 struct yaffs_dev *dev;
2479 obj = yaffs_inode_to_obj(inode);
2481 yaffs_trace(YAFFS_TRACE_OS,
2482 "yaffs_clear_inode: ino %d, count %d %s",
2483 (int)inode->i_ino, atomic_read(&inode->i_count),
2484 obj ? "object exists" : "null object");
2488 yaffs_gross_lock(dev);
2489 yaffs_unstitch_obj(inode, obj);
2490 yaffs_gross_unlock(dev);
2495 /* delete is called when the link count is zero and the inode
2496 * is put (ie. nobody wants to know about it anymore, time to
2498 * NB Must call clear_inode()
2500 static void yaffs_delete_inode(struct inode *inode)
2502 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2503 struct yaffs_dev *dev;
2505 yaffs_trace(YAFFS_TRACE_OS,
2506 "yaffs_delete_inode: ino %d, count %d %s",
2507 (int)inode->i_ino, atomic_read(&inode->i_count),
2508 obj ? "object exists" : "null object");
2512 yaffs_gross_lock(dev);
2514 yaffs_gross_unlock(dev);
2516 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
2517 truncate_inode_pages(&inode->i_data, 0);
2526 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2527 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2529 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2530 struct super_block *sb = dentry->d_sb;
2531 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2532 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2534 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2536 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2538 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2541 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
2543 yaffs_gross_lock(dev);
2545 buf->f_type = YAFFS_MAGIC;
2546 buf->f_bsize = sb->s_blocksize;
2547 buf->f_namelen = 255;
2549 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2550 /* Do this if chunk size is not a power of 2 */
2552 uint64_t bytes_in_dev;
2553 uint64_t bytes_free;
2557 ((dev->param.end_block - dev->param.start_block +
2558 1))) * ((uint64_t) (dev->param.chunks_per_block *
2559 dev->data_bytes_per_chunk));
2561 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2562 buf->f_blocks = bytes_in_dev;
2564 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
2565 ((uint64_t) (dev->data_bytes_per_chunk));
2567 do_div(bytes_free, sb->s_blocksize);
2569 buf->f_bfree = bytes_free;
2571 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2574 (dev->param.end_block - dev->param.start_block + 1) *
2575 dev->param.chunks_per_block /
2576 (sb->s_blocksize / dev->data_bytes_per_chunk);
2578 yaffs_get_n_free_chunks(dev) /
2579 (sb->s_blocksize / dev->data_bytes_per_chunk);
2582 (dev->param.end_block - dev->param.start_block + 1) *
2583 dev->param.chunks_per_block *
2584 (dev->data_bytes_per_chunk / sb->s_blocksize);
2587 yaffs_get_n_free_chunks(dev) *
2588 (dev->data_bytes_per_chunk / sb->s_blocksize);
2593 buf->f_bavail = buf->f_bfree;
2595 yaffs_gross_unlock(dev);
2601 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
2604 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2605 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2606 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2608 int dirty = yaffs_check_super_dirty(dev);
2610 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2611 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
2613 dirty ? "dirty" : "clean",
2614 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2615 oneshot_checkpoint ? " one-shot" : "");
2617 yaffs_gross_lock(dev);
2618 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2619 oneshot_checkpoint) && !dev->is_checkpointed;
2621 if (dirty || do_checkpoint) {
2622 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2623 yaffs_clear_super_dirty(dev);
2624 if (oneshot_checkpoint)
2625 yaffs_auto_checkpoint &= ~4;
2627 yaffs_gross_unlock(dev);
2633 #ifdef YAFFS_HAS_WRITE_SUPER
2634 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2635 static void yaffs_write_super(struct super_block *sb)
2637 static int yaffs_write_super(struct super_block *sb)
2640 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2642 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2643 "yaffs_write_super %s",
2644 request_checkpoint ? " checkpt" : "");
2646 yaffs_do_sync_fs(sb, request_checkpoint);
2648 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2654 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2655 static int yaffs_sync_fs(struct super_block *sb, int wait)
2657 static int yaffs_sync_fs(struct super_block *sb)
2660 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2662 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2663 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
2665 yaffs_do_sync_fs(sb, request_checkpoint);
2670 /* the function only is used to change dev->read_only when this file system
2673 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2676 struct mtd_info *mtd;
2677 struct yaffs_dev *dev = 0;
2679 /* Get the device */
2680 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2682 yaffs_trace(YAFFS_TRACE_ALWAYS,
2683 "MTD device #%u doesn't appear to exist",
2688 /* Check it's NAND */
2689 if (mtd->type != MTD_NANDFLASH) {
2690 yaffs_trace(YAFFS_TRACE_ALWAYS,
2691 "MTD device is not NAND it's type %d",
2696 read_only = ((*flags & MS_RDONLY) != 0);
2697 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2700 "yaffs: mtd is read only, setting superblock read only");
2701 *flags |= MS_RDONLY;
2704 dev = sb->s_fs_info;
2705 dev->read_only = read_only;
2710 static const struct super_operations yaffs_super_ops = {
2711 .statfs = yaffs_statfs,
2713 #ifndef YAFFS_USE_OWN_IGET
2714 .read_inode = yaffs_read_inode,
2716 #ifdef YAFFS_HAS_PUT_INODE
2717 .put_inode = yaffs_put_inode,
2719 .put_super = yaffs_put_super,
2720 #ifdef YAFFS_HAS_EVICT_INODE
2721 .evict_inode = yaffs_evict_inode,
2723 .delete_inode = yaffs_delete_inode,
2724 .clear_inode = yaffs_clear_inode,
2726 .sync_fs = yaffs_sync_fs,
2727 #ifdef YAFFS_HAS_WRITE_SUPER
2728 .write_super = yaffs_write_super,
2730 .remount_fs = yaffs_remount_fs,
2733 struct yaffs_options {
2735 int skip_checkpoint_read;
2736 int skip_checkpoint_write;
2739 int tags_ecc_overridden;
2740 int lazy_loading_enabled;
2741 int lazy_loading_overridden;
2742 int empty_lost_and_found;
2743 int empty_lost_and_found_overridden;
2744 int disable_summary;
2747 #define MAX_OPT_LEN 30
2748 static int yaffs_parse_options(struct yaffs_options *options,
2749 const char *options_str)
2751 char cur_opt[MAX_OPT_LEN + 1];
2755 /* Parse through the options which is a comma seperated list */
2757 while (options_str && *options_str && !error) {
2758 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2761 while (*options_str == ',')
2764 while (*options_str && *options_str != ',') {
2765 if (p < MAX_OPT_LEN) {
2766 cur_opt[p] = *options_str;
2772 if (!strcmp(cur_opt, "inband-tags")) {
2773 options->inband_tags = 1;
2774 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2775 options->tags_ecc_on = 0;
2776 options->tags_ecc_overridden = 1;
2777 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2778 options->tags_ecc_on = 1;
2779 options->tags_ecc_overridden = 1;
2780 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2781 options->lazy_loading_enabled = 0;
2782 options->lazy_loading_overridden = 1;
2783 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2784 options->lazy_loading_enabled = 1;
2785 options->lazy_loading_overridden = 1;
2786 } else if (!strcmp(cur_opt, "disable-summary")) {
2787 options->disable_summary = 1;
2788 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2789 options->empty_lost_and_found = 0;
2790 options->empty_lost_and_found_overridden = 1;
2791 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2792 options->empty_lost_and_found = 1;
2793 options->empty_lost_and_found_overridden = 1;
2794 } else if (!strcmp(cur_opt, "no-cache")) {
2795 options->no_cache = 1;
2796 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2797 options->skip_checkpoint_read = 1;
2798 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2799 options->skip_checkpoint_write = 1;
2800 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2801 options->skip_checkpoint_read = 1;
2802 options->skip_checkpoint_write = 1;
2804 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2814 static struct dentry *yaffs_make_root(struct inode *inode)
2816 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
2817 struct dentry *root = d_alloc_root(inode);
2824 return d_make_root(inode);
2831 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2832 struct super_block *sb,
2833 void *data, int silent)
2836 struct inode *inode = NULL;
2837 struct dentry *root;
2838 struct yaffs_dev *dev = 0;
2839 char devname_buf[BDEVNAME_SIZE + 1];
2840 struct mtd_info *mtd;
2842 char *data_str = (char *)data;
2843 struct yaffs_linux_context *context = NULL;
2844 struct yaffs_param *param;
2847 int inband_tags = 0;
2849 struct yaffs_options options;
2853 struct yaffs_linux_context *context_iterator;
2854 struct list_head *l;
2857 printk(KERN_INFO "yaffs: sb is NULL\n");
2861 sb->s_magic = YAFFS_MAGIC;
2862 sb->s_op = &yaffs_super_ops;
2863 sb->s_flags |= MS_NOATIME;
2865 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2867 #ifdef YAFFS_COMPILE_EXPORTFS
2868 sb->s_export_op = &yaffs_export_ops;
2872 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2873 else if (!yaffs_devname(sb, devname_buf))
2874 printk(KERN_INFO "yaffs: devname is NULL\n");
2876 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2878 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2883 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2885 memset(&options, 0, sizeof(options));
2887 if (yaffs_parse_options(&options, data_str)) {
2888 /* Option parsing failed */
2892 sb->s_blocksize = PAGE_CACHE_SIZE;
2893 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2895 yaffs_trace(YAFFS_TRACE_OS,
2896 "yaffs_read_super: Using yaffs%d", yaffs_version);
2897 yaffs_trace(YAFFS_TRACE_OS,
2898 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2900 yaffs_trace(YAFFS_TRACE_ALWAYS,
2901 "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
2902 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2903 yaffs_devname(sb, devname_buf));
2905 /* Get the device */
2906 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2908 yaffs_trace(YAFFS_TRACE_ALWAYS,
2909 "yaffs: MTD device %u either not valid or unavailable",
2914 if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2915 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2919 /* Added NCB 26/5/2006 for completeness */
2920 if (yaffs_version == 2 && !options.inband_tags
2921 && WRITE_SIZE(mtd) == 512) {
2922 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2926 if (mtd->oobavail < sizeof(struct yaffs_packed_tags2) ||
2927 options.inband_tags)
2930 if(yaffs_verify_mtd(mtd, yaffs_version, inband_tags) < 0)
2933 /* OK, so if we got here, we have an MTD that's NAND and looks
2934 * like it has the right capabilities
2935 * Set the struct yaffs_dev up for mtd
2938 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2941 "yaffs: mtd is read only, setting superblock read only\n"
2943 sb->s_flags |= MS_RDONLY;
2946 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2947 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2949 if (!dev || !context) {
2955 /* Deep shit could not allocate device structure */
2956 yaffs_trace(YAFFS_TRACE_ALWAYS,
2957 "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
2961 memset(dev, 0, sizeof(struct yaffs_dev));
2962 param = &(dev->param);
2964 memset(context, 0, sizeof(struct yaffs_linux_context));
2965 dev->os_context = context;
2966 INIT_LIST_HEAD(&(context->context_list));
2968 context->super = sb;
2970 dev->read_only = read_only;
2972 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2973 sb->s_fs_info = dev;
2975 sb->u.generic_sbp = dev;
2979 dev->driver_context = mtd;
2980 param->name = mtd->name;
2982 /* Set up the memory size parameters.... */
2985 param->n_reserved_blocks = 5;
2986 param->n_caches = (options.no_cache) ? 0 : 10;
2987 param->inband_tags = inband_tags;
2989 param->enable_xattr = 1;
2990 if (options.lazy_loading_overridden)
2991 param->disable_lazy_load = !options.lazy_loading_enabled;
2993 param->defered_dir_update = 1;
2995 if (options.tags_ecc_overridden)
2996 param->no_tags_ecc = !options.tags_ecc_on;
2998 param->empty_lost_n_found = 1;
2999 param->refresh_period = 500;
3000 param->disable_summary = options.disable_summary;
3003 #ifdef CONFIG_YAFFS_DISABLE_BAD_BLOCK_MARKING
3004 param->disable_bad_block_marking = 1;
3006 if (options.empty_lost_and_found_overridden)
3007 param->empty_lost_n_found = options.empty_lost_and_found;
3009 /* ... and the functions. */
3010 if (yaffs_version == 2) {
3011 param->is_yaffs2 = 1;
3012 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3013 param->total_bytes_per_chunk = mtd->writesize;
3014 param->chunks_per_block = mtd->erasesize / mtd->writesize;
3016 param->total_bytes_per_chunk = mtd->oobblock;
3017 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
3019 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
3021 param->start_block = 0;
3022 param->end_block = n_blocks - 1;
3024 param->is_yaffs2 = 0;
3025 n_blocks = YCALCBLOCKS(mtd->size,
3026 YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
3028 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
3029 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
3032 param->start_block = 0;
3033 param->end_block = n_blocks - 1;
3035 yaffs_mtd_drv_install(dev);
3037 param->sb_dirty_fn = yaffs_set_super_dirty;
3038 param->gc_control_fn = yaffs_gc_control_callback;
3040 yaffs_dev_to_lc(dev)->super = sb;
3042 param->use_nand_ecc = 1;
3044 param->skip_checkpt_rd = options.skip_checkpoint_read;
3045 param->skip_checkpt_wr = options.skip_checkpoint_write;
3047 mutex_lock(&yaffs_context_lock);
3048 /* Get a mount id */
3050 for (mount_id = 0; !found; mount_id++) {
3052 list_for_each(l, &yaffs_context_list) {
3054 list_entry(l, struct yaffs_linux_context,
3056 if (context_iterator->mount_id == mount_id)
3060 context->mount_id = mount_id;
3062 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
3063 &yaffs_context_list);
3064 mutex_unlock(&yaffs_context_lock);
3066 /* Directory search handling... */
3067 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
3068 param->remove_obj_fn = yaffs_remove_obj_callback;
3070 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
3072 yaffs_gross_lock(dev);
3074 err = yaffs_guts_initialise(dev);
3076 yaffs_trace(YAFFS_TRACE_OS,
3077 "yaffs_read_super: guts initialised %s",
3078 (err == YAFFS_OK) ? "OK" : "FAILED");
3080 if (err == YAFFS_OK)
3081 yaffs_bg_start(dev);
3083 if (!context->bg_thread)
3084 param->defered_dir_update = 0;
3086 sb->s_maxbytes = yaffs_max_file_size(dev);
3088 /* Release lock before yaffs_get_inode() */
3089 yaffs_gross_unlock(dev);
3091 /* Create root inode */
3092 if (err == YAFFS_OK)
3093 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
3098 inode->i_op = &yaffs_dir_inode_operations;
3099 inode->i_fop = &yaffs_dir_operations;
3101 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
3103 root = yaffs_make_root(inode);
3109 if(!dev->is_checkpointed)
3110 yaffs_set_super_dirty(dev);
3112 yaffs_trace(YAFFS_TRACE_ALWAYS,
3113 "yaffs_read_super: is_checkpointed %d",
3114 dev->is_checkpointed);
3116 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
3120 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3121 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3124 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3127 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3128 static struct dentry *yaffs_mount(struct file_system_type *fs_type, int flags,
3129 const char *dev_name, void *data)
3131 return mount_bdev(fs_type, flags, dev_name, data, yaffs_internal_read_super_mtd);
3133 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3134 static int yaffs_read_super(struct file_system_type *fs,
3135 int flags, const char *dev_name,
3136 void *data, struct vfsmount *mnt)
3139 return get_sb_bdev(fs, flags, dev_name, data,
3140 yaffs_internal_read_super_mtd, mnt);
3143 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3144 int flags, const char *dev_name,
3148 return get_sb_bdev(fs, flags, dev_name, data,
3149 yaffs_internal_read_super_mtd);
3153 static struct file_system_type yaffs_fs_type = {
3154 .owner = THIS_MODULE,
3156 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3157 .mount = yaffs_mount,
3159 .get_sb = yaffs_read_super,
3161 .kill_sb = kill_block_super,
3162 .fs_flags = FS_REQUIRES_DEV,
3165 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3168 return yaffs_internal_read_super(1, sb, data, silent);
3171 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3176 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3177 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3180 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3183 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3184 static struct dentry *yaffs2_mount(struct file_system_type *fs_type, int flags,
3185 const char *dev_name, void *data)
3187 return mount_bdev(fs_type, flags, dev_name, data, yaffs2_internal_read_super_mtd);
3189 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3190 static int yaffs2_read_super(struct file_system_type *fs,
3191 int flags, const char *dev_name, void *data,
3192 struct vfsmount *mnt)
3194 return get_sb_bdev(fs, flags, dev_name, data,
3195 yaffs2_internal_read_super_mtd, mnt);
3198 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3199 int flags, const char *dev_name,
3203 return get_sb_bdev(fs, flags, dev_name, data,
3204 yaffs2_internal_read_super_mtd);
3208 static struct file_system_type yaffs2_fs_type = {
3209 .owner = THIS_MODULE,
3211 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3212 .mount = yaffs2_mount,
3214 .get_sb = yaffs2_read_super,
3216 .kill_sb = kill_block_super,
3217 .fs_flags = FS_REQUIRES_DEV,
3220 static struct super_block *yaffs2_read_super(struct super_block *sb,
3221 void *data, int silent)
3223 return yaffs_internal_read_super(2, sb, data, silent);
3226 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3231 static struct proc_dir_entry *my_proc_entry;
3233 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3235 struct yaffs_param *param = &dev->param;
3238 yaffs_count_blocks_by_state(dev,bs);
3240 buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
3241 buf += sprintf(buf, "end_block............ %d\n", param->end_block);
3242 buf += sprintf(buf, "total_bytes_per_chunk %d\n",
3243 param->total_bytes_per_chunk);
3244 buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
3245 buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
3246 buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
3247 buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
3248 buf += sprintf(buf, "empty_lost_n_found... %d\n",
3249 param->empty_lost_n_found);
3250 buf += sprintf(buf, "disable_lazy_load.... %d\n",
3251 param->disable_lazy_load);
3252 buf += sprintf(buf, "disable_bad_block_mrk %d\n",
3253 param->disable_bad_block_marking);
3254 buf += sprintf(buf, "refresh_period....... %d\n",
3255 param->refresh_period);
3256 buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
3257 buf += sprintf(buf, "n_reserved_blocks.... %d\n",
3258 param->n_reserved_blocks);
3259 buf += sprintf(buf, "always_check_erased.. %d\n",
3260 param->always_check_erased);
3261 buf += sprintf(buf, "\n");
3262 buf += sprintf(buf, "block count by state\n");
3263 buf += sprintf(buf, "0:%d 1:%d 2:%d 3:%d 4:%d\n",
3264 bs[0], bs[1], bs[2], bs[3], bs[4]);
3265 buf += sprintf(buf, "5:%d 6:%d 7:%d 8:%d 9:%d\n",
3266 bs[5], bs[6], bs[7], bs[8], bs[9]);
3271 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3273 buf += sprintf(buf, "max file size....... %lld\n",
3274 (long long) yaffs_max_file_size(dev));
3275 buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
3276 dev->data_bytes_per_chunk);
3277 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3278 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3279 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3280 buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
3281 dev->blocks_in_checkpt);
3282 buf += sprintf(buf, "\n");
3283 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3284 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3285 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3286 buf += sprintf(buf, "\n");
3287 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3288 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3289 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3290 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3291 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3292 buf += sprintf(buf, "passive_gc_count..... %u\n",
3293 dev->passive_gc_count);
3294 buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
3295 dev->oldest_dirty_gc_count);
3296 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3297 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3298 buf += sprintf(buf, "n_retried_writes..... %u\n",
3299 dev->n_retried_writes);
3300 buf += sprintf(buf, "n_retired_blocks..... %u\n",
3301 dev->n_retired_blocks);
3302 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3303 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3304 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
3305 dev->n_tags_ecc_fixed);
3306 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
3307 dev->n_tags_ecc_unfixed);
3308 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3309 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3310 buf += sprintf(buf, "n_unlinked_files..... %u\n",
3311 dev->n_unlinked_files);
3312 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3313 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3314 buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
3315 buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
3320 static int yaffs_proc_read(char *page,
3322 off_t offset, int count, int *eof, void *data)
3324 struct list_head *item;
3329 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3330 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3331 * This also assumes the user has posted a read buffer large
3332 * enough to hold the complete output; but that's life in /proc.
3337 /* Print header first */
3340 sprintf(buf, "Multi-version YAFFS\n");
3342 buf += sprintf(buf, "\n");
3346 mutex_lock(&yaffs_context_lock);
3348 /* Locate and print the Nth entry. Order N-squared but N is small. */
3349 list_for_each(item, &yaffs_context_list) {
3350 struct yaffs_linux_context *dc =
3351 list_entry(item, struct yaffs_linux_context,
3353 struct yaffs_dev *dev = dc->dev;
3355 if (n < (step & ~1)) {
3359 if ((step & 1) == 0) {
3361 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3363 buf = yaffs_dump_dev_part0(buf, dev);
3365 buf = yaffs_dump_dev_part1(buf, dev);
3370 mutex_unlock(&yaffs_context_lock);
3373 return buf - page < count ? buf - page : count;
3377 * Set the verbosity of the warnings and error messages.
3379 * Note that the names can only be a..z or _ with the current code.
3384 unsigned mask_bitfield;
3386 {"allocate", YAFFS_TRACE_ALLOCATE},
3387 {"always", YAFFS_TRACE_ALWAYS},
3388 {"background", YAFFS_TRACE_BACKGROUND},
3389 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3390 {"buffers", YAFFS_TRACE_BUFFERS},
3391 {"bug", YAFFS_TRACE_BUG},
3392 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3393 {"deletion", YAFFS_TRACE_DELETION},
3394 {"erase", YAFFS_TRACE_ERASE},
3395 {"error", YAFFS_TRACE_ERROR},
3396 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3397 {"gc", YAFFS_TRACE_GC},
3398 {"lock", YAFFS_TRACE_LOCK},
3399 {"mtd", YAFFS_TRACE_MTD},
3400 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3401 {"os", YAFFS_TRACE_OS},
3402 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3403 {"scan", YAFFS_TRACE_SCAN},
3404 {"mount", YAFFS_TRACE_MOUNT},
3405 {"tracing", YAFFS_TRACE_TRACING},
3406 {"sync", YAFFS_TRACE_SYNC},
3407 {"write", YAFFS_TRACE_WRITE},
3408 {"verify", YAFFS_TRACE_VERIFY},
3409 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3410 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3411 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3412 {"all", 0xffffffff},
3417 #define MAX_MASK_NAME_LENGTH 40
3418 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3419 unsigned long count)
3421 unsigned rg = 0, mask_bitfield;
3425 char substring[MAX_MASK_NAME_LENGTH + 1];
3431 rg = yaffs_trace_mask;
3433 while (!done && (pos < count)) {
3435 while ((pos < count) && isspace(buf[pos]))
3452 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3454 if (end > buf + pos) {
3455 mask_name = "numeral";
3456 len = end - (buf + pos);
3460 for (x = buf + pos, i = 0;
3461 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3462 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3464 substring[i] = '\0';
3466 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3467 if (strcmp(substring, mask_flags[i].mask_name)
3469 mask_name = mask_flags[i].mask_name;
3471 mask_flags[i].mask_bitfield;
3478 if (mask_name != NULL) {
3482 rg &= ~mask_bitfield;
3485 rg |= mask_bitfield;
3491 rg |= mask_bitfield;
3497 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3499 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3501 if (rg & YAFFS_TRACE_ALWAYS) {
3502 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3504 flag = ((rg & mask_flags[i].mask_bitfield) ==
3505 mask_flags[i].mask_bitfield) ? '+' : '-';
3506 printk(KERN_DEBUG "%c%s\n", flag,
3507 mask_flags[i].mask_name);
3514 /* Debug strings are of the form:
3515 * .bnnn print info on block n
3516 * .cobjn,chunkn print nand chunk id for objn:chunkn
3519 static int yaffs_proc_debug_write(struct file *file, const char *buf,
3520 unsigned long count)
3529 struct list_head *item;
3531 memset(str, 0, sizeof(str));
3532 memcpy(str, buf, min((size_t)count, sizeof(str) -1));
3540 while (*p1 && *p1 != ',') {
3546 p0_val = simple_strtol(p0, NULL, 0);
3547 p1_val = simple_strtol(p1, NULL, 0);
3550 mutex_lock(&yaffs_context_lock);
3552 /* Locate and print the Nth entry. Order N-squared but N is small. */
3553 list_for_each(item, &yaffs_context_list) {
3554 struct yaffs_linux_context *dc =
3555 list_entry(item, struct yaffs_linux_context,
3557 struct yaffs_dev *dev = dc->dev;
3560 struct yaffs_block_info *bi;
3562 bi = yaffs_get_block_info(dev,p0_val);
3565 printk("Block %d: state %d, retire %d, use %d, seq %d\n",
3566 (int)p0_val, bi->block_state,
3567 bi->needs_retiring, bi->pages_in_use,
3570 } else if (cmd == 'c') {
3571 struct yaffs_obj *obj;
3574 obj = yaffs_find_by_number(dev, p0_val);
3576 printk("No obj %d\n", (int)p0_val);
3579 nand_chunk = obj->hdr_chunk;
3582 yaffs_find_chunk_in_file(obj,
3584 printk("Nand chunk for %d:%d is %d\n",
3585 (int)p0_val, (int)p1_val, nand_chunk);
3590 mutex_unlock(&yaffs_context_lock);
3596 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0))
3597 static int yaffs_proc_write(struct file *file, const char *buf,
3598 unsigned long count, void *ppos)
3600 static ssize_t yaffs_proc_write(struct file *file, const char __user *buf,
3601 size_t count, loff_t *ppos)
3605 return yaffs_proc_debug_write(file, buf, count);
3606 return yaffs_proc_write_trace_options(file, buf, count);
3609 /* Stuff to handle installation of file systems */
3610 struct file_system_to_install {
3611 struct file_system_type *fst;
3615 static struct file_system_to_install fs_to_install[] = {
3616 {&yaffs_fs_type, 0},
3617 {&yaffs2_fs_type, 0},
3622 #ifdef YAFFS_NEW_PROCFS
3623 static int yaffs_proc_show(struct seq_file *m, void *v)
3625 /* FIXME: Unify in a better way? */
3630 len = yaffs_proc_read(buffer, &start, 0, sizeof(buffer), NULL, NULL);
3631 seq_puts(m, buffer);
3635 static int yaffs_proc_open(struct inode *inode, struct file *file)
3637 return single_open(file, yaffs_proc_show, NULL);
3640 static struct file_operations procfs_ops = {
3641 .owner = THIS_MODULE,
3642 .open = yaffs_proc_open,
3644 .write = yaffs_proc_write,
3647 static int yaffs_procfs_init(void)
3649 /* Install the proc_fs entries */
3650 my_proc_entry = proc_create("yaffs",
3655 if (my_proc_entry) {
3665 static int yaffs_procfs_init(void)
3667 /* Install the proc_fs entries */
3668 my_proc_entry = create_proc_entry("yaffs",
3669 S_IRUGO | S_IFREG, YPROC_ROOT);
3671 if (my_proc_entry) {
3672 my_proc_entry->write_proc = yaffs_proc_write;
3673 my_proc_entry->read_proc = yaffs_proc_read;
3674 my_proc_entry->data = NULL;
3684 static int __init init_yaffs_fs(void)
3687 struct file_system_to_install *fsinst;
3689 yaffs_trace(YAFFS_TRACE_ALWAYS,
3690 "yaffs Installing.");
3692 mutex_init(&yaffs_context_lock);
3694 error = yaffs_procfs_init();
3698 /* Now add the file system entries */
3700 fsinst = fs_to_install;
3702 while (fsinst->fst && !error) {
3703 error = register_filesystem(fsinst->fst);
3705 fsinst->installed = 1;
3709 /* Any errors? uninstall */
3711 fsinst = fs_to_install;
3713 while (fsinst->fst) {
3714 if (fsinst->installed) {
3715 unregister_filesystem(fsinst->fst);
3716 fsinst->installed = 0;
3725 static void __exit exit_yaffs_fs(void)
3728 struct file_system_to_install *fsinst;
3730 yaffs_trace(YAFFS_TRACE_ALWAYS,
3733 remove_proc_entry("yaffs", YPROC_ROOT);
3735 fsinst = fs_to_install;
3737 while (fsinst->fst) {
3738 if (fsinst->installed) {
3739 unregister_filesystem(fsinst->fst);
3740 fsinst->installed = 0;
3746 module_init(init_yaffs_fs)
3747 module_exit(exit_yaffs_fs)
3749 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3750 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
3751 MODULE_LICENSE("GPL");