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 ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos);
64 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos);
66 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync);
68 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
70 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
71 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry);
72 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry);
73 static int yaffs_unlink(struct inode * dir, struct dentry *dentry);
74 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname);
75 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode);
76 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev);
77 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry);
78 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
80 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
81 static void yaffs_read_inode (struct inode *inode);
82 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);
83 static void yaffs_put_inode (struct inode *inode);
85 //static int yaffs_readpage(struct file*,struct page *
87 static struct address_space_operations yaffs_address_ops = {
88 // readpage: yaffs_readpage,
89 // prepare_write: yaffs_prepare_write,
90 // commit_write: yaffs_commit_write
94 static struct file_operations yaffs_file_operations = {
95 read: yaffs_file_read,
96 write: yaffs_file_write,
97 // mmap: generic_file_mmap,
98 fsync: yaffs_sync_object,
102 static struct inode_operations yaffs_file_inode_operations = {
103 setattr: yaffs_setattr,
108 static struct file_operations yaffs_dir_operations = {
109 read: generic_read_dir,
110 readdir: yaffs_readdir,
111 fsync: yaffs_sync_object,
114 static struct inode_operations yaffs_dir_inode_operations = {
115 create: yaffs_create,
116 lookup: yaffs_lookup,
118 unlink: yaffs_unlink,
119 symlink: yaffs_symlink,
123 rename: yaffs_rename,
124 setattr: yaffs_setattr,
127 static struct super_operations yaffs_super_ops = {
128 statfs: yaffs_statfs,
129 read_inode: yaffs_read_inode,
130 put_inode: yaffs_put_inode,
137 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj);
140 * Lookup is used to find objects in the fs
142 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
148 T((KERN_DEBUG"yaffs_lookup for %d:%s\n",yaffs_InodeToObject(dir)->objectId,dentry->d_name.name));
150 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),dentry->d_name.name);
154 T((KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
156 inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj);
160 T((KERN_DEBUG"yaffs_loookup looks good\n"));
161 dget(dentry); // try to solve directory bug
169 T((KERN_DEBUG"yaffs_lookup not found\n"));
176 // For now put inode is just for debugging
177 static void yaffs_put_inode(struct inode *inode)
179 T(("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count)));
182 #ifdef YAFFS_ADDRESS_OPS
183 static int yaffs_readpage(struct file *file, struct page * page)
185 T((KERN_DEBUG"yaffs_readpage\n"));
191 static int yaffs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
193 T((KERN_DEBUG"yaffs_prepare_write\n"));
199 static int yaffs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
202 struct inode *inode = page->mapping->host;
203 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
205 T((KERN_DEBUG"yaffs_commit_write\n"));
210 #endif //YAFFS_ADDRESS_OPS
213 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
217 inode->i_ino = obj->objectId;
218 inode->i_mode = obj->st_mode;
219 inode->i_uid = obj->st_uid;
220 inode->i_gid = obj->st_gid;
221 inode->i_blksize = YAFFS_BYTES_PER_CHUNK;
223 inode->i_rdev = NODEV;
224 inode->i_mapping->a_ops = &yaffs_address_ops;
225 inode->i_atime = obj->st_atime;
226 inode->i_mtime = obj->st_mtime;
227 inode->i_ctime = obj->st_ctime;
228 inode->i_size = yaffs_GetObjectFileLength(obj);
229 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
231 T((KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
232 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
234 switch (obj->st_mode & S_IFMT)
237 // init_special_inode(inode, mode, dev);
239 case S_IFREG: // file
240 inode->i_op = &yaffs_file_inode_operations;
241 inode->i_fop = &yaffs_file_operations;
243 case S_IFDIR: // directory
244 inode->i_op = &yaffs_dir_inode_operations;
245 inode->i_fop = &yaffs_dir_operations;
247 case S_IFLNK: // symlink
248 inode->i_op = &page_symlink_inode_operations;
253 inode->u.generic_ip = obj;
258 T((KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
263 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
265 struct inode * inode;
267 T((KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
269 inode = iget(sb,obj->objectId);
271 // No need to call this since iget calls it via the read_inode callback
272 // yaffs_FillInodeFromObject(inode,obj);
277 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
283 T((KERN_DEBUG"yaffs_file_read\n"));
285 obj = yaffs_DentryToObject(f->f_dentry);
286 inode = f->f_dentry->d_inode;
288 if (*pos < inode->i_size)
290 if (*pos + n > inode->i_size)
292 n = inode->i_size - *pos;
300 nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
306 T((KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
311 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
317 obj = yaffs_DentryToObject(f->f_dentry);
318 inode = f->f_dentry->d_inode;
319 nWritten = yaffs_WriteDataToFile(obj,buf,*pos,n);
321 T((KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
326 if(*pos > inode->i_size)
328 inode->i_size = *pos;
329 T((KERN_DEBUG"yaffs_file_write size updated to %d\n",ipos));
337 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
340 struct inode *inode = f->f_dentry->d_inode;
341 unsigned long offset, curoffs;
345 char name[YAFFS_MAX_NAME_LENGTH +1];
347 obj = yaffs_DentryToObject(f->f_dentry);
351 T(("yaffs_readdir: starting at %d\n",(int)offset));
355 T((KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
356 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
365 T((KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
366 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
379 list_for_each(i,&obj->variant.directoryVariant.children)
382 if(curoffs >= offset)
384 l = list_entry(i, yaffs_Object,siblings);
386 yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1);
387 T((KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
393 yaffs_GetObjectInode(l),
394 yaffs_GetObjectType(l))
415 * File creation. Allocate an inode, and we're done..
417 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
421 yaffs_Object *obj = NULL;
422 yaffs_Object *parent = yaffs_InodeToObject(dir);
428 T((KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
429 parent->objectId,parent->variantType));
433 T((KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
437 T(("yaffs_mknod: making oject for %s, mode %x\n",
438 dentry->d_name.name, mode));
440 switch (mode & S_IFMT)
445 case S_IFREG: // file
446 T((KERN_DEBUG"yaffs_mknod: making file\n"));
447 obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
449 case S_IFDIR: // directory
450 T((KERN_DEBUG"yaffs_mknod: making directory\n"));
451 obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
453 case S_IFLNK: // symlink
454 T((KERN_DEBUG"yaffs_mknod: making file\n"));
461 inode = yaffs_get_inode(dir->i_sb, mode, dev, obj);
463 // did not fix dir bug if((mode & S_IFMT) == S_IFDIR) atomic_inc(&inode->i_count);
465 d_instantiate(dentry, inode);
466 T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
471 T((KERN_DEBUG"yaffs_mknod failed making object\n"));
479 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
482 T((KERN_DEBUG"yaffs_mkdir\n"));
483 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
485 // attempt to fix dir bug - didn't work
494 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
496 T((KERN_DEBUG"yaffs_create\n"));
497 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
501 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
504 T((KERN_DEBUG"yaffs_unlink\n"));
506 if(yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name) == YAFFS_OK)
520 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
522 struct inode *inode = old_dentry->d_inode;
524 T((KERN_DEBUG"yaffs_link\n"));
526 return -EPERM; //Todo
529 if (S_ISDIR(inode->i_mode))
537 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
541 T((KERN_DEBUG"yaffs_symlink\n"));
544 return -ENOMEM; //Todo
546 error = yaffs_mknod(dir, dentry, S_IFLNK | S_IRWXUGO, 0);
550 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
553 T((KERN_DEBUG"yaffs_sync_object\n"));
554 yaffs_FlushFile(yaffs_DentryToObject(dentry));
559 * The VFS layer already does all the dentry stuff for rename.
561 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
565 if( yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
566 yaffs_InodeToObject(new_dir),new_dentry->d_name.name) == YAFFS_OK)
578 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
580 struct inode *inode = dentry->d_inode;
583 T((KERN_DEBUG"yaffs_setattr\n"));
585 if((error = inode_change_ok(inode,attr)) == 0)
588 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
597 inode_setattr(inode,attr);
602 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
604 T((KERN_DEBUG"yaffs_statfs\n"));
606 buf->f_type = YAFFS_MAGIC;
607 buf->f_bsize = YAFFS_BYTES_PER_CHUNK;
608 buf->f_namelen = 255;
609 buf->f_blocks = yaffs_SuperToDevice(sb)->nBlocks * YAFFS_CHUNKS_PER_BLOCK;
612 buf->f_bavail = yaffs_GetNumberOfFreeChunks(yaffs_SuperToDevice(sb));
616 static void yaffs_read_inode (struct inode *inode)
621 T((KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
623 obj = yaffs_FindObjectByNumber(yaffs_SuperToDevice(inode->i_sb),inode->i_ino);
625 yaffs_FillInodeFromObject(inode,obj);
629 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
631 struct inode * inode;
632 struct dentry * root;
636 T(("yaffs_read_super: %s\n", useRam ? "RAM" : "MTD"));
637 sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
638 sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
639 sb->s_magic = YAFFS_MAGIC;
640 sb->s_op = &yaffs_super_ops;
643 printk(KERN_INFO"yaffs: sb is NULL\n");
645 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
646 else if(! kdevname(sb->s_dev))
647 printk(KERN_INFO"yaffs: kdevname is NULL\n");
649 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
656 #if YAFFS_RAM_ENABLED
657 // Set the yaffs_Device up for ram emulation
659 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
662 // Deep shit could not allocate device structure
663 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
667 memset(dev,0,sizeof(yaffs_Device));
668 dev->genericDevice = NULL; // Not used for RAM emulation.
670 dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
671 dev->startBlock = 1; // Don't use block 0
672 dev->endBlock = dev->nBlocks - 1;
674 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
675 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
676 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
677 dev->initialiseNAND = nandemul_InitialiseNAND;
683 #ifdef YAFFS_MTD_ENABLED
684 struct mtd_info *mtd;
686 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
687 MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
689 // Hope it's a NAND mtd
690 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
693 printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
697 if(mtd->type != MTD_NANDFLASH)
699 printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
703 printk(KERN_DEBUG" erase %x\n",mtd->erase);
704 printk(KERN_DEBUG" read %x\n",mtd->read);
705 printk(KERN_DEBUG" write %x\n",mtd->write);
706 printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
707 printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
708 printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
709 printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
718 printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
722 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
723 mtd->oobsize != YAFFS_BYTES_PER_SPARE)
725 printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
730 // OK, so if we got here, we have an MTD that's NAND and looks
731 // like it has the right capabilities
732 // Set the yaffs_Device up for ram emulation
734 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
737 // Deep shit could not allocate device structure
738 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
742 memset(dev,0,sizeof(yaffs_Device));
743 dev->genericDevice = mtd;
745 // Set up the memory size parameters....
747 dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
748 dev->startBlock = 1; // Don't use block 0
749 dev->endBlock = dev->nBlocks - 1;
751 // ... and the functions.
752 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
753 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
754 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
755 dev->initialiseNAND = nandmtd_InitialiseNAND;
762 yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
763 T(("yaffs_read_super: guts initialised\n"));
766 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
770 T(("yaffs_read_super: got root inode\n"));
773 root = d_alloc_root(inode);
775 T(("yaffs_read_super: d_alloc_root done\n"));
783 T(("yaffs_read_super: done\n"));
787 #ifdef YAFFS_MTD_ENABLED
788 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
790 return yaffs_internal_read_super(0,sb,data,silent);
793 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
796 #ifdef YAFFS_RAM_ENABLED
798 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
800 return yaffs_internal_read_super(1,sb,data,silent);
803 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
804 #endif // YAFFS_RAM_ENABLED
807 static struct proc_dir_entry *my_proc_entry;
810 static int yaffs_proc_read(
820 static char my_buffer[1000];
822 if (offset > 0) return 0;
824 /* Fill the buffer and get its length */
826 "YAFFS built:"__DATE__ " "__TIME__"\n"
830 strcpy(page,my_buffer);
831 return strlen(my_buffer);
834 static int __init init_yaffs_fs(void)
838 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
839 /* Install the proc_fs entry */
840 my_proc_entry = create_proc_read_entry("yaffs",
850 #ifdef YAFFS_RAM_ENABLED
852 error = register_filesystem(&yaffs_ram_fs_type);
857 #endif //YAFFS_RAM_ENABLED
859 #ifdef YAFFS_MTD_ENABLED
860 error = register_filesystem(&yaffs_fs_type);
863 #ifdef YAFFS_RAM_ENABLED
864 unregister_filesystem(&yaffs_ram_fs_type);
865 #endif //YAFFS_RAM_ENABLED
867 #endif // YAFFS_MTD_ENABLED
872 static void __exit exit_yaffs_fs(void)
874 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
876 remove_proc_entry("yaffs",&proc_root);
878 #ifdef YAFFS_RAM_ENABLED
879 unregister_filesystem(&yaffs_fs_type);
881 #ifdef YAFFS_MTD_ENABLED
882 unregister_filesystem(&yaffs_ram_fs_type);
887 module_init(init_yaffs_fs)
888 module_exit(exit_yaffs_fs)
890 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
891 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
892 MODULE_LICENSE("GPL");