📄 yaffs_fs.c
字号:
/* * YAFFS: Yet another FFS. A NAND-flash specific file system. * yaffs_fs.c * * Copyright (C) 2002 Aleph One Ltd. * for Toby Churchill Ltd and Brightstar Engineering * * Created by Charles Manning <charles@aleph1.co.uk> * * 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. * * 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 JFFS2. */const char *yaffs_fs_c_version = "$Id: yaffs_fs.c,v 1.38 2005/12/15 20:27:56 charles Exp $";extern const char *yaffs_guts_c_version;#include <linux/config.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/version.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>#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)#endif#include <asm/uaccess.h>#include "yportenv.h"#include "yaffs_guts.h"unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS | YAFFS_TRACE_BAD_BLOCKS /* | 0xFFFFFFFF */; #include <linux/mtd/mtd.h>#include "yaffs_mtdif.h"#include "yaffs_mtdif2.h"/*#define T(x) printk x */#define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))#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 int yaffs_file_flush(struct file *file);static 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,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 = { .read = generic_file_read, .write = generic_file_write, .mmap = generic_file_mmap, .flush = yaffs_file_flush, .fsync = yaffs_sync_object,};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,};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; inode->u.generic_ip = 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);}static int yaffs_file_flush(struct file *file){ 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);}/* writepage inspired by/stolen from smbfs */#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{ struct address_space *mapping = page->mapping; loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT; struct inode *inode; unsigned long end_index; char *buffer; yaffs_Object *obj; int nWritten = 0; unsigned nBytes; if (!mapping) BUG(); inode = mapping->host; if (!inode) BUG(); if (offset > inode->i_size) { T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_writepage at %08x, inode size = %08x!!!\n", (unsigned)(page->index << PAGE_CACHE_SHIFT), (unsigned)inode->i_size)); T(YAFFS_TRACE_OS, (KERN_DEBUG " -> don't care!!\n")); unlock_page(page); return 0; } end_index = inode->i_size >> PAGE_CACHE_SHIFT; /* easy case */ if (page->index < end_index) { nBytes = PAGE_CACHE_SIZE; } else { nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1); } get_page(page); buffer = kmap(page); obj = yaffs_InodeToObject(inode); yaffs_GrossLock(obj->myDev); T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n", (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes)); T(YAFFS_TRACE_OS, (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n", (int)obj->variant.fileVariant.fileSize, (int)inode->i_size)); nWritten = yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT, nBytes, 0); T(YAFFS_TRACE_OS, (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n", (int)obj->variant.fileVariant.fileSize, (int)inode->i_size)); yaffs_GrossUnlock(obj->myDev); kunmap(page); SetPageUptodate(page); UnlockPage(page); put_page(page); return (nWritten == nBytes) ? 0 : -ENOSPC;}static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to){ T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n")); if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE)) return yaffs_readpage_nolock(f, pg); return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -