2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * Luc van OostenRyck for numerous patches.
9 * Nick Bane for numerous patches.
10 * Nick Bane for 2.5/2.6 integration.
11 * Andras Toth for mknod rdev issue.
12 * Michael Fischer for finding the problem with inode inconsistency.
13 * Some code bodily lifted from JFFS
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
22 * This is the file system front-end to YAFFS that hooks it up to
26 * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
28 * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
30 * >> inode->u.generic_ip points to the associated struct yaffs_obj.
34 * There are two variants of the VFS glue code. This variant should compile
35 * for any version of Linux.
37 #include <linux/version.h>
39 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
40 #define YAFFS_COMPILE_BACKGROUND
41 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23))
42 #define YAFFS_COMPILE_FREEZER
46 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
47 #define YAFFS_COMPILE_EXPORTFS
50 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
51 #define YAFFS_USE_SETATTR_COPY
52 #define YAFFS_USE_TRUNCATE_SETSIZE
54 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
55 #define YAFFS_HAS_EVICT_INODE
58 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13)) && \
59 (LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0))
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"
179 #include "yaffs_packedtags2.h"
180 #include "yaffs_getblockinfo.h"
182 unsigned int yaffs_trace_mask =
183 YAFFS_TRACE_BAD_BLOCKS |
187 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
188 unsigned int yaffs_auto_checkpoint = 1;
189 unsigned int yaffs_gc_control = 1;
190 unsigned int yaffs_bg_enable = 1;
191 unsigned int yaffs_auto_select = 1;
192 /* Module Parameters */
193 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
194 module_param(yaffs_trace_mask, uint, 0644);
195 module_param(yaffs_wr_attempts, uint, 0644);
196 module_param(yaffs_auto_checkpoint, uint, 0644);
197 module_param(yaffs_gc_control, uint, 0644);
198 module_param(yaffs_bg_enable, uint, 0644);
200 MODULE_PARM(yaffs_trace_mask, "i");
201 MODULE_PARM(yaffs_wr_attempts, "i");
202 MODULE_PARM(yaffs_auto_checkpoint, "i");
203 MODULE_PARM(yaffs_gc_control, "i");
206 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
207 /* use iget and read_inode */
208 #define Y_IGET(sb, inum) iget((sb), (inum))
211 /* Call local equivalent */
212 #define YAFFS_USE_OWN_IGET
213 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
217 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
218 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
220 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
223 #define yaffs_inode_to_obj(iptr) \
224 ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
225 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
227 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
228 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
230 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
233 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
234 #define Y_CLEAR_INODE(i) clear_inode(i)
236 #define Y_CLEAR_INODE(i) end_writeback(i)
239 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
240 #define YAFFS_USE_DIR_ITERATE
243 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0))
244 #define YAFFS_USE_XATTR
247 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,12,0))
248 #define YAFFS_NEW_PROCFS
249 #include <linux/seq_file.h>
252 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
253 #define PAGE_CACHE_SIZE PAGE_SIZE
254 #define PAGE_CACHE_SHIFT PAGE_SHIFT
255 #define Y_GET_DENTRY(f) ((f)->f_path.dentry)
256 #define YAFFS_NEW_XATTR 1
257 #define YAFFS_NEW_GET_LINK 1
259 #define Y_GET_DENTRY(f) ((f)->f_dentry)
260 #define YAFFS_NEW_XATTR 0
261 #define YAFFS_NEW_GET_LINK 0
264 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0))
265 #define page_cache_release put_page
268 #define update_dir_time(dir) do {\
269 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
272 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0))
273 static inline int setattr_prepare(struct dentry *dentry, struct iattr *attr)
275 return inode_change_ok(dentry->d_inode, attr);
279 static void yaffs_fill_inode_from_obj(struct inode *inode,
280 struct yaffs_obj *obj);
283 static void yaffs_gross_lock(struct yaffs_dev *dev)
285 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
286 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
287 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
290 static void yaffs_gross_unlock(struct yaffs_dev *dev)
292 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
293 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
297 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
299 /* Lifted from jffs2 */
301 struct yaffs_obj *obj;
302 unsigned char *pg_buf;
304 loff_t pos = ((loff_t) pg->index) << PAGE_SHIFT;
305 struct yaffs_dev *dev;
307 yaffs_trace(YAFFS_TRACE_OS,
308 "yaffs_readpage_nolock at %lld, size %08x",
310 (unsigned)PAGE_SIZE);
312 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
316 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
317 BUG_ON(!PageLocked(pg));
324 /* FIXME: Can kmap fail? */
326 yaffs_gross_lock(dev);
328 ret = yaffs_file_rd(obj, pg_buf, pos, PAGE_CACHE_SIZE);
330 yaffs_gross_unlock(dev);
336 ClearPageUptodate(pg);
343 flush_dcache_page(pg);
346 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
350 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
352 int ret = yaffs_readpage_nolock(f, pg);
357 static int yaffs_readpage(struct file *f, struct page *pg)
361 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
362 ret = yaffs_readpage_unlock(f, pg);
363 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
368 static void yaffs_set_super_dirty_val(struct yaffs_dev *dev, int val)
370 struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
375 # ifdef YAFFS_SUPER_HAS_DIRTY
377 struct super_block *sb = lc->super;
386 static void yaffs_set_super_dirty(struct yaffs_dev *dev)
388 yaffs_set_super_dirty_val(dev, 1);
391 static void yaffs_clear_super_dirty(struct yaffs_dev *dev)
393 yaffs_set_super_dirty_val(dev, 0);
396 static int yaffs_check_super_dirty(struct yaffs_dev *dev)
398 struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
403 # ifdef YAFFS_SUPER_HAS_DIRTY
405 struct super_block *sb = lc->super;
407 if (sb && sb->s_dirt)
415 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
416 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
418 static int yaffs_writepage(struct page *page)
421 struct yaffs_dev *dev;
422 struct address_space *mapping = page->mapping;
424 unsigned long end_index;
426 struct yaffs_obj *obj;
433 inode = mapping->host;
436 i_size = i_size_read(inode);
438 end_index = i_size >> PAGE_CACHE_SHIFT;
440 if (page->index < end_index)
441 n_bytes = PAGE_CACHE_SIZE;
443 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
445 if (page->index > end_index || !n_bytes) {
446 yaffs_trace(YAFFS_TRACE_OS,
447 "yaffs_writepage at %lld, inode size = %lld!!",
448 ((loff_t)page->index) << PAGE_CACHE_SHIFT,
450 yaffs_trace(YAFFS_TRACE_OS,
453 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
454 set_page_writeback(page);
456 end_page_writeback(page);
461 if (n_bytes != PAGE_CACHE_SIZE)
462 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
468 obj = yaffs_inode_to_obj(inode);
470 yaffs_gross_lock(dev);
472 yaffs_trace(YAFFS_TRACE_OS,
473 "yaffs_writepage at %lld, size %08x",
474 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes);
475 yaffs_trace(YAFFS_TRACE_OS,
476 "writepag0: obj = %lld, ino = %lld",
477 obj->variant.file_variant.file_size, inode->i_size);
479 n_written = yaffs_wr_file(obj, buffer,
480 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes, 0);
482 yaffs_set_super_dirty(dev);
484 yaffs_trace(YAFFS_TRACE_OS,
485 "writepag1: obj = %lld, ino = %lld",
486 obj->variant.file_variant.file_size, inode->i_size);
488 yaffs_gross_unlock(dev);
491 set_page_writeback(page);
493 end_page_writeback(page);
496 return (n_written == n_bytes) ? 0 : -ENOSPC;
499 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
500 /* For now we just assume few parallel writes and check against a small number. */
501 /* Todo: need to do this with a counter to handle parallel reads better */
503 static ssize_t yaffs_hold_space(struct file *f)
505 struct yaffs_obj *obj;
506 struct yaffs_dev *dev;
510 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
514 yaffs_gross_lock(dev);
516 n_free_chunks = yaffs_get_n_free_chunks(dev);
518 yaffs_gross_unlock(dev);
520 return (n_free_chunks > 20) ? 1 : 0;
523 static void yaffs_release_space(struct file *f)
525 struct yaffs_obj *obj;
526 struct yaffs_dev *dev;
528 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
532 yaffs_gross_lock(dev);
534 yaffs_gross_unlock(dev);
537 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
538 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
539 loff_t pos, unsigned len, unsigned flags,
540 struct page **pagep, void **fsdata)
542 struct page *pg = NULL;
543 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
549 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
550 pg = grab_cache_page_write_begin(mapping, index, flags);
552 pg = __grab_cache_page(mapping, index);
560 yaffs_trace(YAFFS_TRACE_OS,
561 "start yaffs_write_begin index %d(%x) uptodate %d",
562 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
565 space_held = yaffs_hold_space(filp);
572 /* Update page if required */
574 if (!Page_Uptodate(pg))
575 ret = yaffs_readpage_nolock(filp, pg);
580 /* Happy path return */
581 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
586 yaffs_trace(YAFFS_TRACE_OS,
587 "end yaffs_write_begin fail returning %d", ret);
589 yaffs_release_space(filp);
592 page_cache_release(pg);
599 static int yaffs_prepare_write(struct file *f, struct page *pg,
600 unsigned offset, unsigned to)
602 yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
604 if (!Page_Uptodate(pg))
605 return yaffs_readpage_nolock(f, pg);
611 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
614 struct yaffs_obj *obj;
618 struct yaffs_dev *dev;
620 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
623 yaffs_trace(YAFFS_TRACE_OS,
624 "yaffs_file_write: hey obj is null!");
630 yaffs_gross_lock(dev);
632 inode = Y_GET_DENTRY(f)->d_inode;
634 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
635 ipos = inode->i_size;
639 yaffs_trace(YAFFS_TRACE_OS,
640 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %lld",
641 (unsigned)n, (unsigned)n, obj->obj_id, ipos);
643 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
645 yaffs_set_super_dirty(dev);
647 yaffs_trace(YAFFS_TRACE_OS,
648 "yaffs_file_write: %d(%x) bytes written",
649 (unsigned)n, (unsigned)n);
654 if (ipos > inode->i_size) {
655 inode->i_size = ipos;
656 inode->i_blocks = (ipos + 511) >> 9;
658 yaffs_trace(YAFFS_TRACE_OS,
659 "yaffs_file_write size updated to %lld bytes, %d blocks",
660 ipos, (int)(inode->i_blocks));
664 yaffs_gross_unlock(dev);
665 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
669 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
670 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
671 loff_t pos, unsigned len, unsigned copied,
672 struct page *pg, void *fsdadata)
676 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
679 addr = kva + offset_into_page;
681 yaffs_trace(YAFFS_TRACE_OS,
682 "yaffs_write_end addr %p pos %lld n_bytes %d",
685 ret = yaffs_file_write(filp, addr, copied, &pos);
688 yaffs_trace(YAFFS_TRACE_OS,
689 "yaffs_write_end not same size ret %d copied %d",
696 yaffs_release_space(filp);
698 page_cache_release(pg);
703 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
708 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
709 int n_bytes = to - offset;
715 yaffs_trace(YAFFS_TRACE_OS,
716 "yaffs_commit_write addr %p pos %lld n_bytes %d",
719 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
721 if (n_written != n_bytes) {
722 yaffs_trace(YAFFS_TRACE_OS,
723 "yaffs_commit_write not same size n_written %d n_bytes %d",
729 yaffs_trace(YAFFS_TRACE_OS,
730 "yaffs_commit_write returning %d",
731 n_written == n_bytes ? 0 : n_written);
733 return n_written == n_bytes ? 0 : n_written;
737 static struct address_space_operations yaffs_file_address_operations = {
738 .readpage = yaffs_readpage,
739 .writepage = yaffs_writepage,
740 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
741 .write_begin = yaffs_write_begin,
742 .write_end = yaffs_write_end,
744 .prepare_write = yaffs_prepare_write,
745 .commit_write = yaffs_commit_write,
750 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
751 static int yaffs_file_flush(struct file *file, fl_owner_t id)
753 static int yaffs_file_flush(struct file *file)
756 struct yaffs_obj *obj = yaffs_dentry_to_obj(Y_GET_DENTRY(file));
758 struct yaffs_dev *dev = obj->my_dev;
760 yaffs_trace(YAFFS_TRACE_OS,
761 "yaffs_file_flush object %d (%s)",
763 obj->dirty ? "dirty" : "clean");
765 yaffs_gross_lock(dev);
767 yaffs_flush_file(obj, 1, 0, 0);
769 yaffs_gross_unlock(dev);
775 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
776 static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync)
777 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
778 static int yaffs_sync_object(struct file *file, int datasync)
780 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
784 struct yaffs_obj *obj;
785 struct yaffs_dev *dev;
786 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
787 struct dentry *dentry = file->f_path.dentry;
790 obj = yaffs_dentry_to_obj(dentry);
794 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
795 "yaffs_sync_object");
796 yaffs_gross_lock(dev);
797 yaffs_flush_file(obj, 1, datasync, 0);
798 yaffs_gross_unlock(dev);
803 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
804 static const struct file_operations yaffs_file_operations = {
805 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
806 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
807 .read = new_sync_read,
808 .write = new_sync_write,
810 .read_iter = generic_file_read_iter,
811 .write_iter = generic_file_write_iter,
813 .read = do_sync_read,
814 .write = do_sync_write,
815 .aio_read = generic_file_aio_read,
816 .aio_write = generic_file_aio_write,
818 .mmap = generic_file_mmap,
819 .flush = yaffs_file_flush,
820 .fsync = yaffs_sync_object,
821 .splice_read = generic_file_splice_read,
822 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
823 .splice_write = iter_file_splice_write,
825 .splice_write = generic_file_splice_write,
827 .llseek = generic_file_llseek,
830 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
832 static const struct file_operations yaffs_file_operations = {
833 .read = do_sync_read,
834 .write = do_sync_write,
835 .aio_read = generic_file_aio_read,
836 .aio_write = generic_file_aio_write,
837 .mmap = generic_file_mmap,
838 .flush = yaffs_file_flush,
839 .fsync = yaffs_sync_object,
840 .sendfile = generic_file_sendfile,
845 static const struct file_operations yaffs_file_operations = {
846 .read = generic_file_read,
847 .write = generic_file_write,
848 .mmap = generic_file_mmap,
849 .flush = yaffs_file_flush,
850 .fsync = yaffs_sync_object,
851 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
852 .sendfile = generic_file_sendfile,
860 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
861 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
863 void *kaddr = kmap_atomic(page, KM_USER0);
864 memset(kaddr + start, 0, end - start);
865 kunmap_atomic(kaddr, KM_USER0);
866 flush_dcache_page(page);
871 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
873 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
874 truncate_setsize(inode, newsize);
877 truncate_inode_pages(&inode->i_data, newsize);
884 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
886 #ifdef YAFFS_USE_SETATTR_COPY
887 setattr_copy(inode, attr);
890 return inode_setattr(inode, attr);
895 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
897 struct inode *inode = dentry->d_inode;
899 struct yaffs_dev *dev;
901 yaffs_trace(YAFFS_TRACE_OS,
902 "yaffs_setattr of object %d",
903 yaffs_inode_to_obj(inode)->obj_id);
905 /* Fail if a requested resize >= 2GB */
906 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
911 error = setattr_prepare(dentry, attr);
915 error = yaffs_vfs_setattr(inode, attr);
916 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
917 if (attr->ia_valid & ATTR_SIZE) {
918 yaffs_vfs_setsize(inode, attr->ia_size);
919 inode->i_blocks = (inode->i_size + 511) >> 9;
922 dev = yaffs_inode_to_obj(inode)->my_dev;
923 if (attr->ia_valid & ATTR_SIZE) {
924 yaffs_trace(YAFFS_TRACE_OS,
926 (int)(attr->ia_size),
927 (int)(attr->ia_size));
929 yaffs_gross_lock(dev);
930 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
931 if (result == YAFFS_OK) {
936 yaffs_gross_unlock(dev);
940 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
945 #ifdef YAFFS_USE_XATTR
946 #if (YAFFS_NEW_XATTR > 0)
947 static int yaffs_setxattr(struct dentry *dentry, struct inode *inode,
948 const char *name, const void *value, size_t size, int flags)
951 static int yaffs_setxattr(struct dentry *dentry, const char *name,
952 const void *value, size_t size, int flags)
954 struct inode *inode = dentry->d_inode;
957 struct yaffs_dev *dev;
958 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
960 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
965 yaffs_gross_lock(dev);
966 result = yaffs_set_xattrib(obj, name, value, size, flags);
967 if (result == YAFFS_OK)
971 yaffs_gross_unlock(dev);
974 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
979 #if (YAFFS_NEW_XATTR > 0)
980 static ssize_t yaffs_getxattr(struct dentry * dentry, struct inode *inode,
981 const char *name, void *buff, size_t size)
984 static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
985 void *buff, size_t size)
987 struct inode *inode = dentry->d_inode;
990 struct yaffs_dev *dev;
991 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
993 yaffs_trace(YAFFS_TRACE_OS,
994 "yaffs_getxattr \"%s\" from object %d",
999 yaffs_gross_lock(dev);
1000 error = yaffs_get_xattrib(obj, name, buff, size);
1001 yaffs_gross_unlock(dev);
1004 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
1009 static int yaffs_removexattr(struct dentry *dentry, const char *name)
1011 struct inode *inode = dentry->d_inode;
1013 struct yaffs_dev *dev;
1014 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1016 yaffs_trace(YAFFS_TRACE_OS,
1017 "yaffs_removexattr of object %d", obj->obj_id);
1022 yaffs_gross_lock(dev);
1023 result = yaffs_remove_xattrib(obj, name);
1024 if (result == YAFFS_OK)
1026 else if (result < 0)
1028 yaffs_gross_unlock(dev);
1031 yaffs_trace(YAFFS_TRACE_OS,
1032 "yaffs_removexattr done returning %d", error);
1038 static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
1040 struct inode *inode = dentry->d_inode;
1042 struct yaffs_dev *dev;
1043 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1045 yaffs_trace(YAFFS_TRACE_OS,
1046 "yaffs_listxattr of object %d", obj->obj_id);
1050 yaffs_gross_lock(dev);
1051 error = yaffs_list_xattrib(obj, buff, size);
1052 yaffs_gross_unlock(dev);
1055 yaffs_trace(YAFFS_TRACE_OS,
1056 "yaffs_listxattr done returning %d", error);
1062 static const struct inode_operations yaffs_file_inode_operations = {
1063 .setattr = yaffs_setattr,
1064 #ifdef YAFFS_USE_XATTR
1065 .setxattr = yaffs_setxattr,
1066 .getxattr = yaffs_getxattr,
1067 .removexattr = yaffs_removexattr,
1069 .listxattr = yaffs_listxattr,
1073 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1076 unsigned char *alias;
1079 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1081 yaffs_gross_lock(dev);
1083 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1085 yaffs_gross_unlock(dev);
1090 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0)
1091 ret = vfs_readlink(dentry, buffer, buflen, alias);
1093 ret = readlink_copy(buffer, buflen, alias);
1099 #if (YAFFS_NEW_GET_LINK == 0)
1100 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1101 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1105 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1109 unsigned char *alias;
1111 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1113 yaffs_gross_lock(dev);
1115 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1116 yaffs_gross_unlock(dev);
1122 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1123 nd_set_link(nd, alias);
1127 ret = ERR_PTR(ret_int);
1130 ret = vfs_follow_link(nd, alias);
1139 static const char *yaffs_get_link(struct dentry *dentry, struct inode *inode, struct delayed_call *done)
1141 unsigned char *alias;
1142 struct yaffs_dev *dev;
1145 return ERR_PTR(-ECHILD);
1147 dev = yaffs_dentry_to_obj(dentry)->my_dev;
1149 yaffs_gross_lock(dev);
1151 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1152 yaffs_gross_unlock(dev);
1155 return ERR_PTR(-ENOMEM);
1156 set_delayed_call(done, kfree_link, alias);
1161 #ifdef YAFFS_HAS_PUT_INODE
1163 /* For now put inode is just for debugging
1164 * Put inode is called when the inode **structure** is put.
1166 static void yaffs_put_inode(struct inode *inode)
1168 yaffs_trace(YAFFS_TRACE_OS,
1169 "yaffs_put_inode: ino %d, count %d"),
1170 (int)inode->i_ino, atomic_read(&inode->i_count);
1175 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1176 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
1182 static const struct inode_operations yaffs_symlink_inode_operations = {
1183 .readlink = yaffs_readlink,
1184 #if (YAFFS_NEW_GET_LINK == 1)
1185 .get_link = yaffs_get_link,
1187 .follow_link = yaffs_follow_link,
1189 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1190 .put_link = yaffs_put_link,
1192 .setattr = yaffs_setattr,
1193 #ifdef YAFFS_USE_XATTR
1194 .setxattr = yaffs_setxattr,
1195 .getxattr = yaffs_getxattr,
1196 .removexattr = yaffs_removexattr,
1198 .listxattr = yaffs_listxattr,
1201 #ifdef YAFFS_USE_OWN_IGET
1203 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1205 struct inode *inode;
1206 struct yaffs_obj *obj;
1207 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1209 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
1211 inode = iget_locked(sb, ino);
1213 return ERR_PTR(-ENOMEM);
1214 if (!(inode->i_state & I_NEW))
1217 /* NB This is called as a side effect of other functions, but
1218 * we had to release the lock to prevent deadlocks, so
1219 * need to lock again.
1222 yaffs_gross_lock(dev);
1224 obj = yaffs_find_by_number(dev, inode->i_ino);
1226 yaffs_fill_inode_from_obj(inode, obj);
1228 yaffs_gross_unlock(dev);
1230 unlock_new_inode(inode);
1236 static void yaffs_read_inode(struct inode *inode)
1238 /* NB This is called as a side effect of other functions, but
1239 * we had to release the lock to prevent deadlocks, so
1240 * need to lock again.
1243 struct yaffs_obj *obj;
1244 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
1246 yaffs_trace(YAFFS_TRACE_OS,
1247 "yaffs_read_inode for %d", (int)inode->i_ino);
1249 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1250 yaffs_gross_lock(dev);
1252 obj = yaffs_find_by_number(dev, inode->i_ino);
1254 yaffs_fill_inode_from_obj(inode, obj);
1256 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1257 yaffs_gross_unlock(dev);
1264 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1265 struct yaffs_obj *obj)
1267 struct inode *inode;
1270 yaffs_trace(YAFFS_TRACE_OS,
1271 "yaffs_get_inode for NULL super_block!!");
1277 yaffs_trace(YAFFS_TRACE_OS,
1278 "yaffs_get_inode for NULL object!!");
1283 yaffs_trace(YAFFS_TRACE_OS,
1284 "yaffs_get_inode for object %d", obj->obj_id);
1286 inode = Y_IGET(sb, obj->obj_id);
1290 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1291 /* iget also increments the inode's i_count */
1292 /* NB You can't be holding gross_lock or deadlock will happen! */
1299 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1302 #define YCRED(x) (x->cred)
1305 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0)
1306 #define YPROC_uid(p) (YCRED(p)->fsuid)
1307 #define YPROC_gid(p) (YCRED(p)->fsgid)
1308 #define EXTRACT_gid(x) x
1309 #define EXTRACT_uid(x) x
1310 #define MAKE_gid(x) x
1311 #define MAKE_uid(x) x
1313 #define YPROC_uid(p) from_kuid(&init_user_ns, YCRED(p)->fsuid)
1314 #define YPROC_gid(p) from_kgid(&init_user_ns, YCRED(p)->fsgid)
1315 #define EXTRACT_gid(x) from_kgid(&init_user_ns, x)
1316 #define EXTRACT_uid(x) from_kuid(&init_user_ns, x)
1317 #define MAKE_gid(x) make_kgid(&init_user_ns, x)
1318 #define MAKE_uid(x) make_kuid(&init_user_ns, x)
1322 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1323 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1325 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1326 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1329 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1333 struct inode *inode;
1335 struct yaffs_obj *obj = NULL;
1336 struct yaffs_dev *dev;
1338 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1340 int error = -ENOSPC;
1341 uid_t uid = YPROC_uid(current);
1343 (dir->i_mode & S_ISGID) ? EXTRACT_gid(dir->i_gid) : YPROC_gid(current);
1345 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1349 yaffs_trace(YAFFS_TRACE_OS,
1350 "yaffs_mknod: parent object %d type %d",
1351 parent->obj_id, parent->variant_type);
1353 yaffs_trace(YAFFS_TRACE_OS,
1354 "yaffs_mknod: could not get parent object");
1358 yaffs_trace(YAFFS_TRACE_OS,
1359 "yaffs_mknod: making oject for %s, mode %x dev %x",
1360 dentry->d_name.name, mode, rdev);
1362 dev = parent->my_dev;
1364 yaffs_gross_lock(dev);
1366 switch (mode & S_IFMT) {
1368 /* Special (socket, fifo, device...) */
1369 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
1370 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1372 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1373 gid, old_encode_dev(rdev));
1376 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1380 case S_IFREG: /* file */
1381 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
1382 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1385 case S_IFDIR: /* directory */
1386 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
1387 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1390 case S_IFLNK: /* symlink */
1391 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
1392 obj = NULL; /* Do we ever get here? */
1396 /* Can not call yaffs_get_inode() with gross lock held */
1397 yaffs_gross_unlock(dev);
1400 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1401 d_instantiate(dentry, inode);
1402 update_dir_time(dir);
1403 yaffs_trace(YAFFS_TRACE_OS,
1404 "yaffs_mknod created object %d count = %d",
1405 obj->obj_id, atomic_read(&inode->i_count));
1407 yaffs_fill_inode_from_obj(dir, parent);
1409 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
1416 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1417 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1419 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1423 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
1424 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1429 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
1430 static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1432 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1433 static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1434 struct nameidata *n)
1435 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1436 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1437 struct nameidata *n)
1439 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1442 yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
1443 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1446 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
1447 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1449 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1450 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1451 struct nameidata *n)
1453 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1456 struct yaffs_obj *obj;
1457 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1459 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
1461 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1462 yaffs_gross_lock(dev);
1464 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
1465 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
1467 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
1469 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
1471 /* Can't hold gross lock when calling yaffs_get_inode() */
1472 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1473 yaffs_gross_unlock(dev);
1476 yaffs_trace(YAFFS_TRACE_OS,
1477 "yaffs_lookup found %d", obj->obj_id);
1479 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1481 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
1485 /* added NCB for 2.5/6 compatability - forces add even if inode is
1486 * NULL which creates dentry hash */
1487 d_add(dentry, inode);
1495 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1496 struct dentry *dentry)
1498 struct inode *inode = old_dentry->d_inode;
1499 struct yaffs_obj *obj = NULL;
1500 struct yaffs_obj *link = NULL;
1501 struct yaffs_dev *dev;
1503 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
1505 obj = yaffs_inode_to_obj(inode);
1508 yaffs_gross_lock(dev);
1510 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1512 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1516 set_nlink(old_dentry->d_inode, yaffs_get_obj_link_count(obj));
1517 d_instantiate(dentry, old_dentry->d_inode);
1518 atomic_inc(&old_dentry->d_inode->i_count);
1519 yaffs_trace(YAFFS_TRACE_OS,
1520 "yaffs_link link count %d i_count %d",
1521 old_dentry->d_inode->i_nlink,
1522 atomic_read(&old_dentry->d_inode->i_count));
1525 yaffs_gross_unlock(dev);
1528 update_dir_time(dir);
1535 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1536 const char *symname)
1538 struct yaffs_obj *obj;
1539 struct yaffs_dev *dev;
1540 uid_t uid = YPROC_uid(current);
1542 (dir->i_mode & S_ISGID) ? EXTRACT_gid(dir->i_gid) : YPROC_gid(current);
1544 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
1546 if (strnlen(dentry->d_name.name, YAFFS_MAX_NAME_LENGTH + 1) >
1547 YAFFS_MAX_NAME_LENGTH)
1548 return -ENAMETOOLONG;
1550 if (strnlen(symname, YAFFS_MAX_ALIAS_LENGTH + 1) >
1551 YAFFS_MAX_ALIAS_LENGTH)
1552 return -ENAMETOOLONG;
1554 dev = yaffs_inode_to_obj(dir)->my_dev;
1555 yaffs_gross_lock(dev);
1556 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1557 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1558 yaffs_gross_unlock(dev);
1561 struct inode *inode;
1563 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1564 d_instantiate(dentry, inode);
1565 update_dir_time(dir);
1566 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
1569 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
1576 * The VFS layer already does all the dentry stuff for rename.
1578 * NB: POSIX says you can rename an object over an old object of the same name
1580 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0))
1581 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1582 struct inode *new_dir, struct dentry *new_dentry, unsigned int unused)
1584 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1585 struct inode *new_dir, struct dentry *new_dentry)
1588 struct yaffs_dev *dev;
1589 int ret_val = YAFFS_FAIL;
1590 struct yaffs_obj *target;
1592 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
1593 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1595 yaffs_gross_lock(dev);
1597 /* Check if the target is an existing directory that is not empty. */
1598 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1599 new_dentry->d_name.name);
1601 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1602 !list_empty(&target->variant.dir_variant.children)) {
1604 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
1606 ret_val = YAFFS_FAIL;
1608 /* Now does unlinking internally using shadowing mechanism */
1609 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
1611 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1612 old_dentry->d_name.name,
1613 yaffs_inode_to_obj(new_dir),
1614 new_dentry->d_name.name);
1616 yaffs_gross_unlock(dev);
1618 if (ret_val == YAFFS_OK) {
1620 inode_dec_link_count(new_dentry->d_inode);
1622 update_dir_time(old_dir);
1623 if (old_dir != new_dir)
1624 update_dir_time(new_dir);
1634 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1638 struct yaffs_dev *dev;
1639 struct yaffs_obj *obj;
1641 yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
1642 (int)(dir->i_ino), dentry->d_name.name);
1643 obj = yaffs_inode_to_obj(dir);
1646 yaffs_gross_lock(dev);
1648 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1650 if (ret_val == YAFFS_OK) {
1651 inode_dec_link_count(dentry->d_inode);
1653 yaffs_gross_unlock(dev);
1654 update_dir_time(dir);
1657 yaffs_gross_unlock(dev);
1663 static const struct inode_operations yaffs_dir_inode_operations = {
1664 .create = yaffs_create,
1665 .lookup = yaffs_lookup,
1667 .unlink = yaffs_unlink,
1668 .symlink = yaffs_symlink,
1669 .mkdir = yaffs_mkdir,
1670 .rmdir = yaffs_unlink,
1671 .mknod = yaffs_mknod,
1672 .rename = yaffs_rename,
1673 .setattr = yaffs_setattr,
1674 .listxattr = yaffs_listxattr,
1675 #ifdef YAFFS_USE_XATTR
1676 .setxattr = yaffs_setxattr,
1677 .getxattr = yaffs_getxattr,
1678 .removexattr = yaffs_removexattr,
1682 /*-----------------------------------------------------------------*/
1683 /* Directory search context allows us to unlock access to yaffs during
1684 * filldir without causing problems with the directory being modified.
1685 * This is similar to the tried and tested mechanism used in yaffs direct.
1687 * A search context iterates along a doubly linked list of siblings in the
1688 * directory. If the iterating object is deleted then this would corrupt
1689 * the list iteration, likely causing a crash. The search context avoids
1690 * this by using the remove_obj_fn to move the search context to the
1691 * next object before the object is deleted.
1693 * Many readdirs (and thus seach conexts) may be alive simulateously so
1694 * each struct yaffs_dev has a list of these.
1696 * A seach context lives for the duration of a readdir.
1698 * All these functions must be called while yaffs is locked.
1701 struct yaffs_search_context {
1702 struct yaffs_dev *dev;
1703 struct yaffs_obj *dir_obj;
1704 struct yaffs_obj *next_return;
1705 struct list_head others;
1709 * yaffs_new_search() creates a new search context, initialises it and
1710 * adds it to the device's search context list.
1712 * Called at start of readdir.
1714 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
1716 struct yaffs_dev *dev = dir->my_dev;
1717 struct yaffs_search_context *sc =
1718 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
1722 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
1723 sc->next_return = NULL;
1726 list_entry(dir->variant.dir_variant.children.next,
1727 struct yaffs_obj, siblings);
1728 INIT_LIST_HEAD(&sc->others);
1729 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
1735 * yaffs_search_end() disposes of a search context and cleans up.
1737 static void yaffs_search_end(struct yaffs_search_context *sc)
1740 list_del(&sc->others);
1746 * yaffs_search_advance() moves a search context to the next object.
1747 * Called when the search iterates or when an object removal causes
1748 * the search context to be moved to the next object.
1750 static void yaffs_search_advance(struct yaffs_search_context *sc)
1755 if (sc->next_return == NULL ||
1756 list_empty(&sc->dir_obj->variant.dir_variant.children))
1757 sc->next_return = NULL;
1759 struct list_head *next = sc->next_return->siblings.next;
1761 if (next == &sc->dir_obj->variant.dir_variant.children)
1762 sc->next_return = NULL; /* end of list */
1765 list_entry(next, struct yaffs_obj, siblings);
1770 * yaffs_remove_obj_callback() is called when an object is unlinked.
1771 * We check open search contexts and advance any which are currently
1772 * on the object being iterated.
1774 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
1777 struct list_head *i;
1778 struct yaffs_search_context *sc;
1779 struct list_head *search_contexts =
1780 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
1782 /* Iterate through the directory search contexts.
1783 * If any are currently on the object being removed, then advance
1784 * the search context to the next object to prevent a hanging pointer.
1786 list_for_each(i, search_contexts) {
1787 sc = list_entry(i, struct yaffs_search_context, others);
1788 if (sc->next_return == obj)
1789 yaffs_search_advance(sc);
1795 /*-----------------------------------------------------------------*/
1797 #ifdef YAFFS_USE_DIR_ITERATE
1798 static int yaffs_iterate(struct file *f, struct dir_context *dc)
1800 struct yaffs_obj *obj;
1801 struct yaffs_dev *dev;
1802 struct yaffs_search_context *sc;
1803 unsigned long curoffs;
1804 struct yaffs_obj *l;
1807 char name[YAFFS_MAX_NAME_LENGTH + 1];
1809 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
1812 yaffs_gross_lock(dev);
1814 yaffs_dev_to_lc(dev)->readdir_process = current;
1816 sc = yaffs_new_search(obj);
1822 if (!dir_emit_dots(f, dc)) {
1823 yaffs_gross_unlock(dev);
1829 while (sc->next_return) {
1831 l = sc->next_return;
1832 if (curoffs >= dc->pos) {
1833 int this_inode = yaffs_get_obj_inode(l);
1834 int this_type = yaffs_get_obj_type(l);
1836 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1837 yaffs_trace(YAFFS_TRACE_OS,
1838 "yaffs_readdir: %s inode %d",
1839 name, yaffs_get_obj_inode(l));
1841 yaffs_gross_unlock(dev);
1848 yaffs_gross_lock(dev);
1852 yaffs_gross_lock(dev);
1857 yaffs_search_advance(sc);
1861 yaffs_search_end(sc);
1862 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1863 yaffs_gross_unlock(dev);
1870 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1872 struct yaffs_obj *obj;
1873 struct yaffs_dev *dev;
1874 struct yaffs_search_context *sc;
1875 struct inode *inode = Y_GET_DENTRY(f)->d_inode;
1876 unsigned long offset, curoffs;
1877 struct yaffs_obj *l;
1880 char name[YAFFS_MAX_NAME_LENGTH + 1];
1882 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
1885 yaffs_gross_lock(dev);
1887 yaffs_dev_to_lc(dev)->readdir_process = current;
1891 sc = yaffs_new_search(obj);
1897 yaffs_trace(YAFFS_TRACE_OS,
1898 "yaffs_readdir: starting at %d", (int)offset);
1901 yaffs_trace(YAFFS_TRACE_OS,
1902 "yaffs_readdir: entry . ino %d",
1904 yaffs_gross_unlock(dev);
1905 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1906 yaffs_gross_lock(dev);
1909 yaffs_gross_lock(dev);
1914 yaffs_trace(YAFFS_TRACE_OS,
1915 "yaffs_readdir: entry .. ino %d",
1916 (int)f->f_dentry->d_parent->d_inode->i_ino);
1917 yaffs_gross_unlock(dev);
1918 if (filldir(dirent, "..", 2, offset,
1919 f->f_dentry->d_parent->d_inode->i_ino,
1921 yaffs_gross_lock(dev);
1924 yaffs_gross_lock(dev);
1931 /* If the directory has changed since the open or last call to
1932 readdir, rewind to after the 2 canned entries. */
1933 if (f->f_version != inode->i_version) {
1936 f->f_version = inode->i_version;
1939 while (sc->next_return) {
1941 l = sc->next_return;
1942 if (curoffs >= offset) {
1943 int this_inode = yaffs_get_obj_inode(l);
1944 int this_type = yaffs_get_obj_type(l);
1946 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1947 yaffs_trace(YAFFS_TRACE_OS,
1948 "yaffs_readdir: %s inode %d",
1949 name, yaffs_get_obj_inode(l));
1951 yaffs_gross_unlock(dev);
1956 offset, this_inode, this_type) < 0) {
1957 yaffs_gross_lock(dev);
1961 yaffs_gross_lock(dev);
1966 yaffs_search_advance(sc);
1970 yaffs_search_end(sc);
1971 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1972 yaffs_gross_unlock(dev);
1979 static const struct file_operations yaffs_dir_operations = {
1980 .read = generic_read_dir,
1981 #ifdef YAFFS_USE_DIR_ITERATE
1982 .iterate = yaffs_iterate,
1984 .readdir = yaffs_readdir,
1986 .fsync = yaffs_sync_object,
1987 .llseek = generic_file_llseek,
1990 static void yaffs_fill_inode_from_obj(struct inode *inode,
1991 struct yaffs_obj *obj)
1995 /* Check mode against the variant type and attempt to repair if broken. */
1996 u32 mode = obj->yst_mode;
1997 switch (obj->variant_type) {
1998 case YAFFS_OBJECT_TYPE_FILE:
1999 if (!S_ISREG(mode)) {
2000 obj->yst_mode &= ~S_IFMT;
2001 obj->yst_mode |= S_IFREG;
2005 case YAFFS_OBJECT_TYPE_SYMLINK:
2006 if (!S_ISLNK(mode)) {
2007 obj->yst_mode &= ~S_IFMT;
2008 obj->yst_mode |= S_IFLNK;
2012 case YAFFS_OBJECT_TYPE_DIRECTORY:
2013 if (!S_ISDIR(mode)) {
2014 obj->yst_mode &= ~S_IFMT;
2015 obj->yst_mode |= S_IFDIR;
2019 case YAFFS_OBJECT_TYPE_UNKNOWN:
2020 case YAFFS_OBJECT_TYPE_HARDLINK:
2021 case YAFFS_OBJECT_TYPE_SPECIAL:
2027 inode->i_flags |= S_NOATIME;
2029 inode->i_ino = obj->obj_id;
2030 inode->i_mode = obj->yst_mode;
2031 inode->i_uid = MAKE_uid(obj->yst_uid);
2032 inode->i_gid = MAKE_gid(obj->yst_gid);
2033 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
2034 inode->i_blksize = inode->i_sb->s_blocksize;
2036 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2038 inode->i_rdev = old_decode_dev(obj->yst_rdev);
2039 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2040 inode->i_atime.tv_nsec = 0;
2041 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2042 inode->i_mtime.tv_nsec = 0;
2043 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2044 inode->i_ctime.tv_nsec = 0;
2046 inode->i_rdev = obj->yst_rdev;
2047 inode->i_atime = obj->yst_atime;
2048 inode->i_mtime = obj->yst_mtime;
2049 inode->i_ctime = obj->yst_ctime;
2051 inode->i_size = yaffs_get_obj_length(obj);
2052 inode->i_blocks = (inode->i_size + 511) >> 9;
2054 set_nlink(inode, yaffs_get_obj_link_count(obj));
2056 yaffs_trace(YAFFS_TRACE_OS,
2057 "yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
2058 inode->i_mode, obj->yst_uid, obj->yst_gid,
2059 inode->i_size, atomic_read(&inode->i_count));
2061 switch (obj->yst_mode & S_IFMT) {
2062 default: /* fifo, device or socket */
2063 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2064 init_special_inode(inode, obj->yst_mode,
2065 old_decode_dev(obj->yst_rdev));
2067 init_special_inode(inode, obj->yst_mode,
2068 (dev_t) (obj->yst_rdev));
2071 case S_IFREG: /* file */
2072 inode->i_op = &yaffs_file_inode_operations;
2073 inode->i_fop = &yaffs_file_operations;
2074 inode->i_mapping->a_ops =
2075 &yaffs_file_address_operations;
2077 case S_IFDIR: /* directory */
2078 inode->i_op = &yaffs_dir_inode_operations;
2079 inode->i_fop = &yaffs_dir_operations;
2081 case S_IFLNK: /* symlink */
2082 inode->i_op = &yaffs_symlink_inode_operations;
2086 yaffs_inode_to_obj_lv(inode) = obj;
2088 obj->my_inode = inode;
2091 yaffs_trace(YAFFS_TRACE_OS,
2092 "yaffs_fill_inode invalid parameters");
2100 * yaffs background thread functions .
2101 * yaffs_bg_thread_fn() the thread function
2102 * yaffs_bg_start() launches the background thread.
2103 * yaffs_bg_stop() cleans up the background thread.
2106 * The thread should only run after the yaffs is initialised
2107 * The thread should be stopped before yaffs is unmounted.
2108 * The thread should not do any writing while the fs is in read only.
2111 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2113 unsigned erased_chunks =
2114 dev->n_erased_blocks * dev->param.chunks_per_block;
2115 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2116 unsigned scattered = 0; /* Free chunks not in an erased block */
2118 if (erased_chunks < dev->n_free_chunks)
2119 scattered = (dev->n_free_chunks - erased_chunks);
2121 if (!context->bg_running)
2123 else if (scattered < (dev->param.chunks_per_block * 2))
2125 else if (erased_chunks > dev->n_free_chunks / 2)
2127 else if (erased_chunks > dev->n_free_chunks / 4)
2133 #ifdef YAFFS_COMPILE_BACKGROUND
2135 void yaffs_background_waker(unsigned long data)
2137 wake_up_process((struct task_struct *)data);
2140 static int yaffs_bg_thread_fn(void *data)
2142 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2143 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2144 unsigned long now = jiffies;
2145 unsigned long next_dir_update = now;
2146 unsigned long next_gc = now;
2147 unsigned long expires;
2148 unsigned int urgency;
2151 struct timer_list timer;
2153 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2154 "yaffs_background starting for dev %p", (void *)dev);
2156 #ifdef YAFFS_COMPILE_FREEZER
2159 while (context->bg_running) {
2160 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
2162 if (kthread_should_stop())
2165 #ifdef YAFFS_COMPILE_FREEZER
2166 if (try_to_freeze())
2169 yaffs_gross_lock(dev);
2173 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
2174 yaffs_update_dirty_dirs(dev);
2175 next_dir_update = now + HZ;
2178 if (time_after(now, next_gc) && yaffs_bg_enable) {
2179 if (!dev->is_checkpointed) {
2180 urgency = yaffs_bg_gc_urgency(dev);
2181 gc_result = yaffs_bg_gc(dev, urgency);
2183 next_gc = now + HZ / 20 + 1;
2184 else if (urgency > 0)
2185 next_gc = now + HZ / 10 + 1;
2187 next_gc = now + HZ * 2;
2190 * gc not running so set to next_dir_update
2191 * to cut down on wake ups
2193 next_gc = next_dir_update;
2196 yaffs_gross_unlock(dev);
2198 expires = next_dir_update;
2199 if (time_before(next_gc, expires))
2201 if (time_before(expires, now))
2204 Y_INIT_TIMER(&timer);
2205 timer.expires = expires + 1;
2206 timer.data = (unsigned long)current;
2207 timer.function = yaffs_background_waker;
2209 set_current_state(TASK_INTERRUPTIBLE);
2212 del_timer_sync(&timer);
2221 static int yaffs_bg_start(struct yaffs_dev *dev)
2224 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2229 context->bg_running = 1;
2231 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2232 (void *)dev, "yaffs-bg-%d",
2235 if (IS_ERR(context->bg_thread)) {
2236 retval = PTR_ERR(context->bg_thread);
2237 context->bg_thread = NULL;
2238 context->bg_running = 0;
2243 static void yaffs_bg_stop(struct yaffs_dev *dev)
2245 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2247 ctxt->bg_running = 0;
2249 if (ctxt->bg_thread) {
2250 kthread_stop(ctxt->bg_thread);
2251 ctxt->bg_thread = NULL;
2255 static int yaffs_bg_thread_fn(void *data)
2260 static int yaffs_bg_start(struct yaffs_dev *dev)
2265 static void yaffs_bg_stop(struct yaffs_dev *dev)
2271 static void yaffs_flush_inodes(struct super_block *sb)
2274 struct yaffs_obj *obj;
2276 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
2277 obj = yaffs_inode_to_obj(iptr);
2279 yaffs_trace(YAFFS_TRACE_OS,
2282 yaffs_flush_file(obj, 1, 0, 0);
2287 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2289 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2293 yaffs_flush_inodes(sb);
2294 yaffs_update_dirty_dirs(dev);
2295 yaffs_flush_whole_cache(dev, 1);
2297 yaffs_checkpoint_save(dev);
2300 static LIST_HEAD(yaffs_context_list);
2301 struct mutex yaffs_context_lock;
2303 static void yaffs_put_super(struct super_block *sb)
2305 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2306 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
2308 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2311 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2312 "Shutting down yaffs background thread");
2314 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2315 "yaffs background thread shut down");
2317 yaffs_gross_lock(dev);
2319 yaffs_flush_super(sb, 1);
2321 yaffs_deinitialise(dev);
2323 yaffs_gross_unlock(dev);
2325 mutex_lock(&yaffs_context_lock);
2326 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2327 mutex_unlock(&yaffs_context_lock);
2329 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2330 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
2331 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2336 yaffs_put_mtd_device(mtd);
2338 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2339 "yaffs_put_super done");
2343 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
2345 return yaffs_gc_control;
2349 #ifdef YAFFS_COMPILE_EXPORTFS
2351 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
2352 uint32_t generation)
2354 return Y_IGET(sb, ino);
2357 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
2358 struct fid *fid, int fh_len,
2361 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2362 yaffs2_nfs_get_inode);
2365 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
2366 struct fid *fid, int fh_len,
2369 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2370 yaffs2_nfs_get_inode);
2373 struct dentry *yaffs2_get_parent(struct dentry *dentry)
2376 struct super_block *sb = dentry->d_inode->i_sb;
2377 struct dentry *parent = ERR_PTR(-ENOENT);
2378 struct inode *inode;
2379 unsigned long parent_ino;
2380 struct yaffs_obj *d_obj;
2381 struct yaffs_obj *parent_obj;
2383 d_obj = yaffs_inode_to_obj(dentry->d_inode);
2386 parent_obj = d_obj->parent;
2388 parent_ino = yaffs_get_obj_inode(parent_obj);
2389 inode = Y_IGET(sb, parent_ino);
2391 if (IS_ERR(inode)) {
2392 parent = ERR_CAST(inode);
2394 parent = d_obtain_alias(inode);
2395 if (!IS_ERR(parent)) {
2396 parent = ERR_PTR(-ENOMEM);
2406 /* Just declare a zero structure as a NULL value implies
2407 * using the default functions of exportfs.
2410 static struct export_operations yaffs_export_ops = {
2411 .fh_to_dentry = yaffs2_fh_to_dentry,
2412 .fh_to_parent = yaffs2_fh_to_parent,
2413 .get_parent = yaffs2_get_parent,
2418 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
2420 /* Clear the association between the inode and
2421 * the struct yaffs_obj.
2423 obj->my_inode = NULL;
2424 yaffs_inode_to_obj_lv(inode) = NULL;
2426 /* If the object freeing was deferred, then the real
2428 * This should fix the inode inconsistency problem.
2430 yaffs_handle_defered_free(obj);
2433 #ifdef YAFFS_HAS_EVICT_INODE
2434 /* yaffs_evict_inode combines into one operation what was previously done in
2435 * yaffs_clear_inode() and yaffs_delete_inode()
2438 static void yaffs_evict_inode(struct inode *inode)
2440 struct yaffs_obj *obj;
2441 struct yaffs_dev *dev;
2444 obj = yaffs_inode_to_obj(inode);
2446 yaffs_trace(YAFFS_TRACE_OS,
2447 "yaffs_evict_inode: ino %d, count %d %s",
2448 (int)inode->i_ino, atomic_read(&inode->i_count),
2449 obj ? "object exists" : "null object");
2451 if (!inode->i_nlink && !is_bad_inode(inode))
2453 truncate_inode_pages(&inode->i_data, 0);
2454 Y_CLEAR_INODE(inode);
2456 if (deleteme && obj) {
2458 yaffs_gross_lock(dev);
2460 yaffs_gross_unlock(dev);
2464 yaffs_gross_lock(dev);
2465 yaffs_unstitch_obj(inode, obj);
2466 yaffs_gross_unlock(dev);
2471 /* clear is called to tell the fs to release any per-inode data it holds.
2472 * The object might still exist on disk and is just being thrown out of the cache
2473 * or else the object has actually been deleted and we're being called via
2475 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
2478 static void yaffs_clear_inode(struct inode *inode)
2480 struct yaffs_obj *obj;
2481 struct yaffs_dev *dev;
2483 obj = yaffs_inode_to_obj(inode);
2485 yaffs_trace(YAFFS_TRACE_OS,
2486 "yaffs_clear_inode: ino %d, count %d %s",
2487 (int)inode->i_ino, atomic_read(&inode->i_count),
2488 obj ? "object exists" : "null object");
2492 yaffs_gross_lock(dev);
2493 yaffs_unstitch_obj(inode, obj);
2494 yaffs_gross_unlock(dev);
2499 /* delete is called when the link count is zero and the inode
2500 * is put (ie. nobody wants to know about it anymore, time to
2502 * NB Must call clear_inode()
2504 static void yaffs_delete_inode(struct inode *inode)
2506 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2507 struct yaffs_dev *dev;
2509 yaffs_trace(YAFFS_TRACE_OS,
2510 "yaffs_delete_inode: ino %d, count %d %s",
2511 (int)inode->i_ino, atomic_read(&inode->i_count),
2512 obj ? "object exists" : "null object");
2516 yaffs_gross_lock(dev);
2518 yaffs_gross_unlock(dev);
2520 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
2521 truncate_inode_pages(&inode->i_data, 0);
2530 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2531 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2533 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2534 struct super_block *sb = dentry->d_sb;
2535 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2536 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2538 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2540 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2542 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2545 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
2547 yaffs_gross_lock(dev);
2549 buf->f_type = YAFFS_MAGIC;
2550 buf->f_bsize = sb->s_blocksize;
2551 buf->f_namelen = 255;
2553 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2554 /* Do this if chunk size is not a power of 2 */
2556 uint64_t bytes_in_dev;
2557 uint64_t bytes_free;
2561 ((dev->param.end_block - dev->param.start_block +
2562 1))) * ((uint64_t) (dev->param.chunks_per_block *
2563 dev->data_bytes_per_chunk));
2565 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2566 buf->f_blocks = bytes_in_dev;
2568 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
2569 ((uint64_t) (dev->data_bytes_per_chunk));
2571 do_div(bytes_free, sb->s_blocksize);
2573 buf->f_bfree = bytes_free;
2575 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2578 (dev->param.end_block - dev->param.start_block + 1) *
2579 dev->param.chunks_per_block /
2580 (sb->s_blocksize / dev->data_bytes_per_chunk);
2582 yaffs_get_n_free_chunks(dev) /
2583 (sb->s_blocksize / dev->data_bytes_per_chunk);
2586 (dev->param.end_block - dev->param.start_block + 1) *
2587 dev->param.chunks_per_block *
2588 (dev->data_bytes_per_chunk / sb->s_blocksize);
2591 yaffs_get_n_free_chunks(dev) *
2592 (dev->data_bytes_per_chunk / sb->s_blocksize);
2597 buf->f_bavail = buf->f_bfree;
2599 yaffs_gross_unlock(dev);
2605 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
2608 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2609 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2610 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2612 int dirty = yaffs_check_super_dirty(dev);
2614 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2615 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
2617 dirty ? "dirty" : "clean",
2618 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2619 oneshot_checkpoint ? " one-shot" : "");
2621 yaffs_gross_lock(dev);
2622 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2623 oneshot_checkpoint) && !dev->is_checkpointed;
2625 if (dirty || do_checkpoint) {
2626 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2627 yaffs_clear_super_dirty(dev);
2628 if (oneshot_checkpoint)
2629 yaffs_auto_checkpoint &= ~4;
2631 yaffs_gross_unlock(dev);
2637 #ifdef YAFFS_HAS_WRITE_SUPER
2638 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2639 static void yaffs_write_super(struct super_block *sb)
2641 static int yaffs_write_super(struct super_block *sb)
2644 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2646 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2647 "yaffs_write_super %s",
2648 request_checkpoint ? " checkpt" : "");
2650 yaffs_do_sync_fs(sb, request_checkpoint);
2652 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2658 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2659 static int yaffs_sync_fs(struct super_block *sb, int wait)
2661 static int yaffs_sync_fs(struct super_block *sb)
2664 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2666 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2667 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
2669 yaffs_do_sync_fs(sb, request_checkpoint);
2674 /* the function only is used to change dev->read_only when this file system
2677 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2680 struct mtd_info *mtd;
2681 struct yaffs_dev *dev = 0;
2683 /* Get the device */
2684 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2686 yaffs_trace(YAFFS_TRACE_ALWAYS,
2687 "MTD device #%u doesn't appear to exist",
2692 /* Check it's NAND */
2693 if (mtd->type != MTD_NANDFLASH) {
2694 yaffs_trace(YAFFS_TRACE_ALWAYS,
2695 "MTD device is not NAND it's type %d",
2700 read_only = ((*flags & MS_RDONLY) != 0);
2701 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2704 "yaffs: mtd is read only, setting superblock read only");
2705 *flags |= MS_RDONLY;
2708 dev = sb->s_fs_info;
2709 dev->read_only = read_only;
2714 static const struct super_operations yaffs_super_ops = {
2715 .statfs = yaffs_statfs,
2717 #ifndef YAFFS_USE_OWN_IGET
2718 .read_inode = yaffs_read_inode,
2720 #ifdef YAFFS_HAS_PUT_INODE
2721 .put_inode = yaffs_put_inode,
2723 .put_super = yaffs_put_super,
2724 #ifdef YAFFS_HAS_EVICT_INODE
2725 .evict_inode = yaffs_evict_inode,
2727 .delete_inode = yaffs_delete_inode,
2728 .clear_inode = yaffs_clear_inode,
2730 .sync_fs = yaffs_sync_fs,
2731 #ifdef YAFFS_HAS_WRITE_SUPER
2732 .write_super = yaffs_write_super,
2734 .remount_fs = yaffs_remount_fs,
2737 struct yaffs_options {
2739 int skip_checkpoint_read;
2740 int skip_checkpoint_write;
2743 int tags_ecc_overridden;
2744 int lazy_loading_enabled;
2745 int lazy_loading_overridden;
2746 int empty_lost_and_found;
2747 int empty_lost_and_found_overridden;
2748 int disable_summary;
2751 #define MAX_OPT_LEN 30
2752 static int yaffs_parse_options(struct yaffs_options *options,
2753 const char *options_str)
2755 char cur_opt[MAX_OPT_LEN + 1];
2759 /* Parse through the options which is a comma seperated list */
2761 while (options_str && *options_str && !error) {
2762 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2765 while (*options_str == ',')
2768 while (*options_str && *options_str != ',') {
2769 if (p < MAX_OPT_LEN) {
2770 cur_opt[p] = *options_str;
2776 if (!strcmp(cur_opt, "inband-tags")) {
2777 options->inband_tags = 1;
2778 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2779 options->tags_ecc_on = 0;
2780 options->tags_ecc_overridden = 1;
2781 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2782 options->tags_ecc_on = 1;
2783 options->tags_ecc_overridden = 1;
2784 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2785 options->lazy_loading_enabled = 0;
2786 options->lazy_loading_overridden = 1;
2787 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2788 options->lazy_loading_enabled = 1;
2789 options->lazy_loading_overridden = 1;
2790 } else if (!strcmp(cur_opt, "disable-summary")) {
2791 options->disable_summary = 1;
2792 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2793 options->empty_lost_and_found = 0;
2794 options->empty_lost_and_found_overridden = 1;
2795 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2796 options->empty_lost_and_found = 1;
2797 options->empty_lost_and_found_overridden = 1;
2798 } else if (!strcmp(cur_opt, "no-cache")) {
2799 options->no_cache = 1;
2800 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2801 options->skip_checkpoint_read = 1;
2802 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2803 options->skip_checkpoint_write = 1;
2804 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2805 options->skip_checkpoint_read = 1;
2806 options->skip_checkpoint_write = 1;
2808 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2818 static struct dentry *yaffs_make_root(struct inode *inode)
2820 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
2821 struct dentry *root = d_alloc_root(inode);
2828 return d_make_root(inode);
2835 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2836 struct super_block *sb,
2837 void *data, int silent)
2840 struct inode *inode = NULL;
2841 struct dentry *root;
2842 struct yaffs_dev *dev = 0;
2843 char devname_buf[BDEVNAME_SIZE + 1];
2844 struct mtd_info *mtd;
2846 char *data_str = (char *)data;
2847 struct yaffs_linux_context *context = NULL;
2848 struct yaffs_param *param;
2851 int inband_tags = 0;
2853 struct yaffs_options options;
2857 struct yaffs_linux_context *context_iterator;
2858 struct list_head *l;
2861 printk(KERN_INFO "yaffs: sb is NULL\n");
2865 sb->s_magic = YAFFS_MAGIC;
2866 sb->s_op = &yaffs_super_ops;
2867 sb->s_flags |= MS_NOATIME;
2869 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2871 #ifdef YAFFS_COMPILE_EXPORTFS
2872 sb->s_export_op = &yaffs_export_ops;
2876 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2877 else if (!yaffs_devname(sb, devname_buf))
2878 printk(KERN_INFO "yaffs: devname is NULL\n");
2880 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2882 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2887 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2889 memset(&options, 0, sizeof(options));
2891 if (yaffs_parse_options(&options, data_str)) {
2892 /* Option parsing failed */
2896 sb->s_blocksize = PAGE_CACHE_SIZE;
2897 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2899 yaffs_trace(YAFFS_TRACE_OS,
2900 "yaffs_read_super: Using yaffs%d", yaffs_version);
2901 yaffs_trace(YAFFS_TRACE_OS,
2902 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2904 yaffs_trace(YAFFS_TRACE_ALWAYS,
2905 "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
2906 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2907 yaffs_devname(sb, devname_buf));
2909 /* Get the device */
2910 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2912 yaffs_trace(YAFFS_TRACE_ALWAYS,
2913 "yaffs: MTD device %u either not valid or unavailable",
2918 if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2919 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2923 /* Added NCB 26/5/2006 for completeness */
2924 if (yaffs_version == 2 && !options.inband_tags
2925 && WRITE_SIZE(mtd) == 512) {
2926 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2930 if (mtd->oobavail < sizeof(struct yaffs_packed_tags2) ||
2931 options.inband_tags)
2934 if(yaffs_verify_mtd(mtd, yaffs_version, inband_tags) < 0)
2937 /* OK, so if we got here, we have an MTD that's NAND and looks
2938 * like it has the right capabilities
2939 * Set the struct yaffs_dev up for mtd
2942 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2945 "yaffs: mtd is read only, setting superblock read only\n"
2947 sb->s_flags |= MS_RDONLY;
2950 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2951 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2953 if (!dev || !context) {
2959 /* Deep shit could not allocate device structure */
2960 yaffs_trace(YAFFS_TRACE_ALWAYS,
2961 "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
2965 memset(dev, 0, sizeof(struct yaffs_dev));
2966 param = &(dev->param);
2968 memset(context, 0, sizeof(struct yaffs_linux_context));
2969 dev->os_context = context;
2970 INIT_LIST_HEAD(&(context->context_list));
2972 context->super = sb;
2974 dev->read_only = read_only;
2976 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2977 sb->s_fs_info = dev;
2979 sb->u.generic_sbp = dev;
2983 dev->driver_context = mtd;
2984 param->name = mtd->name;
2986 /* Set up the memory size parameters.... */
2989 param->n_reserved_blocks = 5;
2990 param->n_caches = (options.no_cache) ? 0 : 10;
2991 param->inband_tags = inband_tags;
2993 param->enable_xattr = 1;
2994 if (options.lazy_loading_overridden)
2995 param->disable_lazy_load = !options.lazy_loading_enabled;
2997 param->defered_dir_update = 1;
2999 if (options.tags_ecc_overridden)
3000 param->no_tags_ecc = !options.tags_ecc_on;
3002 param->empty_lost_n_found = 1;
3003 param->refresh_period = 500;
3004 param->disable_summary = options.disable_summary;
3007 #ifdef CONFIG_YAFFS_DISABLE_BAD_BLOCK_MARKING
3008 param->disable_bad_block_marking = 1;
3010 if (options.empty_lost_and_found_overridden)
3011 param->empty_lost_n_found = options.empty_lost_and_found;
3013 /* ... and the functions. */
3014 if (yaffs_version == 2) {
3015 param->is_yaffs2 = 1;
3016 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3017 param->total_bytes_per_chunk = mtd->writesize;
3018 param->chunks_per_block = mtd->erasesize / mtd->writesize;
3020 param->total_bytes_per_chunk = mtd->oobblock;
3021 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
3023 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
3025 param->start_block = 0;
3026 param->end_block = n_blocks - 1;
3028 param->is_yaffs2 = 0;
3029 n_blocks = YCALCBLOCKS(mtd->size,
3030 YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
3032 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
3033 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
3036 param->start_block = 0;
3037 param->end_block = n_blocks - 1;
3039 yaffs_mtd_drv_install(dev);
3041 param->sb_dirty_fn = yaffs_set_super_dirty;
3042 param->gc_control_fn = yaffs_gc_control_callback;
3044 yaffs_dev_to_lc(dev)->super = sb;
3046 param->use_nand_ecc = 1;
3048 param->skip_checkpt_rd = options.skip_checkpoint_read;
3049 param->skip_checkpt_wr = options.skip_checkpoint_write;
3051 mutex_lock(&yaffs_context_lock);
3052 /* Get a mount id */
3054 for (mount_id = 0; !found; mount_id++) {
3056 list_for_each(l, &yaffs_context_list) {
3058 list_entry(l, struct yaffs_linux_context,
3060 if (context_iterator->mount_id == mount_id)
3064 context->mount_id = mount_id;
3066 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
3067 &yaffs_context_list);
3068 mutex_unlock(&yaffs_context_lock);
3070 /* Directory search handling... */
3071 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
3072 param->remove_obj_fn = yaffs_remove_obj_callback;
3074 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
3076 yaffs_gross_lock(dev);
3078 err = yaffs_guts_initialise(dev);
3080 yaffs_trace(YAFFS_TRACE_OS,
3081 "yaffs_read_super: guts initialised %s",
3082 (err == YAFFS_OK) ? "OK" : "FAILED");
3084 if (err == YAFFS_OK)
3085 yaffs_bg_start(dev);
3087 if (!context->bg_thread)
3088 param->defered_dir_update = 0;
3090 sb->s_maxbytes = yaffs_max_file_size(dev);
3092 /* Release lock before yaffs_get_inode() */
3093 yaffs_gross_unlock(dev);
3095 /* Create root inode */
3096 if (err == YAFFS_OK)
3097 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
3102 inode->i_op = &yaffs_dir_inode_operations;
3103 inode->i_fop = &yaffs_dir_operations;
3105 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
3107 root = yaffs_make_root(inode);
3113 if(!dev->is_checkpointed)
3114 yaffs_set_super_dirty(dev);
3116 yaffs_trace(YAFFS_TRACE_ALWAYS,
3117 "yaffs_read_super: is_checkpointed %d",
3118 dev->is_checkpointed);
3120 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
3124 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3125 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3128 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3131 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3132 static struct dentry *yaffs_mount(struct file_system_type *fs_type, int flags,
3133 const char *dev_name, void *data)
3135 return mount_bdev(fs_type, flags, dev_name, data, yaffs_internal_read_super_mtd);
3137 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3138 static int yaffs_read_super(struct file_system_type *fs,
3139 int flags, const char *dev_name,
3140 void *data, struct vfsmount *mnt)
3143 return get_sb_bdev(fs, flags, dev_name, data,
3144 yaffs_internal_read_super_mtd, mnt);
3147 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3148 int flags, const char *dev_name,
3152 return get_sb_bdev(fs, flags, dev_name, data,
3153 yaffs_internal_read_super_mtd);
3157 static struct file_system_type yaffs_fs_type = {
3158 .owner = THIS_MODULE,
3160 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3161 .mount = yaffs_mount,
3163 .get_sb = yaffs_read_super,
3165 .kill_sb = kill_block_super,
3166 .fs_flags = FS_REQUIRES_DEV,
3169 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3172 return yaffs_internal_read_super(1, sb, data, silent);
3175 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3180 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3181 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3184 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3187 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3188 static struct dentry *yaffs2_mount(struct file_system_type *fs_type, int flags,
3189 const char *dev_name, void *data)
3191 return mount_bdev(fs_type, flags, dev_name, data, yaffs2_internal_read_super_mtd);
3193 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3194 static int yaffs2_read_super(struct file_system_type *fs,
3195 int flags, const char *dev_name, void *data,
3196 struct vfsmount *mnt)
3198 return get_sb_bdev(fs, flags, dev_name, data,
3199 yaffs2_internal_read_super_mtd, mnt);
3202 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3203 int flags, const char *dev_name,
3207 return get_sb_bdev(fs, flags, dev_name, data,
3208 yaffs2_internal_read_super_mtd);
3212 static struct file_system_type yaffs2_fs_type = {
3213 .owner = THIS_MODULE,
3215 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3216 .mount = yaffs2_mount,
3218 .get_sb = yaffs2_read_super,
3220 .kill_sb = kill_block_super,
3221 .fs_flags = FS_REQUIRES_DEV,
3224 static struct super_block *yaffs2_read_super(struct super_block *sb,
3225 void *data, int silent)
3227 return yaffs_internal_read_super(2, sb, data, silent);
3230 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3235 static struct proc_dir_entry *my_proc_entry;
3237 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3239 struct yaffs_param *param = &dev->param;
3242 yaffs_count_blocks_by_state(dev,bs);
3244 buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
3245 buf += sprintf(buf, "end_block............ %d\n", param->end_block);
3246 buf += sprintf(buf, "total_bytes_per_chunk %d\n",
3247 param->total_bytes_per_chunk);
3248 buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
3249 buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
3250 buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
3251 buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
3252 buf += sprintf(buf, "empty_lost_n_found... %d\n",
3253 param->empty_lost_n_found);
3254 buf += sprintf(buf, "disable_lazy_load.... %d\n",
3255 param->disable_lazy_load);
3256 buf += sprintf(buf, "disable_bad_block_mrk %d\n",
3257 param->disable_bad_block_marking);
3258 buf += sprintf(buf, "refresh_period....... %d\n",
3259 param->refresh_period);
3260 buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
3261 buf += sprintf(buf, "n_reserved_blocks.... %d\n",
3262 param->n_reserved_blocks);
3263 buf += sprintf(buf, "always_check_erased.. %d\n",
3264 param->always_check_erased);
3265 buf += sprintf(buf, "\n");
3266 buf += sprintf(buf, "block count by state\n");
3267 buf += sprintf(buf, "0:%d 1:%d 2:%d 3:%d 4:%d\n",
3268 bs[0], bs[1], bs[2], bs[3], bs[4]);
3269 buf += sprintf(buf, "5:%d 6:%d 7:%d 8:%d 9:%d\n",
3270 bs[5], bs[6], bs[7], bs[8], bs[9]);
3275 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3277 buf += sprintf(buf, "max file size....... %lld\n",
3278 (long long) yaffs_max_file_size(dev));
3279 buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
3280 dev->data_bytes_per_chunk);
3281 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3282 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3283 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3284 buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
3285 dev->blocks_in_checkpt);
3286 buf += sprintf(buf, "\n");
3287 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3288 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3289 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3290 buf += sprintf(buf, "\n");
3291 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3292 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3293 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3294 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3295 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3296 buf += sprintf(buf, "passive_gc_count..... %u\n",
3297 dev->passive_gc_count);
3298 buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
3299 dev->oldest_dirty_gc_count);
3300 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3301 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3302 buf += sprintf(buf, "n_retried_writes..... %u\n",
3303 dev->n_retried_writes);
3304 buf += sprintf(buf, "n_retired_blocks..... %u\n",
3305 dev->n_retired_blocks);
3306 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3307 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3308 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
3309 dev->n_tags_ecc_fixed);
3310 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
3311 dev->n_tags_ecc_unfixed);
3312 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3313 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3314 buf += sprintf(buf, "n_unlinked_files..... %u\n",
3315 dev->n_unlinked_files);
3316 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3317 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3318 buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
3319 buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
3324 static int yaffs_proc_read(char *page,
3326 off_t offset, int count, int *eof, void *data)
3328 struct list_head *item;
3333 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3334 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3335 * This also assumes the user has posted a read buffer large
3336 * enough to hold the complete output; but that's life in /proc.
3341 /* Print header first */
3344 sprintf(buf, "Multi-version YAFFS\n");
3346 buf += sprintf(buf, "\n");
3350 mutex_lock(&yaffs_context_lock);
3352 /* Locate and print the Nth entry. Order N-squared but N is small. */
3353 list_for_each(item, &yaffs_context_list) {
3354 struct yaffs_linux_context *dc =
3355 list_entry(item, struct yaffs_linux_context,
3357 struct yaffs_dev *dev = dc->dev;
3359 if (n < (step & ~1)) {
3363 if ((step & 1) == 0) {
3365 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3367 buf = yaffs_dump_dev_part0(buf, dev);
3369 buf = yaffs_dump_dev_part1(buf, dev);
3374 mutex_unlock(&yaffs_context_lock);
3377 return buf - page < count ? buf - page : count;
3381 * Set the verbosity of the warnings and error messages.
3383 * Note that the names can only be a..z or _ with the current code.
3388 unsigned mask_bitfield;
3390 {"allocate", YAFFS_TRACE_ALLOCATE},
3391 {"always", YAFFS_TRACE_ALWAYS},
3392 {"background", YAFFS_TRACE_BACKGROUND},
3393 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3394 {"buffers", YAFFS_TRACE_BUFFERS},
3395 {"bug", YAFFS_TRACE_BUG},
3396 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3397 {"deletion", YAFFS_TRACE_DELETION},
3398 {"erase", YAFFS_TRACE_ERASE},
3399 {"error", YAFFS_TRACE_ERROR},
3400 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3401 {"gc", YAFFS_TRACE_GC},
3402 {"lock", YAFFS_TRACE_LOCK},
3403 {"mtd", YAFFS_TRACE_MTD},
3404 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3405 {"os", YAFFS_TRACE_OS},
3406 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3407 {"scan", YAFFS_TRACE_SCAN},
3408 {"mount", YAFFS_TRACE_MOUNT},
3409 {"tracing", YAFFS_TRACE_TRACING},
3410 {"sync", YAFFS_TRACE_SYNC},
3411 {"write", YAFFS_TRACE_WRITE},
3412 {"verify", YAFFS_TRACE_VERIFY},
3413 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3414 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3415 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3416 {"all", 0xffffffff},
3421 #define MAX_MASK_NAME_LENGTH 40
3422 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3423 unsigned long count)
3425 unsigned rg = 0, mask_bitfield;
3429 char substring[MAX_MASK_NAME_LENGTH + 1];
3435 rg = yaffs_trace_mask;
3437 while (!done && (pos < count)) {
3439 while ((pos < count) && isspace(buf[pos]))
3456 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3458 if (end > buf + pos) {
3459 mask_name = "numeral";
3460 len = end - (buf + pos);
3464 for (x = buf + pos, i = 0;
3465 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3466 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3468 substring[i] = '\0';
3470 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3471 if (strcmp(substring, mask_flags[i].mask_name)
3473 mask_name = mask_flags[i].mask_name;
3475 mask_flags[i].mask_bitfield;
3482 if (mask_name != NULL) {
3486 rg &= ~mask_bitfield;
3489 rg |= mask_bitfield;
3495 rg |= mask_bitfield;
3501 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3503 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3505 if (rg & YAFFS_TRACE_ALWAYS) {
3506 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3508 flag = ((rg & mask_flags[i].mask_bitfield) ==
3509 mask_flags[i].mask_bitfield) ? '+' : '-';
3510 printk(KERN_DEBUG "%c%s\n", flag,
3511 mask_flags[i].mask_name);
3518 /* Debug strings are of the form:
3519 * .bnnn print info on block n
3520 * .cobjn,chunkn print nand chunk id for objn:chunkn
3523 static int yaffs_proc_debug_write(struct file *file, const char *buf,
3524 unsigned long count)
3533 struct list_head *item;
3535 memset(str, 0, sizeof(str));
3536 memcpy(str, buf, min((size_t)count, sizeof(str) -1));
3544 while (*p1 && *p1 != ',') {
3550 p0_val = simple_strtol(p0, NULL, 0);
3551 p1_val = simple_strtol(p1, NULL, 0);
3554 mutex_lock(&yaffs_context_lock);
3556 /* Locate and print the Nth entry. Order N-squared but N is small. */
3557 list_for_each(item, &yaffs_context_list) {
3558 struct yaffs_linux_context *dc =
3559 list_entry(item, struct yaffs_linux_context,
3561 struct yaffs_dev *dev = dc->dev;
3564 struct yaffs_block_info *bi;
3566 bi = yaffs_get_block_info(dev,p0_val);
3569 printk("Block %d: state %d, retire %d, use %d, seq %d\n",
3570 (int)p0_val, bi->block_state,
3571 bi->needs_retiring, bi->pages_in_use,
3574 } else if (cmd == 'c') {
3575 struct yaffs_obj *obj;
3578 obj = yaffs_find_by_number(dev, p0_val);
3580 printk("No obj %d\n", (int)p0_val);
3583 nand_chunk = obj->hdr_chunk;
3586 yaffs_find_chunk_in_file(obj,
3588 printk("Nand chunk for %d:%d is %d\n",
3589 (int)p0_val, (int)p1_val, nand_chunk);
3594 mutex_unlock(&yaffs_context_lock);
3600 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0))
3601 static int yaffs_proc_write(struct file *file, const char *buf,
3602 unsigned long count, void *ppos)
3604 static ssize_t yaffs_proc_write(struct file *file, const char __user *buf,
3605 size_t count, loff_t *ppos)
3609 return yaffs_proc_debug_write(file, buf, count);
3610 return yaffs_proc_write_trace_options(file, buf, count);
3613 /* Stuff to handle installation of file systems */
3614 struct file_system_to_install {
3615 struct file_system_type *fst;
3619 static struct file_system_to_install fs_to_install[] = {
3620 {&yaffs_fs_type, 0},
3621 {&yaffs2_fs_type, 0},
3626 #ifdef YAFFS_NEW_PROCFS
3627 static int yaffs_proc_show(struct seq_file *m, void *v)
3629 /* FIXME: Unify in a better way? */
3634 len = yaffs_proc_read(buffer, &start, 0, sizeof(buffer), NULL, NULL);
3635 seq_puts(m, buffer);
3639 static int yaffs_proc_open(struct inode *inode, struct file *file)
3641 return single_open(file, yaffs_proc_show, NULL);
3644 static struct file_operations procfs_ops = {
3645 .owner = THIS_MODULE,
3646 .open = yaffs_proc_open,
3648 .write = yaffs_proc_write,
3651 static int yaffs_procfs_init(void)
3653 /* Install the proc_fs entries */
3654 my_proc_entry = proc_create("yaffs",
3659 if (my_proc_entry) {
3669 static int yaffs_procfs_init(void)
3671 /* Install the proc_fs entries */
3672 my_proc_entry = create_proc_entry("yaffs",
3673 S_IRUGO | S_IFREG, YPROC_ROOT);
3675 if (my_proc_entry) {
3676 my_proc_entry->write_proc = yaffs_proc_write;
3677 my_proc_entry->read_proc = yaffs_proc_read;
3678 my_proc_entry->data = NULL;
3688 static int __init init_yaffs_fs(void)
3691 struct file_system_to_install *fsinst;
3693 yaffs_trace(YAFFS_TRACE_ALWAYS,
3694 "yaffs Installing.");
3696 mutex_init(&yaffs_context_lock);
3698 error = yaffs_procfs_init();
3702 /* Now add the file system entries */
3704 fsinst = fs_to_install;
3706 while (fsinst->fst && !error) {
3707 error = register_filesystem(fsinst->fst);
3709 fsinst->installed = 1;
3713 /* Any errors? uninstall */
3715 fsinst = fs_to_install;
3717 while (fsinst->fst) {
3718 if (fsinst->installed) {
3719 unregister_filesystem(fsinst->fst);
3720 fsinst->installed = 0;
3729 static void __exit exit_yaffs_fs(void)
3732 struct file_system_to_install *fsinst;
3734 yaffs_trace(YAFFS_TRACE_ALWAYS,
3737 remove_proc_entry("yaffs", YPROC_ROOT);
3739 fsinst = fs_to_install;
3741 while (fsinst->fst) {
3742 if (fsinst->installed) {
3743 unregister_filesystem(fsinst->fst);
3744 fsinst->installed = 0;
3750 module_init(init_yaffs_fs)
3751 module_exit(exit_yaffs_fs)
3753 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3754 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
3755 MODULE_LICENSE("GPL");