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.60 2007-05-15 20:07:40 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>
46 #include <linux/list.h>
48 #include <linux/proc_fs.h>
49 #include <linux/smp_lock.h>
50 #include <linux/pagemap.h>
51 #include <linux/mtd/mtd.h>
52 #include <linux/interrupt.h>
53 #include <linux/string.h>
54 #include <linux/ctype.h>
56 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
58 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
59 #include <asm/statfs.h>
60 #define UnlockPage(p) unlock_page(p)
61 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
63 /* FIXME: use sb->s_id instead ? */
64 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
68 #include <linux/locks.h>
69 #define BDEVNAME_SIZE 0
70 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
72 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
73 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
79 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
80 #define WRITE_SIZE_STR "writesize"
81 #define WRITE_SIZE(mtd) (mtd)->writesize
83 #define WRITE_SIZE_STR "oobblock"
84 #define WRITE_SIZE(mtd) (mtd)->oobblock
87 #include <asm/uaccess.h>
90 #include "yaffs_guts.h"
92 unsigned yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS
95 #include <linux/mtd/mtd.h>
96 #include "yaffs_mtdif.h"
97 #include "yaffs_mtdif2.h"
99 /*#define T(x) printk x */
101 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
102 #define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
104 #define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
107 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
108 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
110 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
111 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
113 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
116 static void yaffs_put_super(struct super_block *sb);
118 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
121 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
122 static int yaffs_file_flush(struct file *file, fl_owner_t id);
124 static int yaffs_file_flush(struct file *file);
127 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
130 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
132 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
133 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
134 struct nameidata *n);
135 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
136 struct nameidata *n);
138 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
139 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
141 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
142 struct dentry *dentry);
143 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
144 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
145 const char *symname);
146 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
148 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
149 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
152 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
155 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
156 struct inode *new_dir, struct dentry *new_dentry);
157 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
159 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
160 static int yaffs_sync_fs(struct super_block *sb, int wait);
161 static void yaffs_write_super(struct super_block *sb);
163 static int yaffs_sync_fs(struct super_block *sb);
164 static int yaffs_write_super(struct super_block *sb);
167 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
168 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
169 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
170 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
172 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
174 static void yaffs_read_inode(struct inode *inode);
176 static void yaffs_put_inode(struct inode *inode);
177 static void yaffs_delete_inode(struct inode *);
178 static void yaffs_clear_inode(struct inode *);
180 static int yaffs_readpage(struct file *file, struct page *page);
181 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
182 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
184 static int yaffs_writepage(struct page *page);
186 static int yaffs_prepare_write(struct file *f, struct page *pg,
187 unsigned offset, unsigned to);
188 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
191 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
193 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
194 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
196 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
199 static struct address_space_operations yaffs_file_address_operations = {
200 .readpage = yaffs_readpage,
201 .writepage = yaffs_writepage,
202 .prepare_write = yaffs_prepare_write,
203 .commit_write = yaffs_commit_write,
206 static struct file_operations yaffs_file_operations = {
207 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
208 .read = do_sync_read,
209 .write = do_sync_write,
210 .aio_read = generic_file_aio_read,
211 .aio_write = generic_file_aio_write,
213 .read = generic_file_read,
214 .write = generic_file_write,
216 .mmap = generic_file_mmap,
217 .flush = yaffs_file_flush,
218 .fsync = yaffs_sync_object,
219 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
220 .sendfile = generic_file_sendfile,
225 static struct inode_operations yaffs_file_inode_operations = {
226 .setattr = yaffs_setattr,
229 static struct inode_operations yaffs_symlink_inode_operations = {
230 .readlink = yaffs_readlink,
231 .follow_link = yaffs_follow_link,
232 .setattr = yaffs_setattr,
235 static struct inode_operations yaffs_dir_inode_operations = {
236 .create = yaffs_create,
237 .lookup = yaffs_lookup,
239 .unlink = yaffs_unlink,
240 .symlink = yaffs_symlink,
241 .mkdir = yaffs_mkdir,
242 .rmdir = yaffs_unlink,
243 .mknod = yaffs_mknod,
244 .rename = yaffs_rename,
245 .setattr = yaffs_setattr,
248 static struct file_operations yaffs_dir_operations = {
249 .read = generic_read_dir,
250 .readdir = yaffs_readdir,
251 .fsync = yaffs_sync_object,
254 static struct super_operations yaffs_super_ops = {
255 .statfs = yaffs_statfs,
256 .read_inode = yaffs_read_inode,
257 .put_inode = yaffs_put_inode,
258 .put_super = yaffs_put_super,
259 .delete_inode = yaffs_delete_inode,
260 .clear_inode = yaffs_clear_inode,
261 .sync_fs = yaffs_sync_fs,
262 .write_super = yaffs_write_super,
265 static void yaffs_GrossLock(yaffs_Device * dev)
267 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
269 down(&dev->grossLock);
272 static void yaffs_GrossUnlock(yaffs_Device * dev)
274 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
279 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
282 unsigned char *alias;
285 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
287 yaffs_GrossLock(dev);
289 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
291 yaffs_GrossUnlock(dev);
296 ret = vfs_readlink(dentry, buffer, buflen, alias);
301 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
302 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
304 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
307 unsigned char *alias;
309 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
311 yaffs_GrossLock(dev);
313 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
315 yaffs_GrossUnlock(dev);
323 ret = vfs_follow_link(nd, alias);
326 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
327 return ERR_PTR (ret);
333 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
337 * Lookup is used to find objects in the fs
339 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
341 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
344 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
348 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
350 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
352 yaffs_GrossLock(dev);
355 (KERN_DEBUG "yaffs_lookup for %d:%s\n",
356 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
359 yaffs_FindObjectByName(yaffs_InodeToObject(dir),
360 dentry->d_name.name);
362 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
364 /* Can't hold gross lock when calling yaffs_get_inode() */
365 yaffs_GrossUnlock(dev);
369 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
371 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
375 (KERN_DEBUG "yaffs_loookup dentry \n"));
376 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
377 * d_add even if NULL inode */
379 /*dget(dentry); // try to solve directory bug */
380 d_add(dentry, inode);
388 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
392 /* added NCB for 2.5/6 compatability - forces add even if inode is
393 * NULL which creates dentry hash */
394 d_add(dentry, inode);
397 /* return (ERR_PTR(-EIO)); */
401 /* For now put inode is just for debugging
402 * Put inode is called when the inode **structure** is put.
404 static void yaffs_put_inode(struct inode *inode)
407 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
408 atomic_read(&inode->i_count)));
412 /* clear is called to tell the fs to release any per-inode data it holds */
413 static void yaffs_clear_inode(struct inode *inode)
418 obj = yaffs_InodeToObject(inode);
421 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
422 atomic_read(&inode->i_count),
423 obj ? "object exists" : "null object"));
427 yaffs_GrossLock(dev);
429 /* Clear the association between the inode and
433 yaffs_InodeToObjectLV(inode) = NULL;
435 /* If the object freeing was deferred, then the real
437 * This should fix the inode inconsistency problem.
440 yaffs_HandleDeferedFree(obj);
442 yaffs_GrossUnlock(dev);
447 /* delete is called when the link count is zero and the inode
448 * is put (ie. nobody wants to know about it anymore, time to
450 * NB Must call clear_inode()
452 static void yaffs_delete_inode(struct inode *inode)
454 yaffs_Object *obj = yaffs_InodeToObject(inode);
458 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
459 atomic_read(&inode->i_count),
460 obj ? "object exists" : "null object"));
464 yaffs_GrossLock(dev);
465 yaffs_DeleteFile(obj);
466 yaffs_GrossUnlock(dev);
468 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
469 truncate_inode_pages (&inode->i_data, 0);
474 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
475 static int yaffs_file_flush(struct file *file, fl_owner_t id)
477 static int yaffs_file_flush(struct file *file)
480 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
482 yaffs_Device *dev = obj->myDev;
485 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
486 obj->dirty ? "dirty" : "clean"));
488 yaffs_GrossLock(dev);
490 yaffs_FlushFile(obj, 1);
492 yaffs_GrossUnlock(dev);
497 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
499 /* Lifted from jffs2 */
502 unsigned char *pg_buf;
507 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
508 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
509 (unsigned)PAGE_CACHE_SIZE));
511 obj = yaffs_DentryToObject(f->f_dentry);
515 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
516 BUG_ON(!PageLocked(pg));
523 /* FIXME: Can kmap fail? */
525 yaffs_GrossLock(dev);
528 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
531 yaffs_GrossUnlock(dev);
537 ClearPageUptodate(pg);
544 flush_dcache_page(pg);
547 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
551 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
553 int ret = yaffs_readpage_nolock(f, pg);
558 static int yaffs_readpage(struct file *f, struct page *pg)
560 return yaffs_readpage_unlock(f, pg);
563 /* writepage inspired by/stolen from smbfs */
565 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
566 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
568 static int yaffs_writepage(struct page *page)
571 struct address_space *mapping = page->mapping;
572 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
574 unsigned long end_index;
582 inode = mapping->host;
586 if (offset > inode->i_size) {
589 "yaffs_writepage at %08x, inode size = %08x!!!\n",
590 (unsigned)(page->index << PAGE_CACHE_SHIFT),
591 (unsigned)inode->i_size));
593 (KERN_DEBUG " -> don't care!!\n"));
598 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
601 if (page->index < end_index) {
602 nBytes = PAGE_CACHE_SIZE;
604 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
611 obj = yaffs_InodeToObject(inode);
612 yaffs_GrossLock(obj->myDev);
615 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
616 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
618 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
619 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
622 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
626 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
627 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
629 yaffs_GrossUnlock(obj->myDev);
632 SetPageUptodate(page);
636 return (nWritten == nBytes) ? 0 : -ENOSPC;
639 static int yaffs_prepare_write(struct file *f, struct page *pg,
640 unsigned offset, unsigned to)
643 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
644 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
645 return yaffs_readpage_nolock(f, pg);
651 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
655 void *addr = page_address(pg) + offset;
656 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
657 int nBytes = to - offset;
661 unsigned saddr = (unsigned)addr;
664 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
667 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
669 if (nWritten != nBytes) {
672 "yaffs_commit_write not same size nWritten %d nBytes %d\n",
675 ClearPageUptodate(pg);
681 (KERN_DEBUG "yaffs_commit_write returning %d\n",
682 nWritten == nBytes ? 0 : nWritten));
684 return nWritten == nBytes ? 0 : nWritten;
688 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
693 /* Check mode against the variant type and attempt to repair if broken. */
694 __u32 mode = obj->yst_mode;
695 switch( obj->variantType ){
696 case YAFFS_OBJECT_TYPE_FILE :
697 if( ! S_ISREG(mode) ){
698 obj->yst_mode &= ~S_IFMT;
699 obj->yst_mode |= S_IFREG;
703 case YAFFS_OBJECT_TYPE_SYMLINK :
704 if( ! S_ISLNK(mode) ){
705 obj->yst_mode &= ~S_IFMT;
706 obj->yst_mode |= S_IFLNK;
710 case YAFFS_OBJECT_TYPE_DIRECTORY :
711 if( ! S_ISDIR(mode) ){
712 obj->yst_mode &= ~S_IFMT;
713 obj->yst_mode |= S_IFDIR;
717 case YAFFS_OBJECT_TYPE_UNKNOWN :
718 case YAFFS_OBJECT_TYPE_HARDLINK :
719 case YAFFS_OBJECT_TYPE_SPECIAL :
725 inode->i_ino = obj->objectId;
726 inode->i_mode = obj->yst_mode;
727 inode->i_uid = obj->yst_uid;
728 inode->i_gid = obj->yst_gid;
729 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
730 inode->i_blksize = inode->i_sb->s_blocksize;
732 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
734 inode->i_rdev = old_decode_dev(obj->yst_rdev);
735 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
736 inode->i_atime.tv_nsec = 0;
737 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
738 inode->i_mtime.tv_nsec = 0;
739 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
740 inode->i_ctime.tv_nsec = 0;
742 inode->i_rdev = obj->yst_rdev;
743 inode->i_atime = obj->yst_atime;
744 inode->i_mtime = obj->yst_mtime;
745 inode->i_ctime = obj->yst_ctime;
747 inode->i_size = yaffs_GetObjectFileLength(obj);
748 inode->i_blocks = (inode->i_size + 511) >> 9;
750 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
754 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
755 inode->i_mode, inode->i_uid, inode->i_gid,
756 (int)inode->i_size, atomic_read(&inode->i_count)));
758 switch (obj->yst_mode & S_IFMT) {
759 default: /* fifo, device or socket */
760 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
761 init_special_inode(inode, obj->yst_mode,
762 old_decode_dev(obj->yst_rdev));
764 init_special_inode(inode, obj->yst_mode,
765 (dev_t) (obj->yst_rdev));
768 case S_IFREG: /* file */
769 inode->i_op = &yaffs_file_inode_operations;
770 inode->i_fop = &yaffs_file_operations;
771 inode->i_mapping->a_ops =
772 &yaffs_file_address_operations;
774 case S_IFDIR: /* directory */
775 inode->i_op = &yaffs_dir_inode_operations;
776 inode->i_fop = &yaffs_dir_operations;
778 case S_IFLNK: /* symlink */
779 inode->i_op = &yaffs_symlink_inode_operations;
783 yaffs_InodeToObjectLV(inode) = obj;
785 obj->myInode = inode;
789 (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
794 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
801 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
808 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
814 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
816 inode = iget(sb, obj->objectId);
818 /* NB Side effect: iget calls back to yaffs_read_inode(). */
819 /* iget also increments the inode's i_count */
820 /* NB You can't be holding grossLock or deadlock will happen! */
825 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
833 obj = yaffs_DentryToObject(f->f_dentry);
837 yaffs_GrossLock(dev);
839 inode = f->f_dentry->d_inode;
841 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
842 ipos = inode->i_size;
849 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
853 "yaffs_file_write about to write writing %d bytes"
854 "to object %d at %d\n",
855 n, obj->objectId, ipos));
858 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
861 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
866 if (ipos > inode->i_size) {
867 inode->i_size = ipos;
868 inode->i_blocks = (ipos + 511) >> 9;
872 "yaffs_file_write size updated to %d bytes, "
874 ipos, (int)(inode->i_blocks)));
878 yaffs_GrossUnlock(dev);
879 return nWritten == 0 ? -ENOSPC : nWritten;
882 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
886 struct inode *inode = f->f_dentry->d_inode;
887 unsigned long offset, curoffs;
891 char name[YAFFS_MAX_NAME_LENGTH + 1];
893 obj = yaffs_DentryToObject(f->f_dentry);
896 yaffs_GrossLock(dev);
900 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
904 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
906 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
915 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
916 (int)f->f_dentry->d_parent->d_inode->i_ino));
918 (dirent, "..", 2, offset,
919 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
928 /* If the directory has changed since the open or last call to
929 readdir, rewind to after the 2 canned entries. */
931 if (f->f_version != inode->i_version) {
934 f->f_version = inode->i_version;
937 list_for_each(i, &obj->variant.directoryVariant.children) {
939 if (curoffs >= offset) {
940 l = list_entry(i, yaffs_Object, siblings);
942 yaffs_GetObjectName(l, name,
943 YAFFS_MAX_NAME_LENGTH + 1);
945 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
946 yaffs_GetObjectInode(l)));
952 yaffs_GetObjectInode(l),
953 yaffs_GetObjectType(l))
966 yaffs_GrossUnlock(dev);
972 * File creation. Allocate an inode, and we're done..
974 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
975 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
978 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
984 yaffs_Object *obj = NULL;
987 yaffs_Object *parent = yaffs_InodeToObject(dir);
990 uid_t uid = current->fsuid;
991 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
993 if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
998 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
999 parent->objectId, parent->variantType));
1002 (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
1006 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1008 dentry->d_name.name, mode, rdev));
1010 dev = parent->myDev;
1012 yaffs_GrossLock(dev);
1014 switch (mode & S_IFMT) {
1016 /* Special (socket, fifo, device...) */
1017 T(YAFFS_TRACE_OS, (KERN_DEBUG
1018 "yaffs_mknod: making special\n"));
1019 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1021 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1022 gid, old_encode_dev(rdev));
1025 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1029 case S_IFREG: /* file */
1030 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1032 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1035 case S_IFDIR: /* directory */
1037 (KERN_DEBUG "yaffs_mknod: making directory\n"));
1039 yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1042 case S_IFLNK: /* symlink */
1043 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1044 obj = NULL; /* Do we ever get here? */
1048 /* Can not call yaffs_get_inode() with gross lock held */
1049 yaffs_GrossUnlock(dev);
1052 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1053 d_instantiate(dentry, inode);
1055 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
1056 obj->objectId, atomic_read(&inode->i_count)));
1060 (KERN_DEBUG "yaffs_mknod failed making object\n"));
1067 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1070 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
1071 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1073 /* attempt to fix dir bug - didn't work */
1081 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1082 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1083 struct nameidata *n)
1085 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1088 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1089 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1092 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1099 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1100 dentry->d_name.name));
1102 dev = yaffs_InodeToObject(dir)->myDev;
1104 yaffs_GrossLock(dev);
1106 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1108 if (retVal == YAFFS_OK) {
1109 dentry->d_inode->i_nlink--;
1111 yaffs_GrossUnlock(dev);
1112 mark_inode_dirty(dentry->d_inode);
1115 yaffs_GrossUnlock(dev);
1122 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1123 struct dentry *dentry)
1125 struct inode *inode = old_dentry->d_inode;
1126 yaffs_Object *obj = NULL;
1127 yaffs_Object *link = NULL;
1130 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1132 obj = yaffs_InodeToObject(inode);
1135 yaffs_GrossLock(dev);
1137 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1140 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1145 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1146 d_instantiate(dentry, old_dentry->d_inode);
1147 atomic_inc(&old_dentry->d_inode->i_count);
1149 (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1150 old_dentry->d_inode->i_nlink,
1151 atomic_read(&old_dentry->d_inode->i_count)));
1155 yaffs_GrossUnlock(dev);
1165 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1166 const char *symname)
1170 uid_t uid = current->fsuid;
1171 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1173 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1175 dev = yaffs_InodeToObject(dir)->myDev;
1176 yaffs_GrossLock(dev);
1177 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1178 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1179 yaffs_GrossUnlock(dev);
1183 struct inode *inode;
1185 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1186 d_instantiate(dentry, inode);
1187 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1190 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1197 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1204 obj = yaffs_DentryToObject(dentry);
1208 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1209 yaffs_GrossLock(dev);
1210 yaffs_FlushFile(obj, 1);
1211 yaffs_GrossUnlock(dev);
1216 * The VFS layer already does all the dentry stuff for rename.
1218 * NB: POSIX says you can rename an object over an old object of the same name
1220 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1221 struct inode *new_dir, struct dentry *new_dentry)
1224 int retVal = YAFFS_FAIL;
1225 yaffs_Object *target;
1227 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1228 dev = yaffs_InodeToObject(old_dir)->myDev;
1230 yaffs_GrossLock(dev);
1232 /* Check if the target is an existing directory that is not empty. */
1234 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1235 new_dentry->d_name.name);
1240 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1241 !list_empty(&target->variant.directoryVariant.children)) {
1243 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1245 retVal = YAFFS_FAIL;
1248 /* Now does unlinking internally using shadowing mechanism */
1249 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1252 yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1253 old_dentry->d_name.name,
1254 yaffs_InodeToObject(new_dir),
1255 new_dentry->d_name.name);
1258 yaffs_GrossUnlock(dev);
1260 if (retVal == YAFFS_OK) {
1262 new_dentry->d_inode->i_nlink--;
1263 mark_inode_dirty(new_dentry->d_inode);
1273 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1275 struct inode *inode = dentry->d_inode;
1280 (KERN_DEBUG "yaffs_setattr of object %d\n",
1281 yaffs_InodeToObject(inode)->objectId));
1283 if ((error = inode_change_ok(inode, attr)) == 0) {
1285 dev = yaffs_InodeToObject(inode)->myDev;
1286 yaffs_GrossLock(dev);
1287 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1293 yaffs_GrossUnlock(dev);
1295 error = inode_setattr(inode, attr);
1300 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1301 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1303 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1304 struct super_block *sb = dentry->d_sb;
1305 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1306 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1308 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1310 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1312 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1315 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1317 yaffs_GrossLock(dev);
1319 buf->f_type = YAFFS_MAGIC;
1320 buf->f_bsize = sb->s_blocksize;
1321 buf->f_namelen = 255;
1322 if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1325 (dev->endBlock - dev->startBlock +
1326 1) * dev->nChunksPerBlock / (sb->s_blocksize /
1327 dev->nDataBytesPerChunk);
1329 yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
1330 dev->nDataBytesPerChunk);
1334 (dev->endBlock - dev->startBlock +
1335 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
1338 yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
1343 buf->f_bavail = buf->f_bfree;
1345 yaffs_GrossUnlock(dev);
1351 static int yaffs_do_sync_fs(struct super_block *sb)
1354 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1355 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
1358 yaffs_GrossLock(dev);
1361 yaffs_CheckpointSave(dev);
1363 yaffs_GrossUnlock(dev);
1371 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1372 static void yaffs_write_super(struct super_block *sb)
1374 static int yaffs_write_super(struct super_block *sb)
1378 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
1379 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
1380 return 0; /* yaffs_do_sync_fs(sb);*/
1385 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1386 static int yaffs_sync_fs(struct super_block *sb, int wait)
1388 static int yaffs_sync_fs(struct super_block *sb)
1392 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
1394 return 0; /* yaffs_do_sync_fs(sb);*/
1399 static void yaffs_read_inode(struct inode *inode)
1401 /* NB This is called as a side effect of other functions, but
1402 * we had to release the lock to prevent deadlocks, so
1403 * need to lock again.
1407 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1410 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1412 yaffs_GrossLock(dev);
1414 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1416 yaffs_FillInodeFromObject(inode, obj);
1418 yaffs_GrossUnlock(dev);
1421 static LIST_HEAD(yaffs_dev_list);
1423 static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
1425 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1427 if( *flags & MS_RDONLY ) {
1428 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1431 (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
1433 yaffs_GrossLock(dev);
1435 yaffs_FlushEntireDeviceCache(dev);
1437 yaffs_CheckpointSave(dev);
1442 yaffs_GrossUnlock(dev);
1446 (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
1452 static void yaffs_put_super(struct super_block *sb)
1454 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1456 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
1458 yaffs_GrossLock(dev);
1460 yaffs_FlushEntireDeviceCache(dev);
1462 yaffs_CheckpointSave(dev);
1464 if (dev->putSuperFunc) {
1465 dev->putSuperFunc(sb);
1468 yaffs_Deinitialise(dev);
1470 yaffs_GrossUnlock(dev);
1472 /* we assume this is protected by lock_kernel() in mount/umount */
1473 list_del(&dev->devList);
1475 if(dev->spareBuffer){
1476 YFREE(dev->spareBuffer);
1477 dev->spareBuffer = NULL;
1484 static void yaffs_MTDPutSuper(struct super_block *sb)
1487 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1493 put_mtd_device(mtd);
1497 static void yaffs_MarkSuperBlockDirty(void *vsb)
1499 struct super_block *sb = (struct super_block *)vsb;
1501 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
1508 int skip_checkpoint_read;
1509 int skip_checkpoint_write;
1513 #define MAX_OPT_LEN 20
1514 static int yaffs_parse_options(yaffs_options *options, const char *options_str)
1516 char cur_opt[MAX_OPT_LEN+1];
1520 /* Parse through the options which is a comma seperated list */
1522 while(options_str && *options_str && !error){
1523 memset(cur_opt,0,MAX_OPT_LEN+1);
1526 while(*options_str && *options_str != ','){
1527 if(p < MAX_OPT_LEN){
1528 cur_opt[p] = *options_str;
1534 if(!strcmp(cur_opt,"inband-tags"))
1535 options->inband_tags = 1;
1536 else if(!strcmp(cur_opt,"no-cache"))
1537 options->no_cache = 1;
1538 else if(!strcmp(cur_opt,"no-checkpoint-read"))
1539 options->skip_checkpoint_read = 1;
1540 else if(!strcmp(cur_opt,"no-checkpoint-write"))
1541 options->skip_checkpoint_write = 1;
1542 else if(!strcmp(cur_opt,"no-checkpoint")){
1543 options->skip_checkpoint_read = 1;
1544 options->skip_checkpoint_write = 1;
1546 printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
1555 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1556 struct super_block *sb,
1557 void *data, int silent)
1560 struct inode *inode = NULL;
1561 struct dentry *root;
1562 yaffs_Device *dev = 0;
1563 char devname_buf[BDEVNAME_SIZE + 1];
1564 struct mtd_info *mtd;
1566 char *data_str = (char *)data;
1568 yaffs_options options;
1570 sb->s_magic = YAFFS_MAGIC;
1571 sb->s_op = &yaffs_super_ops;
1574 printk(KERN_INFO "yaffs: sb is NULL\n");
1575 else if (!sb->s_dev)
1576 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1577 else if (!yaffs_devname(sb, devname_buf))
1578 printk(KERN_INFO "yaffs: devname is NULL\n");
1580 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1582 yaffs_devname(sb, devname_buf));
1587 printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
1589 memset(&options,0,sizeof(options));
1591 if(yaffs_parse_options(&options,data_str)){
1592 /* Option parsing failed */
1597 sb->s_blocksize = PAGE_CACHE_SIZE;
1598 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1599 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1601 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1603 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1605 ("yaffs: Write verification disabled. All guarantees "
1606 "null and void\n"));
1609 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1611 MAJOR(sb->s_dev), MINOR(sb->s_dev),
1612 yaffs_devname(sb, devname_buf)));
1614 /* Check it's an mtd device..... */
1615 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1616 return NULL; /* This isn't an mtd device */
1618 /* Get the device */
1619 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1621 T(YAFFS_TRACE_ALWAYS,
1622 ("yaffs: MTD device #%u doesn't appear to exist\n",
1626 /* Check it's NAND */
1627 if (mtd->type != MTD_NANDFLASH) {
1628 T(YAFFS_TRACE_ALWAYS,
1629 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1633 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1634 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1635 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1636 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1637 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1638 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1639 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1640 T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
1641 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1642 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1643 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1645 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1647 if (yaffsVersion == 1 &&
1648 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1649 mtd->writesize >= 2048) {
1651 mtd->oobblock >= 2048) {
1653 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1657 /* Added NCB 26/5/2006 for completeness */
1658 if (yaffsVersion == 2 &&
1659 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1660 mtd->writesize == 512) {
1662 mtd->oobblock == 512) {
1664 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
1670 if (yaffsVersion == 2) {
1671 /* Check for version 2 style functions */
1673 !mtd->block_isbad ||
1674 !mtd->block_markbad ||
1677 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1678 !mtd->read_oob || !mtd->write_oob) {
1681 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1683 T(YAFFS_TRACE_ALWAYS,
1684 ("yaffs: MTD device does not support required "
1689 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1690 if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1692 if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1694 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
1695 T(YAFFS_TRACE_ALWAYS,
1696 ("yaffs: MTD device does not have the "
1697 "right page sizes\n"));
1701 /* Check for V1 style functions */
1705 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1706 !mtd->read_oob || !mtd->write_oob) {
1709 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1711 T(YAFFS_TRACE_ALWAYS,
1712 ("yaffs: MTD device does not support required "
1717 if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
1718 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1719 T(YAFFS_TRACE_ALWAYS,
1720 ("yaffs: MTD device does not support have the "
1721 "right page sizes\n"));
1726 /* OK, so if we got here, we have an MTD that's NAND and looks
1727 * like it has the right capabilities
1728 * Set the yaffs_Device up for mtd
1731 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1732 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1734 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1737 /* Deep shit could not allocate device structure */
1738 T(YAFFS_TRACE_ALWAYS,
1739 ("yaffs_read_super: Failed trying to allocate "
1740 "yaffs_Device. \n"));
1744 memset(dev, 0, sizeof(yaffs_Device));
1745 dev->genericDevice = mtd;
1746 dev->name = mtd->name;
1748 /* Set up the memory size parameters.... */
1750 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1751 dev->startBlock = 0;
1752 dev->endBlock = nBlocks - 1;
1753 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1754 dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1755 dev->nReservedBlocks = 5;
1756 dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
1758 /* ... and the functions. */
1759 if (yaffsVersion == 2) {
1760 dev->writeChunkWithTagsToNAND =
1761 nandmtd2_WriteChunkWithTagsToNAND;
1762 dev->readChunkWithTagsFromNAND =
1763 nandmtd2_ReadChunkWithTagsFromNAND;
1764 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1765 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1766 dev->spareBuffer = YMALLOC(mtd->oobsize);
1768 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1769 dev->nDataBytesPerChunk = mtd->writesize;
1770 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
1772 dev->nDataBytesPerChunk = mtd->oobblock;
1773 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1775 nBlocks = mtd->size / mtd->erasesize;
1777 dev->nCheckpointReservedBlocks = CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS;
1778 dev->startBlock = 0;
1779 dev->endBlock = nBlocks - 1;
1781 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1782 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1785 /* ... and common functions */
1786 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1787 dev->initialiseNAND = nandmtd_InitialiseNAND;
1789 dev->putSuperFunc = yaffs_MTDPutSuper;
1791 dev->superBlock = (void *)sb;
1792 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
1795 #ifndef CONFIG_YAFFS_DOES_ECC
1796 dev->useNANDECC = 1;
1799 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1800 dev->wideTnodesDisabled = 1;
1803 dev->skipCheckpointRead = options.skip_checkpoint_read;
1804 dev->skipCheckpointWrite = options.skip_checkpoint_write;
1806 /* we assume this is protected by lock_kernel() in mount/umount */
1807 list_add_tail(&dev->devList, &yaffs_dev_list);
1809 init_MUTEX(&dev->grossLock);
1811 yaffs_GrossLock(dev);
1813 err = yaffs_GutsInitialise(dev);
1816 ("yaffs_read_super: guts initialised %s\n",
1817 (err == YAFFS_OK) ? "OK" : "FAILED"));
1819 /* Release lock before yaffs_get_inode() */
1820 yaffs_GrossUnlock(dev);
1822 /* Create root inode */
1823 if (err == YAFFS_OK)
1824 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1830 inode->i_op = &yaffs_dir_inode_operations;
1831 inode->i_fop = &yaffs_dir_operations;
1833 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1835 root = d_alloc_root(inode);
1837 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1845 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1850 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1851 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
1854 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
1857 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1858 static int yaffs_read_super(struct file_system_type *fs,
1859 int flags, const char *dev_name,
1860 void *data, struct vfsmount *mnt)
1863 return get_sb_bdev(fs, flags, dev_name, data,
1864 yaffs_internal_read_super_mtd, mnt);
1867 static struct super_block *yaffs_read_super(struct file_system_type *fs,
1868 int flags, const char *dev_name,
1872 return get_sb_bdev(fs, flags, dev_name, data,
1873 yaffs_internal_read_super_mtd);
1877 static struct file_system_type yaffs_fs_type = {
1878 .owner = THIS_MODULE,
1880 .get_sb = yaffs_read_super,
1881 .kill_sb = kill_block_super,
1882 .fs_flags = FS_REQUIRES_DEV,
1885 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
1888 return yaffs_internal_read_super(1, sb, data, silent);
1891 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
1896 #ifdef CONFIG_YAFFS_YAFFS2
1898 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1899 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
1902 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
1905 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1906 static int yaffs2_read_super(struct file_system_type *fs,
1907 int flags, const char *dev_name, void *data,
1908 struct vfsmount *mnt)
1910 return get_sb_bdev(fs, flags, dev_name, data,
1911 yaffs2_internal_read_super_mtd, mnt);
1914 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
1915 int flags, const char *dev_name,
1919 return get_sb_bdev(fs, flags, dev_name, data,
1920 yaffs2_internal_read_super_mtd);
1924 static struct file_system_type yaffs2_fs_type = {
1925 .owner = THIS_MODULE,
1927 .get_sb = yaffs2_read_super,
1928 .kill_sb = kill_block_super,
1929 .fs_flags = FS_REQUIRES_DEV,
1932 static struct super_block *yaffs2_read_super(struct super_block *sb,
1933 void *data, int silent)
1935 return yaffs_internal_read_super(2, sb, data, silent);
1938 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
1942 #endif /* CONFIG_YAFFS_YAFFS2 */
1944 static struct proc_dir_entry *my_proc_entry;
1946 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
1948 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
1949 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
1950 buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
1951 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
1952 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
1953 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
1954 buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
1955 buf += sprintf(buf, "nCheckptResBlocks.. %d\n", dev->nCheckpointReservedBlocks);
1956 buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
1957 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
1958 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
1959 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
1960 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
1961 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
1962 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
1963 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
1964 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
1965 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
1967 sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
1969 sprintf(buf, "passiveGCs......... %d\n",
1970 dev->passiveGarbageCollections);
1971 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
1972 buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
1973 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
1974 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
1975 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
1976 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
1977 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
1978 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
1979 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
1980 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
1982 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
1983 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
1984 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
1989 static int yaffs_proc_read(char *page,
1991 off_t offset, int count, int *eof, void *data)
1993 struct list_head *item;
1998 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
1999 * We use 'offset' (*ppos) to indicate where we are in devList.
2000 * This also assumes the user has posted a read buffer large
2001 * enough to hold the complete output; but that's life in /proc.
2006 /* Print header first */
2008 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
2009 "\n%s\n%s\n", yaffs_fs_c_version,
2010 yaffs_guts_c_version);
2013 /* hold lock_kernel while traversing yaffs_dev_list */
2016 /* Locate and print the Nth entry. Order N-squared but N is small. */
2017 list_for_each(item, &yaffs_dev_list) {
2018 yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
2023 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
2024 buf = yaffs_dump_dev(buf, dev);
2029 return buf - page < count ? buf - page : count;
2033 * Set the verbosity of the warnings and error messages.
2035 * Note that the names can only be a..z or _ with the current code.
2040 unsigned mask_bitfield;
2042 {"allocate", YAFFS_TRACE_ALLOCATE},
2043 {"always", YAFFS_TRACE_ALWAYS},
2044 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
2045 {"buffers", YAFFS_TRACE_BUFFERS},
2046 {"bug", YAFFS_TRACE_BUG},
2047 {"checkpt", YAFFS_TRACE_CHECKPOINT},
2048 {"deletion", YAFFS_TRACE_DELETION},
2049 {"erase", YAFFS_TRACE_ERASE},
2050 {"error", YAFFS_TRACE_ERROR},
2051 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
2052 {"gc", YAFFS_TRACE_GC},
2053 {"mtd", YAFFS_TRACE_MTD},
2054 {"nandaccess", YAFFS_TRACE_NANDACCESS},
2055 {"os", YAFFS_TRACE_OS},
2056 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
2057 {"scan", YAFFS_TRACE_SCAN},
2058 {"tracing", YAFFS_TRACE_TRACING},
2060 {"verify", YAFFS_TRACE_VERIFY},
2061 {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
2062 {"verify_full", YAFFS_TRACE_VERIFY_FULL},
2063 {"verify_all", YAFFS_TRACE_VERIFY_ALL},
2065 {"write", YAFFS_TRACE_WRITE},
2066 {"all", 0xffffffff},
2071 #define MAX_MASK_NAME_LENGTH 40
2072 static int yaffs_proc_write(struct file *file, const char *buf,
2073 unsigned long count, void *data)
2075 unsigned rg = 0, mask_bitfield;
2079 char substring[MAX_MASK_NAME_LENGTH+1];
2085 rg = yaffs_traceMask;
2087 while (!done && (pos < count)) {
2089 while ((pos < count) && isspace(buf[pos])) {
2107 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
2108 if (end > buf + pos) {
2109 mask_name = "numeral";
2110 len = end - (buf + pos);
2113 for(x = buf + pos, i = 0;
2114 (*x == '_' || (*x >='a' && *x <= 'z')) &&
2115 i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
2117 substring[i] = '\0';
2119 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2120 //len = strlen(mask_flags[i].mask_name);
2121 //if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
2122 if(strcmp(substring,mask_flags[i].mask_name) == 0){
2123 mask_name = mask_flags[i].mask_name;
2124 mask_bitfield = mask_flags[i].mask_bitfield;
2131 if (mask_name != NULL) {
2136 rg &= ~mask_bitfield;
2139 rg |= mask_bitfield;
2145 rg |= mask_bitfield;
2151 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2153 printk("new trace = 0x%08X\n",yaffs_traceMask);
2155 if (rg & YAFFS_TRACE_ALWAYS) {
2156 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2158 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2159 printk("%c%s\n", flag, mask_flags[i].mask_name);
2166 /* Stuff to handle installation of file systems */
2167 struct file_system_to_install {
2168 struct file_system_type *fst;
2172 static struct file_system_to_install fs_to_install[] = {
2173 //#ifdef CONFIG_YAFFS_YAFFS1
2174 {&yaffs_fs_type, 0},
2176 //#ifdef CONFIG_YAFFS_YAFFS2
2177 {&yaffs2_fs_type, 0},
2182 static int __init init_yaffs_fs(void)
2185 struct file_system_to_install *fsinst;
2187 T(YAFFS_TRACE_ALWAYS,
2188 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2190 /* Install the proc_fs entry */
2191 my_proc_entry = create_proc_entry("yaffs",
2195 if (my_proc_entry) {
2196 my_proc_entry->write_proc = yaffs_proc_write;
2197 my_proc_entry->read_proc = yaffs_proc_read;
2198 my_proc_entry->data = NULL;
2203 /* Now add the file system entries */
2205 fsinst = fs_to_install;
2207 while (fsinst->fst && !error) {
2208 error = register_filesystem(fsinst->fst);
2210 fsinst->installed = 1;
2215 /* Any errors? uninstall */
2217 fsinst = fs_to_install;
2219 while (fsinst->fst) {
2220 if (fsinst->installed) {
2221 unregister_filesystem(fsinst->fst);
2222 fsinst->installed = 0;
2231 static void __exit exit_yaffs_fs(void)
2234 struct file_system_to_install *fsinst;
2236 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2239 remove_proc_entry("yaffs", &proc_root);
2241 fsinst = fs_to_install;
2243 while (fsinst->fst) {
2244 if (fsinst->installed) {
2245 unregister_filesystem(fsinst->fst);
2246 fsinst->installed = 0;
2253 module_init(init_yaffs_fs)
2254 module_exit(exit_yaffs_fs)
2256 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2257 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2258 MODULE_LICENSE("GPL");