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 numerous patches.
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 void yaffs_delete_inode(struct inode *);
94 static void yaffs_clear_inode(struct inode *);
96 static int yaffs_readpage(struct file *file, struct page * page);
98 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
99 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
101 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen);
102 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
107 static struct address_space_operations yaffs_file_address_operations = {
108 readpage: yaffs_readpage,
109 prepare_write: yaffs_prepare_write,
110 commit_write: yaffs_commit_write
114 static struct file_operations yaffs_file_operations = {
115 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
116 read: generic_file_read,
117 write: generic_file_write,
119 read: yaffs_file_read,
120 write: yaffs_file_write,
122 mmap: generic_file_mmap,
123 flush: yaffs_file_flush,
124 fsync: yaffs_sync_object,
128 static struct inode_operations yaffs_file_inode_operations = {
129 setattr: yaffs_setattr,
133 struct inode_operations yaffs_symlink_inode_operations =
135 readlink: yaffs_readlink,
136 follow_link: yaffs_follow_link,
137 setattr: yaffs_setattr
140 static struct inode_operations yaffs_dir_inode_operations = {
141 create: yaffs_create,
142 lookup: yaffs_lookup,
144 unlink: yaffs_unlink,
145 symlink: yaffs_symlink,
149 rename: yaffs_rename,
150 setattr: yaffs_setattr,
153 static struct file_operations yaffs_dir_operations = {
154 read: generic_read_dir,
155 readdir: yaffs_readdir,
156 fsync: yaffs_sync_object,
160 static struct super_operations yaffs_super_ops = {
161 statfs: yaffs_statfs,
162 read_inode: yaffs_read_inode,
163 put_inode: yaffs_put_inode,
164 put_super: yaffs_put_super,
166 delete_inode: yaffs_delete_inode,
167 clear_inode: yaffs_clear_inode,
172 static void yaffs_GrossLock(yaffs_Device *dev)
174 T((KERN_DEBUG"yaffs locking\n"));
176 down(&dev->grossLock);
179 static void yaffs_GrossUnlock(yaffs_Device *dev)
181 T((KERN_DEBUG"yaffs unlocking\n"));
186 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen)
188 unsigned char *alias;
191 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
194 yaffs_GrossLock(dev);
196 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
198 yaffs_GrossUnlock(dev);
203 ret = vfs_readlink(dentry, buffer, buflen, alias);
208 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
210 unsigned char *alias;
212 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
215 yaffs_GrossLock(dev);
217 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
219 yaffs_GrossUnlock(dev);
224 ret = vfs_follow_link(nd,alias);
230 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj);
233 * Lookup is used to find objects in the fs
235 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
240 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
243 yaffs_GrossLock(dev);
246 T((KERN_DEBUG"yaffs_lookup for %d:%s\n",yaffs_InodeToObject(dir)->objectId,dentry->d_name.name));
248 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),dentry->d_name.name);
250 obj = yaffs_GetEquivalentObject(obj); // in case it was a hardlink
256 T((KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
258 inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj);
262 T((KERN_DEBUG"yaffs_loookup dentry \n"));
263 //dget(dentry); // try to solve directory bug
266 yaffs_GrossUnlock(dev);
275 T((KERN_DEBUG"yaffs_lookup not found\n"));
278 yaffs_GrossUnlock(dev);
281 // return (ERR_PTR(-EIO));
285 // For now put inode is just for debugging
286 // Put inode is called when the inode **structure** is put.
287 static void yaffs_put_inode(struct inode *inode)
289 T(("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count)));
293 // clear is called to tell the fs to release any per-inode data it holds
294 static void yaffs_clear_inode(struct inode *inode)
296 yaffs_Object *obj = yaffs_InodeToObject(inode);
298 T(("yaffs_clear_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count),
299 obj ? "object exists" : "null object"));
304 inode->u.generic_ip = NULL;
310 // delete is called when the link count is zero and the inode
311 // is put (ie. nobody wants to know about it anymore, time to
313 // NB Must call clear_inode()
314 static void yaffs_delete_inode(struct inode *inode)
316 yaffs_Object *obj = yaffs_InodeToObject(inode);
319 T(("yaffs_delete_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count),
320 obj ? "object exists" : "null object"));
325 yaffs_GrossLock(dev);
326 yaffs_DeleteFile(obj);
327 yaffs_GrossUnlock(dev);
333 static int yaffs_file_flush(struct file* file)
335 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
337 yaffs_Device *dev = obj->myDev;
339 T((KERN_DEBUG"yaffs_file_flush object %d (%s)\n",obj->objectId,
340 obj->dirty ? "dirty" : "clean"));
342 yaffs_GrossLock(dev);
344 yaffs_FlushFile(obj);
346 yaffs_GrossUnlock(dev);
353 static int yaffs_readpage_nolock(struct file *f, struct page * pg)
358 unsigned char *pg_buf;
363 T((KERN_DEBUG"yaffs_readpage at %08x, size %08x\n",
364 pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE));
366 obj = yaffs_DentryToObject(f->f_dentry);
375 /* FIXME: Can kmap fail? */
377 yaffs_GrossLock(dev);
379 ret = yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
381 yaffs_GrossUnlock(dev);
383 if(ret >= 0) ret = 0;
386 ClearPageUptodate(pg);
393 flush_dcache_page(pg);
397 T((KERN_DEBUG"yaffs_readpage done\n"));
401 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
403 int ret = yaffs_readpage_nolock(f,pg);
408 static int yaffs_readpage(struct file *f, struct page * pg)
410 return yaffs_readpage_unlock(f,pg);
414 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
417 T((KERN_DEBUG"yaffs_prepair_write\n"));
418 if(!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
419 return yaffs_readpage_nolock(f,pg);
425 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
428 void *addr = page_address(pg) + offset;
429 loff_t pos = (((loff_t)pg->index) << PAGE_CACHE_SHIFT) + offset;
430 int nBytes = to - offset;
434 unsigned saddr = addr;
436 T((KERN_DEBUG"yaffs_commit_write addr %x pos %x nBytes %d\n",saddr,spos,nBytes));
438 nWritten = yaffs_file_write(f,addr, nBytes, &pos);
440 if(nWritten != nBytes)
442 T((KERN_DEBUG"yaffs_commit_write not same size nWritten %d nBytes %d\n",nWritten,nBytes));
444 ClearPageUptodate(pg);
451 T((KERN_DEBUG"yaffs_commit_write returning %d\n",nWritten));
459 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
463 inode->i_ino = obj->objectId;
464 inode->i_mode = obj->st_mode;
465 inode->i_uid = obj->st_uid;
466 inode->i_gid = obj->st_gid;
467 inode->i_blksize = inode->i_sb->s_blocksize;
469 inode->i_rdev = obj->st_rdev;;
470 inode->i_atime = obj->st_atime;
471 inode->i_mtime = obj->st_mtime;
472 inode->i_ctime = obj->st_ctime;
473 inode->i_size = yaffs_GetObjectFileLength(obj);
474 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
476 T((KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
477 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
479 switch (obj->st_mode & S_IFMT)
481 default: // fifo, device or socket
482 init_special_inode(inode, obj->st_mode, obj->st_rdev);
484 case S_IFREG: // file
485 inode->i_op = &yaffs_file_inode_operations;
486 inode->i_fop = &yaffs_file_operations;
487 inode->i_mapping->a_ops = &yaffs_file_address_operations;
489 case S_IFDIR: // directory
490 inode->i_op = &yaffs_dir_inode_operations;
491 inode->i_fop = &yaffs_dir_operations;
493 case S_IFLNK: // symlink
494 inode->i_op = &yaffs_symlink_inode_operations;
499 inode->u.generic_ip = obj;
500 obj->myInode = inode;
505 T((KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
510 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
512 struct inode * inode;
514 T((KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
516 inode = iget(sb,obj->objectId);
518 // NB Side effect: iget calls back to yaffs_read_inode().
519 // iget also increments the inode's i_count
524 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
531 T((KERN_DEBUG"yaffs_file_read\n"));
533 obj = yaffs_DentryToObject(f->f_dentry);
537 yaffs_GrossLock(dev);
539 inode = f->f_dentry->d_inode;
541 if (*pos < inode->i_size)
543 if (*pos + n > inode->i_size)
545 n = inode->i_size - *pos;
553 nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
559 yaffs_GrossUnlock(dev);
563 T((KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
569 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
577 obj = yaffs_DentryToObject(f->f_dentry);
581 yaffs_GrossLock(dev);
583 inode = f->f_dentry->d_inode;
585 if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
587 ipos = inode->i_size;
597 T((KERN_DEBUG"yaffs_file_write: hey obj is null!\n"));
601 T((KERN_DEBUG"yaffs_file_write about to write writing %d bytes to object %d at %d\n",n,obj->objectId,ipos));
604 nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n);
606 T((KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
611 if(ipos > inode->i_size)
613 inode->i_size = ipos;
614 inode->i_blocks = (ipos + inode->i_blksize - 1)/ inode->i_blksize;
616 T((KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,inode->i_blocks));
620 yaffs_GrossUnlock(dev);
622 return nWritten != n ? -ENOSPC : nWritten;
626 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
630 struct inode *inode = f->f_dentry->d_inode;
631 unsigned long offset, curoffs;
635 char name[YAFFS_MAX_NAME_LENGTH +1];
637 obj = yaffs_DentryToObject(f->f_dentry);
640 yaffs_GrossLock(dev);
644 T(("yaffs_readdir: starting at %d\n",(int)offset));
648 T((KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
649 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
658 T((KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
659 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
669 list_for_each(i,&obj->variant.directoryVariant.children)
672 if(curoffs >= offset)
674 l = list_entry(i, yaffs_Object,siblings);
676 yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1);
677 T((KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
683 yaffs_GetObjectInode(l),
684 yaffs_GetObjectType(l))
698 yaffs_GrossUnlock(dev);
705 * File creation. Allocate an inode, and we're done..
707 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
711 yaffs_Object *obj = NULL;
714 yaffs_Object *parent = yaffs_InodeToObject(dir);
720 T((KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
721 parent->objectId,parent->variantType));
725 T((KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
729 T(("yaffs_mknod: making oject for %s, mode %x dev %x\n",
730 dentry->d_name.name, mode,dev));
734 yaffs_GrossLock(dev);
736 switch (mode & S_IFMT)
739 // Special (socket, fifo, device...)
740 T((KERN_DEBUG"yaffs_mknod: making special\n"));
741 obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,dev);
743 case S_IFREG: // file
744 T((KERN_DEBUG"yaffs_mknod: making file\n"));
745 obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
747 case S_IFDIR: // directory
748 T((KERN_DEBUG"yaffs_mknod: making directory\n"));
749 obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
751 case S_IFLNK: // symlink
752 T((KERN_DEBUG"yaffs_mknod: making file\n"));
753 obj = NULL; // Do we ever get here?
759 inode = yaffs_get_inode(dir->i_sb, mode, dev, obj);
760 d_instantiate(dentry, inode);
761 T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
766 T((KERN_DEBUG"yaffs_mknod failed making object\n"));
770 yaffs_GrossUnlock(dev);
775 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
778 T((KERN_DEBUG"yaffs_mkdir\n"));
779 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
781 // attempt to fix dir bug - didn't work
790 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
792 T((KERN_DEBUG"yaffs_create\n"));
793 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
797 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
805 T((KERN_DEBUG"yaffs_unlink %d:%s\n",dir->i_ino,dentry->d_name.name));
807 dev = yaffs_InodeToObject(dir)->myDev;
809 yaffs_GrossLock(dev);
813 retVal = yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name);
816 yaffs_GrossUnlock(dev);
818 if( retVal == YAFFS_OK)
820 dentry->d_inode->i_nlink--;
833 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
835 struct inode *inode = old_dentry->d_inode;
836 yaffs_Object *obj = NULL;
837 yaffs_Object *link=NULL;
840 T((KERN_DEBUG"yaffs_link\n"));
842 obj = yaffs_InodeToObject(inode);
845 yaffs_GrossLock(dev);
847 link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
852 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
853 d_instantiate(dentry, old_dentry->d_inode);
854 atomic_inc(&old_dentry->d_inode->i_count);
855 T((KERN_DEBUG"yaffs_link link count %d i_count %d\n",
856 old_dentry->d_inode->i_nlink,atomic_read(&old_dentry->d_inode->i_count)));
860 yaffs_GrossUnlock(dev);
874 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
879 T((KERN_DEBUG"yaffs_symlink\n"));
881 dev = yaffs_InodeToObject(dir)->myDev;
882 yaffs_GrossLock(dev);
883 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
884 S_IFLNK | S_IRWXUGO, current->uid, current->gid,
886 yaffs_GrossUnlock(dev);
890 T((KERN_DEBUG"symlink created OK\n"));
895 T((KERN_DEBUG"symlink not created\n"));
902 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
908 obj = yaffs_DentryToObject(dentry);
912 T((KERN_DEBUG"yaffs_sync_object\n"));
913 yaffs_GrossLock(dev);
914 yaffs_FlushFile(obj);
915 yaffs_GrossUnlock(dev);
920 * The VFS layer already does all the dentry stuff for rename.
922 * NB: POSIX says you can rename an object over an old object of the same name
924 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
929 dev = yaffs_InodeToObject(old_dir)->myDev;
931 yaffs_GrossLock(dev);
933 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
934 yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
935 yaffs_GrossUnlock(dev);
937 if(retVal == YAFFS_OK)
949 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
951 struct inode *inode = dentry->d_inode;
955 T((KERN_DEBUG"yaffs_setattr of object %d\n",yaffs_InodeToObject(inode)->objectId));
957 if((error = inode_change_ok(inode,attr)) == 0)
960 dev = yaffs_InodeToObject(inode)->myDev;
961 yaffs_GrossLock(dev);
962 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
970 yaffs_GrossUnlock(dev);
971 inode_setattr(inode,attr);
976 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
978 yaffs_Device *dev = yaffs_SuperToDevice(sb);
979 T((KERN_DEBUG"yaffs_statfs\n"));
981 yaffs_GrossLock(dev);
983 buf->f_type = YAFFS_MAGIC;
984 buf->f_bsize = sb->s_blocksize;
985 buf->f_namelen = 255;
986 buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * YAFFS_CHUNKS_PER_BLOCK/
987 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
990 buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev)/
991 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
992 buf->f_bavail = buf->f_bfree;
994 yaffs_GrossUnlock(dev);
998 static void yaffs_read_inode (struct inode *inode)
1000 // NB This is called as a side effect of other functions and
1001 // thus gross locking should always be in place already.
1004 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1006 T((KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
1008 obj = yaffs_FindObjectByNumber(dev,inode->i_ino);
1010 yaffs_FillInodeFromObject(inode,obj);
1016 static yaffs_Device *yaffs_dev;
1017 static yaffs_Device *yaffsram_dev;
1021 static void yaffs_put_super(struct super_block *sb)
1023 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1025 yaffs_GrossLock(dev);
1026 if(dev->putSuperFunc)
1028 dev->putSuperFunc(sb);
1030 yaffs_Deinitialise(dev);
1031 yaffs_GrossUnlock(dev);
1033 if(dev == yaffs_dev) yaffs_dev = NULL;
1034 if(dev == yaffsram_dev) yaffsram_dev = NULL;
1040 #ifdef CONFIG_YAFFS_MTD_ENABLED
1042 static void yaffs_MTDPutSuper(struct super_block *sb)
1045 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1052 put_mtd_device(mtd);
1058 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
1061 struct inode * inode;
1062 struct dentry * root;
1065 sb->s_magic = YAFFS_MAGIC;
1066 sb->s_op = &yaffs_super_ops;
1069 printk(KERN_INFO"yaffs: sb is NULL\n");
1071 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
1072 else if(! kdevname(sb->s_dev))
1073 printk(KERN_INFO"yaffs: kdevname is NULL\n");
1075 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
1079 #ifdef CONFIG_YAFFS_USE_CHUNK_SIZE
1080 sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
1081 sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
1083 sb->s_blocksize = PAGE_CACHE_SIZE;
1084 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1086 T(("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",sb->s_blocksize));
1088 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1089 T(("yaffs: Write verification disabled. All guarantees null and void\n");
1097 #ifdef CONFIG_YAFFS_RAM_ENABLED
1098 // Set the yaffs_Device up for ram emulation
1100 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1103 // Deep shit could not allocate device structure
1104 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1108 memset(dev,0,sizeof(yaffs_Device));
1109 dev->genericDevice = NULL; // Not used for RAM emulation.
1111 nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1112 dev->startBlock = 1; // Don't use block 0
1113 dev->endBlock = nBlocks - 1;
1115 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
1116 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
1117 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
1118 dev->initialiseNAND = nandemul_InitialiseNAND;
1127 #ifdef CONFIG_YAFFS_MTD_ENABLED
1128 struct mtd_info *mtd;
1130 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
1131 MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
1133 // Hope it's a NAND mtd
1134 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1137 printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
1141 if(mtd->type != MTD_NANDFLASH)
1143 printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
1147 printk(KERN_DEBUG" erase %x\n",mtd->erase);
1148 printk(KERN_DEBUG" read %x\n",mtd->read);
1149 printk(KERN_DEBUG" write %x\n",mtd->write);
1150 printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
1151 printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
1152 printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
1153 printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
1162 printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
1166 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
1167 mtd->oobsize != YAFFS_BYTES_PER_SPARE)
1169 printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
1174 // OK, so if we got here, we have an MTD that's NAND and looks
1175 // like it has the right capabilities
1176 // Set the yaffs_Device up for ram emulation
1178 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1181 // Deep shit could not allocate device structure
1182 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1186 memset(dev,0,sizeof(yaffs_Device));
1187 dev->genericDevice = mtd;
1189 // Set up the memory size parameters....
1191 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1192 dev->startBlock = 1; // Don't use block 0
1193 dev->endBlock = nBlocks - 1;
1195 // ... and the functions.
1196 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1197 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1198 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1199 dev->initialiseNAND = nandmtd_InitialiseNAND;
1201 dev->putSuperFunc = yaffs_MTDPutSuper;
1208 init_MUTEX(&dev->grossLock);
1211 yaffs_GrossLock(dev);
1212 yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
1214 T(("yaffs_read_super: guts initialised\n"));
1216 // Create root inode
1217 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
1219 yaffs_GrossUnlock(dev);
1224 T(("yaffs_read_super: got root inode\n"));
1227 root = d_alloc_root(inode);
1229 T(("yaffs_read_super: d_alloc_root done\n"));
1237 T(("yaffs_read_super: done\n"));
1241 #ifdef CONFIG_YAFFS_MTD_ENABLED
1242 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
1244 return yaffs_internal_read_super(0,sb,data,silent);
1247 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1250 #ifdef CONFIG_YAFFS_RAM_ENABLED
1252 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
1254 return yaffs_internal_read_super(1,sb,data,silent);
1257 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
1258 #endif // CONFIG_YAFFS_RAM_ENABLED
1261 static struct proc_dir_entry *my_proc_entry;
1262 static struct proc_dir_entry *my_proc_ram_write_entry;
1264 static char * yaffs_dump_dev(char *buf,yaffs_Device *dev,char *name)
1266 buf +=sprintf(buf,"\nDevice %s\n",name);
1267 buf +=sprintf(buf,"startBlock......... %d\n",dev->startBlock);
1268 buf +=sprintf(buf,"endBlock........... %d\n",dev->endBlock);
1269 buf +=sprintf(buf,"chunkGroupBits..... %d\n",dev->chunkGroupBits);
1270 buf +=sprintf(buf,"chunkGroupSize..... %d\n",dev->chunkGroupSize);
1271 buf +=sprintf(buf,"nErasedBlocks...... %d\n",dev->nErasedBlocks);
1272 buf +=sprintf(buf,"nTnodesCreated..... %d\n",dev->nTnodesCreated);
1273 buf +=sprintf(buf,"nFreeTnodes........ %d\n",dev->nFreeTnodes);
1274 buf +=sprintf(buf,"nObjectsCreated.... %d\n",dev->nObjectsCreated);
1275 buf +=sprintf(buf,"nFreeObjects....... %d\n",dev->nFreeObjects);
1276 buf +=sprintf(buf,"nFreeChunks........ %d\n",dev->nFreeChunks);
1277 buf +=sprintf(buf,"nPageWrites........ %d\n",dev->nPageWrites);
1278 buf +=sprintf(buf,"nPageReads......... %d\n",dev->nPageReads);
1279 buf +=sprintf(buf,"nBlockErasures..... %d\n",dev->nBlockErasures);
1280 buf +=sprintf(buf,"nGCCopies.......... %d\n",dev->nGCCopies);
1281 buf +=sprintf(buf,"garbageCollections. %d\n",dev->garbageCollections);
1282 buf +=sprintf(buf,"nRetriedWrites..... %d\n",dev->nRetriedWrites);
1283 buf +=sprintf(buf,"nRetireBlocks...... %d\n",dev->nRetiredBlocks);
1284 buf +=sprintf(buf,"cacheHits.......... %d\n",dev->cacheHits);
1285 buf +=sprintf(buf,"nDeletedFiles...... %d\n",dev->nDeletedFiles);
1286 buf +=sprintf(buf,"nUnlinkedFiles..... %d\n",dev->nUnlinkedFiles);
1287 buf +=sprintf(buf,"nBackgroudDeletions %d\n",dev->nBackgroundDeletions);
1292 static int yaffs_proc_read(
1302 char my_buffer[2000];
1306 if (offset > 0) return 0;
1308 /* Fill the buffer and get its length */
1309 buf +=sprintf(buf,"YAFFS built:"__DATE__ " "__TIME__"\n");
1311 if(yaffs_dev) buf = yaffs_dump_dev(buf,yaffs_dev,"yaffs");
1312 if(yaffsram_dev) buf = yaffs_dump_dev(buf,yaffsram_dev,"yaffsram");
1315 strcpy(page,my_buffer);
1316 return strlen(my_buffer);
1320 static int yaffs_proc_ram_write(
1330 printk(KERN_DEBUG "yaffs write size %d\n",count);
1334 static int __init init_yaffs_fs(void)
1338 yaffs_dev = yaffsram_dev = NULL;
1340 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
1341 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
1342 printk(KERN_DEBUG "yaffs is using generic read/write (caching)\n");
1344 printk(KERN_DEBUG "yaffs is using direct read/write (uncached)\n");
1348 /* Install the proc_fs entry */
1349 my_proc_entry = create_proc_read_entry("yaffs",
1359 #ifdef CONFIG_YAFFS_RAM_ENABLED
1361 my_proc_ram_write_entry = create_proc_entry("yaffs_ram",
1365 if(!my_proc_ram_write_entry)
1371 my_proc_ram_write_entry->write_proc = yaffs_proc_ram_write;
1373 error = register_filesystem(&yaffs_ram_fs_type);
1378 #endif //CONFIG_YAFFS_RAM_ENABLED
1380 #ifdef CONFIG_YAFFS_MTD_ENABLED
1381 error = register_filesystem(&yaffs_fs_type);
1384 #ifdef CONFIG_YAFFS_RAM_ENABLED
1385 unregister_filesystem(&yaffs_ram_fs_type);
1386 #endif //CONFIG_YAFFS_RAM_ENABLED
1388 #endif // CONFIG_YAFFS_MTD_ENABLED
1393 static void __exit exit_yaffs_fs(void)
1395 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
1397 remove_proc_entry("yaffs",&proc_root);
1399 #ifdef CONFIG_YAFFS_RAM_ENABLED
1400 unregister_filesystem(&yaffs_ram_fs_type);
1402 #ifdef CONFIG_YAFFS_MTD_ENABLED
1403 unregister_filesystem(&yaffs_fs_type);
1408 module_init(init_yaffs_fs)
1409 module_exit(exit_yaffs_fs)
1411 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
1412 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
1413 MODULE_LICENSE("GPL");