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>
38 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0))
39 #include <linux/iversion.h>
42 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
43 #define YAFFS_COMPILE_BACKGROUND
44 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23))
45 #define YAFFS_COMPILE_FREEZER
49 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
50 #define YAFFS_COMPILE_EXPORTFS
53 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
54 #define YAFFS_USE_SETATTR_COPY
55 #define YAFFS_USE_TRUNCATE_SETSIZE
57 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
58 #define YAFFS_HAS_EVICT_INODE
61 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13)) && \
62 (LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0))
63 #define YAFFS_NEW_FOLLOW_LINK 1
65 #define YAFFS_NEW_FOLLOW_LINK 0
68 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
69 #define YAFFS_HAS_WRITE_SUPER
73 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
74 #include <linux/config.h>
77 #include <linux/kernel.h>
78 #include <linux/module.h>
79 #include <linux/slab.h>
80 #include <linux/init.h>
82 #include <linux/proc_fs.h>
83 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
84 #include <linux/smp_lock.h>
86 #include <linux/pagemap.h>
87 #include <linux/mtd/mtd.h>
88 #include <linux/interrupt.h>
89 #include <linux/string.h>
90 #include <linux/ctype.h>
92 #if (YAFFS_NEW_FOLLOW_LINK == 1)
93 #include <linux/namei.h>
96 #ifdef YAFFS_COMPILE_EXPORTFS
97 #include <linux/exportfs.h>
100 #ifdef YAFFS_COMPILE_BACKGROUND
101 #include <linux/kthread.h>
102 #include <linux/delay.h>
104 #ifdef YAFFS_COMPILE_FREEZER
105 #include <linux/freezer.h>
108 #include <asm/div64.h>
110 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
112 #include <linux/statfs.h>
114 #define UnlockPage(p) unlock_page(p)
115 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
117 /* FIXME: use sb->s_id instead ? */
118 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
122 #include <linux/locks.h>
123 #define BDEVNAME_SIZE 0
124 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
126 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
127 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
133 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
134 #define YPROC_ROOT (&proc_root)
136 #define YPROC_ROOT NULL
139 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
140 #define Y_INIT_TIMER(a) init_timer(a)
141 #elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
142 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
144 #define Y_INIT_TIMER(a,cb) timer_setup_on_stack(a,cb,0)
147 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
148 #define YAFFS_USE_WRITE_BEGIN_END 1
150 #define YAFFS_USE_WRITE_BEGIN_END 0
153 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
154 #define YAFFS_SUPER_HAS_DIRTY
158 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
159 #define set_nlink(inode, count) do { (inode)->i_nlink = (count); } while(0)
162 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
163 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
165 uint64_t result = partition_size;
166 do_div(result, block_size);
167 return (uint32_t) result;
170 #define YCALCBLOCKS(s, b) ((s)/(b))
173 #include <linux/uaccess.h>
174 #include <linux/mtd/mtd.h>
176 #include "yportenv.h"
177 #include "yaffs_trace.h"
178 #include "yaffs_guts.h"
179 #include "yaffs_attribs.h"
180 #include "yaffs_linux.h"
181 #include "yaffs_mtdif.h"
182 #include "yaffs_packedtags2.h"
183 #include "yaffs_getblockinfo.h"
185 unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS | 0;
186 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
187 unsigned int yaffs_auto_checkpoint = 1;
188 unsigned int yaffs_gc_control = 1;
189 unsigned int yaffs_bg_enable = 1;
190 unsigned int yaffs_auto_select = 1;
191 /* Module Parameters */
192 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
193 module_param(yaffs_trace_mask, uint, 0644);
194 module_param(yaffs_wr_attempts, uint, 0644);
195 module_param(yaffs_auto_checkpoint, uint, 0644);
196 module_param(yaffs_gc_control, uint, 0644);
197 module_param(yaffs_bg_enable, uint, 0644);
199 MODULE_PARM(yaffs_trace_mask, "i");
200 MODULE_PARM(yaffs_wr_attempts, "i");
201 MODULE_PARM(yaffs_auto_checkpoint, "i");
202 MODULE_PARM(yaffs_gc_control, "i");
205 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
206 /* use iget and read_inode */
207 #define Y_IGET(sb, inum) iget((sb), (inum))
210 /* Call local equivalent */
211 #define YAFFS_USE_OWN_IGET
212 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
216 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
217 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
219 #define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
222 #define yaffs_inode_to_obj(iptr) \
223 ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
224 #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
226 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
227 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
229 #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
232 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
233 #define Y_CLEAR_INODE(i) clear_inode(i)
235 #define Y_CLEAR_INODE(i) end_writeback(i)
238 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
239 #define YAFFS_USE_DIR_ITERATE
242 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0))
243 #define YAFFS_USE_XATTR
246 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,12,0))
247 #define YAFFS_NEW_PROCFS
248 #include <linux/seq_file.h>
251 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
252 #define PAGE_CACHE_SIZE PAGE_SIZE
253 #define PAGE_CACHE_SHIFT PAGE_SHIFT
254 #define Y_GET_DENTRY(f) ((f)->f_path.dentry)
255 #define YAFFS_NEW_XATTR 1
256 #define YAFFS_NEW_GET_LINK 1
258 #define Y_GET_DENTRY(f) ((f)->f_dentry)
259 #define YAFFS_NEW_XATTR 0
260 #define YAFFS_NEW_GET_LINK 0
263 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0))
264 #define page_cache_release put_page
267 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4,12,0))
268 #define update_dir_time(dir) do {\
269 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
271 #elif (LINUX_VERSION_CODE < KERNEL_VERSION(4,18,0))
272 #define update_dir_time(dir) do {\
273 (dir)->i_ctime = (dir)->i_mtime = current_kernel_time(); \
276 #define update_dir_time(dir) do {\
277 (dir)->i_ctime = (dir)->i_mtime = current_kernel_time64(); \
281 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0))
282 static inline int setattr_prepare(struct dentry *dentry, struct iattr *attr)
284 return inode_change_ok(dentry->d_inode, attr);
288 static void yaffs_fill_inode_from_obj(struct inode *inode,
289 struct yaffs_obj *obj);
292 static void yaffs_gross_lock(struct yaffs_dev *dev)
294 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
295 mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
296 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
299 static void yaffs_gross_unlock(struct yaffs_dev *dev)
301 yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
302 mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
306 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
308 /* Lifted from jffs2 */
310 struct yaffs_obj *obj;
311 unsigned char *pg_buf;
313 loff_t pos = ((loff_t) pg->index) << PAGE_SHIFT;
314 struct yaffs_dev *dev;
316 yaffs_trace(YAFFS_TRACE_OS,
317 "yaffs_readpage_nolock at %lld, size %08x",
319 (unsigned)PAGE_SIZE);
321 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
325 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
326 BUG_ON(!PageLocked(pg));
333 /* FIXME: Can kmap fail? */
335 yaffs_gross_lock(dev);
337 ret = yaffs_file_rd(obj, pg_buf, pos, PAGE_CACHE_SIZE);
339 yaffs_gross_unlock(dev);
345 ClearPageUptodate(pg);
352 flush_dcache_page(pg);
355 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
359 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
361 int ret = yaffs_readpage_nolock(f, pg);
366 static int yaffs_readpage(struct file *f, struct page *pg)
370 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
371 ret = yaffs_readpage_unlock(f, pg);
372 yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
377 static void yaffs_set_super_dirty_val(struct yaffs_dev *dev, int val)
379 struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
384 # ifdef YAFFS_SUPER_HAS_DIRTY
386 struct super_block *sb = lc->super;
395 static void yaffs_set_super_dirty(struct yaffs_dev *dev)
397 yaffs_set_super_dirty_val(dev, 1);
400 static void yaffs_clear_super_dirty(struct yaffs_dev *dev)
402 yaffs_set_super_dirty_val(dev, 0);
405 static int yaffs_check_super_dirty(struct yaffs_dev *dev)
407 struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
412 # ifdef YAFFS_SUPER_HAS_DIRTY
414 struct super_block *sb = lc->super;
416 if (sb && sb->s_dirt)
424 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
425 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
427 static int yaffs_writepage(struct page *page)
430 struct yaffs_dev *dev;
431 struct address_space *mapping = page->mapping;
433 unsigned long end_index;
435 struct yaffs_obj *obj;
442 inode = mapping->host;
445 i_size = i_size_read(inode);
447 end_index = i_size >> PAGE_CACHE_SHIFT;
449 if (page->index < end_index)
450 n_bytes = PAGE_CACHE_SIZE;
452 n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
454 if (page->index > end_index || !n_bytes) {
455 yaffs_trace(YAFFS_TRACE_OS,
456 "yaffs_writepage at %lld, inode size = %lld!!",
457 ((loff_t)page->index) << PAGE_CACHE_SHIFT,
459 yaffs_trace(YAFFS_TRACE_OS,
462 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
463 set_page_writeback(page);
465 end_page_writeback(page);
470 if (n_bytes != PAGE_CACHE_SIZE)
471 zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
477 obj = yaffs_inode_to_obj(inode);
479 yaffs_gross_lock(dev);
481 yaffs_trace(YAFFS_TRACE_OS,
482 "yaffs_writepage at %lld, size %08x",
483 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes);
484 yaffs_trace(YAFFS_TRACE_OS,
485 "writepag0: obj = %lld, ino = %lld",
486 obj->variant.file_variant.file_size, inode->i_size);
488 n_written = yaffs_wr_file(obj, buffer,
489 ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes, 0);
491 yaffs_set_super_dirty(dev);
493 yaffs_trace(YAFFS_TRACE_OS,
494 "writepag1: obj = %lld, ino = %lld",
495 obj->variant.file_variant.file_size, inode->i_size);
497 yaffs_gross_unlock(dev);
500 set_page_writeback(page);
502 end_page_writeback(page);
505 return (n_written == n_bytes) ? 0 : -ENOSPC;
508 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
509 /* For now we just assume few parallel writes and check against a small number. */
510 /* Todo: need to do this with a counter to handle parallel reads better */
512 static ssize_t yaffs_hold_space(struct file *f)
514 struct yaffs_obj *obj;
515 struct yaffs_dev *dev;
519 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
523 yaffs_gross_lock(dev);
525 n_free_chunks = yaffs_get_n_free_chunks(dev);
527 yaffs_gross_unlock(dev);
529 return (n_free_chunks > 20) ? 1 : 0;
532 static void yaffs_release_space(struct file *f)
534 struct yaffs_obj *obj;
535 struct yaffs_dev *dev;
537 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
541 yaffs_gross_lock(dev);
543 yaffs_gross_unlock(dev);
546 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
547 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
548 loff_t pos, unsigned len, unsigned flags,
549 struct page **pagep, void **fsdata)
551 struct page *pg = NULL;
552 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
558 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
559 pg = grab_cache_page_write_begin(mapping, index, flags);
561 pg = __grab_cache_page(mapping, index);
569 yaffs_trace(YAFFS_TRACE_OS,
570 "start yaffs_write_begin index %d(%x) uptodate %d",
571 (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
574 space_held = yaffs_hold_space(filp);
581 /* Update page if required */
583 if (!Page_Uptodate(pg))
584 ret = yaffs_readpage_nolock(filp, pg);
589 /* Happy path return */
590 yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
595 yaffs_trace(YAFFS_TRACE_OS,
596 "end yaffs_write_begin fail returning %d", ret);
598 yaffs_release_space(filp);
601 page_cache_release(pg);
608 static int yaffs_prepare_write(struct file *f, struct page *pg,
609 unsigned offset, unsigned to)
611 yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
613 if (!Page_Uptodate(pg))
614 return yaffs_readpage_nolock(f, pg);
620 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
623 struct yaffs_obj *obj;
627 struct yaffs_dev *dev;
629 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
632 yaffs_trace(YAFFS_TRACE_OS,
633 "yaffs_file_write: hey obj is null!");
639 yaffs_gross_lock(dev);
641 inode = Y_GET_DENTRY(f)->d_inode;
643 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
644 ipos = inode->i_size;
648 yaffs_trace(YAFFS_TRACE_OS,
649 "yaffs_file_write about to write writing %u(%x) bytes to object %d at %lld",
650 (unsigned)n, (unsigned)n, obj->obj_id, ipos);
652 n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
654 yaffs_set_super_dirty(dev);
656 yaffs_trace(YAFFS_TRACE_OS,
657 "yaffs_file_write: %d(%x) bytes written",
658 (unsigned)n, (unsigned)n);
663 if (ipos > inode->i_size) {
664 inode->i_size = ipos;
665 inode->i_blocks = (ipos + 511) >> 9;
667 yaffs_trace(YAFFS_TRACE_OS,
668 "yaffs_file_write size updated to %lld bytes, %d blocks",
669 ipos, (int)(inode->i_blocks));
673 yaffs_gross_unlock(dev);
674 return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
678 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
679 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
680 loff_t pos, unsigned len, unsigned copied,
681 struct page *pg, void *fsdadata)
685 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
688 addr = kva + offset_into_page;
690 yaffs_trace(YAFFS_TRACE_OS,
691 "yaffs_write_end addr %p pos %lld n_bytes %d",
694 ret = yaffs_file_write(filp, addr, copied, &pos);
697 yaffs_trace(YAFFS_TRACE_OS,
698 "yaffs_write_end not same size ret %d copied %d",
705 yaffs_release_space(filp);
707 page_cache_release(pg);
712 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
717 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
718 int n_bytes = to - offset;
724 yaffs_trace(YAFFS_TRACE_OS,
725 "yaffs_commit_write addr %p pos %lld n_bytes %d",
728 n_written = yaffs_file_write(f, addr, n_bytes, &pos);
730 if (n_written != n_bytes) {
731 yaffs_trace(YAFFS_TRACE_OS,
732 "yaffs_commit_write not same size n_written %d n_bytes %d",
738 yaffs_trace(YAFFS_TRACE_OS,
739 "yaffs_commit_write returning %d",
740 n_written == n_bytes ? 0 : n_written);
742 return n_written == n_bytes ? 0 : n_written;
746 static struct address_space_operations yaffs_file_address_operations = {
747 .readpage = yaffs_readpage,
748 .writepage = yaffs_writepage,
749 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
750 .write_begin = yaffs_write_begin,
751 .write_end = yaffs_write_end,
753 .prepare_write = yaffs_prepare_write,
754 .commit_write = yaffs_commit_write,
759 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
760 static int yaffs_file_flush(struct file *file, fl_owner_t id)
762 static int yaffs_file_flush(struct file *file)
765 struct yaffs_obj *obj = yaffs_dentry_to_obj(Y_GET_DENTRY(file));
767 struct yaffs_dev *dev = obj->my_dev;
769 yaffs_trace(YAFFS_TRACE_OS,
770 "yaffs_file_flush object %d (%s)",
772 obj->dirty ? "dirty" : "clean");
774 yaffs_gross_lock(dev);
776 yaffs_flush_file(obj, 1, 0, 0);
778 yaffs_gross_unlock(dev);
784 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
785 static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync)
786 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
787 static int yaffs_sync_object(struct file *file, int datasync)
789 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
793 struct yaffs_obj *obj;
794 struct yaffs_dev *dev;
795 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
796 struct dentry *dentry = file->f_path.dentry;
799 obj = yaffs_dentry_to_obj(dentry);
803 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
804 "yaffs_sync_object");
805 yaffs_gross_lock(dev);
806 yaffs_flush_file(obj, 1, datasync, 0);
807 yaffs_gross_unlock(dev);
812 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
813 static const struct file_operations yaffs_file_operations = {
814 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
815 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
816 .read = new_sync_read,
817 .write = new_sync_write,
819 .read_iter = generic_file_read_iter,
820 .write_iter = generic_file_write_iter,
822 .read = do_sync_read,
823 .write = do_sync_write,
824 .aio_read = generic_file_aio_read,
825 .aio_write = generic_file_aio_write,
827 .mmap = generic_file_mmap,
828 .flush = yaffs_file_flush,
829 .fsync = yaffs_sync_object,
830 .splice_read = generic_file_splice_read,
831 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
832 .splice_write = iter_file_splice_write,
834 .splice_write = generic_file_splice_write,
836 .llseek = generic_file_llseek,
839 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
841 static const struct file_operations yaffs_file_operations = {
842 .read = do_sync_read,
843 .write = do_sync_write,
844 .aio_read = generic_file_aio_read,
845 .aio_write = generic_file_aio_write,
846 .mmap = generic_file_mmap,
847 .flush = yaffs_file_flush,
848 .fsync = yaffs_sync_object,
849 .sendfile = generic_file_sendfile,
854 static const struct file_operations yaffs_file_operations = {
855 .read = generic_file_read,
856 .write = generic_file_write,
857 .mmap = generic_file_mmap,
858 .flush = yaffs_file_flush,
859 .fsync = yaffs_sync_object,
860 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
861 .sendfile = generic_file_sendfile,
869 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
870 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
872 void *kaddr = kmap_atomic(page, KM_USER0);
873 memset(kaddr + start, 0, end - start);
874 kunmap_atomic(kaddr, KM_USER0);
875 flush_dcache_page(page);
880 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
882 #ifdef YAFFS_USE_TRUNCATE_SETSIZE
883 truncate_setsize(inode, newsize);
886 truncate_inode_pages(&inode->i_data, newsize);
893 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
895 #ifdef YAFFS_USE_SETATTR_COPY
896 setattr_copy(inode, attr);
899 return inode_setattr(inode, attr);
904 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
906 struct inode *inode = dentry->d_inode;
908 struct yaffs_dev *dev;
910 yaffs_trace(YAFFS_TRACE_OS,
911 "yaffs_setattr of object %d",
912 yaffs_inode_to_obj(inode)->obj_id);
914 /* Fail if a requested resize >= 2GB */
915 if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
920 error = setattr_prepare(dentry, attr);
924 error = yaffs_vfs_setattr(inode, attr);
925 yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
926 if (attr->ia_valid & ATTR_SIZE) {
927 yaffs_vfs_setsize(inode, attr->ia_size);
928 inode->i_blocks = (inode->i_size + 511) >> 9;
931 dev = yaffs_inode_to_obj(inode)->my_dev;
932 if (attr->ia_valid & ATTR_SIZE) {
933 yaffs_trace(YAFFS_TRACE_OS,
935 (int)(attr->ia_size),
936 (int)(attr->ia_size));
938 yaffs_gross_lock(dev);
939 result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
940 if (result == YAFFS_OK) {
945 yaffs_gross_unlock(dev);
949 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
954 #ifdef YAFFS_USE_XATTR
955 #if (YAFFS_NEW_XATTR > 0)
956 static int yaffs_setxattr(struct dentry *dentry, struct inode *inode,
957 const char *name, const void *value, size_t size, int flags)
960 static int yaffs_setxattr(struct dentry *dentry, const char *name,
961 const void *value, size_t size, int flags)
963 struct inode *inode = dentry->d_inode;
966 struct yaffs_dev *dev;
967 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
969 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
974 yaffs_gross_lock(dev);
975 result = yaffs_set_xattrib(obj, name, value, size, flags);
976 if (result == YAFFS_OK)
980 yaffs_gross_unlock(dev);
983 yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
988 #if (YAFFS_NEW_XATTR > 0)
989 static ssize_t yaffs_getxattr(struct dentry * dentry, struct inode *inode,
990 const char *name, void *buff, size_t size)
993 static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
994 void *buff, size_t size)
996 struct inode *inode = dentry->d_inode;
999 struct yaffs_dev *dev;
1000 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1002 yaffs_trace(YAFFS_TRACE_OS,
1003 "yaffs_getxattr \"%s\" from object %d",
1008 yaffs_gross_lock(dev);
1009 error = yaffs_get_xattrib(obj, name, buff, size);
1010 yaffs_gross_unlock(dev);
1013 yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
1018 static int yaffs_removexattr(struct dentry *dentry, const char *name)
1020 struct inode *inode = dentry->d_inode;
1022 struct yaffs_dev *dev;
1023 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1025 yaffs_trace(YAFFS_TRACE_OS,
1026 "yaffs_removexattr of object %d", obj->obj_id);
1031 yaffs_gross_lock(dev);
1032 result = yaffs_remove_xattrib(obj, name);
1033 if (result == YAFFS_OK)
1035 else if (result < 0)
1037 yaffs_gross_unlock(dev);
1040 yaffs_trace(YAFFS_TRACE_OS,
1041 "yaffs_removexattr done returning %d", error);
1047 static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
1049 struct inode *inode = dentry->d_inode;
1051 struct yaffs_dev *dev;
1052 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
1054 yaffs_trace(YAFFS_TRACE_OS,
1055 "yaffs_listxattr of object %d", obj->obj_id);
1059 yaffs_gross_lock(dev);
1060 error = yaffs_list_xattrib(obj, buff, size);
1061 yaffs_gross_unlock(dev);
1064 yaffs_trace(YAFFS_TRACE_OS,
1065 "yaffs_listxattr done returning %d", error);
1071 static const struct inode_operations yaffs_file_inode_operations = {
1072 .setattr = yaffs_setattr,
1073 #ifdef YAFFS_USE_XATTR
1074 .setxattr = yaffs_setxattr,
1075 .getxattr = yaffs_getxattr,
1076 .removexattr = yaffs_removexattr,
1078 .listxattr = yaffs_listxattr,
1082 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1085 unsigned char *alias;
1088 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1090 yaffs_gross_lock(dev);
1092 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1094 yaffs_gross_unlock(dev);
1099 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0)
1100 ret = vfs_readlink(dentry, buffer, buflen, alias);
1102 ret = readlink_copy(buffer, buflen, alias);
1108 #if (YAFFS_NEW_GET_LINK == 0)
1109 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1110 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1114 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1118 unsigned char *alias;
1120 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
1122 yaffs_gross_lock(dev);
1124 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1125 yaffs_gross_unlock(dev);
1131 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1132 nd_set_link(nd, alias);
1136 ret = ERR_PTR(ret_int);
1139 ret = vfs_follow_link(nd, alias);
1148 static const char *yaffs_get_link(struct dentry *dentry, struct inode *inode, struct delayed_call *done)
1150 unsigned char *alias;
1151 struct yaffs_dev *dev;
1154 return ERR_PTR(-ECHILD);
1156 dev = yaffs_dentry_to_obj(dentry)->my_dev;
1158 yaffs_gross_lock(dev);
1160 alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
1161 yaffs_gross_unlock(dev);
1164 return ERR_PTR(-ENOMEM);
1165 set_delayed_call(done, kfree_link, alias);
1170 #ifdef YAFFS_HAS_PUT_INODE
1172 /* For now put inode is just for debugging
1173 * Put inode is called when the inode **structure** is put.
1175 static void yaffs_put_inode(struct inode *inode)
1177 yaffs_trace(YAFFS_TRACE_OS,
1178 "yaffs_put_inode: ino %d, count %d"),
1179 (int)inode->i_ino, atomic_read(&inode->i_count);
1184 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1185 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
1191 static const struct inode_operations yaffs_symlink_inode_operations = {
1192 .readlink = yaffs_readlink,
1193 #if (YAFFS_NEW_GET_LINK == 1)
1194 .get_link = yaffs_get_link,
1196 .follow_link = yaffs_follow_link,
1198 #if (YAFFS_NEW_FOLLOW_LINK == 1)
1199 .put_link = yaffs_put_link,
1201 .setattr = yaffs_setattr,
1202 #ifdef YAFFS_USE_XATTR
1203 .setxattr = yaffs_setxattr,
1204 .getxattr = yaffs_getxattr,
1205 .removexattr = yaffs_removexattr,
1207 .listxattr = yaffs_listxattr,
1210 #ifdef YAFFS_USE_OWN_IGET
1212 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1214 struct inode *inode;
1215 struct yaffs_obj *obj;
1216 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
1218 yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
1220 inode = iget_locked(sb, ino);
1222 return ERR_PTR(-ENOMEM);
1223 if (!(inode->i_state & I_NEW))
1226 /* NB This is called as a side effect of other functions, but
1227 * we had to release the lock to prevent deadlocks, so
1228 * need to lock again.
1231 yaffs_gross_lock(dev);
1233 obj = yaffs_find_by_number(dev, inode->i_ino);
1235 yaffs_fill_inode_from_obj(inode, obj);
1237 yaffs_gross_unlock(dev);
1239 unlock_new_inode(inode);
1245 static void yaffs_read_inode(struct inode *inode)
1247 /* NB This is called as a side effect of other functions, but
1248 * we had to release the lock to prevent deadlocks, so
1249 * need to lock again.
1252 struct yaffs_obj *obj;
1253 struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
1255 yaffs_trace(YAFFS_TRACE_OS,
1256 "yaffs_read_inode for %d", (int)inode->i_ino);
1258 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1259 yaffs_gross_lock(dev);
1261 obj = yaffs_find_by_number(dev, inode->i_ino);
1263 yaffs_fill_inode_from_obj(inode, obj);
1265 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1266 yaffs_gross_unlock(dev);
1273 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1274 struct yaffs_obj *obj)
1276 struct inode *inode;
1279 yaffs_trace(YAFFS_TRACE_OS,
1280 "yaffs_get_inode for NULL super_block!!");
1286 yaffs_trace(YAFFS_TRACE_OS,
1287 "yaffs_get_inode for NULL object!!");
1292 yaffs_trace(YAFFS_TRACE_OS,
1293 "yaffs_get_inode for object %d", obj->obj_id);
1295 inode = Y_IGET(sb, obj->obj_id);
1299 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1300 /* iget also increments the inode's i_count */
1301 /* NB You can't be holding gross_lock or deadlock will happen! */
1308 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1311 #define YCRED(x) (x->cred)
1314 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0)
1315 #define YPROC_uid(p) (YCRED(p)->fsuid)
1316 #define YPROC_gid(p) (YCRED(p)->fsgid)
1317 #define EXTRACT_gid(x) x
1318 #define EXTRACT_uid(x) x
1319 #define MAKE_gid(x) x
1320 #define MAKE_uid(x) x
1322 #define YPROC_uid(p) from_kuid(&init_user_ns, YCRED(p)->fsuid)
1323 #define YPROC_gid(p) from_kgid(&init_user_ns, YCRED(p)->fsgid)
1324 #define EXTRACT_gid(x) from_kgid(&init_user_ns, x)
1325 #define EXTRACT_uid(x) from_kuid(&init_user_ns, x)
1326 #define MAKE_gid(x) make_kgid(&init_user_ns, x)
1327 #define MAKE_uid(x) make_kuid(&init_user_ns, x)
1331 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1332 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1334 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1335 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1338 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1342 struct inode *inode;
1344 struct yaffs_obj *obj = NULL;
1345 struct yaffs_dev *dev;
1347 struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
1349 int error = -ENOSPC;
1350 uid_t uid = YPROC_uid(current);
1352 (dir->i_mode & S_ISGID) ? EXTRACT_gid(dir->i_gid) : YPROC_gid(current);
1354 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1358 yaffs_trace(YAFFS_TRACE_OS,
1359 "yaffs_mknod: parent object %d type %d",
1360 parent->obj_id, parent->variant_type);
1362 yaffs_trace(YAFFS_TRACE_OS,
1363 "yaffs_mknod: could not get parent object");
1367 yaffs_trace(YAFFS_TRACE_OS,
1368 "yaffs_mknod: making oject for %s, mode %x dev %x",
1369 dentry->d_name.name, mode, rdev);
1371 dev = parent->my_dev;
1373 yaffs_gross_lock(dev);
1375 switch (mode & S_IFMT) {
1377 /* Special (socket, fifo, device...) */
1378 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
1379 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1381 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1382 gid, old_encode_dev(rdev));
1385 yaffs_create_special(parent, dentry->d_name.name, mode, uid,
1389 case S_IFREG: /* file */
1390 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
1391 obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
1394 case S_IFDIR: /* directory */
1395 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
1396 obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
1399 case S_IFLNK: /* symlink */
1400 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
1401 obj = NULL; /* Do we ever get here? */
1405 /* Can not call yaffs_get_inode() with gross lock held */
1406 yaffs_gross_unlock(dev);
1409 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1410 d_instantiate(dentry, inode);
1411 update_dir_time(dir);
1412 yaffs_trace(YAFFS_TRACE_OS,
1413 "yaffs_mknod created object %d count = %d",
1414 obj->obj_id, atomic_read(&inode->i_count));
1416 yaffs_fill_inode_from_obj(dir, parent);
1418 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
1425 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1426 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1428 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1432 yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
1433 ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1438 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
1439 static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1441 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
1442 static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1443 struct nameidata *n)
1444 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1445 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1446 struct nameidata *n)
1448 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1451 yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
1452 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1455 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
1456 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1458 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1459 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1460 struct nameidata *n)
1462 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1465 struct yaffs_obj *obj;
1466 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1468 struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
1470 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1471 yaffs_gross_lock(dev);
1473 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
1474 yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
1476 obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
1478 obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
1480 /* Can't hold gross lock when calling yaffs_get_inode() */
1481 if (current != yaffs_dev_to_lc(dev)->readdir_process)
1482 yaffs_gross_unlock(dev);
1485 yaffs_trace(YAFFS_TRACE_OS,
1486 "yaffs_lookup found %d", obj->obj_id);
1488 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1490 yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
1494 /* added NCB for 2.5/6 compatability - forces add even if inode is
1495 * NULL which creates dentry hash */
1496 d_add(dentry, inode);
1504 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1505 struct dentry *dentry)
1507 struct inode *inode = old_dentry->d_inode;
1508 struct yaffs_obj *obj = NULL;
1509 struct yaffs_obj *link = NULL;
1510 struct yaffs_dev *dev;
1512 yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
1514 obj = yaffs_inode_to_obj(inode);
1517 yaffs_gross_lock(dev);
1519 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1521 yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
1525 set_nlink(old_dentry->d_inode, yaffs_get_obj_link_count(obj));
1526 d_instantiate(dentry, old_dentry->d_inode);
1527 atomic_inc(&old_dentry->d_inode->i_count);
1528 yaffs_trace(YAFFS_TRACE_OS,
1529 "yaffs_link link count %d i_count %d",
1530 old_dentry->d_inode->i_nlink,
1531 atomic_read(&old_dentry->d_inode->i_count));
1534 yaffs_gross_unlock(dev);
1537 update_dir_time(dir);
1544 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1545 const char *symname)
1547 struct yaffs_obj *obj;
1548 struct yaffs_dev *dev;
1549 uid_t uid = YPROC_uid(current);
1551 (dir->i_mode & S_ISGID) ? EXTRACT_gid(dir->i_gid) : YPROC_gid(current);
1553 yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
1555 if (strnlen(dentry->d_name.name, YAFFS_MAX_NAME_LENGTH + 1) >
1556 YAFFS_MAX_NAME_LENGTH)
1557 return -ENAMETOOLONG;
1559 if (strnlen(symname, YAFFS_MAX_ALIAS_LENGTH + 1) >
1560 YAFFS_MAX_ALIAS_LENGTH)
1561 return -ENAMETOOLONG;
1563 dev = yaffs_inode_to_obj(dir)->my_dev;
1564 yaffs_gross_lock(dev);
1565 obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
1566 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1567 yaffs_gross_unlock(dev);
1570 struct inode *inode;
1572 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1573 d_instantiate(dentry, inode);
1574 update_dir_time(dir);
1575 yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
1578 yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
1585 * The VFS layer already does all the dentry stuff for rename.
1587 * NB: POSIX says you can rename an object over an old object of the same name
1589 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0))
1590 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1591 struct inode *new_dir, struct dentry *new_dentry, unsigned int unused)
1593 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1594 struct inode *new_dir, struct dentry *new_dentry)
1597 struct yaffs_dev *dev;
1598 int ret_val = YAFFS_FAIL;
1599 struct yaffs_obj *target;
1601 yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
1602 dev = yaffs_inode_to_obj(old_dir)->my_dev;
1604 yaffs_gross_lock(dev);
1606 /* Check if the target is an existing directory that is not empty. */
1607 target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
1608 new_dentry->d_name.name);
1610 if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1611 !list_empty(&target->variant.dir_variant.children)) {
1613 yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
1615 ret_val = YAFFS_FAIL;
1617 /* Now does unlinking internally using shadowing mechanism */
1618 yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
1620 ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
1621 old_dentry->d_name.name,
1622 yaffs_inode_to_obj(new_dir),
1623 new_dentry->d_name.name);
1625 yaffs_gross_unlock(dev);
1627 if (ret_val == YAFFS_OK) {
1629 inode_dec_link_count(new_dentry->d_inode);
1631 update_dir_time(old_dir);
1632 if (old_dir != new_dir)
1633 update_dir_time(new_dir);
1643 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1647 struct yaffs_dev *dev;
1648 struct yaffs_obj *obj;
1650 yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
1651 (int)(dir->i_ino), dentry->d_name.name);
1652 obj = yaffs_inode_to_obj(dir);
1655 yaffs_gross_lock(dev);
1657 ret_val = yaffs_unlinker(obj, dentry->d_name.name);
1659 if (ret_val == YAFFS_OK) {
1660 inode_dec_link_count(dentry->d_inode);
1661 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0))
1662 inode_inc_iversion(dir);
1666 yaffs_gross_unlock(dev);
1667 update_dir_time(dir);
1670 yaffs_gross_unlock(dev);
1676 static const struct inode_operations yaffs_dir_inode_operations = {
1677 .create = yaffs_create,
1678 .lookup = yaffs_lookup,
1680 .unlink = yaffs_unlink,
1681 .symlink = yaffs_symlink,
1682 .mkdir = yaffs_mkdir,
1683 .rmdir = yaffs_unlink,
1684 .mknod = yaffs_mknod,
1685 .rename = yaffs_rename,
1686 .setattr = yaffs_setattr,
1687 .listxattr = yaffs_listxattr,
1688 #ifdef YAFFS_USE_XATTR
1689 .setxattr = yaffs_setxattr,
1690 .getxattr = yaffs_getxattr,
1691 .removexattr = yaffs_removexattr,
1695 /*-----------------------------------------------------------------*/
1696 /* Directory search context allows us to unlock access to yaffs during
1697 * filldir without causing problems with the directory being modified.
1698 * This is similar to the tried and tested mechanism used in yaffs direct.
1700 * A search context iterates along a doubly linked list of siblings in the
1701 * directory. If the iterating object is deleted then this would corrupt
1702 * the list iteration, likely causing a crash. The search context avoids
1703 * this by using the remove_obj_fn to move the search context to the
1704 * next object before the object is deleted.
1706 * Many readdirs (and thus seach conexts) may be alive simulateously so
1707 * each struct yaffs_dev has a list of these.
1709 * A seach context lives for the duration of a readdir.
1711 * All these functions must be called while yaffs is locked.
1714 struct yaffs_search_context {
1715 struct yaffs_dev *dev;
1716 struct yaffs_obj *dir_obj;
1717 struct yaffs_obj *next_return;
1718 struct list_head others;
1722 * yaffs_new_search() creates a new search context, initialises it and
1723 * adds it to the device's search context list.
1725 * Called at start of readdir.
1727 static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
1729 struct yaffs_dev *dev = dir->my_dev;
1730 struct yaffs_search_context *sc =
1731 kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
1735 if (list_empty(&sc->dir_obj->variant.dir_variant.children))
1736 sc->next_return = NULL;
1739 list_entry(dir->variant.dir_variant.children.next,
1740 struct yaffs_obj, siblings);
1741 INIT_LIST_HEAD(&sc->others);
1742 list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
1748 * yaffs_search_end() disposes of a search context and cleans up.
1750 static void yaffs_search_end(struct yaffs_search_context *sc)
1753 list_del(&sc->others);
1759 * yaffs_search_advance() moves a search context to the next object.
1760 * Called when the search iterates or when an object removal causes
1761 * the search context to be moved to the next object.
1763 static void yaffs_search_advance(struct yaffs_search_context *sc)
1768 if (sc->next_return == NULL ||
1769 list_empty(&sc->dir_obj->variant.dir_variant.children))
1770 sc->next_return = NULL;
1772 struct list_head *next = sc->next_return->siblings.next;
1774 if (next == &sc->dir_obj->variant.dir_variant.children)
1775 sc->next_return = NULL; /* end of list */
1778 list_entry(next, struct yaffs_obj, siblings);
1783 * yaffs_remove_obj_callback() is called when an object is unlinked.
1784 * We check open search contexts and advance any which are currently
1785 * on the object being iterated.
1787 static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
1790 struct list_head *i;
1791 struct yaffs_search_context *sc;
1792 struct list_head *search_contexts =
1793 &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
1795 /* Iterate through the directory search contexts.
1796 * If any are currently on the object being removed, then advance
1797 * the search context to the next object to prevent a hanging pointer.
1799 list_for_each(i, search_contexts) {
1800 sc = list_entry(i, struct yaffs_search_context, others);
1801 if (sc->next_return == obj)
1802 yaffs_search_advance(sc);
1808 /*-----------------------------------------------------------------*/
1810 #ifdef YAFFS_USE_DIR_ITERATE
1811 static int yaffs_iterate(struct file *f, struct dir_context *dc)
1813 struct yaffs_obj *obj;
1814 struct yaffs_dev *dev;
1815 struct yaffs_search_context *sc;
1816 unsigned long curoffs;
1817 struct yaffs_obj *l;
1820 char name[YAFFS_MAX_NAME_LENGTH + 1];
1822 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
1825 yaffs_gross_lock(dev);
1827 yaffs_dev_to_lc(dev)->readdir_process = current;
1829 sc = yaffs_new_search(obj);
1835 if (!dir_emit_dots(f, dc))
1840 while (sc->next_return) {
1842 l = sc->next_return;
1843 if (curoffs >= dc->pos) {
1844 int this_inode = yaffs_get_obj_inode(l);
1845 int this_type = yaffs_get_obj_type(l);
1847 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1848 yaffs_trace(YAFFS_TRACE_OS,
1849 "yaffs_readdir: %s inode %d",
1850 name, yaffs_get_obj_inode(l));
1852 yaffs_gross_unlock(dev);
1859 yaffs_gross_lock(dev);
1863 yaffs_gross_lock(dev);
1868 yaffs_search_advance(sc);
1872 yaffs_search_end(sc);
1873 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1874 yaffs_gross_unlock(dev);
1881 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1883 struct yaffs_obj *obj;
1884 struct yaffs_dev *dev;
1885 struct yaffs_search_context *sc;
1886 struct inode *inode = Y_GET_DENTRY(f)->d_inode;
1887 unsigned long offset, curoffs;
1888 struct yaffs_obj *l;
1891 char name[YAFFS_MAX_NAME_LENGTH + 1];
1893 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
1896 yaffs_gross_lock(dev);
1898 yaffs_dev_to_lc(dev)->readdir_process = current;
1902 sc = yaffs_new_search(obj);
1908 yaffs_trace(YAFFS_TRACE_OS,
1909 "yaffs_readdir: starting at %d", (int)offset);
1912 yaffs_trace(YAFFS_TRACE_OS,
1913 "yaffs_readdir: entry . ino %d",
1915 yaffs_gross_unlock(dev);
1916 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1917 yaffs_gross_lock(dev);
1920 yaffs_gross_lock(dev);
1925 yaffs_trace(YAFFS_TRACE_OS,
1926 "yaffs_readdir: entry .. ino %d",
1927 (int)f->f_dentry->d_parent->d_inode->i_ino);
1928 yaffs_gross_unlock(dev);
1929 if (filldir(dirent, "..", 2, offset,
1930 f->f_dentry->d_parent->d_inode->i_ino,
1932 yaffs_gross_lock(dev);
1935 yaffs_gross_lock(dev);
1942 /* If the directory has changed since the open or last call to
1943 readdir, rewind to after the 2 canned entries. */
1944 if (f->f_version != inode->i_version) {
1947 f->f_version = inode->i_version;
1950 while (sc->next_return) {
1952 l = sc->next_return;
1953 if (curoffs >= offset) {
1954 int this_inode = yaffs_get_obj_inode(l);
1955 int this_type = yaffs_get_obj_type(l);
1957 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1958 yaffs_trace(YAFFS_TRACE_OS,
1959 "yaffs_readdir: %s inode %d",
1960 name, yaffs_get_obj_inode(l));
1962 yaffs_gross_unlock(dev);
1967 offset, this_inode, this_type) < 0) {
1968 yaffs_gross_lock(dev);
1972 yaffs_gross_lock(dev);
1977 yaffs_search_advance(sc);
1981 yaffs_search_end(sc);
1982 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1983 yaffs_gross_unlock(dev);
1990 static const struct file_operations yaffs_dir_operations = {
1991 .read = generic_read_dir,
1992 #ifdef YAFFS_USE_DIR_ITERATE
1993 .iterate = yaffs_iterate,
1995 .readdir = yaffs_readdir,
1997 .fsync = yaffs_sync_object,
1998 .llseek = generic_file_llseek,
2001 static void yaffs_fill_inode_from_obj(struct inode *inode,
2002 struct yaffs_obj *obj)
2006 /* Check mode against the variant type and attempt to repair if broken. */
2007 u32 mode = obj->yst_mode;
2008 switch (obj->variant_type) {
2009 case YAFFS_OBJECT_TYPE_FILE:
2010 if (!S_ISREG(mode)) {
2011 obj->yst_mode &= ~S_IFMT;
2012 obj->yst_mode |= S_IFREG;
2016 case YAFFS_OBJECT_TYPE_SYMLINK:
2017 if (!S_ISLNK(mode)) {
2018 obj->yst_mode &= ~S_IFMT;
2019 obj->yst_mode |= S_IFLNK;
2023 case YAFFS_OBJECT_TYPE_DIRECTORY:
2024 if (!S_ISDIR(mode)) {
2025 obj->yst_mode &= ~S_IFMT;
2026 obj->yst_mode |= S_IFDIR;
2030 case YAFFS_OBJECT_TYPE_UNKNOWN:
2031 case YAFFS_OBJECT_TYPE_HARDLINK:
2032 case YAFFS_OBJECT_TYPE_SPECIAL:
2038 inode->i_flags |= S_NOATIME;
2040 inode->i_ino = obj->obj_id;
2041 inode->i_mode = obj->yst_mode;
2042 inode->i_uid = MAKE_uid(obj->yst_uid);
2043 inode->i_gid = MAKE_gid(obj->yst_gid);
2044 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
2045 inode->i_blksize = inode->i_sb->s_blocksize;
2047 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2049 inode->i_rdev = old_decode_dev(obj->yst_rdev);
2050 inode->i_atime.tv_sec = (YTIME_T) (obj->yst_atime);
2051 inode->i_atime.tv_nsec = 0;
2052 inode->i_mtime.tv_sec = (YTIME_T) obj->yst_mtime;
2053 inode->i_mtime.tv_nsec = 0;
2054 inode->i_ctime.tv_sec = (YTIME_T) obj->yst_ctime;
2055 inode->i_ctime.tv_nsec = 0;
2057 inode->i_rdev = obj->yst_rdev;
2058 inode->i_atime = obj->yst_atime;
2059 inode->i_mtime = obj->yst_mtime;
2060 inode->i_ctime = obj->yst_ctime;
2062 inode->i_size = yaffs_get_obj_length(obj);
2063 inode->i_blocks = (inode->i_size + 511) >> 9;
2065 set_nlink(inode, yaffs_get_obj_link_count(obj));
2067 yaffs_trace(YAFFS_TRACE_OS,
2068 "yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
2069 inode->i_mode, obj->yst_uid, obj->yst_gid,
2070 inode->i_size, atomic_read(&inode->i_count));
2072 switch (obj->yst_mode & S_IFMT) {
2073 default: /* fifo, device or socket */
2074 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2075 init_special_inode(inode, obj->yst_mode,
2076 old_decode_dev(obj->yst_rdev));
2078 init_special_inode(inode, obj->yst_mode,
2079 (dev_t) (obj->yst_rdev));
2082 case S_IFREG: /* file */
2083 inode->i_op = &yaffs_file_inode_operations;
2084 inode->i_fop = &yaffs_file_operations;
2085 inode->i_mapping->a_ops =
2086 &yaffs_file_address_operations;
2088 case S_IFDIR: /* directory */
2089 inode->i_op = &yaffs_dir_inode_operations;
2090 inode->i_fop = &yaffs_dir_operations;
2092 case S_IFLNK: /* symlink */
2093 inode->i_op = &yaffs_symlink_inode_operations;
2097 yaffs_inode_to_obj_lv(inode) = obj;
2099 obj->my_inode = inode;
2102 yaffs_trace(YAFFS_TRACE_OS,
2103 "yaffs_fill_inode invalid parameters");
2111 * yaffs background thread functions .
2112 * yaffs_bg_thread_fn() the thread function
2113 * yaffs_bg_start() launches the background thread.
2114 * yaffs_bg_stop() cleans up the background thread.
2117 * The thread should only run after the yaffs is initialised
2118 * The thread should be stopped before yaffs is unmounted.
2119 * The thread should not do any writing while the fs is in read only.
2122 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2124 unsigned erased_chunks =
2125 dev->n_erased_blocks * dev->param.chunks_per_block;
2126 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2127 unsigned scattered = 0; /* Free chunks not in an erased block */
2129 if (erased_chunks < dev->n_free_chunks)
2130 scattered = (dev->n_free_chunks - erased_chunks);
2132 if (!context->bg_running)
2134 else if (scattered < (dev->param.chunks_per_block * 2))
2136 else if (erased_chunks > dev->n_free_chunks / 2)
2138 else if (erased_chunks > dev->n_free_chunks / 4)
2144 #ifdef YAFFS_COMPILE_BACKGROUND
2146 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2147 struct timer_struct {
2148 struct task_struct *task;
2149 struct timer_list timer;
2153 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2154 static void yaffs_background_waker(struct timer_list *t)
2156 struct timer_struct *ts = from_timer(ts, t, timer);
2158 wake_up_process(ts->task);
2161 void yaffs_background_waker(unsigned long data)
2163 wake_up_process((struct task_struct *)data);
2167 static int yaffs_bg_thread_fn(void *data)
2169 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2170 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2171 unsigned long now = jiffies;
2172 unsigned long next_dir_update = now;
2173 unsigned long next_gc = now;
2174 unsigned long expires;
2175 unsigned int urgency;
2178 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2179 struct timer_struct timer;
2181 struct timer_list timer;
2184 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2185 "yaffs_background starting for dev %p", (void *)dev);
2187 #ifdef YAFFS_COMPILE_FREEZER
2190 while (context->bg_running) {
2191 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
2193 if (kthread_should_stop())
2196 #ifdef YAFFS_COMPILE_FREEZER
2197 if (try_to_freeze())
2200 yaffs_gross_lock(dev);
2204 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
2205 yaffs_update_dirty_dirs(dev);
2206 next_dir_update = now + HZ;
2209 if (time_after(now, next_gc) && yaffs_bg_enable) {
2210 if (!dev->is_checkpointed) {
2211 urgency = yaffs_bg_gc_urgency(dev);
2212 gc_result = yaffs_bg_gc(dev, urgency);
2214 next_gc = now + HZ / 20 + 1;
2215 else if (urgency > 0)
2216 next_gc = now + HZ / 10 + 1;
2218 next_gc = now + HZ * 2;
2221 * gc not running so set to next_dir_update
2222 * to cut down on wake ups
2224 next_gc = next_dir_update;
2227 yaffs_gross_unlock(dev);
2228 expires = next_dir_update;
2229 if (time_before(next_gc, expires))
2231 if (time_before(expires, now))
2234 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2235 Y_INIT_TIMER(&timer.timer, yaffs_background_waker);
2236 timer.timer.expires = expires + 1;
2237 timer.task = current;
2239 Y_INIT_TIMER(&timer);
2240 timer.expires = expires + 1;
2241 timer.data = (unsigned long)current;
2242 timer.function = yaffs_background_waker;
2245 set_current_state(TASK_INTERRUPTIBLE);
2246 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2247 add_timer(&timer.timer);
2249 del_timer_sync(&timer.timer);
2253 del_timer_sync(&timer);
2260 static int yaffs_bg_start(struct yaffs_dev *dev)
2263 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2268 context->bg_running = 1;
2270 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2271 (void *)dev, "yaffs-bg-%d",
2274 if (IS_ERR(context->bg_thread)) {
2275 retval = PTR_ERR(context->bg_thread);
2276 context->bg_thread = NULL;
2277 context->bg_running = 0;
2282 static void yaffs_bg_stop(struct yaffs_dev *dev)
2284 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2286 ctxt->bg_running = 0;
2288 if (ctxt->bg_thread) {
2289 kthread_stop(ctxt->bg_thread);
2290 ctxt->bg_thread = NULL;
2294 static int yaffs_bg_thread_fn(void *data)
2299 static int yaffs_bg_start(struct yaffs_dev *dev)
2304 static void yaffs_bg_stop(struct yaffs_dev *dev)
2310 static void yaffs_flush_inodes(struct super_block *sb)
2313 struct yaffs_obj *obj;
2315 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
2316 obj = yaffs_inode_to_obj(iptr);
2318 yaffs_trace(YAFFS_TRACE_OS,
2321 yaffs_flush_file(obj, 1, 0, 0);
2326 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2328 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2332 yaffs_flush_inodes(sb);
2333 yaffs_update_dirty_dirs(dev);
2334 yaffs_flush_whole_cache(dev, 1);
2336 yaffs_checkpoint_save(dev);
2339 static LIST_HEAD(yaffs_context_list);
2340 struct mutex yaffs_context_lock;
2342 static void yaffs_put_super(struct super_block *sb)
2344 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2345 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
2347 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2350 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2351 "Shutting down yaffs background thread");
2353 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2354 "yaffs background thread shut down");
2356 yaffs_gross_lock(dev);
2358 yaffs_flush_super(sb, 1);
2360 yaffs_deinitialise(dev);
2362 yaffs_gross_unlock(dev);
2364 mutex_lock(&yaffs_context_lock);
2365 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2366 mutex_unlock(&yaffs_context_lock);
2368 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2369 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
2370 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2375 yaffs_put_mtd_device(mtd);
2377 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2378 "yaffs_put_super done");
2382 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
2384 return yaffs_gc_control;
2388 #ifdef YAFFS_COMPILE_EXPORTFS
2390 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
2391 uint32_t generation)
2393 return Y_IGET(sb, ino);
2396 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
2397 struct fid *fid, int fh_len,
2400 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2401 yaffs2_nfs_get_inode);
2404 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
2405 struct fid *fid, int fh_len,
2408 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2409 yaffs2_nfs_get_inode);
2412 struct dentry *yaffs2_get_parent(struct dentry *dentry)
2415 struct super_block *sb = dentry->d_inode->i_sb;
2416 struct dentry *parent = ERR_PTR(-ENOENT);
2417 struct inode *inode;
2418 unsigned long parent_ino;
2419 struct yaffs_obj *d_obj;
2420 struct yaffs_obj *parent_obj;
2422 d_obj = yaffs_inode_to_obj(dentry->d_inode);
2425 parent_obj = d_obj->parent;
2427 parent_ino = yaffs_get_obj_inode(parent_obj);
2428 inode = Y_IGET(sb, parent_ino);
2430 if (IS_ERR(inode)) {
2431 parent = ERR_CAST(inode);
2433 parent = d_obtain_alias(inode);
2434 if (!IS_ERR(parent)) {
2435 parent = ERR_PTR(-ENOMEM);
2445 /* Just declare a zero structure as a NULL value implies
2446 * using the default functions of exportfs.
2449 static struct export_operations yaffs_export_ops = {
2450 .fh_to_dentry = yaffs2_fh_to_dentry,
2451 .fh_to_parent = yaffs2_fh_to_parent,
2452 .get_parent = yaffs2_get_parent,
2457 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
2459 /* Clear the association between the inode and
2460 * the struct yaffs_obj.
2462 obj->my_inode = NULL;
2463 yaffs_inode_to_obj_lv(inode) = NULL;
2465 /* If the object freeing was deferred, then the real
2467 * This should fix the inode inconsistency problem.
2469 yaffs_handle_defered_free(obj);
2472 #ifdef YAFFS_HAS_EVICT_INODE
2473 /* yaffs_evict_inode combines into one operation what was previously done in
2474 * yaffs_clear_inode() and yaffs_delete_inode()
2477 static void yaffs_evict_inode(struct inode *inode)
2479 struct yaffs_obj *obj;
2480 struct yaffs_dev *dev;
2483 obj = yaffs_inode_to_obj(inode);
2485 yaffs_trace(YAFFS_TRACE_OS,
2486 "yaffs_evict_inode: ino %d, count %d %s",
2487 (int)inode->i_ino, atomic_read(&inode->i_count),
2488 obj ? "object exists" : "null object");
2490 if (!inode->i_nlink && !is_bad_inode(inode))
2492 truncate_inode_pages(&inode->i_data, 0);
2493 Y_CLEAR_INODE(inode);
2495 if (deleteme && obj) {
2497 yaffs_gross_lock(dev);
2499 yaffs_gross_unlock(dev);
2503 yaffs_gross_lock(dev);
2504 yaffs_unstitch_obj(inode, obj);
2505 yaffs_gross_unlock(dev);
2510 /* clear is called to tell the fs to release any per-inode data it holds.
2511 * The object might still exist on disk and is just being thrown out of the cache
2512 * or else the object has actually been deleted and we're being called via
2514 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
2517 static void yaffs_clear_inode(struct inode *inode)
2519 struct yaffs_obj *obj;
2520 struct yaffs_dev *dev;
2522 obj = yaffs_inode_to_obj(inode);
2524 yaffs_trace(YAFFS_TRACE_OS,
2525 "yaffs_clear_inode: ino %d, count %d %s",
2526 (int)inode->i_ino, atomic_read(&inode->i_count),
2527 obj ? "object exists" : "null object");
2531 yaffs_gross_lock(dev);
2532 yaffs_unstitch_obj(inode, obj);
2533 yaffs_gross_unlock(dev);
2538 /* delete is called when the link count is zero and the inode
2539 * is put (ie. nobody wants to know about it anymore, time to
2541 * NB Must call clear_inode()
2543 static void yaffs_delete_inode(struct inode *inode)
2545 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2546 struct yaffs_dev *dev;
2548 yaffs_trace(YAFFS_TRACE_OS,
2549 "yaffs_delete_inode: ino %d, count %d %s",
2550 (int)inode->i_ino, atomic_read(&inode->i_count),
2551 obj ? "object exists" : "null object");
2555 yaffs_gross_lock(dev);
2557 yaffs_gross_unlock(dev);
2559 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
2560 truncate_inode_pages(&inode->i_data, 0);
2569 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2570 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2572 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2573 struct super_block *sb = dentry->d_sb;
2574 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2575 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2577 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2579 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2581 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2584 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
2586 yaffs_gross_lock(dev);
2588 buf->f_type = YAFFS_MAGIC;
2589 buf->f_bsize = sb->s_blocksize;
2590 buf->f_namelen = 255;
2592 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2593 /* Do this if chunk size is not a power of 2 */
2595 uint64_t bytes_in_dev;
2596 uint64_t bytes_free;
2600 ((dev->param.end_block - dev->param.start_block +
2601 1))) * ((uint64_t) (dev->param.chunks_per_block *
2602 dev->data_bytes_per_chunk));
2604 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2605 buf->f_blocks = bytes_in_dev;
2607 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
2608 ((uint64_t) (dev->data_bytes_per_chunk));
2610 do_div(bytes_free, sb->s_blocksize);
2612 buf->f_bfree = bytes_free;
2614 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2617 (dev->param.end_block - dev->param.start_block + 1) *
2618 dev->param.chunks_per_block /
2619 (sb->s_blocksize / dev->data_bytes_per_chunk);
2621 yaffs_get_n_free_chunks(dev) /
2622 (sb->s_blocksize / dev->data_bytes_per_chunk);
2625 (dev->param.end_block - dev->param.start_block + 1) *
2626 dev->param.chunks_per_block *
2627 (dev->data_bytes_per_chunk / sb->s_blocksize);
2630 yaffs_get_n_free_chunks(dev) *
2631 (dev->data_bytes_per_chunk / sb->s_blocksize);
2636 buf->f_bavail = buf->f_bfree;
2638 yaffs_gross_unlock(dev);
2644 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
2647 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2648 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2649 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2651 int dirty = yaffs_check_super_dirty(dev);
2653 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2654 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
2656 dirty ? "dirty" : "clean",
2657 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2658 oneshot_checkpoint ? " one-shot" : "");
2660 yaffs_gross_lock(dev);
2661 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2662 oneshot_checkpoint) && !dev->is_checkpointed;
2664 if (dirty || do_checkpoint) {
2665 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2666 yaffs_clear_super_dirty(dev);
2667 if (oneshot_checkpoint)
2668 yaffs_auto_checkpoint &= ~4;
2670 yaffs_gross_unlock(dev);
2676 #ifdef YAFFS_HAS_WRITE_SUPER
2677 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2678 static void yaffs_write_super(struct super_block *sb)
2680 static int yaffs_write_super(struct super_block *sb)
2683 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2685 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2686 "yaffs_write_super %s",
2687 request_checkpoint ? " checkpt" : "");
2689 yaffs_do_sync_fs(sb, request_checkpoint);
2691 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2697 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2698 static int yaffs_sync_fs(struct super_block *sb, int wait)
2700 static int yaffs_sync_fs(struct super_block *sb)
2703 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2705 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2706 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
2708 yaffs_do_sync_fs(sb, request_checkpoint);
2713 /* the function only is used to change dev->read_only when this file system
2716 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2719 int was_read_only = 0;
2720 struct mtd_info *mtd;
2721 struct yaffs_dev *dev = 0;
2723 /* Get the device */
2724 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2726 yaffs_trace(YAFFS_TRACE_ALWAYS,
2727 "MTD device #%u doesn't appear to exist",
2732 /* Check it's NAND */
2733 if (mtd->type != MTD_NANDFLASH) {
2734 yaffs_trace(YAFFS_TRACE_ALWAYS,
2735 "MTD device is not NAND it's type %d",
2740 read_only = ((*flags & MS_RDONLY) != 0);
2741 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2744 "yaffs: mtd is read only, setting superblock read only");
2745 *flags |= MS_RDONLY;
2748 dev = sb->s_fs_info;
2749 was_read_only = dev->read_only;
2750 dev->read_only = read_only;
2752 if (was_read_only && !read_only) {
2753 yaffs_gross_lock(dev);
2754 yaffs_guts_cleanup(dev);
2755 yaffs_gross_unlock(dev);
2756 yaffs_bg_start(dev);
2757 } else if (!was_read_only && read_only) {
2764 static const struct super_operations yaffs_super_ops = {
2765 .statfs = yaffs_statfs,
2767 #ifndef YAFFS_USE_OWN_IGET
2768 .read_inode = yaffs_read_inode,
2770 #ifdef YAFFS_HAS_PUT_INODE
2771 .put_inode = yaffs_put_inode,
2773 .put_super = yaffs_put_super,
2774 #ifdef YAFFS_HAS_EVICT_INODE
2775 .evict_inode = yaffs_evict_inode,
2777 .delete_inode = yaffs_delete_inode,
2778 .clear_inode = yaffs_clear_inode,
2780 .sync_fs = yaffs_sync_fs,
2781 #ifdef YAFFS_HAS_WRITE_SUPER
2782 .write_super = yaffs_write_super,
2784 .remount_fs = yaffs_remount_fs,
2787 struct yaffs_options {
2789 int skip_checkpoint_read;
2790 int skip_checkpoint_write;
2793 int tags_ecc_overridden;
2794 int lazy_loading_enabled;
2795 int lazy_loading_overridden;
2796 int empty_lost_and_found;
2797 int empty_lost_and_found_overridden;
2798 int disable_summary;
2801 #define MAX_OPT_LEN 30
2802 static int yaffs_parse_options(struct yaffs_options *options,
2803 const char *options_str)
2805 char cur_opt[MAX_OPT_LEN + 1];
2809 /* Parse through the options which is a comma seperated list */
2811 while (options_str && *options_str && !error) {
2812 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2815 while (*options_str == ',')
2818 while (*options_str && *options_str != ',') {
2819 if (p < MAX_OPT_LEN) {
2820 cur_opt[p] = *options_str;
2826 if (!strcmp(cur_opt, "inband-tags")) {
2827 options->inband_tags = 1;
2828 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2829 options->tags_ecc_on = 0;
2830 options->tags_ecc_overridden = 1;
2831 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2832 options->tags_ecc_on = 1;
2833 options->tags_ecc_overridden = 1;
2834 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2835 options->lazy_loading_enabled = 0;
2836 options->lazy_loading_overridden = 1;
2837 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2838 options->lazy_loading_enabled = 1;
2839 options->lazy_loading_overridden = 1;
2840 } else if (!strcmp(cur_opt, "disable-summary")) {
2841 options->disable_summary = 1;
2842 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2843 options->empty_lost_and_found = 0;
2844 options->empty_lost_and_found_overridden = 1;
2845 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2846 options->empty_lost_and_found = 1;
2847 options->empty_lost_and_found_overridden = 1;
2848 } else if (!strcmp(cur_opt, "no-cache")) {
2849 options->no_cache = 1;
2850 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2851 options->skip_checkpoint_read = 1;
2852 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2853 options->skip_checkpoint_write = 1;
2854 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2855 options->skip_checkpoint_read = 1;
2856 options->skip_checkpoint_write = 1;
2858 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2868 static struct dentry *yaffs_make_root(struct inode *inode)
2870 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
2871 struct dentry *root = d_alloc_root(inode);
2878 return d_make_root(inode);
2885 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2886 struct super_block *sb,
2887 void *data, int silent)
2890 struct inode *inode = NULL;
2891 struct dentry *root;
2892 struct yaffs_dev *dev = 0;
2893 char devname_buf[BDEVNAME_SIZE + 1];
2894 struct mtd_info *mtd;
2896 char *data_str = (char *)data;
2897 struct yaffs_linux_context *context = NULL;
2898 struct yaffs_param *param;
2901 int inband_tags = 0;
2903 struct yaffs_options options;
2907 struct yaffs_linux_context *context_iterator;
2908 struct list_head *l;
2911 printk(KERN_INFO "yaffs: sb is NULL\n");
2915 sb->s_magic = YAFFS_MAGIC;
2916 sb->s_op = &yaffs_super_ops;
2917 sb->s_flags |= MS_NOATIME;
2919 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2921 #ifdef YAFFS_COMPILE_EXPORTFS
2922 sb->s_export_op = &yaffs_export_ops;
2926 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2927 else if (!yaffs_devname(sb, devname_buf))
2928 printk(KERN_INFO "yaffs: devname is NULL\n");
2930 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2932 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2937 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2939 memset(&options, 0, sizeof(options));
2941 if (yaffs_parse_options(&options, data_str)) {
2942 /* Option parsing failed */
2946 sb->s_blocksize = PAGE_CACHE_SIZE;
2947 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2949 yaffs_trace(YAFFS_TRACE_OS,
2950 "yaffs_read_super: Using yaffs%d", yaffs_version);
2951 yaffs_trace(YAFFS_TRACE_OS,
2952 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2954 yaffs_trace(YAFFS_TRACE_ALWAYS,
2955 "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
2956 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2957 yaffs_devname(sb, devname_buf));
2959 /* Get the device */
2960 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2962 yaffs_trace(YAFFS_TRACE_ALWAYS,
2963 "yaffs: MTD device %u either not valid or unavailable",
2968 if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2969 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2973 /* Added NCB 26/5/2006 for completeness */
2974 if (yaffs_version == 2 && !options.inband_tags
2975 && WRITE_SIZE(mtd) == 512) {
2976 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2980 if (mtd->oobavail < sizeof(struct yaffs_packed_tags2) ||
2981 options.inband_tags)
2984 if(yaffs_verify_mtd(mtd, yaffs_version, inband_tags) < 0)
2987 /* OK, so if we got here, we have an MTD that's NAND and looks
2988 * like it has the right capabilities
2989 * Set the struct yaffs_dev up for mtd
2992 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2995 "yaffs: mtd is read only, setting superblock read only\n"
2997 sb->s_flags |= MS_RDONLY;
3000 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
3001 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
3003 if (!dev || !context) {
3009 /* Deep shit could not allocate device structure */
3010 yaffs_trace(YAFFS_TRACE_ALWAYS,
3011 "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
3015 memset(dev, 0, sizeof(struct yaffs_dev));
3016 param = &(dev->param);
3018 memset(context, 0, sizeof(struct yaffs_linux_context));
3019 dev->os_context = context;
3020 INIT_LIST_HEAD(&(context->context_list));
3022 context->super = sb;
3024 dev->read_only = read_only;
3026 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3027 sb->s_fs_info = dev;
3029 sb->u.generic_sbp = dev;
3033 dev->driver_context = mtd;
3034 param->name = mtd->name;
3036 /* Set up the memory size parameters.... */
3039 param->n_reserved_blocks = 5;
3040 param->n_caches = (options.no_cache) ? 0 : 10;
3041 param->inband_tags = inband_tags;
3043 param->enable_xattr = 1;
3044 if (options.lazy_loading_overridden)
3045 param->disable_lazy_load = !options.lazy_loading_enabled;
3047 param->defered_dir_update = 1;
3049 if (options.tags_ecc_overridden)
3050 param->no_tags_ecc = !options.tags_ecc_on;
3052 param->empty_lost_n_found = 1;
3053 param->refresh_period = 500;
3054 param->disable_summary = options.disable_summary;
3057 #ifdef CONFIG_YAFFS_DISABLE_BAD_BLOCK_MARKING
3058 param->disable_bad_block_marking = 1;
3060 if (options.empty_lost_and_found_overridden)
3061 param->empty_lost_n_found = options.empty_lost_and_found;
3063 /* ... and the functions. */
3064 if (yaffs_version == 2) {
3065 param->is_yaffs2 = 1;
3066 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3067 param->total_bytes_per_chunk = mtd->writesize;
3068 param->chunks_per_block = mtd->erasesize / mtd->writesize;
3070 param->total_bytes_per_chunk = mtd->oobblock;
3071 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
3073 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
3075 param->start_block = 0;
3076 param->end_block = n_blocks - 1;
3078 param->is_yaffs2 = 0;
3079 n_blocks = YCALCBLOCKS(mtd->size,
3080 YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
3082 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
3083 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
3086 param->start_block = 0;
3087 param->end_block = n_blocks - 1;
3089 yaffs_mtd_drv_install(dev);
3091 param->sb_dirty_fn = yaffs_set_super_dirty;
3092 param->gc_control_fn = yaffs_gc_control_callback;
3094 yaffs_dev_to_lc(dev)->super = sb;
3096 param->use_nand_ecc = 1;
3098 param->skip_checkpt_rd = options.skip_checkpoint_read;
3099 param->skip_checkpt_wr = options.skip_checkpoint_write;
3101 mutex_lock(&yaffs_context_lock);
3102 /* Get a mount id */
3104 for (mount_id = 0; !found; mount_id++) {
3106 list_for_each(l, &yaffs_context_list) {
3108 list_entry(l, struct yaffs_linux_context,
3110 if (context_iterator->mount_id == mount_id)
3114 context->mount_id = mount_id;
3116 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
3117 &yaffs_context_list);
3118 mutex_unlock(&yaffs_context_lock);
3120 /* Directory search handling... */
3121 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
3122 param->remove_obj_fn = yaffs_remove_obj_callback;
3124 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
3126 yaffs_gross_lock(dev);
3128 err = yaffs_guts_initialise(dev);
3130 yaffs_trace(YAFFS_TRACE_OS,
3131 "yaffs_read_super: guts initialised %s",
3132 (err == YAFFS_OK) ? "OK" : "FAILED");
3134 if (err == YAFFS_OK)
3135 yaffs_bg_start(dev);
3137 if (!context->bg_thread)
3138 param->defered_dir_update = 0;
3140 sb->s_maxbytes = yaffs_max_file_size(dev);
3142 /* Release lock before yaffs_get_inode() */
3143 yaffs_gross_unlock(dev);
3145 /* Create root inode */
3146 if (err == YAFFS_OK)
3147 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
3152 inode->i_op = &yaffs_dir_inode_operations;
3153 inode->i_fop = &yaffs_dir_operations;
3155 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
3157 root = yaffs_make_root(inode);
3163 if(!dev->is_checkpointed)
3164 yaffs_set_super_dirty(dev);
3166 yaffs_trace(YAFFS_TRACE_ALWAYS,
3167 "yaffs_read_super: is_checkpointed %d",
3168 dev->is_checkpointed);
3170 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
3174 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3175 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3178 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3181 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3182 static struct dentry *yaffs_mount(struct file_system_type *fs_type, int flags,
3183 const char *dev_name, void *data)
3185 return mount_bdev(fs_type, flags, dev_name, data, yaffs_internal_read_super_mtd);
3187 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3188 static int yaffs_read_super(struct file_system_type *fs,
3189 int flags, const char *dev_name,
3190 void *data, struct vfsmount *mnt)
3193 return get_sb_bdev(fs, flags, dev_name, data,
3194 yaffs_internal_read_super_mtd, mnt);
3197 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3198 int flags, const char *dev_name,
3202 return get_sb_bdev(fs, flags, dev_name, data,
3203 yaffs_internal_read_super_mtd);
3207 static struct file_system_type yaffs_fs_type = {
3208 .owner = THIS_MODULE,
3210 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3211 .mount = yaffs_mount,
3213 .get_sb = yaffs_read_super,
3215 .kill_sb = kill_block_super,
3216 .fs_flags = FS_REQUIRES_DEV,
3219 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3222 return yaffs_internal_read_super(1, sb, data, silent);
3225 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3230 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3231 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3234 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3237 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3238 static struct dentry *yaffs2_mount(struct file_system_type *fs_type, int flags,
3239 const char *dev_name, void *data)
3241 return mount_bdev(fs_type, flags, dev_name, data, yaffs2_internal_read_super_mtd);
3243 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3244 static int yaffs2_read_super(struct file_system_type *fs,
3245 int flags, const char *dev_name, void *data,
3246 struct vfsmount *mnt)
3248 return get_sb_bdev(fs, flags, dev_name, data,
3249 yaffs2_internal_read_super_mtd, mnt);
3252 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3253 int flags, const char *dev_name,
3257 return get_sb_bdev(fs, flags, dev_name, data,
3258 yaffs2_internal_read_super_mtd);
3262 static struct file_system_type yaffs2_fs_type = {
3263 .owner = THIS_MODULE,
3265 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3266 .mount = yaffs2_mount,
3268 .get_sb = yaffs2_read_super,
3270 .kill_sb = kill_block_super,
3271 .fs_flags = FS_REQUIRES_DEV,
3274 static struct super_block *yaffs2_read_super(struct super_block *sb,
3275 void *data, int silent)
3277 return yaffs_internal_read_super(2, sb, data, silent);
3280 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3285 static struct proc_dir_entry *my_proc_entry;
3287 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3289 struct yaffs_param *param = &dev->param;
3292 yaffs_count_blocks_by_state(dev,bs);
3294 buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
3295 buf += sprintf(buf, "end_block............ %d\n", param->end_block);
3296 buf += sprintf(buf, "total_bytes_per_chunk %d\n",
3297 param->total_bytes_per_chunk);
3298 buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
3299 buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
3300 buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
3301 buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
3302 buf += sprintf(buf, "empty_lost_n_found... %d\n",
3303 param->empty_lost_n_found);
3304 buf += sprintf(buf, "disable_lazy_load.... %d\n",
3305 param->disable_lazy_load);
3306 buf += sprintf(buf, "disable_bad_block_mrk %d\n",
3307 param->disable_bad_block_marking);
3308 buf += sprintf(buf, "refresh_period....... %d\n",
3309 param->refresh_period);
3310 buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
3311 buf += sprintf(buf, "n_reserved_blocks.... %d\n",
3312 param->n_reserved_blocks);
3313 buf += sprintf(buf, "always_check_erased.. %d\n",
3314 param->always_check_erased);
3315 buf += sprintf(buf, "\n");
3316 buf += sprintf(buf, "block count by state\n");
3317 buf += sprintf(buf, "0:%d 1:%d 2:%d 3:%d 4:%d\n",
3318 bs[0], bs[1], bs[2], bs[3], bs[4]);
3319 buf += sprintf(buf, "5:%d 6:%d 7:%d 8:%d 9:%d\n",
3320 bs[5], bs[6], bs[7], bs[8], bs[9]);
3325 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3327 buf += sprintf(buf, "max file size....... %lld\n",
3328 (long long) yaffs_max_file_size(dev));
3329 buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
3330 dev->data_bytes_per_chunk);
3331 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3332 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3333 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3334 buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
3335 dev->blocks_in_checkpt);
3336 buf += sprintf(buf, "\n");
3337 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3338 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3339 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3340 buf += sprintf(buf, "\n");
3341 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3342 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3343 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3344 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3345 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3346 buf += sprintf(buf, "passive_gc_count..... %u\n",
3347 dev->passive_gc_count);
3348 buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
3349 dev->oldest_dirty_gc_count);
3350 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3351 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3352 buf += sprintf(buf, "n_retried_writes..... %u\n",
3353 dev->n_retried_writes);
3354 buf += sprintf(buf, "n_retired_blocks..... %u\n",
3355 dev->n_retired_blocks);
3356 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3357 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3358 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
3359 dev->n_tags_ecc_fixed);
3360 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
3361 dev->n_tags_ecc_unfixed);
3362 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3363 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3364 buf += sprintf(buf, "n_unlinked_files..... %u\n",
3365 dev->n_unlinked_files);
3366 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3367 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3368 buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
3369 buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
3374 static int yaffs_proc_read(char *page,
3376 off_t offset, int count, int *eof, void *data)
3378 struct list_head *item;
3383 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3384 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3385 * This also assumes the user has posted a read buffer large
3386 * enough to hold the complete output; but that's life in /proc.
3391 /* Print header first */
3394 sprintf(buf, "Multi-version YAFFS\n");
3396 buf += sprintf(buf, "\n");
3400 mutex_lock(&yaffs_context_lock);
3402 /* Locate and print the Nth entry. Order N-squared but N is small. */
3403 list_for_each(item, &yaffs_context_list) {
3404 struct yaffs_linux_context *dc =
3405 list_entry(item, struct yaffs_linux_context,
3407 struct yaffs_dev *dev = dc->dev;
3409 if (n < (step & ~1)) {
3413 if ((step & 1) == 0) {
3415 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3417 buf = yaffs_dump_dev_part0(buf, dev);
3419 buf = yaffs_dump_dev_part1(buf, dev);
3424 mutex_unlock(&yaffs_context_lock);
3427 return buf - page < count ? buf - page : count;
3431 * Set the verbosity of the warnings and error messages.
3433 * Note that the names can only be a..z or _ with the current code.
3438 unsigned mask_bitfield;
3440 {"allocate", YAFFS_TRACE_ALLOCATE},
3441 {"always", YAFFS_TRACE_ALWAYS},
3442 {"background", YAFFS_TRACE_BACKGROUND},
3443 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3444 {"buffers", YAFFS_TRACE_BUFFERS},
3445 {"bug", YAFFS_TRACE_BUG},
3446 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3447 {"deletion", YAFFS_TRACE_DELETION},
3448 {"erase", YAFFS_TRACE_ERASE},
3449 {"error", YAFFS_TRACE_ERROR},
3450 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3451 {"gc", YAFFS_TRACE_GC},
3452 {"lock", YAFFS_TRACE_LOCK},
3453 {"mtd", YAFFS_TRACE_MTD},
3454 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3455 {"os", YAFFS_TRACE_OS},
3456 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3457 {"scan", YAFFS_TRACE_SCAN},
3458 {"mount", YAFFS_TRACE_MOUNT},
3459 {"tracing", YAFFS_TRACE_TRACING},
3460 {"sync", YAFFS_TRACE_SYNC},
3461 {"write", YAFFS_TRACE_WRITE},
3462 {"verify", YAFFS_TRACE_VERIFY},
3463 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3464 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3465 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3466 {"all", 0xffffffff},
3471 #define MAX_MASK_NAME_LENGTH 40
3472 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3473 unsigned long count)
3475 unsigned rg = 0, mask_bitfield;
3479 char substring[MAX_MASK_NAME_LENGTH + 1];
3485 rg = yaffs_trace_mask;
3487 while (!done && (pos < count)) {
3489 while ((pos < count) && isspace(buf[pos]))
3506 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3508 if (end > buf + pos) {
3509 mask_name = "numeral";
3510 len = end - (buf + pos);
3514 for (x = buf + pos, i = 0;
3515 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3516 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3518 substring[i] = '\0';
3520 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3521 if (strcmp(substring, mask_flags[i].mask_name)
3523 mask_name = mask_flags[i].mask_name;
3525 mask_flags[i].mask_bitfield;
3532 if (mask_name != NULL) {
3536 rg &= ~mask_bitfield;
3539 rg |= mask_bitfield;
3545 rg |= mask_bitfield;
3551 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3553 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3555 if (rg & YAFFS_TRACE_ALWAYS) {
3556 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3558 flag = ((rg & mask_flags[i].mask_bitfield) ==
3559 mask_flags[i].mask_bitfield) ? '+' : '-';
3560 printk(KERN_DEBUG "%c%s\n", flag,
3561 mask_flags[i].mask_name);
3568 /* Debug strings are of the form:
3569 * .bnnn print info on block n
3570 * .cobjn,chunkn print nand chunk id for objn:chunkn
3573 static int yaffs_proc_debug_write(struct file *file, const char *buf,
3574 unsigned long count)
3583 struct list_head *item;
3585 memset(str, 0, sizeof(str));
3586 memcpy(str, buf, min((size_t)count, sizeof(str) -1));
3594 while (*p1 && *p1 != ',') {
3600 p0_val = simple_strtol(p0, NULL, 0);
3601 p1_val = simple_strtol(p1, NULL, 0);
3604 mutex_lock(&yaffs_context_lock);
3606 /* Locate and print the Nth entry. Order N-squared but N is small. */
3607 list_for_each(item, &yaffs_context_list) {
3608 struct yaffs_linux_context *dc =
3609 list_entry(item, struct yaffs_linux_context,
3611 struct yaffs_dev *dev = dc->dev;
3614 struct yaffs_block_info *bi;
3616 bi = yaffs_get_block_info(dev,p0_val);
3619 printk("Block %d: state %d, retire %d, use %d, seq %d\n",
3620 (int)p0_val, bi->block_state,
3621 bi->needs_retiring, bi->pages_in_use,
3624 } else if (cmd == 'c') {
3625 struct yaffs_obj *obj;
3628 obj = yaffs_find_by_number(dev, p0_val);
3630 printk("No obj %d\n", (int)p0_val);
3633 nand_chunk = obj->hdr_chunk;
3636 yaffs_find_chunk_in_file(obj,
3638 printk("Nand chunk for %d:%d is %d\n",
3639 (int)p0_val, (int)p1_val, nand_chunk);
3644 mutex_unlock(&yaffs_context_lock);
3650 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0))
3651 static int yaffs_proc_write(struct file *file, const char *buf,
3652 unsigned long count, void *ppos)
3654 static ssize_t yaffs_proc_write(struct file *file, const char __user *buf,
3655 size_t count, loff_t *ppos)
3659 return yaffs_proc_debug_write(file, buf, count);
3660 return yaffs_proc_write_trace_options(file, buf, count);
3663 /* Stuff to handle installation of file systems */
3664 struct file_system_to_install {
3665 struct file_system_type *fst;
3669 static struct file_system_to_install fs_to_install[] = {
3670 {&yaffs_fs_type, 0},
3671 {&yaffs2_fs_type, 0},
3676 #ifdef YAFFS_NEW_PROCFS
3677 static int yaffs_proc_show(struct seq_file *m, void *v)
3679 /* FIXME: Unify in a better way? */
3684 len = yaffs_proc_read(buffer, &start, 0, sizeof(buffer), NULL, NULL);
3685 seq_puts(m, buffer);
3689 static int yaffs_proc_open(struct inode *inode, struct file *file)
3691 return single_open(file, yaffs_proc_show, NULL);
3694 static struct file_operations procfs_ops = {
3695 .owner = THIS_MODULE,
3696 .open = yaffs_proc_open,
3698 .write = yaffs_proc_write,
3699 .release = single_release,
3702 static int yaffs_procfs_init(void)
3704 /* Install the proc_fs entries */
3705 my_proc_entry = proc_create("yaffs",
3710 if (my_proc_entry) {
3720 static int yaffs_procfs_init(void)
3722 /* Install the proc_fs entries */
3723 my_proc_entry = create_proc_entry("yaffs",
3724 S_IRUGO | S_IFREG, YPROC_ROOT);
3726 if (my_proc_entry) {
3727 my_proc_entry->write_proc = yaffs_proc_write;
3728 my_proc_entry->read_proc = yaffs_proc_read;
3729 my_proc_entry->data = NULL;
3739 static int __init init_yaffs_fs(void)
3742 struct file_system_to_install *fsinst;
3744 yaffs_trace(YAFFS_TRACE_ALWAYS,
3745 "yaffs Installing.");
3747 mutex_init(&yaffs_context_lock);
3749 error = yaffs_procfs_init();
3753 /* Now add the file system entries */
3755 fsinst = fs_to_install;
3757 while (fsinst->fst && !error) {
3758 error = register_filesystem(fsinst->fst);
3760 fsinst->installed = 1;
3764 /* Any errors? uninstall */
3766 fsinst = fs_to_install;
3768 while (fsinst->fst) {
3769 if (fsinst->installed) {
3770 unregister_filesystem(fsinst->fst);
3771 fsinst->installed = 0;
3780 static void __exit exit_yaffs_fs(void)
3783 struct file_system_to_install *fsinst;
3785 yaffs_trace(YAFFS_TRACE_ALWAYS,
3788 remove_proc_entry("yaffs", YPROC_ROOT);
3790 fsinst = fs_to_install;
3792 while (fsinst->fst) {
3793 if (fsinst->installed) {
3794 unregister_filesystem(fsinst->fst);
3795 fsinst->installed = 0;
3801 module_init(init_yaffs_fs)
3802 module_exit(exit_yaffs_fs)
3804 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3805 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
3806 MODULE_LICENSE("GPL");