📄 yaffs_fs.c
字号:
/* * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. * * Copyright (C) 2002-2007 Aleph One Ltd. * for Toby Churchill Ltd and Brightstar Engineering * * Created by Charles Manning <charles@aleph1.co.uk> * Acknowledgements: * Luc van OostenRyck for numerous patches. * Nick Bane for numerous patches. * Nick Bane for 2.5/2.6 integration. * Andras Toth for mknod rdev issue. * Michael Fischer for finding the problem with inode inconsistency. * Some code bodily lifted from JFFS * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. *//* * * This is the file system front-end to YAFFS that hooks it up to * the VFS. * * Special notes: * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with * this superblock * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this * superblock * >> inode->u.generic_ip points to the associated yaffs_Object. */const char *yaffs_fs_c_version = "$Id: yaffs_fs.c,v 1.62 2007/08/16 20:42:11 imcd Exp $";extern const char *yaffs_guts_c_version;#include <linux/version.h>#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))#include <linux/config.h>#endif#include <linux/kernel.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/list.h>#include <linux/fs.h>#include <linux/proc_fs.h>#include <linux/smp_lock.h>#include <linux/pagemap.h>#include <linux/mtd/mtd.h>#include <linux/interrupt.h>#include <linux/string.h>#include <linux/ctype.h>#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))#include <linux/statfs.h> /* Added NCB 15-8-2003 */#include <asm/statfs.h>#define UnlockPage(p) unlock_page(p)#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)/* FIXME: use sb->s_id instead ? */#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)#else#include <linux/locks.h>#define BDEVNAME_SIZE 0#define yaffs_devname(sb, buf) kdevname(sb->s_dev)#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */#define __user#endif#endif#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))#define WRITE_SIZE_STR "writesize"#define WRITE_SIZE(mtd) (mtd)->writesize#else#define WRITE_SIZE_STR "oobblock"#define WRITE_SIZE(mtd) (mtd)->oobblock#endif#include <asm/uaccess.h>#include "yportenv.h"#include "yaffs_guts.h"#include <linux/mtd/mtd.h>#include "yaffs_mtdif.h"#include "yaffs_mtdif1.h"#include "yaffs_mtdif2.h"unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;/* Module Parameters */#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))module_param(yaffs_traceMask,uint,0644);module_param(yaffs_wr_attempts,uint,0644);#elseMODULE_PARM(yaffs_traceMask,"i");MODULE_PARM(yaffs_wr_attempts,"i");#endif/*#define T(x) printk x */#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))#define yaffs_InodeToObjectLV(iptr) (iptr)->i_private#else#define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip#endif#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)#else#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)#endifstatic void yaffs_put_super(struct super_block *sb);static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t * pos);#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))static int yaffs_file_flush(struct file *file, fl_owner_t id);#elsestatic int yaffs_file_flush(struct file *file);#endifstatic int yaffs_sync_object(struct file *file, struct dentry *dentry, int datasync);static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *n);static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *n);#elsestatic int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);#endifstatic int yaffs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry);static int yaffs_unlink(struct inode *dir, struct dentry *dentry);static int yaffs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev);#elsestatic int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev);#endifstatic int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry);static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))static int yaffs_sync_fs(struct super_block *sb, int wait);static void yaffs_write_super(struct super_block *sb);#elsestatic int yaffs_sync_fs(struct super_block *sb);static int yaffs_write_super(struct super_block *sb);#endif#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);#elsestatic int yaffs_statfs(struct super_block *sb, struct statfs *buf);#endifstatic void yaffs_read_inode(struct inode *inode);static void yaffs_put_inode(struct inode *inode);static void yaffs_delete_inode(struct inode *);static void yaffs_clear_inode(struct inode *);static int yaffs_readpage(struct file *file, struct page *page);#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))static int yaffs_writepage(struct page *page, struct writeback_control *wbc);#elsestatic int yaffs_writepage(struct page *page);#endifstatic int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to);static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to);static int yaffs_readlink(struct dentry *dentry, char __user * buffer, int buflen);#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);#elsestatic int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);#endifstatic struct address_space_operations yaffs_file_address_operations = { .readpage = yaffs_readpage, .writepage = yaffs_writepage, .prepare_write = yaffs_prepare_write, .commit_write = yaffs_commit_write,};static struct file_operations yaffs_file_operations = {#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)) .read = do_sync_read, .write = do_sync_write, .aio_read = generic_file_aio_read, .aio_write = generic_file_aio_write,#else .read = generic_file_read, .write = generic_file_write,#endif .mmap = generic_file_mmap, .flush = yaffs_file_flush, .fsync = yaffs_sync_object,#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) .sendfile = generic_file_sendfile,#endif};static struct inode_operations yaffs_file_inode_operations = { .setattr = yaffs_setattr,};static struct inode_operations yaffs_symlink_inode_operations = { .readlink = yaffs_readlink, .follow_link = yaffs_follow_link, .setattr = yaffs_setattr,};static struct inode_operations yaffs_dir_inode_operations = { .create = yaffs_create, .lookup = yaffs_lookup, .link = yaffs_link, .unlink = yaffs_unlink, .symlink = yaffs_symlink, .mkdir = yaffs_mkdir, .rmdir = yaffs_unlink, .mknod = yaffs_mknod, .rename = yaffs_rename, .setattr = yaffs_setattr,};static struct file_operations yaffs_dir_operations = { .read = generic_read_dir, .readdir = yaffs_readdir, .fsync = yaffs_sync_object,};static struct super_operations yaffs_super_ops = { .statfs = yaffs_statfs, .read_inode = yaffs_read_inode, .put_inode = yaffs_put_inode, .put_super = yaffs_put_super, .delete_inode = yaffs_delete_inode, .clear_inode = yaffs_clear_inode, .sync_fs = yaffs_sync_fs, .write_super = yaffs_write_super,};static void yaffs_GrossLock(yaffs_Device * dev){ T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n")); down(&dev->grossLock);}static void yaffs_GrossUnlock(yaffs_Device * dev){ T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n")); up(&dev->grossLock);}static int yaffs_readlink(struct dentry *dentry, char __user * buffer, int buflen){ unsigned char *alias; int ret; yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev; yaffs_GrossLock(dev); alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry)); yaffs_GrossUnlock(dev); if (!alias) return -ENOMEM; ret = vfs_readlink(dentry, buffer, buflen, alias); kfree(alias); return ret;}#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)#elsestatic int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)#endif{ unsigned char *alias; int ret; yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev; yaffs_GrossLock(dev); alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry)); yaffs_GrossUnlock(dev); if (!alias) { ret = -ENOMEM; goto out; } ret = vfs_follow_link(nd, alias); kfree(alias);out:#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13)) return ERR_PTR (ret);#else return ret;#endif}struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev, yaffs_Object * obj);/* * Lookup is used to find objects in the fs */#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *n)#elsestatic struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)#endif{ yaffs_Object *obj; struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */ yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev; yaffs_GrossLock(dev); T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup for %d:%s\n", yaffs_InodeToObject(dir)->objectId, dentry->d_name.name)); obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir), dentry->d_name.name); obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */ /* Can't hold gross lock when calling yaffs_get_inode() */ yaffs_GrossUnlock(dev); if (obj) { T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId)); inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj); if (inode) { T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_loookup dentry \n"));/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to * d_add even if NULL inode */#if 0 /*dget(dentry); // try to solve directory bug */ d_add(dentry, inode); /* return dentry; */ return NULL;#endif } } else { T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n")); }/* added NCB for 2.5/6 compatability - forces add even if inode is * NULL which creates dentry hash */ d_add(dentry, inode); return NULL; /* return (ERR_PTR(-EIO)); */}/* For now put inode is just for debugging * Put inode is called when the inode **structure** is put. */static void yaffs_put_inode(struct inode *inode){ T(YAFFS_TRACE_OS, ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino, atomic_read(&inode->i_count)));}/* clear is called to tell the fs to release any per-inode data it holds */static void yaffs_clear_inode(struct inode *inode){ yaffs_Object *obj; yaffs_Device *dev; obj = yaffs_InodeToObject(inode); T(YAFFS_TRACE_OS, ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino, atomic_read(&inode->i_count), obj ? "object exists" : "null object")); if (obj) { dev = obj->myDev; yaffs_GrossLock(dev); /* Clear the association between the inode and * the yaffs_Object. */ obj->myInode = NULL; yaffs_InodeToObjectLV(inode) = NULL; /* If the object freeing was deferred, then the real * free happens now. * This should fix the inode inconsistency problem. */ yaffs_HandleDeferedFree(obj); yaffs_GrossUnlock(dev); }}/* delete is called when the link count is zero and the inode * is put (ie. nobody wants to know about it anymore, time to * delete the file). * NB Must call clear_inode() */static void yaffs_delete_inode(struct inode *inode){ yaffs_Object *obj = yaffs_InodeToObject(inode); yaffs_Device *dev; T(YAFFS_TRACE_OS, ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino, atomic_read(&inode->i_count), obj ? "object exists" : "null object")); if (obj) { dev = obj->myDev; yaffs_GrossLock(dev); yaffs_DeleteFile(obj); yaffs_GrossUnlock(dev); }#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13)) truncate_inode_pages (&inode->i_data, 0);#endif clear_inode(inode);}#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))static int yaffs_file_flush(struct file *file, fl_owner_t id)#elsestatic int yaffs_file_flush(struct file *file)#endif{ yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry); yaffs_Device *dev = obj->myDev; T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId, obj->dirty ? "dirty" : "clean")); yaffs_GrossLock(dev); yaffs_FlushFile(obj, 1); yaffs_GrossUnlock(dev); return 0;}static int yaffs_readpage_nolock(struct file *f, struct page *pg){ /* Lifted from jffs2 */ yaffs_Object *obj; unsigned char *pg_buf; int ret; yaffs_Device *dev; T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n", (unsigned)(pg->index << PAGE_CACHE_SHIFT), (unsigned)PAGE_CACHE_SIZE)); obj = yaffs_DentryToObject(f->f_dentry); dev = obj->myDev;#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) BUG_ON(!PageLocked(pg));#else if (!PageLocked(pg)) PAGE_BUG(pg);#endif pg_buf = kmap(pg); /* FIXME: Can kmap fail? */ yaffs_GrossLock(dev); ret = yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE); yaffs_GrossUnlock(dev); if (ret >= 0) ret = 0; if (ret) { ClearPageUptodate(pg); SetPageError(pg); } else { SetPageUptodate(pg); ClearPageError(pg); } flush_dcache_page(pg); kunmap(pg); T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n")); return ret;}static int yaffs_readpage_unlock(struct file *f, struct page *pg){ int ret = yaffs_readpage_nolock(f, pg); UnlockPage(pg); return ret;}static int yaffs_readpage(struct file *f, struct page *pg){ return yaffs_readpage_unlock(f, pg);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -