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.46 2006-05-08 10:13:34 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 |
77 YAFFS_TRACE_BAD_BLOCKS |
78 YAFFS_TRACE_CHECKPOINT
81 #include <linux/mtd/mtd.h>
82 #include "yaffs_mtdif.h"
83 #include "yaffs_mtdif2.h"
85 /*#define T(x) printk x */
87 #define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
88 #define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
90 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
91 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
93 #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
96 static void yaffs_put_super(struct super_block *sb);
98 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
101 static int yaffs_file_flush(struct file *file);
103 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
106 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
108 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
109 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
110 struct nameidata *n);
111 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
112 struct nameidata *n);
114 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
115 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
117 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
118 struct dentry *dentry);
119 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
120 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
121 const char *symname);
122 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
124 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
125 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
128 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
131 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
132 struct inode *new_dir, struct dentry *new_dentry);
133 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
135 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
136 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
138 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
140 static void yaffs_read_inode(struct inode *inode);
142 static void yaffs_put_inode(struct inode *inode);
143 static void yaffs_delete_inode(struct inode *);
144 static void yaffs_clear_inode(struct inode *);
146 static int yaffs_readpage(struct file *file, struct page *page);
147 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
148 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
150 static int yaffs_writepage(struct page *page);
152 static int yaffs_prepare_write(struct file *f, struct page *pg,
153 unsigned offset, unsigned to);
154 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
157 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
159 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
160 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
162 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
165 static struct address_space_operations yaffs_file_address_operations = {
166 .readpage = yaffs_readpage,
167 .writepage = yaffs_writepage,
168 .prepare_write = yaffs_prepare_write,
169 .commit_write = yaffs_commit_write,
172 static struct file_operations yaffs_file_operations = {
173 .read = generic_file_read,
174 .write = generic_file_write,
175 .mmap = generic_file_mmap,
176 .flush = yaffs_file_flush,
177 .fsync = yaffs_sync_object,
178 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
179 .sendfile = generic_file_sendfile,
184 static struct inode_operations yaffs_file_inode_operations = {
185 .setattr = yaffs_setattr,
188 static struct inode_operations yaffs_symlink_inode_operations = {
189 .readlink = yaffs_readlink,
190 .follow_link = yaffs_follow_link,
191 .setattr = yaffs_setattr,
194 static struct inode_operations yaffs_dir_inode_operations = {
195 .create = yaffs_create,
196 .lookup = yaffs_lookup,
198 .unlink = yaffs_unlink,
199 .symlink = yaffs_symlink,
200 .mkdir = yaffs_mkdir,
201 .rmdir = yaffs_unlink,
202 .mknod = yaffs_mknod,
203 .rename = yaffs_rename,
204 .setattr = yaffs_setattr,
207 static struct file_operations yaffs_dir_operations = {
208 .read = generic_read_dir,
209 .readdir = yaffs_readdir,
210 .fsync = yaffs_sync_object,
213 static struct super_operations yaffs_super_ops = {
214 .statfs = yaffs_statfs,
215 .read_inode = yaffs_read_inode,
216 .put_inode = yaffs_put_inode,
217 .put_super = yaffs_put_super,
218 .delete_inode = yaffs_delete_inode,
219 .clear_inode = yaffs_clear_inode,
222 static void yaffs_GrossLock(yaffs_Device * dev)
224 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
226 down(&dev->grossLock);
229 static void yaffs_GrossUnlock(yaffs_Device * dev)
231 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
236 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
239 unsigned char *alias;
242 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
244 yaffs_GrossLock(dev);
246 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
248 yaffs_GrossUnlock(dev);
253 ret = vfs_readlink(dentry, buffer, buflen, alias);
258 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
259 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
261 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
264 unsigned char *alias;
266 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
268 yaffs_GrossLock(dev);
270 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
272 yaffs_GrossUnlock(dev);
280 ret = vfs_follow_link(nd, alias);
283 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
284 return ERR_PTR (ret);
290 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
294 * Lookup is used to find objects in the fs
296 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
298 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
301 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
305 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
307 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
309 yaffs_GrossLock(dev);
312 (KERN_DEBUG "yaffs_lookup for %d:%s\n",
313 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
316 yaffs_FindObjectByName(yaffs_InodeToObject(dir),
317 dentry->d_name.name);
319 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
321 /* Can't hold gross lock when calling yaffs_get_inode() */
322 yaffs_GrossUnlock(dev);
326 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
328 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
332 (KERN_DEBUG "yaffs_loookup dentry \n"));
333 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
334 * d_add even if NULL inode */
336 /*dget(dentry); // try to solve directory bug */
337 d_add(dentry, inode);
345 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
349 /* added NCB for 2.5/6 compatability - forces add even if inode is
350 * NULL which creates dentry hash */
351 d_add(dentry, inode);
354 /* return (ERR_PTR(-EIO)); */
358 /* For now put inode is just for debugging
359 * Put inode is called when the inode **structure** is put.
361 static void yaffs_put_inode(struct inode *inode)
364 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
365 atomic_read(&inode->i_count)));
369 /* clear is called to tell the fs to release any per-inode data it holds */
370 static void yaffs_clear_inode(struct inode *inode)
375 obj = yaffs_InodeToObject(inode);
378 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
379 atomic_read(&inode->i_count),
380 obj ? "object exists" : "null object"));
384 yaffs_GrossLock(dev);
386 /* Clear the association between the inode and
390 inode->u.generic_ip = NULL;
392 /* If the object freeing was deferred, then the real
394 * This should fix the inode inconsistency problem.
397 yaffs_HandleDeferedFree(obj);
399 yaffs_GrossUnlock(dev);
404 /* delete is called when the link count is zero and the inode
405 * is put (ie. nobody wants to know about it anymore, time to
407 * NB Must call clear_inode()
409 static void yaffs_delete_inode(struct inode *inode)
411 yaffs_Object *obj = yaffs_InodeToObject(inode);
415 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
416 atomic_read(&inode->i_count),
417 obj ? "object exists" : "null object"));
421 yaffs_GrossLock(dev);
422 yaffs_DeleteFile(obj);
423 yaffs_GrossUnlock(dev);
425 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
426 truncate_inode_pages (&inode->i_data, 0);
431 static int yaffs_file_flush(struct file *file)
433 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
435 yaffs_Device *dev = obj->myDev;
438 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
439 obj->dirty ? "dirty" : "clean"));
441 yaffs_GrossLock(dev);
443 yaffs_FlushFile(obj, 1);
445 yaffs_GrossUnlock(dev);
450 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
452 /* Lifted from jffs2 */
455 unsigned char *pg_buf;
460 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
461 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
462 (unsigned)PAGE_CACHE_SIZE));
464 obj = yaffs_DentryToObject(f->f_dentry);
468 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
469 BUG_ON(!PageLocked(pg));
476 /* FIXME: Can kmap fail? */
478 yaffs_GrossLock(dev);
481 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
484 yaffs_GrossUnlock(dev);
490 ClearPageUptodate(pg);
497 flush_dcache_page(pg);
500 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
504 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
506 int ret = yaffs_readpage_nolock(f, pg);
511 static int yaffs_readpage(struct file *f, struct page *pg)
513 return yaffs_readpage_unlock(f, pg);
516 /* writepage inspired by/stolen from smbfs */
518 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
519 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
521 static int yaffs_writepage(struct page *page)
524 struct address_space *mapping = page->mapping;
525 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
527 unsigned long end_index;
535 inode = mapping->host;
539 if (offset > inode->i_size) {
542 "yaffs_writepage at %08x, inode size = %08x!!!\n",
543 (unsigned)(page->index << PAGE_CACHE_SHIFT),
544 (unsigned)inode->i_size));
546 (KERN_DEBUG " -> don't care!!\n"));
551 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
554 if (page->index < end_index) {
555 nBytes = PAGE_CACHE_SIZE;
557 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
564 obj = yaffs_InodeToObject(inode);
565 yaffs_GrossLock(obj->myDev);
568 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
569 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
571 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
572 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
575 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
579 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
580 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
582 yaffs_GrossUnlock(obj->myDev);
585 SetPageUptodate(page);
589 return (nWritten == nBytes) ? 0 : -ENOSPC;
592 static int yaffs_prepare_write(struct file *f, struct page *pg,
593 unsigned offset, unsigned to)
596 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
597 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
598 return yaffs_readpage_nolock(f, pg);
604 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
608 void *addr = page_address(pg) + offset;
609 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
610 int nBytes = to - offset;
614 unsigned saddr = (unsigned)addr;
617 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
620 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
622 if (nWritten != nBytes) {
625 "yaffs_commit_write not same size nWritten %d nBytes %d\n",
628 ClearPageUptodate(pg);
634 (KERN_DEBUG "yaffs_commit_write returning %d\n",
635 nWritten == nBytes ? 0 : nWritten));
637 return nWritten == nBytes ? 0 : nWritten;
641 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
644 inode->i_ino = obj->objectId;
645 inode->i_mode = obj->yst_mode;
646 inode->i_uid = obj->yst_uid;
647 inode->i_gid = obj->yst_gid;
648 inode->i_blksize = inode->i_sb->s_blocksize;
649 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
651 inode->i_rdev = old_decode_dev(obj->yst_rdev);
652 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
653 inode->i_atime.tv_nsec = 0;
654 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
655 inode->i_mtime.tv_nsec = 0;
656 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
657 inode->i_ctime.tv_nsec = 0;
659 inode->i_rdev = obj->yst_rdev;
660 inode->i_atime = obj->yst_atime;
661 inode->i_mtime = obj->yst_mtime;
662 inode->i_ctime = obj->yst_ctime;
664 inode->i_size = yaffs_GetObjectFileLength(obj);
665 inode->i_blocks = (inode->i_size + 511) >> 9;
667 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
671 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
672 inode->i_mode, inode->i_uid, inode->i_gid,
673 (int)inode->i_size, atomic_read(&inode->i_count)));
675 switch (obj->yst_mode & S_IFMT) {
676 default: /* fifo, device or socket */
677 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
678 init_special_inode(inode, obj->yst_mode,
679 old_decode_dev(obj->yst_rdev));
681 init_special_inode(inode, obj->yst_mode,
682 (dev_t) (obj->yst_rdev));
685 case S_IFREG: /* file */
686 inode->i_op = &yaffs_file_inode_operations;
687 inode->i_fop = &yaffs_file_operations;
688 inode->i_mapping->a_ops =
689 &yaffs_file_address_operations;
691 case S_IFDIR: /* directory */
692 inode->i_op = &yaffs_dir_inode_operations;
693 inode->i_fop = &yaffs_dir_operations;
695 case S_IFLNK: /* symlink */
696 inode->i_op = &yaffs_symlink_inode_operations;
700 inode->u.generic_ip = obj;
701 obj->myInode = inode;
705 (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
710 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
717 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
724 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
730 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
732 inode = iget(sb, obj->objectId);
734 /* NB Side effect: iget calls back to yaffs_read_inode(). */
735 /* iget also increments the inode's i_count */
736 /* NB You can't be holding grossLock or deadlock will happen! */
741 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
749 obj = yaffs_DentryToObject(f->f_dentry);
753 yaffs_GrossLock(dev);
755 inode = f->f_dentry->d_inode;
757 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
758 ipos = inode->i_size;
765 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
769 "yaffs_file_write about to write writing %d bytes"
770 "to object %d at %d\n",
771 n, obj->objectId, ipos));
774 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
777 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
782 if (ipos > inode->i_size) {
783 inode->i_size = ipos;
784 inode->i_blocks = (ipos + 511) >> 9;
788 "yaffs_file_write size updated to %d bytes, "
790 ipos, (int)(inode->i_blocks)));
794 yaffs_GrossUnlock(dev);
795 return nWritten == 0 ? -ENOSPC : nWritten;
798 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
802 struct inode *inode = f->f_dentry->d_inode;
803 unsigned long offset, curoffs;
807 char name[YAFFS_MAX_NAME_LENGTH + 1];
809 obj = yaffs_DentryToObject(f->f_dentry);
812 yaffs_GrossLock(dev);
816 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
820 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
822 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
831 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
832 (int)f->f_dentry->d_parent->d_inode->i_ino));
834 (dirent, "..", 2, offset,
835 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
844 /* If the directory has changed since the open or last call to
845 readdir, rewind to after the 2 canned entries. */
847 if (f->f_version != inode->i_version) {
850 f->f_version = inode->i_version;
853 list_for_each(i, &obj->variant.directoryVariant.children) {
855 if (curoffs >= offset) {
856 l = list_entry(i, yaffs_Object, siblings);
858 yaffs_GetObjectName(l, name,
859 YAFFS_MAX_NAME_LENGTH + 1);
861 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
862 yaffs_GetObjectInode(l)));
868 yaffs_GetObjectInode(l),
869 yaffs_GetObjectType(l))
882 yaffs_GrossUnlock(dev);
888 * File creation. Allocate an inode, and we're done..
890 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
891 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
894 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
900 yaffs_Object *obj = NULL;
903 yaffs_Object *parent = yaffs_InodeToObject(dir);
906 uid_t uid = current->fsuid;
907 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
911 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
912 parent->objectId, parent->variantType));
915 (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
919 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
921 dentry->d_name.name, mode, rdev));
925 yaffs_GrossLock(dev);
927 switch (mode & S_IFMT) {
929 /* Special (socket, fifo, device...) */
930 T(YAFFS_TRACE_OS, (KERN_DEBUG
931 "yaffs_mknod: making special\n"));
932 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
934 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
935 gid, old_encode_dev(rdev));
938 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
942 case S_IFREG: /* file */
943 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
945 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
948 case S_IFDIR: /* directory */
950 (KERN_DEBUG "yaffs_mknod: making directory\n"));
952 yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
955 case S_IFLNK: /* symlink */
956 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
957 obj = NULL; /* Do we ever get here? */
961 /* Can not call yaffs_get_inode() with gross lock held */
962 yaffs_GrossUnlock(dev);
965 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
966 d_instantiate(dentry, inode);
968 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
969 obj->objectId, atomic_read(&inode->i_count)));
973 (KERN_DEBUG "yaffs_mknod failed making object\n"));
980 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
983 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
984 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
986 /* attempt to fix dir bug - didn't work */
994 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
995 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
998 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1001 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1002 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1005 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1012 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1013 dentry->d_name.name));
1015 dev = yaffs_InodeToObject(dir)->myDev;
1017 yaffs_GrossLock(dev);
1019 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1021 if (retVal == YAFFS_OK) {
1022 dentry->d_inode->i_nlink--;
1024 yaffs_GrossUnlock(dev);
1025 mark_inode_dirty(dentry->d_inode);
1028 yaffs_GrossUnlock(dev);
1035 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1036 struct dentry *dentry)
1038 struct inode *inode = old_dentry->d_inode;
1039 yaffs_Object *obj = NULL;
1040 yaffs_Object *link = NULL;
1043 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1045 obj = yaffs_InodeToObject(inode);
1048 yaffs_GrossLock(dev);
1050 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1053 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1058 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1059 d_instantiate(dentry, old_dentry->d_inode);
1060 atomic_inc(&old_dentry->d_inode->i_count);
1062 (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1063 old_dentry->d_inode->i_nlink,
1064 atomic_read(&old_dentry->d_inode->i_count)));
1068 yaffs_GrossUnlock(dev);
1078 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1079 const char *symname)
1083 uid_t uid = current->fsuid;
1084 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1086 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1088 dev = yaffs_InodeToObject(dir)->myDev;
1089 yaffs_GrossLock(dev);
1090 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1091 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1092 yaffs_GrossUnlock(dev);
1096 struct inode *inode;
1098 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1099 d_instantiate(dentry, inode);
1100 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1103 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1110 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1117 obj = yaffs_DentryToObject(dentry);
1121 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1122 yaffs_GrossLock(dev);
1123 yaffs_FlushFile(obj, 1);
1124 yaffs_GrossUnlock(dev);
1129 * The VFS layer already does all the dentry stuff for rename.
1131 * NB: POSIX says you can rename an object over an old object of the same name
1133 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1134 struct inode *new_dir, struct dentry *new_dentry)
1137 int retVal = YAFFS_FAIL;
1138 yaffs_Object *target;
1140 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1141 dev = yaffs_InodeToObject(old_dir)->myDev;
1143 yaffs_GrossLock(dev);
1145 /* Check if the target is an existing directory that is not empty. */
1147 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1148 new_dentry->d_name.name);
1153 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1154 !list_empty(&target->variant.directoryVariant.children)) {
1156 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1158 retVal = YAFFS_FAIL;
1161 /* Now does unlinking internally using shadowing mechanism */
1162 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1165 yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1166 old_dentry->d_name.name,
1167 yaffs_InodeToObject(new_dir),
1168 new_dentry->d_name.name);
1171 yaffs_GrossUnlock(dev);
1173 if (retVal == YAFFS_OK) {
1175 new_dentry->d_inode->i_nlink--;
1176 mark_inode_dirty(new_dentry->d_inode);
1186 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1188 struct inode *inode = dentry->d_inode;
1193 (KERN_DEBUG "yaffs_setattr of object %d\n",
1194 yaffs_InodeToObject(inode)->objectId));
1196 if ((error = inode_change_ok(inode, attr)) == 0) {
1198 dev = yaffs_InodeToObject(inode)->myDev;
1199 yaffs_GrossLock(dev);
1200 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1206 yaffs_GrossUnlock(dev);
1208 error = inode_setattr(inode, attr);
1213 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1214 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1216 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1220 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1221 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1223 yaffs_GrossLock(dev);
1225 buf->f_type = YAFFS_MAGIC;
1226 buf->f_bsize = sb->s_blocksize;
1227 buf->f_namelen = 255;
1228 if (sb->s_blocksize > dev->nBytesPerChunk) {
1231 (dev->endBlock - dev->startBlock +
1232 1) * dev->nChunksPerBlock / (sb->s_blocksize /
1233 dev->nBytesPerChunk);
1235 yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
1236 dev->nBytesPerChunk);
1240 (dev->endBlock - dev->startBlock +
1241 1) * dev->nChunksPerBlock * (dev->nBytesPerChunk /
1244 yaffs_GetNumberOfFreeChunks(dev) * (dev->nBytesPerChunk /
1249 buf->f_bavail = buf->f_bfree;
1251 yaffs_GrossUnlock(dev);
1255 static void yaffs_read_inode(struct inode *inode)
1257 /* NB This is called as a side effect of other functions, but
1258 * we had to release the lock to prevent deadlocks, so
1259 * need to lock again.
1263 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1266 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1268 yaffs_GrossLock(dev);
1270 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1272 yaffs_FillInodeFromObject(inode, obj);
1274 yaffs_GrossUnlock(dev);
1277 static LIST_HEAD(yaffs_dev_list);
1279 static void yaffs_put_super(struct super_block *sb)
1281 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1283 yaffs_GrossLock(dev);
1285 yaffs_FlushEntireDeviceCache(dev);
1287 if (dev->putSuperFunc) {
1288 dev->putSuperFunc(sb);
1291 yaffs_CheckpointSave(dev);
1292 yaffs_Deinitialise(dev);
1294 yaffs_GrossUnlock(dev);
1296 /* we assume this is protected by lock_kernel() in mount/umount */
1297 list_del(&dev->devList);
1299 if(dev->spareBuffer){
1300 YFREE(dev->spareBuffer);
1301 dev->spareBuffer = NULL;
1308 static void yaffs_MTDPutSuper(struct super_block *sb)
1311 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1317 put_mtd_device(mtd);
1321 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1322 struct super_block *sb,
1323 void *data, int silent)
1326 struct inode *inode = NULL;
1327 struct dentry *root;
1328 yaffs_Device *dev = 0;
1329 char devname_buf[BDEVNAME_SIZE + 1];
1330 struct mtd_info *mtd;
1333 sb->s_magic = YAFFS_MAGIC;
1334 sb->s_op = &yaffs_super_ops;
1337 printk(KERN_INFO "yaffs: sb is NULL\n");
1338 else if (!sb->s_dev)
1339 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1340 else if (!yaffs_devname(sb, devname_buf))
1341 printk(KERN_INFO "yaffs: devname is NULL\n");
1343 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1345 yaffs_devname(sb, devname_buf));
1347 sb->s_blocksize = PAGE_CACHE_SIZE;
1348 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1349 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1351 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1353 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1355 ("yaffs: Write verification disabled. All guarantees "
1356 "null and void\n"));
1359 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1361 MAJOR(sb->s_dev), MINOR(sb->s_dev),
1362 yaffs_devname(sb, devname_buf)));
1364 /* Check it's an mtd device..... */
1365 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1366 return NULL; /* This isn't an mtd device */
1368 /* Get the device */
1369 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1371 T(YAFFS_TRACE_ALWAYS,
1372 ("yaffs: MTD device #%u doesn't appear to exist\n",
1376 /* Check it's NAND */
1377 if (mtd->type != MTD_NANDFLASH) {
1378 T(YAFFS_TRACE_ALWAYS,
1379 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1383 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1384 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1385 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1386 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1387 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1388 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1389 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1390 T(YAFFS_TRACE_OS, (" oobblock %d\n", mtd->oobblock));
1391 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1392 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1393 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1395 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1397 if (yaffsVersion == 1 &&
1398 mtd->oobblock >= 2048) {
1399 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1404 if (yaffsVersion == 2) {
1405 /* Check for version 2 style functions */
1407 !mtd->block_isbad ||
1408 !mtd->block_markbad ||
1412 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1413 T(YAFFS_TRACE_ALWAYS,
1414 ("yaffs: MTD device does not support required "
1419 if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1420 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
1421 T(YAFFS_TRACE_ALWAYS,
1422 ("yaffs: MTD device does not have the "
1423 "right page sizes\n"));
1427 /* Check for V1 style functions */
1432 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1433 T(YAFFS_TRACE_ALWAYS,
1434 ("yaffs: MTD device does not support required "
1439 if (mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
1440 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1441 T(YAFFS_TRACE_ALWAYS,
1442 ("yaffs: MTD device does not support have the "
1443 "right page sizes\n"));
1448 /* OK, so if we got here, we have an MTD that's NAND and looks
1449 * like it has the right capabilities
1450 * Set the yaffs_Device up for mtd
1453 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1454 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1456 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1459 /* Deep shit could not allocate device structure */
1460 T(YAFFS_TRACE_ALWAYS,
1461 ("yaffs_read_super: Failed trying to allocate "
1462 "yaffs_Device. \n"));
1466 memset(dev, 0, sizeof(yaffs_Device));
1467 dev->genericDevice = mtd;
1468 dev->name = mtd->name;
1470 /* Set up the memory size parameters.... */
1472 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1473 dev->startBlock = 0;
1474 dev->endBlock = nBlocks - 1;
1475 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1476 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1477 dev->nReservedBlocks = 5;
1478 dev->nShortOpCaches = 10; /* Enable short op caching */
1480 /* ... and the functions. */
1481 if (yaffsVersion == 2) {
1482 dev->writeChunkWithTagsToNAND =
1483 nandmtd2_WriteChunkWithTagsToNAND;
1484 dev->readChunkWithTagsFromNAND =
1485 nandmtd2_ReadChunkWithTagsFromNAND;
1486 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1487 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1488 dev->spareBuffer = YMALLOC(mtd->oobsize);
1490 dev->nBytesPerChunk = mtd->oobblock;
1491 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1492 nBlocks = mtd->size / mtd->erasesize;
1493 dev->checkpointStartBlock = 0;
1494 dev->checkpointEndBlock = 20;
1495 dev->startBlock = dev->checkpointEndBlock + 1;
1496 dev->endBlock = nBlocks - 1;
1498 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1499 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1502 /* ... and common functions */
1503 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1504 dev->initialiseNAND = nandmtd_InitialiseNAND;
1506 dev->putSuperFunc = yaffs_MTDPutSuper;
1508 #ifndef CONFIG_YAFFS_DOES_ECC
1509 dev->useNANDECC = 1;
1512 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1513 dev->wideTnodesDisabled = 1;
1516 /* we assume this is protected by lock_kernel() in mount/umount */
1517 list_add_tail(&dev->devList, &yaffs_dev_list);
1519 init_MUTEX(&dev->grossLock);
1521 yaffs_GrossLock(dev);
1523 err = yaffs_GutsInitialise(dev);
1526 ("yaffs_read_super: guts initialised %s\n",
1527 (err == YAFFS_OK) ? "OK" : "FAILED"));
1529 /* Release lock before yaffs_get_inode() */
1530 yaffs_GrossUnlock(dev);
1532 /* Create root inode */
1533 if (err == YAFFS_OK)
1534 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1540 inode->i_op = &yaffs_dir_inode_operations;
1541 inode->i_fop = &yaffs_dir_operations;
1543 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1545 root = d_alloc_root(inode);
1547 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1555 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1560 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1561 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
1564 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
1567 static struct super_block *yaffs_read_super(struct file_system_type *fs,
1568 int flags, const char *dev_name,
1572 return get_sb_bdev(fs, flags, dev_name, data,
1573 yaffs_internal_read_super_mtd);
1576 static struct file_system_type yaffs_fs_type = {
1577 .owner = THIS_MODULE,
1579 .get_sb = yaffs_read_super,
1580 .kill_sb = kill_block_super,
1581 .fs_flags = FS_REQUIRES_DEV,
1584 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
1587 return yaffs_internal_read_super(1, sb, data, silent);
1590 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
1595 #ifdef CONFIG_YAFFS_YAFFS2
1597 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1598 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
1601 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
1604 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
1605 int flags, const char *dev_name,
1609 return get_sb_bdev(fs, flags, dev_name, data,
1610 yaffs2_internal_read_super_mtd);
1613 static struct file_system_type yaffs2_fs_type = {
1614 .owner = THIS_MODULE,
1616 .get_sb = yaffs2_read_super,
1617 .kill_sb = kill_block_super,
1618 .fs_flags = FS_REQUIRES_DEV,
1621 static struct super_block *yaffs2_read_super(struct super_block *sb,
1622 void *data, int silent)
1624 return yaffs_internal_read_super(2, sb, data, silent);
1627 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
1631 #endif /* CONFIG_YAFFS_YAFFS2 */
1633 static struct proc_dir_entry *my_proc_entry;
1635 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
1637 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
1638 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
1639 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
1640 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
1641 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
1642 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
1643 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
1644 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
1645 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
1646 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
1647 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
1648 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
1649 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
1650 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
1652 sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
1654 sprintf(buf, "passiveGCs......... %d\n",
1655 dev->passiveGarbageCollections);
1656 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
1657 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
1658 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
1659 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
1660 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
1661 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
1662 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
1663 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
1664 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
1666 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
1667 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
1668 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
1673 static int yaffs_proc_read(char *page,
1675 off_t offset, int count, int *eof, void *data)
1677 struct list_head *item;
1682 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
1683 * We use 'offset' (*ppos) to indicate where we are in devList.
1684 * This also assumes the user has posted a read buffer large
1685 * enough to hold the complete output; but that's life in /proc.
1690 /* Print header first */
1692 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
1693 "\n%s\n%s\n", yaffs_fs_c_version,
1694 yaffs_guts_c_version);
1697 /* hold lock_kernel while traversing yaffs_dev_list */
1700 /* Locate and print the Nth entry. Order N-squared but N is small. */
1701 list_for_each(item, &yaffs_dev_list) {
1702 yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
1707 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
1708 buf = yaffs_dump_dev(buf, dev);
1713 return buf - page < count ? buf - page : count;
1717 * Set the verbosity of the warnings and error messages.
1723 unsigned mask_bitfield;
1725 {"allocate", YAFFS_TRACE_ALLOCATE},
1726 {"always", YAFFS_TRACE_ALWAYS},
1727 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
1728 {"buffers", YAFFS_TRACE_BUFFERS},
1729 {"bug", YAFFS_TRACE_BUG},
1730 {"deletion", YAFFS_TRACE_DELETION},
1731 {"erase", YAFFS_TRACE_ERASE},
1732 {"error", YAFFS_TRACE_ERROR},
1733 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
1734 {"gc", YAFFS_TRACE_GC},
1735 {"mtd", YAFFS_TRACE_MTD},
1736 {"nandaccess", YAFFS_TRACE_NANDACCESS},
1737 {"os", YAFFS_TRACE_OS},
1738 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
1739 {"scan", YAFFS_TRACE_SCAN},
1740 {"tracing", YAFFS_TRACE_TRACING},
1741 {"write", YAFFS_TRACE_WRITE},
1742 {"all", 0xffffffff},
1747 static int yaffs_proc_write(struct file *file, const char *buf,
1748 unsigned long count, void *data)
1750 unsigned rg = 0, mask_bitfield;
1751 char *end, *mask_name;
1757 rg = yaffs_traceMask;
1759 while (!done && (pos < count)) {
1761 while ((pos < count) && isspace(buf[pos])) {
1778 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
1779 if (end > buf + pos) {
1780 mask_name = "numeral";
1781 len = end - (buf + pos);
1785 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
1786 len = strlen(mask_flags[i].mask_name);
1787 if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
1788 mask_name = mask_flags[i].mask_name;
1789 mask_bitfield = mask_flags[i].mask_bitfield;
1796 if (mask_name != NULL) {
1801 rg &= ~mask_bitfield;
1804 rg |= mask_bitfield;
1810 rg |= mask_bitfield;
1816 yaffs_traceMask = rg;
1817 if (rg & YAFFS_TRACE_ALWAYS) {
1818 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
1820 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
1821 printk("%c%s\n", flag, mask_flags[i].mask_name);
1828 /* Stuff to handle installation of file systems */
1829 struct file_system_to_install {
1830 struct file_system_type *fst;
1834 static struct file_system_to_install fs_to_install[] = {
1835 //#ifdef CONFIG_YAFFS_YAFFS1
1836 {&yaffs_fs_type, 0},
1838 //#ifdef CONFIG_YAFFS_YAFFS2
1839 {&yaffs2_fs_type, 0},
1844 static int __init init_yaffs_fs(void)
1847 struct file_system_to_install *fsinst;
1849 T(YAFFS_TRACE_ALWAYS,
1850 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
1852 /* Install the proc_fs entry */
1853 my_proc_entry = create_proc_entry("yaffs",
1857 if (my_proc_entry) {
1858 my_proc_entry->write_proc = yaffs_proc_write;
1859 my_proc_entry->read_proc = yaffs_proc_read;
1860 my_proc_entry->data = NULL;
1865 /* Now add the file system entries */
1867 fsinst = fs_to_install;
1869 while (fsinst->fst && !error) {
1870 error = register_filesystem(fsinst->fst);
1872 fsinst->installed = 1;
1877 /* Any errors? uninstall */
1879 fsinst = fs_to_install;
1881 while (fsinst->fst) {
1882 if (fsinst->installed) {
1883 unregister_filesystem(fsinst->fst);
1884 fsinst->installed = 0;
1893 static void __exit exit_yaffs_fs(void)
1896 struct file_system_to_install *fsinst;
1898 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
1901 remove_proc_entry("yaffs", &proc_root);
1903 fsinst = fs_to_install;
1905 while (fsinst->fst) {
1906 if (fsinst->installed) {
1907 unregister_filesystem(fsinst->fst);
1908 fsinst->installed = 0;
1915 module_init(init_yaffs_fs)
1916 module_exit(exit_yaffs_fs)
1918 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
1919 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
1920 MODULE_LICENSE("GPL");