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.98 2010-03-10 01:22:19 charles Exp $";
36 extern const char *yaffs_guts_c_version;
38 #include <linux/version.h>
40 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
41 #define YAFFS_COMPILE_BACKGROUND
44 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
45 #include <linux/config.h>
47 #include <linux/kernel.h>
48 #include <linux/module.h>
49 #include <linux/slab.h>
50 #include <linux/init.h>
52 #include <linux/proc_fs.h>
53 #include <linux/smp_lock.h>
54 #include <linux/pagemap.h>
55 #include <linux/mtd/mtd.h>
56 #include <linux/interrupt.h>
57 #include <linux/string.h>
58 #include <linux/ctype.h>
60 #ifdef YAFFS_COMPILE_BACKGROUND
61 #include <linux/kthread.h>
62 #include <linux/delay.h>
66 #include "asm/div64.h"
71 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
73 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
74 #include <linux/statfs.h>
75 #define UnlockPage(p) unlock_page(p)
76 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
78 /* FIXME: use sb->s_id instead ? */
79 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
83 #include <linux/locks.h>
84 #define BDEVNAME_SIZE 0
85 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
87 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
88 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
94 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
95 #define YPROC_ROOT (&proc_root)
97 #define YPROC_ROOT NULL
100 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
101 #define WRITE_SIZE_STR "writesize"
102 #define WRITE_SIZE(mtd) ((mtd)->writesize)
104 #define WRITE_SIZE_STR "oobblock"
105 #define WRITE_SIZE(mtd) ((mtd)->oobblock)
108 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
109 #define YAFFS_USE_WRITE_BEGIN_END 1
111 #define YAFFS_USE_WRITE_BEGIN_END 0
114 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
115 static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
117 uint64_t result = partition_size;
118 do_div(result, block_size);
119 return (uint32_t)result;
122 #define YCALCBLOCKS(s, b) ((s)/(b))
125 #include <linux/uaccess.h>
127 #include "yportenv.h"
128 #include "yaffs_trace.h"
129 #include "yaffs_guts.h"
131 #include "yaffs_linux.h"
133 #include <linux/mtd/mtd.h>
134 #include "yaffs_mtdif.h"
135 #include "yaffs_mtdif1.h"
136 #include "yaffs_mtdif2.h"
138 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
139 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
140 unsigned int yaffs_auto_checkpoint = 1;
142 /* Module Parameters */
143 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
144 module_param(yaffs_traceMask, uint, 0644);
145 module_param(yaffs_wr_attempts, uint, 0644);
146 module_param(yaffs_auto_checkpoint, uint, 0644);
148 MODULE_PARM(yaffs_traceMask, "i");
149 MODULE_PARM(yaffs_wr_attempts, "i");
150 MODULE_PARM(yaffs_auto_checkpoint, "i");
153 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
154 /* use iget and read_inode */
155 #define Y_IGET(sb, inum) iget((sb), (inum))
156 static void yaffs_read_inode(struct inode *inode);
159 /* Call local equivalent */
160 #define YAFFS_USE_OWN_IGET
161 #define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
163 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
166 /*#define T(x) printk x */
168 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
169 #define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
171 #define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
174 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
175 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
177 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
178 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
180 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
184 #define update_dir_time(dir) do {\
185 (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
188 static void yaffs_put_super(struct super_block *sb);
190 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
192 static ssize_t yaffs_hold_space(struct file *f);
193 static void yaffs_release_space(struct file *f);
195 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
196 static int yaffs_file_flush(struct file *file, fl_owner_t id);
198 static int yaffs_file_flush(struct file *file);
201 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
204 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
206 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
207 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
208 struct nameidata *n);
209 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
210 struct nameidata *n);
212 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
213 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
215 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
216 struct dentry *dentry);
217 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
218 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
219 const char *symname);
220 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
222 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
223 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
226 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
229 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
230 struct inode *new_dir, struct dentry *new_dentry);
231 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
233 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
234 static int yaffs_sync_fs(struct super_block *sb, int wait);
235 static void yaffs_write_super(struct super_block *sb);
237 static int yaffs_sync_fs(struct super_block *sb);
238 static int yaffs_write_super(struct super_block *sb);
241 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
242 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
243 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
244 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
246 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
249 #ifdef YAFFS_HAS_PUT_INODE
250 static void yaffs_put_inode(struct inode *inode);
253 static void yaffs_delete_inode(struct inode *);
254 static void yaffs_clear_inode(struct inode *);
256 static int yaffs_readpage(struct file *file, struct page *page);
257 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
258 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
260 static int yaffs_writepage(struct page *page);
264 #if (YAFFS_USE_WRITE_BEGIN_END != 0)
265 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
266 loff_t pos, unsigned len, unsigned flags,
267 struct page **pagep, void **fsdata);
268 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
269 loff_t pos, unsigned len, unsigned copied,
270 struct page *pg, void *fsdadata);
272 static int yaffs_prepare_write(struct file *f, struct page *pg,
273 unsigned offset, unsigned to);
274 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
279 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
281 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
282 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
284 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
287 static struct address_space_operations yaffs_file_address_operations = {
288 .readpage = yaffs_readpage,
289 .writepage = yaffs_writepage,
290 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
291 .write_begin = yaffs_write_begin,
292 .write_end = yaffs_write_end,
294 .prepare_write = yaffs_prepare_write,
295 .commit_write = yaffs_commit_write,
299 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
300 static const struct file_operations yaffs_file_operations = {
301 .read = do_sync_read,
302 .write = do_sync_write,
303 .aio_read = generic_file_aio_read,
304 .aio_write = generic_file_aio_write,
305 .mmap = generic_file_mmap,
306 .flush = yaffs_file_flush,
307 .fsync = yaffs_sync_object,
308 .splice_read = generic_file_splice_read,
309 .splice_write = generic_file_splice_write,
310 .llseek = generic_file_llseek,
313 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
315 static const struct file_operations yaffs_file_operations = {
316 .read = do_sync_read,
317 .write = do_sync_write,
318 .aio_read = generic_file_aio_read,
319 .aio_write = generic_file_aio_write,
320 .mmap = generic_file_mmap,
321 .flush = yaffs_file_flush,
322 .fsync = yaffs_sync_object,
323 .sendfile = generic_file_sendfile,
328 static const struct file_operations yaffs_file_operations = {
329 .read = generic_file_read,
330 .write = generic_file_write,
331 .mmap = generic_file_mmap,
332 .flush = yaffs_file_flush,
333 .fsync = yaffs_sync_object,
334 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
335 .sendfile = generic_file_sendfile,
340 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
341 static void zero_user_segment(struct page *page, unsigned start, unsigned end)
343 void * kaddr = kmap_atomic(page, KM_USER0);
344 memset(kaddr + start, 0, end - start);
345 kunmap_atomic(kaddr, KM_USER0);
346 flush_dcache_page(page);
351 static const struct inode_operations yaffs_file_inode_operations = {
352 .setattr = yaffs_setattr,
355 static const struct inode_operations yaffs_symlink_inode_operations = {
356 .readlink = yaffs_readlink,
357 .follow_link = yaffs_follow_link,
358 .setattr = yaffs_setattr,
361 static const struct inode_operations yaffs_dir_inode_operations = {
362 .create = yaffs_create,
363 .lookup = yaffs_lookup,
365 .unlink = yaffs_unlink,
366 .symlink = yaffs_symlink,
367 .mkdir = yaffs_mkdir,
368 .rmdir = yaffs_unlink,
369 .mknod = yaffs_mknod,
370 .rename = yaffs_rename,
371 .setattr = yaffs_setattr,
374 static const struct file_operations yaffs_dir_operations = {
375 .read = generic_read_dir,
376 .readdir = yaffs_readdir,
377 .fsync = yaffs_sync_object,
380 static const struct super_operations yaffs_super_ops = {
381 .statfs = yaffs_statfs,
383 #ifndef YAFFS_USE_OWN_IGET
384 .read_inode = yaffs_read_inode,
386 #ifdef YAFFS_HAS_PUT_INODE
387 .put_inode = yaffs_put_inode,
389 .put_super = yaffs_put_super,
390 .delete_inode = yaffs_delete_inode,
391 .clear_inode = yaffs_clear_inode,
392 .sync_fs = yaffs_sync_fs,
393 .write_super = yaffs_write_super,
396 static void yaffs_GrossLock(yaffs_Device *dev)
398 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
399 down(&(yaffs_DeviceToContext(dev)->grossLock));
400 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
403 static void yaffs_GrossUnlock(yaffs_Device *dev)
405 T(LOCK_TRACE && YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
406 up(&(yaffs_DeviceToContext(dev)->grossLock));
410 /*-----------------------------------------------------------------*/
411 /* Directory search context allows us to unlock access to yaffs during
412 * filldir without causing problems with the directory being modified.
413 * This is similar to the tried and tested mechanism used in yaffs direct.
415 * A search context iterates along a doubly linked list of siblings in the
416 * directory. If the iterating object is deleted then this would corrupt
417 * the list iteration, likely causing a crash. The search context avoids
418 * this by using the removeObjectCallback to move the search context to the
419 * next object before the object is deleted.
421 * Many readdirs (and thus seach conexts) may be alive simulateously so
422 * each yaffs_Device has a list of these.
424 * A seach context lives for the duration of a readdir.
426 * All these functions must be called while yaffs is locked.
429 struct yaffs_SearchContext {
431 yaffs_Object *dirObj;
432 yaffs_Object *nextReturn;
433 struct ylist_head others;
437 * yaffs_NewSearch() creates a new search context, initialises it and
438 * adds it to the device's search context list.
440 * Called at start of readdir.
442 static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_Object *dir)
444 yaffs_Device *dev = dir->myDev;
445 struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
449 if( ylist_empty(&sc->dirObj->variant.directoryVariant.children))
450 sc->nextReturn = NULL;
452 sc->nextReturn = ylist_entry(
453 dir->variant.directoryVariant.children.next,
454 yaffs_Object,siblings);
455 YINIT_LIST_HEAD(&sc->others);
456 ylist_add(&sc->others,&(yaffs_DeviceToContext(dev)->searchContexts));
462 * yaffs_EndSearch() disposes of a search context and cleans up.
464 static void yaffs_EndSearch(struct yaffs_SearchContext * sc)
467 ylist_del(&sc->others);
473 * yaffs_SearchAdvance() moves a search context to the next object.
474 * Called when the search iterates or when an object removal causes
475 * the search context to be moved to the next object.
477 static void yaffs_SearchAdvance(struct yaffs_SearchContext *sc)
482 if( sc->nextReturn == NULL ||
483 ylist_empty(&sc->dirObj->variant.directoryVariant.children))
484 sc->nextReturn = NULL;
486 struct ylist_head *next = sc->nextReturn->siblings.next;
488 if( next == &sc->dirObj->variant.directoryVariant.children)
489 sc->nextReturn = NULL; /* end of list */
491 sc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
496 * yaffs_RemoveObjectCallback() is called when an object is unlinked.
497 * We check open search contexts and advance any which are currently
498 * on the object being iterated.
500 static void yaffs_RemoveObjectCallback(yaffs_Object *obj)
503 struct ylist_head *i;
504 struct yaffs_SearchContext *sc;
505 struct ylist_head *search_contexts = &(yaffs_DeviceToContext(obj->myDev)->searchContexts);
508 /* Iterate through the directory search contexts.
509 * If any are currently on the object being removed, then advance
510 * the search context to the next object to prevent a hanging pointer.
512 ylist_for_each(i, search_contexts) {
514 sc = ylist_entry(i, struct yaffs_SearchContext,others);
515 if(sc->nextReturn == obj)
516 yaffs_SearchAdvance(sc);
523 /*-----------------------------------------------------------------*/
525 static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
528 unsigned char *alias;
531 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
533 yaffs_GrossLock(dev);
535 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
537 yaffs_GrossUnlock(dev);
542 ret = vfs_readlink(dentry, buffer, buflen, alias);
547 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
548 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
550 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
553 unsigned char *alias;
555 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
557 yaffs_GrossLock(dev);
559 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
561 yaffs_GrossUnlock(dev);
568 ret = vfs_follow_link(nd, alias);
571 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
578 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
582 * Lookup is used to find objects in the fs
584 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
586 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
589 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
593 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
595 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
597 yaffs_GrossLock(dev);
600 ("yaffs_lookup for %d:%s\n",
601 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
603 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
604 dentry->d_name.name);
606 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
608 /* Can't hold gross lock when calling yaffs_get_inode() */
609 yaffs_GrossUnlock(dev);
613 ("yaffs_lookup found %d\n", obj->objectId));
615 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
619 ("yaffs_loookup dentry \n"));
620 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
621 * d_add even if NULL inode */
623 /*dget(dentry); // try to solve directory bug */
624 d_add(dentry, inode);
632 T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
636 /* added NCB for 2.5/6 compatability - forces add even if inode is
637 * NULL which creates dentry hash */
638 d_add(dentry, inode);
644 #ifdef YAFFS_HAS_PUT_INODE
646 /* For now put inode is just for debugging
647 * Put inode is called when the inode **structure** is put.
649 static void yaffs_put_inode(struct inode *inode)
652 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
653 atomic_read(&inode->i_count)));
658 /* clear is called to tell the fs to release any per-inode data it holds */
659 static void yaffs_clear_inode(struct inode *inode)
664 obj = yaffs_InodeToObject(inode);
667 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
668 atomic_read(&inode->i_count),
669 obj ? "object exists" : "null object"));
673 yaffs_GrossLock(dev);
675 /* Clear the association between the inode and
679 yaffs_InodeToObjectLV(inode) = NULL;
681 /* If the object freeing was deferred, then the real
683 * This should fix the inode inconsistency problem.
686 yaffs_HandleDeferedFree(obj);
688 yaffs_GrossUnlock(dev);
693 /* delete is called when the link count is zero and the inode
694 * is put (ie. nobody wants to know about it anymore, time to
696 * NB Must call clear_inode()
698 static void yaffs_delete_inode(struct inode *inode)
700 yaffs_Object *obj = yaffs_InodeToObject(inode);
704 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
705 atomic_read(&inode->i_count),
706 obj ? "object exists" : "null object"));
710 yaffs_GrossLock(dev);
711 yaffs_DeleteObject(obj);
712 yaffs_GrossUnlock(dev);
714 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
715 truncate_inode_pages(&inode->i_data, 0);
720 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
721 static int yaffs_file_flush(struct file *file, fl_owner_t id)
723 static int yaffs_file_flush(struct file *file)
726 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
728 yaffs_Device *dev = obj->myDev;
731 ("yaffs_file_flush object %d (%s)\n", obj->objectId,
732 obj->dirty ? "dirty" : "clean"));
734 yaffs_GrossLock(dev);
736 yaffs_FlushFile(obj, 1, 0);
738 yaffs_GrossUnlock(dev);
743 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
745 /* Lifted from jffs2 */
748 unsigned char *pg_buf;
753 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock at %08x, size %08x\n",
754 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
755 (unsigned)PAGE_CACHE_SIZE));
757 obj = yaffs_DentryToObject(f->f_dentry);
761 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
762 BUG_ON(!PageLocked(pg));
769 /* FIXME: Can kmap fail? */
771 yaffs_GrossLock(dev);
773 ret = yaffs_ReadDataFromFile(obj, pg_buf,
774 pg->index << PAGE_CACHE_SHIFT,
777 yaffs_GrossUnlock(dev);
783 ClearPageUptodate(pg);
790 flush_dcache_page(pg);
793 T(YAFFS_TRACE_OS, ("yaffs_readpage_nolock done\n"));
797 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
799 int ret = yaffs_readpage_nolock(f, pg);
804 static int yaffs_readpage(struct file *f, struct page *pg)
808 T(YAFFS_TRACE_OS, ("yaffs_readpage\n"));
809 ret=yaffs_readpage_unlock(f, pg);
810 T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
814 /* writepage inspired by/stolen from smbfs */
816 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
817 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
819 static int yaffs_writepage(struct page *page)
822 struct address_space *mapping = page->mapping;
824 unsigned long end_index;
833 inode = mapping->host;
836 i_size = i_size_read(inode);
838 end_index = i_size >> PAGE_CACHE_SHIFT;
840 if(page->index < end_index)
841 nBytes = PAGE_CACHE_SIZE;
843 nBytes = i_size & (PAGE_CACHE_SIZE -1);
845 if (page->index > end_index || !nBytes) {
847 ("yaffs_writepage at %08x, inode size = %08x!!!\n",
848 (unsigned)(page->index << PAGE_CACHE_SHIFT),
849 (unsigned)inode->i_size));
851 (" -> don't care!!\n"));
853 zero_user_segment(page,0,PAGE_CACHE_SIZE);
854 set_page_writeback(page);
856 end_page_writeback(page);
861 if(nBytes != PAGE_CACHE_SIZE)
862 zero_user_segment(page,nBytes,PAGE_CACHE_SIZE);
868 obj = yaffs_InodeToObject(inode);
869 yaffs_GrossLock(obj->myDev);
872 ("yaffs_writepage at %08x, size %08x\n",
873 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
875 ("writepag0: obj = %05x, ino = %05x\n",
876 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
878 nWritten = yaffs_WriteDataToFile(obj, buffer,
879 page->index << PAGE_CACHE_SHIFT, nBytes, 0);
882 ("writepag1: obj = %05x, ino = %05x\n",
883 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
885 yaffs_GrossUnlock(obj->myDev);
888 set_page_writeback(page);
890 end_page_writeback(page);
893 return (nWritten == nBytes) ? 0 : -ENOSPC;
897 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
898 static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
899 loff_t pos, unsigned len, unsigned flags,
900 struct page **pagep, void **fsdata)
902 struct page *pg = NULL;
903 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
909 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
910 pg = grab_cache_page_write_begin(mapping, index, flags);
912 pg = __grab_cache_page(mapping, index);
920 T(YAFFS_TRACE_OS, ("start yaffs_write_begin index %d(%x) uptodate %d\n",(int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
923 space_held = yaffs_hold_space(filp);
930 /* Update page if required */
932 if (!Page_Uptodate(pg))
933 ret = yaffs_readpage_nolock(filp, pg);
938 /* Happy path return */
939 T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
944 T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
946 yaffs_release_space(filp);
949 page_cache_release(pg);
956 static int yaffs_prepare_write(struct file *f, struct page *pg,
957 unsigned offset, unsigned to)
959 T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
961 if (!Page_Uptodate(pg))
962 return yaffs_readpage_nolock(f, pg);
967 #if (YAFFS_USE_WRITE_BEGIN_END > 0)
968 static int yaffs_write_end(struct file *filp, struct address_space *mapping,
969 loff_t pos, unsigned len, unsigned copied,
970 struct page *pg, void *fsdadata)
974 uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
977 addr = kva + offset_into_page;
980 ("yaffs_write_end addr %x pos %x nBytes %d\n",
984 ret = yaffs_file_write(filp, addr, copied, &pos);
988 ("yaffs_write_end not same size ret %d copied %d\n",
997 yaffs_release_space(filp);
999 page_cache_release(pg);
1004 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1009 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1010 int nBytes = to - offset;
1013 unsigned spos = pos;
1017 addr = kva + offset;
1019 saddr = (unsigned) addr;
1022 ("yaffs_commit_write addr %x pos %x nBytes %d\n",
1023 saddr, spos, nBytes));
1025 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1027 if (nWritten != nBytes) {
1029 ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
1039 ("yaffs_commit_write returning %d\n",
1040 nWritten == nBytes ? 0 : nWritten));
1042 return nWritten == nBytes ? 0 : nWritten;
1047 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
1052 /* Check mode against the variant type and attempt to repair if broken. */
1053 __u32 mode = obj->yst_mode;
1054 switch (obj->variantType) {
1055 case YAFFS_OBJECT_TYPE_FILE:
1056 if (!S_ISREG(mode)) {
1057 obj->yst_mode &= ~S_IFMT;
1058 obj->yst_mode |= S_IFREG;
1062 case YAFFS_OBJECT_TYPE_SYMLINK:
1063 if (!S_ISLNK(mode)) {
1064 obj->yst_mode &= ~S_IFMT;
1065 obj->yst_mode |= S_IFLNK;
1069 case YAFFS_OBJECT_TYPE_DIRECTORY:
1070 if (!S_ISDIR(mode)) {
1071 obj->yst_mode &= ~S_IFMT;
1072 obj->yst_mode |= S_IFDIR;
1076 case YAFFS_OBJECT_TYPE_UNKNOWN:
1077 case YAFFS_OBJECT_TYPE_HARDLINK:
1078 case YAFFS_OBJECT_TYPE_SPECIAL:
1084 inode->i_flags |= S_NOATIME;
1086 inode->i_ino = obj->objectId;
1087 inode->i_mode = obj->yst_mode;
1088 inode->i_uid = obj->yst_uid;
1089 inode->i_gid = obj->yst_gid;
1090 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1091 inode->i_blksize = inode->i_sb->s_blocksize;
1093 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1095 inode->i_rdev = old_decode_dev(obj->yst_rdev);
1096 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
1097 inode->i_atime.tv_nsec = 0;
1098 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
1099 inode->i_mtime.tv_nsec = 0;
1100 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
1101 inode->i_ctime.tv_nsec = 0;
1103 inode->i_rdev = obj->yst_rdev;
1104 inode->i_atime = obj->yst_atime;
1105 inode->i_mtime = obj->yst_mtime;
1106 inode->i_ctime = obj->yst_ctime;
1108 inode->i_size = yaffs_GetObjectFileLength(obj);
1109 inode->i_blocks = (inode->i_size + 511) >> 9;
1111 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1114 ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
1115 inode->i_mode, inode->i_uid, inode->i_gid,
1116 (int)inode->i_size, atomic_read(&inode->i_count)));
1118 switch (obj->yst_mode & S_IFMT) {
1119 default: /* fifo, device or socket */
1120 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1121 init_special_inode(inode, obj->yst_mode,
1122 old_decode_dev(obj->yst_rdev));
1124 init_special_inode(inode, obj->yst_mode,
1125 (dev_t) (obj->yst_rdev));
1128 case S_IFREG: /* file */
1129 inode->i_op = &yaffs_file_inode_operations;
1130 inode->i_fop = &yaffs_file_operations;
1131 inode->i_mapping->a_ops =
1132 &yaffs_file_address_operations;
1134 case S_IFDIR: /* directory */
1135 inode->i_op = &yaffs_dir_inode_operations;
1136 inode->i_fop = &yaffs_dir_operations;
1138 case S_IFLNK: /* symlink */
1139 inode->i_op = &yaffs_symlink_inode_operations;
1143 yaffs_InodeToObjectLV(inode) = obj;
1145 obj->myInode = inode;
1149 ("yaffs_FileInode invalid parameters\n"));
1154 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1157 struct inode *inode;
1161 ("yaffs_get_inode for NULL super_block!!\n"));
1168 ("yaffs_get_inode for NULL object!!\n"));
1174 ("yaffs_get_inode for object %d\n", obj->objectId));
1176 inode = Y_IGET(sb, obj->objectId);
1180 /* NB Side effect: iget calls back to yaffs_read_inode(). */
1181 /* iget also increments the inode's i_count */
1182 /* NB You can't be holding grossLock or deadlock will happen! */
1187 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1192 struct inode *inode;
1195 obj = yaffs_DentryToObject(f->f_dentry);
1199 yaffs_GrossLock(dev);
1201 inode = f->f_dentry->d_inode;
1203 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
1204 ipos = inode->i_size;
1210 ("yaffs_file_write: hey obj is null!\n"));
1213 ("yaffs_file_write about to write writing %u(%x) bytes"
1214 "to object %d at %d(%x)\n",
1215 (unsigned) n, (unsigned) n, obj->objectId, ipos,ipos));
1217 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
1220 ("yaffs_file_write: %d(%x) bytes written\n",
1221 (unsigned )n,(unsigned)n));
1226 if (ipos > inode->i_size) {
1227 inode->i_size = ipos;
1228 inode->i_blocks = (ipos + 511) >> 9;
1231 ("yaffs_file_write size updated to %d bytes, "
1233 ipos, (int)(inode->i_blocks)));
1237 yaffs_GrossUnlock(dev);
1238 return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
1241 /* Space holding and freeing is done to ensure we have space available for write_begin/end */
1242 /* For now we just assume few parallel writes and check against a small number. */
1243 /* Todo: need to do this with a counter to handle parallel reads better */
1245 static ssize_t yaffs_hold_space(struct file *f)
1253 obj = yaffs_DentryToObject(f->f_dentry);
1257 yaffs_GrossLock(dev);
1259 nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
1261 yaffs_GrossUnlock(dev);
1263 return (nFreeChunks > 20) ? 1 : 0;
1266 static void yaffs_release_space(struct file *f)
1272 obj = yaffs_DentryToObject(f->f_dentry);
1276 yaffs_GrossLock(dev);
1279 yaffs_GrossUnlock(dev);
1282 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
1286 struct yaffs_SearchContext *sc;
1287 struct inode *inode = f->f_dentry->d_inode;
1288 unsigned long offset, curoffs;
1292 char name[YAFFS_MAX_NAME_LENGTH + 1];
1294 obj = yaffs_DentryToObject(f->f_dentry);
1297 yaffs_GrossLock(dev);
1301 sc = yaffs_NewSearch(obj);
1307 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
1311 ("yaffs_readdir: entry . ino %d \n",
1312 (int)inode->i_ino));
1313 yaffs_GrossUnlock(dev);
1314 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
1316 yaffs_GrossLock(dev);
1322 ("yaffs_readdir: entry .. ino %d \n",
1323 (int)f->f_dentry->d_parent->d_inode->i_ino));
1324 yaffs_GrossUnlock(dev);
1325 if (filldir(dirent, "..", 2, offset,
1326 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
1328 yaffs_GrossLock(dev);
1335 /* If the directory has changed since the open or last call to
1336 readdir, rewind to after the 2 canned entries. */
1338 if (f->f_version != inode->i_version) {
1341 f->f_version = inode->i_version;
1344 while(sc->nextReturn){
1347 if (curoffs >= offset) {
1348 int this_inode = yaffs_GetObjectInode(l);
1349 int this_type = yaffs_GetObjectType(l);
1351 yaffs_GetObjectName(l, name,
1352 YAFFS_MAX_NAME_LENGTH + 1);
1354 ("yaffs_readdir: %s inode %d\n", name,
1355 yaffs_GetObjectInode(l)));
1357 yaffs_GrossUnlock(dev);
1367 yaffs_GrossLock(dev);
1372 yaffs_SearchAdvance(sc);
1376 yaffs_GrossUnlock(dev);
1378 yaffs_EndSearch(sc);
1384 * File creation. Allocate an inode, and we're done..
1387 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
1390 #define YCRED(x) (x->cred)
1393 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1394 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1397 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1401 struct inode *inode;
1403 yaffs_Object *obj = NULL;
1406 yaffs_Object *parent = yaffs_InodeToObject(dir);
1408 int error = -ENOSPC;
1409 uid_t uid = YCRED(current)->fsuid;
1410 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1412 if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1417 ("yaffs_mknod: parent object %d type %d\n",
1418 parent->objectId, parent->variantType));
1421 ("yaffs_mknod: could not get parent object\n"));
1425 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1427 dentry->d_name.name, mode, rdev));
1429 dev = parent->myDev;
1431 yaffs_GrossLock(dev);
1433 switch (mode & S_IFMT) {
1435 /* Special (socket, fifo, device...) */
1436 T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
1437 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1438 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1439 gid, old_encode_dev(rdev));
1441 obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1445 case S_IFREG: /* file */
1446 T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
1447 obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1450 case S_IFDIR: /* directory */
1452 ("yaffs_mknod: making directory\n"));
1453 obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1456 case S_IFLNK: /* symlink */
1457 T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
1458 obj = NULL; /* Do we ever get here? */
1462 /* Can not call yaffs_get_inode() with gross lock held */
1463 yaffs_GrossUnlock(dev);
1466 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1467 d_instantiate(dentry, inode);
1468 update_dir_time(dir);
1470 ("yaffs_mknod created object %d count = %d\n",
1471 obj->objectId, atomic_read(&inode->i_count)));
1475 ("yaffs_mknod failed making object\n"));
1482 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1485 T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
1486 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1490 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1491 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1492 struct nameidata *n)
1494 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1497 T(YAFFS_TRACE_OS, ("yaffs_create\n"));
1498 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1501 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1508 ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1509 dentry->d_name.name));
1511 dev = yaffs_InodeToObject(dir)->myDev;
1513 yaffs_GrossLock(dev);
1515 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1517 if (retVal == YAFFS_OK) {
1518 dentry->d_inode->i_nlink--;
1520 yaffs_GrossUnlock(dev);
1521 mark_inode_dirty(dentry->d_inode);
1522 update_dir_time(dir);
1525 yaffs_GrossUnlock(dev);
1532 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1533 struct dentry *dentry)
1535 struct inode *inode = old_dentry->d_inode;
1536 yaffs_Object *obj = NULL;
1537 yaffs_Object *link = NULL;
1540 T(YAFFS_TRACE_OS, ("yaffs_link\n"));
1542 obj = yaffs_InodeToObject(inode);
1545 yaffs_GrossLock(dev);
1547 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1548 link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1552 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1553 d_instantiate(dentry, old_dentry->d_inode);
1554 atomic_inc(&old_dentry->d_inode->i_count);
1556 ("yaffs_link link count %d i_count %d\n",
1557 old_dentry->d_inode->i_nlink,
1558 atomic_read(&old_dentry->d_inode->i_count)));
1561 yaffs_GrossUnlock(dev);
1564 update_dir_time(dir);
1571 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1572 const char *symname)
1576 uid_t uid = YCRED(current)->fsuid;
1577 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
1579 T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
1581 dev = yaffs_InodeToObject(dir)->myDev;
1582 yaffs_GrossLock(dev);
1583 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1584 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1585 yaffs_GrossUnlock(dev);
1588 struct inode *inode;
1590 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1591 d_instantiate(dentry, inode);
1592 update_dir_time(dir);
1593 T(YAFFS_TRACE_OS, ("symlink created OK\n"));
1596 T(YAFFS_TRACE_OS, ("symlink not created\n"));
1602 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1609 obj = yaffs_DentryToObject(dentry);
1613 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_object\n"));
1614 yaffs_GrossLock(dev);
1615 yaffs_FlushFile(obj, 1, datasync);
1616 yaffs_GrossUnlock(dev);
1621 * The VFS layer already does all the dentry stuff for rename.
1623 * NB: POSIX says you can rename an object over an old object of the same name
1625 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1626 struct inode *new_dir, struct dentry *new_dentry)
1629 int retVal = YAFFS_FAIL;
1630 yaffs_Object *target;
1632 T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
1633 dev = yaffs_InodeToObject(old_dir)->myDev;
1635 yaffs_GrossLock(dev);
1637 /* Check if the target is an existing directory that is not empty. */
1638 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1639 new_dentry->d_name.name);
1643 if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1644 !ylist_empty(&target->variant.directoryVariant.children)) {
1646 T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
1648 retVal = YAFFS_FAIL;
1650 /* Now does unlinking internally using shadowing mechanism */
1651 T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
1653 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1654 old_dentry->d_name.name,
1655 yaffs_InodeToObject(new_dir),
1656 new_dentry->d_name.name);
1658 yaffs_GrossUnlock(dev);
1660 if (retVal == YAFFS_OK) {
1662 new_dentry->d_inode->i_nlink--;
1663 mark_inode_dirty(new_dentry->d_inode);
1666 update_dir_time(old_dir);
1667 if(old_dir != new_dir)
1668 update_dir_time(new_dir);
1675 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1677 struct inode *inode = dentry->d_inode;
1682 ("yaffs_setattr of object %d\n",
1683 yaffs_InodeToObject(inode)->objectId));
1685 /* Fail if a requested resize >= 2GB */
1686 if (attr->ia_valid & ATTR_SIZE &&
1687 (attr->ia_size >> 31))
1691 error = inode_change_ok(inode, attr);
1695 error = inode_setattr(inode, attr);
1696 T(YAFFS_TRACE_OS,("inode_setattr called\n"));
1697 if (attr->ia_valid & ATTR_SIZE)
1698 truncate_inode_pages(&inode->i_data,attr->ia_size);
1700 dev = yaffs_InodeToObject(inode)->myDev;
1701 if (attr->ia_valid & ATTR_SIZE){
1702 T(YAFFS_TRACE_OS,("resize to %d(%x)\n",(int)(attr->ia_size),(int)(attr->ia_size)));
1704 yaffs_GrossLock(dev);
1705 result = yaffs_SetAttributes(yaffs_InodeToObject(inode), attr);
1706 if(result == YAFFS_OK) {
1711 yaffs_GrossUnlock(dev);
1716 ("yaffs_setattr done returning %d\n",error));
1721 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1722 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1724 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1725 struct super_block *sb = dentry->d_sb;
1726 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1727 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1729 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1731 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1733 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1736 T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
1738 yaffs_GrossLock(dev);
1740 buf->f_type = YAFFS_MAGIC;
1741 buf->f_bsize = sb->s_blocksize;
1742 buf->f_namelen = 255;
1744 if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
1745 /* Do this if chunk size is not a power of 2 */
1747 uint64_t bytesInDev;
1750 bytesInDev = ((uint64_t)((dev->param.endBlock - dev->param.startBlock + 1))) *
1751 ((uint64_t)(dev->param.nChunksPerBlock * dev->nDataBytesPerChunk));
1753 do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1754 buf->f_blocks = bytesInDev;
1756 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1757 ((uint64_t)(dev->nDataBytesPerChunk));
1759 do_div(bytesFree, sb->s_blocksize);
1761 buf->f_bfree = bytesFree;
1763 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1766 (dev->param.endBlock - dev->param.startBlock + 1) *
1767 dev->param.nChunksPerBlock /
1768 (sb->s_blocksize / dev->nDataBytesPerChunk);
1770 yaffs_GetNumberOfFreeChunks(dev) /
1771 (sb->s_blocksize / dev->nDataBytesPerChunk);
1774 (dev->param.endBlock - dev->param.startBlock + 1) *
1775 dev->param.nChunksPerBlock *
1776 (dev->nDataBytesPerChunk / sb->s_blocksize);
1779 yaffs_GetNumberOfFreeChunks(dev) *
1780 (dev->nDataBytesPerChunk / sb->s_blocksize);
1785 buf->f_bavail = buf->f_bfree;
1787 yaffs_GrossUnlock(dev);
1793 static void yaffs_FlushInodes(struct super_block *sb)
1798 list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
1799 obj = yaffs_InodeToObject(iptr);
1801 T(YAFFS_TRACE_OS, ("flushing obj %d\n",obj->objectId));
1802 yaffs_FlushFile(obj,1,0);
1808 static void yaffs_FlushSuperBlock(struct super_block *sb, int do_checkpoint)
1810 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1814 yaffs_FlushInodes(sb);
1815 yaffs_UpdateDirtyDirectories(dev);
1816 yaffs_FlushEntireDeviceCache(dev);
1818 yaffs_CheckpointSave(dev);
1821 static int yaffs_do_sync_fs(struct super_block *sb, int do_checkpoint)
1824 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1825 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
1826 ("yaffs_do_sync_fs: %s %s\n",
1827 sb->s_dirt ? "dirty" : "clean",
1828 do_checkpoint ? "with checkpoint" : "no checkpoint"));
1831 yaffs_GrossLock(dev);
1832 yaffs_FlushSuperBlock(sb,do_checkpoint);
1833 yaffs_GrossUnlock(dev);
1841 * yaffs background thread functions .
1842 * yaffs_BackgroundThread() the thread function
1843 * yaffs_BackgroundStart() launches the background thread.
1844 * yaffs_BackgroundStop() cleans up the background thread.
1847 * The thread should only run after the yaffs is initialised
1848 * The thread should be stopped before yaffs is unmounted.
1849 * The thread should not do any writing while the fs is in read only.
1852 #ifdef YAFFS_COMPILE_BACKGROUND
1853 static int yaffs_BackgroundThread(void *data)
1855 yaffs_Device *dev = (yaffs_Device *)data;
1856 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
1858 T(YAFFS_TRACE_BACKGROUND,
1859 ("yaffs_background starting for dev %p\n",
1862 while(context->bgRunning){
1863 T(YAFFS_TRACE_BACKGROUND,
1864 ("yaffs_background\n"));
1866 if(kthread_should_stop())
1868 yaffs_GrossLock(dev);
1869 yaffs_UpdateDirtyDirectories(dev);
1870 yaffs_GrossUnlock(dev);
1876 static int yaffs_BackgroundStart(yaffs_Device *dev)
1880 struct yaffs_LinuxContext *context = yaffs_DeviceToContext(dev);
1882 context->bgRunning = 1;
1884 context->bgThread = kthread_run(yaffs_BackgroundThread,(void *)dev,"yaffs_%x",(unsigned)dev);
1886 if(IS_ERR(context->bgThread)){
1887 retval = PTR_ERR(context->bgThread);
1888 context->bgThread = NULL;
1889 context->bgRunning = 0;
1894 static void yaffs_BackgroundStop(yaffs_Device *dev)
1896 struct yaffs_LinuxContext *ctxt = yaffs_DeviceToContext(dev);
1898 ctxt->bgRunning = 0;
1900 if( ctxt->bgThread){
1901 kthread_stop(ctxt->bgThread);
1902 ctxt->bgThread = NULL;
1906 static int yaffs_BackgroundThread(void *data)
1911 static int yaffs_BackgroundStart(yaffs_Device *dev)
1916 static void yaffs_BackgroundStop(yaffs_Device *dev)
1922 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1923 static void yaffs_write_super(struct super_block *sb)
1925 static int yaffs_write_super(struct super_block *sb)
1929 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_write_super\n"));
1930 yaffs_do_sync_fs(sb, yaffs_auto_checkpoint >= 2);
1932 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
1938 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1939 static int yaffs_sync_fs(struct super_block *sb, int wait)
1941 static int yaffs_sync_fs(struct super_block *sb)
1944 T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, ("yaffs_sync_fs\n"));
1946 yaffs_do_sync_fs(sb,yaffs_auto_checkpoint >= 1);
1951 #ifdef YAFFS_USE_OWN_IGET
1953 static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
1955 struct inode *inode;
1957 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1960 ("yaffs_iget for %lu\n", ino));
1962 inode = iget_locked(sb, ino);
1964 return ERR_PTR(-ENOMEM);
1965 if (!(inode->i_state & I_NEW))
1968 /* NB This is called as a side effect of other functions, but
1969 * we had to release the lock to prevent deadlocks, so
1970 * need to lock again.
1973 yaffs_GrossLock(dev);
1975 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1977 yaffs_FillInodeFromObject(inode, obj);
1979 yaffs_GrossUnlock(dev);
1981 unlock_new_inode(inode);
1987 static void yaffs_read_inode(struct inode *inode)
1989 /* NB This is called as a side effect of other functions, but
1990 * we had to release the lock to prevent deadlocks, so
1991 * need to lock again.
1995 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1998 ("yaffs_read_inode for %d\n", (int)inode->i_ino));
2000 yaffs_GrossLock(dev);
2002 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2004 yaffs_FillInodeFromObject(inode, obj);
2006 yaffs_GrossUnlock(dev);
2011 static YLIST_HEAD(yaffs_context_list);
2013 #if 0 /* not used */
2014 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2016 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2018 if (*flags & MS_RDONLY) {
2019 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2022 ("yaffs_remount_fs: %s: RO\n", dev->name));
2024 yaffs_GrossLock(dev);
2026 yaffs_FlushSuperBlock(sb,1);
2031 yaffs_GrossUnlock(dev);
2034 ("yaffs_remount_fs: %s: RW\n", dev->name));
2041 static void yaffs_put_super(struct super_block *sb)
2043 yaffs_Device *dev = yaffs_SuperToDevice(sb);
2045 T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
2047 yaffs_GrossLock(dev);
2049 yaffs_FlushSuperBlock(sb,1);
2051 if (yaffs_DeviceToContext(dev)->putSuperFunc)
2052 yaffs_DeviceToContext(dev)->putSuperFunc(sb);
2054 yaffs_BackgroundStop(dev);
2056 yaffs_Deinitialise(dev);
2058 yaffs_GrossUnlock(dev);
2060 /* we assume this is protected by lock_kernel() in mount/umount */
2061 ylist_del_init(&(yaffs_DeviceToContext(dev)->contextList));
2063 if (yaffs_DeviceToContext(dev)->spareBuffer) {
2064 YFREE(yaffs_DeviceToContext(dev)->spareBuffer);
2065 yaffs_DeviceToContext(dev)->spareBuffer = NULL;
2072 static void yaffs_MTDPutSuper(struct super_block *sb)
2074 struct mtd_info *mtd = yaffs_DeviceToContext(yaffs_SuperToDevice(sb))->mtd;
2079 put_mtd_device(mtd);
2083 static void yaffs_MarkSuperBlockDirty(yaffs_Device *dev)
2085 struct super_block *sb = yaffs_DeviceToContext(dev)->superBlock;
2087 T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
2094 int skip_checkpoint_read;
2095 int skip_checkpoint_write;
2098 int tags_ecc_overridden;
2099 int lazy_loading_enabled;
2100 int lazy_loading_overridden;
2101 int empty_lost_and_found;
2102 int empty_lost_and_found_overridden;
2105 #define MAX_OPT_LEN 30
2106 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2108 char cur_opt[MAX_OPT_LEN + 1];
2112 /* Parse through the options which is a comma seperated list */
2114 while (options_str && *options_str && !error) {
2115 memset(cur_opt, 0, MAX_OPT_LEN + 1);
2118 while(*options_str == ',')
2121 while (*options_str && *options_str != ',') {
2122 if (p < MAX_OPT_LEN) {
2123 cur_opt[p] = *options_str;
2129 if (!strcmp(cur_opt, "inband-tags"))
2130 options->inband_tags = 1;
2131 else if (!strcmp(cur_opt, "tags-ecc-off")){
2132 options->tags_ecc_on = 0;
2133 options->tags_ecc_overridden=1;
2134 } else if (!strcmp(cur_opt, "tags-ecc-on")){
2135 options->tags_ecc_on = 1;
2136 options->tags_ecc_overridden = 1;
2137 } else if (!strcmp(cur_opt, "lazy-loading-off")){
2138 options->lazy_loading_enabled = 0;
2139 options->lazy_loading_overridden=1;
2140 } else if (!strcmp(cur_opt, "lazy-loading-on")){
2141 options->lazy_loading_enabled = 1;
2142 options->lazy_loading_overridden = 1;
2143 } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
2144 options->empty_lost_and_found = 0;
2145 options->empty_lost_and_found_overridden=1;
2146 } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
2147 options->empty_lost_and_found = 1;
2148 options->empty_lost_and_found_overridden=1;
2149 } else if (!strcmp(cur_opt, "no-cache"))
2150 options->no_cache = 1;
2151 else if (!strcmp(cur_opt, "no-checkpoint-read"))
2152 options->skip_checkpoint_read = 1;
2153 else if (!strcmp(cur_opt, "no-checkpoint-write"))
2154 options->skip_checkpoint_write = 1;
2155 else if (!strcmp(cur_opt, "no-checkpoint")) {
2156 options->skip_checkpoint_read = 1;
2157 options->skip_checkpoint_write = 1;
2159 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
2168 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2169 struct super_block *sb,
2170 void *data, int silent)
2173 struct inode *inode = NULL;
2174 struct dentry *root;
2175 yaffs_Device *dev = 0;
2176 char devname_buf[BDEVNAME_SIZE + 1];
2177 struct mtd_info *mtd;
2179 char *data_str = (char *)data;
2180 struct yaffs_LinuxContext *context = NULL;
2181 yaffs_DeviceParam *param;
2183 yaffs_options options;
2185 sb->s_magic = YAFFS_MAGIC;
2186 sb->s_op = &yaffs_super_ops;
2187 sb->s_flags |= MS_NOATIME;
2190 printk(KERN_INFO "yaffs: sb is NULL\n");
2191 else if (!sb->s_dev)
2192 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2193 else if (!yaffs_devname(sb, devname_buf))
2194 printk(KERN_INFO "yaffs: devname is NULL\n");
2196 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2198 yaffs_devname(sb, devname_buf));
2203 printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
2205 memset(&options, 0, sizeof(options));
2207 if (yaffs_parse_options(&options, data_str)) {
2208 /* Option parsing failed */
2213 sb->s_blocksize = PAGE_CACHE_SIZE;
2214 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2215 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2217 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2219 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2221 ("yaffs: Write verification disabled. All guarantees "
2222 "null and void\n"));
2225 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2227 MAJOR(sb->s_dev), MINOR(sb->s_dev),
2228 yaffs_devname(sb, devname_buf)));
2230 /* Check it's an mtd device..... */
2231 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
2232 return NULL; /* This isn't an mtd device */
2234 /* Get the device */
2235 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2237 T(YAFFS_TRACE_ALWAYS,
2238 ("yaffs: MTD device #%u doesn't appear to exist\n",
2242 /* Check it's NAND */
2243 if (mtd->type != MTD_NANDFLASH) {
2244 T(YAFFS_TRACE_ALWAYS,
2245 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
2249 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
2250 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
2251 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
2252 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
2253 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
2254 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
2255 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
2256 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
2257 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
2258 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
2259 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2260 T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
2262 T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
2265 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
2267 if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
2268 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
2272 /* Added NCB 26/5/2006 for completeness */
2273 if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
2274 T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
2280 if (yaffsVersion == 2) {
2281 /* Check for version 2 style functions */
2283 !mtd->block_isbad ||
2284 !mtd->block_markbad ||
2287 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2288 !mtd->read_oob || !mtd->write_oob) {
2291 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2293 T(YAFFS_TRACE_ALWAYS,
2294 ("yaffs: MTD device does not support required "
2299 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2300 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
2301 !options.inband_tags) {
2302 T(YAFFS_TRACE_ALWAYS,
2303 ("yaffs: MTD device does not have the "
2304 "right page sizes\n"));
2308 /* Check for V1 style functions */
2312 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2313 !mtd->read_oob || !mtd->write_oob) {
2316 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2318 T(YAFFS_TRACE_ALWAYS,
2319 ("yaffs: MTD device does not support required "
2324 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
2325 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2326 T(YAFFS_TRACE_ALWAYS,
2327 ("yaffs: MTD device does not support have the "
2328 "right page sizes\n"));
2333 /* OK, so if we got here, we have an MTD that's NAND and looks
2334 * like it has the right capabilities
2335 * Set the yaffs_Device up for mtd
2338 dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2339 context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
2341 if(!dev || !context ){
2351 /* Deep shit could not allocate device structure */
2352 T(YAFFS_TRACE_ALWAYS,
2353 ("yaffs_read_super: Failed trying to allocate "
2354 "yaffs_Device. \n"));
2357 memset(dev, 0, sizeof(yaffs_Device));
2358 param = &(dev->param);
2360 memset(context,0,sizeof(struct yaffs_LinuxContext));
2361 dev->context = context;
2362 YINIT_LIST_HEAD(&(context->contextList));
2364 context->superBlock = sb;
2368 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2369 sb->s_fs_info = dev;
2371 sb->u.generic_sbp = dev;
2374 yaffs_DeviceToContext(dev)->mtd = mtd;
2375 param->name = mtd->name;
2377 /* Set up the memory size parameters.... */
2379 nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
2381 param->startBlock = 0;
2382 param->endBlock = nBlocks - 1;
2383 param->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2384 param->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2385 param->nReservedBlocks = 5;
2386 param->nShortOpCaches = (options.no_cache) ? 0 : 10;
2387 param->inbandTags = options.inband_tags;
2389 #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
2390 param->disableLazyLoad = 1;
2392 if(options.lazy_loading_overridden)
2393 param->disableLazyLoad = !options.lazy_loading_enabled;
2395 #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
2396 param->noTagsECC = 1;
2399 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
2401 param->deferDirectoryUpdate = 1;
2404 if(options.tags_ecc_overridden)
2405 param->noTagsECC = !options.tags_ecc_on;
2407 #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
2408 param->emptyLostAndFound = 1;
2411 #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
2412 param->refreshPeriod = 0;
2414 param->refreshPeriod = 10000;
2417 if(options.empty_lost_and_found_overridden)
2418 param->emptyLostAndFound = options.empty_lost_and_found;
2420 /* ... and the functions. */
2421 if (yaffsVersion == 2) {
2422 param->writeChunkWithTagsToNAND =
2423 nandmtd2_WriteChunkWithTagsToNAND;
2424 param->readChunkWithTagsFromNAND =
2425 nandmtd2_ReadChunkWithTagsFromNAND;
2426 param->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2427 param->queryNANDBlock = nandmtd2_QueryNANDBlock;
2428 yaffs_DeviceToContext(dev)->spareBuffer = YMALLOC(mtd->oobsize);
2429 param->isYaffs2 = 1;
2430 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2431 param->totalBytesPerChunk = mtd->writesize;
2432 param->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2434 param->totalBytesPerChunk = mtd->oobblock;
2435 param->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2437 nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
2439 param->startBlock = 0;
2440 param->endBlock = nBlocks - 1;
2442 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2443 /* use the MTD interface in yaffs_mtdif1.c */
2444 param->writeChunkWithTagsToNAND =
2445 nandmtd1_WriteChunkWithTagsToNAND;
2446 param->readChunkWithTagsFromNAND =
2447 nandmtd1_ReadChunkWithTagsFromNAND;
2448 param->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
2449 param->queryNANDBlock = nandmtd1_QueryNANDBlock;
2451 param->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2452 param->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2454 param->isYaffs2 = 0;
2456 /* ... and common functions */
2457 param->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2458 param->initialiseNAND = nandmtd_InitialiseNAND;
2460 yaffs_DeviceToContext(dev)->putSuperFunc = yaffs_MTDPutSuper;
2462 param->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2464 yaffs_DeviceToContext(dev)->superBlock= sb;
2467 #ifndef CONFIG_YAFFS_DOES_ECC
2468 param->useNANDECC = 1;
2471 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2472 param->wideTnodesDisabled = 1;
2475 param->skipCheckpointRead = options.skip_checkpoint_read;
2476 param->skipCheckpointWrite = options.skip_checkpoint_write;
2478 /* we assume this is protected by lock_kernel() in mount/umount */
2479 ylist_add_tail(&(yaffs_DeviceToContext(dev)->contextList), &yaffs_context_list);
2481 /* Directory search handling...*/
2482 YINIT_LIST_HEAD(&(yaffs_DeviceToContext(dev)->searchContexts));
2483 param->removeObjectCallback = yaffs_RemoveObjectCallback;
2485 init_MUTEX(&(yaffs_DeviceToContext(dev)->grossLock));
2487 yaffs_GrossLock(dev);
2489 err = yaffs_GutsInitialise(dev);
2492 ("yaffs_read_super: guts initialised %s\n",
2493 (err == YAFFS_OK) ? "OK" : "FAILED"));
2496 yaffs_BackgroundStart(dev);
2498 if(!context->bgThread)
2499 param->deferDirectoryUpdate = 0;
2502 /* Release lock before yaffs_get_inode() */
2503 yaffs_GrossUnlock(dev);
2505 /* Create root inode */
2506 if (err == YAFFS_OK)
2507 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
2513 inode->i_op = &yaffs_dir_inode_operations;
2514 inode->i_fop = &yaffs_dir_operations;
2516 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
2518 root = d_alloc_root(inode);
2520 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
2527 sb->s_dirt = !dev->isCheckpointed;
2528 T(YAFFS_TRACE_ALWAYS,
2529 ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
2531 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
2536 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2537 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2540 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2543 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2544 static int yaffs_read_super(struct file_system_type *fs,
2545 int flags, const char *dev_name,
2546 void *data, struct vfsmount *mnt)
2549 return get_sb_bdev(fs, flags, dev_name, data,
2550 yaffs_internal_read_super_mtd, mnt);
2553 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2554 int flags, const char *dev_name,
2558 return get_sb_bdev(fs, flags, dev_name, data,
2559 yaffs_internal_read_super_mtd);
2563 static struct file_system_type yaffs_fs_type = {
2564 .owner = THIS_MODULE,
2566 .get_sb = yaffs_read_super,
2567 .kill_sb = kill_block_super,
2568 .fs_flags = FS_REQUIRES_DEV,
2571 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2574 return yaffs_internal_read_super(1, sb, data, silent);
2577 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2582 #ifdef CONFIG_YAFFS_YAFFS2
2584 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2585 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2588 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2591 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2592 static int yaffs2_read_super(struct file_system_type *fs,
2593 int flags, const char *dev_name, void *data,
2594 struct vfsmount *mnt)
2596 return get_sb_bdev(fs, flags, dev_name, data,
2597 yaffs2_internal_read_super_mtd, mnt);
2600 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2601 int flags, const char *dev_name,
2605 return get_sb_bdev(fs, flags, dev_name, data,
2606 yaffs2_internal_read_super_mtd);
2610 static struct file_system_type yaffs2_fs_type = {
2611 .owner = THIS_MODULE,
2613 .get_sb = yaffs2_read_super,
2614 .kill_sb = kill_block_super,
2615 .fs_flags = FS_REQUIRES_DEV,
2618 static struct super_block *yaffs2_read_super(struct super_block *sb,
2619 void *data, int silent)
2621 return yaffs_internal_read_super(2, sb, data, silent);
2624 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2628 #endif /* CONFIG_YAFFS_YAFFS2 */
2630 static struct proc_dir_entry *my_proc_entry;
2632 static char *yaffs_dump_dev_part0(char *buf, yaffs_Device * dev)
2634 buf += sprintf(buf, "startBlock......... %d\n", dev->param.startBlock);
2635 buf += sprintf(buf, "endBlock........... %d\n", dev->param.endBlock);
2636 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->param.totalBytesPerChunk);
2637 buf += sprintf(buf, "useNANDECC......... %d\n", dev->param.useNANDECC);
2638 buf += sprintf(buf, "noTagsECC.......... %d\n", dev->param.noTagsECC);
2639 buf += sprintf(buf, "isYaffs2........... %d\n", dev->param.isYaffs2);
2640 buf += sprintf(buf, "inbandTags......... %d\n", dev->param.inbandTags);
2641 buf += sprintf(buf, "emptyLostAndFound.. %d\n", dev->param.emptyLostAndFound);
2642 buf += sprintf(buf, "disableLazyLoad.... %d\n", dev->param.disableLazyLoad);
2643 buf += sprintf(buf, "refreshPeriod...... %d\n", dev->param.refreshPeriod);
2645 buf += sprintf(buf, "\n");
2651 static char *yaffs_dump_dev_part1(char *buf, yaffs_Device * dev)
2653 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2654 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2655 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2656 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2657 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->param.nReservedBlocks);
2658 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2659 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2660 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2661 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2662 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2663 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2664 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2665 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2666 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2667 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2668 buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2669 buf += sprintf(buf, "passiveGCs......... %d\n", dev->passiveGarbageCollections);
2670 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2671 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->param.nShortOpCaches);
2672 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2673 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2674 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2675 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2676 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2677 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2678 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2679 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2680 buf += sprintf(buf, "refreshCount....... %d\n", dev->refreshCount);
2682 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2687 static int yaffs_proc_read(char *page,
2689 off_t offset, int count, int *eof, void *data)
2691 struct ylist_head *item;
2696 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2697 * We use 'offset' (*ppos) to indicate where we are in devList.
2698 * This also assumes the user has posted a read buffer large
2699 * enough to hold the complete output; but that's life in /proc.
2704 /* Print header first */
2706 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2707 "\n%s\n%s\n", yaffs_fs_c_version,
2708 yaffs_guts_c_version);
2710 buf += sprintf(buf,"\n");
2714 /* hold lock_kernel while traversing yaffs_dev_list */
2717 /* Locate and print the Nth entry. Order N-squared but N is small. */
2718 ylist_for_each(item, &yaffs_context_list) {
2719 struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
2720 yaffs_Device *dev = dc->dev;
2722 if (n < (step & ~1)) {
2727 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
2728 buf = yaffs_dump_dev_part0(buf, dev);
2730 buf = yaffs_dump_dev_part1(buf, dev);
2737 return buf - page < count ? buf - page : count;
2741 * Set the verbosity of the warnings and error messages.
2743 * Note that the names can only be a..z or _ with the current code.
2748 unsigned mask_bitfield;
2750 {"allocate", YAFFS_TRACE_ALLOCATE},
2751 {"always", YAFFS_TRACE_ALWAYS},
2752 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2753 {"buffers", YAFFS_TRACE_BUFFERS},
2754 {"bug", YAFFS_TRACE_BUG},
2755 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2756 {"deletion", YAFFS_TRACE_DELETION},
2757 {"erase", YAFFS_TRACE_ERASE},
2758 {"error", YAFFS_TRACE_ERROR},
2759 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2760 {"gc", YAFFS_TRACE_GC},
2761 {"mtd", YAFFS_TRACE_MTD},
2762 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2763 {"os", YAFFS_TRACE_OS},
2764 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2765 {"scan", YAFFS_TRACE_SCAN},
2766 {"tracing", YAFFS_TRACE_TRACING},
2767 {"sync", YAFFS_TRACE_SYNC},
2769 {"background", YAFFS_TRACE_BACKGROUND},
2771 {"verify", YAFFS_TRACE_VERIFY},
2772 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2773 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2774 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2776 {"write", YAFFS_TRACE_WRITE},
2777 {"all", 0xffffffff},
2782 #define MAX_MASK_NAME_LENGTH 40
2783 static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
2784 unsigned long count, void *data)
2786 unsigned rg = 0, mask_bitfield;
2790 char substring[MAX_MASK_NAME_LENGTH + 1];
2796 rg = yaffs_traceMask;
2798 while (!done && (pos < count)) {
2800 while ((pos < count) && isspace(buf[pos]))
2817 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2819 if (end > buf + pos) {
2820 mask_name = "numeral";
2821 len = end - (buf + pos);
2825 for (x = buf + pos, i = 0;
2826 (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
2827 i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2829 substring[i] = '\0';
2831 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2832 if (strcmp(substring, mask_flags[i].mask_name) == 0) {
2833 mask_name = mask_flags[i].mask_name;
2834 mask_bitfield = mask_flags[i].mask_bitfield;
2841 if (mask_name != NULL) {
2845 rg &= ~mask_bitfield;
2848 rg |= mask_bitfield;
2854 rg |= mask_bitfield;
2860 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2862 printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
2864 if (rg & YAFFS_TRACE_ALWAYS) {
2865 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2867 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2868 printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
2876 static int yaffs_proc_write(struct file *file, const char *buf,
2877 unsigned long count, void *data)
2879 return yaffs_proc_write_trace_options(file, buf, count, data);
2882 /* Stuff to handle installation of file systems */
2883 struct file_system_to_install {
2884 struct file_system_type *fst;
2888 static struct file_system_to_install fs_to_install[] = {
2889 {&yaffs_fs_type, 0},
2890 {&yaffs2_fs_type, 0},
2894 static int __init init_yaffs_fs(void)
2897 struct file_system_to_install *fsinst;
2899 T(YAFFS_TRACE_ALWAYS,
2900 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2902 /* Install the proc_fs entry */
2903 my_proc_entry = create_proc_entry("yaffs",
2907 if (my_proc_entry) {
2908 my_proc_entry->write_proc = yaffs_proc_write;
2909 my_proc_entry->read_proc = yaffs_proc_read;
2910 my_proc_entry->data = NULL;
2914 /* Now add the file system entries */
2916 fsinst = fs_to_install;
2918 while (fsinst->fst && !error) {
2919 error = register_filesystem(fsinst->fst);
2921 fsinst->installed = 1;
2925 /* Any errors? uninstall */
2927 fsinst = fs_to_install;
2929 while (fsinst->fst) {
2930 if (fsinst->installed) {
2931 unregister_filesystem(fsinst->fst);
2932 fsinst->installed = 0;
2941 static void __exit exit_yaffs_fs(void)
2944 struct file_system_to_install *fsinst;
2946 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2949 remove_proc_entry("yaffs", YPROC_ROOT);
2951 fsinst = fs_to_install;
2953 while (fsinst->fst) {
2954 if (fsinst->installed) {
2955 unregister_filesystem(fsinst->fst);
2956 fsinst->installed = 0;
2962 module_init(init_yaffs_fs)
2963 module_exit(exit_yaffs_fs)
2965 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2966 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2967 MODULE_LICENSE("GPL");