📄 yaffs_fs.c
字号:
/* * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. * * Copyright (C) 2002-2009 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.79 2009/03/17 01:12:00 wookey 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/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>#include "asm/div64.h"#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))#include <linux/statfs.h> /* Added NCB 15-8-2003 */#include <linux/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, 26))#define YPROC_ROOT (&proc_root)#else#define YPROC_ROOT NULL#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#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))#define YAFFS_USE_WRITE_BEGIN_END 1#else#define YAFFS_USE_WRITE_BEGIN_END 0#endif#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size){ uint64_t result = partition_size; do_div(result, block_size); return (uint32_t)result;}#else#define YCALCBLOCKS(s, b) ((s)/(b))#endif#include <linux/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;unsigned int yaffs_auto_checkpoint = 1;/* Module Parameters */#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))module_param(yaffs_traceMask, uint, 0644);module_param(yaffs_wr_attempts, uint, 0644);module_param(yaffs_auto_checkpoint, uint, 0644);#elseMODULE_PARM(yaffs_traceMask, "i");MODULE_PARM(yaffs_wr_attempts, "i");MODULE_PARM(yaffs_auto_checkpoint, "i");#endif#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))/* use iget and read_inode */#define Y_IGET(sb, inum) iget((sb), (inum))static void yaffs_read_inode(struct inode *inode);#else/* Call local equivalent */#define YAFFS_USE_OWN_IGET#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);#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);static ssize_t yaffs_hold_space(struct file *f);static void yaffs_release_space(struct file *f);#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);#endif#ifdef YAFFS_HAS_PUT_INODEstatic void yaffs_put_inode(struct inode *inode);#endifstatic 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);#endif#if (YAFFS_USE_WRITE_BEGIN_END != 0)static int yaffs_write_begin(struct file *filp, struct address_space *mapping, loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata);static int yaffs_write_end(struct file *filp, struct address_space *mapping, loff_t pos, unsigned len, unsigned copied, struct page *pg, void *fsdadata);#elsestatic 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);#endifstatic 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,#if (YAFFS_USE_WRITE_BEGIN_END > 0) .write_begin = yaffs_write_begin, .write_end = yaffs_write_end,#else .prepare_write = yaffs_prepare_write, .commit_write = yaffs_commit_write,#endif};#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))static const struct file_operations yaffs_file_operations = { .read = do_sync_read, .write = do_sync_write, .aio_read = generic_file_aio_read, .aio_write = generic_file_aio_write, .mmap = generic_file_mmap, .flush = yaffs_file_flush, .fsync = yaffs_sync_object, .splice_read = generic_file_splice_read, .splice_write = generic_file_splice_write, .llseek = generic_file_llseek,};#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))static const struct file_operations yaffs_file_operations = { .read = do_sync_read, .write = do_sync_write, .aio_read = generic_file_aio_read, .aio_write = generic_file_aio_write, .mmap = generic_file_mmap, .flush = yaffs_file_flush, .fsync = yaffs_sync_object, .sendfile = generic_file_sendfile,};#elsestatic const struct file_operations yaffs_file_operations = { .read = generic_file_read, .write = generic_file_write, .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};#endifstatic const struct inode_operations yaffs_file_inode_operations = { .setattr = yaffs_setattr,};static const struct inode_operations yaffs_symlink_inode_operations = { .readlink = yaffs_readlink, .follow_link = yaffs_follow_link, .setattr = yaffs_setattr,};static const 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 const struct file_operations yaffs_dir_operations = { .read = generic_read_dir, .readdir = yaffs_readdir, .fsync = yaffs_sync_object,};static const struct super_operations yaffs_super_ops = { .statfs = yaffs_statfs,#ifndef YAFFS_USE_OWN_IGET .read_inode = yaffs_read_inode,#endif#ifdef YAFFS_HAS_PUT_INODE .put_inode = yaffs_put_inode,#endif .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, ("yaffs locking %p\n", current)); down(&dev->grossLock); T(YAFFS_TRACE_OS, ("yaffs locked %p\n", current));}static void yaffs_GrossUnlock(yaffs_Device *dev){ T(YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current)); 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, ("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, ("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, ("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, ("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;}#ifdef YAFFS_HAS_PUT_INODE/* 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)));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -