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 * * Andras Toth for mknod rdev issue.
26 * * Some code bodily lifted from JFFS2.
30 const char *yaffs_fs_c_version = "$Id: yaffs_fs.c,v 1.17 2002-10-02 02:11:25 charles Exp $";
31 extern const char *yaffs_guts_c_version;
33 #include <linux/config.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/version.h>
37 #include <linux/slab.h>
38 #include <linux/init.h>
39 #include <linux/list.h>
41 #include <linux/proc_fs.h>
42 #include <linux/pagemap.h>
43 #include <linux/mtd/mtd.h>
44 #include <linux/interrupt.h>
45 #include <linux/string.h>
46 #include <linux/locks.h>
48 #include <asm/uaccess.h>
50 #include "yaffs_guts.h"
52 #ifdef CONFIG_YAFFS_RAM_ENABLED
53 #include "yaffs_nandemul.h"
54 // 2 MB of RAM for emulation
55 #define YAFFS_RAM_EMULATION_SIZE 0x200000
56 #endif //CONFIG_YAFFS_RAM_ENABLED
58 #ifdef CONFIG_YAFFS_MTD_ENABLED
59 #include <linux/mtd/mtd.h>
60 #include "yaffs_mtdif.h"
61 #endif //CONFIG_YAFFS_MTD_ENABLED
67 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
68 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
69 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
73 static void yaffs_put_super(struct super_block *sb);
75 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos);
76 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos);
77 static int yaffs_file_flush(struct file* file);
79 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync);
81 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
83 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
84 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry);
85 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry);
86 static int yaffs_unlink(struct inode * dir, struct dentry *dentry);
87 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname);
88 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode);
89 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev);
90 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry);
91 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
93 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
94 static void yaffs_read_inode (struct inode *inode);
95 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);
96 static void yaffs_put_inode (struct inode *inode);
97 static void yaffs_delete_inode(struct inode *);
98 static void yaffs_clear_inode(struct inode *);
100 static int yaffs_readpage(struct file *file, struct page * page);
102 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
103 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
105 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen);
106 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
111 static struct address_space_operations yaffs_file_address_operations = {
112 readpage: yaffs_readpage,
113 prepare_write: yaffs_prepare_write,
114 commit_write: yaffs_commit_write
118 static struct file_operations yaffs_file_operations = {
119 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
120 read: generic_file_read,
121 write: generic_file_write,
123 read: yaffs_file_read,
124 write: yaffs_file_write,
126 mmap: generic_file_mmap,
127 flush: yaffs_file_flush,
128 fsync: yaffs_sync_object,
132 static struct inode_operations yaffs_file_inode_operations = {
133 setattr: yaffs_setattr,
137 struct inode_operations yaffs_symlink_inode_operations =
139 readlink: yaffs_readlink,
140 follow_link: yaffs_follow_link,
141 setattr: yaffs_setattr
144 static struct inode_operations yaffs_dir_inode_operations = {
145 create: yaffs_create,
146 lookup: yaffs_lookup,
148 unlink: yaffs_unlink,
149 symlink: yaffs_symlink,
153 rename: yaffs_rename,
154 setattr: yaffs_setattr,
157 static struct file_operations yaffs_dir_operations = {
158 read: generic_read_dir,
159 readdir: yaffs_readdir,
160 fsync: yaffs_sync_object,
164 static struct super_operations yaffs_super_ops = {
165 statfs: yaffs_statfs,
166 read_inode: yaffs_read_inode,
167 put_inode: yaffs_put_inode,
168 put_super: yaffs_put_super,
170 delete_inode: yaffs_delete_inode,
171 clear_inode: yaffs_clear_inode,
176 static void yaffs_GrossLock(yaffs_Device *dev)
178 T((KERN_DEBUG"yaffs locking\n"));
180 down(&dev->grossLock);
183 static void yaffs_GrossUnlock(yaffs_Device *dev)
185 T((KERN_DEBUG"yaffs unlocking\n"));
190 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen)
192 unsigned char *alias;
195 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
198 yaffs_GrossLock(dev);
200 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
202 yaffs_GrossUnlock(dev);
207 ret = vfs_readlink(dentry, buffer, buflen, alias);
212 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
214 unsigned char *alias;
216 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
219 yaffs_GrossLock(dev);
221 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
223 yaffs_GrossUnlock(dev);
228 ret = vfs_follow_link(nd,alias);
234 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj);
237 * Lookup is used to find objects in the fs
239 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
244 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
247 yaffs_GrossLock(dev);
250 T((KERN_DEBUG"yaffs_lookup for %d:%s\n",yaffs_InodeToObject(dir)->objectId,dentry->d_name.name));
252 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),dentry->d_name.name);
254 obj = yaffs_GetEquivalentObject(obj); // in case it was a hardlink
260 T((KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
262 inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj);
266 T((KERN_DEBUG"yaffs_loookup dentry \n"));
267 //dget(dentry); // try to solve directory bug
270 yaffs_GrossUnlock(dev);
279 T((KERN_DEBUG"yaffs_lookup not found\n"));
282 yaffs_GrossUnlock(dev);
285 // return (ERR_PTR(-EIO));
289 // For now put inode is just for debugging
290 // Put inode is called when the inode **structure** is put.
291 static void yaffs_put_inode(struct inode *inode)
293 T(("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count)));
297 // clear is called to tell the fs to release any per-inode data it holds
298 static void yaffs_clear_inode(struct inode *inode)
300 yaffs_Object *obj = yaffs_InodeToObject(inode);
302 T(("yaffs_clear_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count),
303 obj ? "object exists" : "null object"));
308 inode->u.generic_ip = NULL;
314 // delete is called when the link count is zero and the inode
315 // is put (ie. nobody wants to know about it anymore, time to
317 // NB Must call clear_inode()
318 static void yaffs_delete_inode(struct inode *inode)
320 yaffs_Object *obj = yaffs_InodeToObject(inode);
323 T(("yaffs_delete_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count),
324 obj ? "object exists" : "null object"));
329 yaffs_GrossLock(dev);
330 yaffs_DeleteFile(obj);
331 yaffs_GrossUnlock(dev);
337 static int yaffs_file_flush(struct file* file)
339 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
341 yaffs_Device *dev = obj->myDev;
343 T((KERN_DEBUG"yaffs_file_flush object %d (%s)\n",obj->objectId,
344 obj->dirty ? "dirty" : "clean"));
346 yaffs_GrossLock(dev);
348 yaffs_FlushFile(obj);
350 yaffs_GrossUnlock(dev);
357 static int yaffs_readpage_nolock(struct file *f, struct page * pg)
362 unsigned char *pg_buf;
367 T((KERN_DEBUG"yaffs_readpage at %08x, size %08x\n",
368 (unsigned)(pg->index << PAGE_CACHE_SHIFT), (unsigned)PAGE_CACHE_SIZE));
370 obj = yaffs_DentryToObject(f->f_dentry);
379 /* FIXME: Can kmap fail? */
381 yaffs_GrossLock(dev);
383 ret = yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
385 yaffs_GrossUnlock(dev);
387 if(ret >= 0) ret = 0;
390 ClearPageUptodate(pg);
397 flush_dcache_page(pg);
401 T((KERN_DEBUG"yaffs_readpage done\n"));
405 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
407 int ret = yaffs_readpage_nolock(f,pg);
412 static int yaffs_readpage(struct file *f, struct page * pg)
414 return yaffs_readpage_unlock(f,pg);
418 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
421 T((KERN_DEBUG"yaffs_prepair_write\n"));
422 if(!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
423 return yaffs_readpage_nolock(f,pg);
429 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
432 void *addr = page_address(pg) + offset;
433 loff_t pos = (((loff_t)pg->index) << PAGE_CACHE_SHIFT) + offset;
434 int nBytes = to - offset;
438 unsigned saddr = (unsigned)addr;
440 T((KERN_DEBUG"yaffs_commit_write addr %x pos %x nBytes %d\n",saddr,spos,nBytes));
442 nWritten = yaffs_file_write(f,addr, nBytes, &pos);
444 if(nWritten != nBytes)
446 T((KERN_DEBUG"yaffs_commit_write not same size nWritten %d nBytes %d\n",nWritten,nBytes));
448 ClearPageUptodate(pg);
455 T((KERN_DEBUG"yaffs_commit_write returning %d\n",nWritten));
463 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
467 inode->i_ino = obj->objectId;
468 inode->i_mode = obj->st_mode;
469 inode->i_uid = obj->st_uid;
470 inode->i_gid = obj->st_gid;
471 inode->i_blksize = inode->i_sb->s_blocksize;
473 inode->i_rdev = obj->st_rdev;;
474 inode->i_atime = obj->st_atime;
475 inode->i_mtime = obj->st_mtime;
476 inode->i_ctime = obj->st_ctime;
477 inode->i_size = yaffs_GetObjectFileLength(obj);
478 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
480 T((KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
481 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
483 switch (obj->st_mode & S_IFMT)
485 default: // fifo, device or socket
486 init_special_inode(inode, obj->st_mode, obj->st_rdev);
488 case S_IFREG: // file
489 inode->i_op = &yaffs_file_inode_operations;
490 inode->i_fop = &yaffs_file_operations;
491 inode->i_mapping->a_ops = &yaffs_file_address_operations;
493 case S_IFDIR: // directory
494 inode->i_op = &yaffs_dir_inode_operations;
495 inode->i_fop = &yaffs_dir_operations;
497 case S_IFLNK: // symlink
498 inode->i_op = &yaffs_symlink_inode_operations;
503 inode->u.generic_ip = obj;
504 obj->myInode = inode;
509 T((KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
514 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
516 struct inode * inode;
518 T((KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
520 inode = iget(sb,obj->objectId);
522 // NB Side effect: iget calls back to yaffs_read_inode().
523 // iget also increments the inode's i_count
528 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
535 T((KERN_DEBUG"yaffs_file_read\n"));
537 obj = yaffs_DentryToObject(f->f_dentry);
541 yaffs_GrossLock(dev);
543 inode = f->f_dentry->d_inode;
545 if (*pos < inode->i_size)
547 if (*pos + n > inode->i_size)
549 n = inode->i_size - *pos;
557 nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
563 yaffs_GrossUnlock(dev);
567 T((KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
573 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
581 obj = yaffs_DentryToObject(f->f_dentry);
585 yaffs_GrossLock(dev);
587 inode = f->f_dentry->d_inode;
589 if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
591 ipos = inode->i_size;
601 T((KERN_DEBUG"yaffs_file_write: hey obj is null!\n"));
605 T((KERN_DEBUG"yaffs_file_write about to write writing %d bytes to object %d at %d\n",n,obj->objectId,ipos));
608 nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n);
610 T((KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
615 if(ipos > inode->i_size)
617 inode->i_size = ipos;
618 inode->i_blocks = (ipos + inode->i_blksize - 1)/ inode->i_blksize;
620 T((KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,(int)(inode->i_blocks)));
624 yaffs_GrossUnlock(dev);
626 return nWritten != n ? -ENOSPC : nWritten;
630 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
634 struct inode *inode = f->f_dentry->d_inode;
635 unsigned long offset, curoffs;
639 char name[YAFFS_MAX_NAME_LENGTH +1];
641 obj = yaffs_DentryToObject(f->f_dentry);
644 yaffs_GrossLock(dev);
648 T(("yaffs_readdir: starting at %d\n",(int)offset));
652 T((KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
653 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
662 T((KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
663 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
673 list_for_each(i,&obj->variant.directoryVariant.children)
676 if(curoffs >= offset)
678 l = list_entry(i, yaffs_Object,siblings);
680 yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1);
681 T((KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
687 yaffs_GetObjectInode(l),
688 yaffs_GetObjectType(l))
702 yaffs_GrossUnlock(dev);
709 * File creation. Allocate an inode, and we're done..
711 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
715 yaffs_Object *obj = NULL;
718 yaffs_Object *parent = yaffs_InodeToObject(dir);
724 T((KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
725 parent->objectId,parent->variantType));
729 T((KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
733 T(("yaffs_mknod: making oject for %s, mode %x dev %x\n",
734 dentry->d_name.name, mode,rdev));
738 yaffs_GrossLock(dev);
740 switch (mode & S_IFMT)
743 // Special (socket, fifo, device...)
744 T((KERN_DEBUG"yaffs_mknod: making special\n"));
745 obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,rdev);
747 case S_IFREG: // file
748 T((KERN_DEBUG"yaffs_mknod: making file\n"));
749 obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
751 case S_IFDIR: // directory
752 T((KERN_DEBUG"yaffs_mknod: making directory\n"));
753 obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
755 case S_IFLNK: // symlink
756 T((KERN_DEBUG"yaffs_mknod: making file\n"));
757 obj = NULL; // Do we ever get here?
763 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
764 d_instantiate(dentry, inode);
765 T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
770 T((KERN_DEBUG"yaffs_mknod failed making object\n"));
774 yaffs_GrossUnlock(dev);
779 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
782 T((KERN_DEBUG"yaffs_mkdir\n"));
783 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
785 // attempt to fix dir bug - didn't work
794 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
796 T((KERN_DEBUG"yaffs_create\n"));
797 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
801 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
809 T((KERN_DEBUG"yaffs_unlink %d:%s\n",(int)(dir->i_ino),dentry->d_name.name));
811 dev = yaffs_InodeToObject(dir)->myDev;
813 yaffs_GrossLock(dev);
816 retVal = yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name);
819 yaffs_GrossUnlock(dev);
821 if( retVal == YAFFS_OK)
823 dentry->d_inode->i_nlink--;
836 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
838 struct inode *inode = old_dentry->d_inode;
839 yaffs_Object *obj = NULL;
840 yaffs_Object *link=NULL;
843 T((KERN_DEBUG"yaffs_link\n"));
845 obj = yaffs_InodeToObject(inode);
848 yaffs_GrossLock(dev);
850 link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
855 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
856 d_instantiate(dentry, old_dentry->d_inode);
857 atomic_inc(&old_dentry->d_inode->i_count);
858 T((KERN_DEBUG"yaffs_link link count %d i_count %d\n",
859 old_dentry->d_inode->i_nlink,atomic_read(&old_dentry->d_inode->i_count)));
863 yaffs_GrossUnlock(dev);
877 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
882 T((KERN_DEBUG"yaffs_symlink\n"));
884 dev = yaffs_InodeToObject(dir)->myDev;
885 yaffs_GrossLock(dev);
886 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
887 S_IFLNK | S_IRWXUGO, current->uid, current->gid,
889 yaffs_GrossUnlock(dev);
893 T((KERN_DEBUG"symlink created OK\n"));
898 T((KERN_DEBUG"symlink not created\n"));
905 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
911 obj = yaffs_DentryToObject(dentry);
915 T((KERN_DEBUG"yaffs_sync_object\n"));
916 yaffs_GrossLock(dev);
917 yaffs_FlushFile(obj);
918 yaffs_GrossUnlock(dev);
923 * The VFS layer already does all the dentry stuff for rename.
925 * NB: POSIX says you can rename an object over an old object of the same name
927 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
932 dev = yaffs_InodeToObject(old_dir)->myDev;
934 yaffs_GrossLock(dev);
936 // Unlink the target if it exists
937 yaffs_Unlink(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
940 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
941 yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
942 yaffs_GrossUnlock(dev);
944 if(retVal == YAFFS_OK)
956 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
958 struct inode *inode = dentry->d_inode;
962 T((KERN_DEBUG"yaffs_setattr of object %d\n",yaffs_InodeToObject(inode)->objectId));
964 if((error = inode_change_ok(inode,attr)) == 0)
967 dev = yaffs_InodeToObject(inode)->myDev;
968 yaffs_GrossLock(dev);
969 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
977 yaffs_GrossUnlock(dev);
978 inode_setattr(inode,attr);
983 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
985 yaffs_Device *dev = yaffs_SuperToDevice(sb);
986 T((KERN_DEBUG"yaffs_statfs\n"));
988 yaffs_GrossLock(dev);
990 buf->f_type = YAFFS_MAGIC;
991 buf->f_bsize = sb->s_blocksize;
992 buf->f_namelen = 255;
993 buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * YAFFS_CHUNKS_PER_BLOCK/
994 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
997 buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev)/
998 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
999 buf->f_bavail = buf->f_bfree;
1001 yaffs_GrossUnlock(dev);
1005 static void yaffs_read_inode (struct inode *inode)
1007 // NB This is called as a side effect of other functions and
1008 // thus gross locking should always be in place already.
1011 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1013 T((KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
1015 obj = yaffs_FindObjectByNumber(dev,inode->i_ino);
1017 yaffs_FillInodeFromObject(inode,obj);
1023 static yaffs_Device *yaffs_dev;
1024 static yaffs_Device *yaffsram_dev;
1028 static void yaffs_put_super(struct super_block *sb)
1030 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1032 yaffs_GrossLock(dev);
1033 if(dev->putSuperFunc)
1035 dev->putSuperFunc(sb);
1037 yaffs_Deinitialise(dev);
1038 yaffs_GrossUnlock(dev);
1040 if(dev == yaffs_dev) yaffs_dev = NULL;
1041 if(dev == yaffsram_dev) yaffsram_dev = NULL;
1047 #ifdef CONFIG_YAFFS_MTD_ENABLED
1049 static void yaffs_MTDPutSuper(struct super_block *sb)
1052 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1059 put_mtd_device(mtd);
1065 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
1068 struct inode * inode;
1069 struct dentry * root;
1072 sb->s_magic = YAFFS_MAGIC;
1073 sb->s_op = &yaffs_super_ops;
1076 printk(KERN_INFO"yaffs: sb is NULL\n");
1078 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
1079 else if(! kdevname(sb->s_dev))
1080 printk(KERN_INFO"yaffs: kdevname is NULL\n");
1082 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
1086 #ifdef CONFIG_YAFFS_USE_CHUNK_SIZE
1087 sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
1088 sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
1090 sb->s_blocksize = PAGE_CACHE_SIZE;
1091 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1093 T(("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",(int)(sb->s_blocksize)));
1095 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1096 T(("yaffs: Write verification disabled. All guarantees null and void\n");
1104 #ifdef CONFIG_YAFFS_RAM_ENABLED
1105 // Set the yaffs_Device up for ram emulation
1107 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1110 // Deep shit could not allocate device structure
1111 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1115 memset(dev,0,sizeof(yaffs_Device));
1116 dev->genericDevice = NULL; // Not used for RAM emulation.
1118 nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1119 dev->startBlock = 1; // Don't use block 0
1120 dev->endBlock = nBlocks - 1;
1122 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
1123 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
1124 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
1125 dev->initialiseNAND = nandemul_InitialiseNAND;
1134 #ifdef CONFIG_YAFFS_MTD_ENABLED
1135 struct mtd_info *mtd;
1137 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
1138 MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
1140 // Hope it's a NAND mtd
1141 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1144 printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
1148 if(mtd->type != MTD_NANDFLASH)
1150 printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
1154 //printk(KERN_DEBUG" erase %x\n",mtd->erase);
1155 //printk(KERN_DEBUG" read %x\n",mtd->read);
1156 //printk(KERN_DEBUG" write %x\n",mtd->write);
1157 //printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
1158 //printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
1159 //printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
1160 //printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
1169 printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
1173 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
1174 mtd->oobsize != YAFFS_BYTES_PER_SPARE)
1176 printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
1181 // OK, so if we got here, we have an MTD that's NAND and looks
1182 // like it has the right capabilities
1183 // Set the yaffs_Device up for ram emulation
1185 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1188 // Deep shit could not allocate device structure
1189 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1193 memset(dev,0,sizeof(yaffs_Device));
1194 dev->genericDevice = mtd;
1196 // Set up the memory size parameters....
1198 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1199 dev->startBlock = 1; // Don't use block 0
1200 dev->endBlock = nBlocks - 1;
1202 // ... and the functions.
1203 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1204 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1205 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1206 dev->initialiseNAND = nandmtd_InitialiseNAND;
1208 dev->putSuperFunc = yaffs_MTDPutSuper;
1215 init_MUTEX(&dev->grossLock);
1218 yaffs_GrossLock(dev);
1219 yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
1221 T(("yaffs_read_super: guts initialised\n"));
1223 // Create root inode
1224 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
1226 yaffs_GrossUnlock(dev);
1231 T(("yaffs_read_super: got root inode\n"));
1234 root = d_alloc_root(inode);
1236 T(("yaffs_read_super: d_alloc_root done\n"));
1244 T(("yaffs_read_super: done\n"));
1248 #ifdef CONFIG_YAFFS_MTD_ENABLED
1249 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
1251 return yaffs_internal_read_super(0,sb,data,silent);
1254 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1257 #ifdef CONFIG_YAFFS_RAM_ENABLED
1259 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
1261 return yaffs_internal_read_super(1,sb,data,silent);
1264 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
1265 #endif // CONFIG_YAFFS_RAM_ENABLED
1268 static struct proc_dir_entry *my_proc_entry;
1269 static struct proc_dir_entry *my_proc_ram_write_entry;
1271 static char * yaffs_dump_dev(char *buf,yaffs_Device *dev,char *name)
1273 buf +=sprintf(buf,"\nDevice %s\n",name);
1274 buf +=sprintf(buf,"startBlock......... %d\n",dev->startBlock);
1275 buf +=sprintf(buf,"endBlock........... %d\n",dev->endBlock);
1276 buf +=sprintf(buf,"chunkGroupBits..... %d\n",dev->chunkGroupBits);
1277 buf +=sprintf(buf,"chunkGroupSize..... %d\n",dev->chunkGroupSize);
1278 buf +=sprintf(buf,"nErasedBlocks...... %d\n",dev->nErasedBlocks);
1279 buf +=sprintf(buf,"nTnodesCreated..... %d\n",dev->nTnodesCreated);
1280 buf +=sprintf(buf,"nFreeTnodes........ %d\n",dev->nFreeTnodes);
1281 buf +=sprintf(buf,"nObjectsCreated.... %d\n",dev->nObjectsCreated);
1282 buf +=sprintf(buf,"nFreeObjects....... %d\n",dev->nFreeObjects);
1283 buf +=sprintf(buf,"nFreeChunks........ %d\n",dev->nFreeChunks);
1284 buf +=sprintf(buf,"nPageWrites........ %d\n",dev->nPageWrites);
1285 buf +=sprintf(buf,"nPageReads......... %d\n",dev->nPageReads);
1286 buf +=sprintf(buf,"nBlockErasures..... %d\n",dev->nBlockErasures);
1287 buf +=sprintf(buf,"nGCCopies.......... %d\n",dev->nGCCopies);
1288 buf +=sprintf(buf,"garbageCollections. %d\n",dev->garbageCollections);
1289 buf +=sprintf(buf,"nRetriedWrites..... %d\n",dev->nRetriedWrites);
1290 buf +=sprintf(buf,"nRetireBlocks...... %d\n",dev->nRetiredBlocks);
1291 buf +=sprintf(buf,"eccFixed........... %d\n",dev->eccFixed);
1292 buf +=sprintf(buf,"eccUnfixed......... %d\n",dev->eccUnfixed);
1293 buf +=sprintf(buf,"tagsEccFixed....... %d\n",dev->tagsEccFixed);
1294 buf +=sprintf(buf,"tagsEccUnfixed..... %d\n",dev->tagsEccUnfixed);
1295 buf +=sprintf(buf,"cacheHits.......... %d\n",dev->cacheHits);
1296 buf +=sprintf(buf,"nDeletedFiles...... %d\n",dev->nDeletedFiles);
1297 buf +=sprintf(buf,"nUnlinkedFiles..... %d\n",dev->nUnlinkedFiles);
1298 buf +=sprintf(buf,"nBackgroudDeletions %d\n",dev->nBackgroundDeletions);
1303 static int yaffs_proc_read(
1313 char my_buffer[3000];
1317 if (offset > 0) return 0;
1319 /* Fill the buffer and get its length */
1320 buf +=sprintf(buf,"YAFFS built:"__DATE__ " "__TIME__"\n%s\n%s\n", yaffs_fs_c_version,yaffs_guts_c_version);
1322 if(yaffs_dev) buf = yaffs_dump_dev(buf,yaffs_dev,"yaffs");
1323 if(yaffsram_dev) buf = yaffs_dump_dev(buf,yaffsram_dev,"yaffsram");
1326 strcpy(page,my_buffer);
1327 return strlen(my_buffer);
1331 static int yaffs_proc_ram_write(
1341 printk(KERN_DEBUG "yaffs write size %d\n",count);
1345 static int __init init_yaffs_fs(void)
1349 yaffs_dev = yaffsram_dev = NULL;
1351 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
1352 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
1353 printk(KERN_DEBUG "yaffs is using generic read/write (caching)\n");
1355 printk(KERN_DEBUG "yaffs is using direct read/write (uncached)\n");
1359 /* Install the proc_fs entry */
1360 my_proc_entry = create_proc_read_entry("yaffs",
1370 #ifdef CONFIG_YAFFS_RAM_ENABLED
1372 my_proc_ram_write_entry = create_proc_entry("yaffs_ram",
1376 if(!my_proc_ram_write_entry)
1382 my_proc_ram_write_entry->write_proc = yaffs_proc_ram_write;
1386 error = register_filesystem(&yaffs_ram_fs_type);
1391 #endif //CONFIG_YAFFS_RAM_ENABLED
1393 #ifdef CONFIG_YAFFS_MTD_ENABLED
1394 error = register_filesystem(&yaffs_fs_type);
1397 #ifdef CONFIG_YAFFS_RAM_ENABLED
1398 unregister_filesystem(&yaffs_ram_fs_type);
1399 #endif //CONFIG_YAFFS_RAM_ENABLED
1401 #endif // CONFIG_YAFFS_MTD_ENABLED
1406 static void __exit exit_yaffs_fs(void)
1408 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
1410 remove_proc_entry("yaffs",&proc_root);
1412 #ifdef CONFIG_YAFFS_RAM_ENABLED
1413 unregister_filesystem(&yaffs_ram_fs_type);
1415 #ifdef CONFIG_YAFFS_MTD_ENABLED
1416 unregister_filesystem(&yaffs_fs_type);
1421 module_init(init_yaffs_fs)
1422 module_exit(exit_yaffs_fs)
1424 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
1425 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
1426 MODULE_LICENSE("GPL");