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 * * Nick Bane for 2.5/2.6 integration.
26 * * Andras Toth for mknod rdev issue.
27 * * Some code bodily lifted from JFFS2.
31 const char *yaffs_fs_c_version = "$Id: yaffs_fs.c,v 1.27 2003-08-16 08:17:32 charles Exp $";
32 extern const char *yaffs_guts_c_version;
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/version.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/list.h>
43 #include <linux/proc_fs.h>
44 #include <linux/pagemap.h>
45 #include <linux/mtd/mtd.h>
46 #include <linux/interrupt.h>
47 #include <linux/string.h>
50 #if defined(CONFIG_KERNEL_2_5)
51 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
52 #include <asm/statfs.h>
53 #define UnlockPage(p) unlock_page(p)
54 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
55 #define kdevname(x) cdevname(to_kdev_t(x))
57 #include <linux/locks.h>
61 #include <asm/uaccess.h>
64 #include "yaffs_guts.h"
69 unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS | YAFFS_TRACE_BAD_BLOCKS;
71 #ifdef CONFIG_YAFFS_RAM_ENABLED
72 #include "yaffs_nandemul.h"
73 // 2 MB of RAM for emulation
74 #define YAFFS_RAM_EMULATION_SIZE 0x200000
75 #endif //CONFIG_YAFFS_RAM_ENABLED
77 #ifdef CONFIG_YAFFS_MTD_ENABLED
78 #include <linux/mtd/mtd.h>
79 #include "yaffs_mtdif.h"
80 #endif //CONFIG_YAFFS_MTD_ENABLED
82 //#define T(x) printk x
86 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
87 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
88 //NCB #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
90 #if defined(CONFIG_KERNEL_2_5)
91 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
93 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
97 static void yaffs_put_super(struct super_block *sb);
99 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos);
100 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos);
101 static int yaffs_file_flush(struct file* file);
103 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync);
105 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
107 #if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */
108 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *n);
109 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *n);
111 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
112 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry);
114 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry);
115 static int yaffs_unlink(struct inode * dir, struct dentry *dentry);
116 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname);
117 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode);
119 #if defined(CONFIG_KERNEL_2_5)
120 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev);
122 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev);
124 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry);
125 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
127 #if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */
128 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
130 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
132 static void yaffs_read_inode (struct inode *inode);
134 #if defined(CONFIG_KERNEL_2_5)
135 static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data);
137 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);
140 static void yaffs_put_inode (struct inode *inode);
141 static void yaffs_delete_inode(struct inode *);
142 static void yaffs_clear_inode(struct inode *);
144 static int yaffs_readpage(struct file *file, struct page * page);
146 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
147 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to);
149 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen);
150 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
155 static struct address_space_operations yaffs_file_address_operations = {
156 readpage: yaffs_readpage,
157 prepare_write: yaffs_prepare_write,
158 commit_write: yaffs_commit_write
162 static struct file_operations yaffs_file_operations = {
163 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
164 read: generic_file_read,
165 write: generic_file_write,
167 read: yaffs_file_read,
168 write: yaffs_file_write,
170 mmap: generic_file_mmap,
171 flush: yaffs_file_flush,
172 fsync: yaffs_sync_object,
176 static struct inode_operations yaffs_file_inode_operations = {
177 setattr: yaffs_setattr,
181 struct inode_operations yaffs_symlink_inode_operations =
183 readlink: yaffs_readlink,
184 follow_link: yaffs_follow_link,
185 setattr: yaffs_setattr
188 static struct inode_operations yaffs_dir_inode_operations = {
189 create: yaffs_create,
190 lookup: yaffs_lookup,
192 unlink: yaffs_unlink,
193 symlink: yaffs_symlink,
197 rename: yaffs_rename,
198 setattr: yaffs_setattr,
201 static struct file_operations yaffs_dir_operations = {
202 read: generic_read_dir,
203 readdir: yaffs_readdir,
204 fsync: yaffs_sync_object,
208 static struct super_operations yaffs_super_ops = {
209 statfs: yaffs_statfs,
210 read_inode: yaffs_read_inode,
211 put_inode: yaffs_put_inode,
212 put_super: yaffs_put_super,
214 delete_inode: yaffs_delete_inode,
215 clear_inode: yaffs_clear_inode,
220 static void yaffs_GrossLock(yaffs_Device *dev)
222 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs locking\n"));
224 down(&dev->grossLock);
227 static void yaffs_GrossUnlock(yaffs_Device *dev)
229 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs unlocking\n"));
234 static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen)
236 unsigned char *alias;
239 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
242 yaffs_GrossLock(dev);
244 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
246 yaffs_GrossUnlock(dev);
251 ret = vfs_readlink(dentry, buffer, buflen, alias);
256 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
258 unsigned char *alias;
260 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
263 yaffs_GrossLock(dev);
265 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
267 yaffs_GrossUnlock(dev);
272 ret = vfs_follow_link(nd,alias);
278 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj);
281 * Lookup is used to find objects in the fs
283 #if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */
284 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *n)
286 static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry)
290 struct inode *inode = NULL; // NCB 2.5/2.6 needs NULL here
292 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
295 yaffs_GrossLock(dev);
298 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_lookup for %d:%s\n",yaffs_InodeToObject(dir)->objectId,dentry->d_name.name));
300 obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),dentry->d_name.name);
302 obj = yaffs_GetEquivalentObject(obj); // in case it was a hardlink
308 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId));
310 inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj);
314 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_loookup dentry \n"));
315 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to d_add even if NULL inode */
317 //dget(dentry); // try to solve directory bug
320 yaffs_GrossUnlock(dev);
330 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_lookup not found\n"));
333 yaffs_GrossUnlock(dev);
335 /* added NCB for 2.5/6 compatability - forces add even if inode is NULL which creates dentry hash*/
339 // return (ERR_PTR(-EIO));
343 // For now put inode is just for debugging
344 // Put inode is called when the inode **structure** is put.
345 static void yaffs_put_inode(struct inode *inode)
347 T(YAFFS_TRACE_OS,("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count)));
351 // clear is called to tell the fs to release any per-inode data it holds
352 static void yaffs_clear_inode(struct inode *inode)
354 yaffs_Object *obj = yaffs_InodeToObject(inode);
356 T(YAFFS_TRACE_OS,("yaffs_clear_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count),
357 obj ? "object exists" : "null object"));
362 inode->u.generic_ip = NULL;
368 // delete is called when the link count is zero and the inode
369 // is put (ie. nobody wants to know about it anymore, time to
371 // NB Must call clear_inode()
372 static void yaffs_delete_inode(struct inode *inode)
374 yaffs_Object *obj = yaffs_InodeToObject(inode);
377 T(YAFFS_TRACE_OS,("yaffs_delete_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count),
378 obj ? "object exists" : "null object"));
383 yaffs_GrossLock(dev);
384 yaffs_DeleteFile(obj);
385 yaffs_GrossUnlock(dev);
391 static int yaffs_file_flush(struct file* file)
393 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
395 yaffs_Device *dev = obj->myDev;
397 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_flush object %d (%s)\n",obj->objectId,
398 obj->dirty ? "dirty" : "clean"));
400 yaffs_GrossLock(dev);
402 yaffs_FlushFile(obj,1);
404 yaffs_GrossUnlock(dev);
411 static int yaffs_readpage_nolock(struct file *f, struct page * pg)
416 unsigned char *pg_buf;
421 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readpage at %08x, size %08x\n",
422 (unsigned)(pg->index << PAGE_CACHE_SHIFT), (unsigned)PAGE_CACHE_SIZE));
424 obj = yaffs_DentryToObject(f->f_dentry);
433 /* FIXME: Can kmap fail? */
435 yaffs_GrossLock(dev);
437 ret = yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
439 yaffs_GrossUnlock(dev);
441 if(ret >= 0) ret = 0;
444 ClearPageUptodate(pg);
451 flush_dcache_page(pg);
455 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readpage done\n"));
459 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
461 int ret = yaffs_readpage_nolock(f,pg);
466 static int yaffs_readpage(struct file *f, struct page * pg)
468 return yaffs_readpage_unlock(f,pg);
472 static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
475 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_prepair_write\n"));
476 if(!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
477 return yaffs_readpage_nolock(f,pg);
483 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to)
486 void *addr = page_address(pg) + offset;
487 loff_t pos = (((loff_t)pg->index) << PAGE_CACHE_SHIFT) + offset;
488 int nBytes = to - offset;
492 unsigned saddr = (unsigned)addr;
494 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write addr %x pos %x nBytes %d\n",saddr,spos,nBytes));
496 nWritten = yaffs_file_write(f,addr, nBytes, &pos);
498 if(nWritten != nBytes)
500 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write not same size nWritten %d nBytes %d\n",nWritten,nBytes));
502 ClearPageUptodate(pg);
509 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write returning %d\n",nWritten));
517 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
521 inode->i_ino = obj->objectId;
522 inode->i_mode = obj->st_mode;
523 inode->i_uid = obj->st_uid;
524 inode->i_gid = obj->st_gid;
525 inode->i_blksize = inode->i_sb->s_blocksize;
527 #if defined(CONFIG_KERNEL_2_5)
528 inode->i_rdev = to_kdev_t(obj->st_rdev);
529 inode->i_atime.tv_sec = (time_t)(obj->st_atime);
530 inode->i_atime.tv_nsec = 0;
531 inode->i_mtime.tv_sec = (time_t)obj->st_mtime;
532 inode->i_mtime.tv_nsec =0;
533 inode->i_ctime.tv_sec = (time_t)obj->st_ctime;
534 inode->i_ctime.tv_nsec = 0;
536 inode->i_rdev = obj->st_rdev;
537 inode->i_atime = obj->st_atime;
538 inode->i_mtime = obj->st_mtime;
539 inode->i_ctime = obj->st_ctime;
541 inode->i_size = yaffs_GetObjectFileLength(obj);
542 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
544 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
545 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
547 switch (obj->st_mode & S_IFMT)
549 default: // fifo, device or socket
550 init_special_inode(inode, obj->st_mode,(dev_t)(obj->st_rdev));
552 case S_IFREG: // file
553 inode->i_op = &yaffs_file_inode_operations;
554 inode->i_fop = &yaffs_file_operations;
555 inode->i_mapping->a_ops = &yaffs_file_address_operations;
557 case S_IFDIR: // directory
558 inode->i_op = &yaffs_dir_inode_operations;
559 inode->i_fop = &yaffs_dir_operations;
561 case S_IFLNK: // symlink
562 inode->i_op = &yaffs_symlink_inode_operations;
567 inode->u.generic_ip = obj;
568 obj->myInode = inode;
573 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
578 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
580 struct inode * inode;
582 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
584 inode = iget(sb,obj->objectId);
586 // NB Side effect: iget calls back to yaffs_read_inode().
587 // iget also increments the inode's i_count
592 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
599 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_read\n"));
601 obj = yaffs_DentryToObject(f->f_dentry);
605 yaffs_GrossLock(dev);
607 inode = f->f_dentry->d_inode;
609 if (*pos < inode->i_size)
611 if (*pos + n > inode->i_size)
613 n = inode->i_size - *pos;
621 nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
627 yaffs_GrossUnlock(dev);
631 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
637 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
645 obj = yaffs_DentryToObject(f->f_dentry);
649 yaffs_GrossLock(dev);
651 inode = f->f_dentry->d_inode;
653 if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
655 ipos = inode->i_size;
665 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write: hey obj is null!\n"));
669 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write about to write writing %d bytes to object %d at %d\n",n,obj->objectId,ipos));
672 nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n);
674 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
679 if(ipos > inode->i_size)
681 inode->i_size = ipos;
682 inode->i_blocks = (ipos + inode->i_blksize - 1)/ inode->i_blksize;
684 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,(int)(inode->i_blocks)));
688 yaffs_GrossUnlock(dev);
690 return nWritten != n ? -ENOSPC : nWritten;
694 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
698 struct inode *inode = f->f_dentry->d_inode;
699 unsigned long offset, curoffs;
703 char name[YAFFS_MAX_NAME_LENGTH +1];
705 obj = yaffs_DentryToObject(f->f_dentry);
708 yaffs_GrossLock(dev);
712 T(YAFFS_TRACE_OS,("yaffs_readdir: starting at %d\n",(int)offset));
716 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
717 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
726 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
727 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
737 list_for_each(i,&obj->variant.directoryVariant.children)
740 if(curoffs >= offset)
742 l = list_entry(i, yaffs_Object,siblings);
744 yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1);
745 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
751 yaffs_GetObjectInode(l),
752 yaffs_GetObjectType(l))
766 yaffs_GrossUnlock(dev);
773 * File creation. Allocate an inode, and we're done..
775 #if defined(CONFIG_KERNEL_2_5)
776 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
778 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
783 yaffs_Object *obj = NULL;
786 yaffs_Object *parent = yaffs_InodeToObject(dir);
792 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
793 parent->objectId,parent->variantType));
797 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
801 T(YAFFS_TRACE_OS,("yaffs_mknod: making oject for %s, mode %x dev %x\n",
802 dentry->d_name.name, mode,rdev));
806 yaffs_GrossLock(dev);
808 switch (mode & S_IFMT)
811 // Special (socket, fifo, device...)
812 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making special\n"));
813 obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,rdev);
815 case S_IFREG: // file
816 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
817 obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
819 case S_IFDIR: // directory
820 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making directory\n"));
821 obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
823 case S_IFLNK: // symlink
824 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
825 obj = NULL; // Do we ever get here?
831 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
832 d_instantiate(dentry, inode);
833 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
838 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod failed making object\n"));
842 yaffs_GrossUnlock(dev);
847 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
850 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mkdir\n"));
851 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
853 // attempt to fix dir bug - didn't work
862 #if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */
863 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *n)
865 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
868 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_create\n"));
869 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
873 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
880 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_unlink %d:%s\n",(int)(dir->i_ino),dentry->d_name.name));
882 dev = yaffs_InodeToObject(dir)->myDev;
884 yaffs_GrossLock(dev);
887 retVal = yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name);
890 yaffs_GrossUnlock(dev);
892 if( retVal == YAFFS_OK)
894 dentry->d_inode->i_nlink--;
895 mark_inode_dirty(dentry->d_inode);
908 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
910 struct inode *inode = old_dentry->d_inode;
911 yaffs_Object *obj = NULL;
912 yaffs_Object *link=NULL;
915 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link\n"));
917 obj = yaffs_InodeToObject(inode);
920 yaffs_GrossLock(dev);
922 if (!S_ISDIR(inode->i_mode)) // Don't link directories
924 link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
930 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
931 d_instantiate(dentry, old_dentry->d_inode);
932 atomic_inc(&old_dentry->d_inode->i_count);
933 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link link count %d i_count %d\n",
934 old_dentry->d_inode->i_nlink,atomic_read(&old_dentry->d_inode->i_count)));
938 yaffs_GrossUnlock(dev);
952 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
957 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_symlink\n"));
959 dev = yaffs_InodeToObject(dir)->myDev;
960 yaffs_GrossLock(dev);
961 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
962 S_IFLNK | S_IRWXUGO, current->uid, current->gid,
964 yaffs_GrossUnlock(dev);
971 inode = yaffs_get_inode(dir->i_sb, obj->st_mode, 0, obj);
972 d_instantiate(dentry, inode);
973 T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink created OK\n"));
978 T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink not created\n"));
985 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
991 obj = yaffs_DentryToObject(dentry);
995 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_sync_object\n"));
996 yaffs_GrossLock(dev);
997 yaffs_FlushFile(obj,1);
998 yaffs_GrossUnlock(dev);
1003 * The VFS layer already does all the dentry stuff for rename.
1005 * NB: POSIX says you can rename an object over an old object of the same name
1007 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
1010 int retVal = YAFFS_FAIL;
1012 yaffs_Object *target;
1014 dev = yaffs_InodeToObject(old_dir)->myDev;
1016 yaffs_GrossLock(dev);
1018 // Check if the target is an existing directory that is not empty.
1019 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1022 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1023 !list_empty(&target->variant.directoryVariant.children))
1025 retVal = YAFFS_FAIL;
1030 // Unlink the target if it exists
1031 removed = yaffs_Unlink(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1034 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
1035 yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1038 yaffs_GrossUnlock(dev);
1040 if(retVal == YAFFS_OK)
1042 if(removed == YAFFS_OK)
1044 new_dentry->d_inode->i_nlink--;
1045 mark_inode_dirty(new_dentry->d_inode);
1058 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1060 struct inode *inode = dentry->d_inode;
1064 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_setattr of object %d\n",yaffs_InodeToObject(inode)->objectId));
1066 if((error = inode_change_ok(inode,attr)) == 0)
1069 dev = yaffs_InodeToObject(inode)->myDev;
1070 yaffs_GrossLock(dev);
1071 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
1079 yaffs_GrossUnlock(dev);
1080 inode_setattr(inode,attr);
1085 #if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */
1086 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1088 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1091 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1092 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_statfs\n"));
1094 yaffs_GrossLock(dev);
1096 buf->f_type = YAFFS_MAGIC;
1097 buf->f_bsize = sb->s_blocksize;
1098 buf->f_namelen = 255;
1099 buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * YAFFS_CHUNKS_PER_BLOCK/
1100 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
1103 buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev)/
1104 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
1105 buf->f_bavail = buf->f_bfree;
1107 yaffs_GrossUnlock(dev);
1111 static void yaffs_read_inode (struct inode *inode)
1113 // NB This is called as a side effect of other functions and
1114 // thus gross locking should always be in place already.
1117 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1119 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
1121 obj = yaffs_FindObjectByNumber(dev,inode->i_ino);
1123 yaffs_FillInodeFromObject(inode,obj);
1129 static yaffs_Device *yaffs_dev;
1130 static yaffs_Device *yaffsram_dev;
1134 static void yaffs_put_super(struct super_block *sb)
1136 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1138 yaffs_GrossLock(dev);
1139 if(dev->putSuperFunc)
1141 dev->putSuperFunc(sb);
1143 yaffs_Deinitialise(dev);
1144 yaffs_GrossUnlock(dev);
1146 if(dev == yaffs_dev) yaffs_dev = NULL;
1147 if(dev == yaffsram_dev) yaffsram_dev = NULL;
1153 #ifdef CONFIG_YAFFS_MTD_ENABLED
1155 static void yaffs_MTDPutSuper(struct super_block *sb)
1158 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1165 put_mtd_device(mtd);
1171 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
1174 struct inode * inode;
1175 struct dentry * root;
1178 sb->s_magic = YAFFS_MAGIC;
1179 sb->s_op = &yaffs_super_ops;
1182 printk(KERN_INFO"yaffs: sb is NULL\n");
1184 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
1185 else if(! kdevname(sb->s_dev))
1186 printk(KERN_INFO"yaffs: kdevname is NULL\n");
1188 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
1192 #ifdef CONFIG_YAFFS_USE_CHUNK_SIZE
1193 sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
1194 sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
1196 sb->s_blocksize = PAGE_CACHE_SIZE;
1197 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1199 T(YAFFS_TRACE_OS,("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",(int)(sb->s_blocksize)));
1201 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1202 T(YAFFS_TRACE_OS,("yaffs: Write verification disabled. All guarantees null and void\n"));
1210 #ifdef CONFIG_YAFFS_RAM_ENABLED
1211 // Set the yaffs_Device up for ram emulation
1213 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1216 // Deep shit could not allocate device structure
1217 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1221 memset(dev,0,sizeof(yaffs_Device));
1222 dev->genericDevice = NULL; // Not used for RAM emulation.
1224 nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1225 dev->startBlock = 1; // Don't use block 0
1226 dev->endBlock = nBlocks - 1;
1227 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1228 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1229 dev->nReservedBlocks = 5;
1231 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
1232 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
1233 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
1234 dev->initialiseNAND = nandemul_InitialiseNAND;
1243 #ifdef CONFIG_YAFFS_MTD_ENABLED
1244 struct mtd_info *mtd;
1246 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
1247 MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
1249 // Hope it's a NAND mtd
1250 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1253 printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
1257 if(mtd->type != MTD_NANDFLASH)
1259 printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
1263 //printk(KERN_DEBUG" erase %x\n",mtd->erase);
1264 //printk(KERN_DEBUG" read %x\n",mtd->read);
1265 //printk(KERN_DEBUG" write %x\n",mtd->write);
1266 //printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
1267 //printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
1268 //printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
1269 //printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
1275 #ifndef CONFIG_YAFFS_USE_OLD_MTD
1282 printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
1286 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
1287 mtd->oobsize != YAFFS_BYTES_PER_SPARE)
1289 printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
1294 // OK, so if we got here, we have an MTD that's NAND and looks
1295 // like it has the right capabilities
1296 // Set the yaffs_Device up for ram emulation
1298 #if defined(CONFIG_KERNEL_2_5)
1299 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1301 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1305 // Deep shit could not allocate device structure
1306 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1310 memset(dev,0,sizeof(yaffs_Device));
1311 dev->genericDevice = mtd;
1313 // Set up the memory size parameters....
1315 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1316 dev->startBlock = 1; // Don't use block 0
1317 dev->endBlock = nBlocks - 1;
1318 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1319 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1320 dev->nReservedBlocks = 5;
1321 dev->nShortOpCaches = 10; // Enable short op caching
1324 // ... and the functions.
1325 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1326 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1327 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1328 dev->initialiseNAND = nandmtd_InitialiseNAND;
1330 dev->putSuperFunc = yaffs_MTDPutSuper;
1332 #ifdef CONFIG_YAFFS_USE_NANDECC
1333 dev->useNANDECC = 1;
1341 init_MUTEX(&dev->grossLock);
1344 yaffs_GrossLock(dev);
1345 yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
1347 T(YAFFS_TRACE_OS,("yaffs_read_super: guts initialised\n"));
1349 // Create root inode
1350 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
1352 yaffs_GrossUnlock(dev);
1358 inode->i_op = & yaffs_dir_inode_operations;
1359 inode->i_fop = & yaffs_dir_operations;
1361 T(YAFFS_TRACE_OS,("yaffs_read_super: got root inode\n"));
1364 root = d_alloc_root(inode);
1366 T(YAFFS_TRACE_OS,("yaffs_read_super: d_alloc_root done\n"));
1374 T(YAFFS_TRACE_OS,("yaffs_read_super: done\n"));
1378 static int yaffs_internal_read_super_ram(struct super_block * sb, void * data, int silent)
1380 return yaffs_internal_read_super(1,sb,data,silent) ? 0 : -1;
1382 static int yaffs_internal_read_super_mtd(struct super_block * sb, void * data, int silent)
1384 return yaffs_internal_read_super(0,sb,data,silent) ? 0 : -1;
1388 #ifdef CONFIG_YAFFS_MTD_ENABLED
1389 #if defined(CONFIG_KERNEL_2_5)
1390 static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1393 return get_sb_bdev(fs, flags, dev_name, data, yaffs_internal_read_super_mtd);
1396 /* changes NCB 2.5.70 */
1397 //static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1398 static struct file_system_type yaffs_fs_type = {
1399 .owner = THIS_MODULE,
1401 .get_sb = yaffs_read_super,
1402 // .kill_sb = kill_block_super,
1403 .kill_sb = kill_litter_super,
1404 .fs_flags = FS_REQUIRES_DEV,
1407 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
1409 return yaffs_internal_read_super(0,sb,data,silent);
1412 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1416 #ifdef CONFIG_YAFFS_RAM_ENABLED
1418 #if defined(CONFIG_KERNEL_2_5)
1419 static struct super_block *yaffs_ram_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1422 return get_sb_bdev(fs, flags, dev_name, data, yaffs_internal_read_super_ram);
1425 /* changes NCB 2.5.70 */
1426 //static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1427 static struct file_system_type yaffs_fs_type = {
1428 .owner = THIS_MODULE,
1430 .get_sb = yaffs_ram_read_super,
1431 // .kill_sb = kill_block_super,
1432 .kill_sb = kill_litter_super,
1433 .fs_flags = FS_SINGLE,
1436 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
1438 return yaffs_internal_read_super(1,sb,data,silent);
1441 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
1443 #endif // CONFIG_YAFFS_RAM_ENABLED
1446 static struct proc_dir_entry *my_proc_entry;
1447 static struct proc_dir_entry *my_proc_ram_write_entry;
1449 static char * yaffs_dump_dev(char *buf,yaffs_Device *dev,char *name)
1451 buf +=sprintf(buf,"\nDevice %s\n",name);
1452 buf +=sprintf(buf,"startBlock......... %d\n",dev->startBlock);
1453 buf +=sprintf(buf,"endBlock........... %d\n",dev->endBlock);
1454 buf +=sprintf(buf,"chunkGroupBits..... %d\n",dev->chunkGroupBits);
1455 buf +=sprintf(buf,"chunkGroupSize..... %d\n",dev->chunkGroupSize);
1456 buf +=sprintf(buf,"nErasedBlocks...... %d\n",dev->nErasedBlocks);
1457 buf +=sprintf(buf,"nTnodesCreated..... %d\n",dev->nTnodesCreated);
1458 buf +=sprintf(buf,"nFreeTnodes........ %d\n",dev->nFreeTnodes);
1459 buf +=sprintf(buf,"nObjectsCreated.... %d\n",dev->nObjectsCreated);
1460 buf +=sprintf(buf,"nFreeObjects....... %d\n",dev->nFreeObjects);
1461 buf +=sprintf(buf,"nFreeChunks........ %d\n",dev->nFreeChunks);
1462 buf +=sprintf(buf,"nPageWrites........ %d\n",dev->nPageWrites);
1463 buf +=sprintf(buf,"nPageReads......... %d\n",dev->nPageReads);
1464 buf +=sprintf(buf,"nBlockErasures..... %d\n",dev->nBlockErasures);
1465 buf +=sprintf(buf,"nGCCopies.......... %d\n",dev->nGCCopies);
1466 buf +=sprintf(buf,"garbageCollections. %d\n",dev->garbageCollections);
1467 buf +=sprintf(buf,"passiveGCs......... %d\n",dev->passiveGarbageCollections);
1468 buf +=sprintf(buf,"nRetriedWrites..... %d\n",dev->nRetriedWrites);
1469 buf +=sprintf(buf,"nRetireBlocks...... %d\n",dev->nRetiredBlocks);
1470 buf +=sprintf(buf,"eccFixed........... %d\n",dev->eccFixed);
1471 buf +=sprintf(buf,"eccUnfixed......... %d\n",dev->eccUnfixed);
1472 buf +=sprintf(buf,"tagsEccFixed....... %d\n",dev->tagsEccFixed);
1473 buf +=sprintf(buf,"tagsEccUnfixed..... %d\n",dev->tagsEccUnfixed);
1474 buf +=sprintf(buf,"cacheHits.......... %d\n",dev->cacheHits);
1475 buf +=sprintf(buf,"nDeletedFiles...... %d\n",dev->nDeletedFiles);
1476 buf +=sprintf(buf,"nUnlinkedFiles..... %d\n",dev->nUnlinkedFiles);
1477 buf +=sprintf(buf,"nBackgroudDeletions %d\n",dev->nBackgroundDeletions);
1478 buf +=sprintf(buf,"useNANDECC......... %d\n",dev->useNANDECC);
1484 static int yaffs_proc_read(
1494 char my_buffer[3000];
1498 if (offset > 0) return 0;
1500 /* Fill the buffer and get its length */
1501 buf +=sprintf(buf,"YAFFS built:"__DATE__ " "__TIME__"\n%s\n%s\n", yaffs_fs_c_version,yaffs_guts_c_version);
1503 if(yaffs_dev) buf = yaffs_dump_dev(buf,yaffs_dev,"yaffs");
1504 if(yaffsram_dev) buf = yaffs_dump_dev(buf,yaffsram_dev,"yaffsram");
1507 strcpy(page,my_buffer);
1508 return strlen(my_buffer);
1512 static int yaffs_proc_ram_write(
1522 printk(KERN_DEBUG "yaffs write size %d\n",count);
1526 static int __init init_yaffs_fs(void)
1530 yaffs_dev = yaffsram_dev = NULL;
1532 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
1533 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
1534 printk(KERN_DEBUG "yaffs is using generic read/write (caching)\n");
1536 printk(KERN_DEBUG "yaffs is using direct read/write (uncached)\n");
1540 /* Install the proc_fs entry */
1541 my_proc_entry = create_proc_read_entry("yaffs",
1551 #ifdef CONFIG_YAFFS_RAM_ENABLED
1553 my_proc_ram_write_entry = create_proc_entry("yaffs_ram",
1557 if(!my_proc_ram_write_entry)
1563 my_proc_ram_write_entry->write_proc = yaffs_proc_ram_write;
1567 error = register_filesystem(&yaffs_ram_fs_type);
1572 #endif //CONFIG_YAFFS_RAM_ENABLED
1574 #ifdef CONFIG_YAFFS_MTD_ENABLED
1575 error = register_filesystem(&yaffs_fs_type);
1578 #ifdef CONFIG_YAFFS_RAM_ENABLED
1579 unregister_filesystem(&yaffs_ram_fs_type);
1580 #endif //CONFIG_YAFFS_RAM_ENABLED
1582 #endif // CONFIG_YAFFS_MTD_ENABLED
1587 static void __exit exit_yaffs_fs(void)
1589 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
1591 remove_proc_entry("yaffs",&proc_root);
1593 #ifdef CONFIG_YAFFS_RAM_ENABLED
1594 unregister_filesystem(&yaffs_ram_fs_type);
1596 #ifdef CONFIG_YAFFS_MTD_ENABLED
1597 unregister_filesystem(&yaffs_fs_type);
1602 module_init(init_yaffs_fs)
1603 module_exit(exit_yaffs_fs)
1605 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
1606 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
1607 MODULE_LICENSE("GPL");