2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 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.70 2009-01-18 21:50:37 charles Exp $";
36 extern const char *yaffs_guts_c_version;
38 #include <linux/version.h>
39 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
40 #include <linux/config.h>
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/init.h>
47 #include <linux/proc_fs.h>
48 #include <linux/smp_lock.h>
49 #include <linux/pagemap.h>
50 #include <linux/mtd/mtd.h>
51 #include <linux/interrupt.h>
52 #include <linux/string.h>
53 #include <linux/ctype.h>
55 #include "asm/div64.h"
57 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
59 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
60 #include <asm/statfs.h>
61 #define UnlockPage(p) unlock_page(p)
62 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
64 /* FIXME: use sb->s_id instead ? */
65 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
69 #include <linux/locks.h>
70 #define BDEVNAME_SIZE 0
71 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
73 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
74 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
80 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
81 #define YPROC_ROOT &proc_root
83 #define YPROC_ROOT NULL
86 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
87 #define WRITE_SIZE_STR "writesize"
88 #define WRITE_SIZE(mtd) (mtd)->writesize
90 #define WRITE_SIZE_STR "oobblock"
91 #define WRITE_SIZE(mtd) (mtd)->oobblock
94 #include <asm/uaccess.h>
97 #include "yaffs_guts.h"
99 #include <linux/mtd/mtd.h>
100 #include "yaffs_mtdif.h"
101 #include "yaffs_mtdif1.h"
102 #include "yaffs_mtdif2.h"
104 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
105 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
106 unsigned int yaffs_auto_checkpoint = 1;
108 /* Module Parameters */
109 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
110 module_param(yaffs_traceMask,uint,0644);
111 module_param(yaffs_wr_attempts,uint,0644);
112 module_param(yaffs_auto_checkpoint,uint,0644);
114 MODULE_PARM(yaffs_traceMask,"i");
115 MODULE_PARM(yaffs_wr_attempts,"i");
116 MODULE_PARM(yaffs_auto_checkpoint,"i");
119 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
120 /* use iget and read_inode */
121 #define Y_IGET(sb,inum) iget((sb),(inum))
122 static void yaffs_read_inode(struct inode *inode);
125 /* Call local equivalent */
126 #define YAFFS_USE_OWN_IGET
127 #define Y_IGET(sb,inum) yaffs_iget((sb),(inum))
129 static struct inode * yaffs_iget(struct super_block *sb, unsigned long ino);
132 /*#define T(x) printk x */
134 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
135 #define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
137 #define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
140 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
141 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
143 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
144 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
146 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
149 static void yaffs_put_super(struct super_block *sb);
151 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
154 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
155 static int yaffs_file_flush(struct file *file, fl_owner_t id);
157 static int yaffs_file_flush(struct file *file);
160 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
163 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
165 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
166 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
167 struct nameidata *n);
168 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
169 struct nameidata *n);
171 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
172 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
174 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
175 struct dentry *dentry);
176 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
177 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
178 const char *symname);
179 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
181 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
182 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
185 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
188 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
189 struct inode *new_dir, struct dentry *new_dentry);
190 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
192 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
193 static int yaffs_sync_fs(struct super_block *sb, int wait);
194 static void yaffs_write_super(struct super_block *sb);
196 static int yaffs_sync_fs(struct super_block *sb);
197 static int yaffs_write_super(struct super_block *sb);
200 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
201 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
202 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
203 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
205 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
208 #ifdef YAFFS_HAS_PUT_INODE
209 static void yaffs_put_inode(struct inode *inode);
212 static void yaffs_delete_inode(struct inode *);
213 static void yaffs_clear_inode(struct inode *);
215 static int yaffs_readpage(struct file *file, struct page *page);
216 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
217 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
219 static int yaffs_writepage(struct page *page);
221 static int yaffs_prepare_write(struct file *f, struct page *pg,
222 unsigned offset, unsigned to);
223 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
226 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
228 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
229 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
231 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
234 static struct address_space_operations yaffs_file_address_operations = {
235 .readpage = yaffs_readpage,
236 .writepage = yaffs_writepage,
237 .prepare_write = yaffs_prepare_write,
238 .commit_write = yaffs_commit_write,
241 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,22))
242 static struct file_operations yaffs_file_operations = {
243 .read = do_sync_read,
244 .write = do_sync_write,
245 .aio_read = generic_file_aio_read,
246 .aio_write = generic_file_aio_write,
247 .mmap = generic_file_mmap,
248 .flush = yaffs_file_flush,
249 .fsync = yaffs_sync_object,
250 .splice_read = generic_file_splice_read,
251 .splice_write = generic_file_splice_write,
254 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
256 static struct file_operations yaffs_file_operations = {
257 .read = do_sync_read,
258 .write = do_sync_write,
259 .aio_read = generic_file_aio_read,
260 .aio_write = generic_file_aio_write,
261 .mmap = generic_file_mmap,
262 .flush = yaffs_file_flush,
263 .fsync = yaffs_sync_object,
264 .sendfile = generic_file_sendfile,
269 static struct file_operations yaffs_file_operations = {
270 .read = generic_file_read,
271 .write = generic_file_write,
272 .mmap = generic_file_mmap,
273 .flush = yaffs_file_flush,
274 .fsync = yaffs_sync_object,
275 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
276 .sendfile = generic_file_sendfile,
281 static struct inode_operations yaffs_file_inode_operations = {
282 .setattr = yaffs_setattr,
285 static struct inode_operations yaffs_symlink_inode_operations = {
286 .readlink = yaffs_readlink,
287 .follow_link = yaffs_follow_link,
288 .setattr = yaffs_setattr,
291 static struct inode_operations yaffs_dir_inode_operations = {
292 .create = yaffs_create,
293 .lookup = yaffs_lookup,
295 .unlink = yaffs_unlink,
296 .symlink = yaffs_symlink,
297 .mkdir = yaffs_mkdir,
298 .rmdir = yaffs_unlink,
299 .mknod = yaffs_mknod,
300 .rename = yaffs_rename,
301 .setattr = yaffs_setattr,
304 static struct file_operations yaffs_dir_operations = {
305 .read = generic_read_dir,
306 .readdir = yaffs_readdir,
307 .fsync = yaffs_sync_object,
310 static struct super_operations yaffs_super_ops = {
311 .statfs = yaffs_statfs,
313 #ifndef YAFFS_USE_OWN_IGET
314 .read_inode = yaffs_read_inode,
316 #ifdef YAFFS_HAS_PUT_INODE
317 .put_inode = yaffs_put_inode,
319 .put_super = yaffs_put_super,
320 .delete_inode = yaffs_delete_inode,
321 .clear_inode = yaffs_clear_inode,
322 .sync_fs = yaffs_sync_fs,
323 .write_super = yaffs_write_super,
326 static void yaffs_GrossLock(yaffs_Device * dev)
328 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
330 down(&dev->grossLock);
333 static void yaffs_GrossUnlock(yaffs_Device * dev)
335 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
340 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
343 unsigned char *alias;
346 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
348 yaffs_GrossLock(dev);
350 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
352 yaffs_GrossUnlock(dev);
357 ret = vfs_readlink(dentry, buffer, buflen, alias);
362 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
363 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
365 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
368 unsigned char *alias;
370 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
372 yaffs_GrossLock(dev);
374 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
376 yaffs_GrossUnlock(dev);
384 ret = vfs_follow_link(nd, alias);
387 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
388 return ERR_PTR (ret);
394 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
398 * Lookup is used to find objects in the fs
400 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
402 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
405 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
409 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
411 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
413 yaffs_GrossLock(dev);
416 (KERN_DEBUG "yaffs_lookup for %d:%s\n",
417 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
420 yaffs_FindObjectByName(yaffs_InodeToObject(dir),
421 dentry->d_name.name);
423 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
425 /* Can't hold gross lock when calling yaffs_get_inode() */
426 yaffs_GrossUnlock(dev);
430 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
432 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
436 (KERN_DEBUG "yaffs_loookup dentry \n"));
437 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
438 * d_add even if NULL inode */
440 /*dget(dentry); // try to solve directory bug */
441 d_add(dentry, inode);
449 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
453 /* added NCB for 2.5/6 compatability - forces add even if inode is
454 * NULL which creates dentry hash */
455 d_add(dentry, inode);
458 /* return (ERR_PTR(-EIO)); */
463 #ifdef YAFFS_HAS_PUT_INODE
465 /* For now put inode is just for debugging
466 * Put inode is called when the inode **structure** is put.
468 static void yaffs_put_inode(struct inode *inode)
471 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
472 atomic_read(&inode->i_count)));
477 /* clear is called to tell the fs to release any per-inode data it holds */
478 static void yaffs_clear_inode(struct inode *inode)
483 obj = yaffs_InodeToObject(inode);
486 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
487 atomic_read(&inode->i_count),
488 obj ? "object exists" : "null object"));
492 yaffs_GrossLock(dev);
494 /* Clear the association between the inode and
498 yaffs_InodeToObjectLV(inode) = NULL;
500 /* If the object freeing was deferred, then the real
502 * This should fix the inode inconsistency problem.
505 yaffs_HandleDeferedFree(obj);
507 yaffs_GrossUnlock(dev);
512 /* delete is called when the link count is zero and the inode
513 * is put (ie. nobody wants to know about it anymore, time to
515 * NB Must call clear_inode()
517 static void yaffs_delete_inode(struct inode *inode)
519 yaffs_Object *obj = yaffs_InodeToObject(inode);
523 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
524 atomic_read(&inode->i_count),
525 obj ? "object exists" : "null object"));
529 yaffs_GrossLock(dev);
530 yaffs_DeleteFile(obj);
531 yaffs_GrossUnlock(dev);
533 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
534 truncate_inode_pages (&inode->i_data, 0);
539 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
540 static int yaffs_file_flush(struct file *file, fl_owner_t id)
542 static int yaffs_file_flush(struct file *file)
545 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
547 yaffs_Device *dev = obj->myDev;
550 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
551 obj->dirty ? "dirty" : "clean"));
553 yaffs_GrossLock(dev);
555 yaffs_FlushFile(obj, 1);
557 yaffs_GrossUnlock(dev);
562 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
564 /* Lifted from jffs2 */
567 unsigned char *pg_buf;
572 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
573 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
574 (unsigned)PAGE_CACHE_SIZE));
576 obj = yaffs_DentryToObject(f->f_dentry);
580 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
581 BUG_ON(!PageLocked(pg));
588 /* FIXME: Can kmap fail? */
590 yaffs_GrossLock(dev);
593 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
596 yaffs_GrossUnlock(dev);
602 ClearPageUptodate(pg);
609 flush_dcache_page(pg);
612 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
616 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
618 int ret = yaffs_readpage_nolock(f, pg);
623 static int yaffs_readpage(struct file *f, struct page *pg)
625 return yaffs_readpage_unlock(f, pg);
628 /* writepage inspired by/stolen from smbfs */
630 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
631 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
633 static int yaffs_writepage(struct page *page)
636 struct address_space *mapping = page->mapping;
637 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
639 unsigned long end_index;
647 inode = mapping->host;
651 if (offset > inode->i_size) {
654 "yaffs_writepage at %08x, inode size = %08x!!!\n",
655 (unsigned)(page->index << PAGE_CACHE_SHIFT),
656 (unsigned)inode->i_size));
658 (KERN_DEBUG " -> don't care!!\n"));
663 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
666 if (page->index < end_index) {
667 nBytes = PAGE_CACHE_SIZE;
669 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
676 obj = yaffs_InodeToObject(inode);
677 yaffs_GrossLock(obj->myDev);
680 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
681 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
683 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
684 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
687 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
691 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
692 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
694 yaffs_GrossUnlock(obj->myDev);
697 SetPageUptodate(page);
701 return (nWritten == nBytes) ? 0 : -ENOSPC;
704 static int yaffs_prepare_write(struct file *f, struct page *pg,
705 unsigned offset, unsigned to)
708 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
709 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
710 return yaffs_readpage_nolock(f, pg);
716 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
722 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
723 int nBytes = to - offset;
727 unsigned saddr = (unsigned)addr;
733 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
736 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
738 if (nWritten != nBytes) {
741 "yaffs_commit_write not same size nWritten %d nBytes %d\n",
744 ClearPageUptodate(pg);
752 (KERN_DEBUG "yaffs_commit_write returning %d\n",
753 nWritten == nBytes ? 0 : nWritten));
755 return nWritten == nBytes ? 0 : nWritten;
759 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
764 /* Check mode against the variant type and attempt to repair if broken. */
765 __u32 mode = obj->yst_mode;
766 switch( obj->variantType ){
767 case YAFFS_OBJECT_TYPE_FILE :
768 if( ! S_ISREG(mode) ){
769 obj->yst_mode &= ~S_IFMT;
770 obj->yst_mode |= S_IFREG;
774 case YAFFS_OBJECT_TYPE_SYMLINK :
775 if( ! S_ISLNK(mode) ){
776 obj->yst_mode &= ~S_IFMT;
777 obj->yst_mode |= S_IFLNK;
781 case YAFFS_OBJECT_TYPE_DIRECTORY :
782 if( ! S_ISDIR(mode) ){
783 obj->yst_mode &= ~S_IFMT;
784 obj->yst_mode |= S_IFDIR;
788 case YAFFS_OBJECT_TYPE_UNKNOWN :
789 case YAFFS_OBJECT_TYPE_HARDLINK :
790 case YAFFS_OBJECT_TYPE_SPECIAL :
796 inode->i_flags |= S_NOATIME;
798 inode->i_ino = obj->objectId;
799 inode->i_mode = obj->yst_mode;
800 inode->i_uid = obj->yst_uid;
801 inode->i_gid = obj->yst_gid;
802 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
803 inode->i_blksize = inode->i_sb->s_blocksize;
805 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
807 inode->i_rdev = old_decode_dev(obj->yst_rdev);
808 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
809 inode->i_atime.tv_nsec = 0;
810 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
811 inode->i_mtime.tv_nsec = 0;
812 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
813 inode->i_ctime.tv_nsec = 0;
815 inode->i_rdev = obj->yst_rdev;
816 inode->i_atime = obj->yst_atime;
817 inode->i_mtime = obj->yst_mtime;
818 inode->i_ctime = obj->yst_ctime;
820 inode->i_size = yaffs_GetObjectFileLength(obj);
821 inode->i_blocks = (inode->i_size + 511) >> 9;
823 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
827 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
828 inode->i_mode, inode->i_uid, inode->i_gid,
829 (int)inode->i_size, atomic_read(&inode->i_count)));
831 switch (obj->yst_mode & S_IFMT) {
832 default: /* fifo, device or socket */
833 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
834 init_special_inode(inode, obj->yst_mode,
835 old_decode_dev(obj->yst_rdev));
837 init_special_inode(inode, obj->yst_mode,
838 (dev_t) (obj->yst_rdev));
841 case S_IFREG: /* file */
842 inode->i_op = &yaffs_file_inode_operations;
843 inode->i_fop = &yaffs_file_operations;
844 inode->i_mapping->a_ops =
845 &yaffs_file_address_operations;
847 case S_IFDIR: /* directory */
848 inode->i_op = &yaffs_dir_inode_operations;
849 inode->i_fop = &yaffs_dir_operations;
851 case S_IFLNK: /* symlink */
852 inode->i_op = &yaffs_symlink_inode_operations;
856 yaffs_InodeToObjectLV(inode) = obj;
858 obj->myInode = inode;
862 (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
867 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
874 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
881 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
887 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
889 inode = Y_IGET(sb, obj->objectId);
893 /* NB Side effect: iget calls back to yaffs_read_inode(). */
894 /* iget also increments the inode's i_count */
895 /* NB You can't be holding grossLock or deadlock will happen! */
900 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
908 obj = yaffs_DentryToObject(f->f_dentry);
912 yaffs_GrossLock(dev);
914 inode = f->f_dentry->d_inode;
916 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
917 ipos = inode->i_size;
924 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
928 "yaffs_file_write about to write writing %d bytes"
929 "to object %d at %d\n",
930 n, obj->objectId, ipos));
933 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
936 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
941 if (ipos > inode->i_size) {
942 inode->i_size = ipos;
943 inode->i_blocks = (ipos + 511) >> 9;
947 "yaffs_file_write size updated to %d bytes, "
949 ipos, (int)(inode->i_blocks)));
953 yaffs_GrossUnlock(dev);
954 return nWritten == 0 ? -ENOSPC : nWritten;
957 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
961 struct inode *inode = f->f_dentry->d_inode;
962 unsigned long offset, curoffs;
963 struct ylist_head *i;
966 char name[YAFFS_MAX_NAME_LENGTH + 1];
968 obj = yaffs_DentryToObject(f->f_dentry);
971 yaffs_GrossLock(dev);
975 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
979 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
981 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
990 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
991 (int)f->f_dentry->d_parent->d_inode->i_ino));
993 (dirent, "..", 2, offset,
994 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
1003 /* If the directory has changed since the open or last call to
1004 readdir, rewind to after the 2 canned entries. */
1006 if (f->f_version != inode->i_version) {
1009 f->f_version = inode->i_version;
1012 ylist_for_each(i, &obj->variant.directoryVariant.children) {
1014 if (curoffs >= offset) {
1015 l = ylist_entry(i, yaffs_Object, siblings);
1017 yaffs_GetObjectName(l, name,
1018 YAFFS_MAX_NAME_LENGTH + 1);
1020 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
1021 yaffs_GetObjectInode(l)));
1027 yaffs_GetObjectInode(l),
1028 yaffs_GetObjectType(l))
1041 yaffs_GrossUnlock(dev);
1047 * File creation. Allocate an inode, and we're done..
1049 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1050 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1053 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1057 struct inode *inode;
1059 yaffs_Object *obj = NULL;
1062 yaffs_Object *parent = yaffs_InodeToObject(dir);
1064 int error = -ENOSPC;
1065 uid_t uid = current->fsuid;
1066 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1068 if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1073 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
1074 parent->objectId, parent->variantType));
1077 (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
1081 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1083 dentry->d_name.name, mode, rdev));
1085 dev = parent->myDev;
1087 yaffs_GrossLock(dev);
1089 switch (mode & S_IFMT) {
1091 /* Special (socket, fifo, device...) */
1092 T(YAFFS_TRACE_OS, (KERN_DEBUG
1093 "yaffs_mknod: making special\n"));
1094 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1096 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1097 gid, old_encode_dev(rdev));
1100 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1104 case S_IFREG: /* file */
1105 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1107 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1110 case S_IFDIR: /* directory */
1112 (KERN_DEBUG "yaffs_mknod: making directory\n"));
1114 yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1117 case S_IFLNK: /* symlink */
1118 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1119 obj = NULL; /* Do we ever get here? */
1123 /* Can not call yaffs_get_inode() with gross lock held */
1124 yaffs_GrossUnlock(dev);
1127 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1128 d_instantiate(dentry, inode);
1130 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
1131 obj->objectId, atomic_read(&inode->i_count)));
1135 (KERN_DEBUG "yaffs_mknod failed making object\n"));
1142 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1145 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
1146 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1148 /* attempt to fix dir bug - didn't work */
1156 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1157 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1158 struct nameidata *n)
1160 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1163 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1164 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1167 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1174 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1175 dentry->d_name.name));
1177 dev = yaffs_InodeToObject(dir)->myDev;
1179 yaffs_GrossLock(dev);
1181 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1183 if (retVal == YAFFS_OK) {
1184 dentry->d_inode->i_nlink--;
1186 yaffs_GrossUnlock(dev);
1187 mark_inode_dirty(dentry->d_inode);
1190 yaffs_GrossUnlock(dev);
1197 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1198 struct dentry *dentry)
1200 struct inode *inode = old_dentry->d_inode;
1201 yaffs_Object *obj = NULL;
1202 yaffs_Object *link = NULL;
1205 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1207 obj = yaffs_InodeToObject(inode);
1210 yaffs_GrossLock(dev);
1212 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1215 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1220 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1221 d_instantiate(dentry, old_dentry->d_inode);
1222 atomic_inc(&old_dentry->d_inode->i_count);
1224 (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1225 old_dentry->d_inode->i_nlink,
1226 atomic_read(&old_dentry->d_inode->i_count)));
1230 yaffs_GrossUnlock(dev);
1240 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1241 const char *symname)
1245 uid_t uid = current->fsuid;
1246 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1248 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1250 dev = yaffs_InodeToObject(dir)->myDev;
1251 yaffs_GrossLock(dev);
1252 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1253 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1254 yaffs_GrossUnlock(dev);
1258 struct inode *inode;
1260 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1261 d_instantiate(dentry, inode);
1262 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1265 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1272 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1279 obj = yaffs_DentryToObject(dentry);
1283 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1284 yaffs_GrossLock(dev);
1285 yaffs_FlushFile(obj, 1);
1286 yaffs_GrossUnlock(dev);
1291 * The VFS layer already does all the dentry stuff for rename.
1293 * NB: POSIX says you can rename an object over an old object of the same name
1295 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1296 struct inode *new_dir, struct dentry *new_dentry)
1299 int retVal = YAFFS_FAIL;
1300 yaffs_Object *target;
1302 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1303 dev = yaffs_InodeToObject(old_dir)->myDev;
1305 yaffs_GrossLock(dev);
1307 /* Check if the target is an existing directory that is not empty. */
1309 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1310 new_dentry->d_name.name);
1315 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1316 !ylist_empty(&target->variant.directoryVariant.children)) {
1318 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1320 retVal = YAFFS_FAIL;
1323 /* Now does unlinking internally using shadowing mechanism */
1324 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1327 yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1328 old_dentry->d_name.name,
1329 yaffs_InodeToObject(new_dir),
1330 new_dentry->d_name.name);
1333 yaffs_GrossUnlock(dev);
1335 if (retVal == YAFFS_OK) {
1337 new_dentry->d_inode->i_nlink--;
1338 mark_inode_dirty(new_dentry->d_inode);
1348 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1350 struct inode *inode = dentry->d_inode;
1355 (KERN_DEBUG "yaffs_setattr of object %d\n",
1356 yaffs_InodeToObject(inode)->objectId));
1358 if ((error = inode_change_ok(inode, attr)) == 0) {
1360 dev = yaffs_InodeToObject(inode)->myDev;
1361 yaffs_GrossLock(dev);
1362 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1368 yaffs_GrossUnlock(dev);
1370 error = inode_setattr(inode, attr);
1375 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1376 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1378 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1379 struct super_block *sb = dentry->d_sb;
1380 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1381 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1383 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1385 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1387 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1390 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1392 yaffs_GrossLock(dev);
1394 buf->f_type = YAFFS_MAGIC;
1395 buf->f_bsize = sb->s_blocksize;
1396 buf->f_namelen = 255;
1398 if(dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)){
1399 /* Do this if chunk size is not a power of 2 */
1401 uint64_t bytesInDev;
1404 bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock +1))) *
1405 ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
1407 do_div(bytesInDev,sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1408 buf->f_blocks = bytesInDev;
1410 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1411 ((uint64_t)(dev->nDataBytesPerChunk));
1413 do_div(bytesFree,sb->s_blocksize);
1415 buf->f_bfree = bytesFree;
1417 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1420 (dev->endBlock - dev->startBlock + 1) *
1421 dev->nChunksPerBlock /
1422 (sb->s_blocksize / dev->nDataBytesPerChunk);
1424 yaffs_GetNumberOfFreeChunks(dev) /
1425 (sb->s_blocksize / dev->nDataBytesPerChunk);
1428 (dev->endBlock - dev->startBlock + 1) *
1429 dev->nChunksPerBlock *
1430 (dev->nDataBytesPerChunk / sb->s_blocksize);
1433 yaffs_GetNumberOfFreeChunks(dev) *
1434 (dev->nDataBytesPerChunk / sb->s_blocksize);
1440 buf->f_bavail = buf->f_bfree;
1442 yaffs_GrossUnlock(dev);
1447 static int yaffs_do_sync_fs(struct super_block *sb)
1450 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1451 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
1454 yaffs_GrossLock(dev);
1457 yaffs_FlushEntireDeviceCache(dev);
1458 yaffs_CheckpointSave(dev);
1461 yaffs_GrossUnlock(dev);
1469 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1470 static void yaffs_write_super(struct super_block *sb)
1472 static int yaffs_write_super(struct super_block *sb)
1476 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
1477 if (yaffs_auto_checkpoint >= 2)
1478 yaffs_do_sync_fs(sb);
1479 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
1485 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1486 static int yaffs_sync_fs(struct super_block *sb, int wait)
1488 static int yaffs_sync_fs(struct super_block *sb)
1492 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
1494 if (yaffs_auto_checkpoint >= 1)
1495 yaffs_do_sync_fs(sb);
1501 #ifdef YAFFS_USE_OWN_IGET
1503 static struct inode * yaffs_iget(struct super_block *sb, unsigned long ino)
1505 struct inode *inode;
1507 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1510 (KERN_DEBUG "yaffs_iget for %lu\n", ino));
1512 inode = iget_locked(sb, ino);
1514 return ERR_PTR(-ENOMEM);
1515 if (!(inode->i_state & I_NEW))
1518 /* NB This is called as a side effect of other functions, but
1519 * we had to release the lock to prevent deadlocks, so
1520 * need to lock again.
1523 yaffs_GrossLock(dev);
1525 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1527 yaffs_FillInodeFromObject(inode, obj);
1529 yaffs_GrossUnlock(dev);
1531 unlock_new_inode(inode);
1537 static void yaffs_read_inode(struct inode *inode)
1539 /* NB This is called as a side effect of other functions, but
1540 * we had to release the lock to prevent deadlocks, so
1541 * need to lock again.
1545 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1548 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1550 yaffs_GrossLock(dev);
1552 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1554 yaffs_FillInodeFromObject(inode, obj);
1556 yaffs_GrossUnlock(dev);
1561 static YLIST_HEAD(yaffs_dev_list);
1564 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1566 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1568 if( *flags & MS_RDONLY ) {
1569 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1572 (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
1574 yaffs_GrossLock(dev);
1576 yaffs_FlushEntireDeviceCache(dev);
1578 yaffs_CheckpointSave(dev);
1583 yaffs_GrossUnlock(dev);
1587 (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
1594 static void yaffs_put_super(struct super_block *sb)
1596 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1598 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
1600 yaffs_GrossLock(dev);
1602 yaffs_FlushEntireDeviceCache(dev);
1604 yaffs_CheckpointSave(dev);
1606 if (dev->putSuperFunc) {
1607 dev->putSuperFunc(sb);
1610 yaffs_Deinitialise(dev);
1612 yaffs_GrossUnlock(dev);
1614 /* we assume this is protected by lock_kernel() in mount/umount */
1615 ylist_del(&dev->devList);
1617 if(dev->spareBuffer){
1618 YFREE(dev->spareBuffer);
1619 dev->spareBuffer = NULL;
1626 static void yaffs_MTDPutSuper(struct super_block *sb)
1629 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1635 put_mtd_device(mtd);
1639 static void yaffs_MarkSuperBlockDirty(void *vsb)
1641 struct super_block *sb = (struct super_block *)vsb;
1643 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
1650 int skip_checkpoint_read;
1651 int skip_checkpoint_write;
1655 #define MAX_OPT_LEN 20
1656 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
1658 char cur_opt[MAX_OPT_LEN+1];
1662 /* Parse through the options which is a comma seperated list */
1664 while(options_str && *options_str && !error){
1665 memset(cur_opt,0,MAX_OPT_LEN+1);
1668 while(*options_str && *options_str != ','){
1669 if(p < MAX_OPT_LEN){
1670 cur_opt[p] = *options_str;
1676 if(!strcmp(cur_opt,"inband-tags"))
1677 options->inband_tags = 1;
1678 else if(!strcmp(cur_opt,"no-cache"))
1679 options->no_cache = 1;
1680 else if(!strcmp(cur_opt,"no-checkpoint-read"))
1681 options->skip_checkpoint_read = 1;
1682 else if(!strcmp(cur_opt,"no-checkpoint-write"))
1683 options->skip_checkpoint_write = 1;
1684 else if(!strcmp(cur_opt,"no-checkpoint")){
1685 options->skip_checkpoint_read = 1;
1686 options->skip_checkpoint_write = 1;
1688 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
1697 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1698 struct super_block *sb,
1699 void *data, int silent)
1702 struct inode *inode = NULL;
1703 struct dentry *root;
1704 yaffs_Device *dev = 0;
1705 char devname_buf[BDEVNAME_SIZE + 1];
1706 struct mtd_info *mtd;
1708 char *data_str = (char *)data;
1710 yaffs_options options;
1712 sb->s_magic = YAFFS_MAGIC;
1713 sb->s_op = &yaffs_super_ops;
1714 sb->s_flags |= MS_NOATIME;
1717 printk(KERN_INFO "yaffs: sb is NULL\n");
1718 else if (!sb->s_dev)
1719 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1720 else if (!yaffs_devname(sb, devname_buf))
1721 printk(KERN_INFO "yaffs: devname is NULL\n");
1723 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1725 yaffs_devname(sb, devname_buf));
1730 printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
1732 memset(&options,0,sizeof(options));
1734 if(yaffs_parse_options(&options,data_str)){
1735 /* Option parsing failed */
1740 sb->s_blocksize = PAGE_CACHE_SIZE;
1741 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1742 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1744 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1746 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1748 ("yaffs: Write verification disabled. All guarantees "
1749 "null and void\n"));
1752 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1754 MAJOR(sb->s_dev), MINOR(sb->s_dev),
1755 yaffs_devname(sb, devname_buf)));
1757 /* Check it's an mtd device..... */
1758 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1759 return NULL; /* This isn't an mtd device */
1761 /* Get the device */
1762 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1764 T(YAFFS_TRACE_ALWAYS,
1765 ("yaffs: MTD device #%u doesn't appear to exist\n",
1769 /* Check it's NAND */
1770 if (mtd->type != MTD_NANDFLASH) {
1771 T(YAFFS_TRACE_ALWAYS,
1772 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1776 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1777 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1778 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1779 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1780 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1781 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1782 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1783 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
1784 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1785 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1786 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1788 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1790 if (yaffsVersion == 1 &&
1791 WRITE_SIZE(mtd) >= 2048) {
1792 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1796 /* Added NCB 26/5/2006 for completeness */
1797 if (yaffsVersion == 2 &&
1798 !options.inband_tags &&
1799 WRITE_SIZE(mtd) == 512){
1800 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
1806 if (yaffsVersion == 2) {
1807 /* Check for version 2 style functions */
1809 !mtd->block_isbad ||
1810 !mtd->block_markbad ||
1813 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1814 !mtd->read_oob || !mtd->write_oob) {
1817 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1819 T(YAFFS_TRACE_ALWAYS,
1820 ("yaffs: MTD device does not support required "
1825 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1826 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
1827 !options.inband_tags) {
1828 T(YAFFS_TRACE_ALWAYS,
1829 ("yaffs: MTD device does not have the "
1830 "right page sizes\n"));
1834 /* Check for V1 style functions */
1838 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1839 !mtd->read_oob || !mtd->write_oob) {
1842 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1844 T(YAFFS_TRACE_ALWAYS,
1845 ("yaffs: MTD device does not support required "
1850 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
1851 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1852 T(YAFFS_TRACE_ALWAYS,
1853 ("yaffs: MTD device does not support have the "
1854 "right page sizes\n"));
1859 /* OK, so if we got here, we have an MTD that's NAND and looks
1860 * like it has the right capabilities
1861 * Set the yaffs_Device up for mtd
1864 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1865 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1867 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1870 /* Deep shit could not allocate device structure */
1871 T(YAFFS_TRACE_ALWAYS,
1872 ("yaffs_read_super: Failed trying to allocate "
1873 "yaffs_Device. \n"));
1877 memset(dev, 0, sizeof(yaffs_Device));
1878 dev->genericDevice = mtd;
1879 dev->name = mtd->name;
1881 /* Set up the memory size parameters.... */
1883 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1884 dev->startBlock = 0;
1885 dev->endBlock = nBlocks - 1;
1886 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1887 dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1888 dev->nReservedBlocks = 5;
1889 dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
1890 dev->inbandTags = options.inband_tags;
1892 /* ... and the functions. */
1893 if (yaffsVersion == 2) {
1894 dev->writeChunkWithTagsToNAND =
1895 nandmtd2_WriteChunkWithTagsToNAND;
1896 dev->readChunkWithTagsFromNAND =
1897 nandmtd2_ReadChunkWithTagsFromNAND;
1898 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1899 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1900 dev->spareBuffer = YMALLOC(mtd->oobsize);
1902 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1903 dev->totalBytesPerChunk = mtd->writesize;
1904 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
1906 dev->totalBytesPerChunk = mtd->oobblock;
1907 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1909 nBlocks = mtd->size / mtd->erasesize;
1911 dev->startBlock = 0;
1912 dev->endBlock = nBlocks - 1;
1914 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1915 /* use the MTD interface in yaffs_mtdif1.c */
1916 dev->writeChunkWithTagsToNAND =
1917 nandmtd1_WriteChunkWithTagsToNAND;
1918 dev->readChunkWithTagsFromNAND =
1919 nandmtd1_ReadChunkWithTagsFromNAND;
1920 dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
1921 dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
1923 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1924 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1928 /* ... and common functions */
1929 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1930 dev->initialiseNAND = nandmtd_InitialiseNAND;
1932 dev->putSuperFunc = yaffs_MTDPutSuper;
1934 dev->superBlock = (void *)sb;
1935 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
1938 #ifndef CONFIG_YAFFS_DOES_ECC
1939 dev->useNANDECC = 1;
1942 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1943 dev->wideTnodesDisabled = 1;
1946 dev->skipCheckpointRead = options.skip_checkpoint_read;
1947 dev->skipCheckpointWrite = options.skip_checkpoint_write;
1949 /* we assume this is protected by lock_kernel() in mount/umount */
1950 ylist_add_tail(&dev->devList, &yaffs_dev_list);
1952 init_MUTEX(&dev->grossLock);
1954 yaffs_GrossLock(dev);
1956 err = yaffs_GutsInitialise(dev);
1959 ("yaffs_read_super: guts initialised %s\n",
1960 (err == YAFFS_OK) ? "OK" : "FAILED"));
1962 /* Release lock before yaffs_get_inode() */
1963 yaffs_GrossUnlock(dev);
1965 /* Create root inode */
1966 if (err == YAFFS_OK)
1967 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1973 inode->i_op = &yaffs_dir_inode_operations;
1974 inode->i_fop = &yaffs_dir_operations;
1976 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1978 root = d_alloc_root(inode);
1980 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1987 sb->s_dirt = !dev->isCheckpointed;
1988 T(YAFFS_TRACE_ALWAYS,
1989 ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
1991 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1996 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1997 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
2000 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
2003 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2004 static int yaffs_read_super(struct file_system_type *fs,
2005 int flags, const char *dev_name,
2006 void *data, struct vfsmount *mnt)
2009 return get_sb_bdev(fs, flags, dev_name, data,
2010 yaffs_internal_read_super_mtd, mnt);
2013 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2014 int flags, const char *dev_name,
2018 return get_sb_bdev(fs, flags, dev_name, data,
2019 yaffs_internal_read_super_mtd);
2023 static struct file_system_type yaffs_fs_type = {
2024 .owner = THIS_MODULE,
2026 .get_sb = yaffs_read_super,
2027 .kill_sb = kill_block_super,
2028 .fs_flags = FS_REQUIRES_DEV,
2031 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2034 return yaffs_internal_read_super(1, sb, data, silent);
2037 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2042 #ifdef CONFIG_YAFFS_YAFFS2
2044 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2045 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2048 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2051 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2052 static int yaffs2_read_super(struct file_system_type *fs,
2053 int flags, const char *dev_name, void *data,
2054 struct vfsmount *mnt)
2056 return get_sb_bdev(fs, flags, dev_name, data,
2057 yaffs2_internal_read_super_mtd, mnt);
2060 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2061 int flags, const char *dev_name,
2065 return get_sb_bdev(fs, flags, dev_name, data,
2066 yaffs2_internal_read_super_mtd);
2070 static struct file_system_type yaffs2_fs_type = {
2071 .owner = THIS_MODULE,
2073 .get_sb = yaffs2_read_super,
2074 .kill_sb = kill_block_super,
2075 .fs_flags = FS_REQUIRES_DEV,
2078 static struct super_block *yaffs2_read_super(struct super_block *sb,
2079 void *data, int silent)
2081 return yaffs_internal_read_super(2, sb, data, silent);
2084 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2088 #endif /* CONFIG_YAFFS_YAFFS2 */
2090 static struct proc_dir_entry *my_proc_entry;
2092 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
2094 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
2095 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
2096 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
2097 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2098 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2099 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2100 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2101 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
2102 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2103 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2104 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2105 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2106 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2107 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2108 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2109 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2110 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2111 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2112 buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2113 buf += sprintf(buf, "passiveGCs......... %d\n",
2114 dev->passiveGarbageCollections);
2115 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2116 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
2117 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2118 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2119 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2120 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2121 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2122 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2123 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2124 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2126 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2127 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
2128 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
2129 buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
2134 static int yaffs_proc_read(char *page,
2136 off_t offset, int count, int *eof, void *data)
2138 struct ylist_head *item;
2143 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2144 * We use 'offset' (*ppos) to indicate where we are in devList.
2145 * This also assumes the user has posted a read buffer large
2146 * enough to hold the complete output; but that's life in /proc.
2151 /* Print header first */
2153 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2154 "\n%s\n%s\n", yaffs_fs_c_version,
2155 yaffs_guts_c_version);
2158 /* hold lock_kernel while traversing yaffs_dev_list */
2161 /* Locate and print the Nth entry. Order N-squared but N is small. */
2162 ylist_for_each(item, &yaffs_dev_list) {
2163 yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
2168 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
2169 buf = yaffs_dump_dev(buf, dev);
2174 return buf - page < count ? buf - page : count;
2178 * Set the verbosity of the warnings and error messages.
2180 * Note that the names can only be a..z or _ with the current code.
2185 unsigned mask_bitfield;
2187 {"allocate", YAFFS_TRACE_ALLOCATE},
2188 {"always", YAFFS_TRACE_ALWAYS},
2189 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2190 {"buffers", YAFFS_TRACE_BUFFERS},
2191 {"bug", YAFFS_TRACE_BUG},
2192 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2193 {"deletion", YAFFS_TRACE_DELETION},
2194 {"erase", YAFFS_TRACE_ERASE},
2195 {"error", YAFFS_TRACE_ERROR},
2196 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2197 {"gc", YAFFS_TRACE_GC},
2198 {"mtd", YAFFS_TRACE_MTD},
2199 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2200 {"os", YAFFS_TRACE_OS},
2201 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2202 {"scan", YAFFS_TRACE_SCAN},
2203 {"tracing", YAFFS_TRACE_TRACING},
2205 {"verify", YAFFS_TRACE_VERIFY},
2206 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2207 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2208 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2210 {"write", YAFFS_TRACE_WRITE},
2211 {"all", 0xffffffff},
2216 #define MAX_MASK_NAME_LENGTH 40
2217 static int yaffs_proc_write(struct file *file, const char *buf,
2218 unsigned long count, void *data)
2220 unsigned rg = 0, mask_bitfield;
2224 char substring[MAX_MASK_NAME_LENGTH+1];
2230 rg = yaffs_traceMask;
2232 while (!done && (pos < count)) {
2234 while ((pos < count) && isspace(buf[pos])) {
2252 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2253 if (end > buf + pos) {
2254 mask_name = "numeral";
2255 len = end - (buf + pos);
2259 for(x = buf + pos, i = 0;
2260 (*x == '_' || (*x >='a' && *x <= 'z')) &&
2261 i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2263 substring[i] = '\0';
2265 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2266 if(strcmp(substring,mask_flags[i].mask_name) == 0){
2267 mask_name = mask_flags[i].mask_name;
2268 mask_bitfield = mask_flags[i].mask_bitfield;
2275 if (mask_name != NULL) {
2279 rg &= ~mask_bitfield;
2282 rg |= mask_bitfield;
2288 rg |= mask_bitfield;
2294 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2296 printk("new trace = 0x%08X\n",yaffs_traceMask);
2298 if (rg & YAFFS_TRACE_ALWAYS) {
2299 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2301 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2302 printk("%c%s\n", flag, mask_flags[i].mask_name);
2309 /* Stuff to handle installation of file systems */
2310 struct file_system_to_install {
2311 struct file_system_type *fst;
2315 static struct file_system_to_install fs_to_install[] = {
2316 //#ifdef CONFIG_YAFFS_YAFFS1
2317 {&yaffs_fs_type, 0},
2319 //#ifdef CONFIG_YAFFS_YAFFS2
2320 {&yaffs2_fs_type, 0},
2325 static int __init init_yaffs_fs(void)
2328 struct file_system_to_install *fsinst;
2330 T(YAFFS_TRACE_ALWAYS,
2331 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2333 /* Install the proc_fs entry */
2334 my_proc_entry = create_proc_entry("yaffs",
2338 if (my_proc_entry) {
2339 my_proc_entry->write_proc = yaffs_proc_write;
2340 my_proc_entry->read_proc = yaffs_proc_read;
2341 my_proc_entry->data = NULL;
2346 /* Now add the file system entries */
2348 fsinst = fs_to_install;
2350 while (fsinst->fst && !error) {
2351 error = register_filesystem(fsinst->fst);
2353 fsinst->installed = 1;
2358 /* Any errors? uninstall */
2360 fsinst = fs_to_install;
2362 while (fsinst->fst) {
2363 if (fsinst->installed) {
2364 unregister_filesystem(fsinst->fst);
2365 fsinst->installed = 0;
2374 static void __exit exit_yaffs_fs(void)
2377 struct file_system_to_install *fsinst;
2379 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2382 remove_proc_entry("yaffs", YPROC_ROOT);
2384 fsinst = fs_to_install;
2386 while (fsinst->fst) {
2387 if (fsinst->installed) {
2388 unregister_filesystem(fsinst->fst);
2389 fsinst->installed = 0;
2396 module_init(init_yaffs_fs)
2397 module_exit(exit_yaffs_fs)
2399 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2400 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2401 MODULE_LICENSE("GPL");