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.97 2010-03-09 04:12:00 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 <linux/kthread.h>
57 #include "asm/div64.h"
62 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
64 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
65 #include <linux/statfs.h>
66 #define UnlockPage(p) unlock_page(p)
67 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
69 /* FIXME: use sb->s_id instead ? */
70 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
74 #include <linux/locks.h>
75 #define BDEVNAME_SIZE 0
76 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
78 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
79 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
85 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
86 #define YPROC_ROOT (&proc_root)
88 #define YPROC_ROOT NULL
91 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
92 #define WRITE_SIZE_STR "writesize"
93 #define WRITE_SIZE(mtd) ((mtd)->writesize)
95 #define WRITE_SIZE_STR "oobblock"
96 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
99 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
100 #define YAFFS_USE_WRITE_BEGIN_END 1
102 #define YAFFS_USE_WRITE_BEGIN_END 0
105 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
106 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
108 uint64_t result = partition_size;
109 do_div(result, block_size);
110 return (uint32_t)result;
113 #define YCALCBLOCKS(s, b) ((s)/(b))
116 #include <linux/uaccess.h>
118 #include "yportenv.h"
119 #include "yaffs_trace.h"
120 #include "yaffs_guts.h"
122 #include "yaffs_linux.h"
124 #include <linux/mtd/mtd.h>
125 #include "yaffs_mtdif.h"
126 #include "yaffs_mtdif1.h"
127 #include "yaffs_mtdif2.h"
129 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
130 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
131 unsigned int yaffs_auto_checkpoint = 1;
133 /* Module Parameters */
134 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
135 module_param(yaffs_traceMask, uint, 0644);
136 module_param(yaffs_wr_attempts, uint, 0644);
137 module_param(yaffs_auto_checkpoint, uint, 0644);
139 MODULE_PARM(yaffs_traceMask, "i");
140 MODULE_PARM(yaffs_wr_attempts, "i");
141 MODULE_PARM(yaffs_auto_checkpoint, "i");
144 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
145 /* use iget and read_inode */
146 #define Y_IGET(sb, inum) iget((sb), (inum))
147 static void yaffs_read_inode(struct inode *inode);
150 /* Call local equivalent */
151 #define YAFFS_USE_OWN_IGET
152 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
154 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
157 /*#define T(x) printk x */
159 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
160 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
162 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
165 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
166 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
168 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
169 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
171 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
175 #define update_dir_time(dir) do {\
176 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
179 static void yaffs_put_super(struct super_block *sb);
181 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
183 static ssize_t yaffs_hold_space(struct file *f);
184 static void yaffs_release_space(struct file *f);
186 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
187 static int yaffs_file_flush(struct file *file, fl_owner_t id);
189 static int yaffs_file_flush(struct file *file);
192 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
195 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
197 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
198 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
199 struct nameidata *n);
200 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
201 struct nameidata *n);
203 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
204 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
206 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
207 struct dentry *dentry);
208 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
209 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
210 const char *symname);
211 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
213 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
214 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
217 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
220 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
221 struct inode *new_dir, struct dentry *new_dentry);
222 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
224 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
225 static int yaffs_sync_fs(struct super_block *sb, int wait);
226 static void yaffs_write_super(struct super_block *sb);
228 static int yaffs_sync_fs(struct super_block *sb);
229 static int yaffs_write_super(struct super_block *sb);
232 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
233 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
234 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
235 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
237 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
240 #ifdef YAFFS_HAS_PUT_INODE
241 static void yaffs_put_inode(struct inode *inode);
244 static void yaffs_delete_inode(struct inode *);
245 static void yaffs_clear_inode(struct inode *);
247 static int yaffs_readpage(struct file *file, struct page *page);
248 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
249 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
251 static int yaffs_writepage(struct page *page);
255 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
256 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
257 loff_t pos, unsigned len, unsigned flags,
258 struct page **pagep, void **fsdata);
259 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
260 loff_t pos, unsigned len, unsigned copied,
261 struct page *pg, void *fsdadata);
263 static int yaffs_prepare_write(struct file *f, struct page *pg,
264 unsigned offset, unsigned to);
265 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
270 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
272 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
273 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
275 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
278 static struct address_space_operations yaffs_file_address_operations = {
279 .readpage = yaffs_readpage,
280 .writepage = yaffs_writepage,
281 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
282 .write_begin = yaffs_write_begin,
283 .write_end = yaffs_write_end,
285 .prepare_write = yaffs_prepare_write,
286 .commit_write = yaffs_commit_write,
290 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
291 static const struct file_operations yaffs_file_operations = {
292 .read = do_sync_read,
293 .write = do_sync_write,
294 .aio_read = generic_file_aio_read,
295 .aio_write = generic_file_aio_write,
296 .mmap = generic_file_mmap,
297 .flush = yaffs_file_flush,
298 .fsync = yaffs_sync_object,
299 .splice_read = generic_file_splice_read,
300 .splice_write = generic_file_splice_write,
301 .llseek = generic_file_llseek,
304 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
306 static const struct file_operations yaffs_file_operations = {
307 .read = do_sync_read,
308 .write = do_sync_write,
309 .aio_read = generic_file_aio_read,
310 .aio_write = generic_file_aio_write,
311 .mmap = generic_file_mmap,
312 .flush = yaffs_file_flush,
313 .fsync = yaffs_sync_object,
314 .sendfile = generic_file_sendfile,
319 static const struct file_operations yaffs_file_operations = {
320 .read = generic_file_read,
321 .write = generic_file_write,
322 .mmap = generic_file_mmap,
323 .flush = yaffs_file_flush,
324 .fsync = yaffs_sync_object,
325 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
326 .sendfile = generic_file_sendfile,
331 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
332 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
334 void * kaddr = kmap_atomic(page, KM_USER0);
335 memset(kaddr + start, 0, end - start);
336 kunmap_atomic(kaddr, KM_USER0);
337 flush_dcache_page(page);
342 static const struct inode_operations yaffs_file_inode_operations = {
343 .setattr = yaffs_setattr,
346 static const struct inode_operations yaffs_symlink_inode_operations = {
347 .readlink = yaffs_readlink,
348 .follow_link = yaffs_follow_link,
349 .setattr = yaffs_setattr,
352 static const struct inode_operations yaffs_dir_inode_operations = {
353 .create = yaffs_create,
354 .lookup = yaffs_lookup,
356 .unlink = yaffs_unlink,
357 .symlink = yaffs_symlink,
358 .mkdir = yaffs_mkdir,
359 .rmdir = yaffs_unlink,
360 .mknod = yaffs_mknod,
361 .rename = yaffs_rename,
362 .setattr = yaffs_setattr,
365 static const struct file_operations yaffs_dir_operations = {
366 .read = generic_read_dir,
367 .readdir = yaffs_readdir,
368 .fsync = yaffs_sync_object,
371 static const struct super_operations yaffs_super_ops = {
372 .statfs = yaffs_statfs,
374 #ifndef YAFFS_USE_OWN_IGET
375 .read_inode = yaffs_read_inode,
377 #ifdef YAFFS_HAS_PUT_INODE
378 .put_inode = yaffs_put_inode,
380 .put_super = yaffs_put_super,
381 .delete_inode = yaffs_delete_inode,
382 .clear_inode = yaffs_clear_inode,
383 .sync_fs = yaffs_sync_fs,
384 .write_super = yaffs_write_super,
387 static void yaffs_GrossLock(yaffs_Device *dev)
389 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
390 down(&(yaffs_DeviceToContext(dev)->grossLock));
391 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
394 static void yaffs_GrossUnlock(yaffs_Device *dev)
396 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
397 up(&(yaffs_DeviceToContext(dev)->grossLock));
401 /*-----------------------------------------------------------------*/
402 /* Directory search context allows us to unlock access to yaffs during
403 * filldir without causing problems with the directory being modified.
404 * This is similar to the tried and tested mechanism used in yaffs direct.
406 * A search context iterates along a doubly linked list of siblings in the
407 * directory. If the iterating object is deleted then this would corrupt
408 * the list iteration, likely causing a crash. The search context avoids
409 * this by using the removeObjectCallback to move the search context to the
410 * next object before the object is deleted.
412 * Many readdirs (and thus seach conexts) may be alive simulateously so
413 * each yaffs_Device has a list of these.
415 * A seach context lives for the duration of a readdir.
417 * All these functions must be called while yaffs is locked.
420 struct yaffs_SearchContext {
422 yaffs_Object *dirObj;
423 yaffs_Object *nextReturn;
424 struct ylist_head others;
428 * yaffs_NewSearch() creates a new search context, initialises it and
429 * adds it to the device's search context list.
431 * Called at start of readdir.
433 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
435 yaffs_Device *dev = dir->myDev;
436 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
440 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
441 sc->nextReturn = NULL;
443 sc->nextReturn = ylist_entry(
444 dir->variant.directoryVariant.children.next,
445 yaffs_Object,siblings);
446 YINIT_LIST_HEAD(&sc->others);
447 ylist_add(&sc->others,&(yaffs_DeviceToContext(dev)->searchContexts));
453 * yaffs_EndSearch() disposes of a search context and cleans up.
455 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
458 ylist_del(&sc->others);
464 * yaffs_SearchAdvance() moves a search context to the next object.
465 * Called when the search iterates or when an object removal causes
466 * the search context to be moved to the next object.
468 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
473 if( sc->nextReturn == NULL ||
474 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
475 sc->nextReturn = NULL;
477 struct ylist_head *next = sc->nextReturn->siblings.next;
479 if( next == &sc->dirObj->variant.directoryVariant.children)
480 sc->nextReturn = NULL; /* end of list */
482 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
487 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
488 * We check open search contexts and advance any which are currently
489 * on the object being iterated.
491 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
494 struct ylist_head *i;
495 struct yaffs_SearchContext *sc;
496 struct ylist_head *search_contexts = &(yaffs_DeviceToContext(obj->myDev)->searchContexts);
499 /* Iterate through the directory search contexts.
500 * If any are currently on the object being removed, then advance
501 * the search context to the next object to prevent a hanging pointer.
503 ylist_for_each(i, search_contexts) {
505 sc = ylist_entry(i, struct yaffs_SearchContext,others);
506 if(sc->nextReturn == obj)
507 yaffs_SearchAdvance(sc);
514 /*-----------------------------------------------------------------*/
516 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
519 unsigned char *alias;
522 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
524 yaffs_GrossLock(dev);
526 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
528 yaffs_GrossUnlock(dev);
533 ret = vfs_readlink(dentry, buffer, buflen, alias);
538 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
539 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
541 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
544 unsigned char *alias;
546 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
548 yaffs_GrossLock(dev);
550 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
552 yaffs_GrossUnlock(dev);
559 ret = vfs_follow_link(nd, alias);
562 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
569 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
573 * Lookup is used to find objects in the fs
575 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
577 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
580 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
584 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
586 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
588 yaffs_GrossLock(dev);
591 ("yaffs_lookup for %d:%s\n",
592 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
594 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
595 dentry->d_name.name);
597 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
599 /* Can't hold gross lock when calling yaffs_get_inode() */
600 yaffs_GrossUnlock(dev);
604 ("yaffs_lookup found %d\n", obj->objectId));
606 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
610 ("yaffs_loookup dentry \n"));
611 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
612 * d_add even if NULL inode */
614 /*dget(dentry); // try to solve directory bug */
615 d_add(dentry, inode);
623 T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
627 /* added NCB for 2.5/6 compatability - forces add even if inode is
628 * NULL which creates dentry hash */
629 d_add(dentry, inode);
635 #ifdef YAFFS_HAS_PUT_INODE
637 /* For now put inode is just for debugging
638 * Put inode is called when the inode **structure** is put.
640 static void yaffs_put_inode(struct inode *inode)
643 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
644 atomic_read(&inode->i_count)));
649 /* clear is called to tell the fs to release any per-inode data it holds */
650 static void yaffs_clear_inode(struct inode *inode)
655 obj = yaffs_InodeToObject(inode);
658 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
659 atomic_read(&inode->i_count),
660 obj ? "object exists" : "null object"));
664 yaffs_GrossLock(dev);
666 /* Clear the association between the inode and
670 yaffs_InodeToObjectLV(inode) = NULL;
672 /* If the object freeing was deferred, then the real
674 * This should fix the inode inconsistency problem.
677 yaffs_HandleDeferedFree(obj);
679 yaffs_GrossUnlock(dev);
684 /* delete is called when the link count is zero and the inode
685 * is put (ie. nobody wants to know about it anymore, time to
687 * NB Must call clear_inode()
689 static void yaffs_delete_inode(struct inode *inode)
691 yaffs_Object *obj = yaffs_InodeToObject(inode);
695 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
696 atomic_read(&inode->i_count),
697 obj ? "object exists" : "null object"));
701 yaffs_GrossLock(dev);
702 yaffs_DeleteObject(obj);
703 yaffs_GrossUnlock(dev);
705 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
706 truncate_inode_pages(&inode->i_data, 0);
711 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
712 static int yaffs_file_flush(struct file *file, fl_owner_t id)
714 static int yaffs_file_flush(struct file *file)
717 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
719 yaffs_Device *dev = obj->myDev;
722 ("yaffs_file_flush object %d (%s)\n", obj->objectId,
723 obj->dirty ? "dirty" : "clean"));
725 yaffs_GrossLock(dev);
727 yaffs_FlushFile(obj, 1, 0);
729 yaffs_GrossUnlock(dev);
734 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
736 /* Lifted from jffs2 */
739 unsigned char *pg_buf;
744 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock at %08x, size %08x\n",
745 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
746 (unsigned)PAGE_CACHE_SIZE));
748 obj = yaffs_DentryToObject(f->f_dentry);
752 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
753 BUG_ON(!PageLocked(pg));
760 /* FIXME: Can kmap fail? */
762 yaffs_GrossLock(dev);
764 ret = yaffs_ReadDataFromFile(obj, pg_buf,
765 pg->index << PAGE_CACHE_SHIFT,
768 yaffs_GrossUnlock(dev);
774 ClearPageUptodate(pg);
781 flush_dcache_page(pg);
784 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock done\n"));
788 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
790 int ret = yaffs_readpage_nolock(f, pg);
795 static int yaffs_readpage(struct file *f, struct page *pg)
799 T(YAFFS_TRACE_OS, ("yaffs_readpage\n"));
800 ret=yaffs_readpage_unlock(f, pg);
801 T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
805 /* writepage inspired by/stolen from smbfs */
807 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
808 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
810 static int yaffs_writepage(struct page *page)
813 struct address_space *mapping = page->mapping;
815 unsigned long end_index;
824 inode = mapping->host;
827 i_size = i_size_read(inode);
829 end_index = i_size >> PAGE_CACHE_SHIFT;
831 if(page->index < end_index)
832 nBytes = PAGE_CACHE_SIZE;
834 nBytes = i_size & (PAGE_CACHE_SIZE -1);
836 if (page->index > end_index || !nBytes) {
838 ("yaffs_writepage at %08x, inode size = %08x!!!\n",
839 (unsigned)(page->index << PAGE_CACHE_SHIFT),
840 (unsigned)inode->i_size));
842 (" -> don't care!!\n"));
844 zero_user_segment(page,0,PAGE_CACHE_SIZE);
845 set_page_writeback(page);
847 end_page_writeback(page);
852 if(nBytes != PAGE_CACHE_SIZE)
853 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
859 obj = yaffs_InodeToObject(inode);
860 yaffs_GrossLock(obj->myDev);
863 ("yaffs_writepage at %08x, size %08x\n",
864 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
866 ("writepag0: obj = %05x, ino = %05x\n",
867 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
869 nWritten = yaffs_WriteDataToFile(obj, buffer,
870 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
873 ("writepag1: obj = %05x, ino = %05x\n",
874 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
876 yaffs_GrossUnlock(obj->myDev);
879 set_page_writeback(page);
881 end_page_writeback(page);
884 return (nWritten == nBytes) ? 0 : -ENOSPC;
888 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
889 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
890 loff_t pos, unsigned len, unsigned flags,
891 struct page **pagep, void **fsdata)
893 struct page *pg = NULL;
894 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
900 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
901 pg = grab_cache_page_write_begin(mapping, index, flags);
903 pg = __grab_cache_page(mapping, index);
911 T(YAFFS_TRACE_OS, ("start yaffs_write_begin index %d(%x) uptodate %d\n",(int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
914 space_held = yaffs_hold_space(filp);
921 /* Update page if required */
923 if (!Page_Uptodate(pg))
924 ret = yaffs_readpage_nolock(filp, pg);
929 /* Happy path return */
930 T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
935 T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
937 yaffs_release_space(filp);
940 page_cache_release(pg);
947 static int yaffs_prepare_write(struct file *f, struct page *pg,
948 unsigned offset, unsigned to)
950 T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
952 if (!Page_Uptodate(pg))
953 return yaffs_readpage_nolock(f, pg);
958 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
959 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
960 loff_t pos, unsigned len, unsigned copied,
961 struct page *pg, void *fsdadata)
965 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
968 addr = kva + offset_into_page;
971 ("yaffs_write_end addr %x pos %x nBytes %d\n",
975 ret = yaffs_file_write(filp, addr, copied, &pos);
979 ("yaffs_write_end not same size ret %d copied %d\n",
988 yaffs_release_space(filp);
990 page_cache_release(pg);
995 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1000 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1001 int nBytes = to - offset;
1004 unsigned spos = pos;
1008 addr = kva + offset;
1010 saddr = (unsigned) addr;
1013 ("yaffs_commit_write addr %x pos %x nBytes %d\n",
1014 saddr, spos, nBytes));
1016 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1018 if (nWritten != nBytes) {
1020 ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
1030 ("yaffs_commit_write returning %d\n",
1031 nWritten == nBytes ? 0 : nWritten));
1033 return nWritten == nBytes ? 0 : nWritten;
1038 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1043 /* Check mode against the variant type and attempt to repair if broken. */
1044 __u32 mode = obj->yst_mode;
1045 switch (obj->variantType) {
1046 case YAFFS_OBJECT_TYPE_FILE:
1047 if (!S_ISREG(mode)) {
1048 obj->yst_mode &= ~S_IFMT;
1049 obj->yst_mode |= S_IFREG;
1053 case YAFFS_OBJECT_TYPE_SYMLINK:
1054 if (!S_ISLNK(mode)) {
1055 obj->yst_mode &= ~S_IFMT;
1056 obj->yst_mode |= S_IFLNK;
1060 case YAFFS_OBJECT_TYPE_DIRECTORY:
1061 if (!S_ISDIR(mode)) {
1062 obj->yst_mode &= ~S_IFMT;
1063 obj->yst_mode |= S_IFDIR;
1067 case YAFFS_OBJECT_TYPE_UNKNOWN:
1068 case YAFFS_OBJECT_TYPE_HARDLINK:
1069 case YAFFS_OBJECT_TYPE_SPECIAL:
1075 inode->i_flags |= S_NOATIME;
1077 inode->i_ino = obj->objectId;
1078 inode->i_mode = obj->yst_mode;
1079 inode->i_uid = obj->yst_uid;
1080 inode->i_gid = obj->yst_gid;
1081 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1082 inode->i_blksize = inode->i_sb->s_blocksize;
1084 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1086 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1087 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1088 inode->i_atime.tv_nsec = 0;
1089 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1090 inode->i_mtime.tv_nsec = 0;
1091 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1092 inode->i_ctime.tv_nsec = 0;
1094 inode->i_rdev = obj->yst_rdev;
1095 inode->i_atime = obj->yst_atime;
1096 inode->i_mtime = obj->yst_mtime;
1097 inode->i_ctime = obj->yst_ctime;
1099 inode->i_size = yaffs_GetObjectFileLength(obj);
1100 inode->i_blocks = (inode->i_size + 511) >> 9;
1102 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1105 ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
1106 inode->i_mode, inode->i_uid, inode->i_gid,
1107 (int)inode->i_size, atomic_read(&inode->i_count)));
1109 switch (obj->yst_mode & S_IFMT) {
1110 default: /* fifo, device or socket */
1111 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1112 init_special_inode(inode, obj->yst_mode,
1113 old_decode_dev(obj->yst_rdev));
1115 init_special_inode(inode, obj->yst_mode,
1116 (dev_t) (obj->yst_rdev));
1119 case S_IFREG: /* file */
1120 inode->i_op = &yaffs_file_inode_operations;
1121 inode->i_fop = &yaffs_file_operations;
1122 inode->i_mapping->a_ops =
1123 &yaffs_file_address_operations;
1125 case S_IFDIR: /* directory */
1126 inode->i_op = &yaffs_dir_inode_operations;
1127 inode->i_fop = &yaffs_dir_operations;
1129 case S_IFLNK: /* symlink */
1130 inode->i_op = &yaffs_symlink_inode_operations;
1134 yaffs_InodeToObjectLV(inode) = obj;
1136 obj->myInode = inode;
1140 ("yaffs_FileInode invalid parameters\n"));
1145 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1148 struct inode *inode;
1152 ("yaffs_get_inode for NULL super_block!!\n"));
1159 ("yaffs_get_inode for NULL object!!\n"));
1165 ("yaffs_get_inode for object %d\n", obj->objectId));
1167 inode = Y_IGET(sb, obj->objectId);
1171 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1172 /* iget also increments the inode's i_count */
1173 /* NB You can't be holding grossLock or deadlock will happen! */
1178 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1183 struct inode *inode;
1186 obj = yaffs_DentryToObject(f->f_dentry);
1190 yaffs_GrossLock(dev);
1192 inode = f->f_dentry->d_inode;
1194 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1195 ipos = inode->i_size;
1201 ("yaffs_file_write: hey obj is null!\n"));
1204 ("yaffs_file_write about to write writing %u(%x) bytes"
1205 "to object %d at %d(%x)\n",
1206 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1208 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1211 ("yaffs_file_write: %d(%x) bytes written\n",
1212 (unsigned )n,(unsigned)n));
1217 if (ipos > inode->i_size) {
1218 inode->i_size = ipos;
1219 inode->i_blocks = (ipos + 511) >> 9;
1222 ("yaffs_file_write size updated to %d bytes, "
1224 ipos, (int)(inode->i_blocks)));
1228 yaffs_GrossUnlock(dev);
1229 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1232 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1233 /* For now we just assume few parallel writes and check against a small number. */
1234 /* Todo: need to do this with a counter to handle parallel reads better */
1236 static ssize_t yaffs_hold_space(struct file *f)
1244 obj = yaffs_DentryToObject(f->f_dentry);
1248 yaffs_GrossLock(dev);
1250 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1252 yaffs_GrossUnlock(dev);
1254 return (nFreeChunks > 20) ? 1 : 0;
1257 static void yaffs_release_space(struct file *f)
1263 obj = yaffs_DentryToObject(f->f_dentry);
1267 yaffs_GrossLock(dev);
1270 yaffs_GrossUnlock(dev);
1273 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1277 struct yaffs_SearchContext *sc;
1278 struct inode *inode = f->f_dentry->d_inode;
1279 unsigned long offset, curoffs;
1283 char name[YAFFS_MAX_NAME_LENGTH + 1];
1285 obj = yaffs_DentryToObject(f->f_dentry);
1288 yaffs_GrossLock(dev);
1292 sc = yaffs_NewSearch(obj);
1298 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
1302 ("yaffs_readdir: entry . ino %d \n",
1303 (int)inode->i_ino));
1304 yaffs_GrossUnlock(dev);
1305 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
1307 yaffs_GrossLock(dev);
1313 ("yaffs_readdir: entry .. ino %d \n",
1314 (int)f->f_dentry->d_parent->d_inode->i_ino));
1315 yaffs_GrossUnlock(dev);
1316 if (filldir(dirent, "..", 2, offset,
1317 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
1319 yaffs_GrossLock(dev);
1326 /* If the directory has changed since the open or last call to
1327 readdir, rewind to after the 2 canned entries. */
1329 if (f->f_version != inode->i_version) {
1332 f->f_version = inode->i_version;
1335 while(sc->nextReturn){
1338 if (curoffs >= offset) {
1339 int this_inode = yaffs_GetObjectInode(l);
1340 int this_type = yaffs_GetObjectType(l);
1342 yaffs_GetObjectName(l, name,
1343 YAFFS_MAX_NAME_LENGTH + 1);
1345 ("yaffs_readdir: %s inode %d\n", name,
1346 yaffs_GetObjectInode(l)));
1348 yaffs_GrossUnlock(dev);
1358 yaffs_GrossLock(dev);
1363 yaffs_SearchAdvance(sc);
1367 yaffs_GrossUnlock(dev);
1369 yaffs_EndSearch(sc);
1375 * File creation. Allocate an inode, and we're done..
1378 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1381 #define YCRED(x) (x->cred)
1384 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1385 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1388 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1392 struct inode *inode;
1394 yaffs_Object *obj = NULL;
1397 yaffs_Object *parent = yaffs_InodeToObject(dir);
1399 int error = -ENOSPC;
1400 uid_t uid = YCRED(current)->fsuid;
1401 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1403 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1408 ("yaffs_mknod: parent object %d type %d\n",
1409 parent->objectId, parent->variantType));
1412 ("yaffs_mknod: could not get parent object\n"));
1416 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1418 dentry->d_name.name, mode, rdev));
1420 dev = parent->myDev;
1422 yaffs_GrossLock(dev);
1424 switch (mode & S_IFMT) {
1426 /* Special (socket, fifo, device...) */
1427 T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
1428 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1429 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1430 gid, old_encode_dev(rdev));
1432 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1436 case S_IFREG: /* file */
1437 T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
1438 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1441 case S_IFDIR: /* directory */
1443 ("yaffs_mknod: making directory\n"));
1444 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1447 case S_IFLNK: /* symlink */
1448 T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
1449 obj = NULL; /* Do we ever get here? */
1453 /* Can not call yaffs_get_inode() with gross lock held */
1454 yaffs_GrossUnlock(dev);
1457 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1458 d_instantiate(dentry, inode);
1459 update_dir_time(dir);
1461 ("yaffs_mknod created object %d count = %d\n",
1462 obj->objectId, atomic_read(&inode->i_count)));
1466 ("yaffs_mknod failed making object\n"));
1473 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1476 T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
1477 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1481 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1482 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1483 struct nameidata *n)
1485 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1488 T(YAFFS_TRACE_OS, ("yaffs_create\n"));
1489 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1492 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1499 ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1500 dentry->d_name.name));
1502 dev = yaffs_InodeToObject(dir)->myDev;
1504 yaffs_GrossLock(dev);
1506 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1508 if (retVal == YAFFS_OK) {
1509 dentry->d_inode->i_nlink--;
1511 yaffs_GrossUnlock(dev);
1512 mark_inode_dirty(dentry->d_inode);
1513 update_dir_time(dir);
1516 yaffs_GrossUnlock(dev);
1523 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1524 struct dentry *dentry)
1526 struct inode *inode = old_dentry->d_inode;
1527 yaffs_Object *obj = NULL;
1528 yaffs_Object *link = NULL;
1531 T(YAFFS_TRACE_OS, ("yaffs_link\n"));
1533 obj = yaffs_InodeToObject(inode);
1536 yaffs_GrossLock(dev);
1538 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1539 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1543 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1544 d_instantiate(dentry, old_dentry->d_inode);
1545 atomic_inc(&old_dentry->d_inode->i_count);
1547 ("yaffs_link link count %d i_count %d\n",
1548 old_dentry->d_inode->i_nlink,
1549 atomic_read(&old_dentry->d_inode->i_count)));
1552 yaffs_GrossUnlock(dev);
1555 update_dir_time(dir);
1562 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1563 const char *symname)
1567 uid_t uid = YCRED(current)->fsuid;
1568 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1570 T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
1572 dev = yaffs_InodeToObject(dir)->myDev;
1573 yaffs_GrossLock(dev);
1574 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1575 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1576 yaffs_GrossUnlock(dev);
1579 struct inode *inode;
1581 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1582 d_instantiate(dentry, inode);
1583 update_dir_time(dir);
1584 T(YAFFS_TRACE_OS, ("symlink created OK\n"));
1587 T(YAFFS_TRACE_OS, ("symlink not created\n"));
1593 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1600 obj = yaffs_DentryToObject(dentry);
1604 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_object\n"));
1605 yaffs_GrossLock(dev);
1606 yaffs_FlushFile(obj, 1, datasync);
1607 yaffs_GrossUnlock(dev);
1612 * The VFS layer already does all the dentry stuff for rename.
1614 * NB: POSIX says you can rename an object over an old object of the same name
1616 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1617 struct inode *new_dir, struct dentry *new_dentry)
1620 int retVal = YAFFS_FAIL;
1621 yaffs_Object *target;
1623 T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
1624 dev = yaffs_InodeToObject(old_dir)->myDev;
1626 yaffs_GrossLock(dev);
1628 /* Check if the target is an existing directory that is not empty. */
1629 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1630 new_dentry->d_name.name);
1634 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1635 !ylist_empty(&target->variant.directoryVariant.children)) {
1637 T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
1639 retVal = YAFFS_FAIL;
1641 /* Now does unlinking internally using shadowing mechanism */
1642 T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
1644 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1645 old_dentry->d_name.name,
1646 yaffs_InodeToObject(new_dir),
1647 new_dentry->d_name.name);
1649 yaffs_GrossUnlock(dev);
1651 if (retVal == YAFFS_OK) {
1653 new_dentry->d_inode->i_nlink--;
1654 mark_inode_dirty(new_dentry->d_inode);
1657 update_dir_time(old_dir);
1658 if(old_dir != new_dir)
1659 update_dir_time(new_dir);
1666 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1668 struct inode *inode = dentry->d_inode;
1673 ("yaffs_setattr of object %d\n",
1674 yaffs_InodeToObject(inode)->objectId));
1676 /* Fail if a requested resize >= 2GB */
1677 if (attr->ia_valid & ATTR_SIZE &&
1678 (attr->ia_size >> 31))
1682 error = inode_change_ok(inode, attr);
1686 error = inode_setattr(inode, attr);
1687 T(YAFFS_TRACE_OS,("inode_setattr called\n"));
1688 if (attr->ia_valid & ATTR_SIZE)
1689 truncate_inode_pages(&inode->i_data,attr->ia_size);
1691 dev = yaffs_InodeToObject(inode)->myDev;
1692 if (attr->ia_valid & ATTR_SIZE){
1693 T(YAFFS_TRACE_OS,("resize to %d(%x)\n",(int)(attr->ia_size),(int)(attr->ia_size)));
1695 yaffs_GrossLock(dev);
1696 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1697 if(result == YAFFS_OK) {
1702 yaffs_GrossUnlock(dev);
1707 ("yaffs_setattr done returning %d\n",error));
1712 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1713 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1715 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1716 struct super_block *sb = dentry->d_sb;
1717 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1718 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1720 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1722 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1724 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1727 T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
1729 yaffs_GrossLock(dev);
1731 buf->f_type = YAFFS_MAGIC;
1732 buf->f_bsize = sb->s_blocksize;
1733 buf->f_namelen = 255;
1735 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1736 /* Do this if chunk size is not a power of 2 */
1738 uint64_t bytesInDev;
1741 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
1742 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
1744 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1745 buf->f_blocks = bytesInDev;
1747 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1748 ((uint64_t)(dev->nDataBytesPerChunk));
1750 do_div(bytesFree, sb->s_blocksize);
1752 buf->f_bfree = bytesFree;
1754 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1757 (dev->param.endBlock - dev->param.startBlock + 1) *
1758 dev->param.nChunksPerBlock /
1759 (sb->s_blocksize / dev->nDataBytesPerChunk);
1761 yaffs_GetNumberOfFreeChunks(dev) /
1762 (sb->s_blocksize / dev->nDataBytesPerChunk);
1765 (dev->param.endBlock - dev->param.startBlock + 1) *
1766 dev->param.nChunksPerBlock *
1767 (dev->nDataBytesPerChunk / sb->s_blocksize);
1770 yaffs_GetNumberOfFreeChunks(dev) *
1771 (dev->nDataBytesPerChunk / sb->s_blocksize);
1776 buf->f_bavail = buf->f_bfree;
1778 yaffs_GrossUnlock(dev);
1784 static void yaffs_FlushInodes(struct super_block *sb)
1789 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1790 obj = yaffs_InodeToObject(iptr);
1792 T(YAFFS_TRACE_OS, ("flushing obj %d\n",obj->objectId));
1793 yaffs_FlushFile(obj,1,0);
1799 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
1801 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1805 yaffs_FlushInodes(sb);
1806 yaffs_UpdateDirtyDirectories(dev);
1807 yaffs_FlushEntireDeviceCache(dev);
1809 yaffs_CheckpointSave(dev);
1812 static int yaffs_do_sync_fs(struct super_block *sb, int do_checkpoint)
1815 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1816 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1817 ("yaffs_do_sync_fs: %s %s\n",
1818 sb->s_dirt ? "dirty" : "clean",
1819 do_checkpoint ? "with checkpoint" : "no checkpoint"));
1822 yaffs_GrossLock(dev);
1823 yaffs_FlushSuperBlock(sb,do_checkpoint);
1824 yaffs_GrossUnlock(dev);
1832 * yaffs background thread functions .
1833 * yaffs_BackgroundThread() the thread function
1834 * yaffs_BackgroundStart() launches the background thread.
1835 * yaffs_BackgroundStop() cleans up the background thread.
1838 * The thread should only run after the yaffs is initialised
1839 * The thread should be stopped before yaffs is unmounted.
1840 * The thread should not do any writing while the fs is in read only.
1843 static int yaffs_BackgroundThread(void *data)
1845 yaffs_Device *dev = (yaffs_Device *)data;
1846 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
1848 T(YAFFS_TRACE_BACKGROUND,
1849 ("yaffs_background starting for dev %p\n",
1852 while(context->bgRunning){
1853 T(YAFFS_TRACE_BACKGROUND,
1854 ("yaffs_background\n"));
1856 if(kthread_should_stop())
1858 yaffs_GrossLock(dev);
1859 yaffs_UpdateDirtyDirectories(dev);
1860 yaffs_GrossUnlock(dev);
1866 static int yaffs_BackgroundStart(yaffs_Device *dev)
1870 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
1872 context->bgRunning = 1;
1874 context->bgThread = kthread_run(yaffs_BackgroundThread,(void *)dev,"yaffs_%x",(unsigned)dev);
1876 if(IS_ERR(context->bgThread)){
1877 retval = PTR_ERR(context->bgThread);
1878 context->bgThread = NULL;
1879 context->bgRunning = 0;
1884 static void yaffs_BackgroundStop(yaffs_Device *dev)
1886 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToContext(dev);
1888 ctxt->bgRunning = 0;
1890 if( ctxt->bgThread){
1891 kthread_stop(ctxt->bgThread);
1892 ctxt->bgThread = NULL;
1896 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1897 static void yaffs_write_super(struct super_block *sb)
1899 static int yaffs_write_super(struct super_block *sb)
1903 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_write_super\n"));
1904 yaffs_do_sync_fs(sb, yaffs_auto_checkpoint >= 2);
1906 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
1912 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1913 static int yaffs_sync_fs(struct super_block *sb, int wait)
1915 static int yaffs_sync_fs(struct super_block *sb)
1918 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_fs\n"));
1920 yaffs_do_sync_fs(sb,yaffs_auto_checkpoint >= 1);
1925 #ifdef YAFFS_USE_OWN_IGET
1927 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1929 struct inode *inode;
1931 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1934 ("yaffs_iget for %lu\n", ino));
1936 inode = iget_locked(sb, ino);
1938 return ERR_PTR(-ENOMEM);
1939 if (!(inode->i_state & I_NEW))
1942 /* NB This is called as a side effect of other functions, but
1943 * we had to release the lock to prevent deadlocks, so
1944 * need to lock again.
1947 yaffs_GrossLock(dev);
1949 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1951 yaffs_FillInodeFromObject(inode, obj);
1953 yaffs_GrossUnlock(dev);
1955 unlock_new_inode(inode);
1961 static void yaffs_read_inode(struct inode *inode)
1963 /* NB This is called as a side effect of other functions, but
1964 * we had to release the lock to prevent deadlocks, so
1965 * need to lock again.
1969 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1972 ("yaffs_read_inode for %d\n", (int)inode->i_ino));
1974 yaffs_GrossLock(dev);
1976 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1978 yaffs_FillInodeFromObject(inode, obj);
1980 yaffs_GrossUnlock(dev);
1985 static YLIST_HEAD(yaffs_context_list);
1987 #if 0 /* not used */
1988 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1990 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1992 if (*flags & MS_RDONLY) {
1993 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1996 ("yaffs_remount_fs: %s: RO\n", dev->name));
1998 yaffs_GrossLock(dev);
2000 yaffs_FlushSuperBlock(sb,1);
2005 yaffs_GrossUnlock(dev);
2008 ("yaffs_remount_fs: %s: RW\n", dev->name));
2015 static void yaffs_put_super(struct super_block *sb)
2017 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2019 T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
2021 yaffs_GrossLock(dev);
2023 yaffs_FlushSuperBlock(sb,1);
2025 if (yaffs_DeviceToContext(dev)->putSuperFunc)
2026 yaffs_DeviceToContext(dev)->putSuperFunc(sb);
2028 yaffs_BackgroundStop(dev);
2030 yaffs_Deinitialise(dev);
2032 yaffs_GrossUnlock(dev);
2034 /* we assume this is protected by lock_kernel() in mount/umount */
2035 ylist_del_init(&(yaffs_DeviceToContext(dev)->contextList));
2037 if (yaffs_DeviceToContext(dev)->spareBuffer) {
2038 YFREE(yaffs_DeviceToContext(dev)->spareBuffer);
2039 yaffs_DeviceToContext(dev)->spareBuffer = NULL;
2046 static void yaffs_MTDPutSuper(struct super_block *sb)
2048 struct mtd_info *mtd = yaffs_DeviceToContext(yaffs_SuperToDevice(sb))->mtd;
2053 put_mtd_device(mtd);
2057 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2059 struct super_block *sb = yaffs_DeviceToContext(dev)->superBlock;
2061 T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
2068 int skip_checkpoint_read;
2069 int skip_checkpoint_write;
2072 int tags_ecc_overridden;
2073 int lazy_loading_enabled;
2074 int lazy_loading_overridden;
2075 int empty_lost_and_found;
2076 int empty_lost_and_found_overridden;
2079 #define MAX_OPT_LEN 30
2080 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2082 char cur_opt[MAX_OPT_LEN + 1];
2086 /* Parse through the options which is a comma seperated list */
2088 while (options_str && *options_str && !error) {
2089 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2092 while(*options_str == ',')
2095 while (*options_str && *options_str != ',') {
2096 if (p < MAX_OPT_LEN) {
2097 cur_opt[p] = *options_str;
2103 if (!strcmp(cur_opt, "inband-tags"))
2104 options->inband_tags = 1;
2105 else if (!strcmp(cur_opt, "tags-ecc-off")){
2106 options->tags_ecc_on = 0;
2107 options->tags_ecc_overridden=1;
2108 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2109 options->tags_ecc_on = 1;
2110 options->tags_ecc_overridden = 1;
2111 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2112 options->lazy_loading_enabled = 0;
2113 options->lazy_loading_overridden=1;
2114 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2115 options->lazy_loading_enabled = 1;
2116 options->lazy_loading_overridden = 1;
2117 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2118 options->empty_lost_and_found = 0;
2119 options->empty_lost_and_found_overridden=1;
2120 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2121 options->empty_lost_and_found = 1;
2122 options->empty_lost_and_found_overridden=1;
2123 } else if (!strcmp(cur_opt, "no-cache"))
2124 options->no_cache = 1;
2125 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2126 options->skip_checkpoint_read = 1;
2127 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2128 options->skip_checkpoint_write = 1;
2129 else if (!strcmp(cur_opt, "no-checkpoint")) {
2130 options->skip_checkpoint_read = 1;
2131 options->skip_checkpoint_write = 1;
2133 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2142 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2143 struct super_block *sb,
2144 void *data, int silent)
2147 struct inode *inode = NULL;
2148 struct dentry *root;
2149 yaffs_Device *dev = 0;
2150 char devname_buf[BDEVNAME_SIZE + 1];
2151 struct mtd_info *mtd;
2153 char *data_str = (char *)data;
2154 struct yaffs_LinuxContext *context = NULL;
2155 yaffs_DeviceParam *param;
2157 yaffs_options options;
2159 sb->s_magic = YAFFS_MAGIC;
2160 sb->s_op = &yaffs_super_ops;
2161 sb->s_flags |= MS_NOATIME;
2164 printk(KERN_INFO "yaffs: sb is NULL\n");
2165 else if (!sb->s_dev)
2166 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2167 else if (!yaffs_devname(sb, devname_buf))
2168 printk(KERN_INFO "yaffs: devname is NULL\n");
2170 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2172 yaffs_devname(sb, devname_buf));
2177 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2179 memset(&options, 0, sizeof(options));
2181 if (yaffs_parse_options(&options, data_str)) {
2182 /* Option parsing failed */
2187 sb->s_blocksize = PAGE_CACHE_SIZE;
2188 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2189 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2191 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2193 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2195 ("yaffs: Write verification disabled. All guarantees "
2196 "null and void\n"));
2199 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2201 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2202 yaffs_devname(sb, devname_buf)));
2204 /* Check it's an mtd device..... */
2205 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2206 return NULL; /* This isn't an mtd device */
2208 /* Get the device */
2209 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2211 T(YAFFS_TRACE_ALWAYS,
2212 ("yaffs: MTD device #%u doesn't appear to exist\n",
2216 /* Check it's NAND */
2217 if (mtd->type != MTD_NANDFLASH) {
2218 T(YAFFS_TRACE_ALWAYS,
2219 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
2223 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
2224 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
2225 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
2226 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
2227 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
2228 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
2229 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
2230 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2231 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
2232 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
2233 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2234 T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
2236 T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
2239 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2241 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2242 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
2246 /* Added NCB 26/5/2006 for completeness */
2247 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2248 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
2254 if (yaffsVersion == 2) {
2255 /* Check for version 2 style functions */
2257 !mtd->block_isbad ||
2258 !mtd->block_markbad ||
2261 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2262 !mtd->read_oob || !mtd->write_oob) {
2265 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2267 T(YAFFS_TRACE_ALWAYS,
2268 ("yaffs: MTD device does not support required "
2273 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2274 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2275 !options.inband_tags) {
2276 T(YAFFS_TRACE_ALWAYS,
2277 ("yaffs: MTD device does not have the "
2278 "right page sizes\n"));
2282 /* Check for V1 style functions */
2286 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2287 !mtd->read_oob || !mtd->write_oob) {
2290 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2292 T(YAFFS_TRACE_ALWAYS,
2293 ("yaffs: MTD device does not support required "
2298 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2299 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2300 T(YAFFS_TRACE_ALWAYS,
2301 ("yaffs: MTD device does not support have the "
2302 "right page sizes\n"));
2307 /* OK, so if we got here, we have an MTD that's NAND and looks
2308 * like it has the right capabilities
2309 * Set the yaffs_Device up for mtd
2312 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2313 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2315 if(!dev || !context ){
2325 /* Deep shit could not allocate device structure */
2326 T(YAFFS_TRACE_ALWAYS,
2327 ("yaffs_read_super: Failed trying to allocate "
2328 "yaffs_Device. \n"));
2331 memset(dev, 0, sizeof(yaffs_Device));
2332 param = &(dev->param);
2334 memset(context,0,sizeof(struct yaffs_LinuxContext));
2335 dev->context = context;
2336 YINIT_LIST_HEAD(&(context->contextList));
2338 context->superBlock = sb;
2342 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2343 sb->s_fs_info = dev;
2345 sb->u.generic_sbp = dev;
2348 yaffs_DeviceToContext(dev)->mtd = mtd;
2349 param->name = mtd->name;
2351 /* Set up the memory size parameters.... */
2353 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2355 param->startBlock = 0;
2356 param->endBlock = nBlocks - 1;
2357 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2358 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2359 param->nReservedBlocks = 5;
2360 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2361 param->inbandTags = options.inband_tags;
2363 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2364 param->disableLazyLoad = 1;
2366 if(options.lazy_loading_overridden)
2367 param->disableLazyLoad = !options.lazy_loading_enabled;
2369 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2370 param->noTagsECC = 1;
2373 param->deferDirectoryUpdate = 1;
2375 if(options.tags_ecc_overridden)
2376 param->noTagsECC = !options.tags_ecc_on;
2378 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2379 param->emptyLostAndFound = 1;
2382 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2383 param->refreshPeriod = 0;
2385 param->refreshPeriod = 10000;
2388 if(options.empty_lost_and_found_overridden)
2389 param->emptyLostAndFound = options.empty_lost_and_found;
2391 /* ... and the functions. */
2392 if (yaffsVersion == 2) {
2393 param->writeChunkWithTagsToNAND =
2394 nandmtd2_WriteChunkWithTagsToNAND;
2395 param->readChunkWithTagsFromNAND =
2396 nandmtd2_ReadChunkWithTagsFromNAND;
2397 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2398 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2399 yaffs_DeviceToContext(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2400 param->isYaffs2 = 1;
2401 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2402 param->totalBytesPerChunk = mtd->writesize;
2403 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2405 param->totalBytesPerChunk = mtd->oobblock;
2406 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2408 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2410 param->startBlock = 0;
2411 param->endBlock = nBlocks - 1;
2413 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2414 /* use the MTD interface in yaffs_mtdif1.c */
2415 param->writeChunkWithTagsToNAND =
2416 nandmtd1_WriteChunkWithTagsToNAND;
2417 param->readChunkWithTagsFromNAND =
2418 nandmtd1_ReadChunkWithTagsFromNAND;
2419 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2420 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2422 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2423 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2425 param->isYaffs2 = 0;
2427 /* ... and common functions */
2428 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2429 param->initialiseNAND = nandmtd_InitialiseNAND;
2431 yaffs_DeviceToContext(dev)->putSuperFunc = yaffs_MTDPutSuper;
2433 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2435 yaffs_DeviceToContext(dev)->superBlock= sb;
2438 #ifndef CONFIG_YAFFS_DOES_ECC
2439 param->useNANDECC = 1;
2442 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2443 param->wideTnodesDisabled = 1;
2446 param->skipCheckpointRead = options.skip_checkpoint_read;
2447 param->skipCheckpointWrite = options.skip_checkpoint_write;
2449 /* we assume this is protected by lock_kernel() in mount/umount */
2450 ylist_add_tail(&(yaffs_DeviceToContext(dev)->contextList), &yaffs_context_list);
2452 /* Directory search handling...*/
2453 YINIT_LIST_HEAD(&(yaffs_DeviceToContext(dev)->searchContexts));
2454 param->removeObjectCallback = yaffs_RemoveObjectCallback;
2456 init_MUTEX(&(yaffs_DeviceToContext(dev)->grossLock));
2458 yaffs_GrossLock(dev);
2460 err = yaffs_GutsInitialise(dev);
2463 ("yaffs_read_super: guts initialised %s\n",
2464 (err == YAFFS_OK) ? "OK" : "FAILED"));
2467 yaffs_BackgroundStart(dev);
2469 if(!context->bgThread)
2470 param->deferDirectoryUpdate = 0;
2473 /* Release lock before yaffs_get_inode() */
2474 yaffs_GrossUnlock(dev);
2476 /* Create root inode */
2477 if (err == YAFFS_OK)
2478 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2484 inode->i_op = &yaffs_dir_inode_operations;
2485 inode->i_fop = &yaffs_dir_operations;
2487 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
2489 root = d_alloc_root(inode);
2491 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
2498 sb->s_dirt = !dev->isCheckpointed;
2499 T(YAFFS_TRACE_ALWAYS,
2500 ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
2502 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
2507 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2508 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2511 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2514 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2515 static int yaffs_read_super(struct file_system_type *fs,
2516 int flags, const char *dev_name,
2517 void *data, struct vfsmount *mnt)
2520 return get_sb_bdev(fs, flags, dev_name, data,
2521 yaffs_internal_read_super_mtd, mnt);
2524 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2525 int flags, const char *dev_name,
2529 return get_sb_bdev(fs, flags, dev_name, data,
2530 yaffs_internal_read_super_mtd);
2534 static struct file_system_type yaffs_fs_type = {
2535 .owner = THIS_MODULE,
2537 .get_sb = yaffs_read_super,
2538 .kill_sb = kill_block_super,
2539 .fs_flags = FS_REQUIRES_DEV,
2542 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2545 return yaffs_internal_read_super(1, sb, data, silent);
2548 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2553 #ifdef CONFIG_YAFFS_YAFFS2
2555 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2556 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2559 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2562 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2563 static int yaffs2_read_super(struct file_system_type *fs,
2564 int flags, const char *dev_name, void *data,
2565 struct vfsmount *mnt)
2567 return get_sb_bdev(fs, flags, dev_name, data,
2568 yaffs2_internal_read_super_mtd, mnt);
2571 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2572 int flags, const char *dev_name,
2576 return get_sb_bdev(fs, flags, dev_name, data,
2577 yaffs2_internal_read_super_mtd);
2581 static struct file_system_type yaffs2_fs_type = {
2582 .owner = THIS_MODULE,
2584 .get_sb = yaffs2_read_super,
2585 .kill_sb = kill_block_super,
2586 .fs_flags = FS_REQUIRES_DEV,
2589 static struct super_block *yaffs2_read_super(struct super_block *sb,
2590 void *data, int silent)
2592 return yaffs_internal_read_super(2, sb, data, silent);
2595 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2599 #endif /* CONFIG_YAFFS_YAFFS2 */
2601 static struct proc_dir_entry *my_proc_entry;
2603 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
2605 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
2606 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
2607 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
2608 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
2609 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
2610 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
2611 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
2612 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
2613 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
2614 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
2616 buf += sprintf(buf, "\n");
2622 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
2624 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2625 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2626 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2627 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2628 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
2629 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2630 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2631 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2632 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2633 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2634 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2635 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2636 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2637 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2638 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2639 buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2640 buf += sprintf(buf, "passiveGCs......... %d\n", dev->passiveGarbageCollections);
2641 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2642 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
2643 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2644 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2645 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2646 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2647 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2648 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2649 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2650 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2651 buf += sprintf(buf, "refreshCount....... %d\n", dev->refreshCount);
2653 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2658 static int yaffs_proc_read(char *page,
2660 off_t offset, int count, int *eof, void *data)
2662 struct ylist_head *item;
2667 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2668 * We use 'offset' (*ppos) to indicate where we are in devList.
2669 * This also assumes the user has posted a read buffer large
2670 * enough to hold the complete output; but that's life in /proc.
2675 /* Print header first */
2677 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2678 "\n%s\n%s\n", yaffs_fs_c_version,
2679 yaffs_guts_c_version);
2681 buf += sprintf(buf,"\n");
2685 /* hold lock_kernel while traversing yaffs_dev_list */
2688 /* Locate and print the Nth entry. Order N-squared but N is small. */
2689 ylist_for_each(item, &yaffs_context_list) {
2690 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2691 yaffs_Device *dev = dc->dev;
2693 if (n < (step & ~1)) {
2698 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
2699 buf = yaffs_dump_dev_part0(buf, dev);
2701 buf = yaffs_dump_dev_part1(buf, dev);
2708 return buf - page < count ? buf - page : count;
2712 * Set the verbosity of the warnings and error messages.
2714 * Note that the names can only be a..z or _ with the current code.
2719 unsigned mask_bitfield;
2721 {"allocate", YAFFS_TRACE_ALLOCATE},
2722 {"always", YAFFS_TRACE_ALWAYS},
2723 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2724 {"buffers", YAFFS_TRACE_BUFFERS},
2725 {"bug", YAFFS_TRACE_BUG},
2726 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2727 {"deletion", YAFFS_TRACE_DELETION},
2728 {"erase", YAFFS_TRACE_ERASE},
2729 {"error", YAFFS_TRACE_ERROR},
2730 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2731 {"gc", YAFFS_TRACE_GC},
2732 {"mtd", YAFFS_TRACE_MTD},
2733 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2734 {"os", YAFFS_TRACE_OS},
2735 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2736 {"scan", YAFFS_TRACE_SCAN},
2737 {"tracing", YAFFS_TRACE_TRACING},
2738 {"sync", YAFFS_TRACE_SYNC},
2740 {"background", YAFFS_TRACE_BACKGROUND},
2742 {"verify", YAFFS_TRACE_VERIFY},
2743 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2744 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2745 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2747 {"write", YAFFS_TRACE_WRITE},
2748 {"all", 0xffffffff},
2753 #define MAX_MASK_NAME_LENGTH 40
2754 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2755 unsigned long count, void *data)
2757 unsigned rg = 0, mask_bitfield;
2761 char substring[MAX_MASK_NAME_LENGTH + 1];
2767 rg = yaffs_traceMask;
2769 while (!done && (pos < count)) {
2771 while ((pos < count) && isspace(buf[pos]))
2788 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2790 if (end > buf + pos) {
2791 mask_name = "numeral";
2792 len = end - (buf + pos);
2796 for (x = buf + pos, i = 0;
2797 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2798 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2800 substring[i] = '\0';
2802 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2803 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
2804 mask_name = mask_flags[i].mask_name;
2805 mask_bitfield = mask_flags[i].mask_bitfield;
2812 if (mask_name != NULL) {
2816 rg &= ~mask_bitfield;
2819 rg |= mask_bitfield;
2825 rg |= mask_bitfield;
2831 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2833 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
2835 if (rg & YAFFS_TRACE_ALWAYS) {
2836 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2838 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2839 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
2847 static int yaffs_proc_write(struct file *file, const char *buf,
2848 unsigned long count, void *data)
2850 return yaffs_proc_write_trace_options(file, buf, count, data);
2853 /* Stuff to handle installation of file systems */
2854 struct file_system_to_install {
2855 struct file_system_type *fst;
2859 static struct file_system_to_install fs_to_install[] = {
2860 {&yaffs_fs_type, 0},
2861 {&yaffs2_fs_type, 0},
2865 static int __init init_yaffs_fs(void)
2868 struct file_system_to_install *fsinst;
2870 T(YAFFS_TRACE_ALWAYS,
2871 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2873 /* Install the proc_fs entry */
2874 my_proc_entry = create_proc_entry("yaffs",
2878 if (my_proc_entry) {
2879 my_proc_entry->write_proc = yaffs_proc_write;
2880 my_proc_entry->read_proc = yaffs_proc_read;
2881 my_proc_entry->data = NULL;
2885 /* Now add the file system entries */
2887 fsinst = fs_to_install;
2889 while (fsinst->fst && !error) {
2890 error = register_filesystem(fsinst->fst);
2892 fsinst->installed = 1;
2896 /* Any errors? uninstall */
2898 fsinst = fs_to_install;
2900 while (fsinst->fst) {
2901 if (fsinst->installed) {
2902 unregister_filesystem(fsinst->fst);
2903 fsinst->installed = 0;
2912 static void __exit exit_yaffs_fs(void)
2915 struct file_system_to_install *fsinst;
2917 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2920 remove_proc_entry("yaffs", YPROC_ROOT);
2922 fsinst = fs_to_install;
2924 while (fsinst->fst) {
2925 if (fsinst->installed) {
2926 unregister_filesystem(fsinst->fst);
2927 fsinst->installed = 0;
2933 module_init(init_yaffs_fs)
2934 module_exit(exit_yaffs_fs)
2936 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2937 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2938 MODULE_LICENSE("GPL");