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.89 2009-11-29 21:50:10 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"
57 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
59 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
60 #include <linux/statfs.h>
61 #define UnlockPage(p) unlock_page(p)
62 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
64 /* FIXME: use sb->s_id instead ? */
65 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
69 #include <linux/locks.h>
70 #define BDEVNAME_SIZE 0
71 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
73 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
74 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
80 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
81 #define YPROC_ROOT (&proc_root)
83 #define YPROC_ROOT NULL
86 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
87 #define WRITE_SIZE_STR "writesize"
88 #define WRITE_SIZE(mtd) ((mtd)->writesize)
90 #define WRITE_SIZE_STR "oobblock"
91 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
94 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
95 #define YAFFS_USE_WRITE_BEGIN_END 1
97 #define YAFFS_USE_WRITE_BEGIN_END 0
100 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
101 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
103 uint64_t result = partition_size;
104 do_div(result, block_size);
105 return (uint32_t)result;
108 #define YCALCBLOCKS(s, b) ((s)/(b))
111 #include <linux/uaccess.h>
113 #include "yportenv.h"
114 #include "yaffs_guts.h"
116 #include <linux/mtd/mtd.h>
117 #include "yaffs_mtdif.h"
118 #include "yaffs_mtdif1.h"
119 #include "yaffs_mtdif2.h"
121 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
122 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
123 unsigned int yaffs_auto_checkpoint = 1;
125 /* Module Parameters */
126 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
127 module_param(yaffs_traceMask, uint, 0644);
128 module_param(yaffs_wr_attempts, uint, 0644);
129 module_param(yaffs_auto_checkpoint, uint, 0644);
131 MODULE_PARM(yaffs_traceMask, "i");
132 MODULE_PARM(yaffs_wr_attempts, "i");
133 MODULE_PARM(yaffs_auto_checkpoint, "i");
136 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
137 /* use iget and read_inode */
138 #define Y_IGET(sb, inum) iget((sb), (inum))
139 static void yaffs_read_inode(struct inode *inode);
142 /* Call local equivalent */
143 #define YAFFS_USE_OWN_IGET
144 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
146 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
149 /*#define T(x) printk x */
151 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
152 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
154 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
157 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
158 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
160 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
161 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
163 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
167 #define update_dir_time(dir) do {\
168 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
171 static void yaffs_put_super(struct super_block *sb);
173 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
175 static ssize_t yaffs_hold_space(struct file *f);
176 static void yaffs_release_space(struct file *f);
178 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
179 static int yaffs_file_flush(struct file *file, fl_owner_t id);
181 static int yaffs_file_flush(struct file *file);
184 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
187 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
189 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
190 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
191 struct nameidata *n);
192 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
193 struct nameidata *n);
195 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
196 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
198 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
199 struct dentry *dentry);
200 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
201 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
202 const char *symname);
203 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
205 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
206 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
209 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
212 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
213 struct inode *new_dir, struct dentry *new_dentry);
214 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
216 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
217 static int yaffs_sync_fs(struct super_block *sb, int wait);
218 static void yaffs_write_super(struct super_block *sb);
220 static int yaffs_sync_fs(struct super_block *sb);
221 static int yaffs_write_super(struct super_block *sb);
224 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
225 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
226 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
227 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
229 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
232 #ifdef YAFFS_HAS_PUT_INODE
233 static void yaffs_put_inode(struct inode *inode);
236 static void yaffs_delete_inode(struct inode *);
237 static void yaffs_clear_inode(struct inode *);
239 static int yaffs_readpage(struct file *file, struct page *page);
240 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
241 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
243 static int yaffs_writepage(struct page *page);
247 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
248 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
249 loff_t pos, unsigned len, unsigned flags,
250 struct page **pagep, void **fsdata);
251 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
252 loff_t pos, unsigned len, unsigned copied,
253 struct page *pg, void *fsdadata);
255 static int yaffs_prepare_write(struct file *f, struct page *pg,
256 unsigned offset, unsigned to);
257 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
262 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
264 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
265 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
267 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
270 static struct address_space_operations yaffs_file_address_operations = {
271 .readpage = yaffs_readpage,
272 .writepage = yaffs_writepage,
273 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
274 .write_begin = yaffs_write_begin,
275 .write_end = yaffs_write_end,
277 .prepare_write = yaffs_prepare_write,
278 .commit_write = yaffs_commit_write,
282 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
283 static const struct file_operations yaffs_file_operations = {
284 .read = do_sync_read,
285 .write = do_sync_write,
286 .aio_read = generic_file_aio_read,
287 .aio_write = generic_file_aio_write,
288 .mmap = generic_file_mmap,
289 .flush = yaffs_file_flush,
290 .fsync = yaffs_sync_object,
291 .splice_read = generic_file_splice_read,
292 .splice_write = generic_file_splice_write,
293 .llseek = generic_file_llseek,
296 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
298 static const struct file_operations yaffs_file_operations = {
299 .read = do_sync_read,
300 .write = do_sync_write,
301 .aio_read = generic_file_aio_read,
302 .aio_write = generic_file_aio_write,
303 .mmap = generic_file_mmap,
304 .flush = yaffs_file_flush,
305 .fsync = yaffs_sync_object,
306 .sendfile = generic_file_sendfile,
311 static const struct file_operations yaffs_file_operations = {
312 .read = generic_file_read,
313 .write = generic_file_write,
314 .mmap = generic_file_mmap,
315 .flush = yaffs_file_flush,
316 .fsync = yaffs_sync_object,
317 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
318 .sendfile = generic_file_sendfile,
323 static const struct inode_operations yaffs_file_inode_operations = {
324 .setattr = yaffs_setattr,
327 static const struct inode_operations yaffs_symlink_inode_operations = {
328 .readlink = yaffs_readlink,
329 .follow_link = yaffs_follow_link,
330 .setattr = yaffs_setattr,
333 static const struct inode_operations yaffs_dir_inode_operations = {
334 .create = yaffs_create,
335 .lookup = yaffs_lookup,
337 .unlink = yaffs_unlink,
338 .symlink = yaffs_symlink,
339 .mkdir = yaffs_mkdir,
340 .rmdir = yaffs_unlink,
341 .mknod = yaffs_mknod,
342 .rename = yaffs_rename,
343 .setattr = yaffs_setattr,
346 static const struct file_operations yaffs_dir_operations = {
347 .read = generic_read_dir,
348 .readdir = yaffs_readdir,
349 .fsync = yaffs_sync_object,
352 static const struct super_operations yaffs_super_ops = {
353 .statfs = yaffs_statfs,
355 #ifndef YAFFS_USE_OWN_IGET
356 .read_inode = yaffs_read_inode,
358 #ifdef YAFFS_HAS_PUT_INODE
359 .put_inode = yaffs_put_inode,
361 .put_super = yaffs_put_super,
362 .delete_inode = yaffs_delete_inode,
363 .clear_inode = yaffs_clear_inode,
364 .sync_fs = yaffs_sync_fs,
365 .write_super = yaffs_write_super,
368 static void yaffs_GrossLock(yaffs_Device *dev)
370 T(YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
371 down(&dev->grossLock);
372 T(YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
375 static void yaffs_GrossUnlock(yaffs_Device *dev)
377 T(YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
382 /*-----------------------------------------------------------------*/
383 /* Directory search context allows us to unlock access to yaffs during
384 * filldir without causing problems with the directory being modified.
385 * This is similar to the tried and tested mechanism used in yaffs direct.
387 * A search context iterates along a doubly linked list of siblings in the
388 * directory. If the iterating object is deleted then this would corrupt
389 * the list iteration, likely causing a crash. The search context avoids
390 * this by using the removeObjectCallback to move the search context to the
391 * next object before the object is deleted.
393 * Many readdirs (and thus seach conexts) may be alive simulateously so
394 * each yaffs_Device has a list of these.
396 * A seach context lives for the duration of a readdir.
398 * All these functions must be called while yaffs is locked.
401 struct yaffs_SearchContext {
403 yaffs_Object *dirObj;
404 yaffs_Object *nextReturn;
405 struct ylist_head others;
409 * yaffs_NewSearch() creates a new search context, initialises it and
410 * adds it to the device's search context list.
412 * Called at start of readdir.
414 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
416 yaffs_Device *dev = dir->myDev;
417 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
421 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
422 sc->nextReturn = NULL;
424 sc->nextReturn = ylist_entry(
425 dir->variant.directoryVariant.children.next,
426 yaffs_Object,siblings);
427 YINIT_LIST_HEAD(&sc->others);
428 ylist_add(&sc->others,&dev->searchContexts);
434 * yaffs_EndSearch() disposes of a search context and cleans up.
436 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
439 ylist_del(&sc->others);
445 * yaffs_SearchAdvance() moves a search context to the next object.
446 * Called when the search iterates or when an object removal causes
447 * the search context to be moved to the next object.
449 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
454 if( sc->nextReturn == NULL ||
455 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
456 sc->nextReturn = NULL;
458 struct ylist_head *next = sc->nextReturn->siblings.next;
460 if( next == &sc->dirObj->variant.directoryVariant.children)
461 sc->nextReturn = NULL; /* end of list */
463 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
468 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
469 * We check open search contexts and advance any which are currently
470 * on the object being iterated.
472 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
475 struct ylist_head *i;
476 struct yaffs_SearchContext *sc;
477 struct ylist_head *search_contexts = &obj->myDev->searchContexts;
480 /* Iterate through the directory search contexts.
481 * If any are currently on the object being removed, then advance
482 * the search context to the next object to prevent a hanging pointer.
484 ylist_for_each(i, search_contexts) {
486 sc = ylist_entry(i, struct yaffs_SearchContext,others);
487 if(sc->nextReturn == obj)
488 yaffs_SearchAdvance(sc);
495 /*-----------------------------------------------------------------*/
497 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
500 unsigned char *alias;
503 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
505 yaffs_GrossLock(dev);
507 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
509 yaffs_GrossUnlock(dev);
514 ret = vfs_readlink(dentry, buffer, buflen, alias);
519 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
520 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
522 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
525 unsigned char *alias;
527 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
529 yaffs_GrossLock(dev);
531 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
533 yaffs_GrossUnlock(dev);
540 ret = vfs_follow_link(nd, alias);
543 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
550 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
554 * Lookup is used to find objects in the fs
556 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
558 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
561 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
565 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
567 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
569 yaffs_GrossLock(dev);
572 ("yaffs_lookup for %d:%s\n",
573 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
575 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
576 dentry->d_name.name);
578 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
580 /* Can't hold gross lock when calling yaffs_get_inode() */
581 yaffs_GrossUnlock(dev);
585 ("yaffs_lookup found %d\n", obj->objectId));
587 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
591 ("yaffs_loookup dentry \n"));
592 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
593 * d_add even if NULL inode */
595 /*dget(dentry); // try to solve directory bug */
596 d_add(dentry, inode);
604 T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
608 /* added NCB for 2.5/6 compatability - forces add even if inode is
609 * NULL which creates dentry hash */
610 d_add(dentry, inode);
616 #ifdef YAFFS_HAS_PUT_INODE
618 /* For now put inode is just for debugging
619 * Put inode is called when the inode **structure** is put.
621 static void yaffs_put_inode(struct inode *inode)
624 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
625 atomic_read(&inode->i_count)));
630 /* clear is called to tell the fs to release any per-inode data it holds */
631 static void yaffs_clear_inode(struct inode *inode)
636 obj = yaffs_InodeToObject(inode);
639 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
640 atomic_read(&inode->i_count),
641 obj ? "object exists" : "null object"));
645 yaffs_GrossLock(dev);
647 /* Clear the association between the inode and
651 yaffs_InodeToObjectLV(inode) = NULL;
653 /* If the object freeing was deferred, then the real
655 * This should fix the inode inconsistency problem.
658 yaffs_HandleDeferedFree(obj);
660 yaffs_GrossUnlock(dev);
665 /* delete is called when the link count is zero and the inode
666 * is put (ie. nobody wants to know about it anymore, time to
668 * NB Must call clear_inode()
670 static void yaffs_delete_inode(struct inode *inode)
672 yaffs_Object *obj = yaffs_InodeToObject(inode);
676 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
677 atomic_read(&inode->i_count),
678 obj ? "object exists" : "null object"));
682 yaffs_GrossLock(dev);
683 yaffs_DeleteObject(obj);
684 yaffs_GrossUnlock(dev);
686 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
687 truncate_inode_pages(&inode->i_data, 0);
692 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
693 static int yaffs_file_flush(struct file *file, fl_owner_t id)
695 static int yaffs_file_flush(struct file *file)
698 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
700 yaffs_Device *dev = obj->myDev;
703 ("yaffs_file_flush object %d (%s)\n", obj->objectId,
704 obj->dirty ? "dirty" : "clean"));
706 yaffs_GrossLock(dev);
708 yaffs_FlushFile(obj, 1,0);
710 yaffs_GrossUnlock(dev);
715 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
717 /* Lifted from jffs2 */
720 unsigned char *pg_buf;
725 T(YAFFS_TRACE_OS, ("yaffs_readpage at %08x, size %08x\n",
726 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
727 (unsigned)PAGE_CACHE_SIZE));
729 obj = yaffs_DentryToObject(f->f_dentry);
733 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
734 BUG_ON(!PageLocked(pg));
741 /* FIXME: Can kmap fail? */
743 yaffs_GrossLock(dev);
745 ret = yaffs_ReadDataFromFile(obj, pg_buf,
746 pg->index << PAGE_CACHE_SHIFT,
749 yaffs_GrossUnlock(dev);
755 ClearPageUptodate(pg);
762 flush_dcache_page(pg);
765 T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
769 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
771 int ret = yaffs_readpage_nolock(f, pg);
776 static int yaffs_readpage(struct file *f, struct page *pg)
778 return yaffs_readpage_unlock(f, pg);
781 /* writepage inspired by/stolen from smbfs */
783 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
784 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
786 static int yaffs_writepage(struct page *page)
789 struct address_space *mapping = page->mapping;
790 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
792 unsigned long end_index;
800 inode = mapping->host;
804 if (offset > inode->i_size) {
806 ("yaffs_writepage at %08x, inode size = %08x!!!\n",
807 (unsigned)(page->index << PAGE_CACHE_SHIFT),
808 (unsigned)inode->i_size));
810 (" -> don't care!!\n"));
815 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
818 if (page->index < end_index)
819 nBytes = PAGE_CACHE_SIZE;
821 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
827 obj = yaffs_InodeToObject(inode);
828 yaffs_GrossLock(obj->myDev);
831 ("yaffs_writepage at %08x, size %08x\n",
832 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
834 ("writepag0: obj = %05x, ino = %05x\n",
835 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
837 nWritten = yaffs_WriteDataToFile(obj, buffer,
838 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
841 ("writepag1: obj = %05x, ino = %05x\n",
842 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
844 yaffs_GrossUnlock(obj->myDev);
847 SetPageUptodate(page);
851 return (nWritten == nBytes) ? 0 : -ENOSPC;
855 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
856 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
857 loff_t pos, unsigned len, unsigned flags,
858 struct page **pagep, void **fsdata)
860 struct page *pg = NULL;
861 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
862 uint32_t offset = pos & (PAGE_CACHE_SIZE - 1);
863 uint32_t to = offset + len;
868 T(YAFFS_TRACE_OS, ("start yaffs_write_begin\n"));
870 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
871 pg = grab_cache_page_write_begin(mapping, index, flags);
873 pg = __grab_cache_page(mapping, index);
882 space_held = yaffs_hold_space(filp);
889 /* Update page if required */
891 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
892 ret = yaffs_readpage_nolock(filp, pg);
897 /* Happy path return */
898 T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
903 T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
905 yaffs_release_space(filp);
908 page_cache_release(pg);
915 static int yaffs_prepare_write(struct file *f, struct page *pg,
916 unsigned offset, unsigned to)
918 T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
920 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
921 return yaffs_readpage_nolock(f, pg);
926 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
927 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
928 loff_t pos, unsigned len, unsigned copied,
929 struct page *pg, void *fsdadata)
933 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
936 addr = kva + offset_into_page;
939 ("yaffs_write_end addr %x pos %x nBytes %d\n",
943 ret = yaffs_file_write(filp, addr, copied, &pos);
947 ("yaffs_write_end not same size ret %d copied %d\n",
950 ClearPageUptodate(pg);
957 yaffs_release_space(filp);
959 page_cache_release(pg);
964 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
969 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
970 int nBytes = to - offset;
979 saddr = (unsigned) addr;
982 ("yaffs_commit_write addr %x pos %x nBytes %d\n",
983 saddr, spos, nBytes));
985 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
987 if (nWritten != nBytes) {
989 ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
992 ClearPageUptodate(pg);
1000 ("yaffs_commit_write returning %d\n",
1001 nWritten == nBytes ? 0 : nWritten));
1003 return nWritten == nBytes ? 0 : nWritten;
1008 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1013 /* Check mode against the variant type and attempt to repair if broken. */
1014 __u32 mode = obj->yst_mode;
1015 switch (obj->variantType) {
1016 case YAFFS_OBJECT_TYPE_FILE:
1017 if (!S_ISREG(mode)) {
1018 obj->yst_mode &= ~S_IFMT;
1019 obj->yst_mode |= S_IFREG;
1023 case YAFFS_OBJECT_TYPE_SYMLINK:
1024 if (!S_ISLNK(mode)) {
1025 obj->yst_mode &= ~S_IFMT;
1026 obj->yst_mode |= S_IFLNK;
1030 case YAFFS_OBJECT_TYPE_DIRECTORY:
1031 if (!S_ISDIR(mode)) {
1032 obj->yst_mode &= ~S_IFMT;
1033 obj->yst_mode |= S_IFDIR;
1037 case YAFFS_OBJECT_TYPE_UNKNOWN:
1038 case YAFFS_OBJECT_TYPE_HARDLINK:
1039 case YAFFS_OBJECT_TYPE_SPECIAL:
1045 inode->i_flags |= S_NOATIME;
1047 inode->i_ino = obj->objectId;
1048 inode->i_mode = obj->yst_mode;
1049 inode->i_uid = obj->yst_uid;
1050 inode->i_gid = obj->yst_gid;
1051 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1052 inode->i_blksize = inode->i_sb->s_blocksize;
1054 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1056 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1057 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1058 inode->i_atime.tv_nsec = 0;
1059 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1060 inode->i_mtime.tv_nsec = 0;
1061 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1062 inode->i_ctime.tv_nsec = 0;
1064 inode->i_rdev = obj->yst_rdev;
1065 inode->i_atime = obj->yst_atime;
1066 inode->i_mtime = obj->yst_mtime;
1067 inode->i_ctime = obj->yst_ctime;
1069 inode->i_size = yaffs_GetObjectFileLength(obj);
1070 inode->i_blocks = (inode->i_size + 511) >> 9;
1072 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1075 ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
1076 inode->i_mode, inode->i_uid, inode->i_gid,
1077 (int)inode->i_size, atomic_read(&inode->i_count)));
1079 switch (obj->yst_mode & S_IFMT) {
1080 default: /* fifo, device or socket */
1081 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1082 init_special_inode(inode, obj->yst_mode,
1083 old_decode_dev(obj->yst_rdev));
1085 init_special_inode(inode, obj->yst_mode,
1086 (dev_t) (obj->yst_rdev));
1089 case S_IFREG: /* file */
1090 inode->i_op = &yaffs_file_inode_operations;
1091 inode->i_fop = &yaffs_file_operations;
1092 inode->i_mapping->a_ops =
1093 &yaffs_file_address_operations;
1095 case S_IFDIR: /* directory */
1096 inode->i_op = &yaffs_dir_inode_operations;
1097 inode->i_fop = &yaffs_dir_operations;
1099 case S_IFLNK: /* symlink */
1100 inode->i_op = &yaffs_symlink_inode_operations;
1104 yaffs_InodeToObjectLV(inode) = obj;
1106 obj->myInode = inode;
1110 ("yaffs_FileInode invalid parameters\n"));
1115 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1118 struct inode *inode;
1122 ("yaffs_get_inode for NULL super_block!!\n"));
1129 ("yaffs_get_inode for NULL object!!\n"));
1135 ("yaffs_get_inode for object %d\n", obj->objectId));
1137 inode = Y_IGET(sb, obj->objectId);
1141 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1142 /* iget also increments the inode's i_count */
1143 /* NB You can't be holding grossLock or deadlock will happen! */
1148 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1153 struct inode *inode;
1156 obj = yaffs_DentryToObject(f->f_dentry);
1160 yaffs_GrossLock(dev);
1162 inode = f->f_dentry->d_inode;
1164 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1165 ipos = inode->i_size;
1171 ("yaffs_file_write: hey obj is null!\n"));
1174 ("yaffs_file_write about to write writing %zu bytes"
1175 "to object %d at %d\n",
1176 n, obj->objectId, ipos));
1178 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1181 ("yaffs_file_write writing %zu bytes, %d written at %d\n",
1182 n, nWritten, ipos));
1187 if (ipos > inode->i_size) {
1188 inode->i_size = ipos;
1189 inode->i_blocks = (ipos + 511) >> 9;
1192 ("yaffs_file_write size updated to %d bytes, "
1194 ipos, (int)(inode->i_blocks)));
1198 yaffs_GrossUnlock(dev);
1199 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1202 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1203 /* For now we just assume few parallel writes and check against a small number. */
1204 /* Todo: need to do this with a counter to handle parallel reads better */
1206 static ssize_t yaffs_hold_space(struct file *f)
1214 obj = yaffs_DentryToObject(f->f_dentry);
1218 yaffs_GrossLock(dev);
1220 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1222 yaffs_GrossUnlock(dev);
1224 return (nFreeChunks > 20) ? 1 : 0;
1227 static void yaffs_release_space(struct file *f)
1233 obj = yaffs_DentryToObject(f->f_dentry);
1237 yaffs_GrossLock(dev);
1240 yaffs_GrossUnlock(dev);
1243 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1247 struct yaffs_SearchContext *sc;
1248 struct inode *inode = f->f_dentry->d_inode;
1249 unsigned long offset, curoffs;
1253 char name[YAFFS_MAX_NAME_LENGTH + 1];
1255 obj = yaffs_DentryToObject(f->f_dentry);
1258 yaffs_GrossLock(dev);
1262 sc = yaffs_NewSearch(obj);
1268 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
1272 ("yaffs_readdir: entry . ino %d \n",
1273 (int)inode->i_ino));
1274 yaffs_GrossUnlock(dev);
1275 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
1277 yaffs_GrossLock(dev);
1283 ("yaffs_readdir: entry .. ino %d \n",
1284 (int)f->f_dentry->d_parent->d_inode->i_ino));
1285 yaffs_GrossUnlock(dev);
1286 if (filldir(dirent, "..", 2, offset,
1287 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
1289 yaffs_GrossLock(dev);
1296 /* If the directory has changed since the open or last call to
1297 readdir, rewind to after the 2 canned entries. */
1299 if (f->f_version != inode->i_version) {
1302 f->f_version = inode->i_version;
1305 while(sc->nextReturn){
1308 if (curoffs >= offset) {
1309 int this_inode = yaffs_GetObjectInode(l);
1310 int this_type = yaffs_GetObjectType(l);
1312 yaffs_GetObjectName(l, name,
1313 YAFFS_MAX_NAME_LENGTH + 1);
1315 ("yaffs_readdir: %s inode %d\n", name,
1316 yaffs_GetObjectInode(l)));
1318 yaffs_GrossUnlock(dev);
1328 yaffs_GrossLock(dev);
1333 yaffs_SearchAdvance(sc);
1337 yaffs_GrossUnlock(dev);
1339 yaffs_EndSearch(sc);
1345 * File creation. Allocate an inode, and we're done..
1348 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1351 #define YCRED(x) (x->cred)
1354 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1355 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1358 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1362 struct inode *inode;
1364 yaffs_Object *obj = NULL;
1367 yaffs_Object *parent = yaffs_InodeToObject(dir);
1369 int error = -ENOSPC;
1370 uid_t uid = YCRED(current)->fsuid;
1371 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1373 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1378 ("yaffs_mknod: parent object %d type %d\n",
1379 parent->objectId, parent->variantType));
1382 ("yaffs_mknod: could not get parent object\n"));
1386 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1388 dentry->d_name.name, mode, rdev));
1390 dev = parent->myDev;
1392 yaffs_GrossLock(dev);
1394 switch (mode & S_IFMT) {
1396 /* Special (socket, fifo, device...) */
1397 T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
1398 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1399 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1400 gid, old_encode_dev(rdev));
1402 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1406 case S_IFREG: /* file */
1407 T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
1408 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1411 case S_IFDIR: /* directory */
1413 ("yaffs_mknod: making directory\n"));
1414 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1417 case S_IFLNK: /* symlink */
1418 T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
1419 obj = NULL; /* Do we ever get here? */
1423 /* Can not call yaffs_get_inode() with gross lock held */
1424 yaffs_GrossUnlock(dev);
1427 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1428 d_instantiate(dentry, inode);
1429 update_dir_time(dir);
1431 ("yaffs_mknod created object %d count = %d\n",
1432 obj->objectId, atomic_read(&inode->i_count)));
1436 ("yaffs_mknod failed making object\n"));
1443 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1446 T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
1447 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1451 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1452 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1453 struct nameidata *n)
1455 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1458 T(YAFFS_TRACE_OS, ("yaffs_create\n"));
1459 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1462 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1469 ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1470 dentry->d_name.name));
1472 dev = yaffs_InodeToObject(dir)->myDev;
1474 yaffs_GrossLock(dev);
1476 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1478 if (retVal == YAFFS_OK) {
1479 dentry->d_inode->i_nlink--;
1481 yaffs_GrossUnlock(dev);
1482 mark_inode_dirty(dentry->d_inode);
1483 update_dir_time(dir);
1486 yaffs_GrossUnlock(dev);
1493 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1494 struct dentry *dentry)
1496 struct inode *inode = old_dentry->d_inode;
1497 yaffs_Object *obj = NULL;
1498 yaffs_Object *link = NULL;
1501 T(YAFFS_TRACE_OS, ("yaffs_link\n"));
1503 obj = yaffs_InodeToObject(inode);
1506 yaffs_GrossLock(dev);
1508 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1509 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1513 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1514 d_instantiate(dentry, old_dentry->d_inode);
1515 atomic_inc(&old_dentry->d_inode->i_count);
1517 ("yaffs_link link count %d i_count %d\n",
1518 old_dentry->d_inode->i_nlink,
1519 atomic_read(&old_dentry->d_inode->i_count)));
1522 yaffs_GrossUnlock(dev);
1525 update_dir_time(dir);
1532 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1533 const char *symname)
1537 uid_t uid = YCRED(current)->fsuid;
1538 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1540 T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
1542 dev = yaffs_InodeToObject(dir)->myDev;
1543 yaffs_GrossLock(dev);
1544 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1545 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1546 yaffs_GrossUnlock(dev);
1549 struct inode *inode;
1551 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1552 d_instantiate(dentry, inode);
1553 update_dir_time(dir);
1554 T(YAFFS_TRACE_OS, ("symlink created OK\n"));
1557 T(YAFFS_TRACE_OS, ("symlink not created\n"));
1563 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1570 obj = yaffs_DentryToObject(dentry);
1574 T(YAFFS_TRACE_OS, ("yaffs_sync_object\n"));
1575 yaffs_GrossLock(dev);
1576 yaffs_FlushFile(obj, 1, datasync);
1577 yaffs_GrossUnlock(dev);
1582 * The VFS layer already does all the dentry stuff for rename.
1584 * NB: POSIX says you can rename an object over an old object of the same name
1586 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1587 struct inode *new_dir, struct dentry *new_dentry)
1590 int retVal = YAFFS_FAIL;
1591 yaffs_Object *target;
1593 T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
1594 dev = yaffs_InodeToObject(old_dir)->myDev;
1596 yaffs_GrossLock(dev);
1598 /* Check if the target is an existing directory that is not empty. */
1599 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1600 new_dentry->d_name.name);
1604 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1605 !ylist_empty(&target->variant.directoryVariant.children)) {
1607 T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
1609 retVal = YAFFS_FAIL;
1611 /* Now does unlinking internally using shadowing mechanism */
1612 T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
1614 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1615 old_dentry->d_name.name,
1616 yaffs_InodeToObject(new_dir),
1617 new_dentry->d_name.name);
1619 yaffs_GrossUnlock(dev);
1621 if (retVal == YAFFS_OK) {
1623 new_dentry->d_inode->i_nlink--;
1624 mark_inode_dirty(new_dentry->d_inode);
1627 update_dir_time(old_dir);
1628 if(old_dir != new_dir)
1629 update_dir_time(new_dir);
1636 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1638 struct inode *inode = dentry->d_inode;
1643 ("yaffs_setattr of object %d\n",
1644 yaffs_InodeToObject(inode)->objectId));
1646 error = inode_change_ok(inode, attr);
1648 dev = yaffs_InodeToObject(inode)->myDev;
1649 yaffs_GrossLock(dev);
1650 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1656 yaffs_GrossUnlock(dev);
1658 error = inode_setattr(inode, attr);
1663 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1664 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1666 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1667 struct super_block *sb = dentry->d_sb;
1668 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1669 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1671 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1673 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1675 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1678 T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
1680 yaffs_GrossLock(dev);
1682 buf->f_type = YAFFS_MAGIC;
1683 buf->f_bsize = sb->s_blocksize;
1684 buf->f_namelen = 255;
1686 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1687 /* Do this if chunk size is not a power of 2 */
1689 uint64_t bytesInDev;
1692 bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock + 1))) *
1693 ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
1695 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1696 buf->f_blocks = bytesInDev;
1698 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1699 ((uint64_t)(dev->nDataBytesPerChunk));
1701 do_div(bytesFree, sb->s_blocksize);
1703 buf->f_bfree = bytesFree;
1705 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1708 (dev->endBlock - dev->startBlock + 1) *
1709 dev->nChunksPerBlock /
1710 (sb->s_blocksize / dev->nDataBytesPerChunk);
1712 yaffs_GetNumberOfFreeChunks(dev) /
1713 (sb->s_blocksize / dev->nDataBytesPerChunk);
1716 (dev->endBlock - dev->startBlock + 1) *
1717 dev->nChunksPerBlock *
1718 (dev->nDataBytesPerChunk / sb->s_blocksize);
1721 yaffs_GetNumberOfFreeChunks(dev) *
1722 (dev->nDataBytesPerChunk / sb->s_blocksize);
1727 buf->f_bavail = buf->f_bfree;
1729 yaffs_GrossUnlock(dev);
1735 static void yaffs_flush_sb_inodes(struct super_block *sb)
1740 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1741 obj = yaffs_InodeToObject(iptr);
1743 T(YAFFS_TRACE_OS, ("flushing obj %d\n",obj->objectId));
1744 yaffs_FlushFile(obj,1,0);
1749 static int yaffs_do_sync_fs(struct super_block *sb)
1752 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1753 T(YAFFS_TRACE_OS, ("yaffs_do_sync_fs\n"));
1756 yaffs_GrossLock(dev);
1759 yaffs_FlushEntireDeviceCache(dev);
1760 yaffs_flush_sb_inodes(sb);
1761 yaffs_CheckpointSave(dev);
1764 yaffs_GrossUnlock(dev);
1772 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1773 static void yaffs_write_super(struct super_block *sb)
1775 static int yaffs_write_super(struct super_block *sb)
1779 T(YAFFS_TRACE_OS, ("yaffs_write_super\n"));
1780 if (yaffs_auto_checkpoint >= 2)
1781 yaffs_do_sync_fs(sb);
1782 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
1788 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1789 static int yaffs_sync_fs(struct super_block *sb, int wait)
1791 static int yaffs_sync_fs(struct super_block *sb)
1794 T(YAFFS_TRACE_OS, ("yaffs_sync_fs\n"));
1796 if (yaffs_auto_checkpoint >= 1)
1797 yaffs_do_sync_fs(sb);
1802 #ifdef YAFFS_USE_OWN_IGET
1804 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1806 struct inode *inode;
1808 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1811 ("yaffs_iget for %lu\n", ino));
1813 inode = iget_locked(sb, ino);
1815 return ERR_PTR(-ENOMEM);
1816 if (!(inode->i_state & I_NEW))
1819 /* NB This is called as a side effect of other functions, but
1820 * we had to release the lock to prevent deadlocks, so
1821 * need to lock again.
1824 yaffs_GrossLock(dev);
1826 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1828 yaffs_FillInodeFromObject(inode, obj);
1830 yaffs_GrossUnlock(dev);
1832 unlock_new_inode(inode);
1838 static void yaffs_read_inode(struct inode *inode)
1840 /* NB This is called as a side effect of other functions, but
1841 * we had to release the lock to prevent deadlocks, so
1842 * need to lock again.
1846 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1849 ("yaffs_read_inode for %d\n", (int)inode->i_ino));
1851 yaffs_GrossLock(dev);
1853 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1855 yaffs_FillInodeFromObject(inode, obj);
1857 yaffs_GrossUnlock(dev);
1862 static YLIST_HEAD(yaffs_dev_list);
1864 #if 0 /* not used */
1865 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1867 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1869 if (*flags & MS_RDONLY) {
1870 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1873 ("yaffs_remount_fs: %s: RO\n", dev->name));
1875 yaffs_GrossLock(dev);
1877 yaffs_FlushEntireDeviceCache(dev);
1879 yaffs_CheckpointSave(dev);
1884 yaffs_GrossUnlock(dev);
1887 ("yaffs_remount_fs: %s: RW\n", dev->name));
1894 static void yaffs_put_super(struct super_block *sb)
1896 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1898 T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
1900 yaffs_GrossLock(dev);
1902 yaffs_FlushEntireDeviceCache(dev);
1904 yaffs_CheckpointSave(dev);
1906 if (dev->putSuperFunc)
1907 dev->putSuperFunc(sb);
1909 yaffs_Deinitialise(dev);
1911 yaffs_GrossUnlock(dev);
1913 /* we assume this is protected by lock_kernel() in mount/umount */
1914 ylist_del(&dev->devList);
1916 if (dev->spareBuffer) {
1917 YFREE(dev->spareBuffer);
1918 dev->spareBuffer = NULL;
1925 static void yaffs_MTDPutSuper(struct super_block *sb)
1927 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1932 put_mtd_device(mtd);
1936 static void yaffs_MarkSuperBlockDirty(void *vsb)
1938 struct super_block *sb = (struct super_block *)vsb;
1940 T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
1947 int skip_checkpoint_read;
1948 int skip_checkpoint_write;
1951 int tags_ecc_overridden;
1952 int lazy_loading_enabled;
1953 int lazy_loading_overridden;
1954 int empty_lost_and_found;
1955 int empty_lost_and_found_overridden;
1958 #define MAX_OPT_LEN 30
1959 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
1961 char cur_opt[MAX_OPT_LEN + 1];
1965 /* Parse through the options which is a comma seperated list */
1967 while (options_str && *options_str && !error) {
1968 memset(cur_opt, 0, MAX_OPT_LEN + 1);
1971 while(*options_str == ',')
1974 while (*options_str && *options_str != ',') {
1975 if (p < MAX_OPT_LEN) {
1976 cur_opt[p] = *options_str;
1982 if (!strcmp(cur_opt, "inband-tags"))
1983 options->inband_tags = 1;
1984 else if (!strcmp(cur_opt, "tags-ecc-off")){
1985 options->tags_ecc_on = 0;
1986 options->tags_ecc_overridden=1;
1987 } else if (!strcmp(cur_opt, "tags-ecc-on")){
1988 options->tags_ecc_on = 1;
1989 options->tags_ecc_overridden = 1;
1990 } else if (!strcmp(cur_opt, "lazy-loading-off")){
1991 options->lazy_loading_enabled = 0;
1992 options->lazy_loading_overridden=1;
1993 } else if (!strcmp(cur_opt, "lazy-loading-on")){
1994 options->lazy_loading_enabled = 1;
1995 options->lazy_loading_overridden = 1;
1996 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
1997 options->empty_lost_and_found = 0;
1998 options->empty_lost_and_found_overridden=1;
1999 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2000 options->empty_lost_and_found = 1;
2001 options->empty_lost_and_found_overridden=1;
2002 } else if (!strcmp(cur_opt, "no-cache"))
2003 options->no_cache = 1;
2004 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2005 options->skip_checkpoint_read = 1;
2006 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2007 options->skip_checkpoint_write = 1;
2008 else if (!strcmp(cur_opt, "no-checkpoint")) {
2009 options->skip_checkpoint_read = 1;
2010 options->skip_checkpoint_write = 1;
2012 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2021 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2022 struct super_block *sb,
2023 void *data, int silent)
2026 struct inode *inode = NULL;
2027 struct dentry *root;
2028 yaffs_Device *dev = 0;
2029 char devname_buf[BDEVNAME_SIZE + 1];
2030 struct mtd_info *mtd;
2032 char *data_str = (char *)data;
2034 yaffs_options options;
2036 sb->s_magic = YAFFS_MAGIC;
2037 sb->s_op = &yaffs_super_ops;
2038 sb->s_flags |= MS_NOATIME;
2041 printk(KERN_INFO "yaffs: sb is NULL\n");
2042 else if (!sb->s_dev)
2043 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2044 else if (!yaffs_devname(sb, devname_buf))
2045 printk(KERN_INFO "yaffs: devname is NULL\n");
2047 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2049 yaffs_devname(sb, devname_buf));
2054 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2056 memset(&options, 0, sizeof(options));
2058 if (yaffs_parse_options(&options, data_str)) {
2059 /* Option parsing failed */
2064 sb->s_blocksize = PAGE_CACHE_SIZE;
2065 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2066 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2068 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2070 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2072 ("yaffs: Write verification disabled. All guarantees "
2073 "null and void\n"));
2076 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2078 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2079 yaffs_devname(sb, devname_buf)));
2081 /* Check it's an mtd device..... */
2082 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2083 return NULL; /* This isn't an mtd device */
2085 /* Get the device */
2086 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2088 T(YAFFS_TRACE_ALWAYS,
2089 ("yaffs: MTD device #%u doesn't appear to exist\n",
2093 /* Check it's NAND */
2094 if (mtd->type != MTD_NANDFLASH) {
2095 T(YAFFS_TRACE_ALWAYS,
2096 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
2100 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
2101 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
2102 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
2103 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
2104 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
2105 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
2106 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
2107 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2108 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
2109 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
2110 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2111 T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
2113 T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
2116 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2118 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2119 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
2123 /* Added NCB 26/5/2006 for completeness */
2124 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2125 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
2131 if (yaffsVersion == 2) {
2132 /* Check for version 2 style functions */
2134 !mtd->block_isbad ||
2135 !mtd->block_markbad ||
2138 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2139 !mtd->read_oob || !mtd->write_oob) {
2142 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2144 T(YAFFS_TRACE_ALWAYS,
2145 ("yaffs: MTD device does not support required "
2150 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2151 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2152 !options.inband_tags) {
2153 T(YAFFS_TRACE_ALWAYS,
2154 ("yaffs: MTD device does not have the "
2155 "right page sizes\n"));
2159 /* Check for V1 style functions */
2163 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2164 !mtd->read_oob || !mtd->write_oob) {
2167 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2169 T(YAFFS_TRACE_ALWAYS,
2170 ("yaffs: MTD device does not support required "
2175 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2176 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2177 T(YAFFS_TRACE_ALWAYS,
2178 ("yaffs: MTD device does not support have the "
2179 "right page sizes\n"));
2184 /* OK, so if we got here, we have an MTD that's NAND and looks
2185 * like it has the right capabilities
2186 * Set the yaffs_Device up for mtd
2189 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2190 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2192 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2195 /* Deep shit could not allocate device structure */
2196 T(YAFFS_TRACE_ALWAYS,
2197 ("yaffs_read_super: Failed trying to allocate "
2198 "yaffs_Device. \n"));
2202 memset(dev, 0, sizeof(yaffs_Device));
2203 dev->genericDevice = mtd;
2204 dev->name = mtd->name;
2206 /* Set up the memory size parameters.... */
2208 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2210 dev->startBlock = 0;
2211 dev->endBlock = nBlocks - 1;
2212 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2213 dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2214 dev->nReservedBlocks = 5;
2215 dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
2216 dev->inbandTags = options.inband_tags;
2218 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2219 dev->disableLazyLoad = 1;
2221 if(options.lazy_loading_overridden)
2222 dev->disableLazyLoad = !options.lazy_loading_enabled;
2224 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2227 if(options.tags_ecc_overridden)
2228 dev->noTagsECC = !options.tags_ecc_on;
2230 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2231 dev->emptyLostAndFound = 1;
2233 if(options.empty_lost_and_found_overridden)
2234 dev->emptyLostAndFound = options.empty_lost_and_found;
2236 /* ... and the functions. */
2237 if (yaffsVersion == 2) {
2238 dev->writeChunkWithTagsToNAND =
2239 nandmtd2_WriteChunkWithTagsToNAND;
2240 dev->readChunkWithTagsFromNAND =
2241 nandmtd2_ReadChunkWithTagsFromNAND;
2242 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2243 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
2244 dev->spareBuffer = YMALLOC(mtd->oobsize);
2246 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2247 dev->totalBytesPerChunk = mtd->writesize;
2248 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2250 dev->totalBytesPerChunk = mtd->oobblock;
2251 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2253 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2255 dev->startBlock = 0;
2256 dev->endBlock = nBlocks - 1;
2258 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2259 /* use the MTD interface in yaffs_mtdif1.c */
2260 dev->writeChunkWithTagsToNAND =
2261 nandmtd1_WriteChunkWithTagsToNAND;
2262 dev->readChunkWithTagsFromNAND =
2263 nandmtd1_ReadChunkWithTagsFromNAND;
2264 dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2265 dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
2267 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2268 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2272 /* ... and common functions */
2273 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2274 dev->initialiseNAND = nandmtd_InitialiseNAND;
2276 dev->putSuperFunc = yaffs_MTDPutSuper;
2278 dev->superBlock = (void *)sb;
2279 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2282 #ifndef CONFIG_YAFFS_DOES_ECC
2283 dev->useNANDECC = 1;
2286 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2287 dev->wideTnodesDisabled = 1;
2290 dev->skipCheckpointRead = options.skip_checkpoint_read;
2291 dev->skipCheckpointWrite = options.skip_checkpoint_write;
2293 /* we assume this is protected by lock_kernel() in mount/umount */
2294 ylist_add_tail(&dev->devList, &yaffs_dev_list);
2296 /* Directory search handling...*/
2297 YINIT_LIST_HEAD(&dev->searchContexts);
2298 dev->removeObjectCallback = yaffs_RemoveObjectCallback;
2300 init_MUTEX(&dev->grossLock);
2302 yaffs_GrossLock(dev);
2304 err = yaffs_GutsInitialise(dev);
2307 ("yaffs_read_super: guts initialised %s\n",
2308 (err == YAFFS_OK) ? "OK" : "FAILED"));
2310 /* Release lock before yaffs_get_inode() */
2311 yaffs_GrossUnlock(dev);
2313 /* Create root inode */
2314 if (err == YAFFS_OK)
2315 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2321 inode->i_op = &yaffs_dir_inode_operations;
2322 inode->i_fop = &yaffs_dir_operations;
2324 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
2326 root = d_alloc_root(inode);
2328 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
2335 sb->s_dirt = !dev->isCheckpointed;
2336 T(YAFFS_TRACE_ALWAYS,
2337 ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
2339 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
2344 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2345 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2348 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2351 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2352 static int yaffs_read_super(struct file_system_type *fs,
2353 int flags, const char *dev_name,
2354 void *data, struct vfsmount *mnt)
2357 return get_sb_bdev(fs, flags, dev_name, data,
2358 yaffs_internal_read_super_mtd, mnt);
2361 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2362 int flags, const char *dev_name,
2366 return get_sb_bdev(fs, flags, dev_name, data,
2367 yaffs_internal_read_super_mtd);
2371 static struct file_system_type yaffs_fs_type = {
2372 .owner = THIS_MODULE,
2374 .get_sb = yaffs_read_super,
2375 .kill_sb = kill_block_super,
2376 .fs_flags = FS_REQUIRES_DEV,
2379 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2382 return yaffs_internal_read_super(1, sb, data, silent);
2385 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2390 #ifdef CONFIG_YAFFS_YAFFS2
2392 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2393 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2396 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2399 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2400 static int yaffs2_read_super(struct file_system_type *fs,
2401 int flags, const char *dev_name, void *data,
2402 struct vfsmount *mnt)
2404 return get_sb_bdev(fs, flags, dev_name, data,
2405 yaffs2_internal_read_super_mtd, mnt);
2408 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2409 int flags, const char *dev_name,
2413 return get_sb_bdev(fs, flags, dev_name, data,
2414 yaffs2_internal_read_super_mtd);
2418 static struct file_system_type yaffs2_fs_type = {
2419 .owner = THIS_MODULE,
2421 .get_sb = yaffs2_read_super,
2422 .kill_sb = kill_block_super,
2423 .fs_flags = FS_REQUIRES_DEV,
2426 static struct super_block *yaffs2_read_super(struct super_block *sb,
2427 void *data, int silent)
2429 return yaffs_internal_read_super(2, sb, data, silent);
2432 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2436 #endif /* CONFIG_YAFFS_YAFFS2 */
2438 static struct proc_dir_entry *my_proc_entry;
2440 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
2442 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
2443 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
2444 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
2445 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2446 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2447 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2448 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2449 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
2450 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2451 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2452 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2453 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2454 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2455 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2456 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2457 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2458 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2459 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2460 buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2461 buf += sprintf(buf, "passiveGCs......... %d\n",
2462 dev->passiveGarbageCollections);
2463 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2464 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
2465 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2466 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2467 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2468 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2469 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2470 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2471 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2472 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2474 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2480 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
2482 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
2483 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->noTagsECC);
2484 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
2485 buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
2486 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->emptyLostAndFound);
2487 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->disableLazyLoad);
2492 static int yaffs_proc_read(char *page,
2494 off_t offset, int count, int *eof, void *data)
2496 struct ylist_head *item;
2501 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2502 * We use 'offset' (*ppos) to indicate where we are in devList.
2503 * This also assumes the user has posted a read buffer large
2504 * enough to hold the complete output; but that's life in /proc.
2509 /* Print header first */
2511 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2512 "\n%s\n%s\n", yaffs_fs_c_version,
2513 yaffs_guts_c_version);
2515 buf += sprintf(buf,"\n");
2519 /* hold lock_kernel while traversing yaffs_dev_list */
2522 /* Locate and print the Nth entry. Order N-squared but N is small. */
2523 ylist_for_each(item, &yaffs_dev_list) {
2524 yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
2525 if (n < (step & ~1)) {
2530 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
2531 buf = yaffs_dump_dev_part0(buf, dev);
2533 buf = yaffs_dump_dev_part1(buf, dev);
2540 return buf - page < count ? buf - page : count;
2544 * Set the verbosity of the warnings and error messages.
2546 * Note that the names can only be a..z or _ with the current code.
2551 unsigned mask_bitfield;
2553 {"allocate", YAFFS_TRACE_ALLOCATE},
2554 {"always", YAFFS_TRACE_ALWAYS},
2555 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2556 {"buffers", YAFFS_TRACE_BUFFERS},
2557 {"bug", YAFFS_TRACE_BUG},
2558 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2559 {"deletion", YAFFS_TRACE_DELETION},
2560 {"erase", YAFFS_TRACE_ERASE},
2561 {"error", YAFFS_TRACE_ERROR},
2562 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2563 {"gc", YAFFS_TRACE_GC},
2564 {"mtd", YAFFS_TRACE_MTD},
2565 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2566 {"os", YAFFS_TRACE_OS},
2567 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2568 {"scan", YAFFS_TRACE_SCAN},
2569 {"tracing", YAFFS_TRACE_TRACING},
2571 {"verify", YAFFS_TRACE_VERIFY},
2572 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2573 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2574 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2576 {"write", YAFFS_TRACE_WRITE},
2577 {"all", 0xffffffff},
2582 #define MAX_MASK_NAME_LENGTH 40
2583 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2584 unsigned long count, void *data)
2586 unsigned rg = 0, mask_bitfield;
2590 char substring[MAX_MASK_NAME_LENGTH + 1];
2596 rg = yaffs_traceMask;
2598 while (!done && (pos < count)) {
2600 while ((pos < count) && isspace(buf[pos]))
2617 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2619 if (end > buf + pos) {
2620 mask_name = "numeral";
2621 len = end - (buf + pos);
2625 for (x = buf + pos, i = 0;
2626 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2627 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2629 substring[i] = '\0';
2631 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2632 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
2633 mask_name = mask_flags[i].mask_name;
2634 mask_bitfield = mask_flags[i].mask_bitfield;
2641 if (mask_name != NULL) {
2645 rg &= ~mask_bitfield;
2648 rg |= mask_bitfield;
2654 rg |= mask_bitfield;
2660 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2662 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
2664 if (rg & YAFFS_TRACE_ALWAYS) {
2665 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2667 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2668 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
2676 static int yaffs_proc_write(struct file *file, const char *buf,
2677 unsigned long count, void *data)
2679 return yaffs_proc_write_trace_options(file, buf, count, data);
2682 /* Stuff to handle installation of file systems */
2683 struct file_system_to_install {
2684 struct file_system_type *fst;
2688 static struct file_system_to_install fs_to_install[] = {
2689 {&yaffs_fs_type, 0},
2690 {&yaffs2_fs_type, 0},
2694 static int __init init_yaffs_fs(void)
2697 struct file_system_to_install *fsinst;
2699 T(YAFFS_TRACE_ALWAYS,
2700 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2702 /* Install the proc_fs entry */
2703 my_proc_entry = create_proc_entry("yaffs",
2707 if (my_proc_entry) {
2708 my_proc_entry->write_proc = yaffs_proc_write;
2709 my_proc_entry->read_proc = yaffs_proc_read;
2710 my_proc_entry->data = NULL;
2714 /* Now add the file system entries */
2716 fsinst = fs_to_install;
2718 while (fsinst->fst && !error) {
2719 error = register_filesystem(fsinst->fst);
2721 fsinst->installed = 1;
2725 /* Any errors? uninstall */
2727 fsinst = fs_to_install;
2729 while (fsinst->fst) {
2730 if (fsinst->installed) {
2731 unregister_filesystem(fsinst->fst);
2732 fsinst->installed = 0;
2741 static void __exit exit_yaffs_fs(void)
2744 struct file_system_to_install *fsinst;
2746 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2749 remove_proc_entry("yaffs", YPROC_ROOT);
2751 fsinst = fs_to_install;
2753 while (fsinst->fst) {
2754 if (fsinst->installed) {
2755 unregister_filesystem(fsinst->fst);
2756 fsinst->installed = 0;
2762 module_init(init_yaffs_fs)
2763 module_exit(exit_yaffs_fs)
2765 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2766 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2767 MODULE_LICENSE("GPL");