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 * * Luc van OostenRyck for numerous patches.
24 * * Nick Bane for patches marked NCB.
25 * * Some code bodily lifted from JFFS2.
29 #include <linux/config.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/version.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/list.h>
37 #include <linux/proc_fs.h>
38 #include <linux/pagemap.h>
39 #include <linux/mtd/mtd.h>
40 #include <linux/interrupt.h>
41 #include <linux/string.h>
42 #include <linux/locks.h>
44 #include <asm/uaccess.h>
46 #include "yaffs_guts.h"
48 #ifdef CONFIG_YAFFS_RAM_ENABLED
49 #include "yaffs_nandemul.h"
50 // 2 MB of RAM for emulation
51 #define YAFFS_RAM_EMULATION_SIZE 0x200000
52 #endif //CONFIG_YAFFS_RAM_ENABLED
54 #ifdef CONFIG_YAFFS_MTD_ENABLED
55 #include <linux/mtd/mtd.h>
56 #include "yaffs_mtdif.h"
57 #endif //CONFIG_YAFFS_MTD_ENABLED
63 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
64 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
65 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
69 static void yaffs_put_super(struct super_block *sb);
71 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos);
72 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos);
73 static int yaffs_file_flush(struct file* file);
75 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync);
77 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
79 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
80 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry);
81 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry);
82 static int yaffs_unlink(struct inode * dir, struct dentry *dentry);
83 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname);
84 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode);
85 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev);
86 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry);
87 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
89 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
90 static void yaffs_read_inode (struct inode *inode);
91 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);
92 static void yaffs_put_inode (struct inode *inode);
93 static int yaffs_readpage(struct file *file, struct page * page);
95 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
96 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
98 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen);
99 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
105 static struct address_space_operations yaffs_file_address_operations = {
106 readpage: yaffs_readpage,
107 prepare_write: yaffs_prepare_write,
108 commit_write: yaffs_commit_write
112 static struct file_operations yaffs_file_operations = {
113 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
114 read: generic_file_read,
115 write: generic_file_write,
117 read: yaffs_file_read,
118 write: yaffs_file_write,
120 mmap: generic_file_mmap,
121 flush: yaffs_file_flush,
122 fsync: yaffs_sync_object,
126 static struct inode_operations yaffs_file_inode_operations = {
127 setattr: yaffs_setattr,
131 struct inode_operations yaffs_symlink_inode_operations =
133 readlink: yaffs_readlink,
134 follow_link: yaffs_follow_link,
135 setattr: yaffs_setattr
138 static struct inode_operations yaffs_dir_inode_operations = {
139 create: yaffs_create,
140 lookup: yaffs_lookup,
142 unlink: yaffs_unlink,
143 symlink: yaffs_symlink,
147 rename: yaffs_rename,
148 setattr: yaffs_setattr,
151 static struct file_operations yaffs_dir_operations = {
152 read: generic_read_dir,
153 readdir: yaffs_readdir,
154 fsync: yaffs_sync_object,
158 static struct super_operations yaffs_super_ops = {
159 statfs: yaffs_statfs,
160 read_inode: yaffs_read_inode,
161 put_inode: yaffs_put_inode,
162 put_super: yaffs_put_super,
170 static void yaffs_GrossLock(yaffs_Device *dev)
172 T((KERN_DEBUG"yaffs locking\n"));
174 down(&dev->grossLock);
177 static void yaffs_GrossUnlock(yaffs_Device *dev)
179 T((KERN_DEBUG"yaffs unlocking\n"));
184 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen)
186 unsigned char *alias;
189 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
192 yaffs_GrossLock(dev);
194 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
196 yaffs_GrossUnlock(dev);
201 ret = vfs_readlink(dentry, buffer, buflen, alias);
206 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
208 unsigned char *alias;
210 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
213 yaffs_GrossLock(dev);
215 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
217 yaffs_GrossUnlock(dev);
222 ret = vfs_follow_link(nd,alias);
228 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj);
231 * Lookup is used to find objects in the fs
233 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
238 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
241 yaffs_GrossLock(dev);
244 T((KERN_DEBUG"yaffs_lookup for %d:%s\n",yaffs_InodeToObject(dir)->objectId,dentry->d_name.name));
246 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),dentry->d_name.name);
248 obj = yaffs_GetEquivalentObject(obj); // in case it was a hardlink
254 T((KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
256 inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj);
260 T((KERN_DEBUG"yaffs_loookup looks good\n"));
261 // try to fix ln -s prob dget(dentry); // try to solve directory bug
264 yaffs_GrossUnlock(dev);
272 T((KERN_DEBUG"yaffs_lookup not found\n"));
275 yaffs_GrossUnlock(dev);
281 // For now put inode is just for debugging
282 static void yaffs_put_inode(struct inode *inode)
284 T(("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count)));
286 // yaffs_FlushFile(yaffs_InodeToObject(inode));
291 static int yaffs_file_flush(struct file* file)
293 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
295 yaffs_Device *dev = obj->myDev;
297 T((KERN_DEBUG"yaffs_file_flush object %d (%s)\n",obj->objectId,
298 obj->dirty ? "dirty" : "clean"));
300 yaffs_GrossLock(dev);
302 yaffs_FlushFile(obj);
304 yaffs_GrossUnlock(dev);
311 static int yaffs_readpage_nolock(struct file *f, struct page * pg)
316 unsigned char *pg_buf;
321 T((KERN_DEBUG"yaffs_readpage at %08x, size %08x\n",
322 pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE));
324 obj = yaffs_DentryToObject(f->f_dentry);
333 /* FIXME: Can kmap fail? */
335 yaffs_GrossLock(dev);
337 ret = yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
339 yaffs_GrossUnlock(dev);
341 if(ret >= 0) ret = 0;
344 ClearPageUptodate(pg);
351 flush_dcache_page(pg);
355 T((KERN_DEBUG"yaffs_readpage done\n"));
359 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
361 int ret = yaffs_readpage_nolock(f,pg);
366 static int yaffs_readpage(struct file *f, struct page * pg)
368 return yaffs_readpage_unlock(f,pg);
372 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
375 T((KERN_DEBUG"yaffs_prepair_write\n"));
376 if(!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
377 return yaffs_readpage_nolock(f,pg);
383 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
386 void *addr = page_address(pg) + offset;
387 loff_t pos = (((loff_t)pg->index) << PAGE_CACHE_SHIFT) + offset;
388 int nBytes = to - offset;
392 unsigned saddr = addr;
394 T((KERN_DEBUG"yaffs_commit_write addr %x pos %x nBytes %d\n",saddr,spos,nBytes));
396 nWritten = yaffs_file_write(f,addr, nBytes, &pos);
398 if(nWritten != nBytes)
400 T((KERN_DEBUG"yaffs_commit_write not same size nWritten %d nBytes %d\n",nWritten,nBytes));
402 ClearPageUptodate(pg);
409 T((KERN_DEBUG"yaffs_commit_write returning %d\n",nWritten));
417 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
421 inode->i_ino = obj->objectId;
422 inode->i_mode = obj->st_mode;
423 inode->i_uid = obj->st_uid;
424 inode->i_gid = obj->st_gid;
425 inode->i_blksize = inode->i_sb->s_blocksize;
427 inode->i_rdev = obj->st_rdev;;
428 inode->i_atime = obj->st_atime;
429 inode->i_mtime = obj->st_mtime;
430 inode->i_ctime = obj->st_ctime;
431 inode->i_size = yaffs_GetObjectFileLength(obj);
432 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
434 T((KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
435 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
437 switch (obj->st_mode & S_IFMT)
439 default: // fifo, device or socket
440 init_special_inode(inode, obj->st_mode, obj->st_rdev);
442 case S_IFREG: // file
443 inode->i_op = &yaffs_file_inode_operations;
444 inode->i_fop = &yaffs_file_operations;
445 inode->i_mapping->a_ops = &yaffs_file_address_operations;
447 case S_IFDIR: // directory
448 inode->i_op = &yaffs_dir_inode_operations;
449 inode->i_fop = &yaffs_dir_operations;
451 case S_IFLNK: // symlink
452 inode->i_op = &yaffs_symlink_inode_operations;
457 inode->u.generic_ip = obj;
462 T((KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
467 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
469 struct inode * inode;
471 T((KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
473 inode = iget(sb,obj->objectId);
475 // NB Side effect: iget calls back to yaffs_read_inode().
480 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
487 T((KERN_DEBUG"yaffs_file_read\n"));
489 obj = yaffs_DentryToObject(f->f_dentry);
493 yaffs_GrossLock(dev);
495 inode = f->f_dentry->d_inode;
497 if (*pos < inode->i_size)
499 if (*pos + n > inode->i_size)
501 n = inode->i_size - *pos;
509 nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
515 yaffs_GrossUnlock(dev);
519 T((KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
525 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
533 obj = yaffs_DentryToObject(f->f_dentry);
537 yaffs_GrossLock(dev);
539 inode = f->f_dentry->d_inode;
541 if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
543 ipos = inode->i_size;
553 T((KERN_DEBUG"yaffs_file_write: hey obj is null!\n"));
557 T((KERN_DEBUG"yaffs_file_write about to write writing %d bytes to object %d at %d\n",n,obj->objectId,ipos));
560 nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n);
562 T((KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
567 if(ipos > inode->i_size)
569 inode->i_size = ipos;
570 inode->i_blocks = (ipos + inode->i_blksize - 1)/ inode->i_blksize;
572 T((KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,inode->i_blocks));
576 yaffs_GrossUnlock(dev);
578 return nWritten != n ? -ENOSPC : nWritten;
582 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
586 struct inode *inode = f->f_dentry->d_inode;
587 unsigned long offset, curoffs;
591 char name[YAFFS_MAX_NAME_LENGTH +1];
593 obj = yaffs_DentryToObject(f->f_dentry);
596 yaffs_GrossLock(dev);
600 T(("yaffs_readdir: starting at %d\n",(int)offset));
604 T((KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
605 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
614 T((KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
615 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
625 list_for_each(i,&obj->variant.directoryVariant.children)
628 if(curoffs >= offset)
630 l = list_entry(i, yaffs_Object,siblings);
632 yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1);
633 T((KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
639 yaffs_GetObjectInode(l),
640 yaffs_GetObjectType(l))
654 yaffs_GrossUnlock(dev);
661 * File creation. Allocate an inode, and we're done..
663 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
667 yaffs_Object *obj = NULL;
670 yaffs_Object *parent = yaffs_InodeToObject(dir);
676 T((KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
677 parent->objectId,parent->variantType));
681 T((KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
685 T(("yaffs_mknod: making oject for %s, mode %x dev %x\n",
686 dentry->d_name.name, mode,dev));
690 yaffs_GrossLock(dev);
692 switch (mode & S_IFMT)
695 // Special (socket, fifo, device...)
696 T((KERN_DEBUG"yaffs_mknod: making special\n"));
697 obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,dev);
699 case S_IFREG: // file
700 T((KERN_DEBUG"yaffs_mknod: making file\n"));
701 obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
703 case S_IFDIR: // directory
704 T((KERN_DEBUG"yaffs_mknod: making directory\n"));
705 obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
707 case S_IFLNK: // symlink
708 T((KERN_DEBUG"yaffs_mknod: making file\n"));
709 obj = NULL; // Do we ever get here?
715 inode = yaffs_get_inode(dir->i_sb, mode, dev, obj);
716 d_instantiate(dentry, inode);
717 T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
722 T((KERN_DEBUG"yaffs_mknod failed making object\n"));
726 yaffs_GrossUnlock(dev);
731 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
734 T((KERN_DEBUG"yaffs_mkdir\n"));
735 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
737 // attempt to fix dir bug - didn't work
746 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
748 T((KERN_DEBUG"yaffs_create\n"));
749 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
753 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
759 T((KERN_DEBUG"yaffs_unlink %d:%s\n",dir->i_ino,dentry->d_name.name));
761 dev = yaffs_InodeToObject(dir)->myDev;
763 yaffs_GrossLock(dev);
764 retVal = yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name);
765 yaffs_GrossUnlock(dev);
767 if( retVal == YAFFS_OK)
781 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
783 struct inode *inode = old_dentry->d_inode;
784 yaffs_Object *obj = NULL;
785 yaffs_Object *link=NULL;
788 T((KERN_DEBUG"yaffs_link\n"));
790 obj = yaffs_InodeToObject(inode);
793 yaffs_GrossLock(dev);
795 link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
797 yaffs_GrossUnlock(dev);
809 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
814 T((KERN_DEBUG"yaffs_symlink\n"));
816 dev = yaffs_InodeToObject(dir)->myDev;
817 yaffs_GrossLock(dev);
818 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
819 S_IFLNK | S_IRWXUGO, current->uid, current->gid,
821 yaffs_GrossUnlock(dev);
825 T((KERN_DEBUG"symlink created OK\n"));
830 T((KERN_DEBUG"symlink not created\n"));
837 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
843 obj = yaffs_DentryToObject(dentry);
847 T((KERN_DEBUG"yaffs_sync_object\n"));
848 yaffs_GrossLock(dev);
849 yaffs_FlushFile(obj);
850 yaffs_GrossUnlock(dev);
855 * The VFS layer already does all the dentry stuff for rename.
857 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
862 dev = yaffs_InodeToObject(old_dir)->myDev;
864 yaffs_GrossLock(dev);
866 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
867 yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
868 yaffs_GrossUnlock(dev);
870 if(retVal == YAFFS_OK)
882 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
884 struct inode *inode = dentry->d_inode;
888 T((KERN_DEBUG"yaffs_setattr of object %d\n",yaffs_InodeToObject(inode)->objectId));
890 if((error = inode_change_ok(inode,attr)) == 0)
893 dev = yaffs_InodeToObject(inode)->myDev;
894 yaffs_GrossLock(dev);
895 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
903 yaffs_GrossUnlock(dev);
904 inode_setattr(inode,attr);
909 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
911 yaffs_Device *dev = yaffs_SuperToDevice(sb);
912 T((KERN_DEBUG"yaffs_statfs\n"));
914 yaffs_GrossLock(dev);
916 buf->f_type = YAFFS_MAGIC;
917 buf->f_bsize = sb->s_blocksize;
918 buf->f_namelen = 255;
919 buf->f_blocks = dev->nBlocks * YAFFS_CHUNKS_PER_BLOCK/
920 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
923 buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev)/
924 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
925 buf->f_bavail = buf->f_bfree;
927 yaffs_GrossUnlock(dev);
931 static void yaffs_read_inode (struct inode *inode)
933 // NB This is called as a side effect of other functions and
934 // thus gross locking should always be in place already.
937 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
939 T((KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
941 obj = yaffs_FindObjectByNumber(dev,inode->i_ino);
943 yaffs_FillInodeFromObject(inode,obj);
948 static void yaffs_put_super(struct super_block *sb)
950 yaffs_Device *dev = yaffs_SuperToDevice(sb);
952 yaffs_GrossLock(dev);
953 if(dev->putSuperFunc)
955 dev->putSuperFunc(sb);
957 yaffs_Deinitialise(dev);
958 yaffs_GrossUnlock(dev);
964 #ifdef CONFIG_YAFFS_MTD_ENABLED
966 static void yaffs_MTDPutSuper(struct super_block *sb)
969 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
981 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
983 struct inode * inode;
984 struct dentry * root;
987 sb->s_magic = YAFFS_MAGIC;
988 sb->s_op = &yaffs_super_ops;
991 printk(KERN_INFO"yaffs: sb is NULL\n");
993 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
994 else if(! kdevname(sb->s_dev))
995 printk(KERN_INFO"yaffs: kdevname is NULL\n");
997 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
1001 #ifdef CONFIG_YAFFS_USE_CHUNK_SIZE
1002 sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
1003 sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
1005 sb->s_blocksize = PAGE_CACHE_SIZE;
1006 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1008 T(("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",sb->s_blocksize));
1010 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1011 T(("yaffs: Write verification disabled. All guarantees null and void\n");
1019 #ifdef CONFIG_YAFFS_RAM_ENABLED
1020 // Set the yaffs_Device up for ram emulation
1022 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1025 // Deep shit could not allocate device structure
1026 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1030 memset(dev,0,sizeof(yaffs_Device));
1031 dev->genericDevice = NULL; // Not used for RAM emulation.
1033 dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1034 dev->startBlock = 1; // Don't use block 0
1035 dev->endBlock = dev->nBlocks - 1;
1037 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
1038 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
1039 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
1040 dev->initialiseNAND = nandemul_InitialiseNAND;
1047 #ifdef CONFIG_YAFFS_MTD_ENABLED
1048 struct mtd_info *mtd;
1050 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
1051 MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
1053 // Hope it's a NAND mtd
1054 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1057 printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
1061 if(mtd->type != MTD_NANDFLASH)
1063 printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
1067 printk(KERN_DEBUG" erase %x\n",mtd->erase);
1068 printk(KERN_DEBUG" read %x\n",mtd->read);
1069 printk(KERN_DEBUG" write %x\n",mtd->write);
1070 printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
1071 printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
1072 printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
1073 printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
1082 printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
1086 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
1087 mtd->oobsize != YAFFS_BYTES_PER_SPARE)
1089 printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
1094 // OK, so if we got here, we have an MTD that's NAND and looks
1095 // like it has the right capabilities
1096 // Set the yaffs_Device up for ram emulation
1098 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1101 // Deep shit could not allocate device structure
1102 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1106 memset(dev,0,sizeof(yaffs_Device));
1107 dev->genericDevice = mtd;
1109 // Set up the memory size parameters....
1111 // NCB dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1112 dev->nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1113 dev->startBlock = 1; // Don't use block 0
1114 dev->endBlock = dev->nBlocks - 1;
1116 // ... and the functions.
1117 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1118 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1119 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1120 dev->initialiseNAND = nandmtd_InitialiseNAND;
1122 dev->putSuperFunc = yaffs_MTDPutSuper;
1126 init_MUTEX(&dev->grossLock);
1129 yaffs_GrossLock(dev);
1130 yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
1132 T(("yaffs_read_super: guts initialised\n"));
1134 // Create root inode
1135 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
1137 yaffs_GrossUnlock(dev);
1142 T(("yaffs_read_super: got root inode\n"));
1145 root = d_alloc_root(inode);
1147 T(("yaffs_read_super: d_alloc_root done\n"));
1155 T(("yaffs_read_super: done\n"));
1159 #ifdef CONFIG_YAFFS_MTD_ENABLED
1160 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
1162 return yaffs_internal_read_super(0,sb,data,silent);
1165 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1168 #ifdef CONFIG_YAFFS_RAM_ENABLED
1170 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
1172 return yaffs_internal_read_super(1,sb,data,silent);
1175 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
1176 #endif // CONFIG_YAFFS_RAM_ENABLED
1179 static struct proc_dir_entry *my_proc_entry;
1182 static int yaffs_proc_read(
1192 static char my_buffer[1000];
1194 if (offset > 0) return 0;
1196 /* Fill the buffer and get its length */
1198 "YAFFS built:"__DATE__ " "__TIME__"\n"
1202 strcpy(page,my_buffer);
1203 return strlen(my_buffer);
1206 static int __init init_yaffs_fs(void)
1210 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
1211 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
1212 printk(KERN_DEBUG "yaffs is using generic read/write (caching)\n");
1214 printk(KERN_DEBUG "yaffs is using direct read/write (uncached)\n");
1218 /* Install the proc_fs entry */
1219 my_proc_entry = create_proc_read_entry("yaffs",
1229 #ifdef CONFIG_YAFFS_RAM_ENABLED
1231 error = register_filesystem(&yaffs_ram_fs_type);
1236 #endif //CONFIG_YAFFS_RAM_ENABLED
1238 #ifdef CONFIG_YAFFS_MTD_ENABLED
1239 error = register_filesystem(&yaffs_fs_type);
1242 #ifdef CONFIG_YAFFS_RAM_ENABLED
1243 unregister_filesystem(&yaffs_ram_fs_type);
1244 #endif //CONFIG_YAFFS_RAM_ENABLED
1246 #endif // CONFIG_YAFFS_MTD_ENABLED
1251 static void __exit exit_yaffs_fs(void)
1253 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
1255 remove_proc_entry("yaffs",&proc_root);
1257 #ifdef CONFIG_YAFFS_RAM_ENABLED
1258 unregister_filesystem(&yaffs_ram_fs_type);
1260 #ifdef CONFIG_YAFFS_MTD_ENABLED
1261 unregister_filesystem(&yaffs_fs_type);
1266 module_init(init_yaffs_fs)
1267 module_exit(exit_yaffs_fs)
1269 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
1270 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
1271 MODULE_LICENSE("GPL");