?? 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: * >> sb->u.generic_sbp 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.41 2005/10/11 23:43:27 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/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)//#define kdevname(x) cdevname(to_kdev_t(x))#define kdevname(x) "(unavailable)" // temporary fix#else#include <linux/locks.h>#endif#include <asm/uaccess.h>#include "yportenv.h"#include "yaffs_guts.h"unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS | YAFFS_TRACE_BAD_BLOCKS;#ifdef CONFIG_YAFFS_RAM_ENABLED#include "yaffs_nandemul.h" // 2 MB of RAM for emulation#define YAFFS_RAM_EMULATION_SIZE 0x200000#endif //CONFIG_YAFFS_RAM_ENABLED#ifdef CONFIG_YAFFS_MTD_ENABLED#include <linux/mtd/mtd.h>#include "yaffs_mtdif.h"#endif //CONFIG_YAFFS_MTD_ENABLED//#define T(x) printk x#define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)//NCB #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)//#if defined(CONFIG_KERNEL_2_5)#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_read(struct file *f, char *buf, size_t n, loff_t *pos);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 defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */#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 defined(CONFIG_KERNEL_2_5)#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 defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */#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);//#if defined(CONFIG_KERNEL_2_5)#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data);#elsestatic struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent);#endifstatic 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);static int yaffs_writepage(struct page *page);static 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 *buffer, int buflen);static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);static 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 = {#ifdef CONFIG_YAFFS_USE_GENERIC_RW read: generic_file_read, write: generic_file_write,#else read: yaffs_file_read, write: yaffs_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,};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,// remount_fs: delete_inode: yaffs_delete_inode, clear_inode: yaffs_clear_inode,};static void yaffs_GrossLock(yaffs_Device *dev){ T(YAFFS_TRACE_OS,("yaffs locking\n")); down(&dev->grossLock);}static void yaffs_GrossUnlock(yaffs_Device *dev){ T(YAFFS_TRACE_OS,("yaffs unlocking\n")); up(&dev->grossLock);}static int yaffs_readlink(struct dentry *dentry, char *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;}static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd){ 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_follow_link(nd,alias); kfree(alias); return ret;}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 defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */#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 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); yaffs_GrossUnlock(dev); // return dentry; return NULL;#endif } } else { T(YAFFS_TRACE_OS,("yaffs_lookup not found\n")); } yaffs_GrossUnlock(dev);/* 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 nlink %d\n", (int)inode->i_ino, atomic_read(&inode->i_count), inode->i_nlink)); }// clear is called to tell the fs to release any per-inode data it holdsstatic 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 ant 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); } 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,("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,("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 (!PageLocked(pg)) PAGE_BUG(pg); 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,("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//static int yaffs_writepage(struct page *page){ struct address_space *mapping = page->mapping; 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(); 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); } // What's happening here? ///* OK, are we completely out? */ //if (page->index >= end_index+1 || !offset) // return -EIO; get_page(page); buffer = kmap(page); obj = yaffs_InodeToObject(inode); yaffs_GrossLock(obj->myDev); nWritten = yaffs_WriteDataToFile(obj,buffer,page->index << PAGE_CACHE_SHIFT,nBytes); 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,("yaffs_prepair_write\n")); if(!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE)) return yaffs_readpage_nolock(f,pg); return 0; }static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to){ void *addr = page_address(pg) + offset;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -