2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2009 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.
34 const char *yaffs_fs_c_version =
35 "$Id: yaffs_fs.c,v 1.102 2010-03-15 23:10:33 charles Exp $";
36 extern const char *yaffs_guts_c_version;
38 #include <linux/version.h>
40 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
41 #define YAFFS_COMPILE_BACKGROUND
44 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
45 #include <linux/config.h>
47 #include <linux/kernel.h>
48 #include <linux/module.h>
49 #include <linux/slab.h>
50 #include <linux/init.h>
52 #include <linux/proc_fs.h>
53 #include <linux/smp_lock.h>
54 #include <linux/pagemap.h>
55 #include <linux/mtd/mtd.h>
56 #include <linux/interrupt.h>
57 #include <linux/string.h>
58 #include <linux/ctype.h>
60 #ifdef YAFFS_COMPILE_BACKGROUND
61 #include <linux/kthread.h>
62 #include <linux/delay.h>
66 #include "asm/div64.h"
71 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
73 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
74 #include <linux/statfs.h>
75 #define UnlockPage(p) unlock_page(p)
76 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
78 /* FIXME: use sb->s_id instead ? */
79 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
83 #include <linux/locks.h>
84 #define BDEVNAME_SIZE 0
85 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
87 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
88 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
94 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
95 #define YPROC_ROOT (&proc_root)
97 #define YPROC_ROOT NULL
100 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
101 #define WRITE_SIZE_STR "writesize"
102 #define WRITE_SIZE(mtd) ((mtd)->writesize)
104 #define WRITE_SIZE_STR "oobblock"
105 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
108 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
109 #define YAFFS_USE_WRITE_BEGIN_END 1
111 #define YAFFS_USE_WRITE_BEGIN_END 0
114 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
115 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
117 uint64_t result = partition_size;
118 do_div(result, block_size);
119 return (uint32_t)result;
122 #define YCALCBLOCKS(s, b) ((s)/(b))
125 #include <linux/uaccess.h>
127 #include "yportenv.h"
128 #include "yaffs_trace.h"
129 #include "yaffs_guts.h"
131 #include "yaffs_linux.h"
133 #include <linux/mtd/mtd.h>
134 #include "yaffs_mtdif.h"
135 #include "yaffs_mtdif1.h"
136 #include "yaffs_mtdif2.h"
138 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
139 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
140 unsigned int yaffs_auto_checkpoint = 1;
141 unsigned int yaffs_gc_control = 1;
143 /* Module Parameters */
144 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
145 module_param(yaffs_traceMask, uint, 0644);
146 module_param(yaffs_wr_attempts, uint, 0644);
147 module_param(yaffs_auto_checkpoint, uint, 0644);
148 module_param(yaffs_gc_control, uint, 0644);
150 MODULE_PARM(yaffs_traceMask, "i");
151 MODULE_PARM(yaffs_wr_attempts, "i");
152 MODULE_PARM(yaffs_auto_checkpoint, "i");
153 MODULE_PARM(yaffs_gc_control, "i");
156 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
157 /* use iget and read_inode */
158 #define Y_IGET(sb, inum) iget((sb), (inum))
159 static void yaffs_read_inode(struct inode *inode);
162 /* Call local equivalent */
163 #define YAFFS_USE_OWN_IGET
164 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
166 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
169 /*#define T(x) printk x */
171 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
172 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
174 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
177 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
178 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
180 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
181 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
183 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
187 #define update_dir_time(dir) do {\
188 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
191 static void yaffs_put_super(struct super_block *sb);
193 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
195 static ssize_t yaffs_hold_space(struct file *f);
196 static void yaffs_release_space(struct file *f);
198 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
199 static int yaffs_file_flush(struct file *file, fl_owner_t id);
201 static int yaffs_file_flush(struct file *file);
204 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
207 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
209 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
210 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
211 struct nameidata *n);
212 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
213 struct nameidata *n);
215 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
216 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
218 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
219 struct dentry *dentry);
220 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
221 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
222 const char *symname);
223 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
225 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
226 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
229 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
232 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
233 struct inode *new_dir, struct dentry *new_dentry);
234 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
236 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
237 static int yaffs_sync_fs(struct super_block *sb, int wait);
238 static void yaffs_write_super(struct super_block *sb);
240 static int yaffs_sync_fs(struct super_block *sb);
241 static int yaffs_write_super(struct super_block *sb);
244 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
245 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
246 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
247 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
249 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
252 #ifdef YAFFS_HAS_PUT_INODE
253 static void yaffs_put_inode(struct inode *inode);
256 static void yaffs_delete_inode(struct inode *);
257 static void yaffs_clear_inode(struct inode *);
259 static int yaffs_readpage(struct file *file, struct page *page);
260 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
261 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
263 static int yaffs_writepage(struct page *page);
267 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
268 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
269 loff_t pos, unsigned len, unsigned flags,
270 struct page **pagep, void **fsdata);
271 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
272 loff_t pos, unsigned len, unsigned copied,
273 struct page *pg, void *fsdadata);
275 static int yaffs_prepare_write(struct file *f, struct page *pg,
276 unsigned offset, unsigned to);
277 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
282 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
284 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
285 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
287 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
290 static struct address_space_operations yaffs_file_address_operations = {
291 .readpage = yaffs_readpage,
292 .writepage = yaffs_writepage,
293 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
294 .write_begin = yaffs_write_begin,
295 .write_end = yaffs_write_end,
297 .prepare_write = yaffs_prepare_write,
298 .commit_write = yaffs_commit_write,
302 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
303 static const struct file_operations yaffs_file_operations = {
304 .read = do_sync_read,
305 .write = do_sync_write,
306 .aio_read = generic_file_aio_read,
307 .aio_write = generic_file_aio_write,
308 .mmap = generic_file_mmap,
309 .flush = yaffs_file_flush,
310 .fsync = yaffs_sync_object,
311 .splice_read = generic_file_splice_read,
312 .splice_write = generic_file_splice_write,
313 .llseek = generic_file_llseek,
316 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
318 static const struct file_operations yaffs_file_operations = {
319 .read = do_sync_read,
320 .write = do_sync_write,
321 .aio_read = generic_file_aio_read,
322 .aio_write = generic_file_aio_write,
323 .mmap = generic_file_mmap,
324 .flush = yaffs_file_flush,
325 .fsync = yaffs_sync_object,
326 .sendfile = generic_file_sendfile,
331 static const struct file_operations yaffs_file_operations = {
332 .read = generic_file_read,
333 .write = generic_file_write,
334 .mmap = generic_file_mmap,
335 .flush = yaffs_file_flush,
336 .fsync = yaffs_sync_object,
337 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
338 .sendfile = generic_file_sendfile,
343 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
344 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
346 void * kaddr = kmap_atomic(page, KM_USER0);
347 memset(kaddr + start, 0, end - start);
348 kunmap_atomic(kaddr, KM_USER0);
349 flush_dcache_page(page);
354 static const struct inode_operations yaffs_file_inode_operations = {
355 .setattr = yaffs_setattr,
358 static const struct inode_operations yaffs_symlink_inode_operations = {
359 .readlink = yaffs_readlink,
360 .follow_link = yaffs_follow_link,
361 .setattr = yaffs_setattr,
364 static const struct inode_operations yaffs_dir_inode_operations = {
365 .create = yaffs_create,
366 .lookup = yaffs_lookup,
368 .unlink = yaffs_unlink,
369 .symlink = yaffs_symlink,
370 .mkdir = yaffs_mkdir,
371 .rmdir = yaffs_unlink,
372 .mknod = yaffs_mknod,
373 .rename = yaffs_rename,
374 .setattr = yaffs_setattr,
377 static const struct file_operations yaffs_dir_operations = {
378 .read = generic_read_dir,
379 .readdir = yaffs_readdir,
380 .fsync = yaffs_sync_object,
383 static const struct super_operations yaffs_super_ops = {
384 .statfs = yaffs_statfs,
386 #ifndef YAFFS_USE_OWN_IGET
387 .read_inode = yaffs_read_inode,
389 #ifdef YAFFS_HAS_PUT_INODE
390 .put_inode = yaffs_put_inode,
392 .put_super = yaffs_put_super,
393 .delete_inode = yaffs_delete_inode,
394 .clear_inode = yaffs_clear_inode,
395 .sync_fs = yaffs_sync_fs,
396 .write_super = yaffs_write_super,
399 static unsigned yaffs_gc_control_callback(yaffs_Device *dev)
401 return yaffs_gc_control;
404 static void yaffs_GrossLock(yaffs_Device *dev)
406 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
407 down(&(yaffs_DeviceToContext(dev)->grossLock));
408 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
411 static void yaffs_GrossUnlock(yaffs_Device *dev)
413 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
414 up(&(yaffs_DeviceToContext(dev)->grossLock));
418 /*-----------------------------------------------------------------*/
419 /* Directory search context allows us to unlock access to yaffs during
420 * filldir without causing problems with the directory being modified.
421 * This is similar to the tried and tested mechanism used in yaffs direct.
423 * A search context iterates along a doubly linked list of siblings in the
424 * directory. If the iterating object is deleted then this would corrupt
425 * the list iteration, likely causing a crash. The search context avoids
426 * this by using the removeObjectCallback to move the search context to the
427 * next object before the object is deleted.
429 * Many readdirs (and thus seach conexts) may be alive simulateously so
430 * each yaffs_Device has a list of these.
432 * A seach context lives for the duration of a readdir.
434 * All these functions must be called while yaffs is locked.
437 struct yaffs_SearchContext {
439 yaffs_Object *dirObj;
440 yaffs_Object *nextReturn;
441 struct ylist_head others;
445 * yaffs_NewSearch() creates a new search context, initialises it and
446 * adds it to the device's search context list.
448 * Called at start of readdir.
450 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
452 yaffs_Device *dev = dir->myDev;
453 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
457 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
458 sc->nextReturn = NULL;
460 sc->nextReturn = ylist_entry(
461 dir->variant.directoryVariant.children.next,
462 yaffs_Object,siblings);
463 YINIT_LIST_HEAD(&sc->others);
464 ylist_add(&sc->others,&(yaffs_DeviceToContext(dev)->searchContexts));
470 * yaffs_EndSearch() disposes of a search context and cleans up.
472 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
475 ylist_del(&sc->others);
481 * yaffs_SearchAdvance() moves a search context to the next object.
482 * Called when the search iterates or when an object removal causes
483 * the search context to be moved to the next object.
485 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
490 if( sc->nextReturn == NULL ||
491 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
492 sc->nextReturn = NULL;
494 struct ylist_head *next = sc->nextReturn->siblings.next;
496 if( next == &sc->dirObj->variant.directoryVariant.children)
497 sc->nextReturn = NULL; /* end of list */
499 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
504 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
505 * We check open search contexts and advance any which are currently
506 * on the object being iterated.
508 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
511 struct ylist_head *i;
512 struct yaffs_SearchContext *sc;
513 struct ylist_head *search_contexts = &(yaffs_DeviceToContext(obj->myDev)->searchContexts);
516 /* Iterate through the directory search contexts.
517 * If any are currently on the object being removed, then advance
518 * the search context to the next object to prevent a hanging pointer.
520 ylist_for_each(i, search_contexts) {
522 sc = ylist_entry(i, struct yaffs_SearchContext,others);
523 if(sc->nextReturn == obj)
524 yaffs_SearchAdvance(sc);
531 /*-----------------------------------------------------------------*/
533 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
536 unsigned char *alias;
539 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
541 yaffs_GrossLock(dev);
543 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
545 yaffs_GrossUnlock(dev);
550 ret = vfs_readlink(dentry, buffer, buflen, alias);
555 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
556 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
558 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
561 unsigned char *alias;
563 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
565 yaffs_GrossLock(dev);
567 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
569 yaffs_GrossUnlock(dev);
576 ret = vfs_follow_link(nd, alias);
579 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
586 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
590 * Lookup is used to find objects in the fs
592 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
594 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
597 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
601 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
603 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
605 yaffs_GrossLock(dev);
608 ("yaffs_lookup for %d:%s\n",
609 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
611 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
612 dentry->d_name.name);
614 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
616 /* Can't hold gross lock when calling yaffs_get_inode() */
617 yaffs_GrossUnlock(dev);
621 ("yaffs_lookup found %d\n", obj->objectId));
623 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
627 ("yaffs_loookup dentry \n"));
628 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
629 * d_add even if NULL inode */
631 /*dget(dentry); // try to solve directory bug */
632 d_add(dentry, inode);
640 T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
644 /* added NCB for 2.5/6 compatability - forces add even if inode is
645 * NULL which creates dentry hash */
646 d_add(dentry, inode);
652 #ifdef YAFFS_HAS_PUT_INODE
654 /* For now put inode is just for debugging
655 * Put inode is called when the inode **structure** is put.
657 static void yaffs_put_inode(struct inode *inode)
660 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
661 atomic_read(&inode->i_count)));
666 /* clear is called to tell the fs to release any per-inode data it holds */
667 static void yaffs_clear_inode(struct inode *inode)
672 obj = yaffs_InodeToObject(inode);
675 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
676 atomic_read(&inode->i_count),
677 obj ? "object exists" : "null object"));
681 yaffs_GrossLock(dev);
683 /* Clear the association between the inode and
687 yaffs_InodeToObjectLV(inode) = NULL;
689 /* If the object freeing was deferred, then the real
691 * This should fix the inode inconsistency problem.
694 yaffs_HandleDeferedFree(obj);
696 yaffs_GrossUnlock(dev);
701 /* delete is called when the link count is zero and the inode
702 * is put (ie. nobody wants to know about it anymore, time to
704 * NB Must call clear_inode()
706 static void yaffs_delete_inode(struct inode *inode)
708 yaffs_Object *obj = yaffs_InodeToObject(inode);
712 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
713 atomic_read(&inode->i_count),
714 obj ? "object exists" : "null object"));
718 yaffs_GrossLock(dev);
719 yaffs_DeleteObject(obj);
720 yaffs_GrossUnlock(dev);
722 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
723 truncate_inode_pages(&inode->i_data, 0);
728 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
729 static int yaffs_file_flush(struct file *file, fl_owner_t id)
731 static int yaffs_file_flush(struct file *file)
734 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
736 yaffs_Device *dev = obj->myDev;
739 ("yaffs_file_flush object %d (%s)\n", obj->objectId,
740 obj->dirty ? "dirty" : "clean"));
742 yaffs_GrossLock(dev);
744 yaffs_FlushFile(obj, 1, 0);
746 yaffs_GrossUnlock(dev);
751 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
753 /* Lifted from jffs2 */
756 unsigned char *pg_buf;
761 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock at %08x, size %08x\n",
762 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
763 (unsigned)PAGE_CACHE_SIZE));
765 obj = yaffs_DentryToObject(f->f_dentry);
769 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
770 BUG_ON(!PageLocked(pg));
777 /* FIXME: Can kmap fail? */
779 yaffs_GrossLock(dev);
781 ret = yaffs_ReadDataFromFile(obj, pg_buf,
782 pg->index << PAGE_CACHE_SHIFT,
785 yaffs_GrossUnlock(dev);
791 ClearPageUptodate(pg);
798 flush_dcache_page(pg);
801 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock done\n"));
805 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
807 int ret = yaffs_readpage_nolock(f, pg);
812 static int yaffs_readpage(struct file *f, struct page *pg)
816 T(YAFFS_TRACE_OS, ("yaffs_readpage\n"));
817 ret=yaffs_readpage_unlock(f, pg);
818 T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
822 /* writepage inspired by/stolen from smbfs */
824 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
825 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
827 static int yaffs_writepage(struct page *page)
830 struct address_space *mapping = page->mapping;
832 unsigned long end_index;
841 inode = mapping->host;
844 i_size = i_size_read(inode);
846 end_index = i_size >> PAGE_CACHE_SHIFT;
848 if(page->index < end_index)
849 nBytes = PAGE_CACHE_SIZE;
851 nBytes = i_size & (PAGE_CACHE_SIZE -1);
853 if (page->index > end_index || !nBytes) {
855 ("yaffs_writepage at %08x, inode size = %08x!!!\n",
856 (unsigned)(page->index << PAGE_CACHE_SHIFT),
857 (unsigned)inode->i_size));
859 (" -> don't care!!\n"));
861 zero_user_segment(page,0,PAGE_CACHE_SIZE);
862 set_page_writeback(page);
864 end_page_writeback(page);
869 if(nBytes != PAGE_CACHE_SIZE)
870 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
876 obj = yaffs_InodeToObject(inode);
877 yaffs_GrossLock(obj->myDev);
880 ("yaffs_writepage at %08x, size %08x\n",
881 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
883 ("writepag0: obj = %05x, ino = %05x\n",
884 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
886 nWritten = yaffs_WriteDataToFile(obj, buffer,
887 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
890 ("writepag1: obj = %05x, ino = %05x\n",
891 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
893 yaffs_GrossUnlock(obj->myDev);
896 set_page_writeback(page);
898 end_page_writeback(page);
901 return (nWritten == nBytes) ? 0 : -ENOSPC;
905 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
906 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
907 loff_t pos, unsigned len, unsigned flags,
908 struct page **pagep, void **fsdata)
910 struct page *pg = NULL;
911 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
917 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
918 pg = grab_cache_page_write_begin(mapping, index, flags);
920 pg = __grab_cache_page(mapping, index);
928 T(YAFFS_TRACE_OS, ("start yaffs_write_begin index %d(%x) uptodate %d\n",(int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
931 space_held = yaffs_hold_space(filp);
938 /* Update page if required */
940 if (!Page_Uptodate(pg))
941 ret = yaffs_readpage_nolock(filp, pg);
946 /* Happy path return */
947 T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
952 T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
954 yaffs_release_space(filp);
957 page_cache_release(pg);
964 static int yaffs_prepare_write(struct file *f, struct page *pg,
965 unsigned offset, unsigned to)
967 T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
969 if (!Page_Uptodate(pg))
970 return yaffs_readpage_nolock(f, pg);
975 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
976 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
977 loff_t pos, unsigned len, unsigned copied,
978 struct page *pg, void *fsdadata)
982 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
985 addr = kva + offset_into_page;
988 ("yaffs_write_end addr %x pos %x nBytes %d\n",
992 ret = yaffs_file_write(filp, addr, copied, &pos);
996 ("yaffs_write_end not same size ret %d copied %d\n",
1005 yaffs_release_space(filp);
1007 page_cache_release(pg);
1012 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1017 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1018 int nBytes = to - offset;
1021 unsigned spos = pos;
1025 addr = kva + offset;
1027 saddr = (unsigned) addr;
1030 ("yaffs_commit_write addr %x pos %x nBytes %d\n",
1031 saddr, spos, nBytes));
1033 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1035 if (nWritten != nBytes) {
1037 ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
1047 ("yaffs_commit_write returning %d\n",
1048 nWritten == nBytes ? 0 : nWritten));
1050 return nWritten == nBytes ? 0 : nWritten;
1055 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1060 /* Check mode against the variant type and attempt to repair if broken. */
1061 __u32 mode = obj->yst_mode;
1062 switch (obj->variantType) {
1063 case YAFFS_OBJECT_TYPE_FILE:
1064 if (!S_ISREG(mode)) {
1065 obj->yst_mode &= ~S_IFMT;
1066 obj->yst_mode |= S_IFREG;
1070 case YAFFS_OBJECT_TYPE_SYMLINK:
1071 if (!S_ISLNK(mode)) {
1072 obj->yst_mode &= ~S_IFMT;
1073 obj->yst_mode |= S_IFLNK;
1077 case YAFFS_OBJECT_TYPE_DIRECTORY:
1078 if (!S_ISDIR(mode)) {
1079 obj->yst_mode &= ~S_IFMT;
1080 obj->yst_mode |= S_IFDIR;
1084 case YAFFS_OBJECT_TYPE_UNKNOWN:
1085 case YAFFS_OBJECT_TYPE_HARDLINK:
1086 case YAFFS_OBJECT_TYPE_SPECIAL:
1092 inode->i_flags |= S_NOATIME;
1094 inode->i_ino = obj->objectId;
1095 inode->i_mode = obj->yst_mode;
1096 inode->i_uid = obj->yst_uid;
1097 inode->i_gid = obj->yst_gid;
1098 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1099 inode->i_blksize = inode->i_sb->s_blocksize;
1101 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1103 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1104 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1105 inode->i_atime.tv_nsec = 0;
1106 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1107 inode->i_mtime.tv_nsec = 0;
1108 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1109 inode->i_ctime.tv_nsec = 0;
1111 inode->i_rdev = obj->yst_rdev;
1112 inode->i_atime = obj->yst_atime;
1113 inode->i_mtime = obj->yst_mtime;
1114 inode->i_ctime = obj->yst_ctime;
1116 inode->i_size = yaffs_GetObjectFileLength(obj);
1117 inode->i_blocks = (inode->i_size + 511) >> 9;
1119 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1122 ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
1123 inode->i_mode, inode->i_uid, inode->i_gid,
1124 (int)inode->i_size, atomic_read(&inode->i_count)));
1126 switch (obj->yst_mode & S_IFMT) {
1127 default: /* fifo, device or socket */
1128 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1129 init_special_inode(inode, obj->yst_mode,
1130 old_decode_dev(obj->yst_rdev));
1132 init_special_inode(inode, obj->yst_mode,
1133 (dev_t) (obj->yst_rdev));
1136 case S_IFREG: /* file */
1137 inode->i_op = &yaffs_file_inode_operations;
1138 inode->i_fop = &yaffs_file_operations;
1139 inode->i_mapping->a_ops =
1140 &yaffs_file_address_operations;
1142 case S_IFDIR: /* directory */
1143 inode->i_op = &yaffs_dir_inode_operations;
1144 inode->i_fop = &yaffs_dir_operations;
1146 case S_IFLNK: /* symlink */
1147 inode->i_op = &yaffs_symlink_inode_operations;
1151 yaffs_InodeToObjectLV(inode) = obj;
1153 obj->myInode = inode;
1157 ("yaffs_FileInode invalid parameters\n"));
1162 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1165 struct inode *inode;
1169 ("yaffs_get_inode for NULL super_block!!\n"));
1176 ("yaffs_get_inode for NULL object!!\n"));
1182 ("yaffs_get_inode for object %d\n", obj->objectId));
1184 inode = Y_IGET(sb, obj->objectId);
1188 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1189 /* iget also increments the inode's i_count */
1190 /* NB You can't be holding grossLock or deadlock will happen! */
1195 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1200 struct inode *inode;
1203 obj = yaffs_DentryToObject(f->f_dentry);
1207 yaffs_GrossLock(dev);
1209 inode = f->f_dentry->d_inode;
1211 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1212 ipos = inode->i_size;
1218 ("yaffs_file_write: hey obj is null!\n"));
1221 ("yaffs_file_write about to write writing %u(%x) bytes"
1222 "to object %d at %d(%x)\n",
1223 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1225 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1228 ("yaffs_file_write: %d(%x) bytes written\n",
1229 (unsigned )n,(unsigned)n));
1234 if (ipos > inode->i_size) {
1235 inode->i_size = ipos;
1236 inode->i_blocks = (ipos + 511) >> 9;
1239 ("yaffs_file_write size updated to %d bytes, "
1241 ipos, (int)(inode->i_blocks)));
1245 yaffs_GrossUnlock(dev);
1246 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1249 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1250 /* For now we just assume few parallel writes and check against a small number. */
1251 /* Todo: need to do this with a counter to handle parallel reads better */
1253 static ssize_t yaffs_hold_space(struct file *f)
1261 obj = yaffs_DentryToObject(f->f_dentry);
1265 yaffs_GrossLock(dev);
1267 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1269 yaffs_GrossUnlock(dev);
1271 return (nFreeChunks > 20) ? 1 : 0;
1274 static void yaffs_release_space(struct file *f)
1280 obj = yaffs_DentryToObject(f->f_dentry);
1284 yaffs_GrossLock(dev);
1287 yaffs_GrossUnlock(dev);
1290 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1294 struct yaffs_SearchContext *sc;
1295 struct inode *inode = f->f_dentry->d_inode;
1296 unsigned long offset, curoffs;
1300 char name[YAFFS_MAX_NAME_LENGTH + 1];
1302 obj = yaffs_DentryToObject(f->f_dentry);
1305 yaffs_GrossLock(dev);
1309 sc = yaffs_NewSearch(obj);
1315 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
1319 ("yaffs_readdir: entry . ino %d \n",
1320 (int)inode->i_ino));
1321 yaffs_GrossUnlock(dev);
1322 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
1324 yaffs_GrossLock(dev);
1330 ("yaffs_readdir: entry .. ino %d \n",
1331 (int)f->f_dentry->d_parent->d_inode->i_ino));
1332 yaffs_GrossUnlock(dev);
1333 if (filldir(dirent, "..", 2, offset,
1334 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
1336 yaffs_GrossLock(dev);
1343 /* If the directory has changed since the open or last call to
1344 readdir, rewind to after the 2 canned entries. */
1346 if (f->f_version != inode->i_version) {
1349 f->f_version = inode->i_version;
1352 while(sc->nextReturn){
1355 if (curoffs >= offset) {
1356 int this_inode = yaffs_GetObjectInode(l);
1357 int this_type = yaffs_GetObjectType(l);
1359 yaffs_GetObjectName(l, name,
1360 YAFFS_MAX_NAME_LENGTH + 1);
1362 ("yaffs_readdir: %s inode %d\n", name,
1363 yaffs_GetObjectInode(l)));
1365 yaffs_GrossUnlock(dev);
1375 yaffs_GrossLock(dev);
1380 yaffs_SearchAdvance(sc);
1384 yaffs_GrossUnlock(dev);
1386 yaffs_EndSearch(sc);
1392 * File creation. Allocate an inode, and we're done..
1395 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1398 #define YCRED(x) (x->cred)
1401 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1402 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1405 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1409 struct inode *inode;
1411 yaffs_Object *obj = NULL;
1414 yaffs_Object *parent = yaffs_InodeToObject(dir);
1416 int error = -ENOSPC;
1417 uid_t uid = YCRED(current)->fsuid;
1418 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1420 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1425 ("yaffs_mknod: parent object %d type %d\n",
1426 parent->objectId, parent->variantType));
1429 ("yaffs_mknod: could not get parent object\n"));
1433 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1435 dentry->d_name.name, mode, rdev));
1437 dev = parent->myDev;
1439 yaffs_GrossLock(dev);
1441 switch (mode & S_IFMT) {
1443 /* Special (socket, fifo, device...) */
1444 T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
1445 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1446 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1447 gid, old_encode_dev(rdev));
1449 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1453 case S_IFREG: /* file */
1454 T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
1455 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1458 case S_IFDIR: /* directory */
1460 ("yaffs_mknod: making directory\n"));
1461 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1464 case S_IFLNK: /* symlink */
1465 T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
1466 obj = NULL; /* Do we ever get here? */
1470 /* Can not call yaffs_get_inode() with gross lock held */
1471 yaffs_GrossUnlock(dev);
1474 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1475 d_instantiate(dentry, inode);
1476 update_dir_time(dir);
1478 ("yaffs_mknod created object %d count = %d\n",
1479 obj->objectId, atomic_read(&inode->i_count)));
1483 ("yaffs_mknod failed making object\n"));
1490 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1493 T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
1494 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1498 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1499 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1500 struct nameidata *n)
1502 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1505 T(YAFFS_TRACE_OS, ("yaffs_create\n"));
1506 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1509 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1516 ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1517 dentry->d_name.name));
1519 dev = yaffs_InodeToObject(dir)->myDev;
1521 yaffs_GrossLock(dev);
1523 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1525 if (retVal == YAFFS_OK) {
1526 dentry->d_inode->i_nlink--;
1528 yaffs_GrossUnlock(dev);
1529 mark_inode_dirty(dentry->d_inode);
1530 update_dir_time(dir);
1533 yaffs_GrossUnlock(dev);
1540 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1541 struct dentry *dentry)
1543 struct inode *inode = old_dentry->d_inode;
1544 yaffs_Object *obj = NULL;
1545 yaffs_Object *link = NULL;
1548 T(YAFFS_TRACE_OS, ("yaffs_link\n"));
1550 obj = yaffs_InodeToObject(inode);
1553 yaffs_GrossLock(dev);
1555 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1556 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1560 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1561 d_instantiate(dentry, old_dentry->d_inode);
1562 atomic_inc(&old_dentry->d_inode->i_count);
1564 ("yaffs_link link count %d i_count %d\n",
1565 old_dentry->d_inode->i_nlink,
1566 atomic_read(&old_dentry->d_inode->i_count)));
1569 yaffs_GrossUnlock(dev);
1572 update_dir_time(dir);
1579 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1580 const char *symname)
1584 uid_t uid = YCRED(current)->fsuid;
1585 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1587 T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
1589 dev = yaffs_InodeToObject(dir)->myDev;
1590 yaffs_GrossLock(dev);
1591 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1592 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1593 yaffs_GrossUnlock(dev);
1596 struct inode *inode;
1598 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1599 d_instantiate(dentry, inode);
1600 update_dir_time(dir);
1601 T(YAFFS_TRACE_OS, ("symlink created OK\n"));
1604 T(YAFFS_TRACE_OS, ("symlink not created\n"));
1610 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1617 obj = yaffs_DentryToObject(dentry);
1621 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_object\n"));
1622 yaffs_GrossLock(dev);
1623 yaffs_FlushFile(obj, 1, datasync);
1624 yaffs_GrossUnlock(dev);
1629 * The VFS layer already does all the dentry stuff for rename.
1631 * NB: POSIX says you can rename an object over an old object of the same name
1633 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1634 struct inode *new_dir, struct dentry *new_dentry)
1637 int retVal = YAFFS_FAIL;
1638 yaffs_Object *target;
1640 T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
1641 dev = yaffs_InodeToObject(old_dir)->myDev;
1643 yaffs_GrossLock(dev);
1645 /* Check if the target is an existing directory that is not empty. */
1646 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1647 new_dentry->d_name.name);
1651 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1652 !ylist_empty(&target->variant.directoryVariant.children)) {
1654 T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
1656 retVal = YAFFS_FAIL;
1658 /* Now does unlinking internally using shadowing mechanism */
1659 T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
1661 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1662 old_dentry->d_name.name,
1663 yaffs_InodeToObject(new_dir),
1664 new_dentry->d_name.name);
1666 yaffs_GrossUnlock(dev);
1668 if (retVal == YAFFS_OK) {
1670 new_dentry->d_inode->i_nlink--;
1671 mark_inode_dirty(new_dentry->d_inode);
1674 update_dir_time(old_dir);
1675 if(old_dir != new_dir)
1676 update_dir_time(new_dir);
1683 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1685 struct inode *inode = dentry->d_inode;
1690 ("yaffs_setattr of object %d\n",
1691 yaffs_InodeToObject(inode)->objectId));
1693 /* Fail if a requested resize >= 2GB */
1694 if (attr->ia_valid & ATTR_SIZE &&
1695 (attr->ia_size >> 31))
1699 error = inode_change_ok(inode, attr);
1703 error = inode_setattr(inode, attr);
1704 T(YAFFS_TRACE_OS,("inode_setattr called\n"));
1705 if (attr->ia_valid & ATTR_SIZE)
1706 truncate_inode_pages(&inode->i_data,attr->ia_size);
1708 dev = yaffs_InodeToObject(inode)->myDev;
1709 if (attr->ia_valid & ATTR_SIZE){
1710 T(YAFFS_TRACE_OS,("resize to %d(%x)\n",(int)(attr->ia_size),(int)(attr->ia_size)));
1712 yaffs_GrossLock(dev);
1713 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1714 if(result == YAFFS_OK) {
1719 yaffs_GrossUnlock(dev);
1724 ("yaffs_setattr done returning %d\n",error));
1729 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1730 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1732 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1733 struct super_block *sb = dentry->d_sb;
1734 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1735 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1737 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1739 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1741 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1744 T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
1746 yaffs_GrossLock(dev);
1748 buf->f_type = YAFFS_MAGIC;
1749 buf->f_bsize = sb->s_blocksize;
1750 buf->f_namelen = 255;
1752 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1753 /* Do this if chunk size is not a power of 2 */
1755 uint64_t bytesInDev;
1758 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
1759 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
1761 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1762 buf->f_blocks = bytesInDev;
1764 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1765 ((uint64_t)(dev->nDataBytesPerChunk));
1767 do_div(bytesFree, sb->s_blocksize);
1769 buf->f_bfree = bytesFree;
1771 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1774 (dev->param.endBlock - dev->param.startBlock + 1) *
1775 dev->param.nChunksPerBlock /
1776 (sb->s_blocksize / dev->nDataBytesPerChunk);
1778 yaffs_GetNumberOfFreeChunks(dev) /
1779 (sb->s_blocksize / dev->nDataBytesPerChunk);
1782 (dev->param.endBlock - dev->param.startBlock + 1) *
1783 dev->param.nChunksPerBlock *
1784 (dev->nDataBytesPerChunk / sb->s_blocksize);
1787 yaffs_GetNumberOfFreeChunks(dev) *
1788 (dev->nDataBytesPerChunk / sb->s_blocksize);
1793 buf->f_bavail = buf->f_bfree;
1795 yaffs_GrossUnlock(dev);
1801 static void yaffs_FlushInodes(struct super_block *sb)
1806 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1807 obj = yaffs_InodeToObject(iptr);
1809 T(YAFFS_TRACE_OS, ("flushing obj %d\n",obj->objectId));
1810 yaffs_FlushFile(obj,1,0);
1816 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
1818 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1822 yaffs_FlushInodes(sb);
1823 yaffs_UpdateDirtyDirectories(dev);
1824 yaffs_FlushEntireDeviceCache(dev);
1826 yaffs_CheckpointSave(dev);
1829 static int yaffs_do_sync_fs(struct super_block *sb, int do_checkpoint)
1832 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1833 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1834 ("yaffs_do_sync_fs: %s %s\n",
1835 sb->s_dirt ? "dirty" : "clean",
1836 do_checkpoint ? "with checkpoint" : "no checkpoint"));
1839 yaffs_GrossLock(dev);
1840 yaffs_FlushSuperBlock(sb,do_checkpoint);
1841 yaffs_GrossUnlock(dev);
1849 * yaffs background thread functions .
1850 * yaffs_BackgroundThread() the thread function
1851 * yaffs_BackgroundStart() launches the background thread.
1852 * yaffs_BackgroundStop() cleans up the background thread.
1855 * The thread should only run after the yaffs is initialised
1856 * The thread should be stopped before yaffs is unmounted.
1857 * The thread should not do any writing while the fs is in read only.
1860 #ifdef YAFFS_COMPILE_BACKGROUND
1861 static int yaffs_BackgroundThread(void *data)
1863 yaffs_Device *dev = (yaffs_Device *)data;
1864 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
1866 T(YAFFS_TRACE_BACKGROUND,
1867 ("yaffs_background starting for dev %p\n",
1870 while(context->bgRunning){
1871 T(YAFFS_TRACE_BACKGROUND,
1872 ("yaffs_background\n"));
1874 if(kthread_should_stop())
1876 yaffs_GrossLock(dev);
1877 yaffs_UpdateDirtyDirectories(dev);
1878 yaffs_GrossUnlock(dev);
1884 static int yaffs_BackgroundStart(yaffs_Device *dev)
1888 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
1890 context->bgRunning = 1;
1892 context->bgThread = kthread_run(yaffs_BackgroundThread,(void *)dev,"yaffs_%x",(unsigned)dev);
1894 if(IS_ERR(context->bgThread)){
1895 retval = PTR_ERR(context->bgThread);
1896 context->bgThread = NULL;
1897 context->bgRunning = 0;
1902 static void yaffs_BackgroundStop(yaffs_Device *dev)
1904 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToContext(dev);
1906 ctxt->bgRunning = 0;
1908 if( ctxt->bgThread){
1909 kthread_stop(ctxt->bgThread);
1910 ctxt->bgThread = NULL;
1914 static int yaffs_BackgroundThread(void *data)
1919 static int yaffs_BackgroundStart(yaffs_Device *dev)
1924 static void yaffs_BackgroundStop(yaffs_Device *dev)
1930 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1931 static void yaffs_write_super(struct super_block *sb)
1933 static int yaffs_write_super(struct super_block *sb)
1937 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_write_super\n"));
1938 yaffs_do_sync_fs(sb, yaffs_auto_checkpoint >= 2);
1940 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
1946 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1947 static int yaffs_sync_fs(struct super_block *sb, int wait)
1949 static int yaffs_sync_fs(struct super_block *sb)
1952 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_fs\n"));
1954 yaffs_do_sync_fs(sb,yaffs_auto_checkpoint >= 1);
1959 #ifdef YAFFS_USE_OWN_IGET
1961 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1963 struct inode *inode;
1965 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1968 ("yaffs_iget for %lu\n", ino));
1970 inode = iget_locked(sb, ino);
1972 return ERR_PTR(-ENOMEM);
1973 if (!(inode->i_state & I_NEW))
1976 /* NB This is called as a side effect of other functions, but
1977 * we had to release the lock to prevent deadlocks, so
1978 * need to lock again.
1981 yaffs_GrossLock(dev);
1983 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1985 yaffs_FillInodeFromObject(inode, obj);
1987 yaffs_GrossUnlock(dev);
1989 unlock_new_inode(inode);
1995 static void yaffs_read_inode(struct inode *inode)
1997 /* NB This is called as a side effect of other functions, but
1998 * we had to release the lock to prevent deadlocks, so
1999 * need to lock again.
2003 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2006 ("yaffs_read_inode for %d\n", (int)inode->i_ino));
2008 yaffs_GrossLock(dev);
2010 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2012 yaffs_FillInodeFromObject(inode, obj);
2014 yaffs_GrossUnlock(dev);
2019 static YLIST_HEAD(yaffs_context_list);
2020 struct semaphore yaffs_context_lock;
2022 #if 0 /* not used */
2023 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2025 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2027 if (*flags & MS_RDONLY) {
2028 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2031 ("yaffs_remount_fs: %s: RO\n", dev->name));
2033 yaffs_GrossLock(dev);
2035 yaffs_FlushSuperBlock(sb,1);
2040 yaffs_GrossUnlock(dev);
2043 ("yaffs_remount_fs: %s: RW\n", dev->name));
2050 static void yaffs_put_super(struct super_block *sb)
2052 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2054 T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
2056 yaffs_GrossLock(dev);
2058 yaffs_FlushSuperBlock(sb,1);
2060 if (yaffs_DeviceToContext(dev)->putSuperFunc)
2061 yaffs_DeviceToContext(dev)->putSuperFunc(sb);
2063 yaffs_BackgroundStop(dev);
2065 yaffs_Deinitialise(dev);
2067 yaffs_GrossUnlock(dev);
2069 down(&yaffs_context_lock);
2070 ylist_del_init(&(yaffs_DeviceToContext(dev)->contextList));
2071 up(&yaffs_context_lock);
2073 if (yaffs_DeviceToContext(dev)->spareBuffer) {
2074 YFREE(yaffs_DeviceToContext(dev)->spareBuffer);
2075 yaffs_DeviceToContext(dev)->spareBuffer = NULL;
2082 static void yaffs_MTDPutSuper(struct super_block *sb)
2084 struct mtd_info *mtd = yaffs_DeviceToContext(yaffs_SuperToDevice(sb))->mtd;
2089 put_mtd_device(mtd);
2093 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2095 struct super_block *sb = yaffs_DeviceToContext(dev)->superBlock;
2097 T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
2104 int skip_checkpoint_read;
2105 int skip_checkpoint_write;
2108 int tags_ecc_overridden;
2109 int lazy_loading_enabled;
2110 int lazy_loading_overridden;
2111 int empty_lost_and_found;
2112 int empty_lost_and_found_overridden;
2115 #define MAX_OPT_LEN 30
2116 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2118 char cur_opt[MAX_OPT_LEN + 1];
2122 /* Parse through the options which is a comma seperated list */
2124 while (options_str && *options_str && !error) {
2125 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2128 while(*options_str == ',')
2131 while (*options_str && *options_str != ',') {
2132 if (p < MAX_OPT_LEN) {
2133 cur_opt[p] = *options_str;
2139 if (!strcmp(cur_opt, "inband-tags"))
2140 options->inband_tags = 1;
2141 else if (!strcmp(cur_opt, "tags-ecc-off")){
2142 options->tags_ecc_on = 0;
2143 options->tags_ecc_overridden=1;
2144 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2145 options->tags_ecc_on = 1;
2146 options->tags_ecc_overridden = 1;
2147 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2148 options->lazy_loading_enabled = 0;
2149 options->lazy_loading_overridden=1;
2150 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2151 options->lazy_loading_enabled = 1;
2152 options->lazy_loading_overridden = 1;
2153 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2154 options->empty_lost_and_found = 0;
2155 options->empty_lost_and_found_overridden=1;
2156 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2157 options->empty_lost_and_found = 1;
2158 options->empty_lost_and_found_overridden=1;
2159 } else if (!strcmp(cur_opt, "no-cache"))
2160 options->no_cache = 1;
2161 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2162 options->skip_checkpoint_read = 1;
2163 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2164 options->skip_checkpoint_write = 1;
2165 else if (!strcmp(cur_opt, "no-checkpoint")) {
2166 options->skip_checkpoint_read = 1;
2167 options->skip_checkpoint_write = 1;
2169 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2178 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2179 struct super_block *sb,
2180 void *data, int silent)
2183 struct inode *inode = NULL;
2184 struct dentry *root;
2185 yaffs_Device *dev = 0;
2186 char devname_buf[BDEVNAME_SIZE + 1];
2187 struct mtd_info *mtd;
2189 char *data_str = (char *)data;
2190 struct yaffs_LinuxContext *context = NULL;
2191 yaffs_DeviceParam *param;
2193 yaffs_options options;
2195 sb->s_magic = YAFFS_MAGIC;
2196 sb->s_op = &yaffs_super_ops;
2197 sb->s_flags |= MS_NOATIME;
2200 printk(KERN_INFO "yaffs: sb is NULL\n");
2201 else if (!sb->s_dev)
2202 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2203 else if (!yaffs_devname(sb, devname_buf))
2204 printk(KERN_INFO "yaffs: devname is NULL\n");
2206 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2208 yaffs_devname(sb, devname_buf));
2213 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2215 memset(&options, 0, sizeof(options));
2217 if (yaffs_parse_options(&options, data_str)) {
2218 /* Option parsing failed */
2223 sb->s_blocksize = PAGE_CACHE_SIZE;
2224 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2225 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2227 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2229 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2231 ("yaffs: Write verification disabled. All guarantees "
2232 "null and void\n"));
2235 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2237 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2238 yaffs_devname(sb, devname_buf)));
2240 /* Check it's an mtd device..... */
2241 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2242 return NULL; /* This isn't an mtd device */
2244 /* Get the device */
2245 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2247 T(YAFFS_TRACE_ALWAYS,
2248 ("yaffs: MTD device #%u doesn't appear to exist\n",
2252 /* Check it's NAND */
2253 if (mtd->type != MTD_NANDFLASH) {
2254 T(YAFFS_TRACE_ALWAYS,
2255 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
2259 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
2260 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
2261 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
2262 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
2263 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
2264 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
2265 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
2266 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2267 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
2268 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
2269 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2270 T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
2272 T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
2275 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2277 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2278 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
2282 /* Added NCB 26/5/2006 for completeness */
2283 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2284 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
2290 if (yaffsVersion == 2) {
2291 /* Check for version 2 style functions */
2293 !mtd->block_isbad ||
2294 !mtd->block_markbad ||
2297 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2298 !mtd->read_oob || !mtd->write_oob) {
2301 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2303 T(YAFFS_TRACE_ALWAYS,
2304 ("yaffs: MTD device does not support required "
2309 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2310 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2311 !options.inband_tags) {
2312 T(YAFFS_TRACE_ALWAYS,
2313 ("yaffs: MTD device does not have the "
2314 "right page sizes\n"));
2318 /* Check for V1 style functions */
2322 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2323 !mtd->read_oob || !mtd->write_oob) {
2326 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2328 T(YAFFS_TRACE_ALWAYS,
2329 ("yaffs: MTD device does not support required "
2334 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2335 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2336 T(YAFFS_TRACE_ALWAYS,
2337 ("yaffs: MTD device does not support have the "
2338 "right page sizes\n"));
2343 /* OK, so if we got here, we have an MTD that's NAND and looks
2344 * like it has the right capabilities
2345 * Set the yaffs_Device up for mtd
2348 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2349 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2351 if(!dev || !context ){
2361 /* Deep shit could not allocate device structure */
2362 T(YAFFS_TRACE_ALWAYS,
2363 ("yaffs_read_super: Failed trying to allocate "
2364 "yaffs_Device. \n"));
2367 memset(dev, 0, sizeof(yaffs_Device));
2368 param = &(dev->param);
2370 memset(context,0,sizeof(struct yaffs_LinuxContext));
2371 dev->context = context;
2372 YINIT_LIST_HEAD(&(context->contextList));
2374 context->superBlock = sb;
2378 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2379 sb->s_fs_info = dev;
2381 sb->u.generic_sbp = dev;
2384 yaffs_DeviceToContext(dev)->mtd = mtd;
2385 param->name = mtd->name;
2387 /* Set up the memory size parameters.... */
2389 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2391 param->startBlock = 0;
2392 param->endBlock = nBlocks - 1;
2393 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2394 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2395 param->nReservedBlocks = 5;
2396 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2397 param->inbandTags = options.inband_tags;
2399 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2400 param->disableLazyLoad = 1;
2402 if(options.lazy_loading_overridden)
2403 param->disableLazyLoad = !options.lazy_loading_enabled;
2405 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2406 param->noTagsECC = 1;
2409 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2411 param->deferDirectoryUpdate = 1;
2414 if(options.tags_ecc_overridden)
2415 param->noTagsECC = !options.tags_ecc_on;
2417 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2418 param->emptyLostAndFound = 1;
2421 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2422 param->refreshPeriod = 0;
2424 param->refreshPeriod = 10000;
2427 if(options.empty_lost_and_found_overridden)
2428 param->emptyLostAndFound = options.empty_lost_and_found;
2430 /* ... and the functions. */
2431 if (yaffsVersion == 2) {
2432 param->writeChunkWithTagsToNAND =
2433 nandmtd2_WriteChunkWithTagsToNAND;
2434 param->readChunkWithTagsFromNAND =
2435 nandmtd2_ReadChunkWithTagsFromNAND;
2436 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2437 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2438 yaffs_DeviceToContext(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2439 param->isYaffs2 = 1;
2440 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2441 param->totalBytesPerChunk = mtd->writesize;
2442 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2444 param->totalBytesPerChunk = mtd->oobblock;
2445 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2447 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2449 param->startBlock = 0;
2450 param->endBlock = nBlocks - 1;
2452 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2453 /* use the MTD interface in yaffs_mtdif1.c */
2454 param->writeChunkWithTagsToNAND =
2455 nandmtd1_WriteChunkWithTagsToNAND;
2456 param->readChunkWithTagsFromNAND =
2457 nandmtd1_ReadChunkWithTagsFromNAND;
2458 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2459 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2461 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2462 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2464 param->isYaffs2 = 0;
2466 /* ... and common functions */
2467 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2468 param->initialiseNAND = nandmtd_InitialiseNAND;
2470 yaffs_DeviceToContext(dev)->putSuperFunc = yaffs_MTDPutSuper;
2472 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2473 param->gcControl = yaffs_gc_control_callback;
2475 yaffs_DeviceToContext(dev)->superBlock= sb;
2478 #ifndef CONFIG_YAFFS_DOES_ECC
2479 param->useNANDECC = 1;
2482 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2483 param->wideTnodesDisabled = 1;
2486 param->skipCheckpointRead = options.skip_checkpoint_read;
2487 param->skipCheckpointWrite = options.skip_checkpoint_write;
2489 /* we assume this is protected by lock_kernel() in mount/umount */
2490 down(&yaffs_context_lock);
2491 ylist_add_tail(&(yaffs_DeviceToContext(dev)->contextList), &yaffs_context_list);
2492 up(&yaffs_context_lock);
2494 /* Directory search handling...*/
2495 YINIT_LIST_HEAD(&(yaffs_DeviceToContext(dev)->searchContexts));
2496 param->removeObjectCallback = yaffs_RemoveObjectCallback;
2498 init_MUTEX(&(yaffs_DeviceToContext(dev)->grossLock));
2500 yaffs_GrossLock(dev);
2502 err = yaffs_GutsInitialise(dev);
2505 ("yaffs_read_super: guts initialised %s\n",
2506 (err == YAFFS_OK) ? "OK" : "FAILED"));
2509 yaffs_BackgroundStart(dev);
2511 if(!context->bgThread)
2512 param->deferDirectoryUpdate = 0;
2515 /* Release lock before yaffs_get_inode() */
2516 yaffs_GrossUnlock(dev);
2518 /* Create root inode */
2519 if (err == YAFFS_OK)
2520 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2526 inode->i_op = &yaffs_dir_inode_operations;
2527 inode->i_fop = &yaffs_dir_operations;
2529 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
2531 root = d_alloc_root(inode);
2533 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
2540 sb->s_dirt = !dev->isCheckpointed;
2541 T(YAFFS_TRACE_ALWAYS,
2542 ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
2544 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
2549 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2550 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2553 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2556 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2557 static int yaffs_read_super(struct file_system_type *fs,
2558 int flags, const char *dev_name,
2559 void *data, struct vfsmount *mnt)
2562 return get_sb_bdev(fs, flags, dev_name, data,
2563 yaffs_internal_read_super_mtd, mnt);
2566 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2567 int flags, const char *dev_name,
2571 return get_sb_bdev(fs, flags, dev_name, data,
2572 yaffs_internal_read_super_mtd);
2576 static struct file_system_type yaffs_fs_type = {
2577 .owner = THIS_MODULE,
2579 .get_sb = yaffs_read_super,
2580 .kill_sb = kill_block_super,
2581 .fs_flags = FS_REQUIRES_DEV,
2584 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2587 return yaffs_internal_read_super(1, sb, data, silent);
2590 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2595 #ifdef CONFIG_YAFFS_YAFFS2
2597 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2598 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2601 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2604 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2605 static int yaffs2_read_super(struct file_system_type *fs,
2606 int flags, const char *dev_name, void *data,
2607 struct vfsmount *mnt)
2609 return get_sb_bdev(fs, flags, dev_name, data,
2610 yaffs2_internal_read_super_mtd, mnt);
2613 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2614 int flags, const char *dev_name,
2618 return get_sb_bdev(fs, flags, dev_name, data,
2619 yaffs2_internal_read_super_mtd);
2623 static struct file_system_type yaffs2_fs_type = {
2624 .owner = THIS_MODULE,
2626 .get_sb = yaffs2_read_super,
2627 .kill_sb = kill_block_super,
2628 .fs_flags = FS_REQUIRES_DEV,
2631 static struct super_block *yaffs2_read_super(struct super_block *sb,
2632 void *data, int silent)
2634 return yaffs_internal_read_super(2, sb, data, silent);
2637 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2641 #endif /* CONFIG_YAFFS_YAFFS2 */
2643 static struct proc_dir_entry *my_proc_entry;
2644 static struct proc_dir_entry *debug_proc_entry;
2646 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
2648 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
2649 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
2650 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
2651 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
2652 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
2653 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
2654 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
2655 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
2656 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
2657 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
2658 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
2659 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
2661 buf += sprintf(buf, "\n");
2667 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
2669 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2670 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2671 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2672 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2673 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2674 buf += sprintf(buf, "\n");
2675 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2676 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2677 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2678 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2679 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2680 buf += sprintf(buf, "\n");
2681 buf += sprintf(buf, "nPageWrites........ %u\n", dev->nPageWrites);
2682 buf += sprintf(buf, "nPageReads......... %u\n", dev->nPageReads);
2683 buf += sprintf(buf, "nBlockErasures..... %u\n", dev->nBlockErasures);
2684 buf += sprintf(buf, "nGCCopies.......... %u\n", dev->nGCCopies);
2685 buf += sprintf(buf, "garbageCollections. %u\n", dev->garbageCollections);
2686 buf += sprintf(buf, "passiveGCs......... %u\n", dev->passiveGarbageCollections);
2687 buf += sprintf(buf, "nRetriedWrites..... %u\n", dev->nRetriedWrites);
2688 buf += sprintf(buf, "nRetireBlocks...... %u\n", dev->nRetiredBlocks);
2689 buf += sprintf(buf, "eccFixed........... %u\n", dev->eccFixed);
2690 buf += sprintf(buf, "eccUnfixed......... %u\n", dev->eccUnfixed);
2691 buf += sprintf(buf, "tagsEccFixed....... %u\n", dev->tagsEccFixed);
2692 buf += sprintf(buf, "tagsEccUnfixed..... %u\n", dev->tagsEccUnfixed);
2693 buf += sprintf(buf, "cacheHits.......... %u\n", dev->cacheHits);
2694 buf += sprintf(buf, "nDeletedFiles...... %u\n", dev->nDeletedFiles);
2695 buf += sprintf(buf, "nUnlinkedFiles..... %u\n", dev->nUnlinkedFiles);
2696 buf += sprintf(buf, "refreshCount....... %u\n", dev->refreshCount);
2698 sprintf(buf, "nBackgroudDeletions %u\n", dev->nBackgroundDeletions);
2703 static int yaffs_proc_read(char *page,
2705 off_t offset, int count, int *eof, void *data)
2707 struct ylist_head *item;
2712 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2713 * We use 'offset' (*ppos) to indicate where we are in devList.
2714 * This also assumes the user has posted a read buffer large
2715 * enough to hold the complete output; but that's life in /proc.
2720 /* Print header first */
2722 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2723 "\n%s\n%s\n", yaffs_fs_c_version,
2724 yaffs_guts_c_version);
2726 buf += sprintf(buf,"\n");
2730 down(&yaffs_context_lock);
2732 /* Locate and print the Nth entry. Order N-squared but N is small. */
2733 ylist_for_each(item, &yaffs_context_list) {
2734 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2735 yaffs_Device *dev = dc->dev;
2737 if (n < (step & ~1)) {
2742 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
2743 buf = yaffs_dump_dev_part0(buf, dev);
2745 buf = yaffs_dump_dev_part1(buf, dev);
2749 up(&yaffs_context_lock);
2752 return buf - page < count ? buf - page : count;
2755 static int yaffs_debug_proc_read(char *page,
2757 off_t offset, int count, int *eof, void *data)
2759 struct ylist_head *item;
2763 down(&yaffs_context_lock);
2765 /* Locate and print the Nth entry. Order N-squared but N is small. */
2766 ylist_for_each(item, &yaffs_context_list) {
2767 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2768 yaffs_Device *dev = dc->dev;
2774 erasedChunks = dev->nErasedBlocks * dev->param.nChunksPerBlock;
2775 nObjects = dev->nObjectsCreated -dev->nFreeObjects;
2776 nTnodes = dev->nTnodesCreated - dev->nFreeTnodes;
2779 buf += sprintf(buf,"%d, %d, %d, %d, %d\n",
2780 n, dev->nFreeChunks, erasedChunks,
2783 up(&yaffs_context_lock);
2786 return buf - page < count ? buf - page : count;
2790 * Set the verbosity of the warnings and error messages.
2792 * Note that the names can only be a..z or _ with the current code.
2797 unsigned mask_bitfield;
2799 {"allocate", YAFFS_TRACE_ALLOCATE},
2800 {"always", YAFFS_TRACE_ALWAYS},
2801 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2802 {"buffers", YAFFS_TRACE_BUFFERS},
2803 {"bug", YAFFS_TRACE_BUG},
2804 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2805 {"deletion", YAFFS_TRACE_DELETION},
2806 {"erase", YAFFS_TRACE_ERASE},
2807 {"error", YAFFS_TRACE_ERROR},
2808 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2809 {"gc", YAFFS_TRACE_GC},
2810 {"mtd", YAFFS_TRACE_MTD},
2811 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2812 {"os", YAFFS_TRACE_OS},
2813 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2814 {"scan", YAFFS_TRACE_SCAN},
2815 {"tracing", YAFFS_TRACE_TRACING},
2816 {"sync", YAFFS_TRACE_SYNC},
2818 {"background", YAFFS_TRACE_BACKGROUND},
2820 {"verify", YAFFS_TRACE_VERIFY},
2821 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2822 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2823 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2825 {"write", YAFFS_TRACE_WRITE},
2826 {"all", 0xffffffff},
2831 #define MAX_MASK_NAME_LENGTH 40
2832 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2833 unsigned long count, void *data)
2835 unsigned rg = 0, mask_bitfield;
2839 char substring[MAX_MASK_NAME_LENGTH + 1];
2845 rg = yaffs_traceMask;
2847 while (!done && (pos < count)) {
2849 while ((pos < count) && isspace(buf[pos]))
2866 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2868 if (end > buf + pos) {
2869 mask_name = "numeral";
2870 len = end - (buf + pos);
2874 for (x = buf + pos, i = 0;
2875 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2876 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2878 substring[i] = '\0';
2880 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2881 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
2882 mask_name = mask_flags[i].mask_name;
2883 mask_bitfield = mask_flags[i].mask_bitfield;
2890 if (mask_name != NULL) {
2894 rg &= ~mask_bitfield;
2897 rg |= mask_bitfield;
2903 rg |= mask_bitfield;
2909 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2911 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
2913 if (rg & YAFFS_TRACE_ALWAYS) {
2914 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2916 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2917 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
2925 static int yaffs_proc_write(struct file *file, const char *buf,
2926 unsigned long count, void *data)
2928 return yaffs_proc_write_trace_options(file, buf, count, data);
2931 /* Stuff to handle installation of file systems */
2932 struct file_system_to_install {
2933 struct file_system_type *fst;
2937 static struct file_system_to_install fs_to_install[] = {
2938 {&yaffs_fs_type, 0},
2939 {&yaffs2_fs_type, 0},
2943 static int __init init_yaffs_fs(void)
2946 struct file_system_to_install *fsinst;
2948 T(YAFFS_TRACE_ALWAYS,
2949 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2951 init_MUTEX(&yaffs_context_lock);
2953 /* Install the proc_fs entries */
2954 my_proc_entry = create_proc_entry("yaffs",
2958 if (my_proc_entry) {
2959 my_proc_entry->write_proc = yaffs_proc_write;
2960 my_proc_entry->read_proc = yaffs_proc_read;
2961 my_proc_entry->data = NULL;
2965 debug_proc_entry = create_proc_entry("yaffs_debug",
2969 if (debug_proc_entry) {
2970 debug_proc_entry->write_proc = NULL;
2971 debug_proc_entry->read_proc = yaffs_debug_proc_read;
2972 debug_proc_entry->data = NULL;
2976 /* Now add the file system entries */
2978 fsinst = fs_to_install;
2980 while (fsinst->fst && !error) {
2981 error = register_filesystem(fsinst->fst);
2983 fsinst->installed = 1;
2987 /* Any errors? uninstall */
2989 fsinst = fs_to_install;
2991 while (fsinst->fst) {
2992 if (fsinst->installed) {
2993 unregister_filesystem(fsinst->fst);
2994 fsinst->installed = 0;
3003 static void __exit exit_yaffs_fs(void)
3006 struct file_system_to_install *fsinst;
3008 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3011 remove_proc_entry("yaffs", YPROC_ROOT);
3013 fsinst = fs_to_install;
3015 while (fsinst->fst) {
3016 if (fsinst->installed) {
3017 unregister_filesystem(fsinst->fst);
3018 fsinst->installed = 0;
3024 module_init(init_yaffs_fs)
3025 module_exit(exit_yaffs_fs)
3027 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3028 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
3029 MODULE_LICENSE("GPL");