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 * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
20 * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
22 * >> inode->u.generic_ip points to the associated yaffs_Object.
25 * * Luc van OostenRyck for numerous patches.
26 * * Nick Bane for numerous patches.
27 * * Nick Bane for 2.5/2.6 integration.
28 * * Andras Toth for mknod rdev issue.
29 * * Michael Fischer for finding the problem with inode inconsistency.
30 * * Some code bodily lifted from JFFS2.
33 const char *yaffs_fs_c_version =
34 "$Id: yaffs_fs.c,v 1.52 2006-09-26 13:28:13 vwool Exp $";
35 extern const char *yaffs_guts_c_version;
37 #include <linux/config.h>
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/version.h>
41 #include <linux/slab.h>
42 #include <linux/init.h>
43 #include <linux/list.h>
45 #include <linux/proc_fs.h>
46 #include <linux/smp_lock.h>
47 #include <linux/pagemap.h>
48 #include <linux/mtd/mtd.h>
49 #include <linux/interrupt.h>
50 #include <linux/string.h>
51 #include <linux/ctype.h>
53 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
55 #include <linux/statfs.h> /* Added NCB 15-8-2003 */
56 #include <asm/statfs.h>
57 #define UnlockPage(p) unlock_page(p)
58 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
60 /* FIXME: use sb->s_id instead ? */
61 #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
65 #include <linux/locks.h>
66 #define BDEVNAME_SIZE 0
67 #define yaffs_devname(sb, buf) kdevname(sb->s_dev)
69 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
70 /* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
76 #include <asm/uaccess.h>
79 #include "yaffs_guts.h"
81 unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS |
82 YAFFS_TRACE_BAD_BLOCKS |
83 YAFFS_TRACE_CHECKPOINT
86 #include <linux/mtd/mtd.h>
87 #include "yaffs_mtdif.h"
88 #include "yaffs_mtdif2.h"
90 /*#define T(x) printk x */
92 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
93 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
95 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
96 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
98 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
101 static void yaffs_put_super(struct super_block *sb);
103 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
106 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
107 static int yaffs_file_flush(struct file *file, fl_owner_t id);
109 static int yaffs_file_flush(struct file *file);
112 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
115 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
117 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
118 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
119 struct nameidata *n);
120 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
121 struct nameidata *n);
123 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
124 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
126 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
127 struct dentry *dentry);
128 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
129 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
130 const char *symname);
131 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
133 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
134 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
137 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
140 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
141 struct inode *new_dir, struct dentry *new_dentry);
142 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
144 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
145 static int yaffs_sync_fs(struct super_block *sb, int wait);
146 static void yaffs_write_super(struct super_block *sb);
148 static int yaffs_sync_fs(struct super_block *sb);
149 static int yaffs_write_super(struct super_block *sb);
152 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
153 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
154 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
155 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
157 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
159 static void yaffs_read_inode(struct inode *inode);
161 static void yaffs_put_inode(struct inode *inode);
162 static void yaffs_delete_inode(struct inode *);
163 static void yaffs_clear_inode(struct inode *);
165 static int yaffs_readpage(struct file *file, struct page *page);
166 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
167 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
169 static int yaffs_writepage(struct page *page);
171 static int yaffs_prepare_write(struct file *f, struct page *pg,
172 unsigned offset, unsigned to);
173 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
176 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
178 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
179 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
181 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
184 static struct address_space_operations yaffs_file_address_operations = {
185 .readpage = yaffs_readpage,
186 .writepage = yaffs_writepage,
187 .prepare_write = yaffs_prepare_write,
188 .commit_write = yaffs_commit_write,
191 static struct file_operations yaffs_file_operations = {
192 .read = generic_file_read,
193 .write = generic_file_write,
194 .mmap = generic_file_mmap,
195 .flush = yaffs_file_flush,
196 .fsync = yaffs_sync_object,
197 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
198 .sendfile = generic_file_sendfile,
203 static struct inode_operations yaffs_file_inode_operations = {
204 .setattr = yaffs_setattr,
207 static struct inode_operations yaffs_symlink_inode_operations = {
208 .readlink = yaffs_readlink,
209 .follow_link = yaffs_follow_link,
210 .setattr = yaffs_setattr,
213 static struct inode_operations yaffs_dir_inode_operations = {
214 .create = yaffs_create,
215 .lookup = yaffs_lookup,
217 .unlink = yaffs_unlink,
218 .symlink = yaffs_symlink,
219 .mkdir = yaffs_mkdir,
220 .rmdir = yaffs_unlink,
221 .mknod = yaffs_mknod,
222 .rename = yaffs_rename,
223 .setattr = yaffs_setattr,
226 static struct file_operations yaffs_dir_operations = {
227 .read = generic_read_dir,
228 .readdir = yaffs_readdir,
229 .fsync = yaffs_sync_object,
232 static struct super_operations yaffs_super_ops = {
233 .statfs = yaffs_statfs,
234 .read_inode = yaffs_read_inode,
235 .put_inode = yaffs_put_inode,
236 .put_super = yaffs_put_super,
237 .delete_inode = yaffs_delete_inode,
238 .clear_inode = yaffs_clear_inode,
239 .sync_fs = yaffs_sync_fs,
240 .write_super = yaffs_write_super,
243 static void yaffs_GrossLock(yaffs_Device * dev)
245 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
247 down(&dev->grossLock);
250 static void yaffs_GrossUnlock(yaffs_Device * dev)
252 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
257 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
260 unsigned char *alias;
263 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
265 yaffs_GrossLock(dev);
267 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
269 yaffs_GrossUnlock(dev);
274 ret = vfs_readlink(dentry, buffer, buflen, alias);
279 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
280 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
282 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
285 unsigned char *alias;
287 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
289 yaffs_GrossLock(dev);
291 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
293 yaffs_GrossUnlock(dev);
301 ret = vfs_follow_link(nd, alias);
304 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
305 return ERR_PTR (ret);
311 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
315 * Lookup is used to find objects in the fs
317 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
319 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
322 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
326 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
328 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
330 yaffs_GrossLock(dev);
333 (KERN_DEBUG "yaffs_lookup for %d:%s\n",
334 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
337 yaffs_FindObjectByName(yaffs_InodeToObject(dir),
338 dentry->d_name.name);
340 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
342 /* Can't hold gross lock when calling yaffs_get_inode() */
343 yaffs_GrossUnlock(dev);
347 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
349 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
353 (KERN_DEBUG "yaffs_loookup dentry \n"));
354 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
355 * d_add even if NULL inode */
357 /*dget(dentry); // try to solve directory bug */
358 d_add(dentry, inode);
366 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
370 /* added NCB for 2.5/6 compatability - forces add even if inode is
371 * NULL which creates dentry hash */
372 d_add(dentry, inode);
375 /* return (ERR_PTR(-EIO)); */
379 /* For now put inode is just for debugging
380 * Put inode is called when the inode **structure** is put.
382 static void yaffs_put_inode(struct inode *inode)
385 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
386 atomic_read(&inode->i_count)));
390 /* clear is called to tell the fs to release any per-inode data it holds */
391 static void yaffs_clear_inode(struct inode *inode)
396 obj = yaffs_InodeToObject(inode);
399 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
400 atomic_read(&inode->i_count),
401 obj ? "object exists" : "null object"));
405 yaffs_GrossLock(dev);
407 /* Clear the association between the inode and
411 inode->u.generic_ip = NULL;
413 /* If the object freeing was deferred, then the real
415 * This should fix the inode inconsistency problem.
418 yaffs_HandleDeferedFree(obj);
420 yaffs_GrossUnlock(dev);
425 /* delete is called when the link count is zero and the inode
426 * is put (ie. nobody wants to know about it anymore, time to
428 * NB Must call clear_inode()
430 static void yaffs_delete_inode(struct inode *inode)
432 yaffs_Object *obj = yaffs_InodeToObject(inode);
436 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
437 atomic_read(&inode->i_count),
438 obj ? "object exists" : "null object"));
442 yaffs_GrossLock(dev);
443 yaffs_DeleteFile(obj);
444 yaffs_GrossUnlock(dev);
446 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
447 truncate_inode_pages (&inode->i_data, 0);
452 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
453 static int yaffs_file_flush(struct file *file, fl_owner_t id)
455 static int yaffs_file_flush(struct file *file)
458 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
460 yaffs_Device *dev = obj->myDev;
463 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
464 obj->dirty ? "dirty" : "clean"));
466 yaffs_GrossLock(dev);
468 yaffs_FlushFile(obj, 1);
470 yaffs_GrossUnlock(dev);
475 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
477 /* Lifted from jffs2 */
480 unsigned char *pg_buf;
485 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
486 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
487 (unsigned)PAGE_CACHE_SIZE));
489 obj = yaffs_DentryToObject(f->f_dentry);
493 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
494 BUG_ON(!PageLocked(pg));
501 /* FIXME: Can kmap fail? */
503 yaffs_GrossLock(dev);
506 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
509 yaffs_GrossUnlock(dev);
515 ClearPageUptodate(pg);
522 flush_dcache_page(pg);
525 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
529 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
531 int ret = yaffs_readpage_nolock(f, pg);
536 static int yaffs_readpage(struct file *f, struct page *pg)
538 return yaffs_readpage_unlock(f, pg);
541 /* writepage inspired by/stolen from smbfs */
543 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
544 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
546 static int yaffs_writepage(struct page *page)
549 struct address_space *mapping = page->mapping;
550 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
552 unsigned long end_index;
560 inode = mapping->host;
564 if (offset > inode->i_size) {
567 "yaffs_writepage at %08x, inode size = %08x!!!\n",
568 (unsigned)(page->index << PAGE_CACHE_SHIFT),
569 (unsigned)inode->i_size));
571 (KERN_DEBUG " -> don't care!!\n"));
576 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
579 if (page->index < end_index) {
580 nBytes = PAGE_CACHE_SIZE;
582 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
589 obj = yaffs_InodeToObject(inode);
590 yaffs_GrossLock(obj->myDev);
593 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
594 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
596 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
597 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
600 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
604 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
605 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
607 yaffs_GrossUnlock(obj->myDev);
610 SetPageUptodate(page);
614 return (nWritten == nBytes) ? 0 : -ENOSPC;
617 static int yaffs_prepare_write(struct file *f, struct page *pg,
618 unsigned offset, unsigned to)
621 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
622 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
623 return yaffs_readpage_nolock(f, pg);
629 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
633 void *addr = page_address(pg) + offset;
634 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
635 int nBytes = to - offset;
639 unsigned saddr = (unsigned)addr;
642 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
645 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
647 if (nWritten != nBytes) {
650 "yaffs_commit_write not same size nWritten %d nBytes %d\n",
653 ClearPageUptodate(pg);
659 (KERN_DEBUG "yaffs_commit_write returning %d\n",
660 nWritten == nBytes ? 0 : nWritten));
662 return nWritten == nBytes ? 0 : nWritten;
666 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
671 /* Check mode against the variant type and attempt to repair if broken. */
672 __u32 mode = obj->yst_mode;
673 switch( obj->variantType ){
674 case YAFFS_OBJECT_TYPE_FILE :
675 if( ! S_ISREG(mode) ){
676 obj->yst_mode &= ~S_IFMT;
677 obj->yst_mode |= S_IFREG;
681 case YAFFS_OBJECT_TYPE_SYMLINK :
682 if( ! S_ISLNK(mode) ){
683 obj->yst_mode &= ~S_IFMT;
684 obj->yst_mode |= S_IFLNK;
688 case YAFFS_OBJECT_TYPE_DIRECTORY :
689 if( ! S_ISDIR(mode) ){
690 obj->yst_mode &= ~S_IFMT;
691 obj->yst_mode |= S_IFDIR;
695 case YAFFS_OBJECT_TYPE_UNKNOWN :
696 case YAFFS_OBJECT_TYPE_HARDLINK :
697 case YAFFS_OBJECT_TYPE_SPECIAL :
703 inode->i_ino = obj->objectId;
704 inode->i_mode = obj->yst_mode;
705 inode->i_uid = obj->yst_uid;
706 inode->i_gid = obj->yst_gid;
707 inode->i_blksize = inode->i_sb->s_blocksize;
708 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
710 inode->i_rdev = old_decode_dev(obj->yst_rdev);
711 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
712 inode->i_atime.tv_nsec = 0;
713 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
714 inode->i_mtime.tv_nsec = 0;
715 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
716 inode->i_ctime.tv_nsec = 0;
718 inode->i_rdev = obj->yst_rdev;
719 inode->i_atime = obj->yst_atime;
720 inode->i_mtime = obj->yst_mtime;
721 inode->i_ctime = obj->yst_ctime;
723 inode->i_size = yaffs_GetObjectFileLength(obj);
724 inode->i_blocks = (inode->i_size + 511) >> 9;
726 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
730 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
731 inode->i_mode, inode->i_uid, inode->i_gid,
732 (int)inode->i_size, atomic_read(&inode->i_count)));
734 switch (obj->yst_mode & S_IFMT) {
735 default: /* fifo, device or socket */
736 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
737 init_special_inode(inode, obj->yst_mode,
738 old_decode_dev(obj->yst_rdev));
740 init_special_inode(inode, obj->yst_mode,
741 (dev_t) (obj->yst_rdev));
744 case S_IFREG: /* file */
745 inode->i_op = &yaffs_file_inode_operations;
746 inode->i_fop = &yaffs_file_operations;
747 inode->i_mapping->a_ops =
748 &yaffs_file_address_operations;
750 case S_IFDIR: /* directory */
751 inode->i_op = &yaffs_dir_inode_operations;
752 inode->i_fop = &yaffs_dir_operations;
754 case S_IFLNK: /* symlink */
755 inode->i_op = &yaffs_symlink_inode_operations;
759 inode->u.generic_ip = obj;
760 obj->myInode = inode;
764 (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
769 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
776 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
783 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
789 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
791 inode = iget(sb, obj->objectId);
793 /* NB Side effect: iget calls back to yaffs_read_inode(). */
794 /* iget also increments the inode's i_count */
795 /* NB You can't be holding grossLock or deadlock will happen! */
800 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
808 obj = yaffs_DentryToObject(f->f_dentry);
812 yaffs_GrossLock(dev);
814 inode = f->f_dentry->d_inode;
816 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
817 ipos = inode->i_size;
824 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
828 "yaffs_file_write about to write writing %d bytes"
829 "to object %d at %d\n",
830 n, obj->objectId, ipos));
833 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
836 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
841 if (ipos > inode->i_size) {
842 inode->i_size = ipos;
843 inode->i_blocks = (ipos + 511) >> 9;
847 "yaffs_file_write size updated to %d bytes, "
849 ipos, (int)(inode->i_blocks)));
853 yaffs_GrossUnlock(dev);
854 return nWritten == 0 ? -ENOSPC : nWritten;
857 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
861 struct inode *inode = f->f_dentry->d_inode;
862 unsigned long offset, curoffs;
866 char name[YAFFS_MAX_NAME_LENGTH + 1];
868 obj = yaffs_DentryToObject(f->f_dentry);
871 yaffs_GrossLock(dev);
875 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
879 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
881 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
890 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
891 (int)f->f_dentry->d_parent->d_inode->i_ino));
893 (dirent, "..", 2, offset,
894 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
903 /* If the directory has changed since the open or last call to
904 readdir, rewind to after the 2 canned entries. */
906 if (f->f_version != inode->i_version) {
909 f->f_version = inode->i_version;
912 list_for_each(i, &obj->variant.directoryVariant.children) {
914 if (curoffs >= offset) {
915 l = list_entry(i, yaffs_Object, siblings);
917 yaffs_GetObjectName(l, name,
918 YAFFS_MAX_NAME_LENGTH + 1);
920 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
921 yaffs_GetObjectInode(l)));
927 yaffs_GetObjectInode(l),
928 yaffs_GetObjectType(l))
941 yaffs_GrossUnlock(dev);
947 * File creation. Allocate an inode, and we're done..
949 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
950 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
953 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
959 yaffs_Object *obj = NULL;
962 yaffs_Object *parent = yaffs_InodeToObject(dir);
965 uid_t uid = current->fsuid;
966 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
968 if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
973 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
974 parent->objectId, parent->variantType));
977 (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
981 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
983 dentry->d_name.name, mode, rdev));
987 yaffs_GrossLock(dev);
989 switch (mode & S_IFMT) {
991 /* Special (socket, fifo, device...) */
992 T(YAFFS_TRACE_OS, (KERN_DEBUG
993 "yaffs_mknod: making special\n"));
994 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
996 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
997 gid, old_encode_dev(rdev));
1000 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
1004 case S_IFREG: /* file */
1005 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1007 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
1010 case S_IFDIR: /* directory */
1012 (KERN_DEBUG "yaffs_mknod: making directory\n"));
1014 yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
1017 case S_IFLNK: /* symlink */
1018 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
1019 obj = NULL; /* Do we ever get here? */
1023 /* Can not call yaffs_get_inode() with gross lock held */
1024 yaffs_GrossUnlock(dev);
1027 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
1028 d_instantiate(dentry, inode);
1030 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
1031 obj->objectId, atomic_read(&inode->i_count)));
1035 (KERN_DEBUG "yaffs_mknod failed making object\n"));
1042 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1045 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
1046 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
1048 /* attempt to fix dir bug - didn't work */
1056 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1057 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1058 struct nameidata *n)
1060 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1063 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1064 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1067 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1074 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1075 dentry->d_name.name));
1077 dev = yaffs_InodeToObject(dir)->myDev;
1079 yaffs_GrossLock(dev);
1081 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1083 if (retVal == YAFFS_OK) {
1084 dentry->d_inode->i_nlink--;
1086 yaffs_GrossUnlock(dev);
1087 mark_inode_dirty(dentry->d_inode);
1090 yaffs_GrossUnlock(dev);
1097 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1098 struct dentry *dentry)
1100 struct inode *inode = old_dentry->d_inode;
1101 yaffs_Object *obj = NULL;
1102 yaffs_Object *link = NULL;
1105 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1107 obj = yaffs_InodeToObject(inode);
1110 yaffs_GrossLock(dev);
1112 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1115 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1120 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1121 d_instantiate(dentry, old_dentry->d_inode);
1122 atomic_inc(&old_dentry->d_inode->i_count);
1124 (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1125 old_dentry->d_inode->i_nlink,
1126 atomic_read(&old_dentry->d_inode->i_count)));
1130 yaffs_GrossUnlock(dev);
1140 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1141 const char *symname)
1145 uid_t uid = current->fsuid;
1146 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1148 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1150 dev = yaffs_InodeToObject(dir)->myDev;
1151 yaffs_GrossLock(dev);
1152 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1153 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1154 yaffs_GrossUnlock(dev);
1158 struct inode *inode;
1160 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1161 d_instantiate(dentry, inode);
1162 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1165 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1172 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1179 obj = yaffs_DentryToObject(dentry);
1183 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1184 yaffs_GrossLock(dev);
1185 yaffs_FlushFile(obj, 1);
1186 yaffs_GrossUnlock(dev);
1191 * The VFS layer already does all the dentry stuff for rename.
1193 * NB: POSIX says you can rename an object over an old object of the same name
1195 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1196 struct inode *new_dir, struct dentry *new_dentry)
1199 int retVal = YAFFS_FAIL;
1200 yaffs_Object *target;
1202 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1203 dev = yaffs_InodeToObject(old_dir)->myDev;
1205 yaffs_GrossLock(dev);
1207 /* Check if the target is an existing directory that is not empty. */
1209 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1210 new_dentry->d_name.name);
1215 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1216 !list_empty(&target->variant.directoryVariant.children)) {
1218 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1220 retVal = YAFFS_FAIL;
1223 /* Now does unlinking internally using shadowing mechanism */
1224 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1227 yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1228 old_dentry->d_name.name,
1229 yaffs_InodeToObject(new_dir),
1230 new_dentry->d_name.name);
1233 yaffs_GrossUnlock(dev);
1235 if (retVal == YAFFS_OK) {
1237 new_dentry->d_inode->i_nlink--;
1238 mark_inode_dirty(new_dentry->d_inode);
1248 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1250 struct inode *inode = dentry->d_inode;
1255 (KERN_DEBUG "yaffs_setattr of object %d\n",
1256 yaffs_InodeToObject(inode)->objectId));
1258 if ((error = inode_change_ok(inode, attr)) == 0) {
1260 dev = yaffs_InodeToObject(inode)->myDev;
1261 yaffs_GrossLock(dev);
1262 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1268 yaffs_GrossUnlock(dev);
1270 error = inode_setattr(inode, attr);
1275 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1276 static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
1278 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1279 struct super_block *sb = dentry->d_sb;
1280 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1281 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1283 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1285 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1287 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1290 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1292 yaffs_GrossLock(dev);
1294 buf->f_type = YAFFS_MAGIC;
1295 buf->f_bsize = sb->s_blocksize;
1296 buf->f_namelen = 255;
1297 if (sb->s_blocksize > dev->nBytesPerChunk) {
1300 (dev->endBlock - dev->startBlock +
1301 1) * dev->nChunksPerBlock / (sb->s_blocksize /
1302 dev->nBytesPerChunk);
1304 yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
1305 dev->nBytesPerChunk);
1309 (dev->endBlock - dev->startBlock +
1310 1) * dev->nChunksPerBlock * (dev->nBytesPerChunk /
1313 yaffs_GetNumberOfFreeChunks(dev) * (dev->nBytesPerChunk /
1318 buf->f_bavail = buf->f_bfree;
1320 yaffs_GrossUnlock(dev);
1326 static int yaffs_do_sync_fs(struct super_block *sb)
1329 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1330 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
1333 yaffs_GrossLock(dev);
1336 yaffs_CheckpointSave(dev);
1338 yaffs_GrossUnlock(dev);
1346 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1347 static void yaffs_write_super(struct super_block *sb)
1349 static int yaffs_write_super(struct super_block *sb)
1353 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
1354 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
1355 return 0; /* yaffs_do_sync_fs(sb);*/
1360 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1361 static int yaffs_sync_fs(struct super_block *sb, int wait)
1363 static int yaffs_sync_fs(struct super_block *sb)
1367 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
1369 return 0; /* yaffs_do_sync_fs(sb);*/
1374 static void yaffs_read_inode(struct inode *inode)
1376 /* NB This is called as a side effect of other functions, but
1377 * we had to release the lock to prevent deadlocks, so
1378 * need to lock again.
1382 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1385 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1387 yaffs_GrossLock(dev);
1389 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1391 yaffs_FillInodeFromObject(inode, obj);
1393 yaffs_GrossUnlock(dev);
1396 static LIST_HEAD(yaffs_dev_list);
1398 static void yaffs_put_super(struct super_block *sb)
1400 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1402 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
1404 yaffs_GrossLock(dev);
1406 yaffs_FlushEntireDeviceCache(dev);
1408 if (dev->putSuperFunc) {
1409 dev->putSuperFunc(sb);
1412 yaffs_CheckpointSave(dev);
1413 yaffs_Deinitialise(dev);
1415 yaffs_GrossUnlock(dev);
1417 /* we assume this is protected by lock_kernel() in mount/umount */
1418 list_del(&dev->devList);
1420 if(dev->spareBuffer){
1421 YFREE(dev->spareBuffer);
1422 dev->spareBuffer = NULL;
1429 static void yaffs_MTDPutSuper(struct super_block *sb)
1432 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1438 put_mtd_device(mtd);
1442 static void yaffs_MarkSuperBlockDirty(void *vsb)
1444 struct super_block *sb = (struct super_block *)vsb;
1446 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
1451 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1452 struct super_block *sb,
1453 void *data, int silent)
1456 struct inode *inode = NULL;
1457 struct dentry *root;
1458 yaffs_Device *dev = 0;
1459 char devname_buf[BDEVNAME_SIZE + 1];
1460 struct mtd_info *mtd;
1463 sb->s_magic = YAFFS_MAGIC;
1464 sb->s_op = &yaffs_super_ops;
1467 printk(KERN_INFO "yaffs: sb is NULL\n");
1468 else if (!sb->s_dev)
1469 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1470 else if (!yaffs_devname(sb, devname_buf))
1471 printk(KERN_INFO "yaffs: devname is NULL\n");
1473 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1475 yaffs_devname(sb, devname_buf));
1477 sb->s_blocksize = PAGE_CACHE_SIZE;
1478 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1479 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1481 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1483 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1485 ("yaffs: Write verification disabled. All guarantees "
1486 "null and void\n"));
1489 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1491 MAJOR(sb->s_dev), MINOR(sb->s_dev),
1492 yaffs_devname(sb, devname_buf)));
1494 /* Check it's an mtd device..... */
1495 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1496 return NULL; /* This isn't an mtd device */
1498 /* Get the device */
1499 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1501 T(YAFFS_TRACE_ALWAYS,
1502 ("yaffs: MTD device #%u doesn't appear to exist\n",
1506 /* Check it's NAND */
1507 if (mtd->type != MTD_NANDFLASH) {
1508 T(YAFFS_TRACE_ALWAYS,
1509 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1513 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1514 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1515 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1516 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1517 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1518 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1519 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1520 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1521 T(YAFFS_TRACE_OS, (" writesize %d\n", mtd->writesize));
1523 T(YAFFS_TRACE_OS, (" oobblock %d\n", mtd->oobblock));
1525 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1526 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1527 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1529 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1531 if (yaffsVersion == 1 &&
1532 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1533 mtd->writesize >= 2048) {
1535 mtd->oobblock >= 2048) {
1537 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1541 /* Added NCB 26/5/2006 for completeness */
1542 if (yaffsVersion == 2 &&
1543 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1544 mtd->writesize == 512) {
1546 mtd->oobblock >= 512) {
1548 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
1554 if (yaffsVersion == 2) {
1555 /* Check for version 2 style functions */
1557 !mtd->block_isbad ||
1558 !mtd->block_markbad ||
1561 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1562 !mtd->read_oob || !mtd->write_oob) {
1565 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1567 T(YAFFS_TRACE_ALWAYS,
1568 ("yaffs: MTD device does not support required "
1573 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1574 if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1576 if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1578 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
1579 T(YAFFS_TRACE_ALWAYS,
1580 ("yaffs: MTD device does not have the "
1581 "right page sizes\n"));
1585 /* Check for V1 style functions */
1589 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1590 !mtd->read_oob || !mtd->write_oob) {
1593 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1595 T(YAFFS_TRACE_ALWAYS,
1596 ("yaffs: MTD device does not support required "
1601 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1602 if (mtd->writesize < YAFFS_BYTES_PER_CHUNK ||
1604 if (mtd->oobblock < YAFFS_BYTES_PER_CHUNK ||
1606 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1607 T(YAFFS_TRACE_ALWAYS,
1608 ("yaffs: MTD device does not support have the "
1609 "right page sizes\n"));
1614 /* OK, so if we got here, we have an MTD that's NAND and looks
1615 * like it has the right capabilities
1616 * Set the yaffs_Device up for mtd
1619 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1620 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1622 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1625 /* Deep shit could not allocate device structure */
1626 T(YAFFS_TRACE_ALWAYS,
1627 ("yaffs_read_super: Failed trying to allocate "
1628 "yaffs_Device. \n"));
1632 memset(dev, 0, sizeof(yaffs_Device));
1633 dev->genericDevice = mtd;
1634 dev->name = mtd->name;
1636 /* Set up the memory size parameters.... */
1638 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1639 dev->startBlock = 0;
1640 dev->endBlock = nBlocks - 1;
1641 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1642 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1643 dev->nReservedBlocks = 5;
1644 dev->nShortOpCaches = 10; /* Enable short op caching */
1646 /* ... and the functions. */
1647 if (yaffsVersion == 2) {
1648 dev->writeChunkWithTagsToNAND =
1649 nandmtd2_WriteChunkWithTagsToNAND;
1650 dev->readChunkWithTagsFromNAND =
1651 nandmtd2_ReadChunkWithTagsFromNAND;
1652 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1653 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1654 dev->spareBuffer = YMALLOC(mtd->oobsize);
1656 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1657 dev->nBytesPerChunk = mtd->writesize;
1658 dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
1660 dev->nBytesPerChunk = mtd->oobblock;
1661 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1663 nBlocks = mtd->size / mtd->erasesize;
1665 dev->nCheckpointReservedBlocks = 10;
1666 dev->startBlock = 0;
1667 dev->endBlock = nBlocks - 1;
1669 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1670 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1673 /* ... and common functions */
1674 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1675 dev->initialiseNAND = nandmtd_InitialiseNAND;
1677 dev->putSuperFunc = yaffs_MTDPutSuper;
1679 dev->superBlock = (void *)sb;
1680 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
1683 #ifndef CONFIG_YAFFS_DOES_ECC
1684 dev->useNANDECC = 1;
1687 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1688 dev->wideTnodesDisabled = 1;
1691 /* we assume this is protected by lock_kernel() in mount/umount */
1692 list_add_tail(&dev->devList, &yaffs_dev_list);
1694 init_MUTEX(&dev->grossLock);
1696 yaffs_GrossLock(dev);
1698 err = yaffs_GutsInitialise(dev);
1701 ("yaffs_read_super: guts initialised %s\n",
1702 (err == YAFFS_OK) ? "OK" : "FAILED"));
1704 /* Release lock before yaffs_get_inode() */
1705 yaffs_GrossUnlock(dev);
1707 /* Create root inode */
1708 if (err == YAFFS_OK)
1709 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1715 inode->i_op = &yaffs_dir_inode_operations;
1716 inode->i_fop = &yaffs_dir_operations;
1718 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1720 root = d_alloc_root(inode);
1722 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1730 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1735 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1736 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
1739 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
1742 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1743 static int yaffs_read_super(struct file_system_type *fs,
1744 int flags, const char *dev_name,
1745 void *data, struct vfsmount *mnt)
1748 return get_sb_bdev(fs, flags, dev_name, data,
1749 yaffs_internal_read_super_mtd, mnt);
1752 static struct super_block *yaffs_read_super(struct file_system_type *fs,
1753 int flags, const char *dev_name,
1757 return get_sb_bdev(fs, flags, dev_name, data,
1758 yaffs_internal_read_super_mtd);
1762 static struct file_system_type yaffs_fs_type = {
1763 .owner = THIS_MODULE,
1765 .get_sb = yaffs_read_super,
1766 .kill_sb = kill_block_super,
1767 .fs_flags = FS_REQUIRES_DEV,
1770 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
1773 return yaffs_internal_read_super(1, sb, data, silent);
1776 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
1781 #ifdef CONFIG_YAFFS_YAFFS2
1783 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1784 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
1787 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
1790 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1791 static int yaffs2_read_super(struct file_system_type *fs,
1792 int flags, const char *dev_name, void *data,
1793 struct vfsmount *mnt)
1795 return get_sb_bdev(fs, flags, dev_name, data,
1796 yaffs2_internal_read_super_mtd, mnt);
1799 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
1800 int flags, const char *dev_name,
1804 return get_sb_bdev(fs, flags, dev_name, data,
1805 yaffs2_internal_read_super_mtd);
1809 static struct file_system_type yaffs2_fs_type = {
1810 .owner = THIS_MODULE,
1812 .get_sb = yaffs2_read_super,
1813 .kill_sb = kill_block_super,
1814 .fs_flags = FS_REQUIRES_DEV,
1817 static struct super_block *yaffs2_read_super(struct super_block *sb,
1818 void *data, int silent)
1820 return yaffs_internal_read_super(2, sb, data, silent);
1823 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
1827 #endif /* CONFIG_YAFFS_YAFFS2 */
1829 static struct proc_dir_entry *my_proc_entry;
1831 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
1833 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
1834 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
1835 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
1836 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
1837 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
1838 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
1839 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
1840 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
1841 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
1842 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
1843 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
1844 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
1845 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
1846 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
1848 sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
1850 sprintf(buf, "passiveGCs......... %d\n",
1851 dev->passiveGarbageCollections);
1852 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
1853 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
1854 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
1855 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
1856 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
1857 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
1858 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
1859 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
1860 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
1862 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
1863 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
1864 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
1869 static int yaffs_proc_read(char *page,
1871 off_t offset, int count, int *eof, void *data)
1873 struct list_head *item;
1878 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
1879 * We use 'offset' (*ppos) to indicate where we are in devList.
1880 * This also assumes the user has posted a read buffer large
1881 * enough to hold the complete output; but that's life in /proc.
1886 /* Print header first */
1888 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
1889 "\n%s\n%s\n", yaffs_fs_c_version,
1890 yaffs_guts_c_version);
1893 /* hold lock_kernel while traversing yaffs_dev_list */
1896 /* Locate and print the Nth entry. Order N-squared but N is small. */
1897 list_for_each(item, &yaffs_dev_list) {
1898 yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
1903 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
1904 buf = yaffs_dump_dev(buf, dev);
1909 return buf - page < count ? buf - page : count;
1913 * Set the verbosity of the warnings and error messages.
1919 unsigned mask_bitfield;
1921 {"allocate", YAFFS_TRACE_ALLOCATE},
1922 {"always", YAFFS_TRACE_ALWAYS},
1923 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
1924 {"buffers", YAFFS_TRACE_BUFFERS},
1925 {"bug", YAFFS_TRACE_BUG},
1926 {"deletion", YAFFS_TRACE_DELETION},
1927 {"erase", YAFFS_TRACE_ERASE},
1928 {"error", YAFFS_TRACE_ERROR},
1929 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
1930 {"gc", YAFFS_TRACE_GC},
1931 {"mtd", YAFFS_TRACE_MTD},
1932 {"nandaccess", YAFFS_TRACE_NANDACCESS},
1933 {"os", YAFFS_TRACE_OS},
1934 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
1935 {"scan", YAFFS_TRACE_SCAN},
1936 {"tracing", YAFFS_TRACE_TRACING},
1937 {"write", YAFFS_TRACE_WRITE},
1938 {"all", 0xffffffff},
1943 static int yaffs_proc_write(struct file *file, const char *buf,
1944 unsigned long count, void *data)
1946 unsigned rg = 0, mask_bitfield;
1947 char *end, *mask_name;
1953 rg = yaffs_traceMask;
1955 while (!done && (pos < count)) {
1957 while ((pos < count) && isspace(buf[pos])) {
1974 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
1975 if (end > buf + pos) {
1976 mask_name = "numeral";
1977 len = end - (buf + pos);
1981 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
1982 len = strlen(mask_flags[i].mask_name);
1983 if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
1984 mask_name = mask_flags[i].mask_name;
1985 mask_bitfield = mask_flags[i].mask_bitfield;
1992 if (mask_name != NULL) {
1997 rg &= ~mask_bitfield;
2000 rg |= mask_bitfield;
2006 rg |= mask_bitfield;
2012 yaffs_traceMask = rg;
2013 if (rg & YAFFS_TRACE_ALWAYS) {
2014 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
2016 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
2017 printk("%c%s\n", flag, mask_flags[i].mask_name);
2024 /* Stuff to handle installation of file systems */
2025 struct file_system_to_install {
2026 struct file_system_type *fst;
2030 static struct file_system_to_install fs_to_install[] = {
2031 //#ifdef CONFIG_YAFFS_YAFFS1
2032 {&yaffs_fs_type, 0},
2034 //#ifdef CONFIG_YAFFS_YAFFS2
2035 {&yaffs2_fs_type, 0},
2040 static int __init init_yaffs_fs(void)
2043 struct file_system_to_install *fsinst;
2045 T(YAFFS_TRACE_ALWAYS,
2046 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
2048 /* Install the proc_fs entry */
2049 my_proc_entry = create_proc_entry("yaffs",
2053 if (my_proc_entry) {
2054 my_proc_entry->write_proc = yaffs_proc_write;
2055 my_proc_entry->read_proc = yaffs_proc_read;
2056 my_proc_entry->data = NULL;
2061 /* Now add the file system entries */
2063 fsinst = fs_to_install;
2065 while (fsinst->fst && !error) {
2066 error = register_filesystem(fsinst->fst);
2068 fsinst->installed = 1;
2073 /* Any errors? uninstall */
2075 fsinst = fs_to_install;
2077 while (fsinst->fst) {
2078 if (fsinst->installed) {
2079 unregister_filesystem(fsinst->fst);
2080 fsinst->installed = 0;
2089 static void __exit exit_yaffs_fs(void)
2092 struct file_system_to_install *fsinst;
2094 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
2097 remove_proc_entry("yaffs", &proc_root);
2099 fsinst = fs_to_install;
2101 while (fsinst->fst) {
2102 if (fsinst->installed) {
2103 unregister_filesystem(fsinst->fst);
2104 fsinst->installed = 0;
2111 module_init(init_yaffs_fs)
2112 module_exit(exit_yaffs_fs)
2114 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
2115 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
2116 MODULE_LICENSE("GPL");