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 * >> sb->u.generic_sbp points to the yaffs_Device associated with this superblock
19 * >> inode->u.generic_ip points to the associated yaffs_Object.
23 #include <linux/config.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/version.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
31 #include <linux/proc_fs.h>
32 #include <linux/pagemap.h>
33 #include <linux/mtd/mtd.h>
34 #include <linux/interrupt.h>
35 #include <linux/string.h>
36 #include <linux/locks.h>
38 #include <asm/uaccess.h>
40 #include "yaffs_guts.h"
42 #ifdef YAFFS_RAM_ENABLED
43 #include "yaffs_nandemul.h"
44 // 2 MB of RAM for emulation
45 #define YAFFS_RAM_EMULATION_SIZE 0x200000
46 #endif //YAFFS_RAM_ENABLED
48 #ifdef YAFFS_MTD_ENABLED
49 #include <linux/mtd/mtd.h>
50 #include "yaffs_mtdif.h"
51 #endif //YAFFS_MTD_ENABLED
57 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
58 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
59 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
63 static void yaffs_put_super(struct super_block *sb);
65 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos);
66 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos);
68 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync);
70 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
72 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
73 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry);
74 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry);
75 static int yaffs_unlink(struct inode * dir, struct dentry *dentry);
76 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname);
77 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode);
78 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev);
79 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry);
80 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
82 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
83 static void yaffs_read_inode (struct inode *inode);
84 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);
85 static void yaffs_put_inode (struct inode *inode);
87 //static int yaffs_readpage(struct file*,struct page *
89 static struct address_space_operations yaffs_address_ops = {
90 // readpage: yaffs_readpage,
91 // prepare_write: yaffs_prepare_write,
92 // commit_write: yaffs_commit_write
96 static struct file_operations yaffs_file_operations = {
97 read: yaffs_file_read,
98 write: yaffs_file_write,
99 // mmap: generic_file_mmap,
100 fsync: yaffs_sync_object,
104 static struct inode_operations yaffs_file_inode_operations = {
105 setattr: yaffs_setattr,
110 static struct file_operations yaffs_dir_operations = {
111 read: generic_read_dir,
112 readdir: yaffs_readdir,
113 fsync: yaffs_sync_object,
116 static struct inode_operations yaffs_dir_inode_operations = {
117 create: yaffs_create,
118 lookup: yaffs_lookup,
120 unlink: yaffs_unlink,
121 symlink: yaffs_symlink,
125 rename: yaffs_rename,
126 setattr: yaffs_setattr,
129 static struct super_operations yaffs_super_ops = {
130 statfs: yaffs_statfs,
131 read_inode: yaffs_read_inode,
132 put_inode: yaffs_put_inode,
133 put_super: yaffs_put_super,
140 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj);
143 * Lookup is used to find objects in the fs
145 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
151 T((KERN_DEBUG"yaffs_lookup for %d:%s\n",yaffs_InodeToObject(dir)->objectId,dentry->d_name.name));
153 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),dentry->d_name.name);
157 T((KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
159 inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj);
163 T((KERN_DEBUG"yaffs_loookup looks good\n"));
164 dget(dentry); // try to solve directory bug
172 T((KERN_DEBUG"yaffs_lookup not found\n"));
179 // For now put inode is just for debugging
180 static void yaffs_put_inode(struct inode *inode)
182 T(("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count)));
184 yaffs_FlushFile(yaffs_InodeToObject(inode));
188 #ifdef YAFFS_ADDRESS_OPS
189 static int yaffs_readpage(struct file *file, struct page * page)
191 T((KERN_DEBUG"yaffs_readpage\n"));
197 static int yaffs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
199 T((KERN_DEBUG"yaffs_prepare_write\n"));
205 static int yaffs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
208 struct inode *inode = page->mapping->host;
209 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
211 T((KERN_DEBUG"yaffs_commit_write\n"));
216 #endif //YAFFS_ADDRESS_OPS
219 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
223 inode->i_ino = obj->objectId;
224 inode->i_mode = obj->st_mode;
225 inode->i_uid = obj->st_uid;
226 inode->i_gid = obj->st_gid;
227 inode->i_blksize = YAFFS_BYTES_PER_CHUNK;
229 inode->i_rdev = NODEV;
230 inode->i_mapping->a_ops = &yaffs_address_ops;
231 inode->i_atime = obj->st_atime;
232 inode->i_mtime = obj->st_mtime;
233 inode->i_ctime = obj->st_ctime;
234 inode->i_size = yaffs_GetObjectFileLength(obj);
235 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
237 T((KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
238 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
240 switch (obj->st_mode & S_IFMT)
243 // init_special_inode(inode, mode, dev);
245 case S_IFREG: // file
246 inode->i_op = &yaffs_file_inode_operations;
247 inode->i_fop = &yaffs_file_operations;
249 case S_IFDIR: // directory
250 inode->i_op = &yaffs_dir_inode_operations;
251 inode->i_fop = &yaffs_dir_operations;
253 case S_IFLNK: // symlink
254 inode->i_op = &page_symlink_inode_operations;
259 inode->u.generic_ip = obj;
264 T((KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
269 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
271 struct inode * inode;
273 T((KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
275 inode = iget(sb,obj->objectId);
277 // No need to call this since iget calls it via the read_inode callback
278 // yaffs_FillInodeFromObject(inode,obj);
283 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
289 T((KERN_DEBUG"yaffs_file_read\n"));
291 obj = yaffs_DentryToObject(f->f_dentry);
292 inode = f->f_dentry->d_inode;
294 if (*pos < inode->i_size)
296 if (*pos + n > inode->i_size)
298 n = inode->i_size - *pos;
306 nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
312 T((KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
317 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
323 obj = yaffs_DentryToObject(f->f_dentry);
324 inode = f->f_dentry->d_inode;
325 nWritten = yaffs_WriteDataToFile(obj,buf,*pos,n);
327 T((KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
332 if(*pos > inode->i_size)
334 inode->i_size = *pos;
335 T((KERN_DEBUG"yaffs_file_write size updated to %d\n",ipos));
343 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
346 struct inode *inode = f->f_dentry->d_inode;
347 unsigned long offset, curoffs;
351 char name[YAFFS_MAX_NAME_LENGTH +1];
353 obj = yaffs_DentryToObject(f->f_dentry);
357 T(("yaffs_readdir: starting at %d\n",(int)offset));
361 T((KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
362 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
371 T((KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
372 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
385 list_for_each(i,&obj->variant.directoryVariant.children)
388 if(curoffs >= offset)
390 l = list_entry(i, yaffs_Object,siblings);
392 yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1);
393 T((KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
399 yaffs_GetObjectInode(l),
400 yaffs_GetObjectType(l))
421 * File creation. Allocate an inode, and we're done..
423 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
427 yaffs_Object *obj = NULL;
428 yaffs_Object *parent = yaffs_InodeToObject(dir);
434 T((KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
435 parent->objectId,parent->variantType));
439 T((KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
443 T(("yaffs_mknod: making oject for %s, mode %x\n",
444 dentry->d_name.name, mode));
446 switch (mode & S_IFMT)
451 case S_IFREG: // file
452 T((KERN_DEBUG"yaffs_mknod: making file\n"));
453 obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
455 case S_IFDIR: // directory
456 T((KERN_DEBUG"yaffs_mknod: making directory\n"));
457 obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
459 case S_IFLNK: // symlink
460 T((KERN_DEBUG"yaffs_mknod: making file\n"));
467 inode = yaffs_get_inode(dir->i_sb, mode, dev, obj);
469 // did not fix dir bug if((mode & S_IFMT) == S_IFDIR) atomic_inc(&inode->i_count);
471 d_instantiate(dentry, inode);
472 T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
477 T((KERN_DEBUG"yaffs_mknod failed making object\n"));
485 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
488 T((KERN_DEBUG"yaffs_mkdir\n"));
489 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
491 // attempt to fix dir bug - didn't work
500 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
502 T((KERN_DEBUG"yaffs_create\n"));
503 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
507 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
510 T((KERN_DEBUG"yaffs_unlink\n"));
512 if(yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name) == YAFFS_OK)
526 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
528 struct inode *inode = old_dentry->d_inode;
530 T((KERN_DEBUG"yaffs_link\n"));
532 return -EPERM; //Todo
535 if (S_ISDIR(inode->i_mode))
543 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
547 T((KERN_DEBUG"yaffs_symlink\n"));
550 return -ENOMEM; //Todo
552 error = yaffs_mknod(dir, dentry, S_IFLNK | S_IRWXUGO, 0);
556 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
559 T((KERN_DEBUG"yaffs_sync_object\n"));
560 yaffs_FlushFile(yaffs_DentryToObject(dentry));
565 * The VFS layer already does all the dentry stuff for rename.
567 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
571 if( yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
572 yaffs_InodeToObject(new_dir),new_dentry->d_name.name) == YAFFS_OK)
584 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
586 struct inode *inode = dentry->d_inode;
589 T((KERN_DEBUG"yaffs_setattr\n"));
591 if((error = inode_change_ok(inode,attr)) == 0)
594 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
603 inode_setattr(inode,attr);
608 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
610 T((KERN_DEBUG"yaffs_statfs\n"));
612 buf->f_type = YAFFS_MAGIC;
613 buf->f_bsize = YAFFS_BYTES_PER_CHUNK;
614 buf->f_namelen = 255;
615 buf->f_blocks = yaffs_SuperToDevice(sb)->nBlocks * YAFFS_CHUNKS_PER_BLOCK;
618 buf->f_bavail = buf->f_bfree = yaffs_GetNumberOfFreeChunks(yaffs_SuperToDevice(sb));
622 static void yaffs_read_inode (struct inode *inode)
627 T((KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
629 obj = yaffs_FindObjectByNumber(yaffs_SuperToDevice(inode->i_sb),inode->i_ino);
631 yaffs_FillInodeFromObject(inode,obj);
635 static void yaffs_put_super(struct super_block *sb)
637 yaffs_Device *dev = yaffs_SuperToDevice(sb);
639 if(dev->putSuperFunc)
641 dev->putSuperFunc(sb);
646 #ifdef YAFFS_MTD_ENABLED
648 static void yaffs_MTDPutSuper(struct super_block *sb)
651 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
663 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
665 struct inode * inode;
666 struct dentry * root;
670 T(("yaffs_read_super: %s\n", useRam ? "RAM" : "MTD"));
671 sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
672 sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
673 sb->s_magic = YAFFS_MAGIC;
674 sb->s_op = &yaffs_super_ops;
677 printk(KERN_INFO"yaffs: sb is NULL\n");
679 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
680 else if(! kdevname(sb->s_dev))
681 printk(KERN_INFO"yaffs: kdevname is NULL\n");
683 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
690 #ifdef YAFFS_RAM_ENABLED
691 // Set the yaffs_Device up for ram emulation
693 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
696 // Deep shit could not allocate device structure
697 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
701 memset(dev,0,sizeof(yaffs_Device));
702 dev->genericDevice = NULL; // Not used for RAM emulation.
704 dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
705 dev->startBlock = 1; // Don't use block 0
706 dev->endBlock = dev->nBlocks - 1;
708 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
709 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
710 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
711 dev->initialiseNAND = nandemul_InitialiseNAND;
718 #ifdef YAFFS_MTD_ENABLED
719 struct mtd_info *mtd;
721 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
722 MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
724 // Hope it's a NAND mtd
725 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
728 printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
732 if(mtd->type != MTD_NANDFLASH)
734 printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
738 printk(KERN_DEBUG" erase %x\n",mtd->erase);
739 printk(KERN_DEBUG" read %x\n",mtd->read);
740 printk(KERN_DEBUG" write %x\n",mtd->write);
741 printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
742 printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
743 printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
744 printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
753 printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
757 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
758 mtd->oobsize != YAFFS_BYTES_PER_SPARE)
760 printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
765 // OK, so if we got here, we have an MTD that's NAND and looks
766 // like it has the right capabilities
767 // Set the yaffs_Device up for ram emulation
769 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
772 // Deep shit could not allocate device structure
773 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
777 memset(dev,0,sizeof(yaffs_Device));
778 dev->genericDevice = mtd;
780 // Set up the memory size parameters....
782 // NCB dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
783 dev->nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
784 dev->startBlock = 1; // Don't use block 0
785 dev->endBlock = dev->nBlocks - 1;
787 // ... and the functions.
788 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
789 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
790 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
791 dev->initialiseNAND = nandmtd_InitialiseNAND;
793 dev->putSuperFunc = yaffs_MTDPutSuper;
799 yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
800 T(("yaffs_read_super: guts initialised\n"));
803 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
807 T(("yaffs_read_super: got root inode\n"));
810 root = d_alloc_root(inode);
812 T(("yaffs_read_super: d_alloc_root done\n"));
820 T(("yaffs_read_super: done\n"));
824 #ifdef YAFFS_MTD_ENABLED
825 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
827 return yaffs_internal_read_super(0,sb,data,silent);
830 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
833 #ifdef YAFFS_RAM_ENABLED
835 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
837 return yaffs_internal_read_super(1,sb,data,silent);
840 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
841 #endif // YAFFS_RAM_ENABLED
844 static struct proc_dir_entry *my_proc_entry;
847 static int yaffs_proc_read(
857 static char my_buffer[1000];
859 if (offset > 0) return 0;
861 /* Fill the buffer and get its length */
863 "YAFFS built:"__DATE__ " "__TIME__"\n"
867 strcpy(page,my_buffer);
868 return strlen(my_buffer);
871 static int __init init_yaffs_fs(void)
875 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
876 /* Install the proc_fs entry */
877 my_proc_entry = create_proc_read_entry("yaffs",
887 #ifdef YAFFS_RAM_ENABLED
889 error = register_filesystem(&yaffs_ram_fs_type);
894 #endif //YAFFS_RAM_ENABLED
896 #ifdef YAFFS_MTD_ENABLED
897 error = register_filesystem(&yaffs_fs_type);
900 #ifdef YAFFS_RAM_ENABLED
901 unregister_filesystem(&yaffs_ram_fs_type);
902 #endif //YAFFS_RAM_ENABLED
904 #endif // YAFFS_MTD_ENABLED
909 static void __exit exit_yaffs_fs(void)
911 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
913 remove_proc_entry("yaffs",&proc_root);
915 #ifdef YAFFS_RAM_ENABLED
916 unregister_filesystem(&yaffs_ram_fs_type);
918 #ifdef YAFFS_MTD_ENABLED
919 unregister_filesystem(&yaffs_fs_type);
924 module_init(init_yaffs_fs)
925 module_exit(exit_yaffs_fs)
927 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
928 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
929 MODULE_LICENSE("GPL");