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.58 2007-02-14 01:09:06 wookey 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 #include <asm/uaccess.h>
82 #include "yaffs_guts.h"
84 unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS |
85 YAFFS_TRACE_BAD_BLOCKS |
86 YAFFS_TRACE_CHECKPOINT
89 #include <linux/mtd/mtd.h>
90 #include "yaffs_mtdif.h"
91 #include "yaffs_mtdif2.h"
93 /*#define T(x) printk x */
95 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
96 #define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
98 #define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
101 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
102 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
104 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
105 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
107 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
110 static void yaffs_put_super(struct super_block *sb);
112 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
115 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
116 static int yaffs_file_flush(struct file *file, fl_owner_t id);
118 static int yaffs_file_flush(struct file *file);
121 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
124 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
126 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
127 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
128 struct nameidata *n);
129 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
130 struct nameidata *n);
132 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
133 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
135 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
136 struct dentry *dentry);
137 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
138 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
139 const char *symname);
140 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
142 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
143 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
146 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
149 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
150 struct inode *new_dir, struct dentry *new_dentry);
151 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
153 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
154 static int yaffs_sync_fs(struct super_block *sb, int wait);
155 static void yaffs_write_super(struct super_block *sb);
157 static int yaffs_sync_fs(struct super_block *sb);
158 static int yaffs_write_super(struct super_block *sb);
161 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
162 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
163 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
164 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
166 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
168 static void yaffs_read_inode(struct inode *inode);
170 static void yaffs_put_inode(struct inode *inode);
171 static void yaffs_delete_inode(struct inode *);
172 static void yaffs_clear_inode(struct inode *);
174 static int yaffs_readpage(struct file *file, struct page *page);
175 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
176 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
178 static int yaffs_writepage(struct page *page);
180 static int yaffs_prepare_write(struct file *f, struct page *pg,
181 unsigned offset, unsigned to);
182 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
185 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
187 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
188 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
190 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
193 static struct address_space_operations yaffs_file_address_operations = {
194 .readpage = yaffs_readpage,
195 .writepage = yaffs_writepage,
196 .prepare_write = yaffs_prepare_write,
197 .commit_write = yaffs_commit_write,
200 static struct file_operations yaffs_file_operations = {
201 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
202 .read = do_sync_read,
203 .write = do_sync_write,
204 .aio_read = generic_file_aio_read,
205 .aio_write = generic_file_aio_write,
207 .read = generic_file_read,
208 .write = generic_file_write,
210 .mmap = generic_file_mmap,
211 .flush = yaffs_file_flush,
212 .fsync = yaffs_sync_object,
213 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
214 .sendfile = generic_file_sendfile,
219 static struct inode_operations yaffs_file_inode_operations = {
220 .setattr = yaffs_setattr,
223 static struct inode_operations yaffs_symlink_inode_operations = {
224 .readlink = yaffs_readlink,
225 .follow_link = yaffs_follow_link,
226 .setattr = yaffs_setattr,
229 static struct inode_operations yaffs_dir_inode_operations = {
230 .create = yaffs_create,
231 .lookup = yaffs_lookup,
233 .unlink = yaffs_unlink,
234 .symlink = yaffs_symlink,
235 .mkdir = yaffs_mkdir,
236 .rmdir = yaffs_unlink,
237 .mknod = yaffs_mknod,
238 .rename = yaffs_rename,
239 .setattr = yaffs_setattr,
242 static struct file_operations yaffs_dir_operations = {
243 .read = generic_read_dir,
244 .readdir = yaffs_readdir,
245 .fsync = yaffs_sync_object,
248 static struct super_operations yaffs_super_ops = {
249 .statfs = yaffs_statfs,
250 .read_inode = yaffs_read_inode,
251 .put_inode = yaffs_put_inode,
252 .put_super = yaffs_put_super,
253 .delete_inode = yaffs_delete_inode,
254 .clear_inode = yaffs_clear_inode,
255 .sync_fs = yaffs_sync_fs,
256 .write_super = yaffs_write_super,
259 static void yaffs_GrossLock(yaffs_Device * dev)
261 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
263 down(&dev->grossLock);
266 static void yaffs_GrossUnlock(yaffs_Device * dev)
268 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
273 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
276 unsigned char *alias;
279 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
281 yaffs_GrossLock(dev);
283 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
285 yaffs_GrossUnlock(dev);
290 ret = vfs_readlink(dentry, buffer, buflen, alias);
295 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
296 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
298 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
301 unsigned char *alias;
303 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
305 yaffs_GrossLock(dev);
307 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
309 yaffs_GrossUnlock(dev);
317 ret = vfs_follow_link(nd, alias);
320 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
321 return ERR_PTR (ret);
327 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
331 * Lookup is used to find objects in the fs
333 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
335 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
338 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
342 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
344 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
346 yaffs_GrossLock(dev);
349 (KERN_DEBUG "yaffs_lookup for %d:%s\n",
350 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
353 yaffs_FindObjectByName(yaffs_InodeToObject(dir),
354 dentry->d_name.name);
356 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
358 /* Can't hold gross lock when calling yaffs_get_inode() */
359 yaffs_GrossUnlock(dev);
363 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
365 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
369 (KERN_DEBUG "yaffs_loookup dentry \n"));
370 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
371 * d_add even if NULL inode */
373 /*dget(dentry); // try to solve directory bug */
374 d_add(dentry, inode);
382 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
386 /* added NCB for 2.5/6 compatability - forces add even if inode is
387 * NULL which creates dentry hash */
388 d_add(dentry, inode);
391 /* return (ERR_PTR(-EIO)); */
395 /* For now put inode is just for debugging
396 * Put inode is called when the inode **structure** is put.
398 static void yaffs_put_inode(struct inode *inode)
401 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
402 atomic_read(&inode->i_count)));
406 /* clear is called to tell the fs to release any per-inode data it holds */
407 static void yaffs_clear_inode(struct inode *inode)
412 obj = yaffs_InodeToObject(inode);
415 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
416 atomic_read(&inode->i_count),
417 obj ? "object exists" : "null object"));
421 yaffs_GrossLock(dev);
423 /* Clear the association between the inode and
427 yaffs_InodeToObjectLV(inode) = NULL;
429 /* If the object freeing was deferred, then the real
431 * This should fix the inode inconsistency problem.
434 yaffs_HandleDeferedFree(obj);
436 yaffs_GrossUnlock(dev);
441 /* delete is called when the link count is zero and the inode
442 * is put (ie. nobody wants to know about it anymore, time to
444 * NB Must call clear_inode()
446 static void yaffs_delete_inode(struct inode *inode)
448 yaffs_Object *obj = yaffs_InodeToObject(inode);
452 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
453 atomic_read(&inode->i_count),
454 obj ? "object exists" : "null object"));
458 yaffs_GrossLock(dev);
459 yaffs_DeleteFile(obj);
460 yaffs_GrossUnlock(dev);
462 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
463 truncate_inode_pages (&inode->i_data, 0);
468 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
469 static int yaffs_file_flush(struct file *file, fl_owner_t id)
471 static int yaffs_file_flush(struct file *file)
474 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
476 yaffs_Device *dev = obj->myDev;
479 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
480 obj->dirty ? "dirty" : "clean"));
482 yaffs_GrossLock(dev);
484 yaffs_FlushFile(obj, 1);
486 yaffs_GrossUnlock(dev);
491 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
493 /* Lifted from jffs2 */
496 unsigned char *pg_buf;
501 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
502 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
503 (unsigned)PAGE_CACHE_SIZE));
505 obj = yaffs_DentryToObject(f->f_dentry);
509 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
510 BUG_ON(!PageLocked(pg));
517 /* FIXME: Can kmap fail? */
519 yaffs_GrossLock(dev);
522 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
525 yaffs_GrossUnlock(dev);
531 ClearPageUptodate(pg);
538 flush_dcache_page(pg);
541 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
545 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
547 int ret = yaffs_readpage_nolock(f, pg);
552 static int yaffs_readpage(struct file *f, struct page *pg)
554 return yaffs_readpage_unlock(f, pg);
557 /* writepage inspired by/stolen from smbfs */
559 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
560 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
562 static int yaffs_writepage(struct page *page)
565 struct address_space *mapping = page->mapping;
566 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
568 unsigned long end_index;
576 inode = mapping->host;
580 if (offset > inode->i_size) {
583 "yaffs_writepage at %08x, inode size = %08x!!!\n",
584 (unsigned)(page->index << PAGE_CACHE_SHIFT),
585 (unsigned)inode->i_size));
587 (KERN_DEBUG " -> don't care!!\n"));
592 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
595 if (page->index < end_index) {
596 nBytes = PAGE_CACHE_SIZE;
598 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
605 obj = yaffs_InodeToObject(inode);
606 yaffs_GrossLock(obj->myDev);
609 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
610 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
612 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
613 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
616 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
620 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
621 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
623 yaffs_GrossUnlock(obj->myDev);
626 SetPageUptodate(page);
630 return (nWritten == nBytes) ? 0 : -ENOSPC;
633 static int yaffs_prepare_write(struct file *f, struct page *pg,
634 unsigned offset, unsigned to)
637 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
638 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
639 return yaffs_readpage_nolock(f, pg);
645 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
649 void *addr = page_address(pg) + offset;
650 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
651 int nBytes = to - offset;
655 unsigned saddr = (unsigned)addr;
658 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
661 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
663 if (nWritten != nBytes) {
666 "yaffs_commit_write not same size nWritten %d nBytes %d\n",
669 ClearPageUptodate(pg);
675 (KERN_DEBUG "yaffs_commit_write returning %d\n",
676 nWritten == nBytes ? 0 : nWritten));
678 return nWritten == nBytes ? 0 : nWritten;
682 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
687 /* Check mode against the variant type and attempt to repair if broken. */
688 __u32 mode = obj->yst_mode;
689 switch( obj->variantType ){
690 case YAFFS_OBJECT_TYPE_FILE :
691 if( ! S_ISREG(mode) ){
692 obj->yst_mode &= ~S_IFMT;
693 obj->yst_mode |= S_IFREG;
697 case YAFFS_OBJECT_TYPE_SYMLINK :
698 if( ! S_ISLNK(mode) ){
699 obj->yst_mode &= ~S_IFMT;
700 obj->yst_mode |= S_IFLNK;
704 case YAFFS_OBJECT_TYPE_DIRECTORY :
705 if( ! S_ISDIR(mode) ){
706 obj->yst_mode &= ~S_IFMT;
707 obj->yst_mode |= S_IFDIR;
711 case YAFFS_OBJECT_TYPE_UNKNOWN :
712 case YAFFS_OBJECT_TYPE_HARDLINK :
713 case YAFFS_OBJECT_TYPE_SPECIAL :
719 inode->i_ino = obj->objectId;
720 inode->i_mode = obj->yst_mode;
721 inode->i_uid = obj->yst_uid;
722 inode->i_gid = obj->yst_gid;
723 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
724 inode->i_blksize = inode->i_sb->s_blocksize;
726 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
728 inode->i_rdev = old_decode_dev(obj->yst_rdev);
729 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
730 inode->i_atime.tv_nsec = 0;
731 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
732 inode->i_mtime.tv_nsec = 0;
733 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
734 inode->i_ctime.tv_nsec = 0;
736 inode->i_rdev = obj->yst_rdev;
737 inode->i_atime = obj->yst_atime;
738 inode->i_mtime = obj->yst_mtime;
739 inode->i_ctime = obj->yst_ctime;
741 inode->i_size = yaffs_GetObjectFileLength(obj);
742 inode->i_blocks = (inode->i_size + 511) >> 9;
744 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
748 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
749 inode->i_mode, inode->i_uid, inode->i_gid,
750 (int)inode->i_size, atomic_read(&inode->i_count)));
752 switch (obj->yst_mode & S_IFMT) {
753 default: /* fifo, device or socket */
754 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
755 init_special_inode(inode, obj->yst_mode,
756 old_decode_dev(obj->yst_rdev));
758 init_special_inode(inode, obj->yst_mode,
759 (dev_t) (obj->yst_rdev));
762 case S_IFREG: /* file */
763 inode->i_op = &yaffs_file_inode_operations;
764 inode->i_fop = &yaffs_file_operations;
765 inode->i_mapping->a_ops =
766 &yaffs_file_address_operations;
768 case S_IFDIR: /* directory */
769 inode->i_op = &yaffs_dir_inode_operations;
770 inode->i_fop = &yaffs_dir_operations;
772 case S_IFLNK: /* symlink */
773 inode->i_op = &yaffs_symlink_inode_operations;
777 yaffs_InodeToObjectLV(inode) = obj;
779 obj->myInode = inode;
783 (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
788 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
795 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
802 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
808 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
810 inode = iget(sb, obj->objectId);
812 /* NB Side effect: iget calls back to yaffs_read_inode(). */
813 /* iget also increments the inode's i_count */
814 /* NB You can't be holding grossLock or deadlock will happen! */
819 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
827 obj = yaffs_DentryToObject(f->f_dentry);
831 yaffs_GrossLock(dev);
833 inode = f->f_dentry->d_inode;
835 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
836 ipos = inode->i_size;
843 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
847 "yaffs_file_write about to write writing %d bytes"
848 "to object %d at %d\n",
849 n, obj->objectId, ipos));
852 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
855 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
860 if (ipos > inode->i_size) {
861 inode->i_size = ipos;
862 inode->i_blocks = (ipos + 511) >> 9;
866 "yaffs_file_write size updated to %d bytes, "
868 ipos, (int)(inode->i_blocks)));
872 yaffs_GrossUnlock(dev);
873 return nWritten == 0 ? -ENOSPC : nWritten;
876 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
880 struct inode *inode = f->f_dentry->d_inode;
881 unsigned long offset, curoffs;
885 char name[YAFFS_MAX_NAME_LENGTH + 1];
887 obj = yaffs_DentryToObject(f->f_dentry);
890 yaffs_GrossLock(dev);
894 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
898 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
900 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
909 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
910 (int)f->f_dentry->d_parent->d_inode->i_ino));
912 (dirent, "..", 2, offset,
913 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
922 /* If the directory has changed since the open or last call to
923 readdir, rewind to after the 2 canned entries. */
925 if (f->f_version != inode->i_version) {
928 f->f_version = inode->i_version;
931 list_for_each(i, &obj->variant.directoryVariant.children) {
933 if (curoffs >= offset) {
934 l = list_entry(i, yaffs_Object, siblings);
936 yaffs_GetObjectName(l, name,
937 YAFFS_MAX_NAME_LENGTH + 1);
939 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
940 yaffs_GetObjectInode(l)));
946 yaffs_GetObjectInode(l),
947 yaffs_GetObjectType(l))
960 yaffs_GrossUnlock(dev);
966 * File creation. Allocate an inode, and we're done..
968 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
969 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
972 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
978 yaffs_Object *obj = NULL;
981 yaffs_Object *parent = yaffs_InodeToObject(dir);
984 uid_t uid = current->fsuid;
985 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
987 if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
992 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
993 parent->objectId, parent->variantType));
996 (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
1000 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
1002 dentry->d_name.name, mode, rdev));
1004 dev = parent->myDev;
1006 yaffs_GrossLock(dev);
1008 switch (mode & S_IFMT) {
1010 /* Special (socket, fifo, device...) */
1011 T(YAFFS_TRACE_OS, (KERN_DEBUG
1012 "yaffs_mknod: making special\n"));
1013 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1015 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1016 gid, old_encode_dev(rdev));
1019 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1023 case S_IFREG: /* file */
1024 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1026 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1029 case S_IFDIR: /* directory */
1031 (KERN_DEBUG "yaffs_mknod: making directory\n"));
1033 yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1036 case S_IFLNK: /* symlink */
1037 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1038 obj = NULL; /* Do we ever get here? */
1042 /* Can not call yaffs_get_inode() with gross lock held */
1043 yaffs_GrossUnlock(dev);
1046 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1047 d_instantiate(dentry, inode);
1049 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
1050 obj->objectId, atomic_read(&inode->i_count)));
1054 (KERN_DEBUG "yaffs_mknod failed making object\n"));
1061 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1064 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
1065 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1067 /* attempt to fix dir bug - didn't work */
1075 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1076 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1077 struct nameidata *n)
1079 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1082 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1083 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1086 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1093 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1094 dentry->d_name.name));
1096 dev = yaffs_InodeToObject(dir)->myDev;
1098 yaffs_GrossLock(dev);
1100 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1102 if (retVal == YAFFS_OK) {
1103 dentry->d_inode->i_nlink--;
1105 yaffs_GrossUnlock(dev);
1106 mark_inode_dirty(dentry->d_inode);
1109 yaffs_GrossUnlock(dev);
1116 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1117 struct dentry *dentry)
1119 struct inode *inode = old_dentry->d_inode;
1120 yaffs_Object *obj = NULL;
1121 yaffs_Object *link = NULL;
1124 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1126 obj = yaffs_InodeToObject(inode);
1129 yaffs_GrossLock(dev);
1131 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1134 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1139 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1140 d_instantiate(dentry, old_dentry->d_inode);
1141 atomic_inc(&old_dentry->d_inode->i_count);
1143 (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1144 old_dentry->d_inode->i_nlink,
1145 atomic_read(&old_dentry->d_inode->i_count)));
1149 yaffs_GrossUnlock(dev);
1159 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1160 const char *symname)
1164 uid_t uid = current->fsuid;
1165 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1167 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1169 dev = yaffs_InodeToObject(dir)->myDev;
1170 yaffs_GrossLock(dev);
1171 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1172 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1173 yaffs_GrossUnlock(dev);
1177 struct inode *inode;
1179 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1180 d_instantiate(dentry, inode);
1181 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1184 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1191 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1198 obj = yaffs_DentryToObject(dentry);
1202 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1203 yaffs_GrossLock(dev);
1204 yaffs_FlushFile(obj, 1);
1205 yaffs_GrossUnlock(dev);
1210 * The VFS layer already does all the dentry stuff for rename.
1212 * NB: POSIX says you can rename an object over an old object of the same name
1214 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1215 struct inode *new_dir, struct dentry *new_dentry)
1218 int retVal = YAFFS_FAIL;
1219 yaffs_Object *target;
1221 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1222 dev = yaffs_InodeToObject(old_dir)->myDev;
1224 yaffs_GrossLock(dev);
1226 /* Check if the target is an existing directory that is not empty. */
1228 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1229 new_dentry->d_name.name);
1234 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1235 !list_empty(&target->variant.directoryVariant.children)) {
1237 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1239 retVal = YAFFS_FAIL;
1242 /* Now does unlinking internally using shadowing mechanism */
1243 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1246 yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1247 old_dentry->d_name.name,
1248 yaffs_InodeToObject(new_dir),
1249 new_dentry->d_name.name);
1252 yaffs_GrossUnlock(dev);
1254 if (retVal == YAFFS_OK) {
1256 new_dentry->d_inode->i_nlink--;
1257 mark_inode_dirty(new_dentry->d_inode);
1267 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1269 struct inode *inode = dentry->d_inode;
1274 (KERN_DEBUG "yaffs_setattr of object %d\n",
1275 yaffs_InodeToObject(inode)->objectId));
1277 if ((error = inode_change_ok(inode, attr)) == 0) {
1279 dev = yaffs_InodeToObject(inode)->myDev;
1280 yaffs_GrossLock(dev);
1281 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1287 yaffs_GrossUnlock(dev);
1289 error = inode_setattr(inode, attr);
1294 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1295 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1297 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1298 struct super_block *sb = dentry->d_sb;
1299 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1300 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1302 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1304 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1306 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1309 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1311 yaffs_GrossLock(dev);
1313 buf->f_type = YAFFS_MAGIC;
1314 buf->f_bsize = sb->s_blocksize;
1315 buf->f_namelen = 255;
1316 if (sb->s_blocksize > dev->nDataBytesPerChunk) {
1319 (dev->endBlock - dev->startBlock +
1320 1) * dev->nChunksPerBlock / (sb->s_blocksize /
1321 dev->nDataBytesPerChunk);
1323 yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
1324 dev->nDataBytesPerChunk);
1328 (dev->endBlock - dev->startBlock +
1329 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
1332 yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
1337 buf->f_bavail = buf->f_bfree;
1339 yaffs_GrossUnlock(dev);
1345 static int yaffs_do_sync_fs(struct super_block *sb)
1348 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1349 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
1352 yaffs_GrossLock(dev);
1355 yaffs_CheckpointSave(dev);
1357 yaffs_GrossUnlock(dev);
1365 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1366 static void yaffs_write_super(struct super_block *sb)
1368 static int yaffs_write_super(struct super_block *sb)
1372 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
1373 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
1374 return 0; /* yaffs_do_sync_fs(sb);*/
1379 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1380 static int yaffs_sync_fs(struct super_block *sb, int wait)
1382 static int yaffs_sync_fs(struct super_block *sb)
1386 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
1388 return 0; /* yaffs_do_sync_fs(sb);*/
1393 static void yaffs_read_inode(struct inode *inode)
1395 /* NB This is called as a side effect of other functions, but
1396 * we had to release the lock to prevent deadlocks, so
1397 * need to lock again.
1401 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1404 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1406 yaffs_GrossLock(dev);
1408 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1410 yaffs_FillInodeFromObject(inode, obj);
1412 yaffs_GrossUnlock(dev);
1415 static LIST_HEAD(yaffs_dev_list);
1417 static void yaffs_put_super(struct super_block *sb)
1419 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1421 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
1423 yaffs_GrossLock(dev);
1425 yaffs_FlushEntireDeviceCache(dev);
1427 if (dev->putSuperFunc) {
1428 dev->putSuperFunc(sb);
1431 yaffs_CheckpointSave(dev);
1432 yaffs_Deinitialise(dev);
1434 yaffs_GrossUnlock(dev);
1436 /* we assume this is protected by lock_kernel() in mount/umount */
1437 list_del(&dev->devList);
1439 if(dev->spareBuffer){
1440 YFREE(dev->spareBuffer);
1441 dev->spareBuffer = NULL;
1448 static void yaffs_MTDPutSuper(struct super_block *sb)
1451 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1457 put_mtd_device(mtd);
1461 static void yaffs_MarkSuperBlockDirty(void *vsb)
1463 struct super_block *sb = (struct super_block *)vsb;
1465 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
1470 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1471 struct super_block *sb,
1472 void *data, int silent)
1475 struct inode *inode = NULL;
1476 struct dentry *root;
1477 yaffs_Device *dev = 0;
1478 char devname_buf[BDEVNAME_SIZE + 1];
1479 struct mtd_info *mtd;
1482 sb->s_magic = YAFFS_MAGIC;
1483 sb->s_op = &yaffs_super_ops;
1486 printk(KERN_INFO "yaffs: sb is NULL\n");
1487 else if (!sb->s_dev)
1488 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1489 else if (!yaffs_devname(sb, devname_buf))
1490 printk(KERN_INFO "yaffs: devname is NULL\n");
1492 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1494 yaffs_devname(sb, devname_buf));
1496 sb->s_blocksize = PAGE_CACHE_SIZE;
1497 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1498 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1500 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1502 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1504 ("yaffs: Write verification disabled. All guarantees "
1505 "null and void\n"));
1508 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1510 MAJOR(sb->s_dev), MINOR(sb->s_dev),
1511 yaffs_devname(sb, devname_buf)));
1513 /* Check it's an mtd device..... */
1514 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1515 return NULL; /* This isn't an mtd device */
1517 /* Get the device */
1518 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1520 T(YAFFS_TRACE_ALWAYS,
1521 ("yaffs: MTD device #%u doesn't appear to exist\n",
1525 /* Check it's NAND */
1526 if (mtd->type != MTD_NANDFLASH) {
1527 T(YAFFS_TRACE_ALWAYS,
1528 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1532 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1533 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1534 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1535 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1536 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1537 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1538 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1539 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1540 T(YAFFS_TRACE_OS, (" writesize %d\n", mtd->writesize));
1542 T(YAFFS_TRACE_OS, (" oobblock %d\n", mtd->oobblock));
1544 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1545 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1546 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1548 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1550 if (yaffsVersion == 1 &&
1551 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1552 mtd->writesize >= 2048) {
1554 mtd->oobblock >= 2048) {
1556 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1560 /* Added NCB 26/5/2006 for completeness */
1561 if (yaffsVersion == 2 &&
1562 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1563 mtd->writesize == 512) {
1565 mtd->oobblock == 512) {
1567 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
1573 if (yaffsVersion == 2) {
1574 /* Check for version 2 style functions */
1576 !mtd->block_isbad ||
1577 !mtd->block_markbad ||
1580 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1581 !mtd->read_oob || !mtd->write_oob) {
1584 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1586 T(YAFFS_TRACE_ALWAYS,
1587 ("yaffs: MTD device does not support required "
1592 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1593 if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1595 if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1597 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
1598 T(YAFFS_TRACE_ALWAYS,
1599 ("yaffs: MTD device does not have the "
1600 "right page sizes\n"));
1604 /* Check for V1 style functions */
1608 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1609 !mtd->read_oob || !mtd->write_oob) {
1612 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1614 T(YAFFS_TRACE_ALWAYS,
1615 ("yaffs: MTD device does not support required "
1620 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1621 if (mtd->writesize < YAFFS_BYTES_PER_CHUNK ||
1623 if (mtd->oobblock < YAFFS_BYTES_PER_CHUNK ||
1625 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1626 T(YAFFS_TRACE_ALWAYS,
1627 ("yaffs: MTD device does not support have the "
1628 "right page sizes\n"));
1633 /* OK, so if we got here, we have an MTD that's NAND and looks
1634 * like it has the right capabilities
1635 * Set the yaffs_Device up for mtd
1638 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1639 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1641 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1644 /* Deep shit could not allocate device structure */
1645 T(YAFFS_TRACE_ALWAYS,
1646 ("yaffs_read_super: Failed trying to allocate "
1647 "yaffs_Device. \n"));
1651 memset(dev, 0, sizeof(yaffs_Device));
1652 dev->genericDevice = mtd;
1653 dev->name = mtd->name;
1655 /* Set up the memory size parameters.... */
1657 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1658 dev->startBlock = 0;
1659 dev->endBlock = nBlocks - 1;
1660 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1661 dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1662 dev->nReservedBlocks = 5;
1663 dev->nShortOpCaches = 10; /* Enable short op caching */
1665 /* ... and the functions. */
1666 if (yaffsVersion == 2) {
1667 dev->writeChunkWithTagsToNAND =
1668 nandmtd2_WriteChunkWithTagsToNAND;
1669 dev->readChunkWithTagsFromNAND =
1670 nandmtd2_ReadChunkWithTagsFromNAND;
1671 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1672 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1673 dev->spareBuffer = YMALLOC(mtd->oobsize);
1675 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1676 dev->nDataBytesPerChunk = mtd->writesize;
1677 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
1679 dev->nDataBytesPerChunk = mtd->oobblock;
1680 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1682 nBlocks = mtd->size / mtd->erasesize;
1684 dev->nCheckpointReservedBlocks = CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS;
1685 dev->startBlock = 0;
1686 dev->endBlock = nBlocks - 1;
1688 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1689 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1692 /* ... and common functions */
1693 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1694 dev->initialiseNAND = nandmtd_InitialiseNAND;
1696 dev->putSuperFunc = yaffs_MTDPutSuper;
1698 dev->superBlock = (void *)sb;
1699 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
1702 #ifndef CONFIG_YAFFS_DOES_ECC
1703 dev->useNANDECC = 1;
1706 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1707 dev->wideTnodesDisabled = 1;
1710 /* we assume this is protected by lock_kernel() in mount/umount */
1711 list_add_tail(&dev->devList, &yaffs_dev_list);
1713 init_MUTEX(&dev->grossLock);
1715 yaffs_GrossLock(dev);
1717 err = yaffs_GutsInitialise(dev);
1720 ("yaffs_read_super: guts initialised %s\n",
1721 (err == YAFFS_OK) ? "OK" : "FAILED"));
1723 /* Release lock before yaffs_get_inode() */
1724 yaffs_GrossUnlock(dev);
1726 /* Create root inode */
1727 if (err == YAFFS_OK)
1728 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1734 inode->i_op = &yaffs_dir_inode_operations;
1735 inode->i_fop = &yaffs_dir_operations;
1737 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1739 root = d_alloc_root(inode);
1741 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1749 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1754 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1755 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
1758 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
1761 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1762 static int yaffs_read_super(struct file_system_type *fs,
1763 int flags, const char *dev_name,
1764 void *data, struct vfsmount *mnt)
1767 return get_sb_bdev(fs, flags, dev_name, data,
1768 yaffs_internal_read_super_mtd, mnt);
1771 static struct super_block *yaffs_read_super(struct file_system_type *fs,
1772 int flags, const char *dev_name,
1776 return get_sb_bdev(fs, flags, dev_name, data,
1777 yaffs_internal_read_super_mtd);
1781 static struct file_system_type yaffs_fs_type = {
1782 .owner = THIS_MODULE,
1784 .get_sb = yaffs_read_super,
1785 .kill_sb = kill_block_super,
1786 .fs_flags = FS_REQUIRES_DEV,
1789 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
1792 return yaffs_internal_read_super(1, sb, data, silent);
1795 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
1800 #ifdef CONFIG_YAFFS_YAFFS2
1802 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1803 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
1806 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
1809 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1810 static int yaffs2_read_super(struct file_system_type *fs,
1811 int flags, const char *dev_name, void *data,
1812 struct vfsmount *mnt)
1814 return get_sb_bdev(fs, flags, dev_name, data,
1815 yaffs2_internal_read_super_mtd, mnt);
1818 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
1819 int flags, const char *dev_name,
1823 return get_sb_bdev(fs, flags, dev_name, data,
1824 yaffs2_internal_read_super_mtd);
1828 static struct file_system_type yaffs2_fs_type = {
1829 .owner = THIS_MODULE,
1831 .get_sb = yaffs2_read_super,
1832 .kill_sb = kill_block_super,
1833 .fs_flags = FS_REQUIRES_DEV,
1836 static struct super_block *yaffs2_read_super(struct super_block *sb,
1837 void *data, int silent)
1839 return yaffs_internal_read_super(2, sb, data, silent);
1842 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
1846 #endif /* CONFIG_YAFFS_YAFFS2 */
1848 static struct proc_dir_entry *my_proc_entry;
1850 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
1852 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
1853 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
1854 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
1855 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
1856 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
1857 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
1858 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
1859 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
1860 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
1861 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
1862 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
1863 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
1864 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
1865 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
1867 sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
1869 sprintf(buf, "passiveGCs......... %d\n",
1870 dev->passiveGarbageCollections);
1871 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
1872 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
1873 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
1874 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
1875 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
1876 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
1877 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
1878 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
1879 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
1881 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
1882 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
1883 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
1888 static int yaffs_proc_read(char *page,
1890 off_t offset, int count, int *eof, void *data)
1892 struct list_head *item;
1897 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
1898 * We use 'offset' (*ppos) to indicate where we are in devList.
1899 * This also assumes the user has posted a read buffer large
1900 * enough to hold the complete output; but that's life in /proc.
1905 /* Print header first */
1907 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
1908 "\n%s\n%s\n", yaffs_fs_c_version,
1909 yaffs_guts_c_version);
1912 /* hold lock_kernel while traversing yaffs_dev_list */
1915 /* Locate and print the Nth entry. Order N-squared but N is small. */
1916 list_for_each(item, &yaffs_dev_list) {
1917 yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
1922 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
1923 buf = yaffs_dump_dev(buf, dev);
1928 return buf - page < count ? buf - page : count;
1932 * Set the verbosity of the warnings and error messages.
1938 unsigned mask_bitfield;
1940 {"allocate", YAFFS_TRACE_ALLOCATE},
1941 {"always", YAFFS_TRACE_ALWAYS},
1942 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
1943 {"buffers", YAFFS_TRACE_BUFFERS},
1944 {"bug", YAFFS_TRACE_BUG},
1945 {"deletion", YAFFS_TRACE_DELETION},
1946 {"erase", YAFFS_TRACE_ERASE},
1947 {"error", YAFFS_TRACE_ERROR},
1948 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
1949 {"gc", YAFFS_TRACE_GC},
1950 {"mtd", YAFFS_TRACE_MTD},
1951 {"nandaccess", YAFFS_TRACE_NANDACCESS},
1952 {"os", YAFFS_TRACE_OS},
1953 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
1954 {"scan", YAFFS_TRACE_SCAN},
1955 {"tracing", YAFFS_TRACE_TRACING},
1956 {"write", YAFFS_TRACE_WRITE},
1957 {"all", 0xffffffff},
1962 static int yaffs_proc_write(struct file *file, const char *buf,
1963 unsigned long count, void *data)
1965 unsigned rg = 0, mask_bitfield;
1966 char *end, *mask_name;
1972 rg = yaffs_traceMask;
1974 while (!done && (pos < count)) {
1976 while ((pos < count) && isspace(buf[pos])) {
1993 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
1994 if (end > buf + pos) {
1995 mask_name = "numeral";
1996 len = end - (buf + pos);
2000 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2001 len = strlen(mask_flags[i].mask_name);
2002 if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
2003 mask_name = mask_flags[i].mask_name;
2004 mask_bitfield = mask_flags[i].mask_bitfield;
2011 if (mask_name != NULL) {
2016 rg &= ~mask_bitfield;
2019 rg |= mask_bitfield;
2025 rg |= mask_bitfield;
2031 yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
2033 if (rg & YAFFS_TRACE_ALWAYS) {
2034 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2036 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2037 printk("%c%s\n", flag, mask_flags[i].mask_name);
2044 /* Stuff to handle installation of file systems */
2045 struct file_system_to_install {
2046 struct file_system_type *fst;
2050 static struct file_system_to_install fs_to_install[] = {
2051 //#ifdef CONFIG_YAFFS_YAFFS1
2052 {&yaffs_fs_type, 0},
2054 //#ifdef CONFIG_YAFFS_YAFFS2
2055 {&yaffs2_fs_type, 0},
2060 static int __init init_yaffs_fs(void)
2063 struct file_system_to_install *fsinst;
2065 T(YAFFS_TRACE_ALWAYS,
2066 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2068 /* Install the proc_fs entry */
2069 my_proc_entry = create_proc_entry("yaffs",
2073 if (my_proc_entry) {
2074 my_proc_entry->write_proc = yaffs_proc_write;
2075 my_proc_entry->read_proc = yaffs_proc_read;
2076 my_proc_entry->data = NULL;
2081 /* Now add the file system entries */
2083 fsinst = fs_to_install;
2085 while (fsinst->fst && !error) {
2086 error = register_filesystem(fsinst->fst);
2088 fsinst->installed = 1;
2093 /* Any errors? uninstall */
2095 fsinst = fs_to_install;
2097 while (fsinst->fst) {
2098 if (fsinst->installed) {
2099 unregister_filesystem(fsinst->fst);
2100 fsinst->installed = 0;
2109 static void __exit exit_yaffs_fs(void)
2112 struct file_system_to_install *fsinst;
2114 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2117 remove_proc_entry("yaffs", &proc_root);
2119 fsinst = fs_to_install;
2121 while (fsinst->fst) {
2122 if (fsinst->installed) {
2123 unregister_filesystem(fsinst->fst);
2124 fsinst->installed = 0;
2131 module_init(init_yaffs_fs)
2132 module_exit(exit_yaffs_fs)
2134 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2135 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2136 MODULE_LICENSE("GPL");