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.67 2008-07-03 20:06:05 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,17))
81 #define WRITE_SIZE_STR "writesize"
82 #define WRITE_SIZE(mtd) (mtd)->writesize
84 #define WRITE_SIZE_STR "oobblock"
85 #define WRITE_SIZE(mtd) (mtd)->oobblock
88 #include <asm/uaccess.h>
91 #include "yaffs_guts.h"
93 #include <linux/mtd/mtd.h>
94 #include "yaffs_mtdif.h"
95 #include "yaffs_mtdif1.h"
96 #include "yaffs_mtdif2.h"
98 unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
99 unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
101 /* Module Parameters */
102 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
103 module_param(yaffs_traceMask,uint,0644);
104 module_param(yaffs_wr_attempts,uint,0644);
106 MODULE_PARM(yaffs_traceMask,"i");
107 MODULE_PARM(yaffs_wr_attempts,"i");
110 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
111 /* use iget and read_inode */
112 #define Y_IGET(sb,inum) iget((sb),(inum))
113 static void yaffs_read_inode(struct inode *inode);
116 /* Call local equivalent */
117 #define YAFFS_USE_OWN_IGET
118 #define Y_IGET(sb,inum) yaffs_iget((sb),(inum))
120 static struct inode * yaffs_iget(struct super_block *sb, unsigned long ino);
123 /*#define T(x) printk x */
125 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
126 #define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
128 #define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
131 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
132 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
134 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
135 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
137 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
140 static void yaffs_put_super(struct super_block *sb);
142 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
145 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
146 static int yaffs_file_flush(struct file *file, fl_owner_t id);
148 static int yaffs_file_flush(struct file *file);
151 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
154 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
156 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
157 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
158 struct nameidata *n);
159 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
160 struct nameidata *n);
162 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
163 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
165 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
166 struct dentry *dentry);
167 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
168 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
169 const char *symname);
170 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
172 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
173 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
176 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
179 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
180 struct inode *new_dir, struct dentry *new_dentry);
181 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
183 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
184 static int yaffs_sync_fs(struct super_block *sb, int wait);
185 static void yaffs_write_super(struct super_block *sb);
187 static int yaffs_sync_fs(struct super_block *sb);
188 static int yaffs_write_super(struct super_block *sb);
191 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
192 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
193 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
194 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
196 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
199 static void yaffs_put_inode(struct inode *inode);
200 static void yaffs_delete_inode(struct inode *);
201 static void yaffs_clear_inode(struct inode *);
203 static int yaffs_readpage(struct file *file, struct page *page);
204 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
205 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
207 static int yaffs_writepage(struct page *page);
209 static int yaffs_prepare_write(struct file *f, struct page *pg,
210 unsigned offset, unsigned to);
211 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
214 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
216 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
217 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
219 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
222 static struct address_space_operations yaffs_file_address_operations = {
223 .readpage = yaffs_readpage,
224 .writepage = yaffs_writepage,
225 .prepare_write = yaffs_prepare_write,
226 .commit_write = yaffs_commit_write,
229 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,22))
230 static struct file_operations yaffs_file_operations = {
231 .read = do_sync_read,
232 .write = do_sync_write,
233 .aio_read = generic_file_aio_read,
234 .aio_write = generic_file_aio_write,
235 .mmap = generic_file_mmap,
236 .flush = yaffs_file_flush,
237 .fsync = yaffs_sync_object,
238 .splice_read = generic_file_splice_read,
239 .splice_write = generic_file_splice_write,
242 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
244 static struct file_operations yaffs_file_operations = {
245 .read = do_sync_read,
246 .write = do_sync_write,
247 .aio_read = generic_file_aio_read,
248 .aio_write = generic_file_aio_write,
249 .mmap = generic_file_mmap,
250 .flush = yaffs_file_flush,
251 .fsync = yaffs_sync_object,
252 .sendfile = generic_file_sendfile,
257 static struct file_operations yaffs_file_operations = {
258 .read = generic_file_read,
259 .write = generic_file_write,
260 .mmap = generic_file_mmap,
261 .flush = yaffs_file_flush,
262 .fsync = yaffs_sync_object,
263 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
264 .sendfile = generic_file_sendfile,
269 static struct inode_operations yaffs_file_inode_operations = {
270 .setattr = yaffs_setattr,
273 static struct inode_operations yaffs_symlink_inode_operations = {
274 .readlink = yaffs_readlink,
275 .follow_link = yaffs_follow_link,
276 .setattr = yaffs_setattr,
279 static struct inode_operations yaffs_dir_inode_operations = {
280 .create = yaffs_create,
281 .lookup = yaffs_lookup,
283 .unlink = yaffs_unlink,
284 .symlink = yaffs_symlink,
285 .mkdir = yaffs_mkdir,
286 .rmdir = yaffs_unlink,
287 .mknod = yaffs_mknod,
288 .rename = yaffs_rename,
289 .setattr = yaffs_setattr,
292 static struct file_operations yaffs_dir_operations = {
293 .read = generic_read_dir,
294 .readdir = yaffs_readdir,
295 .fsync = yaffs_sync_object,
298 static struct super_operations yaffs_super_ops = {
299 .statfs = yaffs_statfs,
301 #ifndef YAFFS_USE_OWN_IGET
302 .read_inode = yaffs_read_inode,
304 .put_inode = yaffs_put_inode,
305 .put_super = yaffs_put_super,
306 .delete_inode = yaffs_delete_inode,
307 .clear_inode = yaffs_clear_inode,
308 .sync_fs = yaffs_sync_fs,
309 .write_super = yaffs_write_super,
312 static void yaffs_GrossLock(yaffs_Device * dev)
314 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
316 down(&dev->grossLock);
319 static void yaffs_GrossUnlock(yaffs_Device * dev)
321 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
326 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
329 unsigned char *alias;
332 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
334 yaffs_GrossLock(dev);
336 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
338 yaffs_GrossUnlock(dev);
343 ret = vfs_readlink(dentry, buffer, buflen, alias);
348 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
349 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
351 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
354 unsigned char *alias;
356 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
358 yaffs_GrossLock(dev);
360 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
362 yaffs_GrossUnlock(dev);
370 ret = vfs_follow_link(nd, alias);
373 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
374 return ERR_PTR (ret);
380 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
384 * Lookup is used to find objects in the fs
386 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
388 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
391 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
395 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
397 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
399 yaffs_GrossLock(dev);
402 (KERN_DEBUG "yaffs_lookup for %d:%s\n",
403 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
406 yaffs_FindObjectByName(yaffs_InodeToObject(dir),
407 dentry->d_name.name);
409 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
411 /* Can't hold gross lock when calling yaffs_get_inode() */
412 yaffs_GrossUnlock(dev);
416 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
418 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
422 (KERN_DEBUG "yaffs_loookup dentry \n"));
423 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
424 * d_add even if NULL inode */
426 /*dget(dentry); // try to solve directory bug */
427 d_add(dentry, inode);
435 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
439 /* added NCB for 2.5/6 compatability - forces add even if inode is
440 * NULL which creates dentry hash */
441 d_add(dentry, inode);
444 /* return (ERR_PTR(-EIO)); */
448 /* For now put inode is just for debugging
449 * Put inode is called when the inode **structure** is put.
451 static void yaffs_put_inode(struct inode *inode)
454 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
455 atomic_read(&inode->i_count)));
459 /* clear is called to tell the fs to release any per-inode data it holds */
460 static void yaffs_clear_inode(struct inode *inode)
465 obj = yaffs_InodeToObject(inode);
468 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
469 atomic_read(&inode->i_count),
470 obj ? "object exists" : "null object"));
474 yaffs_GrossLock(dev);
476 /* Clear the association between the inode and
480 yaffs_InodeToObjectLV(inode) = NULL;
482 /* If the object freeing was deferred, then the real
484 * This should fix the inode inconsistency problem.
487 yaffs_HandleDeferedFree(obj);
489 yaffs_GrossUnlock(dev);
494 /* delete is called when the link count is zero and the inode
495 * is put (ie. nobody wants to know about it anymore, time to
497 * NB Must call clear_inode()
499 static void yaffs_delete_inode(struct inode *inode)
501 yaffs_Object *obj = yaffs_InodeToObject(inode);
505 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
506 atomic_read(&inode->i_count),
507 obj ? "object exists" : "null object"));
511 yaffs_GrossLock(dev);
512 yaffs_DeleteFile(obj);
513 yaffs_GrossUnlock(dev);
515 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
516 truncate_inode_pages (&inode->i_data, 0);
521 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
522 static int yaffs_file_flush(struct file *file, fl_owner_t id)
524 static int yaffs_file_flush(struct file *file)
527 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
529 yaffs_Device *dev = obj->myDev;
532 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
533 obj->dirty ? "dirty" : "clean"));
535 yaffs_GrossLock(dev);
537 yaffs_FlushFile(obj, 1);
539 yaffs_GrossUnlock(dev);
544 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
546 /* Lifted from jffs2 */
549 unsigned char *pg_buf;
554 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
555 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
556 (unsigned)PAGE_CACHE_SIZE));
558 obj = yaffs_DentryToObject(f->f_dentry);
562 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
563 BUG_ON(!PageLocked(pg));
570 /* FIXME: Can kmap fail? */
572 yaffs_GrossLock(dev);
575 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
578 yaffs_GrossUnlock(dev);
584 ClearPageUptodate(pg);
591 flush_dcache_page(pg);
594 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
598 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
600 int ret = yaffs_readpage_nolock(f, pg);
605 static int yaffs_readpage(struct file *f, struct page *pg)
607 return yaffs_readpage_unlock(f, pg);
610 /* writepage inspired by/stolen from smbfs */
612 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
613 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
615 static int yaffs_writepage(struct page *page)
618 struct address_space *mapping = page->mapping;
619 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
621 unsigned long end_index;
629 inode = mapping->host;
633 if (offset > inode->i_size) {
636 "yaffs_writepage at %08x, inode size = %08x!!!\n",
637 (unsigned)(page->index << PAGE_CACHE_SHIFT),
638 (unsigned)inode->i_size));
640 (KERN_DEBUG " -> don't care!!\n"));
645 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
648 if (page->index < end_index) {
649 nBytes = PAGE_CACHE_SIZE;
651 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
658 obj = yaffs_InodeToObject(inode);
659 yaffs_GrossLock(obj->myDev);
662 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
663 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
665 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
666 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
669 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
673 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
674 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
676 yaffs_GrossUnlock(obj->myDev);
679 SetPageUptodate(page);
683 return (nWritten == nBytes) ? 0 : -ENOSPC;
686 static int yaffs_prepare_write(struct file *f, struct page *pg,
687 unsigned offset, unsigned to)
690 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
691 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
692 return yaffs_readpage_nolock(f, pg);
698 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
702 void *addr = page_address(pg) + offset;
703 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
704 int nBytes = to - offset;
708 unsigned saddr = (unsigned)addr;
711 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
714 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
716 if (nWritten != nBytes) {
719 "yaffs_commit_write not same size nWritten %d nBytes %d\n",
722 ClearPageUptodate(pg);
728 (KERN_DEBUG "yaffs_commit_write returning %d\n",
729 nWritten == nBytes ? 0 : nWritten));
731 return nWritten == nBytes ? 0 : nWritten;
735 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
740 /* Check mode against the variant type and attempt to repair if broken. */
741 __u32 mode = obj->yst_mode;
742 switch( obj->variantType ){
743 case YAFFS_OBJECT_TYPE_FILE :
744 if( ! S_ISREG(mode) ){
745 obj->yst_mode &= ~S_IFMT;
746 obj->yst_mode |= S_IFREG;
750 case YAFFS_OBJECT_TYPE_SYMLINK :
751 if( ! S_ISLNK(mode) ){
752 obj->yst_mode &= ~S_IFMT;
753 obj->yst_mode |= S_IFLNK;
757 case YAFFS_OBJECT_TYPE_DIRECTORY :
758 if( ! S_ISDIR(mode) ){
759 obj->yst_mode &= ~S_IFMT;
760 obj->yst_mode |= S_IFDIR;
764 case YAFFS_OBJECT_TYPE_UNKNOWN :
765 case YAFFS_OBJECT_TYPE_HARDLINK :
766 case YAFFS_OBJECT_TYPE_SPECIAL :
772 inode->i_flags |= S_NOATIME;
774 inode->i_ino = obj->objectId;
775 inode->i_mode = obj->yst_mode;
776 inode->i_uid = obj->yst_uid;
777 inode->i_gid = obj->yst_gid;
778 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
779 inode->i_blksize = inode->i_sb->s_blocksize;
781 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
783 inode->i_rdev = old_decode_dev(obj->yst_rdev);
784 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
785 inode->i_atime.tv_nsec = 0;
786 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
787 inode->i_mtime.tv_nsec = 0;
788 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
789 inode->i_ctime.tv_nsec = 0;
791 inode->i_rdev = obj->yst_rdev;
792 inode->i_atime = obj->yst_atime;
793 inode->i_mtime = obj->yst_mtime;
794 inode->i_ctime = obj->yst_ctime;
796 inode->i_size = yaffs_GetObjectFileLength(obj);
797 inode->i_blocks = (inode->i_size + 511) >> 9;
799 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
803 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
804 inode->i_mode, inode->i_uid, inode->i_gid,
805 (int)inode->i_size, atomic_read(&inode->i_count)));
807 switch (obj->yst_mode & S_IFMT) {
808 default: /* fifo, device or socket */
809 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
810 init_special_inode(inode, obj->yst_mode,
811 old_decode_dev(obj->yst_rdev));
813 init_special_inode(inode, obj->yst_mode,
814 (dev_t) (obj->yst_rdev));
817 case S_IFREG: /* file */
818 inode->i_op = &yaffs_file_inode_operations;
819 inode->i_fop = &yaffs_file_operations;
820 inode->i_mapping->a_ops =
821 &yaffs_file_address_operations;
823 case S_IFDIR: /* directory */
824 inode->i_op = &yaffs_dir_inode_operations;
825 inode->i_fop = &yaffs_dir_operations;
827 case S_IFLNK: /* symlink */
828 inode->i_op = &yaffs_symlink_inode_operations;
832 yaffs_InodeToObjectLV(inode) = obj;
834 obj->myInode = inode;
838 (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
843 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
850 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
857 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
863 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
865 inode = Y_IGET(sb, obj->objectId);
869 /* NB Side effect: iget calls back to yaffs_read_inode(). */
870 /* iget also increments the inode's i_count */
871 /* NB You can't be holding grossLock or deadlock will happen! */
876 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
884 obj = yaffs_DentryToObject(f->f_dentry);
888 yaffs_GrossLock(dev);
890 inode = f->f_dentry->d_inode;
892 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
893 ipos = inode->i_size;
900 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
904 "yaffs_file_write about to write writing %d bytes"
905 "to object %d at %d\n",
906 n, obj->objectId, ipos));
909 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
912 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
917 if (ipos > inode->i_size) {
918 inode->i_size = ipos;
919 inode->i_blocks = (ipos + 511) >> 9;
923 "yaffs_file_write size updated to %d bytes, "
925 ipos, (int)(inode->i_blocks)));
929 yaffs_GrossUnlock(dev);
930 return nWritten == 0 ? -ENOSPC : nWritten;
933 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
937 struct inode *inode = f->f_dentry->d_inode;
938 unsigned long offset, curoffs;
939 struct ylist_head *i;
942 char name[YAFFS_MAX_NAME_LENGTH + 1];
944 obj = yaffs_DentryToObject(f->f_dentry);
947 yaffs_GrossLock(dev);
951 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
955 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
957 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
966 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
967 (int)f->f_dentry->d_parent->d_inode->i_ino));
969 (dirent, "..", 2, offset,
970 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
979 /* If the directory has changed since the open or last call to
980 readdir, rewind to after the 2 canned entries. */
982 if (f->f_version != inode->i_version) {
985 f->f_version = inode->i_version;
988 ylist_for_each(i, &obj->variant.directoryVariant.children) {
990 if (curoffs >= offset) {
991 l = ylist_entry(i, yaffs_Object, siblings);
993 yaffs_GetObjectName(l, name,
994 YAFFS_MAX_NAME_LENGTH + 1);
996 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
997 yaffs_GetObjectInode(l)));
1003 yaffs_GetObjectInode(l),
1004 yaffs_GetObjectType(l))
1017 yaffs_GrossUnlock(dev);
1023 * File creation. Allocate an inode, and we're done..
1025 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1026 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1029 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1033 struct inode *inode;
1035 yaffs_Object *obj = NULL;
1038 yaffs_Object *parent = yaffs_InodeToObject(dir);
1040 int error = -ENOSPC;
1041 uid_t uid = current->fsuid;
1042 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1044 if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1049 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
1050 parent->objectId, parent->variantType));
1053 (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
1057 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1059 dentry->d_name.name, mode, rdev));
1061 dev = parent->myDev;
1063 yaffs_GrossLock(dev);
1065 switch (mode & S_IFMT) {
1067 /* Special (socket, fifo, device...) */
1068 T(YAFFS_TRACE_OS, (KERN_DEBUG
1069 "yaffs_mknod: making special\n"));
1070 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1072 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1073 gid, old_encode_dev(rdev));
1076 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1080 case S_IFREG: /* file */
1081 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1083 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1086 case S_IFDIR: /* directory */
1088 (KERN_DEBUG "yaffs_mknod: making directory\n"));
1090 yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1093 case S_IFLNK: /* symlink */
1094 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1095 obj = NULL; /* Do we ever get here? */
1099 /* Can not call yaffs_get_inode() with gross lock held */
1100 yaffs_GrossUnlock(dev);
1103 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1104 d_instantiate(dentry, inode);
1106 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
1107 obj->objectId, atomic_read(&inode->i_count)));
1111 (KERN_DEBUG "yaffs_mknod failed making object\n"));
1118 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1121 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
1122 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1124 /* attempt to fix dir bug - didn't work */
1132 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1133 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1134 struct nameidata *n)
1136 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1139 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1140 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1143 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1150 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1151 dentry->d_name.name));
1153 dev = yaffs_InodeToObject(dir)->myDev;
1155 yaffs_GrossLock(dev);
1157 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1159 if (retVal == YAFFS_OK) {
1160 dentry->d_inode->i_nlink--;
1162 yaffs_GrossUnlock(dev);
1163 mark_inode_dirty(dentry->d_inode);
1166 yaffs_GrossUnlock(dev);
1173 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1174 struct dentry *dentry)
1176 struct inode *inode = old_dentry->d_inode;
1177 yaffs_Object *obj = NULL;
1178 yaffs_Object *link = NULL;
1181 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1183 obj = yaffs_InodeToObject(inode);
1186 yaffs_GrossLock(dev);
1188 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1191 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1196 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1197 d_instantiate(dentry, old_dentry->d_inode);
1198 atomic_inc(&old_dentry->d_inode->i_count);
1200 (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1201 old_dentry->d_inode->i_nlink,
1202 atomic_read(&old_dentry->d_inode->i_count)));
1206 yaffs_GrossUnlock(dev);
1216 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1217 const char *symname)
1221 uid_t uid = current->fsuid;
1222 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1224 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1226 dev = yaffs_InodeToObject(dir)->myDev;
1227 yaffs_GrossLock(dev);
1228 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1229 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1230 yaffs_GrossUnlock(dev);
1234 struct inode *inode;
1236 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1237 d_instantiate(dentry, inode);
1238 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1241 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1248 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1255 obj = yaffs_DentryToObject(dentry);
1259 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1260 yaffs_GrossLock(dev);
1261 yaffs_FlushFile(obj, 1);
1262 yaffs_GrossUnlock(dev);
1267 * The VFS layer already does all the dentry stuff for rename.
1269 * NB: POSIX says you can rename an object over an old object of the same name
1271 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1272 struct inode *new_dir, struct dentry *new_dentry)
1275 int retVal = YAFFS_FAIL;
1276 yaffs_Object *target;
1278 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1279 dev = yaffs_InodeToObject(old_dir)->myDev;
1281 yaffs_GrossLock(dev);
1283 /* Check if the target is an existing directory that is not empty. */
1285 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1286 new_dentry->d_name.name);
1291 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1292 !ylist_empty(&target->variant.directoryVariant.children)) {
1294 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1296 retVal = YAFFS_FAIL;
1299 /* Now does unlinking internally using shadowing mechanism */
1300 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1303 yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1304 old_dentry->d_name.name,
1305 yaffs_InodeToObject(new_dir),
1306 new_dentry->d_name.name);
1309 yaffs_GrossUnlock(dev);
1311 if (retVal == YAFFS_OK) {
1313 new_dentry->d_inode->i_nlink--;
1314 mark_inode_dirty(new_dentry->d_inode);
1324 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1326 struct inode *inode = dentry->d_inode;
1331 (KERN_DEBUG "yaffs_setattr of object %d\n",
1332 yaffs_InodeToObject(inode)->objectId));
1334 if ((error = inode_change_ok(inode, attr)) == 0) {
1336 dev = yaffs_InodeToObject(inode)->myDev;
1337 yaffs_GrossLock(dev);
1338 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1344 yaffs_GrossUnlock(dev);
1346 error = inode_setattr(inode, attr);
1351 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1352 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1354 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1355 struct super_block *sb = dentry->d_sb;
1356 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1357 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1359 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1361 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1363 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1366 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1368 yaffs_GrossLock(dev);
1370 buf->f_type = YAFFS_MAGIC;
1371 buf->f_bsize = sb->s_blocksize;
1372 buf->f_namelen = 255;
1374 if(dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)){
1375 /* Do this if chunk size is not a power of 2 */
1377 uint64_t bytesInDev;
1380 bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock +1))) *
1381 ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
1383 do_div(bytesInDev,sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1384 buf->f_blocks = bytesInDev;
1386 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1387 ((uint64_t)(dev->nDataBytesPerChunk));
1389 do_div(bytesFree,sb->s_blocksize);
1391 buf->f_bfree = bytesFree;
1393 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1396 (dev->endBlock - dev->startBlock + 1) *
1397 dev->nChunksPerBlock /
1398 (sb->s_blocksize / dev->nDataBytesPerChunk);
1400 yaffs_GetNumberOfFreeChunks(dev) /
1401 (sb->s_blocksize / dev->nDataBytesPerChunk);
1404 (dev->endBlock - dev->startBlock + 1) *
1405 dev->nChunksPerBlock *
1406 (dev->nDataBytesPerChunk / sb->s_blocksize);
1409 yaffs_GetNumberOfFreeChunks(dev) *
1410 (dev->nDataBytesPerChunk / sb->s_blocksize);
1416 buf->f_bavail = buf->f_bfree;
1418 yaffs_GrossUnlock(dev);
1423 static int yaffs_do_sync_fs(struct super_block *sb)
1426 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1427 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
1430 yaffs_GrossLock(dev);
1433 yaffs_FlushEntireDeviceCache(dev);
1434 yaffs_CheckpointSave(dev);
1437 yaffs_GrossUnlock(dev);
1445 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1446 static void yaffs_write_super(struct super_block *sb)
1448 static int yaffs_write_super(struct super_block *sb)
1452 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
1453 yaffs_do_sync_fs(sb);
1454 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
1460 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1461 static int yaffs_sync_fs(struct super_block *sb, int wait)
1463 static int yaffs_sync_fs(struct super_block *sb)
1467 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
1469 yaffs_do_sync_fs(sb);
1475 #ifdef YAFFS_USE_OWN_IGET
1477 static struct inode * yaffs_iget(struct super_block *sb, unsigned long ino)
1479 struct inode *inode;
1481 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1484 (KERN_DEBUG "yaffs_iget for %lu\n", ino));
1486 inode = iget_locked(sb, ino);
1488 return ERR_PTR(-ENOMEM);
1489 if (!(inode->i_state & I_NEW))
1492 /* NB This is called as a side effect of other functions, but
1493 * we had to release the lock to prevent deadlocks, so
1494 * need to lock again.
1497 yaffs_GrossLock(dev);
1499 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1501 yaffs_FillInodeFromObject(inode, obj);
1503 yaffs_GrossUnlock(dev);
1505 unlock_new_inode(inode);
1511 static void yaffs_read_inode(struct inode *inode)
1513 /* NB This is called as a side effect of other functions, but
1514 * we had to release the lock to prevent deadlocks, so
1515 * need to lock again.
1519 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1522 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1524 yaffs_GrossLock(dev);
1526 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1528 yaffs_FillInodeFromObject(inode, obj);
1530 yaffs_GrossUnlock(dev);
1535 static YLIST_HEAD(yaffs_dev_list);
1538 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1540 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1542 if( *flags & MS_RDONLY ) {
1543 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1546 (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
1548 yaffs_GrossLock(dev);
1550 yaffs_FlushEntireDeviceCache(dev);
1552 yaffs_CheckpointSave(dev);
1557 yaffs_GrossUnlock(dev);
1561 (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
1568 static void yaffs_put_super(struct super_block *sb)
1570 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1572 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
1574 yaffs_GrossLock(dev);
1576 yaffs_FlushEntireDeviceCache(dev);
1578 yaffs_CheckpointSave(dev);
1580 if (dev->putSuperFunc) {
1581 dev->putSuperFunc(sb);
1584 yaffs_Deinitialise(dev);
1586 yaffs_GrossUnlock(dev);
1588 /* we assume this is protected by lock_kernel() in mount/umount */
1589 ylist_del(&dev->devList);
1591 if(dev->spareBuffer){
1592 YFREE(dev->spareBuffer);
1593 dev->spareBuffer = NULL;
1600 static void yaffs_MTDPutSuper(struct super_block *sb)
1603 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1609 put_mtd_device(mtd);
1613 static void yaffs_MarkSuperBlockDirty(void *vsb)
1615 struct super_block *sb = (struct super_block *)vsb;
1617 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
1624 int skip_checkpoint_read;
1625 int skip_checkpoint_write;
1629 #define MAX_OPT_LEN 20
1630 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
1632 char cur_opt[MAX_OPT_LEN+1];
1636 /* Parse through the options which is a comma seperated list */
1638 while(options_str && *options_str && !error){
1639 memset(cur_opt,0,MAX_OPT_LEN+1);
1642 while(*options_str && *options_str != ','){
1643 if(p < MAX_OPT_LEN){
1644 cur_opt[p] = *options_str;
1650 if(!strcmp(cur_opt,"inband-tags"))
1651 options->inband_tags = 1;
1652 else if(!strcmp(cur_opt,"no-cache"))
1653 options->no_cache = 1;
1654 else if(!strcmp(cur_opt,"no-checkpoint-read"))
1655 options->skip_checkpoint_read = 1;
1656 else if(!strcmp(cur_opt,"no-checkpoint-write"))
1657 options->skip_checkpoint_write = 1;
1658 else if(!strcmp(cur_opt,"no-checkpoint")){
1659 options->skip_checkpoint_read = 1;
1660 options->skip_checkpoint_write = 1;
1662 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
1671 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1672 struct super_block *sb,
1673 void *data, int silent)
1676 struct inode *inode = NULL;
1677 struct dentry *root;
1678 yaffs_Device *dev = 0;
1679 char devname_buf[BDEVNAME_SIZE + 1];
1680 struct mtd_info *mtd;
1682 char *data_str = (char *)data;
1684 yaffs_options options;
1686 sb->s_magic = YAFFS_MAGIC;
1687 sb->s_op = &yaffs_super_ops;
1688 sb->s_flags |= MS_NOATIME;
1691 printk(KERN_INFO "yaffs: sb is NULL\n");
1692 else if (!sb->s_dev)
1693 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1694 else if (!yaffs_devname(sb, devname_buf))
1695 printk(KERN_INFO "yaffs: devname is NULL\n");
1697 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1699 yaffs_devname(sb, devname_buf));
1704 printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
1706 memset(&options,0,sizeof(options));
1708 if(yaffs_parse_options(&options,data_str)){
1709 /* Option parsing failed */
1714 sb->s_blocksize = PAGE_CACHE_SIZE;
1715 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1716 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1718 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1720 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1722 ("yaffs: Write verification disabled. All guarantees "
1723 "null and void\n"));
1726 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1728 MAJOR(sb->s_dev), MINOR(sb->s_dev),
1729 yaffs_devname(sb, devname_buf)));
1731 /* Check it's an mtd device..... */
1732 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1733 return NULL; /* This isn't an mtd device */
1735 /* Get the device */
1736 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1738 T(YAFFS_TRACE_ALWAYS,
1739 ("yaffs: MTD device #%u doesn't appear to exist\n",
1743 /* Check it's NAND */
1744 if (mtd->type != MTD_NANDFLASH) {
1745 T(YAFFS_TRACE_ALWAYS,
1746 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1750 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1751 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1752 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1753 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1754 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1755 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1756 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1757 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
1758 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1759 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1760 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1762 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1764 if (yaffsVersion == 1 &&
1765 WRITE_SIZE(mtd) >= 2048) {
1766 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1770 /* Added NCB 26/5/2006 for completeness */
1771 if (yaffsVersion == 2 &&
1772 !options.inband_tags &&
1773 WRITE_SIZE(mtd) == 512){
1774 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
1780 if (yaffsVersion == 2) {
1781 /* Check for version 2 style functions */
1783 !mtd->block_isbad ||
1784 !mtd->block_markbad ||
1787 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1788 !mtd->read_oob || !mtd->write_oob) {
1791 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1793 T(YAFFS_TRACE_ALWAYS,
1794 ("yaffs: MTD device does not support required "
1799 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1800 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
1801 !options.inband_tags) {
1802 T(YAFFS_TRACE_ALWAYS,
1803 ("yaffs: MTD device does not have the "
1804 "right page sizes\n"));
1808 /* Check for V1 style functions */
1812 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1813 !mtd->read_oob || !mtd->write_oob) {
1816 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1818 T(YAFFS_TRACE_ALWAYS,
1819 ("yaffs: MTD device does not support required "
1824 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
1825 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1826 T(YAFFS_TRACE_ALWAYS,
1827 ("yaffs: MTD device does not support have the "
1828 "right page sizes\n"));
1833 /* OK, so if we got here, we have an MTD that's NAND and looks
1834 * like it has the right capabilities
1835 * Set the yaffs_Device up for mtd
1838 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1839 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1841 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1844 /* Deep shit could not allocate device structure */
1845 T(YAFFS_TRACE_ALWAYS,
1846 ("yaffs_read_super: Failed trying to allocate "
1847 "yaffs_Device. \n"));
1851 memset(dev, 0, sizeof(yaffs_Device));
1852 dev->genericDevice = mtd;
1853 dev->name = mtd->name;
1855 /* Set up the memory size parameters.... */
1857 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1858 dev->startBlock = 0;
1859 dev->endBlock = nBlocks - 1;
1860 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1861 dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1862 dev->nReservedBlocks = 5;
1863 dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
1864 dev->inbandTags = options.inband_tags;
1866 /* ... and the functions. */
1867 if (yaffsVersion == 2) {
1868 dev->writeChunkWithTagsToNAND =
1869 nandmtd2_WriteChunkWithTagsToNAND;
1870 dev->readChunkWithTagsFromNAND =
1871 nandmtd2_ReadChunkWithTagsFromNAND;
1872 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1873 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1874 dev->spareBuffer = YMALLOC(mtd->oobsize);
1876 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1877 dev->totalBytesPerChunk = mtd->writesize;
1878 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
1880 dev->totalBytesPerChunk = mtd->oobblock;
1881 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1883 nBlocks = mtd->size / mtd->erasesize;
1885 dev->startBlock = 0;
1886 dev->endBlock = nBlocks - 1;
1888 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1889 /* use the MTD interface in yaffs_mtdif1.c */
1890 dev->writeChunkWithTagsToNAND =
1891 nandmtd1_WriteChunkWithTagsToNAND;
1892 dev->readChunkWithTagsFromNAND =
1893 nandmtd1_ReadChunkWithTagsFromNAND;
1894 dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
1895 dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
1897 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1898 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1902 /* ... and common functions */
1903 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1904 dev->initialiseNAND = nandmtd_InitialiseNAND;
1906 dev->putSuperFunc = yaffs_MTDPutSuper;
1908 dev->superBlock = (void *)sb;
1909 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
1912 #ifndef CONFIG_YAFFS_DOES_ECC
1913 dev->useNANDECC = 1;
1916 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1917 dev->wideTnodesDisabled = 1;
1920 dev->skipCheckpointRead = options.skip_checkpoint_read;
1921 dev->skipCheckpointWrite = options.skip_checkpoint_write;
1923 /* we assume this is protected by lock_kernel() in mount/umount */
1924 ylist_add_tail(&dev->devList, &yaffs_dev_list);
1926 init_MUTEX(&dev->grossLock);
1928 yaffs_GrossLock(dev);
1930 err = yaffs_GutsInitialise(dev);
1933 ("yaffs_read_super: guts initialised %s\n",
1934 (err == YAFFS_OK) ? "OK" : "FAILED"));
1936 /* Release lock before yaffs_get_inode() */
1937 yaffs_GrossUnlock(dev);
1939 /* Create root inode */
1940 if (err == YAFFS_OK)
1941 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1947 inode->i_op = &yaffs_dir_inode_operations;
1948 inode->i_fop = &yaffs_dir_operations;
1950 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1952 root = d_alloc_root(inode);
1954 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1962 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1967 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1968 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
1971 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
1974 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1975 static int yaffs_read_super(struct file_system_type *fs,
1976 int flags, const char *dev_name,
1977 void *data, struct vfsmount *mnt)
1980 return get_sb_bdev(fs, flags, dev_name, data,
1981 yaffs_internal_read_super_mtd, mnt);
1984 static struct super_block *yaffs_read_super(struct file_system_type *fs,
1985 int flags, const char *dev_name,
1989 return get_sb_bdev(fs, flags, dev_name, data,
1990 yaffs_internal_read_super_mtd);
1994 static struct file_system_type yaffs_fs_type = {
1995 .owner = THIS_MODULE,
1997 .get_sb = yaffs_read_super,
1998 .kill_sb = kill_block_super,
1999 .fs_flags = FS_REQUIRES_DEV,
2002 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2005 return yaffs_internal_read_super(1, sb, data, silent);
2008 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2013 #ifdef CONFIG_YAFFS_YAFFS2
2015 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2016 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2019 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2022 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2023 static int yaffs2_read_super(struct file_system_type *fs,
2024 int flags, const char *dev_name, void *data,
2025 struct vfsmount *mnt)
2027 return get_sb_bdev(fs, flags, dev_name, data,
2028 yaffs2_internal_read_super_mtd, mnt);
2031 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2032 int flags, const char *dev_name,
2036 return get_sb_bdev(fs, flags, dev_name, data,
2037 yaffs2_internal_read_super_mtd);
2041 static struct file_system_type yaffs2_fs_type = {
2042 .owner = THIS_MODULE,
2044 .get_sb = yaffs2_read_super,
2045 .kill_sb = kill_block_super,
2046 .fs_flags = FS_REQUIRES_DEV,
2049 static struct super_block *yaffs2_read_super(struct super_block *sb,
2050 void *data, int silent)
2052 return yaffs_internal_read_super(2, sb, data, silent);
2055 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2059 #endif /* CONFIG_YAFFS_YAFFS2 */
2061 static struct proc_dir_entry *my_proc_entry;
2063 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
2065 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
2066 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
2067 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
2068 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2069 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2070 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2071 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2072 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
2073 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2074 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2075 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2076 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2077 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2078 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2079 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2080 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2081 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2082 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2083 buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2084 buf += sprintf(buf, "passiveGCs......... %d\n",
2085 dev->passiveGarbageCollections);
2086 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2087 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
2088 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2089 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2090 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2091 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2092 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2093 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2094 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2095 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2097 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2098 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
2099 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
2100 buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
2105 static int yaffs_proc_read(char *page,
2107 off_t offset, int count, int *eof, void *data)
2109 struct ylist_head *item;
2114 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2115 * We use 'offset' (*ppos) to indicate where we are in devList.
2116 * This also assumes the user has posted a read buffer large
2117 * enough to hold the complete output; but that's life in /proc.
2122 /* Print header first */
2124 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2125 "\n%s\n%s\n", yaffs_fs_c_version,
2126 yaffs_guts_c_version);
2129 /* hold lock_kernel while traversing yaffs_dev_list */
2132 /* Locate and print the Nth entry. Order N-squared but N is small. */
2133 ylist_for_each(item, &yaffs_dev_list) {
2134 yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
2139 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
2140 buf = yaffs_dump_dev(buf, dev);
2145 return buf - page < count ? buf - page : count;
2149 * Set the verbosity of the warnings and error messages.
2151 * Note that the names can only be a..z or _ with the current code.
2156 unsigned mask_bitfield;
2158 {"allocate", YAFFS_TRACE_ALLOCATE},
2159 {"always", YAFFS_TRACE_ALWAYS},
2160 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2161 {"buffers", YAFFS_TRACE_BUFFERS},
2162 {"bug", YAFFS_TRACE_BUG},
2163 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2164 {"deletion", YAFFS_TRACE_DELETION},
2165 {"erase", YAFFS_TRACE_ERASE},
2166 {"error", YAFFS_TRACE_ERROR},
2167 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2168 {"gc", YAFFS_TRACE_GC},
2169 {"mtd", YAFFS_TRACE_MTD},
2170 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2171 {"os", YAFFS_TRACE_OS},
2172 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2173 {"scan", YAFFS_TRACE_SCAN},
2174 {"tracing", YAFFS_TRACE_TRACING},
2176 {"verify", YAFFS_TRACE_VERIFY},
2177 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2178 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2179 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2181 {"write", YAFFS_TRACE_WRITE},
2182 {"all", 0xffffffff},
2187 #define MAX_MASK_NAME_LENGTH 40
2188 static int yaffs_proc_write(struct file *file, const char *buf,
2189 unsigned long count, void *data)
2191 unsigned rg = 0, mask_bitfield;
2195 char substring[MAX_MASK_NAME_LENGTH+1];
2201 rg = yaffs_traceMask;
2203 while (!done && (pos < count)) {
2205 while ((pos < count) && isspace(buf[pos])) {
2223 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2224 if (end > buf + pos) {
2225 mask_name = "numeral";
2226 len = end - (buf + pos);
2230 for(x = buf + pos, i = 0;
2231 (*x == '_' || (*x >='a' && *x <= 'z')) &&
2232 i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2234 substring[i] = '\0';
2236 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2237 if(strcmp(substring,mask_flags[i].mask_name) == 0){
2238 mask_name = mask_flags[i].mask_name;
2239 mask_bitfield = mask_flags[i].mask_bitfield;
2246 if (mask_name != NULL) {
2250 rg &= ~mask_bitfield;
2253 rg |= mask_bitfield;
2259 rg |= mask_bitfield;
2265 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2267 printk("new trace = 0x%08X\n",yaffs_traceMask);
2269 if (rg & YAFFS_TRACE_ALWAYS) {
2270 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2272 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2273 printk("%c%s\n", flag, mask_flags[i].mask_name);
2280 /* Stuff to handle installation of file systems */
2281 struct file_system_to_install {
2282 struct file_system_type *fst;
2286 static struct file_system_to_install fs_to_install[] = {
2287 //#ifdef CONFIG_YAFFS_YAFFS1
2288 {&yaffs_fs_type, 0},
2290 //#ifdef CONFIG_YAFFS_YAFFS2
2291 {&yaffs2_fs_type, 0},
2296 static int __init init_yaffs_fs(void)
2299 struct file_system_to_install *fsinst;
2301 T(YAFFS_TRACE_ALWAYS,
2302 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2304 /* Install the proc_fs entry */
2305 my_proc_entry = create_proc_entry("yaffs",
2309 if (my_proc_entry) {
2310 my_proc_entry->write_proc = yaffs_proc_write;
2311 my_proc_entry->read_proc = yaffs_proc_read;
2312 my_proc_entry->data = NULL;
2317 /* Now add the file system entries */
2319 fsinst = fs_to_install;
2321 while (fsinst->fst && !error) {
2322 error = register_filesystem(fsinst->fst);
2324 fsinst->installed = 1;
2329 /* Any errors? uninstall */
2331 fsinst = fs_to_install;
2333 while (fsinst->fst) {
2334 if (fsinst->installed) {
2335 unregister_filesystem(fsinst->fst);
2336 fsinst->installed = 0;
2345 static void __exit exit_yaffs_fs(void)
2348 struct file_system_to_install *fsinst;
2350 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2353 remove_proc_entry("yaffs", &proc_root);
2355 fsinst = fs_to_install;
2357 while (fsinst->fst) {
2358 if (fsinst->installed) {
2359 unregister_filesystem(fsinst->fst);
2360 fsinst->installed = 0;
2367 module_init(init_yaffs_fs)
2368 module_exit(exit_yaffs_fs)
2370 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2371 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2372 MODULE_LICENSE("GPL");