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.50 2006-06-05 04:12:44 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)
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 static int yaffs_file_flush(struct file *file);
108 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
111 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
113 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
114 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
115 struct nameidata *n);
116 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
117 struct nameidata *n);
119 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
120 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
122 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
123 struct dentry *dentry);
124 static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
125 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
126 const char *symname);
127 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
129 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
130 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
133 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
136 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
137 struct inode *new_dir, struct dentry *new_dentry);
138 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
140 static int yaffs_sync_fs(struct super_block *sb);
141 static int yaffs_write_super(struct super_block *sb);
143 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
144 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
146 static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
148 static void yaffs_read_inode(struct inode *inode);
150 static void yaffs_put_inode(struct inode *inode);
151 static void yaffs_delete_inode(struct inode *);
152 static void yaffs_clear_inode(struct inode *);
154 static int yaffs_readpage(struct file *file, struct page *page);
155 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
156 static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
158 static int yaffs_writepage(struct page *page);
160 static int yaffs_prepare_write(struct file *f, struct page *pg,
161 unsigned offset, unsigned to);
162 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
165 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
167 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
168 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
170 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
173 static struct address_space_operations yaffs_file_address_operations = {
174 .readpage = yaffs_readpage,
175 .writepage = yaffs_writepage,
176 .prepare_write = yaffs_prepare_write,
177 .commit_write = yaffs_commit_write,
180 static struct file_operations yaffs_file_operations = {
181 .read = generic_file_read,
182 .write = generic_file_write,
183 .mmap = generic_file_mmap,
184 .flush = yaffs_file_flush,
185 .fsync = yaffs_sync_object,
186 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
187 .sendfile = generic_file_sendfile,
192 static struct inode_operations yaffs_file_inode_operations = {
193 .setattr = yaffs_setattr,
196 static struct inode_operations yaffs_symlink_inode_operations = {
197 .readlink = yaffs_readlink,
198 .follow_link = yaffs_follow_link,
199 .setattr = yaffs_setattr,
202 static struct inode_operations yaffs_dir_inode_operations = {
203 .create = yaffs_create,
204 .lookup = yaffs_lookup,
206 .unlink = yaffs_unlink,
207 .symlink = yaffs_symlink,
208 .mkdir = yaffs_mkdir,
209 .rmdir = yaffs_unlink,
210 .mknod = yaffs_mknod,
211 .rename = yaffs_rename,
212 .setattr = yaffs_setattr,
215 static struct file_operations yaffs_dir_operations = {
216 .read = generic_read_dir,
217 .readdir = yaffs_readdir,
218 .fsync = yaffs_sync_object,
221 static struct super_operations yaffs_super_ops = {
222 .statfs = yaffs_statfs,
223 .read_inode = yaffs_read_inode,
224 .put_inode = yaffs_put_inode,
225 .put_super = yaffs_put_super,
226 .delete_inode = yaffs_delete_inode,
227 .clear_inode = yaffs_clear_inode,
228 .sync_fs = yaffs_sync_fs,
229 .write_super = yaffs_write_super,
232 static void yaffs_GrossLock(yaffs_Device * dev)
234 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
236 down(&dev->grossLock);
239 static void yaffs_GrossUnlock(yaffs_Device * dev)
241 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
246 static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
249 unsigned char *alias;
252 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
254 yaffs_GrossLock(dev);
256 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
258 yaffs_GrossUnlock(dev);
263 ret = vfs_readlink(dentry, buffer, buflen, alias);
268 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
269 static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
271 static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
274 unsigned char *alias;
276 yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
278 yaffs_GrossLock(dev);
280 alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
282 yaffs_GrossUnlock(dev);
290 ret = vfs_follow_link(nd, alias);
293 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
294 return ERR_PTR (ret);
300 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
304 * Lookup is used to find objects in the fs
306 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
308 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
311 static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
315 struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
317 yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
319 yaffs_GrossLock(dev);
322 (KERN_DEBUG "yaffs_lookup for %d:%s\n",
323 yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
326 yaffs_FindObjectByName(yaffs_InodeToObject(dir),
327 dentry->d_name.name);
329 obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
331 /* Can't hold gross lock when calling yaffs_get_inode() */
332 yaffs_GrossUnlock(dev);
336 (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
338 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
342 (KERN_DEBUG "yaffs_loookup dentry \n"));
343 /* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
344 * d_add even if NULL inode */
346 /*dget(dentry); // try to solve directory bug */
347 d_add(dentry, inode);
355 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
359 /* added NCB for 2.5/6 compatability - forces add even if inode is
360 * NULL which creates dentry hash */
361 d_add(dentry, inode);
364 /* return (ERR_PTR(-EIO)); */
368 /* For now put inode is just for debugging
369 * Put inode is called when the inode **structure** is put.
371 static void yaffs_put_inode(struct inode *inode)
374 ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
375 atomic_read(&inode->i_count)));
379 /* clear is called to tell the fs to release any per-inode data it holds */
380 static void yaffs_clear_inode(struct inode *inode)
385 obj = yaffs_InodeToObject(inode);
388 ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
389 atomic_read(&inode->i_count),
390 obj ? "object exists" : "null object"));
394 yaffs_GrossLock(dev);
396 /* Clear the association between the inode and
400 inode->u.generic_ip = NULL;
402 /* If the object freeing was deferred, then the real
404 * This should fix the inode inconsistency problem.
407 yaffs_HandleDeferedFree(obj);
409 yaffs_GrossUnlock(dev);
414 /* delete is called when the link count is zero and the inode
415 * is put (ie. nobody wants to know about it anymore, time to
417 * NB Must call clear_inode()
419 static void yaffs_delete_inode(struct inode *inode)
421 yaffs_Object *obj = yaffs_InodeToObject(inode);
425 ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
426 atomic_read(&inode->i_count),
427 obj ? "object exists" : "null object"));
431 yaffs_GrossLock(dev);
432 yaffs_DeleteFile(obj);
433 yaffs_GrossUnlock(dev);
435 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
436 truncate_inode_pages (&inode->i_data, 0);
441 static int yaffs_file_flush(struct file *file)
443 yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
445 yaffs_Device *dev = obj->myDev;
448 (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
449 obj->dirty ? "dirty" : "clean"));
451 yaffs_GrossLock(dev);
453 yaffs_FlushFile(obj, 1);
455 yaffs_GrossUnlock(dev);
460 static int yaffs_readpage_nolock(struct file *f, struct page *pg)
462 /* Lifted from jffs2 */
465 unsigned char *pg_buf;
470 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
471 (unsigned)(pg->index << PAGE_CACHE_SHIFT),
472 (unsigned)PAGE_CACHE_SIZE));
474 obj = yaffs_DentryToObject(f->f_dentry);
478 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
479 BUG_ON(!PageLocked(pg));
486 /* FIXME: Can kmap fail? */
488 yaffs_GrossLock(dev);
491 yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
494 yaffs_GrossUnlock(dev);
500 ClearPageUptodate(pg);
507 flush_dcache_page(pg);
510 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
514 static int yaffs_readpage_unlock(struct file *f, struct page *pg)
516 int ret = yaffs_readpage_nolock(f, pg);
521 static int yaffs_readpage(struct file *f, struct page *pg)
523 return yaffs_readpage_unlock(f, pg);
526 /* writepage inspired by/stolen from smbfs */
528 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
529 static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
531 static int yaffs_writepage(struct page *page)
534 struct address_space *mapping = page->mapping;
535 loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
537 unsigned long end_index;
545 inode = mapping->host;
549 if (offset > inode->i_size) {
552 "yaffs_writepage at %08x, inode size = %08x!!!\n",
553 (unsigned)(page->index << PAGE_CACHE_SHIFT),
554 (unsigned)inode->i_size));
556 (KERN_DEBUG " -> don't care!!\n"));
561 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
564 if (page->index < end_index) {
565 nBytes = PAGE_CACHE_SIZE;
567 nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
574 obj = yaffs_InodeToObject(inode);
575 yaffs_GrossLock(obj->myDev);
578 (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
579 (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
581 (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
582 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
585 yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
589 (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
590 (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
592 yaffs_GrossUnlock(obj->myDev);
595 SetPageUptodate(page);
599 return (nWritten == nBytes) ? 0 : -ENOSPC;
602 static int yaffs_prepare_write(struct file *f, struct page *pg,
603 unsigned offset, unsigned to)
606 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
607 if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
608 return yaffs_readpage_nolock(f, pg);
614 static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
618 void *addr = page_address(pg) + offset;
619 loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
620 int nBytes = to - offset;
624 unsigned saddr = (unsigned)addr;
627 (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
630 nWritten = yaffs_file_write(f, addr, nBytes, &pos);
632 if (nWritten != nBytes) {
635 "yaffs_commit_write not same size nWritten %d nBytes %d\n",
638 ClearPageUptodate(pg);
644 (KERN_DEBUG "yaffs_commit_write returning %d\n",
645 nWritten == nBytes ? 0 : nWritten));
647 return nWritten == nBytes ? 0 : nWritten;
651 static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
654 inode->i_ino = obj->objectId;
655 inode->i_mode = obj->yst_mode;
656 inode->i_uid = obj->yst_uid;
657 inode->i_gid = obj->yst_gid;
658 inode->i_blksize = inode->i_sb->s_blocksize;
659 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
661 inode->i_rdev = old_decode_dev(obj->yst_rdev);
662 inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
663 inode->i_atime.tv_nsec = 0;
664 inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
665 inode->i_mtime.tv_nsec = 0;
666 inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
667 inode->i_ctime.tv_nsec = 0;
669 inode->i_rdev = obj->yst_rdev;
670 inode->i_atime = obj->yst_atime;
671 inode->i_mtime = obj->yst_mtime;
672 inode->i_ctime = obj->yst_ctime;
674 inode->i_size = yaffs_GetObjectFileLength(obj);
675 inode->i_blocks = (inode->i_size + 511) >> 9;
677 inode->i_nlink = yaffs_GetObjectLinkCount(obj);
681 "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
682 inode->i_mode, inode->i_uid, inode->i_gid,
683 (int)inode->i_size, atomic_read(&inode->i_count)));
685 switch (obj->yst_mode & S_IFMT) {
686 default: /* fifo, device or socket */
687 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
688 init_special_inode(inode, obj->yst_mode,
689 old_decode_dev(obj->yst_rdev));
691 init_special_inode(inode, obj->yst_mode,
692 (dev_t) (obj->yst_rdev));
695 case S_IFREG: /* file */
696 inode->i_op = &yaffs_file_inode_operations;
697 inode->i_fop = &yaffs_file_operations;
698 inode->i_mapping->a_ops =
699 &yaffs_file_address_operations;
701 case S_IFDIR: /* directory */
702 inode->i_op = &yaffs_dir_inode_operations;
703 inode->i_fop = &yaffs_dir_operations;
705 case S_IFLNK: /* symlink */
706 inode->i_op = &yaffs_symlink_inode_operations;
710 inode->u.generic_ip = obj;
711 obj->myInode = inode;
715 (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
720 struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
727 (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
734 (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
740 (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
742 inode = iget(sb, obj->objectId);
744 /* NB Side effect: iget calls back to yaffs_read_inode(). */
745 /* iget also increments the inode's i_count */
746 /* NB You can't be holding grossLock or deadlock will happen! */
751 static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
759 obj = yaffs_DentryToObject(f->f_dentry);
763 yaffs_GrossLock(dev);
765 inode = f->f_dentry->d_inode;
767 if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
768 ipos = inode->i_size;
775 (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
779 "yaffs_file_write about to write writing %d bytes"
780 "to object %d at %d\n",
781 n, obj->objectId, ipos));
784 nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
787 (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
792 if (ipos > inode->i_size) {
793 inode->i_size = ipos;
794 inode->i_blocks = (ipos + 511) >> 9;
798 "yaffs_file_write size updated to %d bytes, "
800 ipos, (int)(inode->i_blocks)));
804 yaffs_GrossUnlock(dev);
805 return nWritten == 0 ? -ENOSPC : nWritten;
808 static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
812 struct inode *inode = f->f_dentry->d_inode;
813 unsigned long offset, curoffs;
817 char name[YAFFS_MAX_NAME_LENGTH + 1];
819 obj = yaffs_DentryToObject(f->f_dentry);
822 yaffs_GrossLock(dev);
826 T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
830 (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
832 if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
841 (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
842 (int)f->f_dentry->d_parent->d_inode->i_ino));
844 (dirent, "..", 2, offset,
845 f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
854 /* If the directory has changed since the open or last call to
855 readdir, rewind to after the 2 canned entries. */
857 if (f->f_version != inode->i_version) {
860 f->f_version = inode->i_version;
863 list_for_each(i, &obj->variant.directoryVariant.children) {
865 if (curoffs >= offset) {
866 l = list_entry(i, yaffs_Object, siblings);
868 yaffs_GetObjectName(l, name,
869 YAFFS_MAX_NAME_LENGTH + 1);
871 (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
872 yaffs_GetObjectInode(l)));
878 yaffs_GetObjectInode(l),
879 yaffs_GetObjectType(l))
892 yaffs_GrossUnlock(dev);
898 * File creation. Allocate an inode, and we're done..
900 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
901 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
904 static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
910 yaffs_Object *obj = NULL;
913 yaffs_Object *parent = yaffs_InodeToObject(dir);
916 uid_t uid = current->fsuid;
917 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
919 if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
924 (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
925 parent->objectId, parent->variantType));
928 (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
932 T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
934 dentry->d_name.name, mode, rdev));
938 yaffs_GrossLock(dev);
940 switch (mode & S_IFMT) {
942 /* Special (socket, fifo, device...) */
943 T(YAFFS_TRACE_OS, (KERN_DEBUG
944 "yaffs_mknod: making special\n"));
945 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
947 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
948 gid, old_encode_dev(rdev));
951 yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
955 case S_IFREG: /* file */
956 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
958 yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
961 case S_IFDIR: /* directory */
963 (KERN_DEBUG "yaffs_mknod: making directory\n"));
965 yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
968 case S_IFLNK: /* symlink */
969 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
970 obj = NULL; /* Do we ever get here? */
974 /* Can not call yaffs_get_inode() with gross lock held */
975 yaffs_GrossUnlock(dev);
978 inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
979 d_instantiate(dentry, inode);
981 (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
982 obj->objectId, atomic_read(&inode->i_count)));
986 (KERN_DEBUG "yaffs_mknod failed making object\n"));
993 static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
996 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
997 retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
999 /* attempt to fix dir bug - didn't work */
1007 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1008 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1009 struct nameidata *n)
1011 static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
1014 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
1015 return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
1018 static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
1025 (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
1026 dentry->d_name.name));
1028 dev = yaffs_InodeToObject(dir)->myDev;
1030 yaffs_GrossLock(dev);
1032 retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
1034 if (retVal == YAFFS_OK) {
1035 dentry->d_inode->i_nlink--;
1037 yaffs_GrossUnlock(dev);
1038 mark_inode_dirty(dentry->d_inode);
1041 yaffs_GrossUnlock(dev);
1048 static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1049 struct dentry *dentry)
1051 struct inode *inode = old_dentry->d_inode;
1052 yaffs_Object *obj = NULL;
1053 yaffs_Object *link = NULL;
1056 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
1058 obj = yaffs_InodeToObject(inode);
1061 yaffs_GrossLock(dev);
1063 if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
1066 yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
1071 old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
1072 d_instantiate(dentry, old_dentry->d_inode);
1073 atomic_inc(&old_dentry->d_inode->i_count);
1075 (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
1076 old_dentry->d_inode->i_nlink,
1077 atomic_read(&old_dentry->d_inode->i_count)));
1081 yaffs_GrossUnlock(dev);
1091 static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1092 const char *symname)
1096 uid_t uid = current->fsuid;
1097 gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1099 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
1101 dev = yaffs_InodeToObject(dir)->myDev;
1102 yaffs_GrossLock(dev);
1103 obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
1104 S_IFLNK | S_IRWXUGO, uid, gid, symname);
1105 yaffs_GrossUnlock(dev);
1109 struct inode *inode;
1111 inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1112 d_instantiate(dentry, inode);
1113 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
1116 T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
1123 static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1130 obj = yaffs_DentryToObject(dentry);
1134 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
1135 yaffs_GrossLock(dev);
1136 yaffs_FlushFile(obj, 1);
1137 yaffs_GrossUnlock(dev);
1142 * The VFS layer already does all the dentry stuff for rename.
1144 * NB: POSIX says you can rename an object over an old object of the same name
1146 static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1147 struct inode *new_dir, struct dentry *new_dentry)
1150 int retVal = YAFFS_FAIL;
1151 yaffs_Object *target;
1153 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
1154 dev = yaffs_InodeToObject(old_dir)->myDev;
1156 yaffs_GrossLock(dev);
1158 /* Check if the target is an existing directory that is not empty. */
1160 yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
1161 new_dentry->d_name.name);
1166 target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
1167 !list_empty(&target->variant.directoryVariant.children)) {
1169 T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
1171 retVal = YAFFS_FAIL;
1174 /* Now does unlinking internally using shadowing mechanism */
1175 T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
1178 yaffs_RenameObject(yaffs_InodeToObject(old_dir),
1179 old_dentry->d_name.name,
1180 yaffs_InodeToObject(new_dir),
1181 new_dentry->d_name.name);
1184 yaffs_GrossUnlock(dev);
1186 if (retVal == YAFFS_OK) {
1188 new_dentry->d_inode->i_nlink--;
1189 mark_inode_dirty(new_dentry->d_inode);
1199 static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
1201 struct inode *inode = dentry->d_inode;
1206 (KERN_DEBUG "yaffs_setattr of object %d\n",
1207 yaffs_InodeToObject(inode)->objectId));
1209 if ((error = inode_change_ok(inode, attr)) == 0) {
1211 dev = yaffs_InodeToObject(inode)->myDev;
1212 yaffs_GrossLock(dev);
1213 if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
1219 yaffs_GrossUnlock(dev);
1221 error = inode_setattr(inode, attr);
1226 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1227 static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
1229 static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
1233 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1234 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
1236 yaffs_GrossLock(dev);
1238 buf->f_type = YAFFS_MAGIC;
1239 buf->f_bsize = sb->s_blocksize;
1240 buf->f_namelen = 255;
1241 if (sb->s_blocksize > dev->nBytesPerChunk) {
1244 (dev->endBlock - dev->startBlock +
1245 1) * dev->nChunksPerBlock / (sb->s_blocksize /
1246 dev->nBytesPerChunk);
1248 yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
1249 dev->nBytesPerChunk);
1253 (dev->endBlock - dev->startBlock +
1254 1) * dev->nChunksPerBlock * (dev->nBytesPerChunk /
1257 yaffs_GetNumberOfFreeChunks(dev) * (dev->nBytesPerChunk /
1262 buf->f_bavail = buf->f_bfree;
1264 yaffs_GrossUnlock(dev);
1270 static int yaffs_do_sync_fs(struct super_block *sb)
1273 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1274 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
1277 yaffs_GrossLock(dev);
1280 yaffs_CheckpointSave(dev);
1282 yaffs_GrossUnlock(dev);
1290 static int yaffs_write_super(struct super_block *sb)
1293 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
1294 return 0; /* yaffs_do_sync_fs(sb);*/
1299 static int yaffs_sync_fs(struct super_block *sb)
1302 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
1304 return 0; /* yaffs_do_sync_fs(sb);*/
1309 static void yaffs_read_inode(struct inode *inode)
1311 /* NB This is called as a side effect of other functions, but
1312 * we had to release the lock to prevent deadlocks, so
1313 * need to lock again.
1317 yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
1320 (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
1322 yaffs_GrossLock(dev);
1324 obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
1326 yaffs_FillInodeFromObject(inode, obj);
1328 yaffs_GrossUnlock(dev);
1331 static LIST_HEAD(yaffs_dev_list);
1333 static void yaffs_put_super(struct super_block *sb)
1335 yaffs_Device *dev = yaffs_SuperToDevice(sb);
1337 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
1339 yaffs_GrossLock(dev);
1341 yaffs_FlushEntireDeviceCache(dev);
1343 if (dev->putSuperFunc) {
1344 dev->putSuperFunc(sb);
1347 yaffs_CheckpointSave(dev);
1348 yaffs_Deinitialise(dev);
1350 yaffs_GrossUnlock(dev);
1352 /* we assume this is protected by lock_kernel() in mount/umount */
1353 list_del(&dev->devList);
1355 if(dev->spareBuffer){
1356 YFREE(dev->spareBuffer);
1357 dev->spareBuffer = NULL;
1364 static void yaffs_MTDPutSuper(struct super_block *sb)
1367 struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
1373 put_mtd_device(mtd);
1377 static void yaffs_MarkSuperBlockDirty(void *vsb)
1379 struct super_block *sb = (struct super_block *)vsb;
1381 T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
1386 static struct super_block *yaffs_internal_read_super(int yaffsVersion,
1387 struct super_block *sb,
1388 void *data, int silent)
1391 struct inode *inode = NULL;
1392 struct dentry *root;
1393 yaffs_Device *dev = 0;
1394 char devname_buf[BDEVNAME_SIZE + 1];
1395 struct mtd_info *mtd;
1398 sb->s_magic = YAFFS_MAGIC;
1399 sb->s_op = &yaffs_super_ops;
1402 printk(KERN_INFO "yaffs: sb is NULL\n");
1403 else if (!sb->s_dev)
1404 printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
1405 else if (!yaffs_devname(sb, devname_buf))
1406 printk(KERN_INFO "yaffs: devname is NULL\n");
1408 printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
1410 yaffs_devname(sb, devname_buf));
1412 sb->s_blocksize = PAGE_CACHE_SIZE;
1413 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1414 T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
1416 ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
1418 #ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
1420 ("yaffs: Write verification disabled. All guarantees "
1421 "null and void\n"));
1424 T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
1426 MAJOR(sb->s_dev), MINOR(sb->s_dev),
1427 yaffs_devname(sb, devname_buf)));
1429 /* Check it's an mtd device..... */
1430 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
1431 return NULL; /* This isn't an mtd device */
1433 /* Get the device */
1434 mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
1436 T(YAFFS_TRACE_ALWAYS,
1437 ("yaffs: MTD device #%u doesn't appear to exist\n",
1441 /* Check it's NAND */
1442 if (mtd->type != MTD_NANDFLASH) {
1443 T(YAFFS_TRACE_ALWAYS,
1444 ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
1448 T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
1449 T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
1450 T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
1451 T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
1452 T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
1453 T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
1454 T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
1455 T(YAFFS_TRACE_OS, (" oobblock %d\n", mtd->oobblock));
1456 T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
1457 T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
1458 T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
1460 #ifdef CONFIG_YAFFS_AUTO_YAFFS2
1462 if (yaffsVersion == 1 &&
1463 mtd->oobblock >= 2048) {
1464 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
1468 /* Added NCB 26/5/2006 for completeness */
1469 if (yaffsVersion == 2 &&
1470 mtd->oobblock == 512) {
1471 T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
1477 if (yaffsVersion == 2) {
1478 /* Check for version 2 style functions */
1480 !mtd->block_isbad ||
1481 !mtd->block_markbad ||
1485 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1486 T(YAFFS_TRACE_ALWAYS,
1487 ("yaffs: MTD device does not support required "
1492 if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
1493 mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
1494 T(YAFFS_TRACE_ALWAYS,
1495 ("yaffs: MTD device does not have the "
1496 "right page sizes\n"));
1500 /* Check for V1 style functions */
1505 !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
1506 T(YAFFS_TRACE_ALWAYS,
1507 ("yaffs: MTD device does not support required "
1512 if (mtd->oobblock != YAFFS_BYTES_PER_CHUNK ||
1513 mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
1514 T(YAFFS_TRACE_ALWAYS,
1515 ("yaffs: MTD device does not support have the "
1516 "right page sizes\n"));
1521 /* OK, so if we got here, we have an MTD that's NAND and looks
1522 * like it has the right capabilities
1523 * Set the yaffs_Device up for mtd
1526 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1527 sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1529 sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
1532 /* Deep shit could not allocate device structure */
1533 T(YAFFS_TRACE_ALWAYS,
1534 ("yaffs_read_super: Failed trying to allocate "
1535 "yaffs_Device. \n"));
1539 memset(dev, 0, sizeof(yaffs_Device));
1540 dev->genericDevice = mtd;
1541 dev->name = mtd->name;
1543 /* Set up the memory size parameters.... */
1545 nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
1546 dev->startBlock = 0;
1547 dev->endBlock = nBlocks - 1;
1548 dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
1549 dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
1550 dev->nReservedBlocks = 5;
1551 dev->nShortOpCaches = 10; /* Enable short op caching */
1553 /* ... and the functions. */
1554 if (yaffsVersion == 2) {
1555 dev->writeChunkWithTagsToNAND =
1556 nandmtd2_WriteChunkWithTagsToNAND;
1557 dev->readChunkWithTagsFromNAND =
1558 nandmtd2_ReadChunkWithTagsFromNAND;
1559 dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
1560 dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
1561 dev->spareBuffer = YMALLOC(mtd->oobsize);
1563 dev->nBytesPerChunk = mtd->oobblock;
1564 dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
1565 nBlocks = mtd->size / mtd->erasesize;
1567 dev->nCheckpointReservedBlocks = 10;
1568 dev->startBlock = 0;
1569 dev->endBlock = nBlocks - 1;
1571 dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
1572 dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
1575 /* ... and common functions */
1576 dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
1577 dev->initialiseNAND = nandmtd_InitialiseNAND;
1579 dev->putSuperFunc = yaffs_MTDPutSuper;
1581 dev->superBlock = (void *)sb;
1582 dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
1585 #ifndef CONFIG_YAFFS_DOES_ECC
1586 dev->useNANDECC = 1;
1589 #ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
1590 dev->wideTnodesDisabled = 1;
1593 /* we assume this is protected by lock_kernel() in mount/umount */
1594 list_add_tail(&dev->devList, &yaffs_dev_list);
1596 init_MUTEX(&dev->grossLock);
1598 yaffs_GrossLock(dev);
1600 err = yaffs_GutsInitialise(dev);
1603 ("yaffs_read_super: guts initialised %s\n",
1604 (err == YAFFS_OK) ? "OK" : "FAILED"));
1606 /* Release lock before yaffs_get_inode() */
1607 yaffs_GrossUnlock(dev);
1609 /* Create root inode */
1610 if (err == YAFFS_OK)
1611 inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
1617 inode->i_op = &yaffs_dir_inode_operations;
1618 inode->i_fop = &yaffs_dir_operations;
1620 T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
1622 root = d_alloc_root(inode);
1624 T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
1632 T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
1637 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1638 static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
1641 return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
1644 static struct super_block *yaffs_read_super(struct file_system_type *fs,
1645 int flags, const char *dev_name,
1649 return get_sb_bdev(fs, flags, dev_name, data,
1650 yaffs_internal_read_super_mtd);
1653 static struct file_system_type yaffs_fs_type = {
1654 .owner = THIS_MODULE,
1656 .get_sb = yaffs_read_super,
1657 .kill_sb = kill_block_super,
1658 .fs_flags = FS_REQUIRES_DEV,
1661 static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
1664 return yaffs_internal_read_super(1, sb, data, silent);
1667 static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
1672 #ifdef CONFIG_YAFFS_YAFFS2
1674 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1675 static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
1678 return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
1681 static struct super_block *yaffs2_read_super(struct file_system_type *fs,
1682 int flags, const char *dev_name,
1686 return get_sb_bdev(fs, flags, dev_name, data,
1687 yaffs2_internal_read_super_mtd);
1690 static struct file_system_type yaffs2_fs_type = {
1691 .owner = THIS_MODULE,
1693 .get_sb = yaffs2_read_super,
1694 .kill_sb = kill_block_super,
1695 .fs_flags = FS_REQUIRES_DEV,
1698 static struct super_block *yaffs2_read_super(struct super_block *sb,
1699 void *data, int silent)
1701 return yaffs_internal_read_super(2, sb, data, silent);
1704 static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
1708 #endif /* CONFIG_YAFFS_YAFFS2 */
1710 static struct proc_dir_entry *my_proc_entry;
1712 static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
1714 buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
1715 buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
1716 buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
1717 buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
1718 buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
1719 buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
1720 buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
1721 buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
1722 buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
1723 buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
1724 buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
1725 buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
1726 buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
1727 buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
1729 sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
1731 sprintf(buf, "passiveGCs......... %d\n",
1732 dev->passiveGarbageCollections);
1733 buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
1734 buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
1735 buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
1736 buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
1737 buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
1738 buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
1739 buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
1740 buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
1741 buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
1743 sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
1744 buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
1745 buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
1750 static int yaffs_proc_read(char *page,
1752 off_t offset, int count, int *eof, void *data)
1754 struct list_head *item;
1759 /* Get proc_file_read() to step 'offset' by one on each sucessive call.
1760 * We use 'offset' (*ppos) to indicate where we are in devList.
1761 * This also assumes the user has posted a read buffer large
1762 * enough to hold the complete output; but that's life in /proc.
1767 /* Print header first */
1769 buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
1770 "\n%s\n%s\n", yaffs_fs_c_version,
1771 yaffs_guts_c_version);
1774 /* hold lock_kernel while traversing yaffs_dev_list */
1777 /* Locate and print the Nth entry. Order N-squared but N is small. */
1778 list_for_each(item, &yaffs_dev_list) {
1779 yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
1784 buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
1785 buf = yaffs_dump_dev(buf, dev);
1790 return buf - page < count ? buf - page : count;
1794 * Set the verbosity of the warnings and error messages.
1800 unsigned mask_bitfield;
1802 {"allocate", YAFFS_TRACE_ALLOCATE},
1803 {"always", YAFFS_TRACE_ALWAYS},
1804 {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
1805 {"buffers", YAFFS_TRACE_BUFFERS},
1806 {"bug", YAFFS_TRACE_BUG},
1807 {"deletion", YAFFS_TRACE_DELETION},
1808 {"erase", YAFFS_TRACE_ERASE},
1809 {"error", YAFFS_TRACE_ERROR},
1810 {"gc_detail", YAFFS_TRACE_GC_DETAIL},
1811 {"gc", YAFFS_TRACE_GC},
1812 {"mtd", YAFFS_TRACE_MTD},
1813 {"nandaccess", YAFFS_TRACE_NANDACCESS},
1814 {"os", YAFFS_TRACE_OS},
1815 {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
1816 {"scan", YAFFS_TRACE_SCAN},
1817 {"tracing", YAFFS_TRACE_TRACING},
1818 {"write", YAFFS_TRACE_WRITE},
1819 {"all", 0xffffffff},
1824 static int yaffs_proc_write(struct file *file, const char *buf,
1825 unsigned long count, void *data)
1827 unsigned rg = 0, mask_bitfield;
1828 char *end, *mask_name;
1834 rg = yaffs_traceMask;
1836 while (!done && (pos < count)) {
1838 while ((pos < count) && isspace(buf[pos])) {
1855 mask_bitfield = simple_strtoul(buf + pos, &end, 0);
1856 if (end > buf + pos) {
1857 mask_name = "numeral";
1858 len = end - (buf + pos);
1862 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
1863 len = strlen(mask_flags[i].mask_name);
1864 if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
1865 mask_name = mask_flags[i].mask_name;
1866 mask_bitfield = mask_flags[i].mask_bitfield;
1873 if (mask_name != NULL) {
1878 rg &= ~mask_bitfield;
1881 rg |= mask_bitfield;
1887 rg |= mask_bitfield;
1893 yaffs_traceMask = rg;
1894 if (rg & YAFFS_TRACE_ALWAYS) {
1895 for (i = 0; mask_flags[i].mask_name != NULL; i++) {
1897 flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
1898 printk("%c%s\n", flag, mask_flags[i].mask_name);
1905 /* Stuff to handle installation of file systems */
1906 struct file_system_to_install {
1907 struct file_system_type *fst;
1911 static struct file_system_to_install fs_to_install[] = {
1912 //#ifdef CONFIG_YAFFS_YAFFS1
1913 {&yaffs_fs_type, 0},
1915 //#ifdef CONFIG_YAFFS_YAFFS2
1916 {&yaffs2_fs_type, 0},
1921 static int __init init_yaffs_fs(void)
1924 struct file_system_to_install *fsinst;
1926 T(YAFFS_TRACE_ALWAYS,
1927 ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
1929 /* Install the proc_fs entry */
1930 my_proc_entry = create_proc_entry("yaffs",
1934 if (my_proc_entry) {
1935 my_proc_entry->write_proc = yaffs_proc_write;
1936 my_proc_entry->read_proc = yaffs_proc_read;
1937 my_proc_entry->data = NULL;
1942 /* Now add the file system entries */
1944 fsinst = fs_to_install;
1946 while (fsinst->fst && !error) {
1947 error = register_filesystem(fsinst->fst);
1949 fsinst->installed = 1;
1954 /* Any errors? uninstall */
1956 fsinst = fs_to_install;
1958 while (fsinst->fst) {
1959 if (fsinst->installed) {
1960 unregister_filesystem(fsinst->fst);
1961 fsinst->installed = 0;
1970 static void __exit exit_yaffs_fs(void)
1973 struct file_system_to_install *fsinst;
1975 T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
1978 remove_proc_entry("yaffs", &proc_root);
1980 fsinst = fs_to_install;
1982 while (fsinst->fst) {
1983 if (fsinst->installed) {
1984 unregister_filesystem(fsinst->fst);
1985 fsinst->installed = 0;
1992 module_init(init_yaffs_fs)
1993 module_exit(exit_yaffs_fs)
1995 MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
1996 MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
1997 MODULE_LICENSE("GPL");