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.96 2010-02-25 22:41:46 charles Exp $";
36 extern const char *yaffs_guts_c_version;
38 #include <linux/version.h>
39 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
40 #include <linux/config.h>
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/init.h>
47 #include <linux/proc_fs.h>
48 #include <linux/smp_lock.h>
49 #include <linux/pagemap.h>
50 #include <linux/mtd/mtd.h>
51 #include <linux/interrupt.h>
52 #include <linux/string.h>
53 #include <linux/ctype.h>
55 #include "asm/div64.h"
60 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
62 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
63 #include <linux/statfs.h>
64 #define UnlockPage(p) unlock_page(p)
65 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
67 /* FIXME: use sb->s_id instead ? */
68 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
72 #include <linux/locks.h>
73 #define BDEVNAME_SIZE 0
74 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
76 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
77 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
83 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
84 #define YPROC_ROOT (&proc_root)
86 #define YPROC_ROOT NULL
89 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
90 #define WRITE_SIZE_STR "writesize"
91 #define WRITE_SIZE(mtd) ((mtd)->writesize)
93 #define WRITE_SIZE_STR "oobblock"
94 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
97 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
98 #define YAFFS_USE_WRITE_BEGIN_END 1
100 #define YAFFS_USE_WRITE_BEGIN_END 0
103 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
104 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
106 uint64_t result = partition_size;
107 do_div(result, block_size);
108 return (uint32_t)result;
111 #define YCALCBLOCKS(s, b) ((s)/(b))
114 #include <linux/uaccess.h>
116 #include "yportenv.h"
117 #include "yaffs_trace.h"
118 #include "yaffs_guts.h"
120 #include "yaffs_linux.h"
122 #include <linux/mtd/mtd.h>
123 #include "yaffs_mtdif.h"
124 #include "yaffs_mtdif1.h"
125 #include "yaffs_mtdif2.h"
127 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
128 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
129 unsigned int yaffs_auto_checkpoint = 1;
131 /* Module Parameters */
132 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
133 module_param(yaffs_traceMask, uint, 0644);
134 module_param(yaffs_wr_attempts, uint, 0644);
135 module_param(yaffs_auto_checkpoint, uint, 0644);
137 MODULE_PARM(yaffs_traceMask, "i");
138 MODULE_PARM(yaffs_wr_attempts, "i");
139 MODULE_PARM(yaffs_auto_checkpoint, "i");
142 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
143 /* use iget and read_inode */
144 #define Y_IGET(sb, inum) iget((sb), (inum))
145 static void yaffs_read_inode(struct inode *inode);
148 /* Call local equivalent */
149 #define YAFFS_USE_OWN_IGET
150 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
152 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
155 /*#define T(x) printk x */
157 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
158 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
160 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
163 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
164 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
166 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
167 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
169 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
173 #define update_dir_time(dir) do {\
174 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
177 static void yaffs_put_super(struct super_block *sb);
179 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
181 static ssize_t yaffs_hold_space(struct file *f);
182 static void yaffs_release_space(struct file *f);
184 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
185 static int yaffs_file_flush(struct file *file, fl_owner_t id);
187 static int yaffs_file_flush(struct file *file);
190 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
193 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
195 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
196 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
197 struct nameidata *n);
198 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
199 struct nameidata *n);
201 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
202 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
204 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
205 struct dentry *dentry);
206 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
207 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
208 const char *symname);
209 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
211 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
212 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
215 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
218 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
219 struct inode *new_dir, struct dentry *new_dentry);
220 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
222 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
223 static int yaffs_sync_fs(struct super_block *sb, int wait);
224 static void yaffs_write_super(struct super_block *sb);
226 static int yaffs_sync_fs(struct super_block *sb);
227 static int yaffs_write_super(struct super_block *sb);
230 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
231 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
232 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
233 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
235 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
238 #ifdef YAFFS_HAS_PUT_INODE
239 static void yaffs_put_inode(struct inode *inode);
242 static void yaffs_delete_inode(struct inode *);
243 static void yaffs_clear_inode(struct inode *);
245 static int yaffs_readpage(struct file *file, struct page *page);
246 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
247 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
249 static int yaffs_writepage(struct page *page);
253 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
254 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
255 loff_t pos, unsigned len, unsigned flags,
256 struct page **pagep, void **fsdata);
257 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
258 loff_t pos, unsigned len, unsigned copied,
259 struct page *pg, void *fsdadata);
261 static int yaffs_prepare_write(struct file *f, struct page *pg,
262 unsigned offset, unsigned to);
263 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
268 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
270 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
271 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
273 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
276 static struct address_space_operations yaffs_file_address_operations = {
277 .readpage = yaffs_readpage,
278 .writepage = yaffs_writepage,
279 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
280 .write_begin = yaffs_write_begin,
281 .write_end = yaffs_write_end,
283 .prepare_write = yaffs_prepare_write,
284 .commit_write = yaffs_commit_write,
288 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
289 static const struct file_operations yaffs_file_operations = {
290 .read = do_sync_read,
291 .write = do_sync_write,
292 .aio_read = generic_file_aio_read,
293 .aio_write = generic_file_aio_write,
294 .mmap = generic_file_mmap,
295 .flush = yaffs_file_flush,
296 .fsync = yaffs_sync_object,
297 .splice_read = generic_file_splice_read,
298 .splice_write = generic_file_splice_write,
299 .llseek = generic_file_llseek,
302 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
304 static const struct file_operations yaffs_file_operations = {
305 .read = do_sync_read,
306 .write = do_sync_write,
307 .aio_read = generic_file_aio_read,
308 .aio_write = generic_file_aio_write,
309 .mmap = generic_file_mmap,
310 .flush = yaffs_file_flush,
311 .fsync = yaffs_sync_object,
312 .sendfile = generic_file_sendfile,
317 static const struct file_operations yaffs_file_operations = {
318 .read = generic_file_read,
319 .write = generic_file_write,
320 .mmap = generic_file_mmap,
321 .flush = yaffs_file_flush,
322 .fsync = yaffs_sync_object,
323 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
324 .sendfile = generic_file_sendfile,
329 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
330 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
332 void * kaddr = kmap_atomic(page, KM_USER0);
333 memset(kaddr + start, 0, end - start);
334 kunmap_atomic(kaddr, KM_USER0);
335 flush_dcache_page(page);
340 static const struct inode_operations yaffs_file_inode_operations = {
341 .setattr = yaffs_setattr,
344 static const struct inode_operations yaffs_symlink_inode_operations = {
345 .readlink = yaffs_readlink,
346 .follow_link = yaffs_follow_link,
347 .setattr = yaffs_setattr,
350 static const struct inode_operations yaffs_dir_inode_operations = {
351 .create = yaffs_create,
352 .lookup = yaffs_lookup,
354 .unlink = yaffs_unlink,
355 .symlink = yaffs_symlink,
356 .mkdir = yaffs_mkdir,
357 .rmdir = yaffs_unlink,
358 .mknod = yaffs_mknod,
359 .rename = yaffs_rename,
360 .setattr = yaffs_setattr,
363 static const struct file_operations yaffs_dir_operations = {
364 .read = generic_read_dir,
365 .readdir = yaffs_readdir,
366 .fsync = yaffs_sync_object,
369 static const struct super_operations yaffs_super_ops = {
370 .statfs = yaffs_statfs,
372 #ifndef YAFFS_USE_OWN_IGET
373 .read_inode = yaffs_read_inode,
375 #ifdef YAFFS_HAS_PUT_INODE
376 .put_inode = yaffs_put_inode,
378 .put_super = yaffs_put_super,
379 .delete_inode = yaffs_delete_inode,
380 .clear_inode = yaffs_clear_inode,
381 .sync_fs = yaffs_sync_fs,
382 .write_super = yaffs_write_super,
385 static void yaffs_GrossLock(yaffs_Device *dev)
387 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
388 down(&(yaffs_DeviceToContext(dev)->grossLock));
389 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
392 static void yaffs_GrossUnlock(yaffs_Device *dev)
394 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
395 up(&(yaffs_DeviceToContext(dev)->grossLock));
399 /*-----------------------------------------------------------------*/
400 /* Directory search context allows us to unlock access to yaffs during
401 * filldir without causing problems with the directory being modified.
402 * This is similar to the tried and tested mechanism used in yaffs direct.
404 * A search context iterates along a doubly linked list of siblings in the
405 * directory. If the iterating object is deleted then this would corrupt
406 * the list iteration, likely causing a crash. The search context avoids
407 * this by using the removeObjectCallback to move the search context to the
408 * next object before the object is deleted.
410 * Many readdirs (and thus seach conexts) may be alive simulateously so
411 * each yaffs_Device has a list of these.
413 * A seach context lives for the duration of a readdir.
415 * All these functions must be called while yaffs is locked.
418 struct yaffs_SearchContext {
420 yaffs_Object *dirObj;
421 yaffs_Object *nextReturn;
422 struct ylist_head others;
426 * yaffs_NewSearch() creates a new search context, initialises it and
427 * adds it to the device's search context list.
429 * Called at start of readdir.
431 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
433 yaffs_Device *dev = dir->myDev;
434 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
438 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
439 sc->nextReturn = NULL;
441 sc->nextReturn = ylist_entry(
442 dir->variant.directoryVariant.children.next,
443 yaffs_Object,siblings);
444 YINIT_LIST_HEAD(&sc->others);
445 ylist_add(&sc->others,&(yaffs_DeviceToContext(dev)->searchContexts));
451 * yaffs_EndSearch() disposes of a search context and cleans up.
453 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
456 ylist_del(&sc->others);
462 * yaffs_SearchAdvance() moves a search context to the next object.
463 * Called when the search iterates or when an object removal causes
464 * the search context to be moved to the next object.
466 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
471 if( sc->nextReturn == NULL ||
472 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
473 sc->nextReturn = NULL;
475 struct ylist_head *next = sc->nextReturn->siblings.next;
477 if( next == &sc->dirObj->variant.directoryVariant.children)
478 sc->nextReturn = NULL; /* end of list */
480 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
485 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
486 * We check open search contexts and advance any which are currently
487 * on the object being iterated.
489 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
492 struct ylist_head *i;
493 struct yaffs_SearchContext *sc;
494 struct ylist_head *search_contexts = &(yaffs_DeviceToContext(obj->myDev)->searchContexts);
497 /* Iterate through the directory search contexts.
498 * If any are currently on the object being removed, then advance
499 * the search context to the next object to prevent a hanging pointer.
501 ylist_for_each(i, search_contexts) {
503 sc = ylist_entry(i, struct yaffs_SearchContext,others);
504 if(sc->nextReturn == obj)
505 yaffs_SearchAdvance(sc);
512 /*-----------------------------------------------------------------*/
514 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
517 unsigned char *alias;
520 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
522 yaffs_GrossLock(dev);
524 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
526 yaffs_GrossUnlock(dev);
531 ret = vfs_readlink(dentry, buffer, buflen, alias);
536 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
537 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
539 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
542 unsigned char *alias;
544 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
546 yaffs_GrossLock(dev);
548 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
550 yaffs_GrossUnlock(dev);
557 ret = vfs_follow_link(nd, alias);
560 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
567 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
571 * Lookup is used to find objects in the fs
573 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
575 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
578 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
582 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
584 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
586 yaffs_GrossLock(dev);
589 ("yaffs_lookup for %d:%s\n",
590 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
592 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
593 dentry->d_name.name);
595 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
597 /* Can't hold gross lock when calling yaffs_get_inode() */
598 yaffs_GrossUnlock(dev);
602 ("yaffs_lookup found %d\n", obj->objectId));
604 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
608 ("yaffs_loookup dentry \n"));
609 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
610 * d_add even if NULL inode */
612 /*dget(dentry); // try to solve directory bug */
613 d_add(dentry, inode);
621 T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
625 /* added NCB for 2.5/6 compatability - forces add even if inode is
626 * NULL which creates dentry hash */
627 d_add(dentry, inode);
633 #ifdef YAFFS_HAS_PUT_INODE
635 /* For now put inode is just for debugging
636 * Put inode is called when the inode **structure** is put.
638 static void yaffs_put_inode(struct inode *inode)
641 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
642 atomic_read(&inode->i_count)));
647 /* clear is called to tell the fs to release any per-inode data it holds */
648 static void yaffs_clear_inode(struct inode *inode)
653 obj = yaffs_InodeToObject(inode);
656 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
657 atomic_read(&inode->i_count),
658 obj ? "object exists" : "null object"));
662 yaffs_GrossLock(dev);
664 /* Clear the association between the inode and
668 yaffs_InodeToObjectLV(inode) = NULL;
670 /* If the object freeing was deferred, then the real
672 * This should fix the inode inconsistency problem.
675 yaffs_HandleDeferedFree(obj);
677 yaffs_GrossUnlock(dev);
682 /* delete is called when the link count is zero and the inode
683 * is put (ie. nobody wants to know about it anymore, time to
685 * NB Must call clear_inode()
687 static void yaffs_delete_inode(struct inode *inode)
689 yaffs_Object *obj = yaffs_InodeToObject(inode);
693 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
694 atomic_read(&inode->i_count),
695 obj ? "object exists" : "null object"));
699 yaffs_GrossLock(dev);
700 yaffs_DeleteObject(obj);
701 yaffs_GrossUnlock(dev);
703 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
704 truncate_inode_pages(&inode->i_data, 0);
709 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
710 static int yaffs_file_flush(struct file *file, fl_owner_t id)
712 static int yaffs_file_flush(struct file *file)
715 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
717 yaffs_Device *dev = obj->myDev;
720 ("yaffs_file_flush object %d (%s)\n", obj->objectId,
721 obj->dirty ? "dirty" : "clean"));
723 yaffs_GrossLock(dev);
725 yaffs_FlushFile(obj, 1, 0);
727 yaffs_GrossUnlock(dev);
732 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
734 /* Lifted from jffs2 */
737 unsigned char *pg_buf;
742 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock at %08x, size %08x\n",
743 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
744 (unsigned)PAGE_CACHE_SIZE));
746 obj = yaffs_DentryToObject(f->f_dentry);
750 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
751 BUG_ON(!PageLocked(pg));
758 /* FIXME: Can kmap fail? */
760 yaffs_GrossLock(dev);
762 ret = yaffs_ReadDataFromFile(obj, pg_buf,
763 pg->index << PAGE_CACHE_SHIFT,
766 yaffs_GrossUnlock(dev);
772 ClearPageUptodate(pg);
779 flush_dcache_page(pg);
782 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock done\n"));
786 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
788 int ret = yaffs_readpage_nolock(f, pg);
793 static int yaffs_readpage(struct file *f, struct page *pg)
797 T(YAFFS_TRACE_OS, ("yaffs_readpage\n"));
798 ret=yaffs_readpage_unlock(f, pg);
799 T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
803 /* writepage inspired by/stolen from smbfs */
805 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
806 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
808 static int yaffs_writepage(struct page *page)
811 struct address_space *mapping = page->mapping;
813 unsigned long end_index;
822 inode = mapping->host;
825 i_size = i_size_read(inode);
827 end_index = i_size >> PAGE_CACHE_SHIFT;
829 if(page->index < end_index)
830 nBytes = PAGE_CACHE_SIZE;
832 nBytes = i_size & (PAGE_CACHE_SIZE -1);
834 if (page->index > end_index || !nBytes) {
836 ("yaffs_writepage at %08x, inode size = %08x!!!\n",
837 (unsigned)(page->index << PAGE_CACHE_SHIFT),
838 (unsigned)inode->i_size));
840 (" -> don't care!!\n"));
842 zero_user_segment(page,0,PAGE_CACHE_SIZE);
843 set_page_writeback(page);
845 end_page_writeback(page);
850 if(nBytes != PAGE_CACHE_SIZE)
851 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
857 obj = yaffs_InodeToObject(inode);
858 yaffs_GrossLock(obj->myDev);
861 ("yaffs_writepage at %08x, size %08x\n",
862 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
864 ("writepag0: obj = %05x, ino = %05x\n",
865 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
867 nWritten = yaffs_WriteDataToFile(obj, buffer,
868 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
871 ("writepag1: obj = %05x, ino = %05x\n",
872 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
874 yaffs_GrossUnlock(obj->myDev);
877 set_page_writeback(page);
879 end_page_writeback(page);
882 return (nWritten == nBytes) ? 0 : -ENOSPC;
886 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
887 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
888 loff_t pos, unsigned len, unsigned flags,
889 struct page **pagep, void **fsdata)
891 struct page *pg = NULL;
892 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
898 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
899 pg = grab_cache_page_write_begin(mapping, index, flags);
901 pg = __grab_cache_page(mapping, index);
909 T(YAFFS_TRACE_OS, ("start yaffs_write_begin index %d(%x) uptodate %d\n",(int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
912 space_held = yaffs_hold_space(filp);
919 /* Update page if required */
921 if (!Page_Uptodate(pg))
922 ret = yaffs_readpage_nolock(filp, pg);
927 /* Happy path return */
928 T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
933 T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
935 yaffs_release_space(filp);
938 page_cache_release(pg);
945 static int yaffs_prepare_write(struct file *f, struct page *pg,
946 unsigned offset, unsigned to)
948 T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
950 if (!Page_Uptodate(pg))
951 return yaffs_readpage_nolock(f, pg);
956 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
957 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
958 loff_t pos, unsigned len, unsigned copied,
959 struct page *pg, void *fsdadata)
963 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
966 addr = kva + offset_into_page;
969 ("yaffs_write_end addr %x pos %x nBytes %d\n",
973 ret = yaffs_file_write(filp, addr, copied, &pos);
977 ("yaffs_write_end not same size ret %d copied %d\n",
986 yaffs_release_space(filp);
988 page_cache_release(pg);
993 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
998 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
999 int nBytes = to - offset;
1002 unsigned spos = pos;
1006 addr = kva + offset;
1008 saddr = (unsigned) addr;
1011 ("yaffs_commit_write addr %x pos %x nBytes %d\n",
1012 saddr, spos, nBytes));
1014 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1016 if (nWritten != nBytes) {
1018 ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
1028 ("yaffs_commit_write returning %d\n",
1029 nWritten == nBytes ? 0 : nWritten));
1031 return nWritten == nBytes ? 0 : nWritten;
1036 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1041 /* Check mode against the variant type and attempt to repair if broken. */
1042 __u32 mode = obj->yst_mode;
1043 switch (obj->variantType) {
1044 case YAFFS_OBJECT_TYPE_FILE:
1045 if (!S_ISREG(mode)) {
1046 obj->yst_mode &= ~S_IFMT;
1047 obj->yst_mode |= S_IFREG;
1051 case YAFFS_OBJECT_TYPE_SYMLINK:
1052 if (!S_ISLNK(mode)) {
1053 obj->yst_mode &= ~S_IFMT;
1054 obj->yst_mode |= S_IFLNK;
1058 case YAFFS_OBJECT_TYPE_DIRECTORY:
1059 if (!S_ISDIR(mode)) {
1060 obj->yst_mode &= ~S_IFMT;
1061 obj->yst_mode |= S_IFDIR;
1065 case YAFFS_OBJECT_TYPE_UNKNOWN:
1066 case YAFFS_OBJECT_TYPE_HARDLINK:
1067 case YAFFS_OBJECT_TYPE_SPECIAL:
1073 inode->i_flags |= S_NOATIME;
1075 inode->i_ino = obj->objectId;
1076 inode->i_mode = obj->yst_mode;
1077 inode->i_uid = obj->yst_uid;
1078 inode->i_gid = obj->yst_gid;
1079 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1080 inode->i_blksize = inode->i_sb->s_blocksize;
1082 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1084 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1085 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1086 inode->i_atime.tv_nsec = 0;
1087 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1088 inode->i_mtime.tv_nsec = 0;
1089 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1090 inode->i_ctime.tv_nsec = 0;
1092 inode->i_rdev = obj->yst_rdev;
1093 inode->i_atime = obj->yst_atime;
1094 inode->i_mtime = obj->yst_mtime;
1095 inode->i_ctime = obj->yst_ctime;
1097 inode->i_size = yaffs_GetObjectFileLength(obj);
1098 inode->i_blocks = (inode->i_size + 511) >> 9;
1100 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1103 ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
1104 inode->i_mode, inode->i_uid, inode->i_gid,
1105 (int)inode->i_size, atomic_read(&inode->i_count)));
1107 switch (obj->yst_mode & S_IFMT) {
1108 default: /* fifo, device or socket */
1109 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1110 init_special_inode(inode, obj->yst_mode,
1111 old_decode_dev(obj->yst_rdev));
1113 init_special_inode(inode, obj->yst_mode,
1114 (dev_t) (obj->yst_rdev));
1117 case S_IFREG: /* file */
1118 inode->i_op = &yaffs_file_inode_operations;
1119 inode->i_fop = &yaffs_file_operations;
1120 inode->i_mapping->a_ops =
1121 &yaffs_file_address_operations;
1123 case S_IFDIR: /* directory */
1124 inode->i_op = &yaffs_dir_inode_operations;
1125 inode->i_fop = &yaffs_dir_operations;
1127 case S_IFLNK: /* symlink */
1128 inode->i_op = &yaffs_symlink_inode_operations;
1132 yaffs_InodeToObjectLV(inode) = obj;
1134 obj->myInode = inode;
1138 ("yaffs_FileInode invalid parameters\n"));
1143 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1146 struct inode *inode;
1150 ("yaffs_get_inode for NULL super_block!!\n"));
1157 ("yaffs_get_inode for NULL object!!\n"));
1163 ("yaffs_get_inode for object %d\n", obj->objectId));
1165 inode = Y_IGET(sb, obj->objectId);
1169 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1170 /* iget also increments the inode's i_count */
1171 /* NB You can't be holding grossLock or deadlock will happen! */
1176 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1181 struct inode *inode;
1184 obj = yaffs_DentryToObject(f->f_dentry);
1188 yaffs_GrossLock(dev);
1190 inode = f->f_dentry->d_inode;
1192 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1193 ipos = inode->i_size;
1199 ("yaffs_file_write: hey obj is null!\n"));
1202 ("yaffs_file_write about to write writing %u(%x) bytes"
1203 "to object %d at %d(%x)\n",
1204 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1206 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1209 ("yaffs_file_write: %d(%x) bytes written\n",
1210 (unsigned )n,(unsigned)n));
1215 if (ipos > inode->i_size) {
1216 inode->i_size = ipos;
1217 inode->i_blocks = (ipos + 511) >> 9;
1220 ("yaffs_file_write size updated to %d bytes, "
1222 ipos, (int)(inode->i_blocks)));
1226 yaffs_GrossUnlock(dev);
1227 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1230 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1231 /* For now we just assume few parallel writes and check against a small number. */
1232 /* Todo: need to do this with a counter to handle parallel reads better */
1234 static ssize_t yaffs_hold_space(struct file *f)
1242 obj = yaffs_DentryToObject(f->f_dentry);
1246 yaffs_GrossLock(dev);
1248 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1250 yaffs_GrossUnlock(dev);
1252 return (nFreeChunks > 20) ? 1 : 0;
1255 static void yaffs_release_space(struct file *f)
1261 obj = yaffs_DentryToObject(f->f_dentry);
1265 yaffs_GrossLock(dev);
1268 yaffs_GrossUnlock(dev);
1271 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1275 struct yaffs_SearchContext *sc;
1276 struct inode *inode = f->f_dentry->d_inode;
1277 unsigned long offset, curoffs;
1281 char name[YAFFS_MAX_NAME_LENGTH + 1];
1283 obj = yaffs_DentryToObject(f->f_dentry);
1286 yaffs_GrossLock(dev);
1290 sc = yaffs_NewSearch(obj);
1296 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
1300 ("yaffs_readdir: entry . ino %d \n",
1301 (int)inode->i_ino));
1302 yaffs_GrossUnlock(dev);
1303 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
1305 yaffs_GrossLock(dev);
1311 ("yaffs_readdir: entry .. ino %d \n",
1312 (int)f->f_dentry->d_parent->d_inode->i_ino));
1313 yaffs_GrossUnlock(dev);
1314 if (filldir(dirent, "..", 2, offset,
1315 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
1317 yaffs_GrossLock(dev);
1324 /* If the directory has changed since the open or last call to
1325 readdir, rewind to after the 2 canned entries. */
1327 if (f->f_version != inode->i_version) {
1330 f->f_version = inode->i_version;
1333 while(sc->nextReturn){
1336 if (curoffs >= offset) {
1337 int this_inode = yaffs_GetObjectInode(l);
1338 int this_type = yaffs_GetObjectType(l);
1340 yaffs_GetObjectName(l, name,
1341 YAFFS_MAX_NAME_LENGTH + 1);
1343 ("yaffs_readdir: %s inode %d\n", name,
1344 yaffs_GetObjectInode(l)));
1346 yaffs_GrossUnlock(dev);
1356 yaffs_GrossLock(dev);
1361 yaffs_SearchAdvance(sc);
1365 yaffs_GrossUnlock(dev);
1367 yaffs_EndSearch(sc);
1373 * File creation. Allocate an inode, and we're done..
1376 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1379 #define YCRED(x) (x->cred)
1382 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1383 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1386 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1390 struct inode *inode;
1392 yaffs_Object *obj = NULL;
1395 yaffs_Object *parent = yaffs_InodeToObject(dir);
1397 int error = -ENOSPC;
1398 uid_t uid = YCRED(current)->fsuid;
1399 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1401 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1406 ("yaffs_mknod: parent object %d type %d\n",
1407 parent->objectId, parent->variantType));
1410 ("yaffs_mknod: could not get parent object\n"));
1414 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1416 dentry->d_name.name, mode, rdev));
1418 dev = parent->myDev;
1420 yaffs_GrossLock(dev);
1422 switch (mode & S_IFMT) {
1424 /* Special (socket, fifo, device...) */
1425 T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
1426 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1427 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1428 gid, old_encode_dev(rdev));
1430 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1434 case S_IFREG: /* file */
1435 T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
1436 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1439 case S_IFDIR: /* directory */
1441 ("yaffs_mknod: making directory\n"));
1442 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1445 case S_IFLNK: /* symlink */
1446 T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
1447 obj = NULL; /* Do we ever get here? */
1451 /* Can not call yaffs_get_inode() with gross lock held */
1452 yaffs_GrossUnlock(dev);
1455 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1456 d_instantiate(dentry, inode);
1457 update_dir_time(dir);
1459 ("yaffs_mknod created object %d count = %d\n",
1460 obj->objectId, atomic_read(&inode->i_count)));
1464 ("yaffs_mknod failed making object\n"));
1471 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1474 T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
1475 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1479 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1480 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1481 struct nameidata *n)
1483 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1486 T(YAFFS_TRACE_OS, ("yaffs_create\n"));
1487 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1490 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1497 ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1498 dentry->d_name.name));
1500 dev = yaffs_InodeToObject(dir)->myDev;
1502 yaffs_GrossLock(dev);
1504 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1506 if (retVal == YAFFS_OK) {
1507 dentry->d_inode->i_nlink--;
1509 yaffs_GrossUnlock(dev);
1510 mark_inode_dirty(dentry->d_inode);
1511 update_dir_time(dir);
1514 yaffs_GrossUnlock(dev);
1521 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1522 struct dentry *dentry)
1524 struct inode *inode = old_dentry->d_inode;
1525 yaffs_Object *obj = NULL;
1526 yaffs_Object *link = NULL;
1529 T(YAFFS_TRACE_OS, ("yaffs_link\n"));
1531 obj = yaffs_InodeToObject(inode);
1534 yaffs_GrossLock(dev);
1536 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1537 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1541 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1542 d_instantiate(dentry, old_dentry->d_inode);
1543 atomic_inc(&old_dentry->d_inode->i_count);
1545 ("yaffs_link link count %d i_count %d\n",
1546 old_dentry->d_inode->i_nlink,
1547 atomic_read(&old_dentry->d_inode->i_count)));
1550 yaffs_GrossUnlock(dev);
1553 update_dir_time(dir);
1560 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1561 const char *symname)
1565 uid_t uid = YCRED(current)->fsuid;
1566 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1568 T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
1570 dev = yaffs_InodeToObject(dir)->myDev;
1571 yaffs_GrossLock(dev);
1572 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1573 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1574 yaffs_GrossUnlock(dev);
1577 struct inode *inode;
1579 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1580 d_instantiate(dentry, inode);
1581 update_dir_time(dir);
1582 T(YAFFS_TRACE_OS, ("symlink created OK\n"));
1585 T(YAFFS_TRACE_OS, ("symlink not created\n"));
1591 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1598 obj = yaffs_DentryToObject(dentry);
1602 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_object\n"));
1603 yaffs_GrossLock(dev);
1604 yaffs_FlushFile(obj, 1, datasync);
1605 yaffs_GrossUnlock(dev);
1610 * The VFS layer already does all the dentry stuff for rename.
1612 * NB: POSIX says you can rename an object over an old object of the same name
1614 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1615 struct inode *new_dir, struct dentry *new_dentry)
1618 int retVal = YAFFS_FAIL;
1619 yaffs_Object *target;
1621 T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
1622 dev = yaffs_InodeToObject(old_dir)->myDev;
1624 yaffs_GrossLock(dev);
1626 /* Check if the target is an existing directory that is not empty. */
1627 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1628 new_dentry->d_name.name);
1632 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1633 !ylist_empty(&target->variant.directoryVariant.children)) {
1635 T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
1637 retVal = YAFFS_FAIL;
1639 /* Now does unlinking internally using shadowing mechanism */
1640 T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
1642 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1643 old_dentry->d_name.name,
1644 yaffs_InodeToObject(new_dir),
1645 new_dentry->d_name.name);
1647 yaffs_GrossUnlock(dev);
1649 if (retVal == YAFFS_OK) {
1651 new_dentry->d_inode->i_nlink--;
1652 mark_inode_dirty(new_dentry->d_inode);
1655 update_dir_time(old_dir);
1656 if(old_dir != new_dir)
1657 update_dir_time(new_dir);
1664 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1666 struct inode *inode = dentry->d_inode;
1671 ("yaffs_setattr of object %d\n",
1672 yaffs_InodeToObject(inode)->objectId));
1674 /* Fail if a requested resize >= 2GB */
1675 if (attr->ia_valid & ATTR_SIZE &&
1676 (attr->ia_size >> 31))
1680 error = inode_change_ok(inode, attr);
1684 error = inode_setattr(inode, attr);
1685 T(YAFFS_TRACE_OS,("inode_setattr called\n"));
1686 if (attr->ia_valid & ATTR_SIZE)
1687 truncate_inode_pages(&inode->i_data,attr->ia_size);
1689 dev = yaffs_InodeToObject(inode)->myDev;
1690 if (attr->ia_valid & ATTR_SIZE){
1691 T(YAFFS_TRACE_OS,("resize to %d(%x)\n",(int)(attr->ia_size),(int)(attr->ia_size)));
1693 yaffs_GrossLock(dev);
1694 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1695 if(result == YAFFS_OK) {
1700 yaffs_GrossUnlock(dev);
1705 ("yaffs_setattr done returning %d\n",error));
1710 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1711 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1713 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1714 struct super_block *sb = dentry->d_sb;
1715 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1716 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1718 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1720 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1722 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1725 T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
1727 yaffs_GrossLock(dev);
1729 buf->f_type = YAFFS_MAGIC;
1730 buf->f_bsize = sb->s_blocksize;
1731 buf->f_namelen = 255;
1733 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1734 /* Do this if chunk size is not a power of 2 */
1736 uint64_t bytesInDev;
1739 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
1740 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
1742 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1743 buf->f_blocks = bytesInDev;
1745 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1746 ((uint64_t)(dev->nDataBytesPerChunk));
1748 do_div(bytesFree, sb->s_blocksize);
1750 buf->f_bfree = bytesFree;
1752 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1755 (dev->param.endBlock - dev->param.startBlock + 1) *
1756 dev->param.nChunksPerBlock /
1757 (sb->s_blocksize / dev->nDataBytesPerChunk);
1759 yaffs_GetNumberOfFreeChunks(dev) /
1760 (sb->s_blocksize / dev->nDataBytesPerChunk);
1763 (dev->param.endBlock - dev->param.startBlock + 1) *
1764 dev->param.nChunksPerBlock *
1765 (dev->nDataBytesPerChunk / sb->s_blocksize);
1768 yaffs_GetNumberOfFreeChunks(dev) *
1769 (dev->nDataBytesPerChunk / sb->s_blocksize);
1774 buf->f_bavail = buf->f_bfree;
1776 yaffs_GrossUnlock(dev);
1782 static void yaffs_flush_sb_inodes(struct super_block *sb)
1787 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1788 obj = yaffs_InodeToObject(iptr);
1790 T(YAFFS_TRACE_OS, ("flushing obj %d\n",obj->objectId));
1791 yaffs_FlushFile(obj,1,0);
1796 static int yaffs_do_sync_fs(struct super_block *sb)
1799 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1800 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_do_sync_fs\n"));
1803 yaffs_GrossLock(dev);
1806 yaffs_FlushEntireDeviceCache(dev);
1807 yaffs_flush_sb_inodes(sb);
1808 yaffs_CheckpointSave(dev);
1811 yaffs_GrossUnlock(dev);
1819 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1820 static void yaffs_write_super(struct super_block *sb)
1822 static int yaffs_write_super(struct super_block *sb)
1826 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_write_super\n"));
1827 if (yaffs_auto_checkpoint >= 2)
1828 yaffs_do_sync_fs(sb);
1829 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
1835 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1836 static int yaffs_sync_fs(struct super_block *sb, int wait)
1838 static int yaffs_sync_fs(struct super_block *sb)
1841 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_fs\n"));
1843 if (yaffs_auto_checkpoint >= 1)
1844 yaffs_do_sync_fs(sb);
1849 #ifdef YAFFS_USE_OWN_IGET
1851 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1853 struct inode *inode;
1855 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1858 ("yaffs_iget for %lu\n", ino));
1860 inode = iget_locked(sb, ino);
1862 return ERR_PTR(-ENOMEM);
1863 if (!(inode->i_state & I_NEW))
1866 /* NB This is called as a side effect of other functions, but
1867 * we had to release the lock to prevent deadlocks, so
1868 * need to lock again.
1871 yaffs_GrossLock(dev);
1873 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1875 yaffs_FillInodeFromObject(inode, obj);
1877 yaffs_GrossUnlock(dev);
1879 unlock_new_inode(inode);
1885 static void yaffs_read_inode(struct inode *inode)
1887 /* NB This is called as a side effect of other functions, but
1888 * we had to release the lock to prevent deadlocks, so
1889 * need to lock again.
1893 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1896 ("yaffs_read_inode for %d\n", (int)inode->i_ino));
1898 yaffs_GrossLock(dev);
1900 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1902 yaffs_FillInodeFromObject(inode, obj);
1904 yaffs_GrossUnlock(dev);
1909 static YLIST_HEAD(yaffs_context_list);
1911 #if 0 /* not used */
1912 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1914 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1916 if (*flags & MS_RDONLY) {
1917 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1920 ("yaffs_remount_fs: %s: RO\n", dev->name));
1922 yaffs_GrossLock(dev);
1924 yaffs_FlushEntireDeviceCache(dev);
1926 yaffs_CheckpointSave(dev);
1931 yaffs_GrossUnlock(dev);
1934 ("yaffs_remount_fs: %s: RW\n", dev->name));
1941 static void yaffs_put_super(struct super_block *sb)
1943 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1945 T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
1947 yaffs_GrossLock(dev);
1949 yaffs_FlushEntireDeviceCache(dev);
1951 yaffs_CheckpointSave(dev);
1953 if (yaffs_DeviceToContext(dev)->putSuperFunc)
1954 yaffs_DeviceToContext(dev)->putSuperFunc(sb);
1956 yaffs_Deinitialise(dev);
1958 yaffs_GrossUnlock(dev);
1960 /* we assume this is protected by lock_kernel() in mount/umount */
1961 ylist_del_init(&(yaffs_DeviceToContext(dev)->contextList));
1963 if (yaffs_DeviceToContext(dev)->spareBuffer) {
1964 YFREE(yaffs_DeviceToContext(dev)->spareBuffer);
1965 yaffs_DeviceToContext(dev)->spareBuffer = NULL;
1972 static void yaffs_MTDPutSuper(struct super_block *sb)
1974 struct mtd_info *mtd = yaffs_DeviceToContext(yaffs_SuperToDevice(sb))->mtd;
1979 put_mtd_device(mtd);
1983 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
1985 struct super_block *sb = yaffs_DeviceToContext(dev)->superBlock;
1987 T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
1994 int skip_checkpoint_read;
1995 int skip_checkpoint_write;
1998 int tags_ecc_overridden;
1999 int lazy_loading_enabled;
2000 int lazy_loading_overridden;
2001 int empty_lost_and_found;
2002 int empty_lost_and_found_overridden;
2005 #define MAX_OPT_LEN 30
2006 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2008 char cur_opt[MAX_OPT_LEN + 1];
2012 /* Parse through the options which is a comma seperated list */
2014 while (options_str && *options_str && !error) {
2015 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2018 while(*options_str == ',')
2021 while (*options_str && *options_str != ',') {
2022 if (p < MAX_OPT_LEN) {
2023 cur_opt[p] = *options_str;
2029 if (!strcmp(cur_opt, "inband-tags"))
2030 options->inband_tags = 1;
2031 else if (!strcmp(cur_opt, "tags-ecc-off")){
2032 options->tags_ecc_on = 0;
2033 options->tags_ecc_overridden=1;
2034 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2035 options->tags_ecc_on = 1;
2036 options->tags_ecc_overridden = 1;
2037 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2038 options->lazy_loading_enabled = 0;
2039 options->lazy_loading_overridden=1;
2040 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2041 options->lazy_loading_enabled = 1;
2042 options->lazy_loading_overridden = 1;
2043 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2044 options->empty_lost_and_found = 0;
2045 options->empty_lost_and_found_overridden=1;
2046 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2047 options->empty_lost_and_found = 1;
2048 options->empty_lost_and_found_overridden=1;
2049 } else if (!strcmp(cur_opt, "no-cache"))
2050 options->no_cache = 1;
2051 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2052 options->skip_checkpoint_read = 1;
2053 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2054 options->skip_checkpoint_write = 1;
2055 else if (!strcmp(cur_opt, "no-checkpoint")) {
2056 options->skip_checkpoint_read = 1;
2057 options->skip_checkpoint_write = 1;
2059 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2068 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2069 struct super_block *sb,
2070 void *data, int silent)
2073 struct inode *inode = NULL;
2074 struct dentry *root;
2075 yaffs_Device *dev = 0;
2076 char devname_buf[BDEVNAME_SIZE + 1];
2077 struct mtd_info *mtd;
2079 char *data_str = (char *)data;
2080 struct yaffs_LinuxContext *context = NULL;
2081 yaffs_DeviceParam *param;
2083 yaffs_options options;
2085 sb->s_magic = YAFFS_MAGIC;
2086 sb->s_op = &yaffs_super_ops;
2087 sb->s_flags |= MS_NOATIME;
2090 printk(KERN_INFO "yaffs: sb is NULL\n");
2091 else if (!sb->s_dev)
2092 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2093 else if (!yaffs_devname(sb, devname_buf))
2094 printk(KERN_INFO "yaffs: devname is NULL\n");
2096 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2098 yaffs_devname(sb, devname_buf));
2103 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2105 memset(&options, 0, sizeof(options));
2107 if (yaffs_parse_options(&options, data_str)) {
2108 /* Option parsing failed */
2113 sb->s_blocksize = PAGE_CACHE_SIZE;
2114 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2115 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2117 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2119 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2121 ("yaffs: Write verification disabled. All guarantees "
2122 "null and void\n"));
2125 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2127 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2128 yaffs_devname(sb, devname_buf)));
2130 /* Check it's an mtd device..... */
2131 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2132 return NULL; /* This isn't an mtd device */
2134 /* Get the device */
2135 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2137 T(YAFFS_TRACE_ALWAYS,
2138 ("yaffs: MTD device #%u doesn't appear to exist\n",
2142 /* Check it's NAND */
2143 if (mtd->type != MTD_NANDFLASH) {
2144 T(YAFFS_TRACE_ALWAYS,
2145 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
2149 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
2150 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
2151 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
2152 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
2153 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
2154 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
2155 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
2156 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2157 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
2158 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
2159 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2160 T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
2162 T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
2165 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2167 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2168 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
2172 /* Added NCB 26/5/2006 for completeness */
2173 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2174 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
2180 if (yaffsVersion == 2) {
2181 /* Check for version 2 style functions */
2183 !mtd->block_isbad ||
2184 !mtd->block_markbad ||
2187 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2188 !mtd->read_oob || !mtd->write_oob) {
2191 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2193 T(YAFFS_TRACE_ALWAYS,
2194 ("yaffs: MTD device does not support required "
2199 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2200 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2201 !options.inband_tags) {
2202 T(YAFFS_TRACE_ALWAYS,
2203 ("yaffs: MTD device does not have the "
2204 "right page sizes\n"));
2208 /* Check for V1 style functions */
2212 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2213 !mtd->read_oob || !mtd->write_oob) {
2216 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2218 T(YAFFS_TRACE_ALWAYS,
2219 ("yaffs: MTD device does not support required "
2224 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2225 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2226 T(YAFFS_TRACE_ALWAYS,
2227 ("yaffs: MTD device does not support have the "
2228 "right page sizes\n"));
2233 /* OK, so if we got here, we have an MTD that's NAND and looks
2234 * like it has the right capabilities
2235 * Set the yaffs_Device up for mtd
2238 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2239 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2241 if(!dev || !context ){
2251 /* Deep shit could not allocate device structure */
2252 T(YAFFS_TRACE_ALWAYS,
2253 ("yaffs_read_super: Failed trying to allocate "
2254 "yaffs_Device. \n"));
2257 memset(dev, 0, sizeof(yaffs_Device));
2258 param = &(dev->param);
2260 memset(context,0,sizeof(struct yaffs_LinuxContext));
2261 dev->context = context;
2262 YINIT_LIST_HEAD(&(context->contextList));
2264 context->superBlock = sb;
2268 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2269 sb->s_fs_info = dev;
2271 sb->u.generic_sbp = dev;
2274 yaffs_DeviceToContext(dev)->mtd = mtd;
2275 param->name = mtd->name;
2277 /* Set up the memory size parameters.... */
2279 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2281 param->startBlock = 0;
2282 param->endBlock = nBlocks - 1;
2283 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2284 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2285 param->nReservedBlocks = 5;
2286 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2287 param->inbandTags = options.inband_tags;
2289 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2290 param->disableLazyLoad = 1;
2292 if(options.lazy_loading_overridden)
2293 param->disableLazyLoad = !options.lazy_loading_enabled;
2295 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2296 param->noTagsECC = 1;
2298 if(options.tags_ecc_overridden)
2299 param->noTagsECC = !options.tags_ecc_on;
2301 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2302 param->emptyLostAndFound = 1;
2305 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2306 param->refreshPeriod = 0;
2308 param->refreshPeriod = 10000;
2311 if(options.empty_lost_and_found_overridden)
2312 param->emptyLostAndFound = options.empty_lost_and_found;
2314 /* ... and the functions. */
2315 if (yaffsVersion == 2) {
2316 param->writeChunkWithTagsToNAND =
2317 nandmtd2_WriteChunkWithTagsToNAND;
2318 param->readChunkWithTagsFromNAND =
2319 nandmtd2_ReadChunkWithTagsFromNAND;
2320 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2321 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2322 yaffs_DeviceToContext(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2323 param->isYaffs2 = 1;
2324 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2325 param->totalBytesPerChunk = mtd->writesize;
2326 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2328 param->totalBytesPerChunk = mtd->oobblock;
2329 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2331 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2333 param->startBlock = 0;
2334 param->endBlock = nBlocks - 1;
2336 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2337 /* use the MTD interface in yaffs_mtdif1.c */
2338 param->writeChunkWithTagsToNAND =
2339 nandmtd1_WriteChunkWithTagsToNAND;
2340 param->readChunkWithTagsFromNAND =
2341 nandmtd1_ReadChunkWithTagsFromNAND;
2342 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2343 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2345 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2346 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2348 param->isYaffs2 = 0;
2350 /* ... and common functions */
2351 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2352 param->initialiseNAND = nandmtd_InitialiseNAND;
2354 yaffs_DeviceToContext(dev)->putSuperFunc = yaffs_MTDPutSuper;
2356 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2358 yaffs_DeviceToContext(dev)->superBlock= sb;
2361 #ifndef CONFIG_YAFFS_DOES_ECC
2362 param->useNANDECC = 1;
2365 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2366 param->wideTnodesDisabled = 1;
2369 param->skipCheckpointRead = options.skip_checkpoint_read;
2370 param->skipCheckpointWrite = options.skip_checkpoint_write;
2372 /* we assume this is protected by lock_kernel() in mount/umount */
2373 ylist_add_tail(&(yaffs_DeviceToContext(dev)->contextList), &yaffs_context_list);
2375 /* Directory search handling...*/
2376 YINIT_LIST_HEAD(&(yaffs_DeviceToContext(dev)->searchContexts));
2377 param->removeObjectCallback = yaffs_RemoveObjectCallback;
2379 init_MUTEX(&(yaffs_DeviceToContext(dev)->grossLock));
2381 yaffs_GrossLock(dev);
2383 err = yaffs_GutsInitialise(dev);
2386 ("yaffs_read_super: guts initialised %s\n",
2387 (err == YAFFS_OK) ? "OK" : "FAILED"));
2389 /* Release lock before yaffs_get_inode() */
2390 yaffs_GrossUnlock(dev);
2392 /* Create root inode */
2393 if (err == YAFFS_OK)
2394 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2400 inode->i_op = &yaffs_dir_inode_operations;
2401 inode->i_fop = &yaffs_dir_operations;
2403 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
2405 root = d_alloc_root(inode);
2407 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
2414 sb->s_dirt = !dev->isCheckpointed;
2415 T(YAFFS_TRACE_ALWAYS,
2416 ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
2418 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
2423 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2424 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2427 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2430 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2431 static int yaffs_read_super(struct file_system_type *fs,
2432 int flags, const char *dev_name,
2433 void *data, struct vfsmount *mnt)
2436 return get_sb_bdev(fs, flags, dev_name, data,
2437 yaffs_internal_read_super_mtd, mnt);
2440 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2441 int flags, const char *dev_name,
2445 return get_sb_bdev(fs, flags, dev_name, data,
2446 yaffs_internal_read_super_mtd);
2450 static struct file_system_type yaffs_fs_type = {
2451 .owner = THIS_MODULE,
2453 .get_sb = yaffs_read_super,
2454 .kill_sb = kill_block_super,
2455 .fs_flags = FS_REQUIRES_DEV,
2458 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2461 return yaffs_internal_read_super(1, sb, data, silent);
2464 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2469 #ifdef CONFIG_YAFFS_YAFFS2
2471 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2472 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2475 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2478 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2479 static int yaffs2_read_super(struct file_system_type *fs,
2480 int flags, const char *dev_name, void *data,
2481 struct vfsmount *mnt)
2483 return get_sb_bdev(fs, flags, dev_name, data,
2484 yaffs2_internal_read_super_mtd, mnt);
2487 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2488 int flags, const char *dev_name,
2492 return get_sb_bdev(fs, flags, dev_name, data,
2493 yaffs2_internal_read_super_mtd);
2497 static struct file_system_type yaffs2_fs_type = {
2498 .owner = THIS_MODULE,
2500 .get_sb = yaffs2_read_super,
2501 .kill_sb = kill_block_super,
2502 .fs_flags = FS_REQUIRES_DEV,
2505 static struct super_block *yaffs2_read_super(struct super_block *sb,
2506 void *data, int silent)
2508 return yaffs_internal_read_super(2, sb, data, silent);
2511 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2515 #endif /* CONFIG_YAFFS_YAFFS2 */
2517 static struct proc_dir_entry *my_proc_entry;
2519 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
2521 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
2522 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
2523 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
2524 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
2525 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
2526 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
2527 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
2528 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
2529 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
2530 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
2532 buf += sprintf(buf, "\n");
2538 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
2540 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2541 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2542 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2543 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2544 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
2545 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2546 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2547 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2548 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2549 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2550 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2551 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2552 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2553 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2554 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2555 buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2556 buf += sprintf(buf, "passiveGCs......... %d\n", dev->passiveGarbageCollections);
2557 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2558 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
2559 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2560 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2561 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2562 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2563 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2564 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2565 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2566 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2567 buf += sprintf(buf, "refreshCount....... %d\n", dev->refreshCount);
2569 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2574 static int yaffs_proc_read(char *page,
2576 off_t offset, int count, int *eof, void *data)
2578 struct ylist_head *item;
2583 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2584 * We use 'offset' (*ppos) to indicate where we are in devList.
2585 * This also assumes the user has posted a read buffer large
2586 * enough to hold the complete output; but that's life in /proc.
2591 /* Print header first */
2593 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2594 "\n%s\n%s\n", yaffs_fs_c_version,
2595 yaffs_guts_c_version);
2597 buf += sprintf(buf,"\n");
2601 /* hold lock_kernel while traversing yaffs_dev_list */
2604 /* Locate and print the Nth entry. Order N-squared but N is small. */
2605 ylist_for_each(item, &yaffs_context_list) {
2606 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2607 yaffs_Device *dev = dc->dev;
2609 if (n < (step & ~1)) {
2614 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
2615 buf = yaffs_dump_dev_part0(buf, dev);
2617 buf = yaffs_dump_dev_part1(buf, dev);
2624 return buf - page < count ? buf - page : count;
2628 * Set the verbosity of the warnings and error messages.
2630 * Note that the names can only be a..z or _ with the current code.
2635 unsigned mask_bitfield;
2637 {"allocate", YAFFS_TRACE_ALLOCATE},
2638 {"always", YAFFS_TRACE_ALWAYS},
2639 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2640 {"buffers", YAFFS_TRACE_BUFFERS},
2641 {"bug", YAFFS_TRACE_BUG},
2642 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2643 {"deletion", YAFFS_TRACE_DELETION},
2644 {"erase", YAFFS_TRACE_ERASE},
2645 {"error", YAFFS_TRACE_ERROR},
2646 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2647 {"gc", YAFFS_TRACE_GC},
2648 {"mtd", YAFFS_TRACE_MTD},
2649 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2650 {"os", YAFFS_TRACE_OS},
2651 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2652 {"scan", YAFFS_TRACE_SCAN},
2653 {"tracing", YAFFS_TRACE_TRACING},
2654 {"sync", YAFFS_TRACE_SYNC},
2656 {"verify", YAFFS_TRACE_VERIFY},
2657 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2658 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2659 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2661 {"write", YAFFS_TRACE_WRITE},
2662 {"all", 0xffffffff},
2667 #define MAX_MASK_NAME_LENGTH 40
2668 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2669 unsigned long count, void *data)
2671 unsigned rg = 0, mask_bitfield;
2675 char substring[MAX_MASK_NAME_LENGTH + 1];
2681 rg = yaffs_traceMask;
2683 while (!done && (pos < count)) {
2685 while ((pos < count) && isspace(buf[pos]))
2702 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2704 if (end > buf + pos) {
2705 mask_name = "numeral";
2706 len = end - (buf + pos);
2710 for (x = buf + pos, i = 0;
2711 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2712 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2714 substring[i] = '\0';
2716 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2717 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
2718 mask_name = mask_flags[i].mask_name;
2719 mask_bitfield = mask_flags[i].mask_bitfield;
2726 if (mask_name != NULL) {
2730 rg &= ~mask_bitfield;
2733 rg |= mask_bitfield;
2739 rg |= mask_bitfield;
2745 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2747 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
2749 if (rg & YAFFS_TRACE_ALWAYS) {
2750 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2752 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2753 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
2761 static int yaffs_proc_write(struct file *file, const char *buf,
2762 unsigned long count, void *data)
2764 return yaffs_proc_write_trace_options(file, buf, count, data);
2767 /* Stuff to handle installation of file systems */
2768 struct file_system_to_install {
2769 struct file_system_type *fst;
2773 static struct file_system_to_install fs_to_install[] = {
2774 {&yaffs_fs_type, 0},
2775 {&yaffs2_fs_type, 0},
2779 static int __init init_yaffs_fs(void)
2782 struct file_system_to_install *fsinst;
2784 T(YAFFS_TRACE_ALWAYS,
2785 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2787 /* Install the proc_fs entry */
2788 my_proc_entry = create_proc_entry("yaffs",
2792 if (my_proc_entry) {
2793 my_proc_entry->write_proc = yaffs_proc_write;
2794 my_proc_entry->read_proc = yaffs_proc_read;
2795 my_proc_entry->data = NULL;
2799 /* Now add the file system entries */
2801 fsinst = fs_to_install;
2803 while (fsinst->fst && !error) {
2804 error = register_filesystem(fsinst->fst);
2806 fsinst->installed = 1;
2810 /* Any errors? uninstall */
2812 fsinst = fs_to_install;
2814 while (fsinst->fst) {
2815 if (fsinst->installed) {
2816 unregister_filesystem(fsinst->fst);
2817 fsinst->installed = 0;
2826 static void __exit exit_yaffs_fs(void)
2829 struct file_system_to_install *fsinst;
2831 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2834 remove_proc_entry("yaffs", YPROC_ROOT);
2836 fsinst = fs_to_install;
2838 while (fsinst->fst) {
2839 if (fsinst->installed) {
2840 unregister_filesystem(fsinst->fst);
2841 fsinst->installed = 0;
2847 module_init(init_yaffs_fs)
2848 module_exit(exit_yaffs_fs)
2850 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2851 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2852 MODULE_LICENSE("GPL");