2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * Luc van OostenRyck for numerous patches.
10 * Nick Bane for numerous patches.
11 * Nick Bane for 2.5/2.6 integration.
12 * Andras Toth for mknod rdev issue.
13 * Michael Fischer for finding the problem with inode inconsistency.
14 * Some code bodily lifted from JFFS
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
23 * This is the file system front-end to YAFFS that hooks it up to
27 * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
29 * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
31 * >> inode->u.generic_ip points to the associated yaffs_Object.
35 * NB There are two variants of Linux VFS glue code. This variant supports
36 * a single version and should not include any multi-version code.
38 #include <linux/version.h>
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/slab.h>
43 #include <linux/init.h>
45 #include <linux/proc_fs.h>
46 #include <linux/smp_lock.h>
47 #include <linux/pagemap.h>
48 #include <linux/mtd/mtd.h>
49 #include <linux/interrupt.h>
50 #include <linux/string.h>
51 #include <linux/ctype.h>
52 #include <linux/namei.h>
53 #include <linux/exportfs.h>
54 #include <linux/kthread.h>
55 #include <linux/delay.h>
56 #include <linux/freezer.h>
58 #include <asm/div64.h>
61 #include <linux/statfs.h>
63 #define UnlockPage(p) unlock_page(p)
64 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
66 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
68 #define YPROC_ROOT NULL
70 #define Y_INIT_TIMER(a) init_timer_on_stack(a)
72 #define WRITE_SIZE_STR "writesize"
73 #define WRITE_SIZE(mtd) ((mtd)->writesize)
76 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
78 uint64_t result = partition_size;
79 do_div(result, block_size);
80 return (uint32_t)result;
83 #include <linux/uaccess.h>
84 #include <linux/mtd/mtd.h>
87 #include "yaffs_trace.h"
88 #include "yaffs_guts.h"
90 #include "yaffs_linux.h"
92 #include "yaffs_mtdif.h"
93 #include "yaffs_mtdif1.h"
94 #include "yaffs_mtdif2.h"
96 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
97 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
98 unsigned int yaffs_auto_checkpoint = 1;
99 unsigned int yaffs_gc_control = 1;
100 unsigned int yaffs_bg_enable = 1;
102 /* Module Parameters */
103 module_param(yaffs_traceMask, uint, 0644);
104 module_param(yaffs_wr_attempts, uint, 0644);
105 module_param(yaffs_auto_checkpoint, uint, 0644);
106 module_param(yaffs_gc_control, uint, 0644);
107 module_param(yaffs_bg_enable, uint, 0644);
109 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
111 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
113 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
114 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
115 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
116 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
119 #define update_dir_time(dir) do {\
120 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
123 static void yaffs_put_super(struct super_block *sb);
125 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
127 static ssize_t yaffs_hold_space(struct file *f);
128 static void yaffs_release_space(struct file *f);
130 static int yaffs_file_flush(struct file *file, fl_owner_t id);
132 static int yaffs_sync_object(struct file *file, int datasync);
134 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
136 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
137 struct nameidata *n);
138 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
139 struct nameidata *n);
140 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
141 struct dentry *dentry);
142 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
143 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
144 const char *symname);
145 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
147 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
149 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
150 struct inode *new_dir, struct dentry *new_dentry);
151 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
153 static int yaffs_sync_fs(struct super_block *sb, int wait);
154 static void yaffs_write_super(struct super_block *sb);
156 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
158 #ifdef YAFFS_HAS_PUT_INODE
159 static void yaffs_put_inode(struct inode *inode);
162 static void yaffs_evict_inode(struct inode *);
164 static int yaffs_readpage(struct file *file, struct page *page);
165 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
167 #ifdef CONFIG_YAFFS_XATTR
168 int yaffs_setxattr(struct dentry *dentry, const char *name,
169 const void *value, size_t size, int flags);
170 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
172 int yaffs_removexattr(struct dentry *dentry, const char *name);
173 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
177 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
178 loff_t pos, unsigned len, unsigned flags,
179 struct page **pagep, void **fsdata);
180 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
181 loff_t pos, unsigned len, unsigned copied,
182 struct page *pg, void *fsdadata);
184 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
186 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
187 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
189 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev);
191 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
193 static int yaffs_vfs_setattr(struct inode *, struct iattr *);
196 static struct address_space_operations yaffs_file_address_operations = {
197 .readpage = yaffs_readpage,
198 .writepage = yaffs_writepage,
199 .write_begin = yaffs_write_begin,
200 .write_end = yaffs_write_end,
204 static const struct file_operations yaffs_file_operations = {
205 .read = do_sync_read,
206 .write = do_sync_write,
207 .aio_read = generic_file_aio_read,
208 .aio_write = generic_file_aio_write,
209 .mmap = generic_file_mmap,
210 .flush = yaffs_file_flush,
211 .fsync = yaffs_sync_object,
212 .splice_read = generic_file_splice_read,
213 .splice_write = generic_file_splice_write,
214 .llseek = generic_file_llseek,
219 static const struct inode_operations yaffs_file_inode_operations = {
220 .setattr = yaffs_setattr,
221 #ifdef CONFIG_YAFFS_XATTR
222 .setxattr = yaffs_setxattr,
223 .getxattr = yaffs_getxattr,
224 .listxattr = yaffs_listxattr,
225 .removexattr = yaffs_removexattr,
229 static const struct inode_operations yaffs_symlink_inode_operations = {
230 .readlink = yaffs_readlink,
231 .follow_link = yaffs_follow_link,
232 .put_link = yaffs_put_link,
233 .setattr = yaffs_setattr,
234 #ifdef CONFIG_YAFFS_XATTR
235 .setxattr = yaffs_setxattr,
236 .getxattr = yaffs_getxattr,
237 .listxattr = yaffs_listxattr,
238 .removexattr = yaffs_removexattr,
242 static const struct inode_operations yaffs_dir_inode_operations = {
243 .create = yaffs_create,
244 .lookup = yaffs_lookup,
246 .unlink = yaffs_unlink,
247 .symlink = yaffs_symlink,
248 .mkdir = yaffs_mkdir,
249 .rmdir = yaffs_unlink,
250 .mknod = yaffs_mknod,
251 .rename = yaffs_rename,
252 .setattr = yaffs_setattr,
253 #ifdef CONFIG_YAFFS_XATTR
254 .setxattr = yaffs_setxattr,
255 .getxattr = yaffs_getxattr,
256 .listxattr = yaffs_listxattr,
257 .removexattr = yaffs_removexattr,
261 static const struct file_operations yaffs_dir_operations = {
262 .read = generic_read_dir,
263 .readdir = yaffs_readdir,
264 .fsync = yaffs_sync_object,
265 .llseek = yaffs_dir_llseek,
268 static const struct super_operations yaffs_super_ops = {
269 .statfs = yaffs_statfs,
270 #ifdef YAFFS_HAS_PUT_INODE
271 .put_inode = yaffs_put_inode,
273 .put_super = yaffs_put_super,
274 .evict_inode = yaffs_evict_inode,
275 .sync_fs = yaffs_sync_fs,
276 .write_super = yaffs_write_super,
280 static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
282 setattr_copy(inode,attr);
286 static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
288 truncate_setsize(inode,newsize);
292 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
294 return yaffs_gc_control;
297 static void yaffs_GrossLock(yaffs_Device *dev)
299 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
300 down(&(yaffs_DeviceToLC(dev)->grossLock));
301 T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
304 static void yaffs_GrossUnlock(yaffs_Device *dev)
306 T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
307 up(&(yaffs_DeviceToLC(dev)->grossLock));
312 /* ExportFS support */
313 static struct inode *
314 yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
316 return Y_IGET(sb, ino);
319 static struct dentry *
320 yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
322 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
325 static struct dentry *
326 yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
328 return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
331 struct dentry *yaffs2_get_parent(struct dentry *dentry)
334 struct super_block *sb = dentry->d_inode->i_sb;
335 struct dentry *parent = ERR_PTR(-ENOENT);
337 unsigned long parent_ino;
339 yaffs_Object *parent_obj;
341 d_obj = yaffs_InodeToObject(dentry->d_inode);
344 parent_obj = d_obj->parent;
346 parent_ino = yaffs_GetObjectInode(parent_obj);
347 inode = Y_IGET(sb, parent_ino);
350 parent = ERR_CAST(inode);
352 parent = d_obtain_alias(inode);
353 if (!IS_ERR(parent)) {
354 parent = ERR_PTR(-ENOMEM);
364 /* Just declare a zero structure as a NULL value implies
365 * using the default functions of exportfs.
368 static struct export_operations yaffs_export_ops =
370 .fh_to_dentry = yaffs2_fh_to_dentry,
371 .fh_to_parent = yaffs2_fh_to_parent,
372 .get_parent = yaffs2_get_parent,
375 /*-----------------------------------------------------------------*/
376 /* Directory search context allows us to unlock access to yaffs during
377 * filldir without causing problems with the directory being modified.
378 * This is similar to the tried and tested mechanism used in yaffs direct.
380 * A search context iterates along a doubly linked list of siblings in the
381 * directory. If the iterating object is deleted then this would corrupt
382 * the list iteration, likely causing a crash. The search context avoids
383 * this by using the removeObjectCallback to move the search context to the
384 * next object before the object is deleted.
386 * Many readdirs (and thus seach conexts) may be alive simulateously so
387 * each yaffs_Device has a list of these.
389 * A seach context lives for the duration of a readdir.
391 * All these functions must be called while yaffs is locked.
394 struct yaffs_SearchContext {
396 yaffs_Object *dirObj;
397 yaffs_Object *nextReturn;
398 struct ylist_head others;
402 * yaffs_NewSearch() creates a new search context, initialises it and
403 * adds it to the device's search context list.
405 * Called at start of readdir.
407 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
409 yaffs_Device *dev = dir->myDev;
410 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
414 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
415 sc->nextReturn = NULL;
417 sc->nextReturn = ylist_entry(
418 dir->variant.directoryVariant.children.next,
419 yaffs_Object,siblings);
420 YINIT_LIST_HEAD(&sc->others);
421 ylist_add(&sc->others,&(yaffs_DeviceToLC(dev)->searchContexts));
427 * yaffs_EndSearch() disposes of a search context and cleans up.
429 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
432 ylist_del(&sc->others);
438 * yaffs_SearchAdvance() moves a search context to the next object.
439 * Called when the search iterates or when an object removal causes
440 * the search context to be moved to the next object.
442 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
447 if( sc->nextReturn == NULL ||
448 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
449 sc->nextReturn = NULL;
451 struct ylist_head *next = sc->nextReturn->siblings.next;
453 if( next == &sc->dirObj->variant.directoryVariant.children)
454 sc->nextReturn = NULL; /* end of list */
456 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
461 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
462 * We check open search contexts and advance any which are currently
463 * on the object being iterated.
465 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
468 struct ylist_head *i;
469 struct yaffs_SearchContext *sc;
470 struct ylist_head *search_contexts = &(yaffs_DeviceToLC(obj->myDev)->searchContexts);
473 /* Iterate through the directory search contexts.
474 * If any are currently on the object being removed, then advance
475 * the search context to the next object to prevent a hanging pointer.
477 ylist_for_each(i, search_contexts) {
479 sc = ylist_entry(i, struct yaffs_SearchContext,others);
480 if(sc->nextReturn == obj)
481 yaffs_SearchAdvance(sc);
488 /*-----------------------------------------------------------------*/
490 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
493 unsigned char *alias;
496 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
498 yaffs_GrossLock(dev);
500 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
502 yaffs_GrossUnlock(dev);
507 ret = vfs_readlink(dentry, buffer, buflen, alias);
512 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
514 unsigned char *alias;
516 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
518 yaffs_GrossLock(dev);
520 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
521 yaffs_GrossUnlock(dev);
528 nd_set_link(nd, alias);
534 void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) {
538 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
542 * Lookup is used to find objects in the fs
545 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
549 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
551 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
553 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
554 yaffs_GrossLock(dev);
557 (TSTR("yaffs_lookup for %d:%s\n"),
558 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
560 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
561 dentry->d_name.name);
563 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
565 /* Can't hold gross lock when calling yaffs_get_inode() */
566 if(current != yaffs_DeviceToLC(dev)->readdirProcess)
567 yaffs_GrossUnlock(dev);
571 (TSTR("yaffs_lookup found %d\n"), obj->objectId));
573 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
577 (TSTR("yaffs_loookup dentry \n")));
578 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
579 * d_add even if NULL inode */
581 /*dget(dentry); // try to solve directory bug */
582 d_add(dentry, inode);
590 T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
594 /* added NCB for 2.5/6 compatability - forces add even if inode is
595 * NULL which creates dentry hash */
596 d_add(dentry, inode);
602 #ifdef YAFFS_HAS_PUT_INODE
604 /* For now put inode is just for debugging
605 * Put inode is called when the inode **structure** is put.
607 static void yaffs_put_inode(struct inode *inode)
610 (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
611 atomic_read(&inode->i_count)));
617 static void yaffs_UnstitchObject(struct inode *inode, yaffs_Object *obj)
619 /* Clear the association between the inode and
623 yaffs_InodeToObjectLV(inode) = NULL;
625 /* If the object freeing was deferred, then the real
627 * This should fix the inode inconsistency problem.
629 yaffs_HandleDeferedFree(obj);
632 /* yaffs_evict_inode combines into one operation what was previously done in
633 * yaffs_clear_inode() and yaffs_delete_inode()
636 static void yaffs_evict_inode( struct inode *inode)
642 obj = yaffs_InodeToObject(inode);
645 (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
646 atomic_read(&inode->i_count),
647 obj ? "object exists" : "null object"));
649 if (!inode->i_nlink && !is_bad_inode(inode))
651 truncate_inode_pages(&inode->i_data,0);
652 end_writeback(inode);
656 yaffs_GrossLock(dev);
657 yaffs_DeleteObject(obj);
658 yaffs_GrossUnlock(dev);
662 yaffs_GrossLock(dev);
663 yaffs_UnstitchObject(inode,obj);
664 yaffs_GrossUnlock(dev);
671 static int yaffs_file_flush(struct file *file, fl_owner_t id)
673 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
675 yaffs_Device *dev = obj->myDev;
678 (TSTR("yaffs_file_flush object %d (%s)\n"), obj->objectId,
679 obj->dirty ? "dirty" : "clean"));
681 yaffs_GrossLock(dev);
683 yaffs_FlushFile(obj, 1, 0);
685 yaffs_GrossUnlock(dev);
690 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
692 /* Lifted from jffs2 */
695 unsigned char *pg_buf;
701 (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
702 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
703 (unsigned)PAGE_CACHE_SIZE));
705 obj = yaffs_DentryToObject(f->f_dentry);
709 BUG_ON(!PageLocked(pg));
712 /* FIXME: Can kmap fail? */
714 yaffs_GrossLock(dev);
716 ret = yaffs_ReadDataFromFile(obj, pg_buf,
717 pg->index << PAGE_CACHE_SHIFT,
720 yaffs_GrossUnlock(dev);
726 ClearPageUptodate(pg);
733 flush_dcache_page(pg);
736 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
740 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
742 int ret = yaffs_readpage_nolock(f, pg);
747 static int yaffs_readpage(struct file *f, struct page *pg)
751 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
752 ret=yaffs_readpage_unlock(f, pg);
753 T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
757 /* writepage inspired by/stolen from smbfs */
759 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
762 struct address_space *mapping = page->mapping;
764 unsigned long end_index;
773 inode = mapping->host;
776 i_size = i_size_read(inode);
778 end_index = i_size >> PAGE_CACHE_SHIFT;
780 if(page->index < end_index)
781 nBytes = PAGE_CACHE_SIZE;
783 nBytes = i_size & (PAGE_CACHE_SIZE -1);
785 if (page->index > end_index || !nBytes) {
787 (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
788 (unsigned)(page->index << PAGE_CACHE_SHIFT),
789 (unsigned)inode->i_size));
791 (TSTR(" -> don't care!!\n")));
793 zero_user_segment(page,0,PAGE_CACHE_SIZE);
794 set_page_writeback(page);
796 end_page_writeback(page);
801 if(nBytes != PAGE_CACHE_SIZE)
802 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
808 obj = yaffs_InodeToObject(inode);
810 yaffs_GrossLock(dev);
813 (TSTR("yaffs_writepage at %08x, size %08x\n"),
814 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
816 (TSTR("writepag0: obj = %05x, ino = %05x\n"),
817 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
819 nWritten = yaffs_WriteDataToFile(obj, buffer,
820 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
822 yaffs_MarkSuperBlockDirty(dev);
825 (TSTR("writepag1: obj = %05x, ino = %05x\n"),
826 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
828 yaffs_GrossUnlock(dev);
831 set_page_writeback(page);
833 end_page_writeback(page);
836 return (nWritten == nBytes) ? 0 : -ENOSPC;
840 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
841 loff_t pos, unsigned len, unsigned flags,
842 struct page **pagep, void **fsdata)
844 struct page *pg = NULL;
845 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
851 pg = grab_cache_page_write_begin(mapping, index, flags);
859 (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
860 (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
863 space_held = yaffs_hold_space(filp);
870 /* Update page if required */
872 if (!Page_Uptodate(pg))
873 ret = yaffs_readpage_nolock(filp, pg);
878 /* Happy path return */
879 T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
885 (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
887 yaffs_release_space(filp);
890 page_cache_release(pg);
896 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
897 loff_t pos, unsigned len, unsigned copied,
898 struct page *pg, void *fsdadata)
902 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
905 addr = kva + offset_into_page;
908 ("yaffs_write_end addr %p pos %x nBytes %d\n",
909 addr,(unsigned)pos, copied));
911 ret = yaffs_file_write(filp, addr, copied, &pos);
915 (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
924 yaffs_release_space(filp);
926 page_cache_release(pg);
931 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
936 /* Check mode against the variant type and attempt to repair if broken. */
937 __u32 mode = obj->yst_mode;
938 switch (obj->variantType) {
939 case YAFFS_OBJECT_TYPE_FILE:
940 if (!S_ISREG(mode)) {
941 obj->yst_mode &= ~S_IFMT;
942 obj->yst_mode |= S_IFREG;
946 case YAFFS_OBJECT_TYPE_SYMLINK:
947 if (!S_ISLNK(mode)) {
948 obj->yst_mode &= ~S_IFMT;
949 obj->yst_mode |= S_IFLNK;
953 case YAFFS_OBJECT_TYPE_DIRECTORY:
954 if (!S_ISDIR(mode)) {
955 obj->yst_mode &= ~S_IFMT;
956 obj->yst_mode |= S_IFDIR;
960 case YAFFS_OBJECT_TYPE_UNKNOWN:
961 case YAFFS_OBJECT_TYPE_HARDLINK:
962 case YAFFS_OBJECT_TYPE_SPECIAL:
968 inode->i_flags |= S_NOATIME;
970 inode->i_ino = obj->objectId;
971 inode->i_mode = obj->yst_mode;
972 inode->i_uid = obj->yst_uid;
973 inode->i_gid = obj->yst_gid;
975 inode->i_rdev = old_decode_dev(obj->yst_rdev);
977 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
978 inode->i_atime.tv_nsec = 0;
979 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
980 inode->i_mtime.tv_nsec = 0;
981 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
982 inode->i_ctime.tv_nsec = 0;
983 inode->i_size = yaffs_GetObjectFileLength(obj);
984 inode->i_blocks = (inode->i_size + 511) >> 9;
986 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
989 (TSTR("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n"),
990 inode->i_mode, inode->i_uid, inode->i_gid,
991 (int)inode->i_size, atomic_read(&inode->i_count)));
993 switch (obj->yst_mode & S_IFMT) {
994 default: /* fifo, device or socket */
995 init_special_inode(inode, obj->yst_mode,
996 old_decode_dev(obj->yst_rdev));
998 case S_IFREG: /* file */
999 inode->i_op = &yaffs_file_inode_operations;
1000 inode->i_fop = &yaffs_file_operations;
1001 inode->i_mapping->a_ops =
1002 &yaffs_file_address_operations;
1004 case S_IFDIR: /* directory */
1005 inode->i_op = &yaffs_dir_inode_operations;
1006 inode->i_fop = &yaffs_dir_operations;
1008 case S_IFLNK: /* symlink */
1009 inode->i_op = &yaffs_symlink_inode_operations;
1013 yaffs_InodeToObjectLV(inode) = obj;
1015 obj->myInode = inode;
1019 (TSTR("yaffs_FileInode invalid parameters\n")));
1024 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1027 struct inode *inode;
1031 (TSTR("yaffs_get_inode for NULL super_block!!\n")));
1038 (TSTR("yaffs_get_inode for NULL object!!\n")));
1044 (TSTR("yaffs_get_inode for object %d\n"), obj->objectId));
1046 inode = Y_IGET(sb, obj->objectId);
1050 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1051 /* iget also increments the inode's i_count */
1052 /* NB You can't be holding grossLock or deadlock will happen! */
1057 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1062 struct inode *inode;
1065 obj = yaffs_DentryToObject(f->f_dentry);
1069 yaffs_GrossLock(dev);
1071 inode = f->f_dentry->d_inode;
1073 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1074 ipos = inode->i_size;
1080 (TSTR("yaffs_file_write: hey obj is null!\n")));
1083 (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
1084 "to object %d at %d(%x)\n"),
1085 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1087 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1089 yaffs_MarkSuperBlockDirty(dev);
1092 (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
1093 (unsigned )n,(unsigned)n));
1098 if (ipos > inode->i_size) {
1099 inode->i_size = ipos;
1100 inode->i_blocks = (ipos + 511) >> 9;
1103 (TSTR("yaffs_file_write size updated to %d bytes, "
1105 ipos, (int)(inode->i_blocks)));
1109 yaffs_GrossUnlock(dev);
1110 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1113 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1114 /* For now we just assume few parallel writes and check against a small number. */
1115 /* Todo: need to do this with a counter to handle parallel reads better */
1117 static ssize_t yaffs_hold_space(struct file *f)
1125 obj = yaffs_DentryToObject(f->f_dentry);
1129 yaffs_GrossLock(dev);
1131 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1133 yaffs_GrossUnlock(dev);
1135 return (nFreeChunks > 20) ? 1 : 0;
1138 static void yaffs_release_space(struct file *f)
1144 obj = yaffs_DentryToObject(f->f_dentry);
1148 yaffs_GrossLock(dev);
1151 yaffs_GrossUnlock(dev);
1155 static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
1163 offset += i_size_read(file->f_path.dentry->d_inode);
1166 offset += file->f_pos;
1171 if (offset != file->f_pos)
1172 file->f_pos = offset;
1181 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1185 struct yaffs_SearchContext *sc;
1186 struct inode *inode = f->f_dentry->d_inode;
1187 unsigned long offset, curoffs;
1191 char name[YAFFS_MAX_NAME_LENGTH + 1];
1193 obj = yaffs_DentryToObject(f->f_dentry);
1196 yaffs_GrossLock(dev);
1198 yaffs_DeviceToLC(dev)->readdirProcess = current;
1202 sc = yaffs_NewSearch(obj);
1208 T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
1212 (TSTR("yaffs_readdir: entry . ino %d \n"),
1213 (int)inode->i_ino));
1214 yaffs_GrossUnlock(dev);
1215 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0){
1216 yaffs_GrossLock(dev);
1219 yaffs_GrossLock(dev);
1225 (TSTR("yaffs_readdir: entry .. ino %d \n"),
1226 (int)f->f_dentry->d_parent->d_inode->i_ino));
1227 yaffs_GrossUnlock(dev);
1228 if (filldir(dirent, "..", 2, offset,
1229 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
1230 yaffs_GrossLock(dev);
1233 yaffs_GrossLock(dev);
1240 /* If the directory has changed since the open or last call to
1241 readdir, rewind to after the 2 canned entries. */
1242 if (f->f_version != inode->i_version) {
1245 f->f_version = inode->i_version;
1248 while(sc->nextReturn){
1251 if (curoffs >= offset) {
1252 int this_inode = yaffs_GetObjectInode(l);
1253 int this_type = yaffs_GetObjectType(l);
1255 yaffs_GetObjectName(l, name,
1256 YAFFS_MAX_NAME_LENGTH + 1);
1258 (TSTR("yaffs_readdir: %s inode %d\n"),
1259 name, yaffs_GetObjectInode(l)));
1261 yaffs_GrossUnlock(dev);
1269 yaffs_GrossLock(dev);
1273 yaffs_GrossLock(dev);
1278 yaffs_SearchAdvance(sc);
1282 yaffs_EndSearch(sc);
1283 yaffs_DeviceToLC(dev)->readdirProcess = NULL;
1284 yaffs_GrossUnlock(dev);
1292 * File creation. Allocate an inode, and we're done..
1295 #define YCRED(x) (x->cred)
1297 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1300 struct inode *inode;
1302 yaffs_Object *obj = NULL;
1305 yaffs_Object *parent = yaffs_InodeToObject(dir);
1307 int error = -ENOSPC;
1308 uid_t uid = YCRED(current)->fsuid;
1309 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1311 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1316 (TSTR("yaffs_mknod: parent object %d type %d\n"),
1317 parent->objectId, parent->variantType));
1320 (TSTR("yaffs_mknod: could not get parent object\n")));
1324 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
1325 "mode %x dev %x\n"),
1326 dentry->d_name.name, mode, rdev));
1328 dev = parent->myDev;
1330 yaffs_GrossLock(dev);
1332 switch (mode & S_IFMT) {
1334 /* Special (socket, fifo, device...) */
1335 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
1336 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1337 gid, old_encode_dev(rdev));
1339 case S_IFREG: /* file */
1340 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
1341 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1344 case S_IFDIR: /* directory */
1346 (TSTR("yaffs_mknod: making directory\n")));
1347 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1350 case S_IFLNK: /* symlink */
1351 T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
1352 obj = NULL; /* Do we ever get here? */
1356 /* Can not call yaffs_get_inode() with gross lock held */
1357 yaffs_GrossUnlock(dev);
1360 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1361 d_instantiate(dentry, inode);
1362 update_dir_time(dir);
1364 (TSTR("yaffs_mknod created object %d count = %d\n"),
1365 obj->objectId, atomic_read(&inode->i_count)));
1367 yaffs_FillInodeFromObject(dir,parent);
1370 (TSTR("yaffs_mknod failed making object\n")));
1377 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1380 T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
1381 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1385 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1386 struct nameidata *n)
1388 T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
1389 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1392 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1400 (TSTR("yaffs_unlink %d:%s\n"),
1402 dentry->d_name.name));
1403 obj = yaffs_InodeToObject(dir);
1406 yaffs_GrossLock(dev);
1408 retVal = yaffs_Unlink(obj, dentry->d_name.name);
1410 if (retVal == YAFFS_OK) {
1411 dentry->d_inode->i_nlink--;
1413 yaffs_GrossUnlock(dev);
1414 mark_inode_dirty(dentry->d_inode);
1415 update_dir_time(dir);
1418 yaffs_GrossUnlock(dev);
1425 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1426 struct dentry *dentry)
1428 struct inode *inode = old_dentry->d_inode;
1429 yaffs_Object *obj = NULL;
1430 yaffs_Object *link = NULL;
1433 T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
1435 obj = yaffs_InodeToObject(inode);
1438 yaffs_GrossLock(dev);
1440 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1441 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1445 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1446 d_instantiate(dentry, old_dentry->d_inode);
1447 atomic_inc(&old_dentry->d_inode->i_count);
1449 (TSTR("yaffs_link link count %d i_count %d\n"),
1450 old_dentry->d_inode->i_nlink,
1451 atomic_read(&old_dentry->d_inode->i_count)));
1454 yaffs_GrossUnlock(dev);
1457 update_dir_time(dir);
1464 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1465 const char *symname)
1469 uid_t uid = YCRED(current)->fsuid;
1470 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1472 T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
1474 dev = yaffs_InodeToObject(dir)->myDev;
1475 yaffs_GrossLock(dev);
1476 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1477 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1478 yaffs_GrossUnlock(dev);
1481 struct inode *inode;
1483 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1484 d_instantiate(dentry, inode);
1485 update_dir_time(dir);
1486 T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
1489 T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
1495 static int yaffs_sync_object(struct file *file, int datasync)
1500 struct dentry *dentry = file->f_path.dentry;
1502 obj = yaffs_DentryToObject(dentry);
1506 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1507 (TSTR("yaffs_sync_object\n")));
1508 yaffs_GrossLock(dev);
1509 yaffs_FlushFile(obj, 1, datasync);
1510 yaffs_GrossUnlock(dev);
1515 * The VFS layer already does all the dentry stuff for rename.
1517 * NB: POSIX says you can rename an object over an old object of the same name
1519 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1520 struct inode *new_dir, struct dentry *new_dentry)
1523 int retVal = YAFFS_FAIL;
1524 yaffs_Object *target;
1526 T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
1527 dev = yaffs_InodeToObject(old_dir)->myDev;
1529 yaffs_GrossLock(dev);
1531 /* Check if the target is an existing directory that is not empty. */
1532 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1533 new_dentry->d_name.name);
1537 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1538 !ylist_empty(&target->variant.directoryVariant.children)) {
1540 T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
1542 retVal = YAFFS_FAIL;
1544 /* Now does unlinking internally using shadowing mechanism */
1545 T(YAFFS_TRACE_OS, (TSTR("calling yaffs_RenameObject\n")));
1547 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1548 old_dentry->d_name.name,
1549 yaffs_InodeToObject(new_dir),
1550 new_dentry->d_name.name);
1552 yaffs_GrossUnlock(dev);
1554 if (retVal == YAFFS_OK) {
1556 new_dentry->d_inode->i_nlink--;
1557 mark_inode_dirty(new_dentry->d_inode);
1560 update_dir_time(old_dir);
1561 if(old_dir != new_dir)
1562 update_dir_time(new_dir);
1569 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1571 struct inode *inode = dentry->d_inode;
1576 (TSTR("yaffs_setattr of object %d\n"),
1577 yaffs_InodeToObject(inode)->objectId));
1579 /* Fail if a requested resize >= 2GB */
1580 if (attr->ia_valid & ATTR_SIZE &&
1581 (attr->ia_size >> 31))
1585 error = inode_change_ok(inode, attr);
1589 error = yaffs_vfs_setattr(inode, attr);
1590 T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
1591 if (attr->ia_valid & ATTR_SIZE){
1592 yaffs_vfs_setsize(inode,attr->ia_size);
1593 inode->i_blocks = (inode->i_size + 511) >> 9;
1596 dev = yaffs_InodeToObject(inode)->myDev;
1597 if (attr->ia_valid & ATTR_SIZE){
1598 T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
1599 (int)(attr->ia_size),(int)(attr->ia_size)));
1601 yaffs_GrossLock(dev);
1602 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1603 if(result == YAFFS_OK) {
1608 yaffs_GrossUnlock(dev);
1613 (TSTR("yaffs_setattr done returning %d\n"),error));
1618 #ifdef CONFIG_YAFFS_XATTR
1619 int yaffs_setxattr(struct dentry *dentry, const char *name,
1620 const void *value, size_t size, int flags)
1622 struct inode *inode = dentry->d_inode;
1625 yaffs_Object *obj = yaffs_InodeToObject(inode);
1628 (TSTR("yaffs_setxattr of object %d\n"),
1635 yaffs_GrossLock(dev);
1636 result = yaffs_SetXAttribute(obj, name, value, size, flags);
1637 if(result == YAFFS_OK)
1641 yaffs_GrossUnlock(dev);
1645 (TSTR("yaffs_setxattr done returning %d\n"),error));
1651 ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
1654 struct inode *inode = dentry->d_inode;
1657 yaffs_Object *obj = yaffs_InodeToObject(inode);
1660 (TSTR("yaffs_getxattr \"%s\" from object %d\n"),
1661 name, obj->objectId));
1665 yaffs_GrossLock(dev);
1666 error = yaffs_GetXAttribute(obj, name, buff, size);
1667 yaffs_GrossUnlock(dev);
1671 (TSTR("yaffs_getxattr done returning %d\n"),error));
1676 int yaffs_removexattr(struct dentry *dentry, const char *name)
1678 struct inode *inode = dentry->d_inode;
1681 yaffs_Object *obj = yaffs_InodeToObject(inode);
1684 (TSTR("yaffs_removexattr of object %d\n"),
1691 yaffs_GrossLock(dev);
1692 result = yaffs_RemoveXAttribute(obj, name);
1693 if(result == YAFFS_OK)
1697 yaffs_GrossUnlock(dev);
1701 (TSTR("yaffs_removexattr done returning %d\n"),error));
1706 ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
1708 struct inode *inode = dentry->d_inode;
1711 yaffs_Object *obj = yaffs_InodeToObject(inode);
1714 (TSTR("yaffs_listxattr of object %d\n"),
1720 yaffs_GrossLock(dev);
1721 error = yaffs_ListXAttributes(obj, buff, size);
1722 yaffs_GrossUnlock(dev);
1726 (TSTR("yaffs_listxattr done returning %d\n"),error));
1734 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1736 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1737 struct super_block *sb = dentry->d_sb;
1739 T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
1741 yaffs_GrossLock(dev);
1743 buf->f_type = YAFFS_MAGIC;
1744 buf->f_bsize = sb->s_blocksize;
1745 buf->f_namelen = 255;
1747 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1748 /* Do this if chunk size is not a power of 2 */
1750 uint64_t bytesInDev;
1753 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
1754 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
1756 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1757 buf->f_blocks = bytesInDev;
1759 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1760 ((uint64_t)(dev->nDataBytesPerChunk));
1762 do_div(bytesFree, sb->s_blocksize);
1764 buf->f_bfree = bytesFree;
1766 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1769 (dev->param.endBlock - dev->param.startBlock + 1) *
1770 dev->param.nChunksPerBlock /
1771 (sb->s_blocksize / dev->nDataBytesPerChunk);
1773 yaffs_GetNumberOfFreeChunks(dev) /
1774 (sb->s_blocksize / dev->nDataBytesPerChunk);
1777 (dev->param.endBlock - dev->param.startBlock + 1) *
1778 dev->param.nChunksPerBlock *
1779 (dev->nDataBytesPerChunk / sb->s_blocksize);
1782 yaffs_GetNumberOfFreeChunks(dev) *
1783 (dev->nDataBytesPerChunk / sb->s_blocksize);
1788 buf->f_bavail = buf->f_bfree;
1790 yaffs_GrossUnlock(dev);
1796 static void yaffs_FlushInodes(struct super_block *sb)
1801 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1802 obj = yaffs_InodeToObject(iptr);
1804 T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
1806 yaffs_FlushFile(obj,1,0);
1812 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
1814 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1818 yaffs_FlushInodes(sb);
1819 yaffs_UpdateDirtyDirectories(dev);
1820 yaffs_FlushEntireDeviceCache(dev);
1822 yaffs_CheckpointSave(dev);
1826 static unsigned yaffs_bg_gc_urgency(yaffs_Device *dev)
1828 unsigned erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
1829 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
1830 unsigned scatteredFree = 0; /* Free chunks not in an erased block */
1832 if(erasedChunks < dev->nFreeChunks)
1833 scatteredFree = (dev->nFreeChunks - erasedChunks);
1835 if(!context->bgRunning)
1837 else if(scatteredFree < (dev->param.nChunksPerBlock * 2))
1839 else if(erasedChunks > dev->nFreeChunks/2)
1841 else if(erasedChunks > dev->nFreeChunks/4)
1847 static int yaffs_do_sync_fs(struct super_block *sb,
1848 int request_checkpoint)
1851 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1852 unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
1853 unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
1856 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
1857 (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
1859 sb->s_dirt ? "dirty" : "clean",
1860 request_checkpoint ? "checkpoint requested" : "no checkpoint",
1861 oneshot_checkpoint ? " one-shot" : "" ));
1863 yaffs_GrossLock(dev);
1864 do_checkpoint = ((request_checkpoint && !gc_urgent) ||
1865 oneshot_checkpoint) &&
1866 !dev->isCheckpointed;
1868 if (sb->s_dirt || do_checkpoint) {
1869 yaffs_FlushSuperBlock(sb, !dev->isCheckpointed && do_checkpoint);
1871 if(oneshot_checkpoint)
1872 yaffs_auto_checkpoint &= ~4;
1874 yaffs_GrossUnlock(dev);
1880 * yaffs background thread functions .
1881 * yaffs_BackgroundThread() the thread function
1882 * yaffs_BackgroundStart() launches the background thread.
1883 * yaffs_BackgroundStop() cleans up the background thread.
1886 * The thread should only run after the yaffs is initialised
1887 * The thread should be stopped before yaffs is unmounted.
1888 * The thread should not do any writing while the fs is in read only.
1892 void yaffs_background_waker(unsigned long data)
1894 wake_up_process((struct task_struct *)data);
1897 static int yaffs_BackgroundThread(void *data)
1899 yaffs_Device *dev = (yaffs_Device *)data;
1900 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
1901 unsigned long now = jiffies;
1902 unsigned long next_dir_update = now;
1903 unsigned long next_gc = now;
1904 unsigned long expires;
1905 unsigned int urgency;
1908 struct timer_list timer;
1910 T(YAFFS_TRACE_BACKGROUND,
1911 (TSTR("yaffs_background starting for dev %p\n"),
1915 while(context->bgRunning){
1916 T(YAFFS_TRACE_BACKGROUND,
1917 (TSTR("yaffs_background\n")));
1919 if(kthread_should_stop())
1925 yaffs_GrossLock(dev);
1929 if(time_after(now, next_dir_update) && yaffs_bg_enable){
1930 yaffs_UpdateDirtyDirectories(dev);
1931 next_dir_update = now + HZ;
1934 if(time_after(now,next_gc) && yaffs_bg_enable){
1935 if(!dev->isCheckpointed){
1936 urgency = yaffs_bg_gc_urgency(dev);
1937 gcResult = yaffs_BackgroundGarbageCollect(dev, urgency);
1939 next_gc = now + HZ/20+1;
1940 else if(urgency > 0)
1941 next_gc = now + HZ/10+1;
1943 next_gc = now + HZ * 2;
1945 * gc not running so set to next_dir_update
1946 * to cut down on wake ups
1948 next_gc = next_dir_update;
1950 yaffs_GrossUnlock(dev);
1951 expires = next_dir_update;
1952 if (time_before(next_gc,expires))
1954 if(time_before(expires,now))
1957 Y_INIT_TIMER(&timer);
1958 timer.expires = expires+1;
1959 timer.data = (unsigned long) current;
1960 timer.function = yaffs_background_waker;
1962 set_current_state(TASK_INTERRUPTIBLE);
1965 del_timer_sync(&timer);
1971 static int yaffs_BackgroundStart(yaffs_Device *dev)
1974 struct yaffs_LinuxContext *context = yaffs_DeviceToLC(dev);
1979 context->bgRunning = 1;
1981 context->bgThread = kthread_run(yaffs_BackgroundThread,
1982 (void *)dev,"yaffs-bg-%d",context->mount_id);
1984 if(IS_ERR(context->bgThread)){
1985 retval = PTR_ERR(context->bgThread);
1986 context->bgThread = NULL;
1987 context->bgRunning = 0;
1992 static void yaffs_BackgroundStop(yaffs_Device *dev)
1994 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToLC(dev);
1996 ctxt->bgRunning = 0;
1998 if( ctxt->bgThread){
1999 kthread_stop(ctxt->bgThread);
2000 ctxt->bgThread = NULL;
2005 static void yaffs_write_super(struct super_block *sb)
2007 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
2009 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
2010 (TSTR("yaffs_write_super%s\n"),
2011 request_checkpoint ? " checkpt" : ""));
2013 yaffs_do_sync_fs(sb, request_checkpoint);
2018 static int yaffs_sync_fs(struct super_block *sb, int wait)
2020 unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
2022 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
2023 (TSTR("yaffs_sync_fs%s\n"),
2024 request_checkpoint ? " checkpt" : ""));
2026 yaffs_do_sync_fs(sb, request_checkpoint);
2031 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
2033 struct inode *inode;
2035 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2038 (TSTR("yaffs_iget for %lu\n"), ino));
2040 inode = iget_locked(sb, ino);
2042 return ERR_PTR(-ENOMEM);
2043 if (!(inode->i_state & I_NEW))
2046 /* NB This is called as a side effect of other functions, but
2047 * we had to release the lock to prevent deadlocks, so
2048 * need to lock again.
2051 yaffs_GrossLock(dev);
2053 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2055 yaffs_FillInodeFromObject(inode, obj);
2057 yaffs_GrossUnlock(dev);
2059 unlock_new_inode(inode);
2063 static YLIST_HEAD(yaffs_context_list);
2064 struct semaphore yaffs_context_lock;
2066 static void yaffs_put_super(struct super_block *sb)
2068 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2070 T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
2072 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2073 (TSTR("Shutting down yaffs background thread\n")));
2074 yaffs_BackgroundStop(dev);
2075 T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
2076 (TSTR("yaffs background thread shut down\n")));
2078 yaffs_GrossLock(dev);
2080 yaffs_FlushSuperBlock(sb,1);
2082 if (yaffs_DeviceToLC(dev)->putSuperFunc)
2083 yaffs_DeviceToLC(dev)->putSuperFunc(sb);
2086 yaffs_Deinitialise(dev);
2088 yaffs_GrossUnlock(dev);
2090 down(&yaffs_context_lock);
2091 ylist_del_init(&(yaffs_DeviceToLC(dev)->contextList));
2092 up(&yaffs_context_lock);
2094 if (yaffs_DeviceToLC(dev)->spareBuffer) {
2095 YFREE(yaffs_DeviceToLC(dev)->spareBuffer);
2096 yaffs_DeviceToLC(dev)->spareBuffer = NULL;
2103 static void yaffs_MTDPutSuper(struct super_block *sb)
2105 struct mtd_info *mtd = yaffs_DeviceToMtd(yaffs_SuperToDevice(sb));
2110 put_mtd_device(mtd);
2114 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2116 struct super_block *sb = yaffs_DeviceToLC(dev)->superBlock;
2118 T(YAFFS_TRACE_OS, (TSTR("yaffs_MarkSuperBlockDirty() sb = %p\n"), sb));
2125 int skip_checkpoint_read;
2126 int skip_checkpoint_write;
2129 int tags_ecc_overridden;
2130 int lazy_loading_enabled;
2131 int lazy_loading_overridden;
2132 int empty_lost_and_found;
2133 int empty_lost_and_found_overridden;
2136 #define MAX_OPT_LEN 30
2137 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2139 char cur_opt[MAX_OPT_LEN + 1];
2143 /* Parse through the options which is a comma seperated list */
2145 while (options_str && *options_str && !error) {
2146 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2149 while(*options_str == ',')
2152 while (*options_str && *options_str != ',') {
2153 if (p < MAX_OPT_LEN) {
2154 cur_opt[p] = *options_str;
2160 if (!strcmp(cur_opt, "inband-tags"))
2161 options->inband_tags = 1;
2162 else if (!strcmp(cur_opt, "tags-ecc-off")){
2163 options->tags_ecc_on = 0;
2164 options->tags_ecc_overridden=1;
2165 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2166 options->tags_ecc_on = 1;
2167 options->tags_ecc_overridden = 1;
2168 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2169 options->lazy_loading_enabled = 0;
2170 options->lazy_loading_overridden=1;
2171 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2172 options->lazy_loading_enabled = 1;
2173 options->lazy_loading_overridden = 1;
2174 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2175 options->empty_lost_and_found = 0;
2176 options->empty_lost_and_found_overridden=1;
2177 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2178 options->empty_lost_and_found = 1;
2179 options->empty_lost_and_found_overridden=1;
2180 } else if (!strcmp(cur_opt, "no-cache"))
2181 options->no_cache = 1;
2182 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2183 options->skip_checkpoint_read = 1;
2184 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2185 options->skip_checkpoint_write = 1;
2186 else if (!strcmp(cur_opt, "no-checkpoint")) {
2187 options->skip_checkpoint_read = 1;
2188 options->skip_checkpoint_write = 1;
2190 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2199 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2200 struct super_block *sb,
2201 void *data, int silent)
2204 struct inode *inode = NULL;
2205 struct dentry *root;
2206 yaffs_Device *dev = 0;
2207 char devname_buf[BDEVNAME_SIZE + 1];
2208 struct mtd_info *mtd;
2210 char *data_str = (char *)data;
2211 struct yaffs_LinuxContext *context = NULL;
2212 yaffs_DeviceParam *param;
2216 yaffs_options options;
2220 struct yaffs_LinuxContext *context_iterator;
2221 struct ylist_head *l;
2223 sb->s_magic = YAFFS_MAGIC;
2224 sb->s_op = &yaffs_super_ops;
2225 sb->s_flags |= MS_NOATIME;
2227 readOnly =((sb->s_flags & MS_RDONLY) != 0);
2229 sb->s_export_op = &yaffs_export_ops;
2232 printk(KERN_INFO "yaffs: sb is NULL\n");
2233 else if (!sb->s_dev)
2234 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2235 else if (!yaffs_devname(sb, devname_buf))
2236 printk(KERN_INFO "yaffs: devname is NULL\n");
2238 printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
2240 yaffs_devname(sb, devname_buf),
2241 readOnly ? "ro" : "rw");
2246 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2248 memset(&options, 0, sizeof(options));
2250 if (yaffs_parse_options(&options, data_str)) {
2251 /* Option parsing failed */
2256 sb->s_blocksize = PAGE_CACHE_SIZE;
2257 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2260 (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffsVersion));
2262 (TSTR("yaffs_read_super: block size %d\n"),
2263 (int)(sb->s_blocksize)));
2265 T(YAFFS_TRACE_ALWAYS,
2266 (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
2267 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2268 yaffs_devname(sb, devname_buf)));
2270 /* Check it's an mtd device..... */
2271 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2272 return NULL; /* This isn't an mtd device */
2274 /* Get the device */
2275 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2277 T(YAFFS_TRACE_ALWAYS,
2278 (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
2282 /* Check it's NAND */
2283 if (mtd->type != MTD_NANDFLASH) {
2284 T(YAFFS_TRACE_ALWAYS,
2285 (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
2290 T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
2291 T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
2292 T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
2293 T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
2294 T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
2295 T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
2296 T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
2297 T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2298 T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
2299 T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
2300 T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
2302 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2304 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2305 T(YAFFS_TRACE_ALWAYS,
2306 (TSTR("yaffs: auto selecting yaffs2\n")));
2310 /* Added NCB 26/5/2006 for completeness */
2311 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2312 T(YAFFS_TRACE_ALWAYS,
2313 (TSTR("yaffs: auto selecting yaffs1\n")));
2319 if (yaffsVersion == 2) {
2320 /* Check for version 2 style functions */
2322 !mtd->block_isbad ||
2323 !mtd->block_markbad ||
2326 !mtd->read_oob || !mtd->write_oob) {
2327 T(YAFFS_TRACE_ALWAYS,
2328 (TSTR("yaffs: MTD device does not support required "
2333 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2334 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2335 !options.inband_tags) {
2336 T(YAFFS_TRACE_ALWAYS,
2337 (TSTR("yaffs: MTD device does not have the "
2338 "right page sizes\n")));
2342 /* Check for V1 style functions */
2346 !mtd->read_oob || !mtd->write_oob) {
2347 T(YAFFS_TRACE_ALWAYS,
2348 (TSTR("yaffs: MTD device does not support required "
2353 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2354 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2355 T(YAFFS_TRACE_ALWAYS,
2356 (TSTR("yaffs: MTD device does not support have the "
2357 "right page sizes\n")));
2362 /* OK, so if we got here, we have an MTD that's NAND and looks
2363 * like it has the right capabilities
2364 * Set the yaffs_Device up for mtd
2367 if (!readOnly && !(mtd->flags & MTD_WRITEABLE)){
2369 printk(KERN_INFO "yaffs: mtd is read only, setting superblock read only");
2370 sb->s_flags |= MS_RDONLY;
2373 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2374 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2376 if(!dev || !context ){
2386 /* Deep shit could not allocate device structure */
2387 T(YAFFS_TRACE_ALWAYS,
2388 (TSTR("yaffs_read_super: Failed trying to allocate "
2389 "yaffs_Device. \n")));
2392 memset(dev, 0, sizeof(yaffs_Device));
2393 param = &(dev->param);
2395 memset(context,0,sizeof(struct yaffs_LinuxContext));
2396 dev->osContext = context;
2397 YINIT_LIST_HEAD(&(context->contextList));
2399 context->superBlock = sb;
2401 dev->readOnly = readOnly;
2403 sb->s_fs_info = dev;
2405 dev->driverContext = mtd;
2406 param->name = mtd->name;
2408 /* Set up the memory size parameters.... */
2410 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2412 param->startBlock = 0;
2413 param->endBlock = nBlocks - 1;
2414 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2415 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2416 param->nReservedBlocks = 5;
2417 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2418 param->inbandTags = options.inband_tags;
2420 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2421 param->disableLazyLoad = 1;
2423 #ifdef CONFIG_YAFFS_XATTR
2424 param->enableXattr = 1;
2426 if(options.lazy_loading_overridden)
2427 param->disableLazyLoad = !options.lazy_loading_enabled;
2429 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2430 param->noTagsECC = 1;
2433 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2435 param->deferDirectoryUpdate = 1;
2438 if(options.tags_ecc_overridden)
2439 param->noTagsECC = !options.tags_ecc_on;
2441 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2442 param->emptyLostAndFound = 1;
2445 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2446 param->refreshPeriod = 0;
2448 param->refreshPeriod = 500;
2451 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2452 param->alwaysCheckErased = 1;
2455 if(options.empty_lost_and_found_overridden)
2456 param->emptyLostAndFound = options.empty_lost_and_found;
2458 /* ... and the functions. */
2459 if (yaffsVersion == 2) {
2460 param->writeChunkWithTagsToNAND =
2461 nandmtd2_WriteChunkWithTagsToNAND;
2462 param->readChunkWithTagsFromNAND =
2463 nandmtd2_ReadChunkWithTagsFromNAND;
2464 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2465 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2466 yaffs_DeviceToLC(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2467 param->isYaffs2 = 1;
2468 param->totalBytesPerChunk = mtd->writesize;
2469 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2470 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2472 param->startBlock = 0;
2473 param->endBlock = nBlocks - 1;
2475 /* use the MTD interface in yaffs_mtdif1.c */
2476 param->writeChunkWithTagsToNAND =
2477 nandmtd1_WriteChunkWithTagsToNAND;
2478 param->readChunkWithTagsFromNAND =
2479 nandmtd1_ReadChunkWithTagsFromNAND;
2480 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2481 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2482 param->isYaffs2 = 0;
2484 /* ... and common functions */
2485 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2486 param->initialiseNAND = nandmtd_InitialiseNAND;
2488 yaffs_DeviceToLC(dev)->putSuperFunc = yaffs_MTDPutSuper;
2490 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2491 param->gcControl = yaffs_gc_control_callback;
2493 yaffs_DeviceToLC(dev)->superBlock= sb;
2496 #ifndef CONFIG_YAFFS_DOES_ECC
2497 param->useNANDECC = 1;
2500 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2501 param->wideTnodesDisabled = 1;
2504 param->skipCheckpointRead = options.skip_checkpoint_read;
2505 param->skipCheckpointWrite = options.skip_checkpoint_write;
2507 down(&yaffs_context_lock);
2508 /* Get a mount id */
2510 for(mount_id=0; ! found; mount_id++){
2512 ylist_for_each(l,&yaffs_context_list){
2513 context_iterator = ylist_entry(l,struct yaffs_LinuxContext,contextList);
2514 if(context_iterator->mount_id == mount_id)
2518 context->mount_id = mount_id;
2520 ylist_add_tail(&(yaffs_DeviceToLC(dev)->contextList), &yaffs_context_list);
2521 up(&yaffs_context_lock);
2523 /* Directory search handling...*/
2524 YINIT_LIST_HEAD(&(yaffs_DeviceToLC(dev)->searchContexts));
2525 param->removeObjectCallback = yaffs_RemoveObjectCallback;
2527 init_MUTEX(&(yaffs_DeviceToLC(dev)->grossLock));
2529 yaffs_GrossLock(dev);
2531 err = yaffs_GutsInitialise(dev);
2534 (TSTR("yaffs_read_super: guts initialised %s\n"),
2535 (err == YAFFS_OK) ? "OK" : "FAILED"));
2538 yaffs_BackgroundStart(dev);
2540 if(!context->bgThread)
2541 param->deferDirectoryUpdate = 0;
2544 /* Release lock before yaffs_get_inode() */
2545 yaffs_GrossUnlock(dev);
2547 /* Create root inode */
2548 if (err == YAFFS_OK)
2549 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2555 inode->i_op = &yaffs_dir_inode_operations;
2556 inode->i_fop = &yaffs_dir_operations;
2558 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
2560 root = d_alloc_root(inode);
2562 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
2569 sb->s_dirt = !dev->isCheckpointed;
2570 T(YAFFS_TRACE_ALWAYS,
2571 (TSTR("yaffs_read_super: isCheckpointed %d\n"),
2572 dev->isCheckpointed));
2574 T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
2579 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2582 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2585 static int yaffs_read_super(struct file_system_type *fs,
2586 int flags, const char *dev_name,
2587 void *data, struct vfsmount *mnt)
2590 return get_sb_bdev(fs, flags, dev_name, data,
2591 yaffs_internal_read_super_mtd, mnt);
2594 static struct file_system_type yaffs_fs_type = {
2595 .owner = THIS_MODULE,
2597 .get_sb = yaffs_read_super,
2598 .kill_sb = kill_block_super,
2599 .fs_flags = FS_REQUIRES_DEV,
2603 #ifdef CONFIG_YAFFS_YAFFS2
2605 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2608 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2611 static int yaffs2_read_super(struct file_system_type *fs,
2612 int flags, const char *dev_name, void *data,
2613 struct vfsmount *mnt)
2615 return get_sb_bdev(fs, flags, dev_name, data,
2616 yaffs2_internal_read_super_mtd, mnt);
2619 static struct file_system_type yaffs2_fs_type = {
2620 .owner = THIS_MODULE,
2622 .get_sb = yaffs2_read_super,
2623 .kill_sb = kill_block_super,
2624 .fs_flags = FS_REQUIRES_DEV,
2626 #endif /* CONFIG_YAFFS_YAFFS2 */
2628 static struct proc_dir_entry *my_proc_entry;
2629 static struct proc_dir_entry *debug_proc_entry;
2631 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
2633 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
2634 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
2635 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
2636 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
2637 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
2638 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
2639 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
2640 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
2641 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
2642 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
2643 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
2644 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
2645 buf += sprintf(buf, "alwaysCheckErased...%d\n", dev->param.alwaysCheckErased);
2647 buf += sprintf(buf, "\n");
2653 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
2655 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2656 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2657 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2658 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2659 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2660 buf += sprintf(buf, "\n");
2661 buf += sprintf(buf, "nTnodes............ %d\n", dev->nTnodes);
2662 buf += sprintf(buf, "nObjects........... %d\n", dev->nObjects);
2663 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2664 buf += sprintf(buf, "\n");
2665 buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
2666 buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
2667 buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
2668 buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
2669 buf += sprintf(buf, "allGCs............. %u\n", dev->allGCs);
2670 buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGCs);
2671 buf += sprintf(buf, "oldestDirtyGCs..... %u\n", dev->oldestDirtyGCs);
2672 buf += sprintf(buf, "nGCBlocks.......... %u\n", dev->nGCBlocks);
2673 buf += sprintf(buf, "backgroundGCs...... %u\n", dev->backgroundGCs);
2674 buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
2675 buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
2676 buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
2677 buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
2678 buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
2679 buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
2680 buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
2681 buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
2682 buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
2683 buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
2685 sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
2690 static int yaffs_proc_read(char *page,
2692 off_t offset, int count, int *eof, void *data)
2694 struct ylist_head *item;
2699 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2700 * We use 'offset' (*ppos) to indicate where we are in devList.
2701 * This also assumes the user has posted a read buffer large
2702 * enough to hold the complete output; but that's life in /proc.
2707 /* Print header first */
2709 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__"\n");
2711 buf += sprintf(buf,"\n");
2715 down(&yaffs_context_lock);
2717 /* Locate and print the Nth entry. Order N-squared but N is small. */
2718 ylist_for_each(item, &yaffs_context_list) {
2719 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2720 yaffs_Device *dev = dc->dev;
2722 if (n < (step & ~1)) {
2727 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
2728 buf = yaffs_dump_dev_part0(buf, dev);
2730 buf = yaffs_dump_dev_part1(buf, dev);
2734 up(&yaffs_context_lock);
2737 return buf - page < count ? buf - page : count;
2740 static int yaffs_stats_proc_read(char *page,
2742 off_t offset, int count, int *eof, void *data)
2744 struct ylist_head *item;
2748 down(&yaffs_context_lock);
2750 /* Locate and print the Nth entry. Order N-squared but N is small. */
2751 ylist_for_each(item, &yaffs_context_list) {
2752 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2753 yaffs_Device *dev = dc->dev;
2757 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2759 buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
2760 n, dev->nFreeChunks, erasedChunks,
2761 dev->backgroundGCs, dev->oldestDirtyGCs,
2762 dev->nObjects, dev->nTnodes);
2764 up(&yaffs_context_lock);
2767 return buf - page < count ? buf - page : count;
2771 * Set the verbosity of the warnings and error messages.
2773 * Note that the names can only be a..z or _ with the current code.
2778 unsigned mask_bitfield;
2780 {"allocate", YAFFS_TRACE_ALLOCATE},
2781 {"always", YAFFS_TRACE_ALWAYS},
2782 {"background", YAFFS_TRACE_BACKGROUND},
2783 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2784 {"buffers", YAFFS_TRACE_BUFFERS},
2785 {"bug", YAFFS_TRACE_BUG},
2786 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2787 {"deletion", YAFFS_TRACE_DELETION},
2788 {"erase", YAFFS_TRACE_ERASE},
2789 {"error", YAFFS_TRACE_ERROR},
2790 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2791 {"gc", YAFFS_TRACE_GC},
2792 {"lock", YAFFS_TRACE_LOCK},
2793 {"mtd", YAFFS_TRACE_MTD},
2794 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2795 {"os", YAFFS_TRACE_OS},
2796 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2797 {"scan", YAFFS_TRACE_SCAN},
2798 {"tracing", YAFFS_TRACE_TRACING},
2799 {"sync", YAFFS_TRACE_SYNC},
2800 {"write", YAFFS_TRACE_WRITE},
2802 {"verify", YAFFS_TRACE_VERIFY},
2803 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2804 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2805 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2807 {"all", 0xffffffff},
2812 #define MAX_MASK_NAME_LENGTH 40
2813 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2814 unsigned long count, void *data)
2816 unsigned rg = 0, mask_bitfield;
2820 char substring[MAX_MASK_NAME_LENGTH + 1];
2826 rg = yaffs_traceMask;
2828 while (!done && (pos < count)) {
2830 while ((pos < count) && isspace(buf[pos]))
2847 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2849 if (end > buf + pos) {
2850 mask_name = "numeral";
2851 len = end - (buf + pos);
2855 for (x = buf + pos, i = 0;
2856 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2857 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2859 substring[i] = '\0';
2861 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2862 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
2863 mask_name = mask_flags[i].mask_name;
2864 mask_bitfield = mask_flags[i].mask_bitfield;
2871 if (mask_name != NULL) {
2875 rg &= ~mask_bitfield;
2878 rg |= mask_bitfield;
2884 rg |= mask_bitfield;
2890 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2892 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
2894 if (rg & YAFFS_TRACE_ALWAYS) {
2895 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2897 flag = ((rg & mask_flags[i].mask_bitfield) ==
2898 mask_flags[i].mask_bitfield) ? '+' : '-';
2899 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
2907 static int yaffs_proc_write(struct file *file, const char *buf,
2908 unsigned long count, void *data)
2910 return yaffs_proc_write_trace_options(file, buf, count, data);
2913 /* Stuff to handle installation of file systems */
2914 struct file_system_to_install {
2915 struct file_system_type *fst;
2919 static struct file_system_to_install fs_to_install[] = {
2920 {&yaffs_fs_type, 0},
2921 {&yaffs2_fs_type, 0},
2925 static int __init init_yaffs_fs(void)
2928 struct file_system_to_install *fsinst;
2930 T(YAFFS_TRACE_ALWAYS,
2931 (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
2933 #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
2934 T(YAFFS_TRACE_ALWAYS,
2935 (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
2941 init_MUTEX(&yaffs_context_lock);
2943 /* Install the proc_fs entries */
2944 my_proc_entry = create_proc_entry("yaffs",
2948 if (my_proc_entry) {
2949 my_proc_entry->write_proc = yaffs_proc_write;
2950 my_proc_entry->read_proc = yaffs_proc_read;
2951 my_proc_entry->data = NULL;
2955 debug_proc_entry = create_proc_entry("yaffs_stats",
2959 if (debug_proc_entry) {
2960 debug_proc_entry->write_proc = NULL;
2961 debug_proc_entry->read_proc = yaffs_stats_proc_read;
2962 debug_proc_entry->data = NULL;
2966 /* Now add the file system entries */
2968 fsinst = fs_to_install;
2970 while (fsinst->fst && !error) {
2971 error = register_filesystem(fsinst->fst);
2973 fsinst->installed = 1;
2977 /* Any errors? uninstall */
2979 fsinst = fs_to_install;
2981 while (fsinst->fst) {
2982 if (fsinst->installed) {
2983 unregister_filesystem(fsinst->fst);
2984 fsinst->installed = 0;
2993 static void __exit exit_yaffs_fs(void)
2996 struct file_system_to_install *fsinst;
2998 T(YAFFS_TRACE_ALWAYS,
2999 (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
3001 remove_proc_entry("yaffs", YPROC_ROOT);
3002 remove_proc_entry("yaffs_stats", YPROC_ROOT);
3004 fsinst = fs_to_install;
3006 while (fsinst->fst) {
3007 if (fsinst->installed) {
3008 unregister_filesystem(fsinst->fst);
3009 fsinst->installed = 0;
3015 module_init(init_yaffs_fs)
3016 module_exit(exit_yaffs_fs)
3018 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3019 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
3020 MODULE_LICENSE("GPL");