2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
5 * Copyright (C) 2002 Aleph One Ltd.
6 * for Toby Churchill Ltd and Brightstar Engineering
8 * Created by Charles Manning <charles@aleph1.co.uk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * This is the file system front-end to YAFFS that hooks it up to
18 * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
20 * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
22 * >> inode->u.generic_ip points to the associated yaffs_Object.
25 * * Luc van OostenRyck for numerous patches.
26 * * Nick Bane for numerous patches.
27 * * Nick Bane for 2.5/2.6 integration.
28 * * Andras Toth for mknod rdev issue.
29 * * Michael Fischer for finding the problem with inode inconsistency.
30 * * Some code bodily lifted from JFFS2.
33 const char *yaffs_fs_c_version =
34 "$Id: yaffs_fs.c,v 1.56 2007-01-19 04:07:48 charles Exp $";
35 extern const char *yaffs_guts_c_version;
37 #include <linux/version.h>
38 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
39 #include <linux/config.h>
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/slab.h>
44 #include <linux/init.h>
45 #include <linux/list.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 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
57 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
58 #include <asm/statfs.h>
59 #define UnlockPage(p) unlock_page(p)
60 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
62 /* FIXME: use sb->s_id instead ? */
63 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
67 #include <linux/locks.h>
68 #define BDEVNAME_SIZE 0
69 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
71 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
72 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
78 #include <asm/uaccess.h>
81 #include "yaffs_guts.h"
83 unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS |
84 YAFFS_TRACE_BAD_BLOCKS |
85 YAFFS_TRACE_CHECKPOINT
88 #include <linux/mtd/mtd.h>
89 #include "yaffs_mtdif.h"
90 #include "yaffs_mtdif2.h"
92 /*#define T(x) printk x */
94 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
95 #define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
97 #define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
100 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
101 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
103 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
104 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
106 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
109 static void yaffs_put_super(struct super_block *sb);
111 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
114 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
115 static int yaffs_file_flush(struct file *file, fl_owner_t id);
117 static int yaffs_file_flush(struct file *file);
120 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
123 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
125 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
126 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
127 struct nameidata *n);
128 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
129 struct nameidata *n);
131 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
132 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
134 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
135 struct dentry *dentry);
136 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
137 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
138 const char *symname);
139 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
141 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
142 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
145 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
148 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
149 struct inode *new_dir, struct dentry *new_dentry);
150 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
152 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
153 static int yaffs_sync_fs(struct super_block *sb, int wait);
154 static void yaffs_write_super(struct super_block *sb);
156 static int yaffs_sync_fs(struct super_block *sb);
157 static int yaffs_write_super(struct super_block *sb);
160 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
161 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
162 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
163 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
165 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
167 static void yaffs_read_inode(struct inode *inode);
169 static void yaffs_put_inode(struct inode *inode);
170 static void yaffs_delete_inode(struct inode *);
171 static void yaffs_clear_inode(struct inode *);
173 static int yaffs_readpage(struct file *file, struct page *page);
174 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
175 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
177 static int yaffs_writepage(struct page *page);
179 static int yaffs_prepare_write(struct file *f, struct page *pg,
180 unsigned offset, unsigned to);
181 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
184 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
186 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
187 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
189 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
192 static struct address_space_operations yaffs_file_address_operations = {
193 .readpage = yaffs_readpage,
194 .writepage = yaffs_writepage,
195 .prepare_write = yaffs_prepare_write,
196 .commit_write = yaffs_commit_write,
199 static struct file_operations yaffs_file_operations = {
200 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
201 .read = do_sync_read,
202 .write = do_sync_write,
203 .aio_read = generic_file_aio_read,
204 .aio_write = generic_file_aio_write,
206 .read = generic_file_read,
207 .write = generic_file_write,
209 .mmap = generic_file_mmap,
210 .flush = yaffs_file_flush,
211 .fsync = yaffs_sync_object,
212 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
213 .sendfile = generic_file_sendfile,
218 static struct inode_operations yaffs_file_inode_operations = {
219 .setattr = yaffs_setattr,
222 static struct inode_operations yaffs_symlink_inode_operations = {
223 .readlink = yaffs_readlink,
224 .follow_link = yaffs_follow_link,
225 .setattr = yaffs_setattr,
228 static struct inode_operations yaffs_dir_inode_operations = {
229 .create = yaffs_create,
230 .lookup = yaffs_lookup,
232 .unlink = yaffs_unlink,
233 .symlink = yaffs_symlink,
234 .mkdir = yaffs_mkdir,
235 .rmdir = yaffs_unlink,
236 .mknod = yaffs_mknod,
237 .rename = yaffs_rename,
238 .setattr = yaffs_setattr,
241 static struct file_operations yaffs_dir_operations = {
242 .read = generic_read_dir,
243 .readdir = yaffs_readdir,
244 .fsync = yaffs_sync_object,
247 static struct super_operations yaffs_super_ops = {
248 .statfs = yaffs_statfs,
249 .read_inode = yaffs_read_inode,
250 .put_inode = yaffs_put_inode,
251 .put_super = yaffs_put_super,
252 .delete_inode = yaffs_delete_inode,
253 .clear_inode = yaffs_clear_inode,
254 .sync_fs = yaffs_sync_fs,
255 .write_super = yaffs_write_super,
258 static void yaffs_GrossLock(yaffs_Device * dev)
260 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
262 down(&dev->grossLock);
265 static void yaffs_GrossUnlock(yaffs_Device * dev)
267 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
272 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
275 unsigned char *alias;
278 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
280 yaffs_GrossLock(dev);
282 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
284 yaffs_GrossUnlock(dev);
289 ret = vfs_readlink(dentry, buffer, buflen, alias);
294 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
295 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
297 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
300 unsigned char *alias;
302 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
304 yaffs_GrossLock(dev);
306 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
308 yaffs_GrossUnlock(dev);
316 ret = vfs_follow_link(nd, alias);
319 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
320 return ERR_PTR (ret);
326 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
330 * Lookup is used to find objects in the fs
332 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
334 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
337 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
341 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
343 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
345 yaffs_GrossLock(dev);
348 (KERN_DEBUG "yaffs_lookup for %d:%s\n",
349 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
352 yaffs_FindObjectByName(yaffs_InodeToObject(dir),
353 dentry->d_name.name);
355 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
357 /* Can't hold gross lock when calling yaffs_get_inode() */
358 yaffs_GrossUnlock(dev);
362 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
364 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
368 (KERN_DEBUG "yaffs_loookup dentry \n"));
369 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
370 * d_add even if NULL inode */
372 /*dget(dentry); // try to solve directory bug */
373 d_add(dentry, inode);
381 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
385 /* added NCB for 2.5/6 compatability - forces add even if inode is
386 * NULL which creates dentry hash */
387 d_add(dentry, inode);
390 /* return (ERR_PTR(-EIO)); */
394 /* For now put inode is just for debugging
395 * Put inode is called when the inode **structure** is put.
397 static void yaffs_put_inode(struct inode *inode)
400 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
401 atomic_read(&inode->i_count)));
405 /* clear is called to tell the fs to release any per-inode data it holds */
406 static void yaffs_clear_inode(struct inode *inode)
411 obj = yaffs_InodeToObject(inode);
414 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
415 atomic_read(&inode->i_count),
416 obj ? "object exists" : "null object"));
420 yaffs_GrossLock(dev);
422 /* Clear the association between the inode and
426 yaffs_InodeToObjectLV(inode) = NULL;
428 /* If the object freeing was deferred, then the real
430 * This should fix the inode inconsistency problem.
433 yaffs_HandleDeferedFree(obj);
435 yaffs_GrossUnlock(dev);
440 /* delete is called when the link count is zero and the inode
441 * is put (ie. nobody wants to know about it anymore, time to
443 * NB Must call clear_inode()
445 static void yaffs_delete_inode(struct inode *inode)
447 yaffs_Object *obj = yaffs_InodeToObject(inode);
451 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
452 atomic_read(&inode->i_count),
453 obj ? "object exists" : "null object"));
457 yaffs_GrossLock(dev);
458 yaffs_DeleteFile(obj);
459 yaffs_GrossUnlock(dev);
461 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
462 truncate_inode_pages (&inode->i_data, 0);
467 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
468 static int yaffs_file_flush(struct file *file, fl_owner_t id)
470 static int yaffs_file_flush(struct file *file)
473 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
475 yaffs_Device *dev = obj->myDev;
478 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
479 obj->dirty ? "dirty" : "clean"));
481 yaffs_GrossLock(dev);
483 yaffs_FlushFile(obj, 1);
485 yaffs_GrossUnlock(dev);
490 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
492 /* Lifted from jffs2 */
495 unsigned char *pg_buf;
500 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
501 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
502 (unsigned)PAGE_CACHE_SIZE));
504 obj = yaffs_DentryToObject(f->f_dentry);
508 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
509 BUG_ON(!PageLocked(pg));
516 /* FIXME: Can kmap fail? */
518 yaffs_GrossLock(dev);
521 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
524 yaffs_GrossUnlock(dev);
530 ClearPageUptodate(pg);
537 flush_dcache_page(pg);
540 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
544 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
546 int ret = yaffs_readpage_nolock(f, pg);
551 static int yaffs_readpage(struct file *f, struct page *pg)
553 return yaffs_readpage_unlock(f, pg);
556 /* writepage inspired by/stolen from smbfs */
558 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
559 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
561 static int yaffs_writepage(struct page *page)
564 struct address_space *mapping = page->mapping;
565 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
567 unsigned long end_index;
575 inode = mapping->host;
579 if (offset > inode->i_size) {
582 "yaffs_writepage at %08x, inode size = %08x!!!\n",
583 (unsigned)(page->index << PAGE_CACHE_SHIFT),
584 (unsigned)inode->i_size));
586 (KERN_DEBUG " -> don't care!!\n"));
591 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
594 if (page->index < end_index) {
595 nBytes = PAGE_CACHE_SIZE;
597 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
604 obj = yaffs_InodeToObject(inode);
605 yaffs_GrossLock(obj->myDev);
608 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
609 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
611 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
612 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
615 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
619 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
620 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
622 yaffs_GrossUnlock(obj->myDev);
625 SetPageUptodate(page);
629 return (nWritten == nBytes) ? 0 : -ENOSPC;
632 static int yaffs_prepare_write(struct file *f, struct page *pg,
633 unsigned offset, unsigned to)
636 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
637 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
638 return yaffs_readpage_nolock(f, pg);
644 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
648 void *addr = page_address(pg) + offset;
649 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
650 int nBytes = to - offset;
654 unsigned saddr = (unsigned)addr;
657 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
660 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
662 if (nWritten != nBytes) {
665 "yaffs_commit_write not same size nWritten %d nBytes %d\n",
668 ClearPageUptodate(pg);
674 (KERN_DEBUG "yaffs_commit_write returning %d\n",
675 nWritten == nBytes ? 0 : nWritten));
677 return nWritten == nBytes ? 0 : nWritten;
681 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
686 /* Check mode against the variant type and attempt to repair if broken. */
687 __u32 mode = obj->yst_mode;
688 switch( obj->variantType ){
689 case YAFFS_OBJECT_TYPE_FILE :
690 if( ! S_ISREG(mode) ){
691 obj->yst_mode &= ~S_IFMT;
692 obj->yst_mode |= S_IFREG;
696 case YAFFS_OBJECT_TYPE_SYMLINK :
697 if( ! S_ISLNK(mode) ){
698 obj->yst_mode &= ~S_IFMT;
699 obj->yst_mode |= S_IFLNK;
703 case YAFFS_OBJECT_TYPE_DIRECTORY :
704 if( ! S_ISDIR(mode) ){
705 obj->yst_mode &= ~S_IFMT;
706 obj->yst_mode |= S_IFDIR;
710 case YAFFS_OBJECT_TYPE_UNKNOWN :
711 case YAFFS_OBJECT_TYPE_HARDLINK :
712 case YAFFS_OBJECT_TYPE_SPECIAL :
718 inode->i_ino = obj->objectId;
719 inode->i_mode = obj->yst_mode;
720 inode->i_uid = obj->yst_uid;
721 inode->i_gid = obj->yst_gid;
722 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
723 inode->i_blksize = inode->i_sb->s_blocksize;
725 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
727 inode->i_rdev = old_decode_dev(obj->yst_rdev);
728 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
729 inode->i_atime.tv_nsec = 0;
730 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
731 inode->i_mtime.tv_nsec = 0;
732 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
733 inode->i_ctime.tv_nsec = 0;
735 inode->i_rdev = obj->yst_rdev;
736 inode->i_atime = obj->yst_atime;
737 inode->i_mtime = obj->yst_mtime;
738 inode->i_ctime = obj->yst_ctime;
740 inode->i_size = yaffs_GetObjectFileLength(obj);
741 inode->i_blocks = (inode->i_size + 511) >> 9;
743 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
747 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
748 inode->i_mode, inode->i_uid, inode->i_gid,
749 (int)inode->i_size, atomic_read(&inode->i_count)));
751 switch (obj->yst_mode & S_IFMT) {
752 default: /* fifo, device or socket */
753 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
754 init_special_inode(inode, obj->yst_mode,
755 old_decode_dev(obj->yst_rdev));
757 init_special_inode(inode, obj->yst_mode,
758 (dev_t) (obj->yst_rdev));
761 case S_IFREG: /* file */
762 inode->i_op = &yaffs_file_inode_operations;
763 inode->i_fop = &yaffs_file_operations;
764 inode->i_mapping->a_ops =
765 &yaffs_file_address_operations;
767 case S_IFDIR: /* directory */
768 inode->i_op = &yaffs_dir_inode_operations;
769 inode->i_fop = &yaffs_dir_operations;
771 case S_IFLNK: /* symlink */
772 inode->i_op = &yaffs_symlink_inode_operations;
776 yaffs_InodeToObjectLV(inode) = obj;
778 obj->myInode = inode;
782 (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
787 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
794 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
801 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
807 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
809 inode = iget(sb, obj->objectId);
811 /* NB Side effect: iget calls back to yaffs_read_inode(). */
812 /* iget also increments the inode's i_count */
813 /* NB You can't be holding grossLock or deadlock will happen! */
818 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
826 obj = yaffs_DentryToObject(f->f_dentry);
830 yaffs_GrossLock(dev);
832 inode = f->f_dentry->d_inode;
834 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
835 ipos = inode->i_size;
842 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
846 "yaffs_file_write about to write writing %d bytes"
847 "to object %d at %d\n",
848 n, obj->objectId, ipos));
851 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
854 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
859 if (ipos > inode->i_size) {
860 inode->i_size = ipos;
861 inode->i_blocks = (ipos + 511) >> 9;
865 "yaffs_file_write size updated to %d bytes, "
867 ipos, (int)(inode->i_blocks)));
871 yaffs_GrossUnlock(dev);
872 return nWritten == 0 ? -ENOSPC : nWritten;
875 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
879 struct inode *inode = f->f_dentry->d_inode;
880 unsigned long offset, curoffs;
884 char name[YAFFS_MAX_NAME_LENGTH + 1];
886 obj = yaffs_DentryToObject(f->f_dentry);
889 yaffs_GrossLock(dev);
893 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
897 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
899 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
908 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
909 (int)f->f_dentry->d_parent->d_inode->i_ino));
911 (dirent, "..", 2, offset,
912 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
921 /* If the directory has changed since the open or last call to
922 readdir, rewind to after the 2 canned entries. */
924 if (f->f_version != inode->i_version) {
927 f->f_version = inode->i_version;
930 list_for_each(i, &obj->variant.directoryVariant.children) {
932 if (curoffs >= offset) {
933 l = list_entry(i, yaffs_Object, siblings);
935 yaffs_GetObjectName(l, name,
936 YAFFS_MAX_NAME_LENGTH + 1);
938 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
939 yaffs_GetObjectInode(l)));
945 yaffs_GetObjectInode(l),
946 yaffs_GetObjectType(l))
959 yaffs_GrossUnlock(dev);
965 * File creation. Allocate an inode, and we're done..
967 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
968 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
971 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
977 yaffs_Object *obj = NULL;
980 yaffs_Object *parent = yaffs_InodeToObject(dir);
983 uid_t uid = current->fsuid;
984 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
986 if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
991 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
992 parent->objectId, parent->variantType));
995 (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
999 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1001 dentry->d_name.name, mode, rdev));
1003 dev = parent->myDev;
1005 yaffs_GrossLock(dev);
1007 switch (mode & S_IFMT) {
1009 /* Special (socket, fifo, device...) */
1010 T(YAFFS_TRACE_OS, (KERN_DEBUG
1011 "yaffs_mknod: making special\n"));
1012 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1014 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1015 gid, old_encode_dev(rdev));
1018 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1022 case S_IFREG: /* file */
1023 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1025 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1028 case S_IFDIR: /* directory */
1030 (KERN_DEBUG "yaffs_mknod: making directory\n"));
1032 yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1035 case S_IFLNK: /* symlink */
1036 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1037 obj = NULL; /* Do we ever get here? */
1041 /* Can not call yaffs_get_inode() with gross lock held */
1042 yaffs_GrossUnlock(dev);
1045 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1046 d_instantiate(dentry, inode);
1048 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
1049 obj->objectId, atomic_read(&inode->i_count)));
1053 (KERN_DEBUG "yaffs_mknod failed making object\n"));
1060 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1063 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
1064 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1066 /* attempt to fix dir bug - didn't work */
1074 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1075 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1076 struct nameidata *n)
1078 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1081 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1082 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1085 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1092 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1093 dentry->d_name.name));
1095 dev = yaffs_InodeToObject(dir)->myDev;
1097 yaffs_GrossLock(dev);
1099 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1101 if (retVal == YAFFS_OK) {
1102 dentry->d_inode->i_nlink--;
1104 yaffs_GrossUnlock(dev);
1105 mark_inode_dirty(dentry->d_inode);
1108 yaffs_GrossUnlock(dev);
1115 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1116 struct dentry *dentry)
1118 struct inode *inode = old_dentry->d_inode;
1119 yaffs_Object *obj = NULL;
1120 yaffs_Object *link = NULL;
1123 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1125 obj = yaffs_InodeToObject(inode);
1128 yaffs_GrossLock(dev);
1130 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1133 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1138 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1139 d_instantiate(dentry, old_dentry->d_inode);
1140 atomic_inc(&old_dentry->d_inode->i_count);
1142 (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1143 old_dentry->d_inode->i_nlink,
1144 atomic_read(&old_dentry->d_inode->i_count)));
1148 yaffs_GrossUnlock(dev);
1158 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1159 const char *symname)
1163 uid_t uid = current->fsuid;
1164 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1166 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1168 dev = yaffs_InodeToObject(dir)->myDev;
1169 yaffs_GrossLock(dev);
1170 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1171 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1172 yaffs_GrossUnlock(dev);
1176 struct inode *inode;
1178 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1179 d_instantiate(dentry, inode);
1180 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1183 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1190 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1197 obj = yaffs_DentryToObject(dentry);
1201 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1202 yaffs_GrossLock(dev);
1203 yaffs_FlushFile(obj, 1);
1204 yaffs_GrossUnlock(dev);
1209 * The VFS layer already does all the dentry stuff for rename.
1211 * NB: POSIX says you can rename an object over an old object of the same name
1213 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1214 struct inode *new_dir, struct dentry *new_dentry)
1217 int retVal = YAFFS_FAIL;
1218 yaffs_Object *target;
1220 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1221 dev = yaffs_InodeToObject(old_dir)->myDev;
1223 yaffs_GrossLock(dev);
1225 /* Check if the target is an existing directory that is not empty. */
1227 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1228 new_dentry->d_name.name);
1233 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1234 !list_empty(&target->variant.directoryVariant.children)) {
1236 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1238 retVal = YAFFS_FAIL;
1241 /* Now does unlinking internally using shadowing mechanism */
1242 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1245 yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1246 old_dentry->d_name.name,
1247 yaffs_InodeToObject(new_dir),
1248 new_dentry->d_name.name);
1251 yaffs_GrossUnlock(dev);
1253 if (retVal == YAFFS_OK) {
1255 new_dentry->d_inode->i_nlink--;
1256 mark_inode_dirty(new_dentry->d_inode);
1266 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1268 struct inode *inode = dentry->d_inode;
1273 (KERN_DEBUG "yaffs_setattr of object %d\n",
1274 yaffs_InodeToObject(inode)->objectId));
1276 if ((error = inode_change_ok(inode, attr)) == 0) {
1278 dev = yaffs_InodeToObject(inode)->myDev;
1279 yaffs_GrossLock(dev);
1280 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1286 yaffs_GrossUnlock(dev);
1288 error = inode_setattr(inode, attr);
1293 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1294 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1296 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1297 struct super_block *sb = dentry->d_sb;
1298 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1299 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1301 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1303 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1305 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1308 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1310 yaffs_GrossLock(dev);
1312 buf->f_type = YAFFS_MAGIC;
1313 buf->f_bsize = sb->s_blocksize;
1314 buf->f_namelen = 255;
1315 if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1318 (dev->endBlock - dev->startBlock +
1319 1) * dev->nChunksPerBlock / (sb->s_blocksize /
1320 dev->nDataBytesPerChunk);
1322 yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
1323 dev->nDataBytesPerChunk);
1327 (dev->endBlock - dev->startBlock +
1328 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
1331 yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
1336 buf->f_bavail = buf->f_bfree;
1338 yaffs_GrossUnlock(dev);
1344 static int yaffs_do_sync_fs(struct super_block *sb)
1347 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1348 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
1351 yaffs_GrossLock(dev);
1354 yaffs_CheckpointSave(dev);
1356 yaffs_GrossUnlock(dev);
1364 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1365 static void yaffs_write_super(struct super_block *sb)
1367 static int yaffs_write_super(struct super_block *sb)
1371 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
1372 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
1373 return 0; /* yaffs_do_sync_fs(sb);*/
1378 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1379 static int yaffs_sync_fs(struct super_block *sb, int wait)
1381 static int yaffs_sync_fs(struct super_block *sb)
1385 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
1387 return 0; /* yaffs_do_sync_fs(sb);*/
1392 static void yaffs_read_inode(struct inode *inode)
1394 /* NB This is called as a side effect of other functions, but
1395 * we had to release the lock to prevent deadlocks, so
1396 * need to lock again.
1400 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1403 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1405 yaffs_GrossLock(dev);
1407 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1409 yaffs_FillInodeFromObject(inode, obj);
1411 yaffs_GrossUnlock(dev);
1414 static LIST_HEAD(yaffs_dev_list);
1416 static void yaffs_put_super(struct super_block *sb)
1418 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1420 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
1422 yaffs_GrossLock(dev);
1424 yaffs_FlushEntireDeviceCache(dev);
1426 if (dev->putSuperFunc) {
1427 dev->putSuperFunc(sb);
1430 yaffs_CheckpointSave(dev);
1431 yaffs_Deinitialise(dev);
1433 yaffs_GrossUnlock(dev);
1435 /* we assume this is protected by lock_kernel() in mount/umount */
1436 list_del(&dev->devList);
1438 if(dev->spareBuffer){
1439 YFREE(dev->spareBuffer);
1440 dev->spareBuffer = NULL;
1447 static void yaffs_MTDPutSuper(struct super_block *sb)
1450 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1456 put_mtd_device(mtd);
1460 static void yaffs_MarkSuperBlockDirty(void *vsb)
1462 struct super_block *sb = (struct super_block *)vsb;
1464 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
1469 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1470 struct super_block *sb,
1471 void *data, int silent)
1474 struct inode *inode = NULL;
1475 struct dentry *root;
1476 yaffs_Device *dev = 0;
1477 char devname_buf[BDEVNAME_SIZE + 1];
1478 struct mtd_info *mtd;
1481 sb->s_magic = YAFFS_MAGIC;
1482 sb->s_op = &yaffs_super_ops;
1485 printk(KERN_INFO "yaffs: sb is NULL\n");
1486 else if (!sb->s_dev)
1487 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1488 else if (!yaffs_devname(sb, devname_buf))
1489 printk(KERN_INFO "yaffs: devname is NULL\n");
1491 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1493 yaffs_devname(sb, devname_buf));
1495 sb->s_blocksize = PAGE_CACHE_SIZE;
1496 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1497 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1499 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1501 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1503 ("yaffs: Write verification disabled. All guarantees "
1504 "null and void\n"));
1507 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1509 MAJOR(sb->s_dev), MINOR(sb->s_dev),
1510 yaffs_devname(sb, devname_buf)));
1512 /* Check it's an mtd device..... */
1513 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1514 return NULL; /* This isn't an mtd device */
1516 /* Get the device */
1517 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1519 T(YAFFS_TRACE_ALWAYS,
1520 ("yaffs: MTD device #%u doesn't appear to exist\n",
1524 /* Check it's NAND */
1525 if (mtd->type != MTD_NANDFLASH) {
1526 T(YAFFS_TRACE_ALWAYS,
1527 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1531 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1532 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1533 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1534 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1535 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1536 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1537 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1538 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1539 T(YAFFS_TRACE_OS, (" writesize %d\n", mtd->writesize));
1541 T(YAFFS_TRACE_OS, (" oobblock %d\n", mtd->oobblock));
1543 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1544 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1545 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1547 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1549 if (yaffsVersion == 1 &&
1550 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1551 mtd->writesize >= 2048) {
1553 mtd->oobblock >= 2048) {
1555 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1559 /* Added NCB 26/5/2006 for completeness */
1560 if (yaffsVersion == 2 &&
1561 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1562 mtd->writesize == 512) {
1564 mtd->oobblock == 512) {
1566 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
1572 if (yaffsVersion == 2) {
1573 /* Check for version 2 style functions */
1575 !mtd->block_isbad ||
1576 !mtd->block_markbad ||
1579 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1580 !mtd->read_oob || !mtd->write_oob) {
1583 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1585 T(YAFFS_TRACE_ALWAYS,
1586 ("yaffs: MTD device does not support required "
1591 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1592 if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1594 if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1596 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
1597 T(YAFFS_TRACE_ALWAYS,
1598 ("yaffs: MTD device does not have the "
1599 "right page sizes\n"));
1603 /* Check for V1 style functions */
1607 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1608 !mtd->read_oob || !mtd->write_oob) {
1611 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1613 T(YAFFS_TRACE_ALWAYS,
1614 ("yaffs: MTD device does not support required "
1619 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1620 if (mtd->writesize < YAFFS_BYTES_PER_CHUNK ||
1622 if (mtd->oobblock < YAFFS_BYTES_PER_CHUNK ||
1624 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1625 T(YAFFS_TRACE_ALWAYS,
1626 ("yaffs: MTD device does not support have the "
1627 "right page sizes\n"));
1632 /* OK, so if we got here, we have an MTD that's NAND and looks
1633 * like it has the right capabilities
1634 * Set the yaffs_Device up for mtd
1637 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1638 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1640 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1643 /* Deep shit could not allocate device structure */
1644 T(YAFFS_TRACE_ALWAYS,
1645 ("yaffs_read_super: Failed trying to allocate "
1646 "yaffs_Device. \n"));
1650 memset(dev, 0, sizeof(yaffs_Device));
1651 dev->genericDevice = mtd;
1652 dev->name = mtd->name;
1654 /* Set up the memory size parameters.... */
1656 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1657 dev->startBlock = 0;
1658 dev->endBlock = nBlocks - 1;
1659 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1660 dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1661 dev->nReservedBlocks = 5;
1662 dev->nShortOpCaches = 10; /* Enable short op caching */
1664 /* ... and the functions. */
1665 if (yaffsVersion == 2) {
1666 dev->writeChunkWithTagsToNAND =
1667 nandmtd2_WriteChunkWithTagsToNAND;
1668 dev->readChunkWithTagsFromNAND =
1669 nandmtd2_ReadChunkWithTagsFromNAND;
1670 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1671 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1672 dev->spareBuffer = YMALLOC(mtd->oobsize);
1674 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1675 dev->nDataBytesPerChunk = mtd->writesize;
1676 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
1678 dev->nDataBytesPerChunk = mtd->oobblock;
1679 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1681 nBlocks = mtd->size / mtd->erasesize;
1683 dev->nCheckpointReservedBlocks = CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS;
1684 dev->startBlock = 0;
1685 dev->endBlock = nBlocks - 1;
1687 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1688 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1691 /* ... and common functions */
1692 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1693 dev->initialiseNAND = nandmtd_InitialiseNAND;
1695 dev->putSuperFunc = yaffs_MTDPutSuper;
1697 dev->superBlock = (void *)sb;
1698 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
1701 #ifndef CONFIG_YAFFS_DOES_ECC
1702 dev->useNANDECC = 1;
1705 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1706 dev->wideTnodesDisabled = 1;
1709 /* we assume this is protected by lock_kernel() in mount/umount */
1710 list_add_tail(&dev->devList, &yaffs_dev_list);
1712 init_MUTEX(&dev->grossLock);
1714 yaffs_GrossLock(dev);
1716 err = yaffs_GutsInitialise(dev);
1719 ("yaffs_read_super: guts initialised %s\n",
1720 (err == YAFFS_OK) ? "OK" : "FAILED"));
1722 /* Release lock before yaffs_get_inode() */
1723 yaffs_GrossUnlock(dev);
1725 /* Create root inode */
1726 if (err == YAFFS_OK)
1727 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1733 inode->i_op = &yaffs_dir_inode_operations;
1734 inode->i_fop = &yaffs_dir_operations;
1736 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1738 root = d_alloc_root(inode);
1740 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1748 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1753 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1754 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
1757 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
1760 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1761 static int yaffs_read_super(struct file_system_type *fs,
1762 int flags, const char *dev_name,
1763 void *data, struct vfsmount *mnt)
1766 return get_sb_bdev(fs, flags, dev_name, data,
1767 yaffs_internal_read_super_mtd, mnt);
1770 static struct super_block *yaffs_read_super(struct file_system_type *fs,
1771 int flags, const char *dev_name,
1775 return get_sb_bdev(fs, flags, dev_name, data,
1776 yaffs_internal_read_super_mtd);
1780 static struct file_system_type yaffs_fs_type = {
1781 .owner = THIS_MODULE,
1783 .get_sb = yaffs_read_super,
1784 .kill_sb = kill_block_super,
1785 .fs_flags = FS_REQUIRES_DEV,
1788 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
1791 return yaffs_internal_read_super(1, sb, data, silent);
1794 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
1799 #ifdef CONFIG_YAFFS_YAFFS2
1801 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1802 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
1805 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
1808 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1809 static int yaffs2_read_super(struct file_system_type *fs,
1810 int flags, const char *dev_name, void *data,
1811 struct vfsmount *mnt)
1813 return get_sb_bdev(fs, flags, dev_name, data,
1814 yaffs2_internal_read_super_mtd, mnt);
1817 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
1818 int flags, const char *dev_name,
1822 return get_sb_bdev(fs, flags, dev_name, data,
1823 yaffs2_internal_read_super_mtd);
1827 static struct file_system_type yaffs2_fs_type = {
1828 .owner = THIS_MODULE,
1830 .get_sb = yaffs2_read_super,
1831 .kill_sb = kill_block_super,
1832 .fs_flags = FS_REQUIRES_DEV,
1835 static struct super_block *yaffs2_read_super(struct super_block *sb,
1836 void *data, int silent)
1838 return yaffs_internal_read_super(2, sb, data, silent);
1841 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
1845 #endif /* CONFIG_YAFFS_YAFFS2 */
1847 static struct proc_dir_entry *my_proc_entry;
1849 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
1851 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
1852 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
1853 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
1854 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
1855 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
1856 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
1857 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
1858 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
1859 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
1860 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
1861 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
1862 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
1863 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
1864 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
1866 sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
1868 sprintf(buf, "passiveGCs......... %d\n",
1869 dev->passiveGarbageCollections);
1870 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
1871 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
1872 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
1873 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
1874 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
1875 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
1876 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
1877 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
1878 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
1880 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
1881 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
1882 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
1887 static int yaffs_proc_read(char *page,
1889 off_t offset, int count, int *eof, void *data)
1891 struct list_head *item;
1896 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
1897 * We use 'offset' (*ppos) to indicate where we are in devList.
1898 * This also assumes the user has posted a read buffer large
1899 * enough to hold the complete output; but that's life in /proc.
1904 /* Print header first */
1906 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
1907 "\n%s\n%s\n", yaffs_fs_c_version,
1908 yaffs_guts_c_version);
1911 /* hold lock_kernel while traversing yaffs_dev_list */
1914 /* Locate and print the Nth entry. Order N-squared but N is small. */
1915 list_for_each(item, &yaffs_dev_list) {
1916 yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
1921 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
1922 buf = yaffs_dump_dev(buf, dev);
1927 return buf - page < count ? buf - page : count;
1931 * Set the verbosity of the warnings and error messages.
1937 unsigned mask_bitfield;
1939 {"allocate", YAFFS_TRACE_ALLOCATE},
1940 {"always", YAFFS_TRACE_ALWAYS},
1941 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
1942 {"buffers", YAFFS_TRACE_BUFFERS},
1943 {"bug", YAFFS_TRACE_BUG},
1944 {"deletion", YAFFS_TRACE_DELETION},
1945 {"erase", YAFFS_TRACE_ERASE},
1946 {"error", YAFFS_TRACE_ERROR},
1947 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
1948 {"gc", YAFFS_TRACE_GC},
1949 {"mtd", YAFFS_TRACE_MTD},
1950 {"nandaccess", YAFFS_TRACE_NANDACCESS},
1951 {"os", YAFFS_TRACE_OS},
1952 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
1953 {"scan", YAFFS_TRACE_SCAN},
1954 {"tracing", YAFFS_TRACE_TRACING},
1955 {"write", YAFFS_TRACE_WRITE},
1956 {"all", 0xffffffff},
1961 static int yaffs_proc_write(struct file *file, const char *buf,
1962 unsigned long count, void *data)
1964 unsigned rg = 0, mask_bitfield;
1965 char *end, *mask_name;
1971 rg = yaffs_traceMask;
1973 while (!done && (pos < count)) {
1975 while ((pos < count) && isspace(buf[pos])) {
1992 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
1993 if (end > buf + pos) {
1994 mask_name = "numeral";
1995 len = end - (buf + pos);
1999 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2000 len = strlen(mask_flags[i].mask_name);
2001 if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
2002 mask_name = mask_flags[i].mask_name;
2003 mask_bitfield = mask_flags[i].mask_bitfield;
2010 if (mask_name != NULL) {
2015 rg &= ~mask_bitfield;
2018 rg |= mask_bitfield;
2024 rg |= mask_bitfield;
2030 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2032 if (rg & YAFFS_TRACE_ALWAYS) {
2033 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2035 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2036 printk("%c%s\n", flag, mask_flags[i].mask_name);
2043 /* Stuff to handle installation of file systems */
2044 struct file_system_to_install {
2045 struct file_system_type *fst;
2049 static struct file_system_to_install fs_to_install[] = {
2050 //#ifdef CONFIG_YAFFS_YAFFS1
2051 {&yaffs_fs_type, 0},
2053 //#ifdef CONFIG_YAFFS_YAFFS2
2054 {&yaffs2_fs_type, 0},
2059 static int __init init_yaffs_fs(void)
2062 struct file_system_to_install *fsinst;
2064 T(YAFFS_TRACE_ALWAYS,
2065 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2067 /* Install the proc_fs entry */
2068 my_proc_entry = create_proc_entry("yaffs",
2072 if (my_proc_entry) {
2073 my_proc_entry->write_proc = yaffs_proc_write;
2074 my_proc_entry->read_proc = yaffs_proc_read;
2075 my_proc_entry->data = NULL;
2080 /* Now add the file system entries */
2082 fsinst = fs_to_install;
2084 while (fsinst->fst && !error) {
2085 error = register_filesystem(fsinst->fst);
2087 fsinst->installed = 1;
2092 /* Any errors? uninstall */
2094 fsinst = fs_to_install;
2096 while (fsinst->fst) {
2097 if (fsinst->installed) {
2098 unregister_filesystem(fsinst->fst);
2099 fsinst->installed = 0;
2108 static void __exit exit_yaffs_fs(void)
2111 struct file_system_to_install *fsinst;
2113 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2116 remove_proc_entry("yaffs", &proc_root);
2118 fsinst = fs_to_install;
2120 while (fsinst->fst) {
2121 if (fsinst->installed) {
2122 unregister_filesystem(fsinst->fst);
2123 fsinst->installed = 0;
2130 module_init(init_yaffs_fs)
2131 module_exit(exit_yaffs_fs)
2133 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2134 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2135 MODULE_LICENSE("GPL");