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.44 2006-03-21 20:31:08 charles 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)
71 #include <asm/uaccess.h>
74 #include "yaffs_guts.h"
76 unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS | YAFFS_TRACE_BAD_BLOCKS /* | 0xFFFFFFFF */;
78 #include <linux/mtd/mtd.h>
79 #include "yaffs_mtdif.h"
80 #include "yaffs_mtdif2.h"
82 /*#define T(x) printk x */
84 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
85 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
87 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
88 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
90 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
93 static void yaffs_put_super(struct super_block *sb);
95 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
98 static int yaffs_file_flush(struct file *file);
100 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
103 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
105 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
106 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
107 struct nameidata *n);
108 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
109 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,
115 struct dentry *dentry);
116 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
117 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
118 const char *symname);
119 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
121 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
122 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
125 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
128 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
129 struct inode *new_dir, struct dentry *new_dentry);
130 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
132 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
133 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
135 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
137 static void yaffs_read_inode(struct inode *inode);
139 static void yaffs_put_inode(struct inode *inode);
140 static void yaffs_delete_inode(struct inode *);
141 static void yaffs_clear_inode(struct inode *);
143 static int yaffs_readpage(struct file *file, struct page *page);
144 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
145 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
147 static int yaffs_writepage(struct page *page);
149 static int yaffs_prepare_write(struct file *f, struct page *pg,
150 unsigned offset, unsigned to);
151 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
154 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
156 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
157 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
159 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
162 static struct address_space_operations yaffs_file_address_operations = {
163 .readpage = yaffs_readpage,
164 .writepage = yaffs_writepage,
165 .prepare_write = yaffs_prepare_write,
166 .commit_write = yaffs_commit_write,
169 static struct file_operations yaffs_file_operations = {
170 .read = generic_file_read,
171 .write = generic_file_write,
172 .mmap = generic_file_mmap,
173 .flush = yaffs_file_flush,
174 .fsync = yaffs_sync_object,
175 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
176 .sendfile = generic_file_sendfile,
181 static struct inode_operations yaffs_file_inode_operations = {
182 .setattr = yaffs_setattr,
185 static struct inode_operations yaffs_symlink_inode_operations = {
186 .readlink = yaffs_readlink,
187 .follow_link = yaffs_follow_link,
188 .setattr = yaffs_setattr,
191 static struct inode_operations yaffs_dir_inode_operations = {
192 .create = yaffs_create,
193 .lookup = yaffs_lookup,
195 .unlink = yaffs_unlink,
196 .symlink = yaffs_symlink,
197 .mkdir = yaffs_mkdir,
198 .rmdir = yaffs_unlink,
199 .mknod = yaffs_mknod,
200 .rename = yaffs_rename,
201 .setattr = yaffs_setattr,
204 static struct file_operations yaffs_dir_operations = {
205 .read = generic_read_dir,
206 .readdir = yaffs_readdir,
207 .fsync = yaffs_sync_object,
210 static struct super_operations yaffs_super_ops = {
211 .statfs = yaffs_statfs,
212 .read_inode = yaffs_read_inode,
213 .put_inode = yaffs_put_inode,
214 .put_super = yaffs_put_super,
215 .delete_inode = yaffs_delete_inode,
216 .clear_inode = yaffs_clear_inode,
219 static void yaffs_GrossLock(yaffs_Device * dev)
221 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
223 down(&dev->grossLock);
226 static void yaffs_GrossUnlock(yaffs_Device * dev)
228 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
233 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
236 unsigned char *alias;
239 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
241 yaffs_GrossLock(dev);
243 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
245 yaffs_GrossUnlock(dev);
250 ret = vfs_readlink(dentry, buffer, buflen, alias);
255 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
256 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
258 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
261 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);
277 ret = vfs_follow_link(nd, alias);
280 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
281 return ERR_PTR (ret);
287 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
291 * Lookup is used to find objects in the fs
293 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
295 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
298 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
302 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
304 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
306 yaffs_GrossLock(dev);
309 (KERN_DEBUG "yaffs_lookup for %d:%s\n",
310 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
313 yaffs_FindObjectByName(yaffs_InodeToObject(dir),
314 dentry->d_name.name);
316 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
318 /* Can't hold gross lock when calling yaffs_get_inode() */
319 yaffs_GrossUnlock(dev);
323 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
325 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
329 (KERN_DEBUG "yaffs_loookup dentry \n"));
330 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
331 * d_add even if NULL inode */
333 /*dget(dentry); // try to solve directory bug */
334 d_add(dentry, inode);
342 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
346 /* added NCB for 2.5/6 compatability - forces add even if inode is
347 * NULL which creates dentry hash */
348 d_add(dentry, inode);
351 /* return (ERR_PTR(-EIO)); */
355 /* For now put inode is just for debugging
356 * Put inode is called when the inode **structure** is put.
358 static void yaffs_put_inode(struct inode *inode)
361 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
362 atomic_read(&inode->i_count)));
366 /* clear is called to tell the fs to release any per-inode data it holds */
367 static void yaffs_clear_inode(struct inode *inode)
372 obj = yaffs_InodeToObject(inode);
375 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
376 atomic_read(&inode->i_count),
377 obj ? "object exists" : "null object"));
381 yaffs_GrossLock(dev);
383 /* Clear the association between the inode and
387 inode->u.generic_ip = NULL;
389 /* If the object freeing was deferred, then the real
391 * This should fix the inode inconsistency problem.
394 yaffs_HandleDeferedFree(obj);
396 yaffs_GrossUnlock(dev);
401 /* delete is called when the link count is zero and the inode
402 * is put (ie. nobody wants to know about it anymore, time to
404 * NB Must call clear_inode()
406 static void yaffs_delete_inode(struct inode *inode)
408 yaffs_Object *obj = yaffs_InodeToObject(inode);
412 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
413 atomic_read(&inode->i_count),
414 obj ? "object exists" : "null object"));
418 yaffs_GrossLock(dev);
419 yaffs_DeleteFile(obj);
420 yaffs_GrossUnlock(dev);
422 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
423 truncate_inode_pages (&inode->i_data, 0);
428 static int yaffs_file_flush(struct file *file)
430 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
432 yaffs_Device *dev = obj->myDev;
435 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
436 obj->dirty ? "dirty" : "clean"));
438 yaffs_GrossLock(dev);
440 yaffs_FlushFile(obj, 1);
442 yaffs_GrossUnlock(dev);
447 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
449 /* Lifted from jffs2 */
452 unsigned char *pg_buf;
457 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
458 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
459 (unsigned)PAGE_CACHE_SIZE));
461 obj = yaffs_DentryToObject(f->f_dentry);
465 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
466 BUG_ON(!PageLocked(pg));
473 /* FIXME: Can kmap fail? */
475 yaffs_GrossLock(dev);
478 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
481 yaffs_GrossUnlock(dev);
487 ClearPageUptodate(pg);
494 flush_dcache_page(pg);
497 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
501 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
503 int ret = yaffs_readpage_nolock(f, pg);
508 static int yaffs_readpage(struct file *f, struct page *pg)
510 return yaffs_readpage_unlock(f, pg);
513 /* writepage inspired by/stolen from smbfs */
515 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
516 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
518 static int yaffs_writepage(struct page *page)
521 struct address_space *mapping = page->mapping;
522 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
524 unsigned long end_index;
532 inode = mapping->host;
536 if (offset > inode->i_size) {
539 "yaffs_writepage at %08x, inode size = %08x!!!\n",
540 (unsigned)(page->index << PAGE_CACHE_SHIFT),
541 (unsigned)inode->i_size));
543 (KERN_DEBUG " -> don't care!!\n"));
548 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
551 if (page->index < end_index) {
552 nBytes = PAGE_CACHE_SIZE;
554 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
561 obj = yaffs_InodeToObject(inode);
562 yaffs_GrossLock(obj->myDev);
565 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
566 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
568 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
569 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
572 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
576 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
577 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
579 yaffs_GrossUnlock(obj->myDev);
582 SetPageUptodate(page);
586 return (nWritten == nBytes) ? 0 : -ENOSPC;
589 static int yaffs_prepare_write(struct file *f, struct page *pg,
590 unsigned offset, unsigned to)
593 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
594 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
595 return yaffs_readpage_nolock(f, pg);
601 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
605 void *addr = page_address(pg) + offset;
606 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
607 int nBytes = to - offset;
611 unsigned saddr = (unsigned)addr;
614 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
617 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
619 if (nWritten != nBytes) {
622 "yaffs_commit_write not same size nWritten %d nBytes %d\n",
625 ClearPageUptodate(pg);
631 (KERN_DEBUG "yaffs_commit_write returning %d\n",
632 nWritten == nBytes ? 0 : nWritten));
634 return nWritten == nBytes ? 0 : nWritten;
638 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
641 inode->i_ino = obj->objectId;
642 inode->i_mode = obj->yst_mode;
643 inode->i_uid = obj->yst_uid;
644 inode->i_gid = obj->yst_gid;
645 inode->i_blksize = inode->i_sb->s_blocksize;
646 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
648 inode->i_rdev = old_decode_dev(obj->yst_rdev);
649 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
650 inode->i_atime.tv_nsec = 0;
651 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
652 inode->i_mtime.tv_nsec = 0;
653 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
654 inode->i_ctime.tv_nsec = 0;
656 inode->i_rdev = obj->yst_rdev;
657 inode->i_atime = obj->yst_atime;
658 inode->i_mtime = obj->yst_mtime;
659 inode->i_ctime = obj->yst_ctime;
661 inode->i_size = yaffs_GetObjectFileLength(obj);
662 inode->i_blocks = (inode->i_size + 511) >> 9;
664 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
668 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
669 inode->i_mode, inode->i_uid, inode->i_gid,
670 (int)inode->i_size, atomic_read(&inode->i_count)));
672 switch (obj->yst_mode & S_IFMT) {
673 default: /* fifo, device or socket */
674 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
675 init_special_inode(inode, obj->yst_mode,
676 old_decode_dev(obj->yst_rdev));
678 init_special_inode(inode, obj->yst_mode,
679 (dev_t) (obj->yst_rdev));
682 case S_IFREG: /* file */
683 inode->i_op = &yaffs_file_inode_operations;
684 inode->i_fop = &yaffs_file_operations;
685 inode->i_mapping->a_ops =
686 &yaffs_file_address_operations;
688 case S_IFDIR: /* directory */
689 inode->i_op = &yaffs_dir_inode_operations;
690 inode->i_fop = &yaffs_dir_operations;
692 case S_IFLNK: /* symlink */
693 inode->i_op = &yaffs_symlink_inode_operations;
697 inode->u.generic_ip = obj;
698 obj->myInode = inode;
702 (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
707 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
714 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
721 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
727 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
729 inode = iget(sb, obj->objectId);
731 /* NB Side effect: iget calls back to yaffs_read_inode(). */
732 /* iget also increments the inode's i_count */
733 /* NB You can't be holding grossLock or deadlock will happen! */
738 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
746 obj = yaffs_DentryToObject(f->f_dentry);
750 yaffs_GrossLock(dev);
752 inode = f->f_dentry->d_inode;
754 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
755 ipos = inode->i_size;
762 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
766 "yaffs_file_write about to write writing %d bytes"
767 "to object %d at %d\n",
768 n, obj->objectId, ipos));
771 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
774 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
779 if (ipos > inode->i_size) {
780 inode->i_size = ipos;
781 inode->i_blocks = (ipos + 511) >> 9;
785 "yaffs_file_write size updated to %d bytes, "
787 ipos, (int)(inode->i_blocks)));
791 yaffs_GrossUnlock(dev);
792 return nWritten == 0 ? -ENOSPC : nWritten;
795 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
799 struct inode *inode = f->f_dentry->d_inode;
800 unsigned long offset, curoffs;
804 char name[YAFFS_MAX_NAME_LENGTH + 1];
806 obj = yaffs_DentryToObject(f->f_dentry);
809 yaffs_GrossLock(dev);
813 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
817 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
819 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
828 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
829 (int)f->f_dentry->d_parent->d_inode->i_ino));
831 (dirent, "..", 2, offset,
832 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
841 /* If the directory has changed since the open or last call to
842 readdir, rewind to after the 2 canned entries. */
844 if (f->f_version != inode->i_version) {
847 f->f_version = inode->i_version;
850 list_for_each(i, &obj->variant.directoryVariant.children) {
852 if (curoffs >= offset) {
853 l = list_entry(i, yaffs_Object, siblings);
855 yaffs_GetObjectName(l, name,
856 YAFFS_MAX_NAME_LENGTH + 1);
858 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
859 yaffs_GetObjectInode(l)));
865 yaffs_GetObjectInode(l),
866 yaffs_GetObjectType(l))
879 yaffs_GrossUnlock(dev);
885 * File creation. Allocate an inode, and we're done..
887 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
888 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
891 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
897 yaffs_Object *obj = NULL;
900 yaffs_Object *parent = yaffs_InodeToObject(dir);
903 uid_t uid = current->fsuid;
904 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
908 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
909 parent->objectId, parent->variantType));
912 (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
916 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
918 dentry->d_name.name, mode, rdev));
922 yaffs_GrossLock(dev);
924 switch (mode & S_IFMT) {
926 /* Special (socket, fifo, device...) */
927 T(YAFFS_TRACE_OS, (KERN_DEBUG
928 "yaffs_mknod: making special\n"));
929 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
931 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
932 gid, old_encode_dev(rdev));
935 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
939 case S_IFREG: /* file */
940 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
942 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
945 case S_IFDIR: /* directory */
947 (KERN_DEBUG "yaffs_mknod: making directory\n"));
949 yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
952 case S_IFLNK: /* symlink */
953 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
954 obj = NULL; /* Do we ever get here? */
958 /* Can not call yaffs_get_inode() with gross lock held */
959 yaffs_GrossUnlock(dev);
962 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
963 d_instantiate(dentry, inode);
965 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
966 obj->objectId, atomic_read(&inode->i_count)));
970 (KERN_DEBUG "yaffs_mknod failed making object\n"));
977 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
980 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
981 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
983 /* attempt to fix dir bug - didn't work */
991 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
992 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
995 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
998 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
999 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1002 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1009 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1010 dentry->d_name.name));
1012 dev = yaffs_InodeToObject(dir)->myDev;
1014 yaffs_GrossLock(dev);
1016 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1018 if (retVal == YAFFS_OK) {
1019 dentry->d_inode->i_nlink--;
1021 yaffs_GrossUnlock(dev);
1022 mark_inode_dirty(dentry->d_inode);
1025 yaffs_GrossUnlock(dev);
1032 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1033 struct dentry *dentry)
1035 struct inode *inode = old_dentry->d_inode;
1036 yaffs_Object *obj = NULL;
1037 yaffs_Object *link = NULL;
1040 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1042 obj = yaffs_InodeToObject(inode);
1045 yaffs_GrossLock(dev);
1047 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1050 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1055 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1056 d_instantiate(dentry, old_dentry->d_inode);
1057 atomic_inc(&old_dentry->d_inode->i_count);
1059 (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1060 old_dentry->d_inode->i_nlink,
1061 atomic_read(&old_dentry->d_inode->i_count)));
1065 yaffs_GrossUnlock(dev);
1075 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1076 const char *symname)
1080 uid_t uid = current->fsuid;
1081 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1083 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1085 dev = yaffs_InodeToObject(dir)->myDev;
1086 yaffs_GrossLock(dev);
1087 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1088 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1089 yaffs_GrossUnlock(dev);
1093 struct inode *inode;
1095 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1096 d_instantiate(dentry, inode);
1097 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1100 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1107 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1114 obj = yaffs_DentryToObject(dentry);
1118 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1119 yaffs_GrossLock(dev);
1120 yaffs_FlushFile(obj, 1);
1121 yaffs_GrossUnlock(dev);
1126 * The VFS layer already does all the dentry stuff for rename.
1128 * NB: POSIX says you can rename an object over an old object of the same name
1130 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1131 struct inode *new_dir, struct dentry *new_dentry)
1134 int retVal = YAFFS_FAIL;
1135 yaffs_Object *target;
1137 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1138 dev = yaffs_InodeToObject(old_dir)->myDev;
1140 yaffs_GrossLock(dev);
1142 /* Check if the target is an existing directory that is not empty. */
1144 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1145 new_dentry->d_name.name);
1150 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1151 !list_empty(&target->variant.directoryVariant.children)) {
1153 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1155 retVal = YAFFS_FAIL;
1158 /* Now does unlinking internally using shadowing mechanism */
1159 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1162 yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1163 old_dentry->d_name.name,
1164 yaffs_InodeToObject(new_dir),
1165 new_dentry->d_name.name);
1168 yaffs_GrossUnlock(dev);
1170 if (retVal == YAFFS_OK) {
1172 new_dentry->d_inode->i_nlink--;
1173 mark_inode_dirty(new_dentry->d_inode);
1183 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1185 struct inode *inode = dentry->d_inode;
1190 (KERN_DEBUG "yaffs_setattr of object %d\n",
1191 yaffs_InodeToObject(inode)->objectId));
1193 if ((error = inode_change_ok(inode, attr)) == 0) {
1195 dev = yaffs_InodeToObject(inode)->myDev;
1196 yaffs_GrossLock(dev);
1197 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1203 yaffs_GrossUnlock(dev);
1205 error = inode_setattr(inode, attr);
1210 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1211 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1213 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1217 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1218 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1220 yaffs_GrossLock(dev);
1222 buf->f_type = YAFFS_MAGIC;
1223 buf->f_bsize = sb->s_blocksize;
1224 buf->f_namelen = 255;
1225 if (sb->s_blocksize > dev->nBytesPerChunk) {
1228 (dev->endBlock - dev->startBlock +
1229 1) * dev->nChunksPerBlock / (sb->s_blocksize /
1230 dev->nBytesPerChunk);
1232 yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
1233 dev->nBytesPerChunk);
1237 (dev->endBlock - dev->startBlock +
1238 1) * dev->nChunksPerBlock * (dev->nBytesPerChunk /
1241 yaffs_GetNumberOfFreeChunks(dev) * (dev->nBytesPerChunk /
1246 buf->f_bavail = buf->f_bfree;
1248 yaffs_GrossUnlock(dev);
1252 static void yaffs_read_inode(struct inode *inode)
1254 /* NB This is called as a side effect of other functions, but
1255 * we had to release the lock to prevent deadlocks, so
1256 * need to lock again.
1260 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1263 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1265 yaffs_GrossLock(dev);
1267 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1269 yaffs_FillInodeFromObject(inode, obj);
1271 yaffs_GrossUnlock(dev);
1274 static LIST_HEAD(yaffs_dev_list);
1276 static void yaffs_put_super(struct super_block *sb)
1278 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1280 yaffs_GrossLock(dev);
1282 yaffs_FlushEntireDeviceCache(dev);
1284 if (dev->putSuperFunc) {
1285 dev->putSuperFunc(sb);
1287 yaffs_Deinitialise(dev);
1288 yaffs_GrossUnlock(dev);
1290 /* we assume this is protected by lock_kernel() in mount/umount */
1291 list_del(&dev->devList);
1293 if(dev->spareBuffer){
1294 YFREE(dev->spareBuffer);
1295 dev->spareBuffer = NULL;
1302 static void yaffs_MTDPutSuper(struct super_block *sb)
1305 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1311 put_mtd_device(mtd);
1315 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1316 struct super_block *sb,
1317 void *data, int silent)
1320 struct inode *inode = NULL;
1321 struct dentry *root;
1322 yaffs_Device *dev = 0;
1323 char devname_buf[BDEVNAME_SIZE + 1];
1324 struct mtd_info *mtd;
1327 sb->s_magic = YAFFS_MAGIC;
1328 sb->s_op = &yaffs_super_ops;
1331 printk(KERN_INFO "yaffs: sb is NULL\n");
1332 else if (!sb->s_dev)
1333 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1334 else if (!yaffs_devname(sb, devname_buf))
1335 printk(KERN_INFO "yaffs: devname is NULL\n");
1337 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1339 yaffs_devname(sb, devname_buf));
1341 sb->s_blocksize = PAGE_CACHE_SIZE;
1342 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1343 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1345 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1347 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1349 ("yaffs: Write verification disabled. All guarantees "
1350 "null and void\n"));
1353 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1355 MAJOR(sb->s_dev), MINOR(sb->s_dev),
1356 yaffs_devname(sb, devname_buf)));
1358 /* Check it's an mtd device..... */
1359 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1360 return NULL; /* This isn't an mtd device */
1362 /* Get the device */
1363 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1365 T(YAFFS_TRACE_ALWAYS,
1366 ("yaffs: MTD device #%u doesn't appear to exist\n",
1370 /* Check it's NAND */
1371 if (mtd->type != MTD_NANDFLASH) {
1372 T(YAFFS_TRACE_ALWAYS,
1373 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1377 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1378 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1379 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1380 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1381 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1382 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1383 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1384 T(YAFFS_TRACE_OS, (" oobblock %d\n", mtd->oobblock));
1385 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1386 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1387 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1389 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1391 if (yaffsVersion == 1 &&
1392 mtd->oobblock >= 2048) {
1393 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1398 if (yaffsVersion == 2) {
1399 /* Check for version 2 style functions */
1401 !mtd->block_isbad ||
1402 !mtd->block_markbad ||
1406 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1407 T(YAFFS_TRACE_ALWAYS,
1408 ("yaffs: MTD device does not support required "
1413 if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1414 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
1415 T(YAFFS_TRACE_ALWAYS,
1416 ("yaffs: MTD device does not support have the "
1417 "right page sizes\n"));
1421 /* Check for V1 style functions */
1426 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1427 T(YAFFS_TRACE_ALWAYS,
1428 ("yaffs: MTD device does not support required "
1433 if (mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
1434 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1435 T(YAFFS_TRACE_ALWAYS,
1436 ("yaffs: MTD device does not support have the "
1437 "right page sizes\n"));
1442 /* OK, so if we got here, we have an MTD that's NAND and looks
1443 * like it has the right capabilities
1444 * Set the yaffs_Device up for mtd
1447 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1448 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1450 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1453 /* Deep shit could not allocate device structure */
1454 T(YAFFS_TRACE_ALWAYS,
1455 ("yaffs_read_super: Failed trying to allocate "
1456 "yaffs_Device. \n"));
1460 memset(dev, 0, sizeof(yaffs_Device));
1461 dev->genericDevice = mtd;
1462 dev->name = mtd->name;
1464 /* Set up the memory size parameters.... */
1466 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1467 dev->startBlock = 0;
1468 dev->endBlock = nBlocks - 1;
1469 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1470 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1471 dev->nReservedBlocks = 5;
1472 dev->nShortOpCaches = 10; /* Enable short op caching */
1474 /* ... and the functions. */
1475 if (yaffsVersion == 2) {
1476 dev->writeChunkWithTagsToNAND =
1477 nandmtd2_WriteChunkWithTagsToNAND;
1478 dev->readChunkWithTagsFromNAND =
1479 nandmtd2_ReadChunkWithTagsFromNAND;
1480 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1481 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1482 dev->spareBuffer = YMALLOC(mtd->oobsize);
1484 dev->nBytesPerChunk = mtd->oobblock;
1485 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1486 nBlocks = mtd->size / mtd->erasesize;
1487 dev->startBlock = 0;
1488 dev->endBlock = nBlocks - 1;
1490 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1491 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1494 /* ... and common functions */
1495 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1496 dev->initialiseNAND = nandmtd_InitialiseNAND;
1498 dev->putSuperFunc = yaffs_MTDPutSuper;
1500 #ifndef CONFIG_YAFFS_DOES_ECC
1501 dev->useNANDECC = 1;
1504 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1505 dev->wideTnodesDisabled = 1;
1508 /* we assume this is protected by lock_kernel() in mount/umount */
1509 list_add_tail(&dev->devList, &yaffs_dev_list);
1511 init_MUTEX(&dev->grossLock);
1513 yaffs_GrossLock(dev);
1515 err = yaffs_GutsInitialise(dev);
1518 ("yaffs_read_super: guts initialised %s\n",
1519 (err == YAFFS_OK) ? "OK" : "FAILED"));
1521 /* Release lock before yaffs_get_inode() */
1522 yaffs_GrossUnlock(dev);
1524 /* Create root inode */
1525 if (err == YAFFS_OK)
1526 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1532 inode->i_op = &yaffs_dir_inode_operations;
1533 inode->i_fop = &yaffs_dir_operations;
1535 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1537 root = d_alloc_root(inode);
1539 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1547 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1552 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1553 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
1556 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
1559 static struct super_block *yaffs_read_super(struct file_system_type *fs,
1560 int flags, const char *dev_name,
1564 return get_sb_bdev(fs, flags, dev_name, data,
1565 yaffs_internal_read_super_mtd);
1568 static struct file_system_type yaffs_fs_type = {
1569 .owner = THIS_MODULE,
1571 .get_sb = yaffs_read_super,
1572 .kill_sb = kill_block_super,
1573 .fs_flags = FS_REQUIRES_DEV,
1576 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
1579 return yaffs_internal_read_super(1, sb, data, silent);
1582 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
1587 #ifdef CONFIG_YAFFS_YAFFS2
1589 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1590 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
1593 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
1596 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
1597 int flags, const char *dev_name,
1601 return get_sb_bdev(fs, flags, dev_name, data,
1602 yaffs2_internal_read_super_mtd);
1605 static struct file_system_type yaffs2_fs_type = {
1606 .owner = THIS_MODULE,
1608 .get_sb = yaffs2_read_super,
1609 .kill_sb = kill_block_super,
1610 .fs_flags = FS_REQUIRES_DEV,
1613 static struct super_block *yaffs2_read_super(struct super_block *sb,
1614 void *data, int silent)
1616 return yaffs_internal_read_super(2, sb, data, silent);
1619 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
1623 #endif /* CONFIG_YAFFS_YAFFS2 */
1625 static struct proc_dir_entry *my_proc_entry;
1627 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
1629 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
1630 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
1631 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
1632 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
1633 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
1634 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
1635 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
1636 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
1637 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
1638 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
1639 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
1640 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
1641 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
1642 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
1644 sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
1646 sprintf(buf, "passiveGCs......... %d\n",
1647 dev->passiveGarbageCollections);
1648 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
1649 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
1650 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
1651 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
1652 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
1653 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
1654 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
1655 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
1656 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
1658 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
1659 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
1660 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
1665 static int yaffs_proc_read(char *page,
1667 off_t offset, int count, int *eof, void *data)
1669 struct list_head *item;
1674 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
1675 * We use 'offset' (*ppos) to indicate where we are in devList.
1676 * This also assumes the user has posted a read buffer large
1677 * enough to hold the complete output; but that's life in /proc.
1682 /* Print header first */
1684 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
1685 "\n%s\n%s\n", yaffs_fs_c_version,
1686 yaffs_guts_c_version);
1689 /* hold lock_kernel while traversing yaffs_dev_list */
1692 /* Locate and print the Nth entry. Order N-squared but N is small. */
1693 list_for_each(item, &yaffs_dev_list) {
1694 yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
1699 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
1700 buf = yaffs_dump_dev(buf, dev);
1705 return buf - page < count ? buf - page : count;
1709 * Set the verbosity of the warnings and error messages.
1715 unsigned mask_bitfield;
1717 {"allocate", YAFFS_TRACE_ALLOCATE},
1718 {"always", YAFFS_TRACE_ALWAYS},
1719 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
1720 {"buffers", YAFFS_TRACE_BUFFERS},
1721 {"bug", YAFFS_TRACE_BUG},
1722 {"deletion", YAFFS_TRACE_DELETION},
1723 {"erase", YAFFS_TRACE_ERASE},
1724 {"error", YAFFS_TRACE_ERROR},
1725 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
1726 {"gc", YAFFS_TRACE_GC},
1727 {"mtd", YAFFS_TRACE_MTD},
1728 {"nandaccess", YAFFS_TRACE_NANDACCESS},
1729 {"os", YAFFS_TRACE_OS},
1730 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
1731 {"scan", YAFFS_TRACE_SCAN},
1732 {"tracing", YAFFS_TRACE_TRACING},
1733 {"write", YAFFS_TRACE_WRITE},
1734 {"all", 0xffffffff},
1739 static int yaffs_proc_write(struct file *file, const char *buf,
1740 unsigned long count, void *data)
1742 unsigned rg = 0, mask_bitfield;
1743 char *end, *mask_name;
1749 rg = yaffs_traceMask;
1751 while (!done && (pos < count)) {
1753 while ((pos < count) && isspace(buf[pos])) {
1770 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
1771 if (end > buf + pos) {
1772 mask_name = "numeral";
1773 len = end - (buf + pos);
1777 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
1778 len = strlen(mask_flags[i].mask_name);
1779 if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
1780 mask_name = mask_flags[i].mask_name;
1781 mask_bitfield = mask_flags[i].mask_bitfield;
1788 if (mask_name != NULL) {
1793 rg &= ~mask_bitfield;
1796 rg |= mask_bitfield;
1802 rg |= mask_bitfield;
1808 yaffs_traceMask = rg;
1809 if (rg & YAFFS_TRACE_ALWAYS) {
1810 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
1812 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
1813 printk("%c%s\n", flag, mask_flags[i].mask_name);
1820 /* Stuff to handle installation of file systems */
1821 struct file_system_to_install {
1822 struct file_system_type *fst;
1826 static struct file_system_to_install fs_to_install[] = {
1827 //#ifdef CONFIG_YAFFS_YAFFS1
1828 {&yaffs_fs_type, 0},
1830 //#ifdef CONFIG_YAFFS_YAFFS2
1831 {&yaffs2_fs_type, 0},
1836 static int __init init_yaffs_fs(void)
1839 struct file_system_to_install *fsinst;
1841 T(YAFFS_TRACE_ALWAYS,
1842 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
1844 /* Install the proc_fs entry */
1845 my_proc_entry = create_proc_entry("yaffs",
1849 if (my_proc_entry) {
1850 my_proc_entry->write_proc = yaffs_proc_write;
1851 my_proc_entry->read_proc = yaffs_proc_read;
1852 my_proc_entry->data = NULL;
1857 /* Now add the file system entries */
1859 fsinst = fs_to_install;
1861 while (fsinst->fst && !error) {
1862 error = register_filesystem(fsinst->fst);
1864 fsinst->installed = 1;
1869 /* Any errors? uninstall */
1871 fsinst = fs_to_install;
1873 while (fsinst->fst) {
1874 if (fsinst->installed) {
1875 unregister_filesystem(fsinst->fst);
1876 fsinst->installed = 0;
1885 static void __exit exit_yaffs_fs(void)
1888 struct file_system_to_install *fsinst;
1890 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
1893 remove_proc_entry("yaffs", &proc_root);
1895 fsinst = fs_to_install;
1897 while (fsinst->fst) {
1898 if (fsinst->installed) {
1899 unregister_filesystem(fsinst->fst);
1900 fsinst->installed = 0;
1907 module_init(init_yaffs_fs)
1908 module_exit(exit_yaffs_fs)
1910 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
1911 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002,2003,2004");
1912 MODULE_LICENSE("GPL");