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.91 2010-01-11 04:06: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 <linux/mtd/mtd.h>
121 #include "yaffs_mtdif.h"
122 #include "yaffs_mtdif1.h"
123 #include "yaffs_mtdif2.h"
125 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
126 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
127 unsigned int yaffs_auto_checkpoint = 1;
129 /* Module Parameters */
130 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
131 module_param(yaffs_traceMask, uint, 0644);
132 module_param(yaffs_wr_attempts, uint, 0644);
133 module_param(yaffs_auto_checkpoint, uint, 0644);
135 MODULE_PARM(yaffs_traceMask, "i");
136 MODULE_PARM(yaffs_wr_attempts, "i");
137 MODULE_PARM(yaffs_auto_checkpoint, "i");
140 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
141 /* use iget and read_inode */
142 #define Y_IGET(sb, inum) iget((sb), (inum))
143 static void yaffs_read_inode(struct inode *inode);
146 /* Call local equivalent */
147 #define YAFFS_USE_OWN_IGET
148 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
150 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
153 /*#define T(x) printk x */
155 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
156 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
158 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
161 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
162 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
164 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
165 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
167 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
171 #define update_dir_time(dir) do {\
172 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
175 static void yaffs_put_super(struct super_block *sb);
177 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
179 static ssize_t yaffs_hold_space(struct file *f);
180 static void yaffs_release_space(struct file *f);
182 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
183 static int yaffs_file_flush(struct file *file, fl_owner_t id);
185 static int yaffs_file_flush(struct file *file);
188 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
191 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
193 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
194 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
195 struct nameidata *n);
196 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
197 struct nameidata *n);
199 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
200 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
202 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
203 struct dentry *dentry);
204 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
205 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
206 const char *symname);
207 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
209 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
210 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
213 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
216 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
217 struct inode *new_dir, struct dentry *new_dentry);
218 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
220 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
221 static int yaffs_sync_fs(struct super_block *sb, int wait);
222 static void yaffs_write_super(struct super_block *sb);
224 static int yaffs_sync_fs(struct super_block *sb);
225 static int yaffs_write_super(struct super_block *sb);
228 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
229 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
230 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
231 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
233 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
236 #ifdef YAFFS_HAS_PUT_INODE
237 static void yaffs_put_inode(struct inode *inode);
240 static void yaffs_delete_inode(struct inode *);
241 static void yaffs_clear_inode(struct inode *);
243 static int yaffs_readpage(struct file *file, struct page *page);
244 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
245 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
247 static int yaffs_writepage(struct page *page);
251 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
252 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
253 loff_t pos, unsigned len, unsigned flags,
254 struct page **pagep, void **fsdata);
255 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
256 loff_t pos, unsigned len, unsigned copied,
257 struct page *pg, void *fsdadata);
259 static int yaffs_prepare_write(struct file *f, struct page *pg,
260 unsigned offset, unsigned to);
261 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
266 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
268 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
269 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
271 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
274 static struct address_space_operations yaffs_file_address_operations = {
275 .readpage = yaffs_readpage,
276 .writepage = yaffs_writepage,
277 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
278 .write_begin = yaffs_write_begin,
279 .write_end = yaffs_write_end,
281 .prepare_write = yaffs_prepare_write,
282 .commit_write = yaffs_commit_write,
286 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
287 static const struct file_operations yaffs_file_operations = {
288 .read = do_sync_read,
289 .write = do_sync_write,
290 .aio_read = generic_file_aio_read,
291 .aio_write = generic_file_aio_write,
292 .mmap = generic_file_mmap,
293 .flush = yaffs_file_flush,
294 .fsync = yaffs_sync_object,
295 .splice_read = generic_file_splice_read,
296 .splice_write = generic_file_splice_write,
297 .llseek = generic_file_llseek,
300 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
302 static const struct file_operations yaffs_file_operations = {
303 .read = do_sync_read,
304 .write = do_sync_write,
305 .aio_read = generic_file_aio_read,
306 .aio_write = generic_file_aio_write,
307 .mmap = generic_file_mmap,
308 .flush = yaffs_file_flush,
309 .fsync = yaffs_sync_object,
310 .sendfile = generic_file_sendfile,
315 static const struct file_operations yaffs_file_operations = {
316 .read = generic_file_read,
317 .write = generic_file_write,
318 .mmap = generic_file_mmap,
319 .flush = yaffs_file_flush,
320 .fsync = yaffs_sync_object,
321 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
322 .sendfile = generic_file_sendfile,
327 static const struct inode_operations yaffs_file_inode_operations = {
328 .setattr = yaffs_setattr,
331 static const struct inode_operations yaffs_symlink_inode_operations = {
332 .readlink = yaffs_readlink,
333 .follow_link = yaffs_follow_link,
334 .setattr = yaffs_setattr,
337 static const struct inode_operations yaffs_dir_inode_operations = {
338 .create = yaffs_create,
339 .lookup = yaffs_lookup,
341 .unlink = yaffs_unlink,
342 .symlink = yaffs_symlink,
343 .mkdir = yaffs_mkdir,
344 .rmdir = yaffs_unlink,
345 .mknod = yaffs_mknod,
346 .rename = yaffs_rename,
347 .setattr = yaffs_setattr,
350 static const struct file_operations yaffs_dir_operations = {
351 .read = generic_read_dir,
352 .readdir = yaffs_readdir,
353 .fsync = yaffs_sync_object,
356 static const struct super_operations yaffs_super_ops = {
357 .statfs = yaffs_statfs,
359 #ifndef YAFFS_USE_OWN_IGET
360 .read_inode = yaffs_read_inode,
362 #ifdef YAFFS_HAS_PUT_INODE
363 .put_inode = yaffs_put_inode,
365 .put_super = yaffs_put_super,
366 .delete_inode = yaffs_delete_inode,
367 .clear_inode = yaffs_clear_inode,
368 .sync_fs = yaffs_sync_fs,
369 .write_super = yaffs_write_super,
372 static void yaffs_GrossLock(yaffs_Device *dev)
374 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
375 down(&dev->grossLock);
376 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
379 static void yaffs_GrossUnlock(yaffs_Device *dev)
381 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
386 /*-----------------------------------------------------------------*/
387 /* Directory search context allows us to unlock access to yaffs during
388 * filldir without causing problems with the directory being modified.
389 * This is similar to the tried and tested mechanism used in yaffs direct.
391 * A search context iterates along a doubly linked list of siblings in the
392 * directory. If the iterating object is deleted then this would corrupt
393 * the list iteration, likely causing a crash. The search context avoids
394 * this by using the removeObjectCallback to move the search context to the
395 * next object before the object is deleted.
397 * Many readdirs (and thus seach conexts) may be alive simulateously so
398 * each yaffs_Device has a list of these.
400 * A seach context lives for the duration of a readdir.
402 * All these functions must be called while yaffs is locked.
405 struct yaffs_SearchContext {
407 yaffs_Object *dirObj;
408 yaffs_Object *nextReturn;
409 struct ylist_head others;
413 * yaffs_NewSearch() creates a new search context, initialises it and
414 * adds it to the device's search context list.
416 * Called at start of readdir.
418 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
420 yaffs_Device *dev = dir->myDev;
421 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
425 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
426 sc->nextReturn = NULL;
428 sc->nextReturn = ylist_entry(
429 dir->variant.directoryVariant.children.next,
430 yaffs_Object,siblings);
431 YINIT_LIST_HEAD(&sc->others);
432 ylist_add(&sc->others,&dev->searchContexts);
438 * yaffs_EndSearch() disposes of a search context and cleans up.
440 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
443 ylist_del(&sc->others);
449 * yaffs_SearchAdvance() moves a search context to the next object.
450 * Called when the search iterates or when an object removal causes
451 * the search context to be moved to the next object.
453 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
458 if( sc->nextReturn == NULL ||
459 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
460 sc->nextReturn = NULL;
462 struct ylist_head *next = sc->nextReturn->siblings.next;
464 if( next == &sc->dirObj->variant.directoryVariant.children)
465 sc->nextReturn = NULL; /* end of list */
467 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
472 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
473 * We check open search contexts and advance any which are currently
474 * on the object being iterated.
476 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
479 struct ylist_head *i;
480 struct yaffs_SearchContext *sc;
481 struct ylist_head *search_contexts = &obj->myDev->searchContexts;
484 /* Iterate through the directory search contexts.
485 * If any are currently on the object being removed, then advance
486 * the search context to the next object to prevent a hanging pointer.
488 ylist_for_each(i, search_contexts) {
490 sc = ylist_entry(i, struct yaffs_SearchContext,others);
491 if(sc->nextReturn == obj)
492 yaffs_SearchAdvance(sc);
499 /*-----------------------------------------------------------------*/
501 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
504 unsigned char *alias;
507 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
509 yaffs_GrossLock(dev);
511 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
513 yaffs_GrossUnlock(dev);
518 ret = vfs_readlink(dentry, buffer, buflen, alias);
523 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
524 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
526 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
529 unsigned char *alias;
531 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
533 yaffs_GrossLock(dev);
535 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
537 yaffs_GrossUnlock(dev);
544 ret = vfs_follow_link(nd, alias);
547 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
554 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
558 * Lookup is used to find objects in the fs
560 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
562 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
565 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
569 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
571 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
573 yaffs_GrossLock(dev);
576 ("yaffs_lookup for %d:%s\n",
577 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
579 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
580 dentry->d_name.name);
582 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
584 /* Can't hold gross lock when calling yaffs_get_inode() */
585 yaffs_GrossUnlock(dev);
589 ("yaffs_lookup found %d\n", obj->objectId));
591 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
595 ("yaffs_loookup dentry \n"));
596 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
597 * d_add even if NULL inode */
599 /*dget(dentry); // try to solve directory bug */
600 d_add(dentry, inode);
608 T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
612 /* added NCB for 2.5/6 compatability - forces add even if inode is
613 * NULL which creates dentry hash */
614 d_add(dentry, inode);
620 #ifdef YAFFS_HAS_PUT_INODE
622 /* For now put inode is just for debugging
623 * Put inode is called when the inode **structure** is put.
625 static void yaffs_put_inode(struct inode *inode)
628 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
629 atomic_read(&inode->i_count)));
634 /* clear is called to tell the fs to release any per-inode data it holds */
635 static void yaffs_clear_inode(struct inode *inode)
640 obj = yaffs_InodeToObject(inode);
643 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
644 atomic_read(&inode->i_count),
645 obj ? "object exists" : "null object"));
649 yaffs_GrossLock(dev);
651 /* Clear the association between the inode and
655 yaffs_InodeToObjectLV(inode) = NULL;
657 /* If the object freeing was deferred, then the real
659 * This should fix the inode inconsistency problem.
662 yaffs_HandleDeferedFree(obj);
664 yaffs_GrossUnlock(dev);
669 /* delete is called when the link count is zero and the inode
670 * is put (ie. nobody wants to know about it anymore, time to
672 * NB Must call clear_inode()
674 static void yaffs_delete_inode(struct inode *inode)
676 yaffs_Object *obj = yaffs_InodeToObject(inode);
680 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
681 atomic_read(&inode->i_count),
682 obj ? "object exists" : "null object"));
686 yaffs_GrossLock(dev);
687 yaffs_DeleteObject(obj);
688 yaffs_GrossUnlock(dev);
690 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
691 truncate_inode_pages(&inode->i_data, 0);
696 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
697 static int yaffs_file_flush(struct file *file, fl_owner_t id)
699 static int yaffs_file_flush(struct file *file)
702 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
704 yaffs_Device *dev = obj->myDev;
707 ("yaffs_file_flush object %d (%s)\n", obj->objectId,
708 obj->dirty ? "dirty" : "clean"));
710 yaffs_GrossLock(dev);
712 yaffs_FlushFile(obj, 1, 0);
714 yaffs_GrossUnlock(dev);
719 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
721 /* Lifted from jffs2 */
724 unsigned char *pg_buf;
729 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock at %08x, size %08x\n",
730 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
731 (unsigned)PAGE_CACHE_SIZE));
733 obj = yaffs_DentryToObject(f->f_dentry);
737 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
738 BUG_ON(!PageLocked(pg));
745 /* FIXME: Can kmap fail? */
747 yaffs_GrossLock(dev);
749 ret = yaffs_ReadDataFromFile(obj, pg_buf,
750 pg->index << PAGE_CACHE_SHIFT,
753 yaffs_GrossUnlock(dev);
759 ClearPageUptodate(pg);
766 flush_dcache_page(pg);
769 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock done\n"));
773 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
775 int ret = yaffs_readpage_nolock(f, pg);
780 static int yaffs_readpage(struct file *f, struct page *pg)
784 T(YAFFS_TRACE_OS, ("yaffs_readpage\n"));
785 ret=yaffs_readpage_unlock(f, pg);
786 T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
790 /* writepage inspired by/stolen from smbfs */
792 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
793 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
795 static int yaffs_writepage(struct page *page)
798 struct address_space *mapping = page->mapping;
800 unsigned long end_index;
809 inode = mapping->host;
812 i_size = i_size_read(inode);
814 end_index = i_size >> PAGE_CACHE_SHIFT;
816 if(page->index < end_index)
817 nBytes = PAGE_CACHE_SIZE;
819 nBytes = i_size & (PAGE_CACHE_SIZE -1);
821 if (page->index > end_index || !nBytes) {
823 ("yaffs_writepage at %08x, inode size = %08x!!!\n",
824 (unsigned)(page->index << PAGE_CACHE_SHIFT),
825 (unsigned)inode->i_size));
827 (" -> don't care!!\n"));
829 zero_user_segment(page,0,PAGE_CACHE_SIZE);
830 set_page_writeback(page);
832 end_page_writeback(page);
837 if(nBytes != PAGE_CACHE_SIZE)
838 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
844 obj = yaffs_InodeToObject(inode);
845 yaffs_GrossLock(obj->myDev);
848 ("yaffs_writepage at %08x, size %08x\n",
849 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
851 ("writepag0: obj = %05x, ino = %05x\n",
852 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
854 nWritten = yaffs_WriteDataToFile(obj, buffer,
855 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
858 ("writepag1: obj = %05x, ino = %05x\n",
859 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
861 yaffs_GrossUnlock(obj->myDev);
864 set_page_writeback(page);
866 end_page_writeback(page);
869 return (nWritten == nBytes) ? 0 : -ENOSPC;
873 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
874 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
875 loff_t pos, unsigned len, unsigned flags,
876 struct page **pagep, void **fsdata)
878 struct page *pg = NULL;
879 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
885 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
886 pg = grab_cache_page_write_begin(mapping, index, flags);
888 pg = __grab_cache_page(mapping, index);
896 T(YAFFS_TRACE_OS, ("start yaffs_write_begin index %d(%x) uptodate %d\n",(int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
899 space_held = yaffs_hold_space(filp);
906 /* Update page if required */
908 if (!Page_Uptodate(pg))
909 ret = yaffs_readpage_nolock(filp, pg);
914 /* Happy path return */
915 T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
920 T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
922 yaffs_release_space(filp);
925 page_cache_release(pg);
932 static int yaffs_prepare_write(struct file *f, struct page *pg,
933 unsigned offset, unsigned to)
935 T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
937 if (!Page_Uptodate(pg))
938 return yaffs_readpage_nolock(f, pg);
943 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
944 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
945 loff_t pos, unsigned len, unsigned copied,
946 struct page *pg, void *fsdadata)
950 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
953 addr = kva + offset_into_page;
956 ("yaffs_write_end addr %x pos %x nBytes %d\n",
960 ret = yaffs_file_write(filp, addr, copied, &pos);
964 ("yaffs_write_end not same size ret %d copied %d\n",
973 yaffs_release_space(filp);
975 page_cache_release(pg);
980 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
985 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
986 int nBytes = to - offset;
995 saddr = (unsigned) addr;
998 ("yaffs_commit_write addr %x pos %x nBytes %d\n",
999 saddr, spos, nBytes));
1001 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1003 if (nWritten != nBytes) {
1005 ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
1015 ("yaffs_commit_write returning %d\n",
1016 nWritten == nBytes ? 0 : nWritten));
1018 return nWritten == nBytes ? 0 : nWritten;
1023 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1028 /* Check mode against the variant type and attempt to repair if broken. */
1029 __u32 mode = obj->yst_mode;
1030 switch (obj->variantType) {
1031 case YAFFS_OBJECT_TYPE_FILE:
1032 if (!S_ISREG(mode)) {
1033 obj->yst_mode &= ~S_IFMT;
1034 obj->yst_mode |= S_IFREG;
1038 case YAFFS_OBJECT_TYPE_SYMLINK:
1039 if (!S_ISLNK(mode)) {
1040 obj->yst_mode &= ~S_IFMT;
1041 obj->yst_mode |= S_IFLNK;
1045 case YAFFS_OBJECT_TYPE_DIRECTORY:
1046 if (!S_ISDIR(mode)) {
1047 obj->yst_mode &= ~S_IFMT;
1048 obj->yst_mode |= S_IFDIR;
1052 case YAFFS_OBJECT_TYPE_UNKNOWN:
1053 case YAFFS_OBJECT_TYPE_HARDLINK:
1054 case YAFFS_OBJECT_TYPE_SPECIAL:
1060 inode->i_flags |= S_NOATIME;
1062 inode->i_ino = obj->objectId;
1063 inode->i_mode = obj->yst_mode;
1064 inode->i_uid = obj->yst_uid;
1065 inode->i_gid = obj->yst_gid;
1066 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1067 inode->i_blksize = inode->i_sb->s_blocksize;
1069 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1071 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1072 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1073 inode->i_atime.tv_nsec = 0;
1074 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1075 inode->i_mtime.tv_nsec = 0;
1076 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1077 inode->i_ctime.tv_nsec = 0;
1079 inode->i_rdev = obj->yst_rdev;
1080 inode->i_atime = obj->yst_atime;
1081 inode->i_mtime = obj->yst_mtime;
1082 inode->i_ctime = obj->yst_ctime;
1084 inode->i_size = yaffs_GetObjectFileLength(obj);
1085 inode->i_blocks = (inode->i_size + 511) >> 9;
1087 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1090 ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
1091 inode->i_mode, inode->i_uid, inode->i_gid,
1092 (int)inode->i_size, atomic_read(&inode->i_count)));
1094 switch (obj->yst_mode & S_IFMT) {
1095 default: /* fifo, device or socket */
1096 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1097 init_special_inode(inode, obj->yst_mode,
1098 old_decode_dev(obj->yst_rdev));
1100 init_special_inode(inode, obj->yst_mode,
1101 (dev_t) (obj->yst_rdev));
1104 case S_IFREG: /* file */
1105 inode->i_op = &yaffs_file_inode_operations;
1106 inode->i_fop = &yaffs_file_operations;
1107 inode->i_mapping->a_ops =
1108 &yaffs_file_address_operations;
1110 case S_IFDIR: /* directory */
1111 inode->i_op = &yaffs_dir_inode_operations;
1112 inode->i_fop = &yaffs_dir_operations;
1114 case S_IFLNK: /* symlink */
1115 inode->i_op = &yaffs_symlink_inode_operations;
1119 yaffs_InodeToObjectLV(inode) = obj;
1121 obj->myInode = inode;
1125 ("yaffs_FileInode invalid parameters\n"));
1130 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1133 struct inode *inode;
1137 ("yaffs_get_inode for NULL super_block!!\n"));
1144 ("yaffs_get_inode for NULL object!!\n"));
1150 ("yaffs_get_inode for object %d\n", obj->objectId));
1152 inode = Y_IGET(sb, obj->objectId);
1156 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1157 /* iget also increments the inode's i_count */
1158 /* NB You can't be holding grossLock or deadlock will happen! */
1163 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1168 struct inode *inode;
1171 obj = yaffs_DentryToObject(f->f_dentry);
1175 yaffs_GrossLock(dev);
1177 inode = f->f_dentry->d_inode;
1179 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1180 ipos = inode->i_size;
1186 ("yaffs_file_write: hey obj is null!\n"));
1189 ("yaffs_file_write about to write writing %u(%x) bytes"
1190 "to object %d at %d(%x)\n",
1191 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1193 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1196 ("yaffs_file_write: %d(%x) bytes written\n",
1197 (unsigned )n,(unsigned)n));
1202 if (ipos > inode->i_size) {
1203 inode->i_size = ipos;
1204 inode->i_blocks = (ipos + 511) >> 9;
1207 ("yaffs_file_write size updated to %d bytes, "
1209 ipos, (int)(inode->i_blocks)));
1213 yaffs_GrossUnlock(dev);
1214 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1217 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1218 /* For now we just assume few parallel writes and check against a small number. */
1219 /* Todo: need to do this with a counter to handle parallel reads better */
1221 static ssize_t yaffs_hold_space(struct file *f)
1229 obj = yaffs_DentryToObject(f->f_dentry);
1233 yaffs_GrossLock(dev);
1235 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1237 yaffs_GrossUnlock(dev);
1239 return (nFreeChunks > 20) ? 1 : 0;
1242 static void yaffs_release_space(struct file *f)
1248 obj = yaffs_DentryToObject(f->f_dentry);
1252 yaffs_GrossLock(dev);
1255 yaffs_GrossUnlock(dev);
1258 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1262 struct yaffs_SearchContext *sc;
1263 struct inode *inode = f->f_dentry->d_inode;
1264 unsigned long offset, curoffs;
1268 char name[YAFFS_MAX_NAME_LENGTH + 1];
1270 obj = yaffs_DentryToObject(f->f_dentry);
1273 yaffs_GrossLock(dev);
1277 sc = yaffs_NewSearch(obj);
1283 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
1287 ("yaffs_readdir: entry . ino %d \n",
1288 (int)inode->i_ino));
1289 yaffs_GrossUnlock(dev);
1290 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
1292 yaffs_GrossLock(dev);
1298 ("yaffs_readdir: entry .. ino %d \n",
1299 (int)f->f_dentry->d_parent->d_inode->i_ino));
1300 yaffs_GrossUnlock(dev);
1301 if (filldir(dirent, "..", 2, offset,
1302 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
1304 yaffs_GrossLock(dev);
1311 /* If the directory has changed since the open or last call to
1312 readdir, rewind to after the 2 canned entries. */
1314 if (f->f_version != inode->i_version) {
1317 f->f_version = inode->i_version;
1320 while(sc->nextReturn){
1323 if (curoffs >= offset) {
1324 int this_inode = yaffs_GetObjectInode(l);
1325 int this_type = yaffs_GetObjectType(l);
1327 yaffs_GetObjectName(l, name,
1328 YAFFS_MAX_NAME_LENGTH + 1);
1330 ("yaffs_readdir: %s inode %d\n", name,
1331 yaffs_GetObjectInode(l)));
1333 yaffs_GrossUnlock(dev);
1343 yaffs_GrossLock(dev);
1348 yaffs_SearchAdvance(sc);
1352 yaffs_GrossUnlock(dev);
1354 yaffs_EndSearch(sc);
1360 * File creation. Allocate an inode, and we're done..
1363 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1366 #define YCRED(x) (x->cred)
1369 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1370 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1373 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1377 struct inode *inode;
1379 yaffs_Object *obj = NULL;
1382 yaffs_Object *parent = yaffs_InodeToObject(dir);
1384 int error = -ENOSPC;
1385 uid_t uid = YCRED(current)->fsuid;
1386 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1388 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1393 ("yaffs_mknod: parent object %d type %d\n",
1394 parent->objectId, parent->variantType));
1397 ("yaffs_mknod: could not get parent object\n"));
1401 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1403 dentry->d_name.name, mode, rdev));
1405 dev = parent->myDev;
1407 yaffs_GrossLock(dev);
1409 switch (mode & S_IFMT) {
1411 /* Special (socket, fifo, device...) */
1412 T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
1413 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1414 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1415 gid, old_encode_dev(rdev));
1417 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1421 case S_IFREG: /* file */
1422 T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
1423 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1426 case S_IFDIR: /* directory */
1428 ("yaffs_mknod: making directory\n"));
1429 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1432 case S_IFLNK: /* symlink */
1433 T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
1434 obj = NULL; /* Do we ever get here? */
1438 /* Can not call yaffs_get_inode() with gross lock held */
1439 yaffs_GrossUnlock(dev);
1442 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1443 d_instantiate(dentry, inode);
1444 update_dir_time(dir);
1446 ("yaffs_mknod created object %d count = %d\n",
1447 obj->objectId, atomic_read(&inode->i_count)));
1451 ("yaffs_mknod failed making object\n"));
1458 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1461 T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
1462 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1466 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1467 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1468 struct nameidata *n)
1470 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1473 T(YAFFS_TRACE_OS, ("yaffs_create\n"));
1474 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1477 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1484 ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1485 dentry->d_name.name));
1487 dev = yaffs_InodeToObject(dir)->myDev;
1489 yaffs_GrossLock(dev);
1491 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1493 if (retVal == YAFFS_OK) {
1494 dentry->d_inode->i_nlink--;
1496 yaffs_GrossUnlock(dev);
1497 mark_inode_dirty(dentry->d_inode);
1498 update_dir_time(dir);
1501 yaffs_GrossUnlock(dev);
1508 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1509 struct dentry *dentry)
1511 struct inode *inode = old_dentry->d_inode;
1512 yaffs_Object *obj = NULL;
1513 yaffs_Object *link = NULL;
1516 T(YAFFS_TRACE_OS, ("yaffs_link\n"));
1518 obj = yaffs_InodeToObject(inode);
1521 yaffs_GrossLock(dev);
1523 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1524 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1528 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1529 d_instantiate(dentry, old_dentry->d_inode);
1530 atomic_inc(&old_dentry->d_inode->i_count);
1532 ("yaffs_link link count %d i_count %d\n",
1533 old_dentry->d_inode->i_nlink,
1534 atomic_read(&old_dentry->d_inode->i_count)));
1537 yaffs_GrossUnlock(dev);
1540 update_dir_time(dir);
1547 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1548 const char *symname)
1552 uid_t uid = YCRED(current)->fsuid;
1553 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1555 T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
1557 dev = yaffs_InodeToObject(dir)->myDev;
1558 yaffs_GrossLock(dev);
1559 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1560 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1561 yaffs_GrossUnlock(dev);
1564 struct inode *inode;
1566 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1567 d_instantiate(dentry, inode);
1568 update_dir_time(dir);
1569 T(YAFFS_TRACE_OS, ("symlink created OK\n"));
1572 T(YAFFS_TRACE_OS, ("symlink not created\n"));
1578 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1585 obj = yaffs_DentryToObject(dentry);
1589 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_object\n"));
1590 yaffs_GrossLock(dev);
1591 yaffs_FlushFile(obj, 1, datasync);
1592 yaffs_GrossUnlock(dev);
1597 * The VFS layer already does all the dentry stuff for rename.
1599 * NB: POSIX says you can rename an object over an old object of the same name
1601 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1602 struct inode *new_dir, struct dentry *new_dentry)
1605 int retVal = YAFFS_FAIL;
1606 yaffs_Object *target;
1608 T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
1609 dev = yaffs_InodeToObject(old_dir)->myDev;
1611 yaffs_GrossLock(dev);
1613 /* Check if the target is an existing directory that is not empty. */
1614 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1615 new_dentry->d_name.name);
1619 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1620 !ylist_empty(&target->variant.directoryVariant.children)) {
1622 T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
1624 retVal = YAFFS_FAIL;
1626 /* Now does unlinking internally using shadowing mechanism */
1627 T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
1629 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1630 old_dentry->d_name.name,
1631 yaffs_InodeToObject(new_dir),
1632 new_dentry->d_name.name);
1634 yaffs_GrossUnlock(dev);
1636 if (retVal == YAFFS_OK) {
1638 new_dentry->d_inode->i_nlink--;
1639 mark_inode_dirty(new_dentry->d_inode);
1642 update_dir_time(old_dir);
1643 if(old_dir != new_dir)
1644 update_dir_time(new_dir);
1651 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1653 struct inode *inode = dentry->d_inode;
1658 ("yaffs_setattr of object %d\n",
1659 yaffs_InodeToObject(inode)->objectId));
1661 error = inode_change_ok(inode, attr);
1665 error = inode_setattr(inode, attr);
1666 T(YAFFS_TRACE_OS,("inode_setattr called\n"));
1667 if (attr->ia_valid & ATTR_SIZE)
1668 truncate_inode_pages(&inode->i_data,attr->ia_size);
1670 dev = yaffs_InodeToObject(inode)->myDev;
1671 if (attr->ia_valid & ATTR_SIZE){
1672 T(YAFFS_TRACE_OS,("resize to %d(%x)\n",(int)(attr->ia_size),(int)(attr->ia_size)));
1674 yaffs_GrossLock(dev);
1675 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1676 if(result == YAFFS_OK) {
1681 yaffs_GrossUnlock(dev);
1685 ("yaffs_setattr done\n"));
1690 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1691 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1693 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1694 struct super_block *sb = dentry->d_sb;
1695 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1696 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1698 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1700 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1702 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1705 T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
1707 yaffs_GrossLock(dev);
1709 buf->f_type = YAFFS_MAGIC;
1710 buf->f_bsize = sb->s_blocksize;
1711 buf->f_namelen = 255;
1713 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1714 /* Do this if chunk size is not a power of 2 */
1716 uint64_t bytesInDev;
1719 bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock + 1))) *
1720 ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
1722 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1723 buf->f_blocks = bytesInDev;
1725 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1726 ((uint64_t)(dev->nDataBytesPerChunk));
1728 do_div(bytesFree, sb->s_blocksize);
1730 buf->f_bfree = bytesFree;
1732 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1735 (dev->endBlock - dev->startBlock + 1) *
1736 dev->nChunksPerBlock /
1737 (sb->s_blocksize / dev->nDataBytesPerChunk);
1739 yaffs_GetNumberOfFreeChunks(dev) /
1740 (sb->s_blocksize / dev->nDataBytesPerChunk);
1743 (dev->endBlock - dev->startBlock + 1) *
1744 dev->nChunksPerBlock *
1745 (dev->nDataBytesPerChunk / sb->s_blocksize);
1748 yaffs_GetNumberOfFreeChunks(dev) *
1749 (dev->nDataBytesPerChunk / sb->s_blocksize);
1754 buf->f_bavail = buf->f_bfree;
1756 yaffs_GrossUnlock(dev);
1762 static void yaffs_flush_sb_inodes(struct super_block *sb)
1767 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1768 obj = yaffs_InodeToObject(iptr);
1770 T(YAFFS_TRACE_OS, ("flushing obj %d\n",obj->objectId));
1771 yaffs_FlushFile(obj,1,0);
1776 static int yaffs_do_sync_fs(struct super_block *sb)
1779 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1780 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_do_sync_fs\n"));
1783 yaffs_GrossLock(dev);
1786 yaffs_FlushEntireDeviceCache(dev);
1787 yaffs_flush_sb_inodes(sb);
1788 yaffs_CheckpointSave(dev);
1791 yaffs_GrossUnlock(dev);
1799 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1800 static void yaffs_write_super(struct super_block *sb)
1802 static int yaffs_write_super(struct super_block *sb)
1806 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_write_super\n"));
1807 if (yaffs_auto_checkpoint >= 2)
1808 yaffs_do_sync_fs(sb);
1809 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
1815 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1816 static int yaffs_sync_fs(struct super_block *sb, int wait)
1818 static int yaffs_sync_fs(struct super_block *sb)
1821 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_fs\n"));
1823 if (yaffs_auto_checkpoint >= 1)
1824 yaffs_do_sync_fs(sb);
1829 #ifdef YAFFS_USE_OWN_IGET
1831 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1833 struct inode *inode;
1835 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1838 ("yaffs_iget for %lu\n", ino));
1840 inode = iget_locked(sb, ino);
1842 return ERR_PTR(-ENOMEM);
1843 if (!(inode->i_state & I_NEW))
1846 /* NB This is called as a side effect of other functions, but
1847 * we had to release the lock to prevent deadlocks, so
1848 * need to lock again.
1851 yaffs_GrossLock(dev);
1853 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1855 yaffs_FillInodeFromObject(inode, obj);
1857 yaffs_GrossUnlock(dev);
1859 unlock_new_inode(inode);
1865 static void yaffs_read_inode(struct inode *inode)
1867 /* NB This is called as a side effect of other functions, but
1868 * we had to release the lock to prevent deadlocks, so
1869 * need to lock again.
1873 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1876 ("yaffs_read_inode for %d\n", (int)inode->i_ino));
1878 yaffs_GrossLock(dev);
1880 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1882 yaffs_FillInodeFromObject(inode, obj);
1884 yaffs_GrossUnlock(dev);
1889 static YLIST_HEAD(yaffs_dev_list);
1891 #if 0 /* not used */
1892 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1894 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1896 if (*flags & MS_RDONLY) {
1897 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1900 ("yaffs_remount_fs: %s: RO\n", dev->name));
1902 yaffs_GrossLock(dev);
1904 yaffs_FlushEntireDeviceCache(dev);
1906 yaffs_CheckpointSave(dev);
1911 yaffs_GrossUnlock(dev);
1914 ("yaffs_remount_fs: %s: RW\n", dev->name));
1921 static void yaffs_put_super(struct super_block *sb)
1923 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1925 T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
1927 yaffs_GrossLock(dev);
1929 yaffs_FlushEntireDeviceCache(dev);
1931 yaffs_CheckpointSave(dev);
1933 if (dev->putSuperFunc)
1934 dev->putSuperFunc(sb);
1936 yaffs_Deinitialise(dev);
1938 yaffs_GrossUnlock(dev);
1940 /* we assume this is protected by lock_kernel() in mount/umount */
1941 ylist_del(&dev->devList);
1943 if (dev->spareBuffer) {
1944 YFREE(dev->spareBuffer);
1945 dev->spareBuffer = NULL;
1952 static void yaffs_MTDPutSuper(struct super_block *sb)
1954 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1959 put_mtd_device(mtd);
1963 static void yaffs_MarkSuperBlockDirty(void *vsb)
1965 struct super_block *sb = (struct super_block *)vsb;
1967 T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
1974 int skip_checkpoint_read;
1975 int skip_checkpoint_write;
1978 int tags_ecc_overridden;
1979 int lazy_loading_enabled;
1980 int lazy_loading_overridden;
1981 int empty_lost_and_found;
1982 int empty_lost_and_found_overridden;
1985 #define MAX_OPT_LEN 30
1986 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
1988 char cur_opt[MAX_OPT_LEN + 1];
1992 /* Parse through the options which is a comma seperated list */
1994 while (options_str && *options_str && !error) {
1995 memset(cur_opt, 0, MAX_OPT_LEN + 1);
1998 while(*options_str == ',')
2001 while (*options_str && *options_str != ',') {
2002 if (p < MAX_OPT_LEN) {
2003 cur_opt[p] = *options_str;
2009 if (!strcmp(cur_opt, "inband-tags"))
2010 options->inband_tags = 1;
2011 else if (!strcmp(cur_opt, "tags-ecc-off")){
2012 options->tags_ecc_on = 0;
2013 options->tags_ecc_overridden=1;
2014 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2015 options->tags_ecc_on = 1;
2016 options->tags_ecc_overridden = 1;
2017 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2018 options->lazy_loading_enabled = 0;
2019 options->lazy_loading_overridden=1;
2020 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2021 options->lazy_loading_enabled = 1;
2022 options->lazy_loading_overridden = 1;
2023 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2024 options->empty_lost_and_found = 0;
2025 options->empty_lost_and_found_overridden=1;
2026 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2027 options->empty_lost_and_found = 1;
2028 options->empty_lost_and_found_overridden=1;
2029 } else if (!strcmp(cur_opt, "no-cache"))
2030 options->no_cache = 1;
2031 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2032 options->skip_checkpoint_read = 1;
2033 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2034 options->skip_checkpoint_write = 1;
2035 else if (!strcmp(cur_opt, "no-checkpoint")) {
2036 options->skip_checkpoint_read = 1;
2037 options->skip_checkpoint_write = 1;
2039 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2048 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2049 struct super_block *sb,
2050 void *data, int silent)
2053 struct inode *inode = NULL;
2054 struct dentry *root;
2055 yaffs_Device *dev = 0;
2056 char devname_buf[BDEVNAME_SIZE + 1];
2057 struct mtd_info *mtd;
2059 char *data_str = (char *)data;
2061 yaffs_options options;
2063 sb->s_magic = YAFFS_MAGIC;
2064 sb->s_op = &yaffs_super_ops;
2065 sb->s_flags |= MS_NOATIME;
2068 printk(KERN_INFO "yaffs: sb is NULL\n");
2069 else if (!sb->s_dev)
2070 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2071 else if (!yaffs_devname(sb, devname_buf))
2072 printk(KERN_INFO "yaffs: devname is NULL\n");
2074 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2076 yaffs_devname(sb, devname_buf));
2081 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2083 memset(&options, 0, sizeof(options));
2085 if (yaffs_parse_options(&options, data_str)) {
2086 /* Option parsing failed */
2091 sb->s_blocksize = PAGE_CACHE_SIZE;
2092 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2093 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2095 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2097 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2099 ("yaffs: Write verification disabled. All guarantees "
2100 "null and void\n"));
2103 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2105 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2106 yaffs_devname(sb, devname_buf)));
2108 /* Check it's an mtd device..... */
2109 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2110 return NULL; /* This isn't an mtd device */
2112 /* Get the device */
2113 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2115 T(YAFFS_TRACE_ALWAYS,
2116 ("yaffs: MTD device #%u doesn't appear to exist\n",
2120 /* Check it's NAND */
2121 if (mtd->type != MTD_NANDFLASH) {
2122 T(YAFFS_TRACE_ALWAYS,
2123 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
2127 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
2128 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
2129 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
2130 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
2131 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
2132 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
2133 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
2134 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2135 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
2136 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
2137 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2138 T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
2140 T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
2143 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2145 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2146 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
2150 /* Added NCB 26/5/2006 for completeness */
2151 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2152 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
2158 if (yaffsVersion == 2) {
2159 /* Check for version 2 style functions */
2161 !mtd->block_isbad ||
2162 !mtd->block_markbad ||
2165 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2166 !mtd->read_oob || !mtd->write_oob) {
2169 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2171 T(YAFFS_TRACE_ALWAYS,
2172 ("yaffs: MTD device does not support required "
2177 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2178 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2179 !options.inband_tags) {
2180 T(YAFFS_TRACE_ALWAYS,
2181 ("yaffs: MTD device does not have the "
2182 "right page sizes\n"));
2186 /* Check for V1 style functions */
2190 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2191 !mtd->read_oob || !mtd->write_oob) {
2194 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2196 T(YAFFS_TRACE_ALWAYS,
2197 ("yaffs: MTD device does not support required "
2202 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2203 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2204 T(YAFFS_TRACE_ALWAYS,
2205 ("yaffs: MTD device does not support have the "
2206 "right page sizes\n"));
2211 /* OK, so if we got here, we have an MTD that's NAND and looks
2212 * like it has the right capabilities
2213 * Set the yaffs_Device up for mtd
2216 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2217 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2219 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2222 /* Deep shit could not allocate device structure */
2223 T(YAFFS_TRACE_ALWAYS,
2224 ("yaffs_read_super: Failed trying to allocate "
2225 "yaffs_Device. \n"));
2229 memset(dev, 0, sizeof(yaffs_Device));
2230 dev->genericDevice = mtd;
2231 dev->name = mtd->name;
2233 /* Set up the memory size parameters.... */
2235 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2237 dev->startBlock = 0;
2238 dev->endBlock = nBlocks - 1;
2239 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2240 dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2241 dev->nReservedBlocks = 5;
2242 dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
2243 dev->inbandTags = options.inband_tags;
2245 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2246 dev->disableLazyLoad = 1;
2248 if(options.lazy_loading_overridden)
2249 dev->disableLazyLoad = !options.lazy_loading_enabled;
2251 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2254 if(options.tags_ecc_overridden)
2255 dev->noTagsECC = !options.tags_ecc_on;
2257 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2258 dev->emptyLostAndFound = 1;
2260 if(options.empty_lost_and_found_overridden)
2261 dev->emptyLostAndFound = options.empty_lost_and_found;
2263 /* ... and the functions. */
2264 if (yaffsVersion == 2) {
2265 dev->writeChunkWithTagsToNAND =
2266 nandmtd2_WriteChunkWithTagsToNAND;
2267 dev->readChunkWithTagsFromNAND =
2268 nandmtd2_ReadChunkWithTagsFromNAND;
2269 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2270 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
2271 dev->spareBuffer = YMALLOC(mtd->oobsize);
2273 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2274 dev->totalBytesPerChunk = mtd->writesize;
2275 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2277 dev->totalBytesPerChunk = mtd->oobblock;
2278 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2280 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2282 dev->startBlock = 0;
2283 dev->endBlock = nBlocks - 1;
2285 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2286 /* use the MTD interface in yaffs_mtdif1.c */
2287 dev->writeChunkWithTagsToNAND =
2288 nandmtd1_WriteChunkWithTagsToNAND;
2289 dev->readChunkWithTagsFromNAND =
2290 nandmtd1_ReadChunkWithTagsFromNAND;
2291 dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2292 dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
2294 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2295 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2299 /* ... and common functions */
2300 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2301 dev->initialiseNAND = nandmtd_InitialiseNAND;
2303 dev->putSuperFunc = yaffs_MTDPutSuper;
2305 dev->superBlock = (void *)sb;
2306 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2309 #ifndef CONFIG_YAFFS_DOES_ECC
2310 dev->useNANDECC = 1;
2313 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2314 dev->wideTnodesDisabled = 1;
2317 dev->skipCheckpointRead = options.skip_checkpoint_read;
2318 dev->skipCheckpointWrite = options.skip_checkpoint_write;
2320 /* we assume this is protected by lock_kernel() in mount/umount */
2321 ylist_add_tail(&dev->devList, &yaffs_dev_list);
2323 /* Directory search handling...*/
2324 YINIT_LIST_HEAD(&dev->searchContexts);
2325 dev->removeObjectCallback = yaffs_RemoveObjectCallback;
2327 init_MUTEX(&dev->grossLock);
2329 yaffs_GrossLock(dev);
2331 err = yaffs_GutsInitialise(dev);
2334 ("yaffs_read_super: guts initialised %s\n",
2335 (err == YAFFS_OK) ? "OK" : "FAILED"));
2337 /* Release lock before yaffs_get_inode() */
2338 yaffs_GrossUnlock(dev);
2340 /* Create root inode */
2341 if (err == YAFFS_OK)
2342 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2348 inode->i_op = &yaffs_dir_inode_operations;
2349 inode->i_fop = &yaffs_dir_operations;
2351 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
2353 root = d_alloc_root(inode);
2355 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
2362 sb->s_dirt = !dev->isCheckpointed;
2363 T(YAFFS_TRACE_ALWAYS,
2364 ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
2366 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
2371 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2372 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2375 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2378 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2379 static int yaffs_read_super(struct file_system_type *fs,
2380 int flags, const char *dev_name,
2381 void *data, struct vfsmount *mnt)
2384 return get_sb_bdev(fs, flags, dev_name, data,
2385 yaffs_internal_read_super_mtd, mnt);
2388 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2389 int flags, const char *dev_name,
2393 return get_sb_bdev(fs, flags, dev_name, data,
2394 yaffs_internal_read_super_mtd);
2398 static struct file_system_type yaffs_fs_type = {
2399 .owner = THIS_MODULE,
2401 .get_sb = yaffs_read_super,
2402 .kill_sb = kill_block_super,
2403 .fs_flags = FS_REQUIRES_DEV,
2406 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2409 return yaffs_internal_read_super(1, sb, data, silent);
2412 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2417 #ifdef CONFIG_YAFFS_YAFFS2
2419 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2420 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2423 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2426 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2427 static int yaffs2_read_super(struct file_system_type *fs,
2428 int flags, const char *dev_name, void *data,
2429 struct vfsmount *mnt)
2431 return get_sb_bdev(fs, flags, dev_name, data,
2432 yaffs2_internal_read_super_mtd, mnt);
2435 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2436 int flags, const char *dev_name,
2440 return get_sb_bdev(fs, flags, dev_name, data,
2441 yaffs2_internal_read_super_mtd);
2445 static struct file_system_type yaffs2_fs_type = {
2446 .owner = THIS_MODULE,
2448 .get_sb = yaffs2_read_super,
2449 .kill_sb = kill_block_super,
2450 .fs_flags = FS_REQUIRES_DEV,
2453 static struct super_block *yaffs2_read_super(struct super_block *sb,
2454 void *data, int silent)
2456 return yaffs_internal_read_super(2, sb, data, silent);
2459 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2463 #endif /* CONFIG_YAFFS_YAFFS2 */
2465 static struct proc_dir_entry *my_proc_entry;
2467 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
2469 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
2470 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
2471 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
2472 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2473 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2474 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2475 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2476 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
2477 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2478 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2479 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2480 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2481 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2482 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2483 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2484 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2485 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2486 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2487 buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2488 buf += sprintf(buf, "passiveGCs......... %d\n",
2489 dev->passiveGarbageCollections);
2490 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2491 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
2492 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2493 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2494 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2495 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2496 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2497 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2498 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2499 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2501 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2507 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
2509 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
2510 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->noTagsECC);
2511 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
2512 buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
2513 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->emptyLostAndFound);
2514 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->disableLazyLoad);
2519 static int yaffs_proc_read(char *page,
2521 off_t offset, int count, int *eof, void *data)
2523 struct ylist_head *item;
2528 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2529 * We use 'offset' (*ppos) to indicate where we are in devList.
2530 * This also assumes the user has posted a read buffer large
2531 * enough to hold the complete output; but that's life in /proc.
2536 /* Print header first */
2538 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2539 "\n%s\n%s\n", yaffs_fs_c_version,
2540 yaffs_guts_c_version);
2542 buf += sprintf(buf,"\n");
2546 /* hold lock_kernel while traversing yaffs_dev_list */
2549 /* Locate and print the Nth entry. Order N-squared but N is small. */
2550 ylist_for_each(item, &yaffs_dev_list) {
2551 yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
2552 if (n < (step & ~1)) {
2557 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
2558 buf = yaffs_dump_dev_part0(buf, dev);
2560 buf = yaffs_dump_dev_part1(buf, dev);
2567 return buf - page < count ? buf - page : count;
2571 * Set the verbosity of the warnings and error messages.
2573 * Note that the names can only be a..z or _ with the current code.
2578 unsigned mask_bitfield;
2580 {"allocate", YAFFS_TRACE_ALLOCATE},
2581 {"always", YAFFS_TRACE_ALWAYS},
2582 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2583 {"buffers", YAFFS_TRACE_BUFFERS},
2584 {"bug", YAFFS_TRACE_BUG},
2585 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2586 {"deletion", YAFFS_TRACE_DELETION},
2587 {"erase", YAFFS_TRACE_ERASE},
2588 {"error", YAFFS_TRACE_ERROR},
2589 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2590 {"gc", YAFFS_TRACE_GC},
2591 {"mtd", YAFFS_TRACE_MTD},
2592 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2593 {"os", YAFFS_TRACE_OS},
2594 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2595 {"scan", YAFFS_TRACE_SCAN},
2596 {"tracing", YAFFS_TRACE_TRACING},
2597 {"sync", YAFFS_TRACE_SYNC},
2599 {"verify", YAFFS_TRACE_VERIFY},
2600 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2601 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2602 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2604 {"write", YAFFS_TRACE_WRITE},
2605 {"all", 0xffffffff},
2610 #define MAX_MASK_NAME_LENGTH 40
2611 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2612 unsigned long count, void *data)
2614 unsigned rg = 0, mask_bitfield;
2618 char substring[MAX_MASK_NAME_LENGTH + 1];
2624 rg = yaffs_traceMask;
2626 while (!done && (pos < count)) {
2628 while ((pos < count) && isspace(buf[pos]))
2645 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2647 if (end > buf + pos) {
2648 mask_name = "numeral";
2649 len = end - (buf + pos);
2653 for (x = buf + pos, i = 0;
2654 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2655 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2657 substring[i] = '\0';
2659 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2660 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
2661 mask_name = mask_flags[i].mask_name;
2662 mask_bitfield = mask_flags[i].mask_bitfield;
2669 if (mask_name != NULL) {
2673 rg &= ~mask_bitfield;
2676 rg |= mask_bitfield;
2682 rg |= mask_bitfield;
2688 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2690 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
2692 if (rg & YAFFS_TRACE_ALWAYS) {
2693 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2695 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2696 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
2704 static int yaffs_proc_write(struct file *file, const char *buf,
2705 unsigned long count, void *data)
2707 return yaffs_proc_write_trace_options(file, buf, count, data);
2710 /* Stuff to handle installation of file systems */
2711 struct file_system_to_install {
2712 struct file_system_type *fst;
2716 static struct file_system_to_install fs_to_install[] = {
2717 {&yaffs_fs_type, 0},
2718 {&yaffs2_fs_type, 0},
2722 static int __init init_yaffs_fs(void)
2725 struct file_system_to_install *fsinst;
2727 T(YAFFS_TRACE_ALWAYS,
2728 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2730 /* Install the proc_fs entry */
2731 my_proc_entry = create_proc_entry("yaffs",
2735 if (my_proc_entry) {
2736 my_proc_entry->write_proc = yaffs_proc_write;
2737 my_proc_entry->read_proc = yaffs_proc_read;
2738 my_proc_entry->data = NULL;
2742 /* Now add the file system entries */
2744 fsinst = fs_to_install;
2746 while (fsinst->fst && !error) {
2747 error = register_filesystem(fsinst->fst);
2749 fsinst->installed = 1;
2753 /* Any errors? uninstall */
2755 fsinst = fs_to_install;
2757 while (fsinst->fst) {
2758 if (fsinst->installed) {
2759 unregister_filesystem(fsinst->fst);
2760 fsinst->installed = 0;
2769 static void __exit exit_yaffs_fs(void)
2772 struct file_system_to_install *fsinst;
2774 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2777 remove_proc_entry("yaffs", YPROC_ROOT);
2779 fsinst = fs_to_install;
2781 while (fsinst->fst) {
2782 if (fsinst->installed) {
2783 unregister_filesystem(fsinst->fst);
2784 fsinst->installed = 0;
2790 module_init(init_yaffs_fs)
2791 module_exit(exit_yaffs_fs)
2793 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2794 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2795 MODULE_LICENSE("GPL");