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.69 2008-08-28 02:42:11 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;
107 /* Module Parameters */
108 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
109 module_param(yaffs_traceMask,uint,0644);
110 module_param(yaffs_wr_attempts,uint,0644);
112 MODULE_PARM(yaffs_traceMask,"i");
113 MODULE_PARM(yaffs_wr_attempts,"i");
116 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
117 /* use iget and read_inode */
118 #define Y_IGET(sb,inum) iget((sb),(inum))
119 static void yaffs_read_inode(struct inode *inode);
122 /* Call local equivalent */
123 #define YAFFS_USE_OWN_IGET
124 #define Y_IGET(sb,inum) yaffs_iget((sb),(inum))
126 static struct inode * yaffs_iget(struct super_block *sb, unsigned long ino);
129 /*#define T(x) printk x */
131 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
132 #define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
134 #define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
137 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
138 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
140 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
141 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
143 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
146 static void yaffs_put_super(struct super_block *sb);
148 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
151 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
152 static int yaffs_file_flush(struct file *file, fl_owner_t id);
154 static int yaffs_file_flush(struct file *file);
157 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
160 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
162 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
163 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
164 struct nameidata *n);
165 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
166 struct nameidata *n);
168 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
169 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
171 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
172 struct dentry *dentry);
173 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
174 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
175 const char *symname);
176 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
178 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
179 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
182 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
185 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
186 struct inode *new_dir, struct dentry *new_dentry);
187 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
189 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
190 static int yaffs_sync_fs(struct super_block *sb, int wait);
191 static void yaffs_write_super(struct super_block *sb);
193 static int yaffs_sync_fs(struct super_block *sb);
194 static int yaffs_write_super(struct super_block *sb);
197 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
198 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
199 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
200 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
202 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
205 #ifdef YAFFS_HAS_PUT_INODE
206 static void yaffs_put_inode(struct inode *inode);
209 static void yaffs_delete_inode(struct inode *);
210 static void yaffs_clear_inode(struct inode *);
212 static int yaffs_readpage(struct file *file, struct page *page);
213 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
214 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
216 static int yaffs_writepage(struct page *page);
218 static int yaffs_prepare_write(struct file *f, struct page *pg,
219 unsigned offset, unsigned to);
220 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
223 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
225 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
226 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
228 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
231 static struct address_space_operations yaffs_file_address_operations = {
232 .readpage = yaffs_readpage,
233 .writepage = yaffs_writepage,
234 .prepare_write = yaffs_prepare_write,
235 .commit_write = yaffs_commit_write,
238 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,22))
239 static struct file_operations yaffs_file_operations = {
240 .read = do_sync_read,
241 .write = do_sync_write,
242 .aio_read = generic_file_aio_read,
243 .aio_write = generic_file_aio_write,
244 .mmap = generic_file_mmap,
245 .flush = yaffs_file_flush,
246 .fsync = yaffs_sync_object,
247 .splice_read = generic_file_splice_read,
248 .splice_write = generic_file_splice_write,
251 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
253 static struct file_operations yaffs_file_operations = {
254 .read = do_sync_read,
255 .write = do_sync_write,
256 .aio_read = generic_file_aio_read,
257 .aio_write = generic_file_aio_write,
258 .mmap = generic_file_mmap,
259 .flush = yaffs_file_flush,
260 .fsync = yaffs_sync_object,
261 .sendfile = generic_file_sendfile,
266 static struct file_operations yaffs_file_operations = {
267 .read = generic_file_read,
268 .write = generic_file_write,
269 .mmap = generic_file_mmap,
270 .flush = yaffs_file_flush,
271 .fsync = yaffs_sync_object,
272 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
273 .sendfile = generic_file_sendfile,
278 static struct inode_operations yaffs_file_inode_operations = {
279 .setattr = yaffs_setattr,
282 static struct inode_operations yaffs_symlink_inode_operations = {
283 .readlink = yaffs_readlink,
284 .follow_link = yaffs_follow_link,
285 .setattr = yaffs_setattr,
288 static struct inode_operations yaffs_dir_inode_operations = {
289 .create = yaffs_create,
290 .lookup = yaffs_lookup,
292 .unlink = yaffs_unlink,
293 .symlink = yaffs_symlink,
294 .mkdir = yaffs_mkdir,
295 .rmdir = yaffs_unlink,
296 .mknod = yaffs_mknod,
297 .rename = yaffs_rename,
298 .setattr = yaffs_setattr,
301 static struct file_operations yaffs_dir_operations = {
302 .read = generic_read_dir,
303 .readdir = yaffs_readdir,
304 .fsync = yaffs_sync_object,
307 static struct super_operations yaffs_super_ops = {
308 .statfs = yaffs_statfs,
310 #ifndef YAFFS_USE_OWN_IGET
311 .read_inode = yaffs_read_inode,
313 #ifdef YAFFS_HAS_PUT_INODE
314 .put_inode = yaffs_put_inode,
316 .put_super = yaffs_put_super,
317 .delete_inode = yaffs_delete_inode,
318 .clear_inode = yaffs_clear_inode,
319 .sync_fs = yaffs_sync_fs,
320 .write_super = yaffs_write_super,
323 static void yaffs_GrossLock(yaffs_Device * dev)
325 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
327 down(&dev->grossLock);
330 static void yaffs_GrossUnlock(yaffs_Device * dev)
332 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
337 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
340 unsigned char *alias;
343 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
345 yaffs_GrossLock(dev);
347 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
349 yaffs_GrossUnlock(dev);
354 ret = vfs_readlink(dentry, buffer, buflen, alias);
359 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
360 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
362 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
365 unsigned char *alias;
367 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
369 yaffs_GrossLock(dev);
371 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
373 yaffs_GrossUnlock(dev);
381 ret = vfs_follow_link(nd, alias);
384 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
385 return ERR_PTR (ret);
391 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
395 * Lookup is used to find objects in the fs
397 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
399 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
402 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
406 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
408 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
410 yaffs_GrossLock(dev);
413 (KERN_DEBUG "yaffs_lookup for %d:%s\n",
414 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
417 yaffs_FindObjectByName(yaffs_InodeToObject(dir),
418 dentry->d_name.name);
420 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
422 /* Can't hold gross lock when calling yaffs_get_inode() */
423 yaffs_GrossUnlock(dev);
427 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
429 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
433 (KERN_DEBUG "yaffs_loookup dentry \n"));
434 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
435 * d_add even if NULL inode */
437 /*dget(dentry); // try to solve directory bug */
438 d_add(dentry, inode);
446 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
450 /* added NCB for 2.5/6 compatability - forces add even if inode is
451 * NULL which creates dentry hash */
452 d_add(dentry, inode);
455 /* return (ERR_PTR(-EIO)); */
460 #ifdef YAFFS_HAS_PUT_INODE
462 /* For now put inode is just for debugging
463 * Put inode is called when the inode **structure** is put.
465 static void yaffs_put_inode(struct inode *inode)
468 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
469 atomic_read(&inode->i_count)));
474 /* clear is called to tell the fs to release any per-inode data it holds */
475 static void yaffs_clear_inode(struct inode *inode)
480 obj = yaffs_InodeToObject(inode);
483 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
484 atomic_read(&inode->i_count),
485 obj ? "object exists" : "null object"));
489 yaffs_GrossLock(dev);
491 /* Clear the association between the inode and
495 yaffs_InodeToObjectLV(inode) = NULL;
497 /* If the object freeing was deferred, then the real
499 * This should fix the inode inconsistency problem.
502 yaffs_HandleDeferedFree(obj);
504 yaffs_GrossUnlock(dev);
509 /* delete is called when the link count is zero and the inode
510 * is put (ie. nobody wants to know about it anymore, time to
512 * NB Must call clear_inode()
514 static void yaffs_delete_inode(struct inode *inode)
516 yaffs_Object *obj = yaffs_InodeToObject(inode);
520 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
521 atomic_read(&inode->i_count),
522 obj ? "object exists" : "null object"));
526 yaffs_GrossLock(dev);
527 yaffs_DeleteFile(obj);
528 yaffs_GrossUnlock(dev);
530 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
531 truncate_inode_pages (&inode->i_data, 0);
536 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
537 static int yaffs_file_flush(struct file *file, fl_owner_t id)
539 static int yaffs_file_flush(struct file *file)
542 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
544 yaffs_Device *dev = obj->myDev;
547 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
548 obj->dirty ? "dirty" : "clean"));
550 yaffs_GrossLock(dev);
552 yaffs_FlushFile(obj, 1);
554 yaffs_GrossUnlock(dev);
559 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
561 /* Lifted from jffs2 */
564 unsigned char *pg_buf;
569 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
570 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
571 (unsigned)PAGE_CACHE_SIZE));
573 obj = yaffs_DentryToObject(f->f_dentry);
577 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
578 BUG_ON(!PageLocked(pg));
585 /* FIXME: Can kmap fail? */
587 yaffs_GrossLock(dev);
590 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
593 yaffs_GrossUnlock(dev);
599 ClearPageUptodate(pg);
606 flush_dcache_page(pg);
609 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
613 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
615 int ret = yaffs_readpage_nolock(f, pg);
620 static int yaffs_readpage(struct file *f, struct page *pg)
622 return yaffs_readpage_unlock(f, pg);
625 /* writepage inspired by/stolen from smbfs */
627 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
628 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
630 static int yaffs_writepage(struct page *page)
633 struct address_space *mapping = page->mapping;
634 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
636 unsigned long end_index;
644 inode = mapping->host;
648 if (offset > inode->i_size) {
651 "yaffs_writepage at %08x, inode size = %08x!!!\n",
652 (unsigned)(page->index << PAGE_CACHE_SHIFT),
653 (unsigned)inode->i_size));
655 (KERN_DEBUG " -> don't care!!\n"));
660 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
663 if (page->index < end_index) {
664 nBytes = PAGE_CACHE_SIZE;
666 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
673 obj = yaffs_InodeToObject(inode);
674 yaffs_GrossLock(obj->myDev);
677 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
678 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
680 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
681 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
684 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
688 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
689 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
691 yaffs_GrossUnlock(obj->myDev);
694 SetPageUptodate(page);
698 return (nWritten == nBytes) ? 0 : -ENOSPC;
701 static int yaffs_prepare_write(struct file *f, struct page *pg,
702 unsigned offset, unsigned to)
705 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
706 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
707 return yaffs_readpage_nolock(f, pg);
713 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
719 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
720 int nBytes = to - offset;
724 unsigned saddr = (unsigned)addr;
730 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
733 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
735 if (nWritten != nBytes) {
738 "yaffs_commit_write not same size nWritten %d nBytes %d\n",
741 ClearPageUptodate(pg);
749 (KERN_DEBUG "yaffs_commit_write returning %d\n",
750 nWritten == nBytes ? 0 : nWritten));
752 return nWritten == nBytes ? 0 : nWritten;
756 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
761 /* Check mode against the variant type and attempt to repair if broken. */
762 __u32 mode = obj->yst_mode;
763 switch( obj->variantType ){
764 case YAFFS_OBJECT_TYPE_FILE :
765 if( ! S_ISREG(mode) ){
766 obj->yst_mode &= ~S_IFMT;
767 obj->yst_mode |= S_IFREG;
771 case YAFFS_OBJECT_TYPE_SYMLINK :
772 if( ! S_ISLNK(mode) ){
773 obj->yst_mode &= ~S_IFMT;
774 obj->yst_mode |= S_IFLNK;
778 case YAFFS_OBJECT_TYPE_DIRECTORY :
779 if( ! S_ISDIR(mode) ){
780 obj->yst_mode &= ~S_IFMT;
781 obj->yst_mode |= S_IFDIR;
785 case YAFFS_OBJECT_TYPE_UNKNOWN :
786 case YAFFS_OBJECT_TYPE_HARDLINK :
787 case YAFFS_OBJECT_TYPE_SPECIAL :
793 inode->i_flags |= S_NOATIME;
795 inode->i_ino = obj->objectId;
796 inode->i_mode = obj->yst_mode;
797 inode->i_uid = obj->yst_uid;
798 inode->i_gid = obj->yst_gid;
799 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
800 inode->i_blksize = inode->i_sb->s_blocksize;
802 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
804 inode->i_rdev = old_decode_dev(obj->yst_rdev);
805 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
806 inode->i_atime.tv_nsec = 0;
807 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
808 inode->i_mtime.tv_nsec = 0;
809 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
810 inode->i_ctime.tv_nsec = 0;
812 inode->i_rdev = obj->yst_rdev;
813 inode->i_atime = obj->yst_atime;
814 inode->i_mtime = obj->yst_mtime;
815 inode->i_ctime = obj->yst_ctime;
817 inode->i_size = yaffs_GetObjectFileLength(obj);
818 inode->i_blocks = (inode->i_size + 511) >> 9;
820 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
824 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
825 inode->i_mode, inode->i_uid, inode->i_gid,
826 (int)inode->i_size, atomic_read(&inode->i_count)));
828 switch (obj->yst_mode & S_IFMT) {
829 default: /* fifo, device or socket */
830 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
831 init_special_inode(inode, obj->yst_mode,
832 old_decode_dev(obj->yst_rdev));
834 init_special_inode(inode, obj->yst_mode,
835 (dev_t) (obj->yst_rdev));
838 case S_IFREG: /* file */
839 inode->i_op = &yaffs_file_inode_operations;
840 inode->i_fop = &yaffs_file_operations;
841 inode->i_mapping->a_ops =
842 &yaffs_file_address_operations;
844 case S_IFDIR: /* directory */
845 inode->i_op = &yaffs_dir_inode_operations;
846 inode->i_fop = &yaffs_dir_operations;
848 case S_IFLNK: /* symlink */
849 inode->i_op = &yaffs_symlink_inode_operations;
853 yaffs_InodeToObjectLV(inode) = obj;
855 obj->myInode = inode;
859 (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
864 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
871 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
878 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
884 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
886 inode = Y_IGET(sb, obj->objectId);
890 /* NB Side effect: iget calls back to yaffs_read_inode(). */
891 /* iget also increments the inode's i_count */
892 /* NB You can't be holding grossLock or deadlock will happen! */
897 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
905 obj = yaffs_DentryToObject(f->f_dentry);
909 yaffs_GrossLock(dev);
911 inode = f->f_dentry->d_inode;
913 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
914 ipos = inode->i_size;
921 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
925 "yaffs_file_write about to write writing %d bytes"
926 "to object %d at %d\n",
927 n, obj->objectId, ipos));
930 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
933 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
938 if (ipos > inode->i_size) {
939 inode->i_size = ipos;
940 inode->i_blocks = (ipos + 511) >> 9;
944 "yaffs_file_write size updated to %d bytes, "
946 ipos, (int)(inode->i_blocks)));
950 yaffs_GrossUnlock(dev);
951 return nWritten == 0 ? -ENOSPC : nWritten;
954 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
958 struct inode *inode = f->f_dentry->d_inode;
959 unsigned long offset, curoffs;
960 struct ylist_head *i;
963 char name[YAFFS_MAX_NAME_LENGTH + 1];
965 obj = yaffs_DentryToObject(f->f_dentry);
968 yaffs_GrossLock(dev);
972 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
976 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
978 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
987 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
988 (int)f->f_dentry->d_parent->d_inode->i_ino));
990 (dirent, "..", 2, offset,
991 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
1000 /* If the directory has changed since the open or last call to
1001 readdir, rewind to after the 2 canned entries. */
1003 if (f->f_version != inode->i_version) {
1006 f->f_version = inode->i_version;
1009 ylist_for_each(i, &obj->variant.directoryVariant.children) {
1011 if (curoffs >= offset) {
1012 l = ylist_entry(i, yaffs_Object, siblings);
1014 yaffs_GetObjectName(l, name,
1015 YAFFS_MAX_NAME_LENGTH + 1);
1017 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
1018 yaffs_GetObjectInode(l)));
1024 yaffs_GetObjectInode(l),
1025 yaffs_GetObjectType(l))
1038 yaffs_GrossUnlock(dev);
1044 * File creation. Allocate an inode, and we're done..
1046 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1047 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1050 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1054 struct inode *inode;
1056 yaffs_Object *obj = NULL;
1059 yaffs_Object *parent = yaffs_InodeToObject(dir);
1061 int error = -ENOSPC;
1062 uid_t uid = current->fsuid;
1063 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1065 if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
1070 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
1071 parent->objectId, parent->variantType));
1074 (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
1078 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1080 dentry->d_name.name, mode, rdev));
1082 dev = parent->myDev;
1084 yaffs_GrossLock(dev);
1086 switch (mode & S_IFMT) {
1088 /* Special (socket, fifo, device...) */
1089 T(YAFFS_TRACE_OS, (KERN_DEBUG
1090 "yaffs_mknod: making special\n"));
1091 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1093 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1094 gid, old_encode_dev(rdev));
1097 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1101 case S_IFREG: /* file */
1102 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1104 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1107 case S_IFDIR: /* directory */
1109 (KERN_DEBUG "yaffs_mknod: making directory\n"));
1111 yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1114 case S_IFLNK: /* symlink */
1115 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1116 obj = NULL; /* Do we ever get here? */
1120 /* Can not call yaffs_get_inode() with gross lock held */
1121 yaffs_GrossUnlock(dev);
1124 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1125 d_instantiate(dentry, inode);
1127 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
1128 obj->objectId, atomic_read(&inode->i_count)));
1132 (KERN_DEBUG "yaffs_mknod failed making object\n"));
1139 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1142 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
1143 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1145 /* attempt to fix dir bug - didn't work */
1153 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1154 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1155 struct nameidata *n)
1157 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1160 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1161 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1164 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1171 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1172 dentry->d_name.name));
1174 dev = yaffs_InodeToObject(dir)->myDev;
1176 yaffs_GrossLock(dev);
1178 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1180 if (retVal == YAFFS_OK) {
1181 dentry->d_inode->i_nlink--;
1183 yaffs_GrossUnlock(dev);
1184 mark_inode_dirty(dentry->d_inode);
1187 yaffs_GrossUnlock(dev);
1194 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1195 struct dentry *dentry)
1197 struct inode *inode = old_dentry->d_inode;
1198 yaffs_Object *obj = NULL;
1199 yaffs_Object *link = NULL;
1202 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1204 obj = yaffs_InodeToObject(inode);
1207 yaffs_GrossLock(dev);
1209 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1212 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1217 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1218 d_instantiate(dentry, old_dentry->d_inode);
1219 atomic_inc(&old_dentry->d_inode->i_count);
1221 (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1222 old_dentry->d_inode->i_nlink,
1223 atomic_read(&old_dentry->d_inode->i_count)));
1227 yaffs_GrossUnlock(dev);
1237 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1238 const char *symname)
1242 uid_t uid = current->fsuid;
1243 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1245 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1247 dev = yaffs_InodeToObject(dir)->myDev;
1248 yaffs_GrossLock(dev);
1249 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1250 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1251 yaffs_GrossUnlock(dev);
1255 struct inode *inode;
1257 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1258 d_instantiate(dentry, inode);
1259 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1262 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1269 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1276 obj = yaffs_DentryToObject(dentry);
1280 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1281 yaffs_GrossLock(dev);
1282 yaffs_FlushFile(obj, 1);
1283 yaffs_GrossUnlock(dev);
1288 * The VFS layer already does all the dentry stuff for rename.
1290 * NB: POSIX says you can rename an object over an old object of the same name
1292 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1293 struct inode *new_dir, struct dentry *new_dentry)
1296 int retVal = YAFFS_FAIL;
1297 yaffs_Object *target;
1299 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1300 dev = yaffs_InodeToObject(old_dir)->myDev;
1302 yaffs_GrossLock(dev);
1304 /* Check if the target is an existing directory that is not empty. */
1306 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1307 new_dentry->d_name.name);
1312 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1313 !ylist_empty(&target->variant.directoryVariant.children)) {
1315 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1317 retVal = YAFFS_FAIL;
1320 /* Now does unlinking internally using shadowing mechanism */
1321 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1324 yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1325 old_dentry->d_name.name,
1326 yaffs_InodeToObject(new_dir),
1327 new_dentry->d_name.name);
1330 yaffs_GrossUnlock(dev);
1332 if (retVal == YAFFS_OK) {
1334 new_dentry->d_inode->i_nlink--;
1335 mark_inode_dirty(new_dentry->d_inode);
1345 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1347 struct inode *inode = dentry->d_inode;
1352 (KERN_DEBUG "yaffs_setattr of object %d\n",
1353 yaffs_InodeToObject(inode)->objectId));
1355 if ((error = inode_change_ok(inode, attr)) == 0) {
1357 dev = yaffs_InodeToObject(inode)->myDev;
1358 yaffs_GrossLock(dev);
1359 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1365 yaffs_GrossUnlock(dev);
1367 error = inode_setattr(inode, attr);
1372 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1373 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1375 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1376 struct super_block *sb = dentry->d_sb;
1377 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1378 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1380 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1382 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1384 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1387 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1389 yaffs_GrossLock(dev);
1391 buf->f_type = YAFFS_MAGIC;
1392 buf->f_bsize = sb->s_blocksize;
1393 buf->f_namelen = 255;
1395 if(dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)){
1396 /* Do this if chunk size is not a power of 2 */
1398 uint64_t bytesInDev;
1401 bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock +1))) *
1402 ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
1404 do_div(bytesInDev,sb->s_blocksize); /* bytesInDev becomes the number of blocks */
1405 buf->f_blocks = bytesInDev;
1407 bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
1408 ((uint64_t)(dev->nDataBytesPerChunk));
1410 do_div(bytesFree,sb->s_blocksize);
1412 buf->f_bfree = bytesFree;
1414 } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1417 (dev->endBlock - dev->startBlock + 1) *
1418 dev->nChunksPerBlock /
1419 (sb->s_blocksize / dev->nDataBytesPerChunk);
1421 yaffs_GetNumberOfFreeChunks(dev) /
1422 (sb->s_blocksize / dev->nDataBytesPerChunk);
1425 (dev->endBlock - dev->startBlock + 1) *
1426 dev->nChunksPerBlock *
1427 (dev->nDataBytesPerChunk / sb->s_blocksize);
1430 yaffs_GetNumberOfFreeChunks(dev) *
1431 (dev->nDataBytesPerChunk / sb->s_blocksize);
1437 buf->f_bavail = buf->f_bfree;
1439 yaffs_GrossUnlock(dev);
1444 static int yaffs_do_sync_fs(struct super_block *sb)
1447 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1448 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
1451 yaffs_GrossLock(dev);
1454 yaffs_FlushEntireDeviceCache(dev);
1455 yaffs_CheckpointSave(dev);
1458 yaffs_GrossUnlock(dev);
1466 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1467 static void yaffs_write_super(struct super_block *sb)
1469 static int yaffs_write_super(struct super_block *sb)
1473 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
1474 yaffs_do_sync_fs(sb);
1475 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
1481 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1482 static int yaffs_sync_fs(struct super_block *sb, int wait)
1484 static int yaffs_sync_fs(struct super_block *sb)
1488 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
1490 yaffs_do_sync_fs(sb);
1496 #ifdef YAFFS_USE_OWN_IGET
1498 static struct inode * yaffs_iget(struct super_block *sb, unsigned long ino)
1500 struct inode *inode;
1502 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1505 (KERN_DEBUG "yaffs_iget for %lu\n", ino));
1507 inode = iget_locked(sb, ino);
1509 return ERR_PTR(-ENOMEM);
1510 if (!(inode->i_state & I_NEW))
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.
1518 yaffs_GrossLock(dev);
1520 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1522 yaffs_FillInodeFromObject(inode, obj);
1524 yaffs_GrossUnlock(dev);
1526 unlock_new_inode(inode);
1532 static void yaffs_read_inode(struct inode *inode)
1534 /* NB This is called as a side effect of other functions, but
1535 * we had to release the lock to prevent deadlocks, so
1536 * need to lock again.
1540 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1543 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1545 yaffs_GrossLock(dev);
1547 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1549 yaffs_FillInodeFromObject(inode, obj);
1551 yaffs_GrossUnlock(dev);
1556 static YLIST_HEAD(yaffs_dev_list);
1559 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1561 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1563 if( *flags & MS_RDONLY ) {
1564 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1567 (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
1569 yaffs_GrossLock(dev);
1571 yaffs_FlushEntireDeviceCache(dev);
1573 yaffs_CheckpointSave(dev);
1578 yaffs_GrossUnlock(dev);
1582 (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
1589 static void yaffs_put_super(struct super_block *sb)
1591 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1593 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
1595 yaffs_GrossLock(dev);
1597 yaffs_FlushEntireDeviceCache(dev);
1599 yaffs_CheckpointSave(dev);
1601 if (dev->putSuperFunc) {
1602 dev->putSuperFunc(sb);
1605 yaffs_Deinitialise(dev);
1607 yaffs_GrossUnlock(dev);
1609 /* we assume this is protected by lock_kernel() in mount/umount */
1610 ylist_del(&dev->devList);
1612 if(dev->spareBuffer){
1613 YFREE(dev->spareBuffer);
1614 dev->spareBuffer = NULL;
1621 static void yaffs_MTDPutSuper(struct super_block *sb)
1624 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1630 put_mtd_device(mtd);
1634 static void yaffs_MarkSuperBlockDirty(void *vsb)
1636 struct super_block *sb = (struct super_block *)vsb;
1638 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
1645 int skip_checkpoint_read;
1646 int skip_checkpoint_write;
1650 #define MAX_OPT_LEN 20
1651 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
1653 char cur_opt[MAX_OPT_LEN+1];
1657 /* Parse through the options which is a comma seperated list */
1659 while(options_str && *options_str && !error){
1660 memset(cur_opt,0,MAX_OPT_LEN+1);
1663 while(*options_str && *options_str != ','){
1664 if(p < MAX_OPT_LEN){
1665 cur_opt[p] = *options_str;
1671 if(!strcmp(cur_opt,"inband-tags"))
1672 options->inband_tags = 1;
1673 else if(!strcmp(cur_opt,"no-cache"))
1674 options->no_cache = 1;
1675 else if(!strcmp(cur_opt,"no-checkpoint-read"))
1676 options->skip_checkpoint_read = 1;
1677 else if(!strcmp(cur_opt,"no-checkpoint-write"))
1678 options->skip_checkpoint_write = 1;
1679 else if(!strcmp(cur_opt,"no-checkpoint")){
1680 options->skip_checkpoint_read = 1;
1681 options->skip_checkpoint_write = 1;
1683 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
1692 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1693 struct super_block *sb,
1694 void *data, int silent)
1697 struct inode *inode = NULL;
1698 struct dentry *root;
1699 yaffs_Device *dev = 0;
1700 char devname_buf[BDEVNAME_SIZE + 1];
1701 struct mtd_info *mtd;
1703 char *data_str = (char *)data;
1705 yaffs_options options;
1707 sb->s_magic = YAFFS_MAGIC;
1708 sb->s_op = &yaffs_super_ops;
1709 sb->s_flags |= MS_NOATIME;
1712 printk(KERN_INFO "yaffs: sb is NULL\n");
1713 else if (!sb->s_dev)
1714 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1715 else if (!yaffs_devname(sb, devname_buf))
1716 printk(KERN_INFO "yaffs: devname is NULL\n");
1718 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1720 yaffs_devname(sb, devname_buf));
1725 printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
1727 memset(&options,0,sizeof(options));
1729 if(yaffs_parse_options(&options,data_str)){
1730 /* Option parsing failed */
1735 sb->s_blocksize = PAGE_CACHE_SIZE;
1736 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1737 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1739 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1741 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1743 ("yaffs: Write verification disabled. All guarantees "
1744 "null and void\n"));
1747 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1749 MAJOR(sb->s_dev), MINOR(sb->s_dev),
1750 yaffs_devname(sb, devname_buf)));
1752 /* Check it's an mtd device..... */
1753 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1754 return NULL; /* This isn't an mtd device */
1756 /* Get the device */
1757 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1759 T(YAFFS_TRACE_ALWAYS,
1760 ("yaffs: MTD device #%u doesn't appear to exist\n",
1764 /* Check it's NAND */
1765 if (mtd->type != MTD_NANDFLASH) {
1766 T(YAFFS_TRACE_ALWAYS,
1767 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1771 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1772 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1773 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1774 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1775 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1776 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1777 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1778 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
1779 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1780 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1781 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1783 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1785 if (yaffsVersion == 1 &&
1786 WRITE_SIZE(mtd) >= 2048) {
1787 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1791 /* Added NCB 26/5/2006 for completeness */
1792 if (yaffsVersion == 2 &&
1793 !options.inband_tags &&
1794 WRITE_SIZE(mtd) == 512){
1795 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
1801 if (yaffsVersion == 2) {
1802 /* Check for version 2 style functions */
1804 !mtd->block_isbad ||
1805 !mtd->block_markbad ||
1808 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1809 !mtd->read_oob || !mtd->write_oob) {
1812 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1814 T(YAFFS_TRACE_ALWAYS,
1815 ("yaffs: MTD device does not support required "
1820 if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1821 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
1822 !options.inband_tags) {
1823 T(YAFFS_TRACE_ALWAYS,
1824 ("yaffs: MTD device does not have the "
1825 "right page sizes\n"));
1829 /* Check for V1 style functions */
1833 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1834 !mtd->read_oob || !mtd->write_oob) {
1837 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1839 T(YAFFS_TRACE_ALWAYS,
1840 ("yaffs: MTD device does not support required "
1845 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
1846 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1847 T(YAFFS_TRACE_ALWAYS,
1848 ("yaffs: MTD device does not support have the "
1849 "right page sizes\n"));
1854 /* OK, so if we got here, we have an MTD that's NAND and looks
1855 * like it has the right capabilities
1856 * Set the yaffs_Device up for mtd
1859 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1860 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1862 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1865 /* Deep shit could not allocate device structure */
1866 T(YAFFS_TRACE_ALWAYS,
1867 ("yaffs_read_super: Failed trying to allocate "
1868 "yaffs_Device. \n"));
1872 memset(dev, 0, sizeof(yaffs_Device));
1873 dev->genericDevice = mtd;
1874 dev->name = mtd->name;
1876 /* Set up the memory size parameters.... */
1878 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1879 dev->startBlock = 0;
1880 dev->endBlock = nBlocks - 1;
1881 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1882 dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1883 dev->nReservedBlocks = 5;
1884 dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
1885 dev->inbandTags = options.inband_tags;
1887 /* ... and the functions. */
1888 if (yaffsVersion == 2) {
1889 dev->writeChunkWithTagsToNAND =
1890 nandmtd2_WriteChunkWithTagsToNAND;
1891 dev->readChunkWithTagsFromNAND =
1892 nandmtd2_ReadChunkWithTagsFromNAND;
1893 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1894 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1895 dev->spareBuffer = YMALLOC(mtd->oobsize);
1897 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1898 dev->totalBytesPerChunk = mtd->writesize;
1899 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
1901 dev->totalBytesPerChunk = mtd->oobblock;
1902 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1904 nBlocks = mtd->size / mtd->erasesize;
1906 dev->startBlock = 0;
1907 dev->endBlock = nBlocks - 1;
1909 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1910 /* use the MTD interface in yaffs_mtdif1.c */
1911 dev->writeChunkWithTagsToNAND =
1912 nandmtd1_WriteChunkWithTagsToNAND;
1913 dev->readChunkWithTagsFromNAND =
1914 nandmtd1_ReadChunkWithTagsFromNAND;
1915 dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
1916 dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
1918 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1919 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1923 /* ... and common functions */
1924 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1925 dev->initialiseNAND = nandmtd_InitialiseNAND;
1927 dev->putSuperFunc = yaffs_MTDPutSuper;
1929 dev->superBlock = (void *)sb;
1930 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
1933 #ifndef CONFIG_YAFFS_DOES_ECC
1934 dev->useNANDECC = 1;
1937 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1938 dev->wideTnodesDisabled = 1;
1941 dev->skipCheckpointRead = options.skip_checkpoint_read;
1942 dev->skipCheckpointWrite = options.skip_checkpoint_write;
1944 /* we assume this is protected by lock_kernel() in mount/umount */
1945 ylist_add_tail(&dev->devList, &yaffs_dev_list);
1947 init_MUTEX(&dev->grossLock);
1949 yaffs_GrossLock(dev);
1951 err = yaffs_GutsInitialise(dev);
1954 ("yaffs_read_super: guts initialised %s\n",
1955 (err == YAFFS_OK) ? "OK" : "FAILED"));
1957 /* Release lock before yaffs_get_inode() */
1958 yaffs_GrossUnlock(dev);
1960 /* Create root inode */
1961 if (err == YAFFS_OK)
1962 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1968 inode->i_op = &yaffs_dir_inode_operations;
1969 inode->i_fop = &yaffs_dir_operations;
1971 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1973 root = d_alloc_root(inode);
1975 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1983 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1988 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1989 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
1992 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
1995 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1996 static int yaffs_read_super(struct file_system_type *fs,
1997 int flags, const char *dev_name,
1998 void *data, struct vfsmount *mnt)
2001 return get_sb_bdev(fs, flags, dev_name, data,
2002 yaffs_internal_read_super_mtd, mnt);
2005 static struct super_block *yaffs_read_super(struct file_system_type *fs,
2006 int flags, const char *dev_name,
2010 return get_sb_bdev(fs, flags, dev_name, data,
2011 yaffs_internal_read_super_mtd);
2015 static struct file_system_type yaffs_fs_type = {
2016 .owner = THIS_MODULE,
2018 .get_sb = yaffs_read_super,
2019 .kill_sb = kill_block_super,
2020 .fs_flags = FS_REQUIRES_DEV,
2023 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
2026 return yaffs_internal_read_super(1, sb, data, silent);
2029 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
2034 #ifdef CONFIG_YAFFS_YAFFS2
2036 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2037 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
2040 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
2043 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2044 static int yaffs2_read_super(struct file_system_type *fs,
2045 int flags, const char *dev_name, void *data,
2046 struct vfsmount *mnt)
2048 return get_sb_bdev(fs, flags, dev_name, data,
2049 yaffs2_internal_read_super_mtd, mnt);
2052 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
2053 int flags, const char *dev_name,
2057 return get_sb_bdev(fs, flags, dev_name, data,
2058 yaffs2_internal_read_super_mtd);
2062 static struct file_system_type yaffs2_fs_type = {
2063 .owner = THIS_MODULE,
2065 .get_sb = yaffs2_read_super,
2066 .kill_sb = kill_block_super,
2067 .fs_flags = FS_REQUIRES_DEV,
2070 static struct super_block *yaffs2_read_super(struct super_block *sb,
2071 void *data, int silent)
2073 return yaffs_internal_read_super(2, sb, data, silent);
2076 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
2080 #endif /* CONFIG_YAFFS_YAFFS2 */
2082 static struct proc_dir_entry *my_proc_entry;
2084 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
2086 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
2087 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
2088 buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
2089 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
2090 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
2091 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
2092 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
2093 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
2094 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
2095 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
2096 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
2097 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
2098 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
2099 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
2100 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
2101 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
2102 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
2103 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
2104 buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
2105 buf += sprintf(buf, "passiveGCs......... %d\n",
2106 dev->passiveGarbageCollections);
2107 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
2108 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
2109 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
2110 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
2111 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
2112 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
2113 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
2114 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
2115 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
2116 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
2118 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
2119 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
2120 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
2121 buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
2126 static int yaffs_proc_read(char *page,
2128 off_t offset, int count, int *eof, void *data)
2130 struct ylist_head *item;
2135 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
2136 * We use 'offset' (*ppos) to indicate where we are in devList.
2137 * This also assumes the user has posted a read buffer large
2138 * enough to hold the complete output; but that's life in /proc.
2143 /* Print header first */
2145 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2146 "\n%s\n%s\n", yaffs_fs_c_version,
2147 yaffs_guts_c_version);
2150 /* hold lock_kernel while traversing yaffs_dev_list */
2153 /* Locate and print the Nth entry. Order N-squared but N is small. */
2154 ylist_for_each(item, &yaffs_dev_list) {
2155 yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
2160 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
2161 buf = yaffs_dump_dev(buf, dev);
2166 return buf - page < count ? buf - page : count;
2170 * Set the verbosity of the warnings and error messages.
2172 * Note that the names can only be a..z or _ with the current code.
2177 unsigned mask_bitfield;
2179 {"allocate", YAFFS_TRACE_ALLOCATE},
2180 {"always", YAFFS_TRACE_ALWAYS},
2181 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2182 {"buffers", YAFFS_TRACE_BUFFERS},
2183 {"bug", YAFFS_TRACE_BUG},
2184 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2185 {"deletion", YAFFS_TRACE_DELETION},
2186 {"erase", YAFFS_TRACE_ERASE},
2187 {"error", YAFFS_TRACE_ERROR},
2188 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2189 {"gc", YAFFS_TRACE_GC},
2190 {"mtd", YAFFS_TRACE_MTD},
2191 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2192 {"os", YAFFS_TRACE_OS},
2193 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2194 {"scan", YAFFS_TRACE_SCAN},
2195 {"tracing", YAFFS_TRACE_TRACING},
2197 {"verify", YAFFS_TRACE_VERIFY},
2198 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2199 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2200 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2202 {"write", YAFFS_TRACE_WRITE},
2203 {"all", 0xffffffff},
2208 #define MAX_MASK_NAME_LENGTH 40
2209 static int yaffs_proc_write(struct file *file, const char *buf,
2210 unsigned long count, void *data)
2212 unsigned rg = 0, mask_bitfield;
2216 char substring[MAX_MASK_NAME_LENGTH+1];
2222 rg = yaffs_traceMask;
2224 while (!done && (pos < count)) {
2226 while ((pos < count) && isspace(buf[pos])) {
2244 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2245 if (end > buf + pos) {
2246 mask_name = "numeral";
2247 len = end - (buf + pos);
2251 for(x = buf + pos, i = 0;
2252 (*x == '_' || (*x >='a' && *x <= 'z')) &&
2253 i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2255 substring[i] = '\0';
2257 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2258 if(strcmp(substring,mask_flags[i].mask_name) == 0){
2259 mask_name = mask_flags[i].mask_name;
2260 mask_bitfield = mask_flags[i].mask_bitfield;
2267 if (mask_name != NULL) {
2271 rg &= ~mask_bitfield;
2274 rg |= mask_bitfield;
2280 rg |= mask_bitfield;
2286 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2288 printk("new trace = 0x%08X\n",yaffs_traceMask);
2290 if (rg & YAFFS_TRACE_ALWAYS) {
2291 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2293 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2294 printk("%c%s\n", flag, mask_flags[i].mask_name);
2301 /* Stuff to handle installation of file systems */
2302 struct file_system_to_install {
2303 struct file_system_type *fst;
2307 static struct file_system_to_install fs_to_install[] = {
2308 //#ifdef CONFIG_YAFFS_YAFFS1
2309 {&yaffs_fs_type, 0},
2311 //#ifdef CONFIG_YAFFS_YAFFS2
2312 {&yaffs2_fs_type, 0},
2317 static int __init init_yaffs_fs(void)
2320 struct file_system_to_install *fsinst;
2322 T(YAFFS_TRACE_ALWAYS,
2323 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2325 /* Install the proc_fs entry */
2326 my_proc_entry = create_proc_entry("yaffs",
2330 if (my_proc_entry) {
2331 my_proc_entry->write_proc = yaffs_proc_write;
2332 my_proc_entry->read_proc = yaffs_proc_read;
2333 my_proc_entry->data = NULL;
2338 /* Now add the file system entries */
2340 fsinst = fs_to_install;
2342 while (fsinst->fst && !error) {
2343 error = register_filesystem(fsinst->fst);
2345 fsinst->installed = 1;
2350 /* Any errors? uninstall */
2352 fsinst = fs_to_install;
2354 while (fsinst->fst) {
2355 if (fsinst->installed) {
2356 unregister_filesystem(fsinst->fst);
2357 fsinst->installed = 0;
2366 static void __exit exit_yaffs_fs(void)
2369 struct file_system_to_install *fsinst;
2371 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2374 remove_proc_entry("yaffs", YPROC_ROOT);
2376 fsinst = fs_to_install;
2378 while (fsinst->fst) {
2379 if (fsinst->installed) {
2380 unregister_filesystem(fsinst->fst);
2381 fsinst->installed = 0;
2388 module_init(init_yaffs_fs)
2389 module_exit(exit_yaffs_fs)
2391 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2392 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2393 MODULE_LICENSE("GPL");