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.29 2003-08-30 05:15:49 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;
526 #if defined(CONFIG_KERNEL_2_5)
527 inode->i_rdev = to_kdev_t(obj->st_rdev);
528 inode->i_atime.tv_sec = (time_t)(obj->st_atime);
529 inode->i_atime.tv_nsec = 0;
530 inode->i_mtime.tv_sec = (time_t)obj->st_mtime;
531 inode->i_mtime.tv_nsec =0;
532 inode->i_ctime.tv_sec = (time_t)obj->st_ctime;
533 inode->i_ctime.tv_nsec = 0;
535 inode->i_rdev = obj->st_rdev;
536 inode->i_atime = obj->st_atime;
537 inode->i_mtime = obj->st_mtime;
538 inode->i_ctime = obj->st_ctime;
540 inode->i_size = yaffs_GetObjectFileLength(obj);
541 inode->i_blocks = (inode->i_size + 511) >> 9;
543 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
545 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
546 inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count)));
548 switch (obj->st_mode & S_IFMT)
550 default: // fifo, device or socket
551 init_special_inode(inode, obj->st_mode,(dev_t)(obj->st_rdev));
553 case S_IFREG: // file
554 inode->i_op = &yaffs_file_inode_operations;
555 inode->i_fop = &yaffs_file_operations;
556 inode->i_mapping->a_ops = &yaffs_file_address_operations;
558 case S_IFDIR: // directory
559 inode->i_op = &yaffs_dir_inode_operations;
560 inode->i_fop = &yaffs_dir_operations;
562 case S_IFLNK: // symlink
563 inode->i_op = &yaffs_symlink_inode_operations;
568 inode->u.generic_ip = obj;
569 obj->myInode = inode;
574 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FileInode invalid parameters\n"));
579 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj)
581 struct inode * inode;
583 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId));
585 inode = iget(sb,obj->objectId);
587 // NB Side effect: iget calls back to yaffs_read_inode().
588 // iget also increments the inode's i_count
593 static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos)
600 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_read\n"));
602 obj = yaffs_DentryToObject(f->f_dentry);
606 yaffs_GrossLock(dev);
608 inode = f->f_dentry->d_inode;
610 if (*pos < inode->i_size)
612 if (*pos + n > inode->i_size)
614 n = inode->i_size - *pos;
622 nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n);
628 yaffs_GrossUnlock(dev);
632 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos));
638 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos)
646 obj = yaffs_DentryToObject(f->f_dentry);
650 yaffs_GrossLock(dev);
652 inode = f->f_dentry->d_inode;
654 if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
656 ipos = inode->i_size;
666 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write: hey obj is null!\n"));
670 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));
673 nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n);
675 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos));
680 if(ipos > inode->i_size)
682 inode->i_size = ipos;
683 inode->i_blocks = (ipos + 511)>>9;
685 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,(int)(inode->i_blocks)));
689 yaffs_GrossUnlock(dev);
691 return nWritten != n ? -ENOSPC : nWritten;
695 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
699 struct inode *inode = f->f_dentry->d_inode;
700 unsigned long offset, curoffs;
704 char name[YAFFS_MAX_NAME_LENGTH +1];
706 obj = yaffs_DentryToObject(f->f_dentry);
709 yaffs_GrossLock(dev);
713 T(YAFFS_TRACE_OS,("yaffs_readdir: starting at %d\n",(int)offset));
717 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino));
718 if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0)
727 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino));
728 if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0)
738 list_for_each(i,&obj->variant.directoryVariant.children)
741 if(curoffs >= offset)
743 l = list_entry(i, yaffs_Object,siblings);
745 yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1);
746 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l)));
752 yaffs_GetObjectInode(l),
753 yaffs_GetObjectType(l))
767 yaffs_GrossUnlock(dev);
774 * File creation. Allocate an inode, and we're done..
776 #if defined(CONFIG_KERNEL_2_5)
777 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
779 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
784 yaffs_Object *obj = NULL;
787 yaffs_Object *parent = yaffs_InodeToObject(dir);
793 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: parent object %d type %d\n",
794 parent->objectId,parent->variantType));
798 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: could not get parent object\n"));
802 T(YAFFS_TRACE_OS,("yaffs_mknod: making oject for %s, mode %x dev %x\n",
803 dentry->d_name.name, mode,rdev));
807 yaffs_GrossLock(dev);
809 switch (mode & S_IFMT)
812 // Special (socket, fifo, device...)
813 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making special\n"));
814 obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,rdev);
816 case S_IFREG: // file
817 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
818 obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid);
820 case S_IFDIR: // directory
821 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making directory\n"));
822 obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid);
824 case S_IFLNK: // symlink
825 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n"));
826 obj = NULL; // Do we ever get here?
832 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
833 d_instantiate(dentry, inode);
834 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count)));
839 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod failed making object\n"));
843 yaffs_GrossUnlock(dev);
848 static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
851 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mkdir\n"));
852 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
854 // attempt to fix dir bug - didn't work
863 #if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */
864 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *n)
866 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
869 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_create\n"));
870 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
874 static int yaffs_unlink(struct inode * dir, struct dentry *dentry)
881 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_unlink %d:%s\n",(int)(dir->i_ino),dentry->d_name.name));
883 dev = yaffs_InodeToObject(dir)->myDev;
885 yaffs_GrossLock(dev);
888 retVal = yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name);
891 yaffs_GrossUnlock(dev);
893 if( retVal == YAFFS_OK)
895 dentry->d_inode->i_nlink--;
896 mark_inode_dirty(dentry->d_inode);
909 static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
911 struct inode *inode = old_dentry->d_inode;
912 yaffs_Object *obj = NULL;
913 yaffs_Object *link=NULL;
916 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link\n"));
918 obj = yaffs_InodeToObject(inode);
921 yaffs_GrossLock(dev);
923 if (!S_ISDIR(inode->i_mode)) // Don't link directories
925 link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj);
931 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
932 d_instantiate(dentry, old_dentry->d_inode);
933 atomic_inc(&old_dentry->d_inode->i_count);
934 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link link count %d i_count %d\n",
935 old_dentry->d_inode->i_nlink,atomic_read(&old_dentry->d_inode->i_count)));
939 yaffs_GrossUnlock(dev);
953 static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
958 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_symlink\n"));
960 dev = yaffs_InodeToObject(dir)->myDev;
961 yaffs_GrossLock(dev);
962 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
963 S_IFLNK | S_IRWXUGO, current->uid, current->gid,
965 yaffs_GrossUnlock(dev);
972 inode = yaffs_get_inode(dir->i_sb, obj->st_mode, 0, obj);
973 d_instantiate(dentry, inode);
974 T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink created OK\n"));
979 T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink not created\n"));
986 static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync)
992 obj = yaffs_DentryToObject(dentry);
996 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_sync_object\n"));
997 yaffs_GrossLock(dev);
998 yaffs_FlushFile(obj,1);
999 yaffs_GrossUnlock(dev);
1004 * The VFS layer already does all the dentry stuff for rename.
1006 * NB: POSIX says you can rename an object over an old object of the same name
1008 static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
1011 int retVal = YAFFS_FAIL;
1013 yaffs_Object *target;
1015 dev = yaffs_InodeToObject(old_dir)->myDev;
1017 yaffs_GrossLock(dev);
1019 // Check if the target is an existing directory that is not empty.
1020 target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1023 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1024 !list_empty(&target->variant.directoryVariant.children))
1026 retVal = YAFFS_FAIL;
1031 // Unlink the target if it exists
1032 removed = yaffs_Unlink(yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1035 retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name,
1036 yaffs_InodeToObject(new_dir),new_dentry->d_name.name);
1039 yaffs_GrossUnlock(dev);
1041 if(retVal == YAFFS_OK)
1043 if(removed == YAFFS_OK)
1045 new_dentry->d_inode->i_nlink--;
1046 mark_inode_dirty(new_dentry->d_inode);
1059 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1061 struct inode *inode = dentry->d_inode;
1065 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_setattr of object %d\n",yaffs_InodeToObject(inode)->objectId));
1067 if((error = inode_change_ok(inode,attr)) == 0)
1070 dev = yaffs_InodeToObject(inode)->myDev;
1071 yaffs_GrossLock(dev);
1072 if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK)
1080 yaffs_GrossUnlock(dev);
1081 inode_setattr(inode,attr);
1086 #if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */
1087 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1089 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1092 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1093 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_statfs\n"));
1095 yaffs_GrossLock(dev);
1097 buf->f_type = YAFFS_MAGIC;
1098 buf->f_bsize = sb->s_blocksize;
1099 buf->f_namelen = 255;
1100 buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * YAFFS_CHUNKS_PER_BLOCK/
1101 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
1104 buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev)/
1105 (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK);
1106 buf->f_bavail = buf->f_bfree;
1108 yaffs_GrossUnlock(dev);
1112 static void yaffs_read_inode (struct inode *inode)
1114 // NB This is called as a side effect of other functions and
1115 // thus gross locking should always be in place already.
1118 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1120 T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino));
1122 obj = yaffs_FindObjectByNumber(dev,inode->i_ino);
1124 yaffs_FillInodeFromObject(inode,obj);
1130 static yaffs_Device *yaffs_dev;
1131 static yaffs_Device *yaffsram_dev;
1135 static void yaffs_put_super(struct super_block *sb)
1137 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1139 yaffs_GrossLock(dev);
1140 if(dev->putSuperFunc)
1142 dev->putSuperFunc(sb);
1144 yaffs_Deinitialise(dev);
1145 yaffs_GrossUnlock(dev);
1147 if(dev == yaffs_dev) yaffs_dev = NULL;
1148 if(dev == yaffsram_dev) yaffsram_dev = NULL;
1154 #ifdef CONFIG_YAFFS_MTD_ENABLED
1156 static void yaffs_MTDPutSuper(struct super_block *sb)
1159 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1166 put_mtd_device(mtd);
1172 static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent)
1175 struct inode * inode;
1176 struct dentry * root;
1179 sb->s_magic = YAFFS_MAGIC;
1180 sb->s_op = &yaffs_super_ops;
1183 printk(KERN_INFO"yaffs: sb is NULL\n");
1185 printk(KERN_INFO"yaffs: sb->s_dev is NULL\n");
1186 else if(! kdevname(sb->s_dev))
1187 printk(KERN_INFO"yaffs: kdevname is NULL\n");
1189 printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev));
1193 #ifdef CONFIG_YAFFS_USE_CHUNK_SIZE
1194 sb->s_blocksize = YAFFS_BYTES_PER_CHUNK;
1195 sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT;
1197 sb->s_blocksize = PAGE_CACHE_SIZE;
1198 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1200 T(YAFFS_TRACE_OS,("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",(int)(sb->s_blocksize)));
1202 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1203 T(YAFFS_TRACE_OS,("yaffs: Write verification disabled. All guarantees null and void\n"));
1211 #ifdef CONFIG_YAFFS_RAM_ENABLED
1212 // Set the yaffs_Device up for ram emulation
1214 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1217 // Deep shit could not allocate device structure
1218 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1222 memset(dev,0,sizeof(yaffs_Device));
1223 dev->genericDevice = NULL; // Not used for RAM emulation.
1225 nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1226 dev->startBlock = 1; // Don't use block 0
1227 dev->endBlock = nBlocks - 1;
1228 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1229 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1230 dev->nReservedBlocks = 5;
1232 dev->writeChunkToNAND = nandemul_WriteChunkToNAND;
1233 dev->readChunkFromNAND = nandemul_ReadChunkFromNAND;
1234 dev->eraseBlockInNAND = nandemul_EraseBlockInNAND;
1235 dev->initialiseNAND = nandemul_InitialiseNAND;
1244 #ifdef CONFIG_YAFFS_MTD_ENABLED
1245 struct mtd_info *mtd;
1247 printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n",
1248 MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev));
1250 // Hope it's a NAND mtd
1251 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1254 printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev));
1258 if(mtd->type != MTD_NANDFLASH)
1260 printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type);
1264 //printk(KERN_DEBUG" erase %x\n",mtd->erase);
1265 //printk(KERN_DEBUG" read %x\n",mtd->read);
1266 //printk(KERN_DEBUG" write %x\n",mtd->write);
1267 //printk(KERN_DEBUG" readoob %x\n",mtd->read_oob);
1268 //printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob);
1269 //printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock);
1270 //printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize);
1276 #ifndef CONFIG_YAFFS_USE_OLD_MTD
1283 printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n");
1287 if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
1288 mtd->oobsize != YAFFS_BYTES_PER_SPARE)
1290 printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n");
1295 // OK, so if we got here, we have an MTD that's NAND and looks
1296 // like it has the right capabilities
1297 // Set the yaffs_Device up for ram emulation
1299 #if defined(CONFIG_KERNEL_2_5)
1300 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1302 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL);
1306 // Deep shit could not allocate device structure
1307 printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n");
1311 memset(dev,0,sizeof(yaffs_Device));
1312 dev->genericDevice = mtd;
1314 // Set up the memory size parameters....
1316 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1317 dev->startBlock = 1; // Don't use block 0
1318 dev->endBlock = nBlocks - 1;
1319 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1320 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1321 dev->nReservedBlocks = 5;
1322 dev->nShortOpCaches = 10; // Enable short op caching
1325 // ... and the functions.
1326 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1327 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1328 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1329 dev->initialiseNAND = nandmtd_InitialiseNAND;
1331 dev->putSuperFunc = yaffs_MTDPutSuper;
1333 #ifdef CONFIG_YAFFS_USE_NANDECC
1334 dev->useNANDECC = 1;
1342 init_MUTEX(&dev->grossLock);
1345 yaffs_GrossLock(dev);
1346 yaffs_GutsInitialise(yaffs_SuperToDevice(sb));
1348 T(YAFFS_TRACE_OS,("yaffs_read_super: guts initialised\n"));
1350 // Create root inode
1351 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb)));
1353 yaffs_GrossUnlock(dev);
1359 inode->i_op = & yaffs_dir_inode_operations;
1360 inode->i_fop = & yaffs_dir_operations;
1362 T(YAFFS_TRACE_OS,("yaffs_read_super: got root inode\n"));
1365 root = d_alloc_root(inode);
1367 T(YAFFS_TRACE_OS,("yaffs_read_super: d_alloc_root done\n"));
1375 T(YAFFS_TRACE_OS,("yaffs_read_super: done\n"));
1379 static int yaffs_internal_read_super_ram(struct super_block * sb, void * data, int silent)
1381 return yaffs_internal_read_super(1,sb,data,silent) ? 0 : -1;
1383 static int yaffs_internal_read_super_mtd(struct super_block * sb, void * data, int silent)
1385 return yaffs_internal_read_super(0,sb,data,silent) ? 0 : -1;
1389 #ifdef CONFIG_YAFFS_MTD_ENABLED
1390 #if defined(CONFIG_KERNEL_2_5)
1391 static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1394 return get_sb_bdev(fs, flags, dev_name, data, yaffs_internal_read_super_mtd);
1397 /* changes NCB 2.5.70 */
1398 //static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1399 static struct file_system_type yaffs_fs_type = {
1400 .owner = THIS_MODULE,
1402 .get_sb = yaffs_read_super,
1403 // .kill_sb = kill_block_super,
1404 .kill_sb = kill_litter_super,
1405 .fs_flags = FS_REQUIRES_DEV,
1408 static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent)
1410 return yaffs_internal_read_super(0,sb,data,silent);
1413 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1417 #ifdef CONFIG_YAFFS_RAM_ENABLED
1419 #if defined(CONFIG_KERNEL_2_5)
1420 static struct super_block *yaffs_ram_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data)
1423 return get_sb_bdev(fs, flags, dev_name, data, yaffs_internal_read_super_ram);
1426 /* changes NCB 2.5.70 */
1427 //static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV);
1428 static struct file_system_type yaffs_fs_type = {
1429 .owner = THIS_MODULE,
1431 .get_sb = yaffs_ram_read_super,
1432 // .kill_sb = kill_block_super,
1433 .kill_sb = kill_litter_super,
1434 .fs_flags = FS_SINGLE,
1437 static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent)
1439 return yaffs_internal_read_super(1,sb,data,silent);
1442 static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE);
1444 #endif // CONFIG_YAFFS_RAM_ENABLED
1447 static struct proc_dir_entry *my_proc_entry;
1448 static struct proc_dir_entry *my_proc_ram_write_entry;
1450 static char * yaffs_dump_dev(char *buf,yaffs_Device *dev,char *name)
1452 buf +=sprintf(buf,"\nDevice %s\n",name);
1453 buf +=sprintf(buf,"startBlock......... %d\n",dev->startBlock);
1454 buf +=sprintf(buf,"endBlock........... %d\n",dev->endBlock);
1455 buf +=sprintf(buf,"chunkGroupBits..... %d\n",dev->chunkGroupBits);
1456 buf +=sprintf(buf,"chunkGroupSize..... %d\n",dev->chunkGroupSize);
1457 buf +=sprintf(buf,"nErasedBlocks...... %d\n",dev->nErasedBlocks);
1458 buf +=sprintf(buf,"nTnodesCreated..... %d\n",dev->nTnodesCreated);
1459 buf +=sprintf(buf,"nFreeTnodes........ %d\n",dev->nFreeTnodes);
1460 buf +=sprintf(buf,"nObjectsCreated.... %d\n",dev->nObjectsCreated);
1461 buf +=sprintf(buf,"nFreeObjects....... %d\n",dev->nFreeObjects);
1462 buf +=sprintf(buf,"nFreeChunks........ %d\n",dev->nFreeChunks);
1463 buf +=sprintf(buf,"nPageWrites........ %d\n",dev->nPageWrites);
1464 buf +=sprintf(buf,"nPageReads......... %d\n",dev->nPageReads);
1465 buf +=sprintf(buf,"nBlockErasures..... %d\n",dev->nBlockErasures);
1466 buf +=sprintf(buf,"nGCCopies.......... %d\n",dev->nGCCopies);
1467 buf +=sprintf(buf,"garbageCollections. %d\n",dev->garbageCollections);
1468 buf +=sprintf(buf,"passiveGCs......... %d\n",dev->passiveGarbageCollections);
1469 buf +=sprintf(buf,"nRetriedWrites..... %d\n",dev->nRetriedWrites);
1470 buf +=sprintf(buf,"nRetireBlocks...... %d\n",dev->nRetiredBlocks);
1471 buf +=sprintf(buf,"eccFixed........... %d\n",dev->eccFixed);
1472 buf +=sprintf(buf,"eccUnfixed......... %d\n",dev->eccUnfixed);
1473 buf +=sprintf(buf,"tagsEccFixed....... %d\n",dev->tagsEccFixed);
1474 buf +=sprintf(buf,"tagsEccUnfixed..... %d\n",dev->tagsEccUnfixed);
1475 buf +=sprintf(buf,"cacheHits.......... %d\n",dev->cacheHits);
1476 buf +=sprintf(buf,"nDeletedFiles...... %d\n",dev->nDeletedFiles);
1477 buf +=sprintf(buf,"nUnlinkedFiles..... %d\n",dev->nUnlinkedFiles);
1478 buf +=sprintf(buf,"nBackgroudDeletions %d\n",dev->nBackgroundDeletions);
1479 buf +=sprintf(buf,"useNANDECC......... %d\n",dev->useNANDECC);
1485 static int yaffs_proc_read(
1495 char my_buffer[3000];
1499 if (offset > 0) return 0;
1501 /* Fill the buffer and get its length */
1502 buf +=sprintf(buf,"YAFFS built:"__DATE__ " "__TIME__"\n%s\n%s\n", yaffs_fs_c_version,yaffs_guts_c_version);
1504 if(yaffs_dev) buf = yaffs_dump_dev(buf,yaffs_dev,"yaffs");
1505 if(yaffsram_dev) buf = yaffs_dump_dev(buf,yaffsram_dev,"yaffsram");
1508 strcpy(page,my_buffer);
1509 return strlen(my_buffer);
1513 static int yaffs_proc_ram_write(
1523 printk(KERN_DEBUG "yaffs write size %d\n",count);
1527 static int __init init_yaffs_fs(void)
1531 yaffs_dev = yaffsram_dev = NULL;
1533 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n");
1534 #ifdef CONFIG_YAFFS_USE_GENERIC_RW
1535 printk(KERN_DEBUG "yaffs is using generic read/write (caching)\n");
1537 printk(KERN_DEBUG "yaffs is using direct read/write (uncached)\n");
1541 /* Install the proc_fs entry */
1542 my_proc_entry = create_proc_read_entry("yaffs",
1552 #ifdef CONFIG_YAFFS_RAM_ENABLED
1554 my_proc_ram_write_entry = create_proc_entry("yaffs_ram",
1558 if(!my_proc_ram_write_entry)
1564 my_proc_ram_write_entry->write_proc = yaffs_proc_ram_write;
1568 error = register_filesystem(&yaffs_ram_fs_type);
1573 #endif //CONFIG_YAFFS_RAM_ENABLED
1575 #ifdef CONFIG_YAFFS_MTD_ENABLED
1576 error = register_filesystem(&yaffs_fs_type);
1579 #ifdef CONFIG_YAFFS_RAM_ENABLED
1580 unregister_filesystem(&yaffs_ram_fs_type);
1581 #endif //CONFIG_YAFFS_RAM_ENABLED
1583 #endif // CONFIG_YAFFS_MTD_ENABLED
1588 static void __exit exit_yaffs_fs(void)
1590 printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n");
1592 remove_proc_entry("yaffs",&proc_root);
1594 #ifdef CONFIG_YAFFS_RAM_ENABLED
1595 unregister_filesystem(&yaffs_ram_fs_type);
1597 #ifdef CONFIG_YAFFS_MTD_ENABLED
1598 unregister_filesystem(&yaffs_fs_type);
1603 module_init(init_yaffs_fs)
1604 module_exit(exit_yaffs_fs)
1606 MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system");
1607 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002");
1608 MODULE_LICENSE("GPL");