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)) {
1836 yaffs_gross_unlock(dev);
1842 while (sc->next_return) {
1844 l = sc->next_return;
1845 if (curoffs >= dc->pos) {
1846 int this_inode = yaffs_get_obj_inode(l);
1847 int this_type = yaffs_get_obj_type(l);
1849 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1850 yaffs_trace(YAFFS_TRACE_OS,
1851 "yaffs_readdir: %s inode %d",
1852 name, yaffs_get_obj_inode(l));
1854 yaffs_gross_unlock(dev);
1861 yaffs_gross_lock(dev);
1865 yaffs_gross_lock(dev);
1870 yaffs_search_advance(sc);
1874 yaffs_search_end(sc);
1875 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1876 yaffs_gross_unlock(dev);
1883 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1885 struct yaffs_obj *obj;
1886 struct yaffs_dev *dev;
1887 struct yaffs_search_context *sc;
1888 struct inode *inode = Y_GET_DENTRY(f)->d_inode;
1889 unsigned long offset, curoffs;
1890 struct yaffs_obj *l;
1893 char name[YAFFS_MAX_NAME_LENGTH + 1];
1895 obj = yaffs_dentry_to_obj(Y_GET_DENTRY(f));
1898 yaffs_gross_lock(dev);
1900 yaffs_dev_to_lc(dev)->readdir_process = current;
1904 sc = yaffs_new_search(obj);
1910 yaffs_trace(YAFFS_TRACE_OS,
1911 "yaffs_readdir: starting at %d", (int)offset);
1914 yaffs_trace(YAFFS_TRACE_OS,
1915 "yaffs_readdir: entry . ino %d",
1917 yaffs_gross_unlock(dev);
1918 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
1919 yaffs_gross_lock(dev);
1922 yaffs_gross_lock(dev);
1927 yaffs_trace(YAFFS_TRACE_OS,
1928 "yaffs_readdir: entry .. ino %d",
1929 (int)f->f_dentry->d_parent->d_inode->i_ino);
1930 yaffs_gross_unlock(dev);
1931 if (filldir(dirent, "..", 2, offset,
1932 f->f_dentry->d_parent->d_inode->i_ino,
1934 yaffs_gross_lock(dev);
1937 yaffs_gross_lock(dev);
1944 /* If the directory has changed since the open or last call to
1945 readdir, rewind to after the 2 canned entries. */
1946 if (f->f_version != inode->i_version) {
1949 f->f_version = inode->i_version;
1952 while (sc->next_return) {
1954 l = sc->next_return;
1955 if (curoffs >= offset) {
1956 int this_inode = yaffs_get_obj_inode(l);
1957 int this_type = yaffs_get_obj_type(l);
1959 yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
1960 yaffs_trace(YAFFS_TRACE_OS,
1961 "yaffs_readdir: %s inode %d",
1962 name, yaffs_get_obj_inode(l));
1964 yaffs_gross_unlock(dev);
1969 offset, this_inode, this_type) < 0) {
1970 yaffs_gross_lock(dev);
1974 yaffs_gross_lock(dev);
1979 yaffs_search_advance(sc);
1983 yaffs_search_end(sc);
1984 yaffs_dev_to_lc(dev)->readdir_process = NULL;
1985 yaffs_gross_unlock(dev);
1992 static const struct file_operations yaffs_dir_operations = {
1993 .read = generic_read_dir,
1994 #ifdef YAFFS_USE_DIR_ITERATE
1995 .iterate = yaffs_iterate,
1997 .readdir = yaffs_readdir,
1999 .fsync = yaffs_sync_object,
2000 .llseek = generic_file_llseek,
2003 static void yaffs_fill_inode_from_obj(struct inode *inode,
2004 struct yaffs_obj *obj)
2008 /* Check mode against the variant type and attempt to repair if broken. */
2009 u32 mode = obj->yst_mode;
2010 switch (obj->variant_type) {
2011 case YAFFS_OBJECT_TYPE_FILE:
2012 if (!S_ISREG(mode)) {
2013 obj->yst_mode &= ~S_IFMT;
2014 obj->yst_mode |= S_IFREG;
2018 case YAFFS_OBJECT_TYPE_SYMLINK:
2019 if (!S_ISLNK(mode)) {
2020 obj->yst_mode &= ~S_IFMT;
2021 obj->yst_mode |= S_IFLNK;
2025 case YAFFS_OBJECT_TYPE_DIRECTORY:
2026 if (!S_ISDIR(mode)) {
2027 obj->yst_mode &= ~S_IFMT;
2028 obj->yst_mode |= S_IFDIR;
2032 case YAFFS_OBJECT_TYPE_UNKNOWN:
2033 case YAFFS_OBJECT_TYPE_HARDLINK:
2034 case YAFFS_OBJECT_TYPE_SPECIAL:
2040 inode->i_flags |= S_NOATIME;
2042 inode->i_ino = obj->obj_id;
2043 inode->i_mode = obj->yst_mode;
2044 inode->i_uid = MAKE_uid(obj->yst_uid);
2045 inode->i_gid = MAKE_gid(obj->yst_gid);
2046 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
2047 inode->i_blksize = inode->i_sb->s_blocksize;
2049 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2051 inode->i_rdev = old_decode_dev(obj->yst_rdev);
2052 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2053 inode->i_atime.tv_nsec = 0;
2054 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2055 inode->i_mtime.tv_nsec = 0;
2056 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2057 inode->i_ctime.tv_nsec = 0;
2059 inode->i_rdev = obj->yst_rdev;
2060 inode->i_atime = obj->yst_atime;
2061 inode->i_mtime = obj->yst_mtime;
2062 inode->i_ctime = obj->yst_ctime;
2064 inode->i_size = yaffs_get_obj_length(obj);
2065 inode->i_blocks = (inode->i_size + 511) >> 9;
2067 set_nlink(inode, yaffs_get_obj_link_count(obj));
2069 yaffs_trace(YAFFS_TRACE_OS,
2070 "yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
2071 inode->i_mode, obj->yst_uid, obj->yst_gid,
2072 inode->i_size, atomic_read(&inode->i_count));
2074 switch (obj->yst_mode & S_IFMT) {
2075 default: /* fifo, device or socket */
2076 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2077 init_special_inode(inode, obj->yst_mode,
2078 old_decode_dev(obj->yst_rdev));
2080 init_special_inode(inode, obj->yst_mode,
2081 (dev_t) (obj->yst_rdev));
2084 case S_IFREG: /* file */
2085 inode->i_op = &yaffs_file_inode_operations;
2086 inode->i_fop = &yaffs_file_operations;
2087 inode->i_mapping->a_ops =
2088 &yaffs_file_address_operations;
2090 case S_IFDIR: /* directory */
2091 inode->i_op = &yaffs_dir_inode_operations;
2092 inode->i_fop = &yaffs_dir_operations;
2094 case S_IFLNK: /* symlink */
2095 inode->i_op = &yaffs_symlink_inode_operations;
2099 yaffs_inode_to_obj_lv(inode) = obj;
2101 obj->my_inode = inode;
2104 yaffs_trace(YAFFS_TRACE_OS,
2105 "yaffs_fill_inode invalid parameters");
2113 * yaffs background thread functions .
2114 * yaffs_bg_thread_fn() the thread function
2115 * yaffs_bg_start() launches the background thread.
2116 * yaffs_bg_stop() cleans up the background thread.
2119 * The thread should only run after the yaffs is initialised
2120 * The thread should be stopped before yaffs is unmounted.
2121 * The thread should not do any writing while the fs is in read only.
2124 static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
2126 unsigned erased_chunks =
2127 dev->n_erased_blocks * dev->param.chunks_per_block;
2128 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2129 unsigned scattered = 0; /* Free chunks not in an erased block */
2131 if (erased_chunks < dev->n_free_chunks)
2132 scattered = (dev->n_free_chunks - erased_chunks);
2134 if (!context->bg_running)
2136 else if (scattered < (dev->param.chunks_per_block * 2))
2138 else if (erased_chunks > dev->n_free_chunks / 2)
2140 else if (erased_chunks > dev->n_free_chunks / 4)
2146 #ifdef YAFFS_COMPILE_BACKGROUND
2148 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2149 struct timer_struct {
2150 struct task_struct *task;
2151 struct timer_list timer;
2155 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2156 static void yaffs_background_waker(struct timer_list *t)
2158 struct timer_struct *ts = from_timer(ts, t, timer);
2160 wake_up_process(ts->task);
2163 void yaffs_background_waker(unsigned long data)
2165 wake_up_process((struct task_struct *)data);
2169 static int yaffs_bg_thread_fn(void *data)
2171 struct yaffs_dev *dev = (struct yaffs_dev *)data;
2172 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2173 unsigned long now = jiffies;
2174 unsigned long next_dir_update = now;
2175 unsigned long next_gc = now;
2176 unsigned long expires;
2177 unsigned int urgency;
2180 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2181 struct timer_struct timer;
2183 struct timer_list timer;
2186 yaffs_trace(YAFFS_TRACE_BACKGROUND,
2187 "yaffs_background starting for dev %p", (void *)dev);
2189 #ifdef YAFFS_COMPILE_FREEZER
2192 while (context->bg_running) {
2193 yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
2195 if (kthread_should_stop())
2198 #ifdef YAFFS_COMPILE_FREEZER
2199 if (try_to_freeze())
2202 yaffs_gross_lock(dev);
2206 if (time_after(now, next_dir_update) && yaffs_bg_enable) {
2207 yaffs_update_dirty_dirs(dev);
2208 next_dir_update = now + HZ;
2211 if (time_after(now, next_gc) && yaffs_bg_enable) {
2212 if (!dev->is_checkpointed) {
2213 urgency = yaffs_bg_gc_urgency(dev);
2214 gc_result = yaffs_bg_gc(dev, urgency);
2216 next_gc = now + HZ / 20 + 1;
2217 else if (urgency > 0)
2218 next_gc = now + HZ / 10 + 1;
2220 next_gc = now + HZ * 2;
2223 * gc not running so set to next_dir_update
2224 * to cut down on wake ups
2226 next_gc = next_dir_update;
2229 yaffs_gross_unlock(dev);
2230 expires = next_dir_update;
2231 if (time_before(next_gc, expires))
2233 if (time_before(expires, now))
2236 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2237 Y_INIT_TIMER(&timer.timer, yaffs_background_waker);
2238 timer.timer.expires = expires + 1;
2239 timer.task = current;
2241 Y_INIT_TIMER(&timer);
2242 timer.expires = expires + 1;
2243 timer.data = (unsigned long)current;
2244 timer.function = yaffs_background_waker;
2247 set_current_state(TASK_INTERRUPTIBLE);
2248 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
2249 add_timer(&timer.timer);
2251 del_timer_sync(&timer.timer);
2255 del_timer_sync(&timer);
2262 static int yaffs_bg_start(struct yaffs_dev *dev)
2265 struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
2270 context->bg_running = 1;
2272 context->bg_thread = kthread_run(yaffs_bg_thread_fn,
2273 (void *)dev, "yaffs-bg-%d",
2276 if (IS_ERR(context->bg_thread)) {
2277 retval = PTR_ERR(context->bg_thread);
2278 context->bg_thread = NULL;
2279 context->bg_running = 0;
2284 static void yaffs_bg_stop(struct yaffs_dev *dev)
2286 struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
2288 ctxt->bg_running = 0;
2290 if (ctxt->bg_thread) {
2291 kthread_stop(ctxt->bg_thread);
2292 ctxt->bg_thread = NULL;
2296 static int yaffs_bg_thread_fn(void *data)
2301 static int yaffs_bg_start(struct yaffs_dev *dev)
2306 static void yaffs_bg_stop(struct yaffs_dev *dev)
2312 static void yaffs_flush_inodes(struct super_block *sb)
2315 struct yaffs_obj *obj;
2317 list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
2318 obj = yaffs_inode_to_obj(iptr);
2320 yaffs_trace(YAFFS_TRACE_OS,
2323 yaffs_flush_file(obj, 1, 0, 0);
2328 static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
2330 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2334 yaffs_flush_inodes(sb);
2335 yaffs_update_dirty_dirs(dev);
2336 yaffs_flush_whole_cache(dev, 1);
2338 yaffs_checkpoint_save(dev);
2341 static LIST_HEAD(yaffs_context_list);
2342 struct mutex yaffs_context_lock;
2344 static void yaffs_put_super(struct super_block *sb)
2346 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2347 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
2349 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2352 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2353 "Shutting down yaffs background thread");
2355 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2356 "yaffs background thread shut down");
2358 yaffs_gross_lock(dev);
2360 yaffs_flush_super(sb, 1);
2362 yaffs_deinitialise(dev);
2364 yaffs_gross_unlock(dev);
2366 mutex_lock(&yaffs_context_lock);
2367 list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
2368 mutex_unlock(&yaffs_context_lock);
2370 if (yaffs_dev_to_lc(dev)->spare_buffer) {
2371 kfree(yaffs_dev_to_lc(dev)->spare_buffer);
2372 yaffs_dev_to_lc(dev)->spare_buffer = NULL;
2377 yaffs_put_mtd_device(mtd);
2379 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
2380 "yaffs_put_super done");
2384 static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
2386 return yaffs_gc_control;
2390 #ifdef YAFFS_COMPILE_EXPORTFS
2392 static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
2393 uint32_t generation)
2395 return Y_IGET(sb, ino);
2398 static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
2399 struct fid *fid, int fh_len,
2402 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
2403 yaffs2_nfs_get_inode);
2406 static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
2407 struct fid *fid, int fh_len,
2410 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
2411 yaffs2_nfs_get_inode);
2414 struct dentry *yaffs2_get_parent(struct dentry *dentry)
2417 struct super_block *sb = dentry->d_inode->i_sb;
2418 struct dentry *parent = ERR_PTR(-ENOENT);
2419 struct inode *inode;
2420 unsigned long parent_ino;
2421 struct yaffs_obj *d_obj;
2422 struct yaffs_obj *parent_obj;
2424 d_obj = yaffs_inode_to_obj(dentry->d_inode);
2427 parent_obj = d_obj->parent;
2429 parent_ino = yaffs_get_obj_inode(parent_obj);
2430 inode = Y_IGET(sb, parent_ino);
2432 if (IS_ERR(inode)) {
2433 parent = ERR_CAST(inode);
2435 parent = d_obtain_alias(inode);
2436 if (!IS_ERR(parent)) {
2437 parent = ERR_PTR(-ENOMEM);
2447 /* Just declare a zero structure as a NULL value implies
2448 * using the default functions of exportfs.
2451 static struct export_operations yaffs_export_ops = {
2452 .fh_to_dentry = yaffs2_fh_to_dentry,
2453 .fh_to_parent = yaffs2_fh_to_parent,
2454 .get_parent = yaffs2_get_parent,
2459 static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
2461 /* Clear the association between the inode and
2462 * the struct yaffs_obj.
2464 obj->my_inode = NULL;
2465 yaffs_inode_to_obj_lv(inode) = NULL;
2467 /* If the object freeing was deferred, then the real
2469 * This should fix the inode inconsistency problem.
2471 yaffs_handle_defered_free(obj);
2474 #ifdef YAFFS_HAS_EVICT_INODE
2475 /* yaffs_evict_inode combines into one operation what was previously done in
2476 * yaffs_clear_inode() and yaffs_delete_inode()
2479 static void yaffs_evict_inode(struct inode *inode)
2481 struct yaffs_obj *obj;
2482 struct yaffs_dev *dev;
2485 obj = yaffs_inode_to_obj(inode);
2487 yaffs_trace(YAFFS_TRACE_OS,
2488 "yaffs_evict_inode: ino %d, count %d %s",
2489 (int)inode->i_ino, atomic_read(&inode->i_count),
2490 obj ? "object exists" : "null object");
2492 if (!inode->i_nlink && !is_bad_inode(inode))
2494 truncate_inode_pages(&inode->i_data, 0);
2495 Y_CLEAR_INODE(inode);
2497 if (deleteme && obj) {
2499 yaffs_gross_lock(dev);
2501 yaffs_gross_unlock(dev);
2505 yaffs_gross_lock(dev);
2506 yaffs_unstitch_obj(inode, obj);
2507 yaffs_gross_unlock(dev);
2512 /* clear is called to tell the fs to release any per-inode data it holds.
2513 * The object might still exist on disk and is just being thrown out of the cache
2514 * or else the object has actually been deleted and we're being called via
2516 * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
2519 static void yaffs_clear_inode(struct inode *inode)
2521 struct yaffs_obj *obj;
2522 struct yaffs_dev *dev;
2524 obj = yaffs_inode_to_obj(inode);
2526 yaffs_trace(YAFFS_TRACE_OS,
2527 "yaffs_clear_inode: ino %d, count %d %s",
2528 (int)inode->i_ino, atomic_read(&inode->i_count),
2529 obj ? "object exists" : "null object");
2533 yaffs_gross_lock(dev);
2534 yaffs_unstitch_obj(inode, obj);
2535 yaffs_gross_unlock(dev);
2540 /* delete is called when the link count is zero and the inode
2541 * is put (ie. nobody wants to know about it anymore, time to
2543 * NB Must call clear_inode()
2545 static void yaffs_delete_inode(struct inode *inode)
2547 struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
2548 struct yaffs_dev *dev;
2550 yaffs_trace(YAFFS_TRACE_OS,
2551 "yaffs_delete_inode: ino %d, count %d %s",
2552 (int)inode->i_ino, atomic_read(&inode->i_count),
2553 obj ? "object exists" : "null object");
2557 yaffs_gross_lock(dev);
2559 yaffs_gross_unlock(dev);
2561 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
2562 truncate_inode_pages(&inode->i_data, 0);
2571 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2572 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2574 struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
2575 struct super_block *sb = dentry->d_sb;
2576 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2577 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2579 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2581 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2583 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2586 yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
2588 yaffs_gross_lock(dev);
2590 buf->f_type = YAFFS_MAGIC;
2591 buf->f_bsize = sb->s_blocksize;
2592 buf->f_namelen = 255;
2594 if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
2595 /* Do this if chunk size is not a power of 2 */
2597 uint64_t bytes_in_dev;
2598 uint64_t bytes_free;
2602 ((dev->param.end_block - dev->param.start_block +
2603 1))) * ((uint64_t) (dev->param.chunks_per_block *
2604 dev->data_bytes_per_chunk));
2606 do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
2607 buf->f_blocks = bytes_in_dev;
2609 bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
2610 ((uint64_t) (dev->data_bytes_per_chunk));
2612 do_div(bytes_free, sb->s_blocksize);
2614 buf->f_bfree = bytes_free;
2616 } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
2619 (dev->param.end_block - dev->param.start_block + 1) *
2620 dev->param.chunks_per_block /
2621 (sb->s_blocksize / dev->data_bytes_per_chunk);
2623 yaffs_get_n_free_chunks(dev) /
2624 (sb->s_blocksize / dev->data_bytes_per_chunk);
2627 (dev->param.end_block - dev->param.start_block + 1) *
2628 dev->param.chunks_per_block *
2629 (dev->data_bytes_per_chunk / sb->s_blocksize);
2632 yaffs_get_n_free_chunks(dev) *
2633 (dev->data_bytes_per_chunk / sb->s_blocksize);
2638 buf->f_bavail = buf->f_bfree;
2640 yaffs_gross_unlock(dev);
2646 static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
2649 struct yaffs_dev *dev = yaffs_super_to_dev(sb);
2650 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
2651 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
2653 int dirty = yaffs_check_super_dirty(dev);
2655 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2656 "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
2658 dirty ? "dirty" : "clean",
2659 request_checkpoint ? "checkpoint requested" : "no checkpoint",
2660 oneshot_checkpoint ? " one-shot" : "");
2662 yaffs_gross_lock(dev);
2663 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
2664 oneshot_checkpoint) && !dev->is_checkpointed;
2666 if (dirty || do_checkpoint) {
2667 yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
2668 yaffs_clear_super_dirty(dev);
2669 if (oneshot_checkpoint)
2670 yaffs_auto_checkpoint &= ~4;
2672 yaffs_gross_unlock(dev);
2678 #ifdef YAFFS_HAS_WRITE_SUPER
2679 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2680 static void yaffs_write_super(struct super_block *sb)
2682 static int yaffs_write_super(struct super_block *sb)
2685 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2687 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2688 "yaffs_write_super %s",
2689 request_checkpoint ? " checkpt" : "");
2691 yaffs_do_sync_fs(sb, request_checkpoint);
2693 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
2699 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2700 static int yaffs_sync_fs(struct super_block *sb, int wait)
2702 static int yaffs_sync_fs(struct super_block *sb)
2705 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2707 yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2708 "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
2710 yaffs_do_sync_fs(sb, request_checkpoint);
2715 /* the function only is used to change dev->read_only when this file system
2718 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2721 struct mtd_info *mtd;
2722 struct yaffs_dev *dev = 0;
2724 /* Get the device */
2725 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2727 yaffs_trace(YAFFS_TRACE_ALWAYS,
2728 "MTD device #%u doesn't appear to exist",
2733 /* Check it's NAND */
2734 if (mtd->type != MTD_NANDFLASH) {
2735 yaffs_trace(YAFFS_TRACE_ALWAYS,
2736 "MTD device is not NAND it's type %d",
2741 read_only = ((*flags & MS_RDONLY) != 0);
2742 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2745 "yaffs: mtd is read only, setting superblock read only");
2746 *flags |= MS_RDONLY;
2749 dev = sb->s_fs_info;
2750 dev->read_only = read_only;
2755 static const struct super_operations yaffs_super_ops = {
2756 .statfs = yaffs_statfs,
2758 #ifndef YAFFS_USE_OWN_IGET
2759 .read_inode = yaffs_read_inode,
2761 #ifdef YAFFS_HAS_PUT_INODE
2762 .put_inode = yaffs_put_inode,
2764 .put_super = yaffs_put_super,
2765 #ifdef YAFFS_HAS_EVICT_INODE
2766 .evict_inode = yaffs_evict_inode,
2768 .delete_inode = yaffs_delete_inode,
2769 .clear_inode = yaffs_clear_inode,
2771 .sync_fs = yaffs_sync_fs,
2772 #ifdef YAFFS_HAS_WRITE_SUPER
2773 .write_super = yaffs_write_super,
2775 .remount_fs = yaffs_remount_fs,
2778 struct yaffs_options {
2780 int skip_checkpoint_read;
2781 int skip_checkpoint_write;
2784 int tags_ecc_overridden;
2785 int lazy_loading_enabled;
2786 int lazy_loading_overridden;
2787 int empty_lost_and_found;
2788 int empty_lost_and_found_overridden;
2789 int disable_summary;
2792 #define MAX_OPT_LEN 30
2793 static int yaffs_parse_options(struct yaffs_options *options,
2794 const char *options_str)
2796 char cur_opt[MAX_OPT_LEN + 1];
2800 /* Parse through the options which is a comma seperated list */
2802 while (options_str && *options_str && !error) {
2803 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2806 while (*options_str == ',')
2809 while (*options_str && *options_str != ',') {
2810 if (p < MAX_OPT_LEN) {
2811 cur_opt[p] = *options_str;
2817 if (!strcmp(cur_opt, "inband-tags")) {
2818 options->inband_tags = 1;
2819 } else if (!strcmp(cur_opt, "tags-ecc-off")) {
2820 options->tags_ecc_on = 0;
2821 options->tags_ecc_overridden = 1;
2822 } else if (!strcmp(cur_opt, "tags-ecc-on")) {
2823 options->tags_ecc_on = 1;
2824 options->tags_ecc_overridden = 1;
2825 } else if (!strcmp(cur_opt, "lazy-loading-off")) {
2826 options->lazy_loading_enabled = 0;
2827 options->lazy_loading_overridden = 1;
2828 } else if (!strcmp(cur_opt, "lazy-loading-on")) {
2829 options->lazy_loading_enabled = 1;
2830 options->lazy_loading_overridden = 1;
2831 } else if (!strcmp(cur_opt, "disable-summary")) {
2832 options->disable_summary = 1;
2833 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
2834 options->empty_lost_and_found = 0;
2835 options->empty_lost_and_found_overridden = 1;
2836 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
2837 options->empty_lost_and_found = 1;
2838 options->empty_lost_and_found_overridden = 1;
2839 } else if (!strcmp(cur_opt, "no-cache")) {
2840 options->no_cache = 1;
2841 } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
2842 options->skip_checkpoint_read = 1;
2843 } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
2844 options->skip_checkpoint_write = 1;
2845 } else if (!strcmp(cur_opt, "no-checkpoint")) {
2846 options->skip_checkpoint_read = 1;
2847 options->skip_checkpoint_write = 1;
2849 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2859 static struct dentry *yaffs_make_root(struct inode *inode)
2861 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
2862 struct dentry *root = d_alloc_root(inode);
2869 return d_make_root(inode);
2876 static struct super_block *yaffs_internal_read_super(int yaffs_version,
2877 struct super_block *sb,
2878 void *data, int silent)
2881 struct inode *inode = NULL;
2882 struct dentry *root;
2883 struct yaffs_dev *dev = 0;
2884 char devname_buf[BDEVNAME_SIZE + 1];
2885 struct mtd_info *mtd;
2887 char *data_str = (char *)data;
2888 struct yaffs_linux_context *context = NULL;
2889 struct yaffs_param *param;
2892 int inband_tags = 0;
2894 struct yaffs_options options;
2898 struct yaffs_linux_context *context_iterator;
2899 struct list_head *l;
2902 printk(KERN_INFO "yaffs: sb is NULL\n");
2906 sb->s_magic = YAFFS_MAGIC;
2907 sb->s_op = &yaffs_super_ops;
2908 sb->s_flags |= MS_NOATIME;
2910 read_only = ((sb->s_flags & MS_RDONLY) != 0);
2912 #ifdef YAFFS_COMPILE_EXPORTFS
2913 sb->s_export_op = &yaffs_export_ops;
2917 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2918 else if (!yaffs_devname(sb, devname_buf))
2919 printk(KERN_INFO "yaffs: devname is NULL\n");
2921 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2923 yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
2928 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2930 memset(&options, 0, sizeof(options));
2932 if (yaffs_parse_options(&options, data_str)) {
2933 /* Option parsing failed */
2937 sb->s_blocksize = PAGE_CACHE_SIZE;
2938 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2940 yaffs_trace(YAFFS_TRACE_OS,
2941 "yaffs_read_super: Using yaffs%d", yaffs_version);
2942 yaffs_trace(YAFFS_TRACE_OS,
2943 "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
2945 yaffs_trace(YAFFS_TRACE_ALWAYS,
2946 "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
2947 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2948 yaffs_devname(sb, devname_buf));
2950 /* Get the device */
2951 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2953 yaffs_trace(YAFFS_TRACE_ALWAYS,
2954 "yaffs: MTD device %u either not valid or unavailable",
2959 if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
2960 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
2964 /* Added NCB 26/5/2006 for completeness */
2965 if (yaffs_version == 2 && !options.inband_tags
2966 && WRITE_SIZE(mtd) == 512) {
2967 yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
2971 if (mtd->oobavail < sizeof(struct yaffs_packed_tags2) ||
2972 options.inband_tags)
2975 if(yaffs_verify_mtd(mtd, yaffs_version, inband_tags) < 0)
2978 /* OK, so if we got here, we have an MTD that's NAND and looks
2979 * like it has the right capabilities
2980 * Set the struct yaffs_dev up for mtd
2983 if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
2986 "yaffs: mtd is read only, setting superblock read only\n"
2988 sb->s_flags |= MS_RDONLY;
2991 dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
2992 context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
2994 if (!dev || !context) {
3000 /* Deep shit could not allocate device structure */
3001 yaffs_trace(YAFFS_TRACE_ALWAYS,
3002 "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
3006 memset(dev, 0, sizeof(struct yaffs_dev));
3007 param = &(dev->param);
3009 memset(context, 0, sizeof(struct yaffs_linux_context));
3010 dev->os_context = context;
3011 INIT_LIST_HEAD(&(context->context_list));
3013 context->super = sb;
3015 dev->read_only = read_only;
3017 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3018 sb->s_fs_info = dev;
3020 sb->u.generic_sbp = dev;
3024 dev->driver_context = mtd;
3025 param->name = mtd->name;
3027 /* Set up the memory size parameters.... */
3030 param->n_reserved_blocks = 5;
3031 param->n_caches = (options.no_cache) ? 0 : 10;
3032 param->inband_tags = inband_tags;
3034 param->enable_xattr = 1;
3035 if (options.lazy_loading_overridden)
3036 param->disable_lazy_load = !options.lazy_loading_enabled;
3038 param->defered_dir_update = 1;
3040 if (options.tags_ecc_overridden)
3041 param->no_tags_ecc = !options.tags_ecc_on;
3043 param->empty_lost_n_found = 1;
3044 param->refresh_period = 500;
3045 param->disable_summary = options.disable_summary;
3048 #ifdef CONFIG_YAFFS_DISABLE_BAD_BLOCK_MARKING
3049 param->disable_bad_block_marking = 1;
3051 if (options.empty_lost_and_found_overridden)
3052 param->empty_lost_n_found = options.empty_lost_and_found;
3054 /* ... and the functions. */
3055 if (yaffs_version == 2) {
3056 param->is_yaffs2 = 1;
3057 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3058 param->total_bytes_per_chunk = mtd->writesize;
3059 param->chunks_per_block = mtd->erasesize / mtd->writesize;
3061 param->total_bytes_per_chunk = mtd->oobblock;
3062 param->chunks_per_block = mtd->erasesize / mtd->oobblock;
3064 n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
3066 param->start_block = 0;
3067 param->end_block = n_blocks - 1;
3069 param->is_yaffs2 = 0;
3070 n_blocks = YCALCBLOCKS(mtd->size,
3071 YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
3073 param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
3074 param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
3077 param->start_block = 0;
3078 param->end_block = n_blocks - 1;
3080 yaffs_mtd_drv_install(dev);
3082 param->sb_dirty_fn = yaffs_set_super_dirty;
3083 param->gc_control_fn = yaffs_gc_control_callback;
3085 yaffs_dev_to_lc(dev)->super = sb;
3087 param->use_nand_ecc = 1;
3089 param->skip_checkpt_rd = options.skip_checkpoint_read;
3090 param->skip_checkpt_wr = options.skip_checkpoint_write;
3092 mutex_lock(&yaffs_context_lock);
3093 /* Get a mount id */
3095 for (mount_id = 0; !found; mount_id++) {
3097 list_for_each(l, &yaffs_context_list) {
3099 list_entry(l, struct yaffs_linux_context,
3101 if (context_iterator->mount_id == mount_id)
3105 context->mount_id = mount_id;
3107 list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
3108 &yaffs_context_list);
3109 mutex_unlock(&yaffs_context_lock);
3111 /* Directory search handling... */
3112 INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
3113 param->remove_obj_fn = yaffs_remove_obj_callback;
3115 mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
3117 yaffs_gross_lock(dev);
3119 err = yaffs_guts_initialise(dev);
3121 yaffs_trace(YAFFS_TRACE_OS,
3122 "yaffs_read_super: guts initialised %s",
3123 (err == YAFFS_OK) ? "OK" : "FAILED");
3125 if (err == YAFFS_OK)
3126 yaffs_bg_start(dev);
3128 if (!context->bg_thread)
3129 param->defered_dir_update = 0;
3131 sb->s_maxbytes = yaffs_max_file_size(dev);
3133 /* Release lock before yaffs_get_inode() */
3134 yaffs_gross_unlock(dev);
3136 /* Create root inode */
3137 if (err == YAFFS_OK)
3138 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
3143 inode->i_op = &yaffs_dir_inode_operations;
3144 inode->i_fop = &yaffs_dir_operations;
3146 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
3148 root = yaffs_make_root(inode);
3154 if(!dev->is_checkpointed)
3155 yaffs_set_super_dirty(dev);
3157 yaffs_trace(YAFFS_TRACE_ALWAYS,
3158 "yaffs_read_super: is_checkpointed %d",
3159 dev->is_checkpointed);
3161 yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
3165 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3166 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3169 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3172 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3173 static struct dentry *yaffs_mount(struct file_system_type *fs_type, int flags,
3174 const char *dev_name, void *data)
3176 return mount_bdev(fs_type, flags, dev_name, data, yaffs_internal_read_super_mtd);
3178 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3179 static int yaffs_read_super(struct file_system_type *fs,
3180 int flags, const char *dev_name,
3181 void *data, struct vfsmount *mnt)
3184 return get_sb_bdev(fs, flags, dev_name, data,
3185 yaffs_internal_read_super_mtd, mnt);
3188 static struct super_block *yaffs_read_super(struct file_system_type *fs,
3189 int flags, const char *dev_name,
3193 return get_sb_bdev(fs, flags, dev_name, data,
3194 yaffs_internal_read_super_mtd);
3198 static struct file_system_type yaffs_fs_type = {
3199 .owner = THIS_MODULE,
3201 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3202 .mount = yaffs_mount,
3204 .get_sb = yaffs_read_super,
3206 .kill_sb = kill_block_super,
3207 .fs_flags = FS_REQUIRES_DEV,
3210 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3213 return yaffs_internal_read_super(1, sb, data, silent);
3216 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3221 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3222 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3225 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3228 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3229 static struct dentry *yaffs2_mount(struct file_system_type *fs_type, int flags,
3230 const char *dev_name, void *data)
3232 return mount_bdev(fs_type, flags, dev_name, data, yaffs2_internal_read_super_mtd);
3234 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3235 static int yaffs2_read_super(struct file_system_type *fs,
3236 int flags, const char *dev_name, void *data,
3237 struct vfsmount *mnt)
3239 return get_sb_bdev(fs, flags, dev_name, data,
3240 yaffs2_internal_read_super_mtd, mnt);
3243 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3244 int flags, const char *dev_name,
3248 return get_sb_bdev(fs, flags, dev_name, data,
3249 yaffs2_internal_read_super_mtd);
3253 static struct file_system_type yaffs2_fs_type = {
3254 .owner = THIS_MODULE,
3256 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
3257 .mount = yaffs2_mount,
3259 .get_sb = yaffs2_read_super,
3261 .kill_sb = kill_block_super,
3262 .fs_flags = FS_REQUIRES_DEV,
3265 static struct super_block *yaffs2_read_super(struct super_block *sb,
3266 void *data, int silent)
3268 return yaffs_internal_read_super(2, sb, data, silent);
3271 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3276 static struct proc_dir_entry *my_proc_entry;
3278 static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
3280 struct yaffs_param *param = &dev->param;
3283 yaffs_count_blocks_by_state(dev,bs);
3285 buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
3286 buf += sprintf(buf, "end_block............ %d\n", param->end_block);
3287 buf += sprintf(buf, "total_bytes_per_chunk %d\n",
3288 param->total_bytes_per_chunk);
3289 buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
3290 buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
3291 buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
3292 buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
3293 buf += sprintf(buf, "empty_lost_n_found... %d\n",
3294 param->empty_lost_n_found);
3295 buf += sprintf(buf, "disable_lazy_load.... %d\n",
3296 param->disable_lazy_load);
3297 buf += sprintf(buf, "disable_bad_block_mrk %d\n",
3298 param->disable_bad_block_marking);
3299 buf += sprintf(buf, "refresh_period....... %d\n",
3300 param->refresh_period);
3301 buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
3302 buf += sprintf(buf, "n_reserved_blocks.... %d\n",
3303 param->n_reserved_blocks);
3304 buf += sprintf(buf, "always_check_erased.. %d\n",
3305 param->always_check_erased);
3306 buf += sprintf(buf, "\n");
3307 buf += sprintf(buf, "block count by state\n");
3308 buf += sprintf(buf, "0:%d 1:%d 2:%d 3:%d 4:%d\n",
3309 bs[0], bs[1], bs[2], bs[3], bs[4]);
3310 buf += sprintf(buf, "5:%d 6:%d 7:%d 8:%d 9:%d\n",
3311 bs[5], bs[6], bs[7], bs[8], bs[9]);
3316 static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
3318 buf += sprintf(buf, "max file size....... %lld\n",
3319 (long long) yaffs_max_file_size(dev));
3320 buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
3321 dev->data_bytes_per_chunk);
3322 buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
3323 buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
3324 buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
3325 buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
3326 dev->blocks_in_checkpt);
3327 buf += sprintf(buf, "\n");
3328 buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
3329 buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
3330 buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
3331 buf += sprintf(buf, "\n");
3332 buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
3333 buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
3334 buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
3335 buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
3336 buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
3337 buf += sprintf(buf, "passive_gc_count..... %u\n",
3338 dev->passive_gc_count);
3339 buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
3340 dev->oldest_dirty_gc_count);
3341 buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
3342 buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
3343 buf += sprintf(buf, "n_retried_writes..... %u\n",
3344 dev->n_retried_writes);
3345 buf += sprintf(buf, "n_retired_blocks..... %u\n",
3346 dev->n_retired_blocks);
3347 buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
3348 buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
3349 buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
3350 dev->n_tags_ecc_fixed);
3351 buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
3352 dev->n_tags_ecc_unfixed);
3353 buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
3354 buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
3355 buf += sprintf(buf, "n_unlinked_files..... %u\n",
3356 dev->n_unlinked_files);
3357 buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
3358 buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
3359 buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
3360 buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
3365 static int yaffs_proc_read(char *page,
3367 off_t offset, int count, int *eof, void *data)
3369 struct list_head *item;
3374 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3375 * We use 'offset' (*ppos) to indicate where we are in dev_list.
3376 * This also assumes the user has posted a read buffer large
3377 * enough to hold the complete output; but that's life in /proc.
3382 /* Print header first */
3385 sprintf(buf, "Multi-version YAFFS\n");
3387 buf += sprintf(buf, "\n");
3391 mutex_lock(&yaffs_context_lock);
3393 /* Locate and print the Nth entry. Order N-squared but N is small. */
3394 list_for_each(item, &yaffs_context_list) {
3395 struct yaffs_linux_context *dc =
3396 list_entry(item, struct yaffs_linux_context,
3398 struct yaffs_dev *dev = dc->dev;
3400 if (n < (step & ~1)) {
3404 if ((step & 1) == 0) {
3406 sprintf(buf, "\nDevice %d \"%s\"\n", n,
3408 buf = yaffs_dump_dev_part0(buf, dev);
3410 buf = yaffs_dump_dev_part1(buf, dev);
3415 mutex_unlock(&yaffs_context_lock);
3418 return buf - page < count ? buf - page : count;
3422 * Set the verbosity of the warnings and error messages.
3424 * Note that the names can only be a..z or _ with the current code.
3429 unsigned mask_bitfield;
3431 {"allocate", YAFFS_TRACE_ALLOCATE},
3432 {"always", YAFFS_TRACE_ALWAYS},
3433 {"background", YAFFS_TRACE_BACKGROUND},
3434 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3435 {"buffers", YAFFS_TRACE_BUFFERS},
3436 {"bug", YAFFS_TRACE_BUG},
3437 {"checkpt", YAFFS_TRACE_CHECKPOINT},
3438 {"deletion", YAFFS_TRACE_DELETION},
3439 {"erase", YAFFS_TRACE_ERASE},
3440 {"error", YAFFS_TRACE_ERROR},
3441 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3442 {"gc", YAFFS_TRACE_GC},
3443 {"lock", YAFFS_TRACE_LOCK},
3444 {"mtd", YAFFS_TRACE_MTD},
3445 {"nandaccess", YAFFS_TRACE_NANDACCESS},
3446 {"os", YAFFS_TRACE_OS},
3447 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3448 {"scan", YAFFS_TRACE_SCAN},
3449 {"mount", YAFFS_TRACE_MOUNT},
3450 {"tracing", YAFFS_TRACE_TRACING},
3451 {"sync", YAFFS_TRACE_SYNC},
3452 {"write", YAFFS_TRACE_WRITE},
3453 {"verify", YAFFS_TRACE_VERIFY},
3454 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3455 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3456 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3457 {"all", 0xffffffff},
3462 #define MAX_MASK_NAME_LENGTH 40
3463 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
3464 unsigned long count)
3466 unsigned rg = 0, mask_bitfield;
3470 char substring[MAX_MASK_NAME_LENGTH + 1];
3476 rg = yaffs_trace_mask;
3478 while (!done && (pos < count)) {
3480 while ((pos < count) && isspace(buf[pos]))
3497 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3499 if (end > buf + pos) {
3500 mask_name = "numeral";
3501 len = end - (buf + pos);
3505 for (x = buf + pos, i = 0;
3506 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3507 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3509 substring[i] = '\0';
3511 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3512 if (strcmp(substring, mask_flags[i].mask_name)
3514 mask_name = mask_flags[i].mask_name;
3516 mask_flags[i].mask_bitfield;
3523 if (mask_name != NULL) {
3527 rg &= ~mask_bitfield;
3530 rg |= mask_bitfield;
3536 rg |= mask_bitfield;
3542 yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
3544 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
3546 if (rg & YAFFS_TRACE_ALWAYS) {
3547 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3549 flag = ((rg & mask_flags[i].mask_bitfield) ==
3550 mask_flags[i].mask_bitfield) ? '+' : '-';
3551 printk(KERN_DEBUG "%c%s\n", flag,
3552 mask_flags[i].mask_name);
3559 /* Debug strings are of the form:
3560 * .bnnn print info on block n
3561 * .cobjn,chunkn print nand chunk id for objn:chunkn
3564 static int yaffs_proc_debug_write(struct file *file, const char *buf,
3565 unsigned long count)
3574 struct list_head *item;
3576 memset(str, 0, sizeof(str));
3577 memcpy(str, buf, min((size_t)count, sizeof(str) -1));
3585 while (*p1 && *p1 != ',') {
3591 p0_val = simple_strtol(p0, NULL, 0);
3592 p1_val = simple_strtol(p1, NULL, 0);
3595 mutex_lock(&yaffs_context_lock);
3597 /* Locate and print the Nth entry. Order N-squared but N is small. */
3598 list_for_each(item, &yaffs_context_list) {
3599 struct yaffs_linux_context *dc =
3600 list_entry(item, struct yaffs_linux_context,
3602 struct yaffs_dev *dev = dc->dev;
3605 struct yaffs_block_info *bi;
3607 bi = yaffs_get_block_info(dev,p0_val);
3610 printk("Block %d: state %d, retire %d, use %d, seq %d\n",
3611 (int)p0_val, bi->block_state,
3612 bi->needs_retiring, bi->pages_in_use,
3615 } else if (cmd == 'c') {
3616 struct yaffs_obj *obj;
3619 obj = yaffs_find_by_number(dev, p0_val);
3621 printk("No obj %d\n", (int)p0_val);
3624 nand_chunk = obj->hdr_chunk;
3627 yaffs_find_chunk_in_file(obj,
3629 printk("Nand chunk for %d:%d is %d\n",
3630 (int)p0_val, (int)p1_val, nand_chunk);
3635 mutex_unlock(&yaffs_context_lock);
3641 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0))
3642 static int yaffs_proc_write(struct file *file, const char *buf,
3643 unsigned long count, void *ppos)
3645 static ssize_t yaffs_proc_write(struct file *file, const char __user *buf,
3646 size_t count, loff_t *ppos)
3650 return yaffs_proc_debug_write(file, buf, count);
3651 return yaffs_proc_write_trace_options(file, buf, count);
3654 /* Stuff to handle installation of file systems */
3655 struct file_system_to_install {
3656 struct file_system_type *fst;
3660 static struct file_system_to_install fs_to_install[] = {
3661 {&yaffs_fs_type, 0},
3662 {&yaffs2_fs_type, 0},
3667 #ifdef YAFFS_NEW_PROCFS
3668 static int yaffs_proc_show(struct seq_file *m, void *v)
3670 /* FIXME: Unify in a better way? */
3675 len = yaffs_proc_read(buffer, &start, 0, sizeof(buffer), NULL, NULL);
3676 seq_puts(m, buffer);
3680 static int yaffs_proc_open(struct inode *inode, struct file *file)
3682 return single_open(file, yaffs_proc_show, NULL);
3685 static struct file_operations procfs_ops = {
3686 .owner = THIS_MODULE,
3687 .open = yaffs_proc_open,
3689 .write = yaffs_proc_write,
3690 .release = single_release,
3693 static int yaffs_procfs_init(void)
3695 /* Install the proc_fs entries */
3696 my_proc_entry = proc_create("yaffs",
3701 if (my_proc_entry) {
3711 static int yaffs_procfs_init(void)
3713 /* Install the proc_fs entries */
3714 my_proc_entry = create_proc_entry("yaffs",
3715 S_IRUGO | S_IFREG, YPROC_ROOT);
3717 if (my_proc_entry) {
3718 my_proc_entry->write_proc = yaffs_proc_write;
3719 my_proc_entry->read_proc = yaffs_proc_read;
3720 my_proc_entry->data = NULL;
3730 static int __init init_yaffs_fs(void)
3733 struct file_system_to_install *fsinst;
3735 yaffs_trace(YAFFS_TRACE_ALWAYS,
3736 "yaffs Installing.");
3738 mutex_init(&yaffs_context_lock);
3740 error = yaffs_procfs_init();
3744 /* Now add the file system entries */
3746 fsinst = fs_to_install;
3748 while (fsinst->fst && !error) {
3749 error = register_filesystem(fsinst->fst);
3751 fsinst->installed = 1;
3755 /* Any errors? uninstall */
3757 fsinst = fs_to_install;
3759 while (fsinst->fst) {
3760 if (fsinst->installed) {
3761 unregister_filesystem(fsinst->fst);
3762 fsinst->installed = 0;
3771 static void __exit exit_yaffs_fs(void)
3774 struct file_system_to_install *fsinst;
3776 yaffs_trace(YAFFS_TRACE_ALWAYS,
3779 remove_proc_entry("yaffs", YPROC_ROOT);
3781 fsinst = fs_to_install;
3783 while (fsinst->fst) {
3784 if (fsinst->installed) {
3785 unregister_filesystem(fsinst->fst);
3786 fsinst->installed = 0;
3792 module_init(init_yaffs_fs)
3793 module_exit(exit_yaffs_fs)
3795 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3796 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
3797 MODULE_LICENSE("GPL");