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.90 2009-12-23 03:14:17 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_guts.h"
119 #include <linux/mtd/mtd.h>
120 #include "yaffs_mtdif.h"
121 #include "yaffs_mtdif1.h"
122 #include "yaffs_mtdif2.h"
124 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
125 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
126 unsigned int yaffs_auto_checkpoint = 1;
128 /* Module Parameters */
129 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
130 module_param(yaffs_traceMask, uint, 0644);
131 module_param(yaffs_wr_attempts, uint, 0644);
132 module_param(yaffs_auto_checkpoint, uint, 0644);
134 MODULE_PARM(yaffs_traceMask, "i");
135 MODULE_PARM(yaffs_wr_attempts, "i");
136 MODULE_PARM(yaffs_auto_checkpoint, "i");
139 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
140 /* use iget and read_inode */
141 #define Y_IGET(sb, inum) iget((sb), (inum))
142 static void yaffs_read_inode(struct inode *inode);
145 /* Call local equivalent */
146 #define YAFFS_USE_OWN_IGET
147 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
149 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
152 /*#define T(x) printk x */
154 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
155 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
157 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
160 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
161 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
163 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
164 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
166 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
170 #define update_dir_time(dir) do {\
171 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
174 static void yaffs_put_super(struct super_block *sb);
176 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
178 static ssize_t yaffs_hold_space(struct file *f);
179 static void yaffs_release_space(struct file *f);
181 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
182 static int yaffs_file_flush(struct file *file, fl_owner_t id);
184 static int yaffs_file_flush(struct file *file);
187 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
190 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
192 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
193 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
194 struct nameidata *n);
195 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
196 struct nameidata *n);
198 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
199 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
201 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
202 struct dentry *dentry);
203 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
204 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
205 const char *symname);
206 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
208 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
209 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
212 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
215 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
216 struct inode *new_dir, struct dentry *new_dentry);
217 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
219 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
220 static int yaffs_sync_fs(struct super_block *sb, int wait);
221 static void yaffs_write_super(struct super_block *sb);
223 static int yaffs_sync_fs(struct super_block *sb);
224 static int yaffs_write_super(struct super_block *sb);
227 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
228 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
229 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
230 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
232 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
235 #ifdef YAFFS_HAS_PUT_INODE
236 static void yaffs_put_inode(struct inode *inode);
239 static void yaffs_delete_inode(struct inode *);
240 static void yaffs_clear_inode(struct inode *);
242 static int yaffs_readpage(struct file *file, struct page *page);
243 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
244 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
246 static int yaffs_writepage(struct page *page);
250 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
251 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
252 loff_t pos, unsigned len, unsigned flags,
253 struct page **pagep, void **fsdata);
254 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
255 loff_t pos, unsigned len, unsigned copied,
256 struct page *pg, void *fsdadata);
258 static int yaffs_prepare_write(struct file *f, struct page *pg,
259 unsigned offset, unsigned to);
260 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
265 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
267 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
268 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
270 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
273 static struct address_space_operations yaffs_file_address_operations = {
274 .readpage = yaffs_readpage,
275 .writepage = yaffs_writepage,
276 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
277 .write_begin = yaffs_write_begin,
278 .write_end = yaffs_write_end,
280 .prepare_write = yaffs_prepare_write,
281 .commit_write = yaffs_commit_write,
285 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
286 static const struct file_operations yaffs_file_operations = {
287 .read = do_sync_read,
288 .write = do_sync_write,
289 .aio_read = generic_file_aio_read,
290 .aio_write = generic_file_aio_write,
291 .mmap = generic_file_mmap,
292 .flush = yaffs_file_flush,
293 .fsync = yaffs_sync_object,
294 .splice_read = generic_file_splice_read,
295 .splice_write = generic_file_splice_write,
296 .llseek = generic_file_llseek,
299 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
301 static const struct file_operations yaffs_file_operations = {
302 .read = do_sync_read,
303 .write = do_sync_write,
304 .aio_read = generic_file_aio_read,
305 .aio_write = generic_file_aio_write,
306 .mmap = generic_file_mmap,
307 .flush = yaffs_file_flush,
308 .fsync = yaffs_sync_object,
309 .sendfile = generic_file_sendfile,
314 static const struct file_operations yaffs_file_operations = {
315 .read = generic_file_read,
316 .write = generic_file_write,
317 .mmap = generic_file_mmap,
318 .flush = yaffs_file_flush,
319 .fsync = yaffs_sync_object,
320 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
321 .sendfile = generic_file_sendfile,
326 static const struct inode_operations yaffs_file_inode_operations = {
327 .setattr = yaffs_setattr,
330 static const struct inode_operations yaffs_symlink_inode_operations = {
331 .readlink = yaffs_readlink,
332 .follow_link = yaffs_follow_link,
333 .setattr = yaffs_setattr,
336 static const struct inode_operations yaffs_dir_inode_operations = {
337 .create = yaffs_create,
338 .lookup = yaffs_lookup,
340 .unlink = yaffs_unlink,
341 .symlink = yaffs_symlink,
342 .mkdir = yaffs_mkdir,
343 .rmdir = yaffs_unlink,
344 .mknod = yaffs_mknod,
345 .rename = yaffs_rename,
346 .setattr = yaffs_setattr,
349 static const struct file_operations yaffs_dir_operations = {
350 .read = generic_read_dir,
351 .readdir = yaffs_readdir,
352 .fsync = yaffs_sync_object,
355 static const struct super_operations yaffs_super_ops = {
356 .statfs = yaffs_statfs,
358 #ifndef YAFFS_USE_OWN_IGET
359 .read_inode = yaffs_read_inode,
361 #ifdef YAFFS_HAS_PUT_INODE
362 .put_inode = yaffs_put_inode,
364 .put_super = yaffs_put_super,
365 .delete_inode = yaffs_delete_inode,
366 .clear_inode = yaffs_clear_inode,
367 .sync_fs = yaffs_sync_fs,
368 .write_super = yaffs_write_super,
371 static void yaffs_GrossLock(yaffs_Device *dev)
373 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
374 down(&dev->grossLock);
375 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
378 static void yaffs_GrossUnlock(yaffs_Device *dev)
380 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
385 /*-----------------------------------------------------------------*/
386 /* Directory search context allows us to unlock access to yaffs during
387 * filldir without causing problems with the directory being modified.
388 * This is similar to the tried and tested mechanism used in yaffs direct.
390 * A search context iterates along a doubly linked list of siblings in the
391 * directory. If the iterating object is deleted then this would corrupt
392 * the list iteration, likely causing a crash. The search context avoids
393 * this by using the removeObjectCallback to move the search context to the
394 * next object before the object is deleted.
396 * Many readdirs (and thus seach conexts) may be alive simulateously so
397 * each yaffs_Device has a list of these.
399 * A seach context lives for the duration of a readdir.
401 * All these functions must be called while yaffs is locked.
404 struct yaffs_SearchContext {
406 yaffs_Object *dirObj;
407 yaffs_Object *nextReturn;
408 struct ylist_head others;
412 * yaffs_NewSearch() creates a new search context, initialises it and
413 * adds it to the device's search context list.
415 * Called at start of readdir.
417 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
419 yaffs_Device *dev = dir->myDev;
420 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
424 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
425 sc->nextReturn = NULL;
427 sc->nextReturn = ylist_entry(
428 dir->variant.directoryVariant.children.next,
429 yaffs_Object,siblings);
430 YINIT_LIST_HEAD(&sc->others);
431 ylist_add(&sc->others,&dev->searchContexts);
437 * yaffs_EndSearch() disposes of a search context and cleans up.
439 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
442 ylist_del(&sc->others);
448 * yaffs_SearchAdvance() moves a search context to the next object.
449 * Called when the search iterates or when an object removal causes
450 * the search context to be moved to the next object.
452 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
457 if( sc->nextReturn == NULL ||
458 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
459 sc->nextReturn = NULL;
461 struct ylist_head *next = sc->nextReturn->siblings.next;
463 if( next == &sc->dirObj->variant.directoryVariant.children)
464 sc->nextReturn = NULL; /* end of list */
466 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
471 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
472 * We check open search contexts and advance any which are currently
473 * on the object being iterated.
475 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
478 struct ylist_head *i;
479 struct yaffs_SearchContext *sc;
480 struct ylist_head *search_contexts = &obj->myDev->searchContexts;
483 /* Iterate through the directory search contexts.
484 * If any are currently on the object being removed, then advance
485 * the search context to the next object to prevent a hanging pointer.
487 ylist_for_each(i, search_contexts) {
489 sc = ylist_entry(i, struct yaffs_SearchContext,others);
490 if(sc->nextReturn == obj)
491 yaffs_SearchAdvance(sc);
498 /*-----------------------------------------------------------------*/
500 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
503 unsigned char *alias;
506 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
508 yaffs_GrossLock(dev);
510 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
512 yaffs_GrossUnlock(dev);
517 ret = vfs_readlink(dentry, buffer, buflen, alias);
522 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
523 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
525 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
528 unsigned char *alias;
530 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
532 yaffs_GrossLock(dev);
534 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
536 yaffs_GrossUnlock(dev);
543 ret = vfs_follow_link(nd, alias);
546 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
553 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
557 * Lookup is used to find objects in the fs
559 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
561 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
564 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
568 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
570 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
572 yaffs_GrossLock(dev);
575 ("yaffs_lookup for %d:%s\n",
576 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
578 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
579 dentry->d_name.name);
581 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
583 /* Can't hold gross lock when calling yaffs_get_inode() */
584 yaffs_GrossUnlock(dev);
588 ("yaffs_lookup found %d\n", obj->objectId));
590 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
594 ("yaffs_loookup dentry \n"));
595 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
596 * d_add even if NULL inode */
598 /*dget(dentry); // try to solve directory bug */
599 d_add(dentry, inode);
607 T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
611 /* added NCB for 2.5/6 compatability - forces add even if inode is
612 * NULL which creates dentry hash */
613 d_add(dentry, inode);
619 #ifdef YAFFS_HAS_PUT_INODE
621 /* For now put inode is just for debugging
622 * Put inode is called when the inode **structure** is put.
624 static void yaffs_put_inode(struct inode *inode)
627 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
628 atomic_read(&inode->i_count)));
633 /* clear is called to tell the fs to release any per-inode data it holds */
634 static void yaffs_clear_inode(struct inode *inode)
639 obj = yaffs_InodeToObject(inode);
642 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
643 atomic_read(&inode->i_count),
644 obj ? "object exists" : "null object"));
648 yaffs_GrossLock(dev);
650 /* Clear the association between the inode and
654 yaffs_InodeToObjectLV(inode) = NULL;
656 /* If the object freeing was deferred, then the real
658 * This should fix the inode inconsistency problem.
661 yaffs_HandleDeferedFree(obj);
663 yaffs_GrossUnlock(dev);
668 /* delete is called when the link count is zero and the inode
669 * is put (ie. nobody wants to know about it anymore, time to
671 * NB Must call clear_inode()
673 static void yaffs_delete_inode(struct inode *inode)
675 yaffs_Object *obj = yaffs_InodeToObject(inode);
679 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
680 atomic_read(&inode->i_count),
681 obj ? "object exists" : "null object"));
685 yaffs_GrossLock(dev);
686 yaffs_DeleteObject(obj);
687 yaffs_GrossUnlock(dev);
689 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
690 truncate_inode_pages(&inode->i_data, 0);
695 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
696 static int yaffs_file_flush(struct file *file, fl_owner_t id)
698 static int yaffs_file_flush(struct file *file)
701 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
703 yaffs_Device *dev = obj->myDev;
706 ("yaffs_file_flush object %d (%s)\n", obj->objectId,
707 obj->dirty ? "dirty" : "clean"));
709 yaffs_GrossLock(dev);
711 yaffs_FlushFile(obj, 1, 0);
713 yaffs_GrossUnlock(dev);
718 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
720 /* Lifted from jffs2 */
723 unsigned char *pg_buf;
728 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock at %08x, size %08x\n",
729 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
730 (unsigned)PAGE_CACHE_SIZE));
732 obj = yaffs_DentryToObject(f->f_dentry);
736 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
737 BUG_ON(!PageLocked(pg));
744 /* FIXME: Can kmap fail? */
746 yaffs_GrossLock(dev);
748 ret = yaffs_ReadDataFromFile(obj, pg_buf,
749 pg->index << PAGE_CACHE_SHIFT,
752 yaffs_GrossUnlock(dev);
758 ClearPageUptodate(pg);
765 flush_dcache_page(pg);
768 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock done\n"));
772 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
774 int ret = yaffs_readpage_nolock(f, pg);
779 static int yaffs_readpage(struct file *f, struct page *pg)
783 T(YAFFS_TRACE_OS, ("yaffs_readpage\n"));
784 ret=yaffs_readpage_unlock(f, pg);
785 T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
789 /* writepage inspired by/stolen from smbfs */
791 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
792 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
794 static int yaffs_writepage(struct page *page)
797 struct address_space *mapping = page->mapping;
799 unsigned long end_index;
808 inode = mapping->host;
811 i_size = i_size_read(inode);
813 end_index = i_size >> PAGE_CACHE_SHIFT;
815 if(page->index < end_index)
816 nBytes = PAGE_CACHE_SIZE;
818 nBytes = i_size & (PAGE_CACHE_SIZE -1);
820 if (page->index > end_index || !nBytes) {
822 ("yaffs_writepage at %08x, inode size = %08x!!!\n",
823 (unsigned)(page->index << PAGE_CACHE_SHIFT),
824 (unsigned)inode->i_size));
826 (" -> don't care!!\n"));
828 zero_user_segment(page,0,PAGE_CACHE_SIZE);
829 set_page_writeback(page);
831 end_page_writeback(page);
836 if(nBytes != PAGE_CACHE_SIZE)
837 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
843 obj = yaffs_InodeToObject(inode);
844 yaffs_GrossLock(obj->myDev);
847 ("yaffs_writepage at %08x, size %08x\n",
848 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
850 ("writepag0: obj = %05x, ino = %05x\n",
851 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
853 nWritten = yaffs_WriteDataToFile(obj, buffer,
854 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
857 ("writepag1: obj = %05x, ino = %05x\n",
858 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
860 yaffs_GrossUnlock(obj->myDev);
863 set_page_writeback(page);
865 end_page_writeback(page);
868 return (nWritten == nBytes) ? 0 : -ENOSPC;
872 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
873 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
874 loff_t pos, unsigned len, unsigned flags,
875 struct page **pagep, void **fsdata)
877 struct page *pg = NULL;
878 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
884 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
885 pg = grab_cache_page_write_begin(mapping, index, flags);
887 pg = __grab_cache_page(mapping, index);
895 T(YAFFS_TRACE_OS, ("start yaffs_write_begin index %d(%x) uptodate %d\n",(int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
898 space_held = yaffs_hold_space(filp);
905 /* Update page if required */
907 if (!Page_Uptodate(pg))
908 ret = yaffs_readpage_nolock(filp, pg);
913 /* Happy path return */
914 T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
919 T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
921 yaffs_release_space(filp);
924 page_cache_release(pg);
931 static int yaffs_prepare_write(struct file *f, struct page *pg,
932 unsigned offset, unsigned to)
934 T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
936 if (!Page_Uptodate(pg))
937 return yaffs_readpage_nolock(f, pg);
942 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
943 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
944 loff_t pos, unsigned len, unsigned copied,
945 struct page *pg, void *fsdadata)
949 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
952 addr = kva + offset_into_page;
955 ("yaffs_write_end addr %x pos %x nBytes %d\n",
959 ret = yaffs_file_write(filp, addr, copied, &pos);
963 ("yaffs_write_end not same size ret %d copied %d\n",
972 yaffs_release_space(filp);
974 page_cache_release(pg);
979 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
984 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
985 int nBytes = to - offset;
994 saddr = (unsigned) addr;
997 ("yaffs_commit_write addr %x pos %x nBytes %d\n",
998 saddr, spos, nBytes));
1000 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1002 if (nWritten != nBytes) {
1004 ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
1014 ("yaffs_commit_write returning %d\n",
1015 nWritten == nBytes ? 0 : nWritten));
1017 return nWritten == nBytes ? 0 : nWritten;
1022 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1027 /* Check mode against the variant type and attempt to repair if broken. */
1028 __u32 mode = obj->yst_mode;
1029 switch (obj->variantType) {
1030 case YAFFS_OBJECT_TYPE_FILE:
1031 if (!S_ISREG(mode)) {
1032 obj->yst_mode &= ~S_IFMT;
1033 obj->yst_mode |= S_IFREG;
1037 case YAFFS_OBJECT_TYPE_SYMLINK:
1038 if (!S_ISLNK(mode)) {
1039 obj->yst_mode &= ~S_IFMT;
1040 obj->yst_mode |= S_IFLNK;
1044 case YAFFS_OBJECT_TYPE_DIRECTORY:
1045 if (!S_ISDIR(mode)) {
1046 obj->yst_mode &= ~S_IFMT;
1047 obj->yst_mode |= S_IFDIR;
1051 case YAFFS_OBJECT_TYPE_UNKNOWN:
1052 case YAFFS_OBJECT_TYPE_HARDLINK:
1053 case YAFFS_OBJECT_TYPE_SPECIAL:
1059 inode->i_flags |= S_NOATIME;
1061 inode->i_ino = obj->objectId;
1062 inode->i_mode = obj->yst_mode;
1063 inode->i_uid = obj->yst_uid;
1064 inode->i_gid = obj->yst_gid;
1065 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1066 inode->i_blksize = inode->i_sb->s_blocksize;
1068 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1070 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1071 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1072 inode->i_atime.tv_nsec = 0;
1073 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1074 inode->i_mtime.tv_nsec = 0;
1075 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1076 inode->i_ctime.tv_nsec = 0;
1078 inode->i_rdev = obj->yst_rdev;
1079 inode->i_atime = obj->yst_atime;
1080 inode->i_mtime = obj->yst_mtime;
1081 inode->i_ctime = obj->yst_ctime;
1083 inode->i_size = yaffs_GetObjectFileLength(obj);
1084 inode->i_blocks = (inode->i_size + 511) >> 9;
1086 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1089 ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
1090 inode->i_mode, inode->i_uid, inode->i_gid,
1091 (int)inode->i_size, atomic_read(&inode->i_count)));
1093 switch (obj->yst_mode & S_IFMT) {
1094 default: /* fifo, device or socket */
1095 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1096 init_special_inode(inode, obj->yst_mode,
1097 old_decode_dev(obj->yst_rdev));
1099 init_special_inode(inode, obj->yst_mode,
1100 (dev_t) (obj->yst_rdev));
1103 case S_IFREG: /* file */
1104 inode->i_op = &yaffs_file_inode_operations;
1105 inode->i_fop = &yaffs_file_operations;
1106 inode->i_mapping->a_ops =
1107 &yaffs_file_address_operations;
1109 case S_IFDIR: /* directory */
1110 inode->i_op = &yaffs_dir_inode_operations;
1111 inode->i_fop = &yaffs_dir_operations;
1113 case S_IFLNK: /* symlink */
1114 inode->i_op = &yaffs_symlink_inode_operations;
1118 yaffs_InodeToObjectLV(inode) = obj;
1120 obj->myInode = inode;
1124 ("yaffs_FileInode invalid parameters\n"));
1129 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1132 struct inode *inode;
1136 ("yaffs_get_inode for NULL super_block!!\n"));
1143 ("yaffs_get_inode for NULL object!!\n"));
1149 ("yaffs_get_inode for object %d\n", obj->objectId));
1151 inode = Y_IGET(sb, obj->objectId);
1155 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1156 /* iget also increments the inode's i_count */
1157 /* NB You can't be holding grossLock or deadlock will happen! */
1162 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1167 struct inode *inode;
1170 obj = yaffs_DentryToObject(f->f_dentry);
1174 yaffs_GrossLock(dev);
1176 inode = f->f_dentry->d_inode;
1178 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1179 ipos = inode->i_size;
1185 ("yaffs_file_write: hey obj is null!\n"));
1188 ("yaffs_file_write about to write writing %u(%x) bytes"
1189 "to object %d at %d(%x)\n",
1190 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1192 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1195 ("yaffs_file_write: %d(%x) bytes written\n",
1196 (unsigned )n,(unsigned)n));
1201 if (ipos > inode->i_size) {
1202 inode->i_size = ipos;
1203 inode->i_blocks = (ipos + 511) >> 9;
1206 ("yaffs_file_write size updated to %d bytes, "
1208 ipos, (int)(inode->i_blocks)));
1212 yaffs_GrossUnlock(dev);
1213 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1216 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1217 /* For now we just assume few parallel writes and check against a small number. */
1218 /* Todo: need to do this with a counter to handle parallel reads better */
1220 static ssize_t yaffs_hold_space(struct file *f)
1228 obj = yaffs_DentryToObject(f->f_dentry);
1232 yaffs_GrossLock(dev);
1234 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1236 yaffs_GrossUnlock(dev);
1238 return (nFreeChunks > 20) ? 1 : 0;
1241 static void yaffs_release_space(struct file *f)
1247 obj = yaffs_DentryToObject(f->f_dentry);
1251 yaffs_GrossLock(dev);
1254 yaffs_GrossUnlock(dev);
1257 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1261 struct yaffs_SearchContext *sc;
1262 struct inode *inode = f->f_dentry->d_inode;
1263 unsigned long offset, curoffs;
1267 char name[YAFFS_MAX_NAME_LENGTH + 1];
1269 obj = yaffs_DentryToObject(f->f_dentry);
1272 yaffs_GrossLock(dev);
1276 sc = yaffs_NewSearch(obj);
1282 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
1286 ("yaffs_readdir: entry . ino %d \n",
1287 (int)inode->i_ino));
1288 yaffs_GrossUnlock(dev);
1289 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
1291 yaffs_GrossLock(dev);
1297 ("yaffs_readdir: entry .. ino %d \n",
1298 (int)f->f_dentry->d_parent->d_inode->i_ino));
1299 yaffs_GrossUnlock(dev);
1300 if (filldir(dirent, "..", 2, offset,
1301 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
1303 yaffs_GrossLock(dev);
1310 /* If the directory has changed since the open or last call to
1311 readdir, rewind to after the 2 canned entries. */
1313 if (f->f_version != inode->i_version) {
1316 f->f_version = inode->i_version;
1319 while(sc->nextReturn){
1322 if (curoffs >= offset) {
1323 int this_inode = yaffs_GetObjectInode(l);
1324 int this_type = yaffs_GetObjectType(l);
1326 yaffs_GetObjectName(l, name,
1327 YAFFS_MAX_NAME_LENGTH + 1);
1329 ("yaffs_readdir: %s inode %d\n", name,
1330 yaffs_GetObjectInode(l)));
1332 yaffs_GrossUnlock(dev);
1342 yaffs_GrossLock(dev);
1347 yaffs_SearchAdvance(sc);
1351 yaffs_GrossUnlock(dev);
1353 yaffs_EndSearch(sc);
1359 * File creation. Allocate an inode, and we're done..
1362 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1365 #define YCRED(x) (x->cred)
1368 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1369 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1372 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1376 struct inode *inode;
1378 yaffs_Object *obj = NULL;
1381 yaffs_Object *parent = yaffs_InodeToObject(dir);
1383 int error = -ENOSPC;
1384 uid_t uid = YCRED(current)->fsuid;
1385 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1387 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1392 ("yaffs_mknod: parent object %d type %d\n",
1393 parent->objectId, parent->variantType));
1396 ("yaffs_mknod: could not get parent object\n"));
1400 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1402 dentry->d_name.name, mode, rdev));
1404 dev = parent->myDev;
1406 yaffs_GrossLock(dev);
1408 switch (mode & S_IFMT) {
1410 /* Special (socket, fifo, device...) */
1411 T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
1412 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1413 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1414 gid, old_encode_dev(rdev));
1416 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1420 case S_IFREG: /* file */
1421 T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
1422 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1425 case S_IFDIR: /* directory */
1427 ("yaffs_mknod: making directory\n"));
1428 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1431 case S_IFLNK: /* symlink */
1432 T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
1433 obj = NULL; /* Do we ever get here? */
1437 /* Can not call yaffs_get_inode() with gross lock held */
1438 yaffs_GrossUnlock(dev);
1441 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1442 d_instantiate(dentry, inode);
1443 update_dir_time(dir);
1445 ("yaffs_mknod created object %d count = %d\n",
1446 obj->objectId, atomic_read(&inode->i_count)));
1450 ("yaffs_mknod failed making object\n"));
1457 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1460 T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
1461 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1465 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1466 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1467 struct nameidata *n)
1469 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1472 T(YAFFS_TRACE_OS, ("yaffs_create\n"));
1473 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1476 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1483 ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1484 dentry->d_name.name));
1486 dev = yaffs_InodeToObject(dir)->myDev;
1488 yaffs_GrossLock(dev);
1490 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1492 if (retVal == YAFFS_OK) {
1493 dentry->d_inode->i_nlink--;
1495 yaffs_GrossUnlock(dev);
1496 mark_inode_dirty(dentry->d_inode);
1497 update_dir_time(dir);
1500 yaffs_GrossUnlock(dev);
1507 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1508 struct dentry *dentry)
1510 struct inode *inode = old_dentry->d_inode;
1511 yaffs_Object *obj = NULL;
1512 yaffs_Object *link = NULL;
1515 T(YAFFS_TRACE_OS, ("yaffs_link\n"));
1517 obj = yaffs_InodeToObject(inode);
1520 yaffs_GrossLock(dev);
1522 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1523 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1527 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1528 d_instantiate(dentry, old_dentry->d_inode);
1529 atomic_inc(&old_dentry->d_inode->i_count);
1531 ("yaffs_link link count %d i_count %d\n",
1532 old_dentry->d_inode->i_nlink,
1533 atomic_read(&old_dentry->d_inode->i_count)));
1536 yaffs_GrossUnlock(dev);
1539 update_dir_time(dir);
1546 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1547 const char *symname)
1551 uid_t uid = YCRED(current)->fsuid;
1552 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1554 T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
1556 dev = yaffs_InodeToObject(dir)->myDev;
1557 yaffs_GrossLock(dev);
1558 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1559 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1560 yaffs_GrossUnlock(dev);
1563 struct inode *inode;
1565 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1566 d_instantiate(dentry, inode);
1567 update_dir_time(dir);
1568 T(YAFFS_TRACE_OS, ("symlink created OK\n"));
1571 T(YAFFS_TRACE_OS, ("symlink not created\n"));
1577 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1584 obj = yaffs_DentryToObject(dentry);
1588 T(YAFFS_TRACE_OS, ("yaffs_sync_object\n"));
1589 yaffs_GrossLock(dev);
1590 yaffs_FlushFile(obj, 1, datasync);
1591 yaffs_GrossUnlock(dev);
1596 * The VFS layer already does all the dentry stuff for rename.
1598 * NB: POSIX says you can rename an object over an old object of the same name
1600 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1601 struct inode *new_dir, struct dentry *new_dentry)
1604 int retVal = YAFFS_FAIL;
1605 yaffs_Object *target;
1607 T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
1608 dev = yaffs_InodeToObject(old_dir)->myDev;
1610 yaffs_GrossLock(dev);
1612 /* Check if the target is an existing directory that is not empty. */
1613 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1614 new_dentry->d_name.name);
1618 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1619 !ylist_empty(&target->variant.directoryVariant.children)) {
1621 T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
1623 retVal = YAFFS_FAIL;
1625 /* Now does unlinking internally using shadowing mechanism */
1626 T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
1628 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1629 old_dentry->d_name.name,
1630 yaffs_InodeToObject(new_dir),
1631 new_dentry->d_name.name);
1633 yaffs_GrossUnlock(dev);
1635 if (retVal == YAFFS_OK) {
1637 new_dentry->d_inode->i_nlink--;
1638 mark_inode_dirty(new_dentry->d_inode);
1641 update_dir_time(old_dir);
1642 if(old_dir != new_dir)
1643 update_dir_time(new_dir);
1650 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1652 struct inode *inode = dentry->d_inode;
1657 ("yaffs_setattr of object %d\n",
1658 yaffs_InodeToObject(inode)->objectId));
1660 error = inode_change_ok(inode, attr);
1664 error = inode_setattr(inode, attr);
1665 T(YAFFS_TRACE_OS,("inode_setattr called\n"));
1666 if (attr->ia_valid & ATTR_SIZE)
1667 truncate_inode_pages(&inode->i_data,attr->ia_size);
1669 dev = yaffs_InodeToObject(inode)->myDev;
1670 if (attr->ia_valid & ATTR_SIZE){
1671 T(YAFFS_TRACE_OS,("resize to %d(%x)\n",(int)(attr->ia_size),(int)(attr->ia_size)));
1673 yaffs_GrossLock(dev);
1674 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1675 if(result == YAFFS_OK) {
1680 yaffs_GrossUnlock(dev);
1684 ("yaffs_setattr done\n"));
1689 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1690 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1692 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1693 struct super_block *sb = dentry->d_sb;
1694 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1695 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1697 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1699 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1701 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1704 T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
1706 yaffs_GrossLock(dev);
1708 buf->f_type = YAFFS_MAGIC;
1709 buf->f_bsize = sb->s_blocksize;
1710 buf->f_namelen = 255;
1712 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1713 /* Do this if chunk size is not a power of 2 */
1715 uint64_t bytesInDev;
1718 bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock + 1))) *
1719 ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
1721 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1722 buf->f_blocks = bytesInDev;
1724 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1725 ((uint64_t)(dev->nDataBytesPerChunk));
1727 do_div(bytesFree, sb->s_blocksize);
1729 buf->f_bfree = bytesFree;
1731 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1734 (dev->endBlock - dev->startBlock + 1) *
1735 dev->nChunksPerBlock /
1736 (sb->s_blocksize / dev->nDataBytesPerChunk);
1738 yaffs_GetNumberOfFreeChunks(dev) /
1739 (sb->s_blocksize / dev->nDataBytesPerChunk);
1742 (dev->endBlock - dev->startBlock + 1) *
1743 dev->nChunksPerBlock *
1744 (dev->nDataBytesPerChunk / sb->s_blocksize);
1747 yaffs_GetNumberOfFreeChunks(dev) *
1748 (dev->nDataBytesPerChunk / sb->s_blocksize);
1753 buf->f_bavail = buf->f_bfree;
1755 yaffs_GrossUnlock(dev);
1761 static void yaffs_flush_sb_inodes(struct super_block *sb)
1766 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1767 obj = yaffs_InodeToObject(iptr);
1769 T(YAFFS_TRACE_OS, ("flushing obj %d\n",obj->objectId));
1770 yaffs_FlushFile(obj,1,0);
1775 static int yaffs_do_sync_fs(struct super_block *sb)
1778 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1779 T(YAFFS_TRACE_OS, ("yaffs_do_sync_fs\n"));
1782 yaffs_GrossLock(dev);
1785 yaffs_FlushEntireDeviceCache(dev);
1786 yaffs_flush_sb_inodes(sb);
1787 yaffs_CheckpointSave(dev);
1790 yaffs_GrossUnlock(dev);
1798 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1799 static void yaffs_write_super(struct super_block *sb)
1801 static int yaffs_write_super(struct super_block *sb)
1805 T(YAFFS_TRACE_OS, ("yaffs_write_super\n"));
1806 if (yaffs_auto_checkpoint >= 2)
1807 yaffs_do_sync_fs(sb);
1808 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
1814 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1815 static int yaffs_sync_fs(struct super_block *sb, int wait)
1817 static int yaffs_sync_fs(struct super_block *sb)
1820 T(YAFFS_TRACE_OS, ("yaffs_sync_fs\n"));
1822 if (yaffs_auto_checkpoint >= 1)
1823 yaffs_do_sync_fs(sb);
1828 #ifdef YAFFS_USE_OWN_IGET
1830 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1832 struct inode *inode;
1834 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1837 ("yaffs_iget for %lu\n", ino));
1839 inode = iget_locked(sb, ino);
1841 return ERR_PTR(-ENOMEM);
1842 if (!(inode->i_state & I_NEW))
1845 /* NB This is called as a side effect of other functions, but
1846 * we had to release the lock to prevent deadlocks, so
1847 * need to lock again.
1850 yaffs_GrossLock(dev);
1852 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1854 yaffs_FillInodeFromObject(inode, obj);
1856 yaffs_GrossUnlock(dev);
1858 unlock_new_inode(inode);
1864 static void yaffs_read_inode(struct inode *inode)
1866 /* NB This is called as a side effect of other functions, but
1867 * we had to release the lock to prevent deadlocks, so
1868 * need to lock again.
1872 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1875 ("yaffs_read_inode for %d\n", (int)inode->i_ino));
1877 yaffs_GrossLock(dev);
1879 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1881 yaffs_FillInodeFromObject(inode, obj);
1883 yaffs_GrossUnlock(dev);
1888 static YLIST_HEAD(yaffs_dev_list);
1890 #if 0 /* not used */
1891 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1893 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1895 if (*flags & MS_RDONLY) {
1896 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1899 ("yaffs_remount_fs: %s: RO\n", dev->name));
1901 yaffs_GrossLock(dev);
1903 yaffs_FlushEntireDeviceCache(dev);
1905 yaffs_CheckpointSave(dev);
1910 yaffs_GrossUnlock(dev);
1913 ("yaffs_remount_fs: %s: RW\n", dev->name));
1920 static void yaffs_put_super(struct super_block *sb)
1922 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1924 T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
1926 yaffs_GrossLock(dev);
1928 yaffs_FlushEntireDeviceCache(dev);
1930 yaffs_CheckpointSave(dev);
1932 if (dev->putSuperFunc)
1933 dev->putSuperFunc(sb);
1935 yaffs_Deinitialise(dev);
1937 yaffs_GrossUnlock(dev);
1939 /* we assume this is protected by lock_kernel() in mount/umount */
1940 ylist_del(&dev->devList);
1942 if (dev->spareBuffer) {
1943 YFREE(dev->spareBuffer);
1944 dev->spareBuffer = NULL;
1951 static void yaffs_MTDPutSuper(struct super_block *sb)
1953 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1958 put_mtd_device(mtd);
1962 static void yaffs_MarkSuperBlockDirty(void *vsb)
1964 struct super_block *sb = (struct super_block *)vsb;
1966 T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
1973 int skip_checkpoint_read;
1974 int skip_checkpoint_write;
1977 int tags_ecc_overridden;
1978 int lazy_loading_enabled;
1979 int lazy_loading_overridden;
1980 int empty_lost_and_found;
1981 int empty_lost_and_found_overridden;
1984 #define MAX_OPT_LEN 30
1985 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
1987 char cur_opt[MAX_OPT_LEN + 1];
1991 /* Parse through the options which is a comma seperated list */
1993 while (options_str && *options_str && !error) {
1994 memset(cur_opt, 0, MAX_OPT_LEN + 1);
1997 while(*options_str == ',')
2000 while (*options_str && *options_str != ',') {
2001 if (p < MAX_OPT_LEN) {
2002 cur_opt[p] = *options_str;
2008 if (!strcmp(cur_opt, "inband-tags"))
2009 options->inband_tags = 1;
2010 else if (!strcmp(cur_opt, "tags-ecc-off")){
2011 options->tags_ecc_on = 0;
2012 options->tags_ecc_overridden=1;
2013 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2014 options->tags_ecc_on = 1;
2015 options->tags_ecc_overridden = 1;
2016 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2017 options->lazy_loading_enabled = 0;
2018 options->lazy_loading_overridden=1;
2019 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2020 options->lazy_loading_enabled = 1;
2021 options->lazy_loading_overridden = 1;
2022 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2023 options->empty_lost_and_found = 0;
2024 options->empty_lost_and_found_overridden=1;
2025 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2026 options->empty_lost_and_found = 1;
2027 options->empty_lost_and_found_overridden=1;
2028 } else if (!strcmp(cur_opt, "no-cache"))
2029 options->no_cache = 1;
2030 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2031 options->skip_checkpoint_read = 1;
2032 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2033 options->skip_checkpoint_write = 1;
2034 else if (!strcmp(cur_opt, "no-checkpoint")) {
2035 options->skip_checkpoint_read = 1;
2036 options->skip_checkpoint_write = 1;
2038 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2047 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2048 struct super_block *sb,
2049 void *data, int silent)
2052 struct inode *inode = NULL;
2053 struct dentry *root;
2054 yaffs_Device *dev = 0;
2055 char devname_buf[BDEVNAME_SIZE + 1];
2056 struct mtd_info *mtd;
2058 char *data_str = (char *)data;
2060 yaffs_options options;
2062 sb->s_magic = YAFFS_MAGIC;
2063 sb->s_op = &yaffs_super_ops;
2064 sb->s_flags |= MS_NOATIME;
2067 printk(KERN_INFO "yaffs: sb is NULL\n");
2068 else if (!sb->s_dev)
2069 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2070 else if (!yaffs_devname(sb, devname_buf))
2071 printk(KERN_INFO "yaffs: devname is NULL\n");
2073 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2075 yaffs_devname(sb, devname_buf));
2080 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2082 memset(&options, 0, sizeof(options));
2084 if (yaffs_parse_options(&options, data_str)) {
2085 /* Option parsing failed */
2090 sb->s_blocksize = PAGE_CACHE_SIZE;
2091 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2092 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2094 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2096 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2098 ("yaffs: Write verification disabled. All guarantees "
2099 "null and void\n"));
2102 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2104 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2105 yaffs_devname(sb, devname_buf)));
2107 /* Check it's an mtd device..... */
2108 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2109 return NULL; /* This isn't an mtd device */
2111 /* Get the device */
2112 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2114 T(YAFFS_TRACE_ALWAYS,
2115 ("yaffs: MTD device #%u doesn't appear to exist\n",
2119 /* Check it's NAND */
2120 if (mtd->type != MTD_NANDFLASH) {
2121 T(YAFFS_TRACE_ALWAYS,
2122 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
2126 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
2127 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
2128 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
2129 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
2130 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
2131 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
2132 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
2133 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2134 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
2135 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
2136 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2137 T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
2139 T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
2142 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2144 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2145 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
2149 /* Added NCB 26/5/2006 for completeness */
2150 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2151 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
2157 if (yaffsVersion == 2) {
2158 /* Check for version 2 style functions */
2160 !mtd->block_isbad ||
2161 !mtd->block_markbad ||
2164 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2165 !mtd->read_oob || !mtd->write_oob) {
2168 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2170 T(YAFFS_TRACE_ALWAYS,
2171 ("yaffs: MTD device does not support required "
2176 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2177 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2178 !options.inband_tags) {
2179 T(YAFFS_TRACE_ALWAYS,
2180 ("yaffs: MTD device does not have the "
2181 "right page sizes\n"));
2185 /* Check for V1 style functions */
2189 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2190 !mtd->read_oob || !mtd->write_oob) {
2193 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2195 T(YAFFS_TRACE_ALWAYS,
2196 ("yaffs: MTD device does not support required "
2201 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2202 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2203 T(YAFFS_TRACE_ALWAYS,
2204 ("yaffs: MTD device does not support have the "
2205 "right page sizes\n"));
2210 /* OK, so if we got here, we have an MTD that's NAND and looks
2211 * like it has the right capabilities
2212 * Set the yaffs_Device up for mtd
2215 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2216 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2218 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2221 /* Deep shit could not allocate device structure */
2222 T(YAFFS_TRACE_ALWAYS,
2223 ("yaffs_read_super: Failed trying to allocate "
2224 "yaffs_Device. \n"));
2228 memset(dev, 0, sizeof(yaffs_Device));
2229 dev->genericDevice = mtd;
2230 dev->name = mtd->name;
2232 /* Set up the memory size parameters.... */
2234 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2236 dev->startBlock = 0;
2237 dev->endBlock = nBlocks - 1;
2238 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2239 dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2240 dev->nReservedBlocks = 5;
2241 dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
2242 dev->inbandTags = options.inband_tags;
2244 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2245 dev->disableLazyLoad = 1;
2247 if(options.lazy_loading_overridden)
2248 dev->disableLazyLoad = !options.lazy_loading_enabled;
2250 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2253 if(options.tags_ecc_overridden)
2254 dev->noTagsECC = !options.tags_ecc_on;
2256 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2257 dev->emptyLostAndFound = 1;
2259 if(options.empty_lost_and_found_overridden)
2260 dev->emptyLostAndFound = options.empty_lost_and_found;
2262 /* ... and the functions. */
2263 if (yaffsVersion == 2) {
2264 dev->writeChunkWithTagsToNAND =
2265 nandmtd2_WriteChunkWithTagsToNAND;
2266 dev->readChunkWithTagsFromNAND =
2267 nandmtd2_ReadChunkWithTagsFromNAND;
2268 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2269 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
2270 dev->spareBuffer = YMALLOC(mtd->oobsize);
2272 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2273 dev->totalBytesPerChunk = mtd->writesize;
2274 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2276 dev->totalBytesPerChunk = mtd->oobblock;
2277 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2279 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2281 dev->startBlock = 0;
2282 dev->endBlock = nBlocks - 1;
2284 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2285 /* use the MTD interface in yaffs_mtdif1.c */
2286 dev->writeChunkWithTagsToNAND =
2287 nandmtd1_WriteChunkWithTagsToNAND;
2288 dev->readChunkWithTagsFromNAND =
2289 nandmtd1_ReadChunkWithTagsFromNAND;
2290 dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2291 dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
2293 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2294 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2298 /* ... and common functions */
2299 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2300 dev->initialiseNAND = nandmtd_InitialiseNAND;
2302 dev->putSuperFunc = yaffs_MTDPutSuper;
2304 dev->superBlock = (void *)sb;
2305 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2308 #ifndef CONFIG_YAFFS_DOES_ECC
2309 dev->useNANDECC = 1;
2312 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2313 dev->wideTnodesDisabled = 1;
2316 dev->skipCheckpointRead = options.skip_checkpoint_read;
2317 dev->skipCheckpointWrite = options.skip_checkpoint_write;
2319 /* we assume this is protected by lock_kernel() in mount/umount */
2320 ylist_add_tail(&dev->devList, &yaffs_dev_list);
2322 /* Directory search handling...*/
2323 YINIT_LIST_HEAD(&dev->searchContexts);
2324 dev->removeObjectCallback = yaffs_RemoveObjectCallback;
2326 init_MUTEX(&dev->grossLock);
2328 yaffs_GrossLock(dev);
2330 err = yaffs_GutsInitialise(dev);
2333 ("yaffs_read_super: guts initialised %s\n",
2334 (err == YAFFS_OK) ? "OK" : "FAILED"));
2336 /* Release lock before yaffs_get_inode() */
2337 yaffs_GrossUnlock(dev);
2339 /* Create root inode */
2340 if (err == YAFFS_OK)
2341 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2347 inode->i_op = &yaffs_dir_inode_operations;
2348 inode->i_fop = &yaffs_dir_operations;
2350 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
2352 root = d_alloc_root(inode);
2354 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
2361 sb->s_dirt = !dev->isCheckpointed;
2362 T(YAFFS_TRACE_ALWAYS,
2363 ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
2365 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
2370 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2371 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2374 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2377 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2378 static int yaffs_read_super(struct file_system_type *fs,
2379 int flags, const char *dev_name,
2380 void *data, struct vfsmount *mnt)
2383 return get_sb_bdev(fs, flags, dev_name, data,
2384 yaffs_internal_read_super_mtd, mnt);
2387 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2388 int flags, const char *dev_name,
2392 return get_sb_bdev(fs, flags, dev_name, data,
2393 yaffs_internal_read_super_mtd);
2397 static struct file_system_type yaffs_fs_type = {
2398 .owner = THIS_MODULE,
2400 .get_sb = yaffs_read_super,
2401 .kill_sb = kill_block_super,
2402 .fs_flags = FS_REQUIRES_DEV,
2405 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2408 return yaffs_internal_read_super(1, sb, data, silent);
2411 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2416 #ifdef CONFIG_YAFFS_YAFFS2
2418 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2419 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2422 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2425 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2426 static int yaffs2_read_super(struct file_system_type *fs,
2427 int flags, const char *dev_name, void *data,
2428 struct vfsmount *mnt)
2430 return get_sb_bdev(fs, flags, dev_name, data,
2431 yaffs2_internal_read_super_mtd, mnt);
2434 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2435 int flags, const char *dev_name,
2439 return get_sb_bdev(fs, flags, dev_name, data,
2440 yaffs2_internal_read_super_mtd);
2444 static struct file_system_type yaffs2_fs_type = {
2445 .owner = THIS_MODULE,
2447 .get_sb = yaffs2_read_super,
2448 .kill_sb = kill_block_super,
2449 .fs_flags = FS_REQUIRES_DEV,
2452 static struct super_block *yaffs2_read_super(struct super_block *sb,
2453 void *data, int silent)
2455 return yaffs_internal_read_super(2, sb, data, silent);
2458 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2462 #endif /* CONFIG_YAFFS_YAFFS2 */
2464 static struct proc_dir_entry *my_proc_entry;
2466 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
2468 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
2469 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
2470 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
2471 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2472 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2473 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2474 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2475 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
2476 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2477 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2478 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2479 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2480 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2481 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2482 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2483 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2484 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2485 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2486 buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2487 buf += sprintf(buf, "passiveGCs......... %d\n",
2488 dev->passiveGarbageCollections);
2489 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2490 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
2491 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2492 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2493 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2494 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2495 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2496 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2497 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2498 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2500 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2506 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
2508 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
2509 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->noTagsECC);
2510 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
2511 buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
2512 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->emptyLostAndFound);
2513 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->disableLazyLoad);
2518 static int yaffs_proc_read(char *page,
2520 off_t offset, int count, int *eof, void *data)
2522 struct ylist_head *item;
2527 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2528 * We use 'offset' (*ppos) to indicate where we are in devList.
2529 * This also assumes the user has posted a read buffer large
2530 * enough to hold the complete output; but that's life in /proc.
2535 /* Print header first */
2537 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2538 "\n%s\n%s\n", yaffs_fs_c_version,
2539 yaffs_guts_c_version);
2541 buf += sprintf(buf,"\n");
2545 /* hold lock_kernel while traversing yaffs_dev_list */
2548 /* Locate and print the Nth entry. Order N-squared but N is small. */
2549 ylist_for_each(item, &yaffs_dev_list) {
2550 yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
2551 if (n < (step & ~1)) {
2556 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
2557 buf = yaffs_dump_dev_part0(buf, dev);
2559 buf = yaffs_dump_dev_part1(buf, dev);
2566 return buf - page < count ? buf - page : count;
2570 * Set the verbosity of the warnings and error messages.
2572 * Note that the names can only be a..z or _ with the current code.
2577 unsigned mask_bitfield;
2579 {"allocate", YAFFS_TRACE_ALLOCATE},
2580 {"always", YAFFS_TRACE_ALWAYS},
2581 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2582 {"buffers", YAFFS_TRACE_BUFFERS},
2583 {"bug", YAFFS_TRACE_BUG},
2584 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2585 {"deletion", YAFFS_TRACE_DELETION},
2586 {"erase", YAFFS_TRACE_ERASE},
2587 {"error", YAFFS_TRACE_ERROR},
2588 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2589 {"gc", YAFFS_TRACE_GC},
2590 {"mtd", YAFFS_TRACE_MTD},
2591 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2592 {"os", YAFFS_TRACE_OS},
2593 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2594 {"scan", YAFFS_TRACE_SCAN},
2595 {"tracing", YAFFS_TRACE_TRACING},
2597 {"verify", YAFFS_TRACE_VERIFY},
2598 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2599 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2600 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2602 {"write", YAFFS_TRACE_WRITE},
2603 {"all", 0xffffffff},
2608 #define MAX_MASK_NAME_LENGTH 40
2609 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2610 unsigned long count, void *data)
2612 unsigned rg = 0, mask_bitfield;
2616 char substring[MAX_MASK_NAME_LENGTH + 1];
2622 rg = yaffs_traceMask;
2624 while (!done && (pos < count)) {
2626 while ((pos < count) && isspace(buf[pos]))
2643 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2645 if (end > buf + pos) {
2646 mask_name = "numeral";
2647 len = end - (buf + pos);
2651 for (x = buf + pos, i = 0;
2652 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2653 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2655 substring[i] = '\0';
2657 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2658 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
2659 mask_name = mask_flags[i].mask_name;
2660 mask_bitfield = mask_flags[i].mask_bitfield;
2667 if (mask_name != NULL) {
2671 rg &= ~mask_bitfield;
2674 rg |= mask_bitfield;
2680 rg |= mask_bitfield;
2686 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2688 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
2690 if (rg & YAFFS_TRACE_ALWAYS) {
2691 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2693 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2694 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
2702 static int yaffs_proc_write(struct file *file, const char *buf,
2703 unsigned long count, void *data)
2705 return yaffs_proc_write_trace_options(file, buf, count, data);
2708 /* Stuff to handle installation of file systems */
2709 struct file_system_to_install {
2710 struct file_system_type *fst;
2714 static struct file_system_to_install fs_to_install[] = {
2715 {&yaffs_fs_type, 0},
2716 {&yaffs2_fs_type, 0},
2720 static int __init init_yaffs_fs(void)
2723 struct file_system_to_install *fsinst;
2725 T(YAFFS_TRACE_ALWAYS,
2726 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2728 /* Install the proc_fs entry */
2729 my_proc_entry = create_proc_entry("yaffs",
2733 if (my_proc_entry) {
2734 my_proc_entry->write_proc = yaffs_proc_write;
2735 my_proc_entry->read_proc = yaffs_proc_read;
2736 my_proc_entry->data = NULL;
2740 /* Now add the file system entries */
2742 fsinst = fs_to_install;
2744 while (fsinst->fst && !error) {
2745 error = register_filesystem(fsinst->fst);
2747 fsinst->installed = 1;
2751 /* Any errors? uninstall */
2753 fsinst = fs_to_install;
2755 while (fsinst->fst) {
2756 if (fsinst->installed) {
2757 unregister_filesystem(fsinst->fst);
2758 fsinst->installed = 0;
2767 static void __exit exit_yaffs_fs(void)
2770 struct file_system_to_install *fsinst;
2772 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2775 remove_proc_entry("yaffs", YPROC_ROOT);
2777 fsinst = fs_to_install;
2779 while (fsinst->fst) {
2780 if (fsinst->installed) {
2781 unregister_filesystem(fsinst->fst);
2782 fsinst->installed = 0;
2788 module_init(init_yaffs_fs)
2789 module_exit(exit_yaffs_fs)
2791 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2792 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2793 MODULE_LICENSE("GPL");