深层解决cramfs文件系统不支持nandflash坏块的解决办法

今日在对公司的产品调试时,发现如果nandflash有坏块在cramfs文件系统分区,那么linux系统无法启动,提示如下信息:

uncorrectable error : 
uncorrectable error : 
end_request: I/O error, dev mtdblock0, sector 512
Buffer I/O error on device mtdblock0, logical block 64
uncorrectable error : 
uncorrectable error : 
end_request: I/O error, dev mtdblock0, sector 512
Buffer I/O error on device mtdblock0, logical block 64
uncorrectable error : 
uncorrectable error : 
end_request: I/O error, dev mtdblock0, sector 520
Buffer I/O error on device mtdblock0, logical block 65
uncorrectable error : 
uncorrectable error : 
end_request: I/O error, dev mtdblock0, sector 520
Buffer I/O error on device mtdblock0, logical block 65
Error -5 while decompressing!
802fd844(853)->81526000(4096)
g_serial gadget: g_serial ready
mice: PS/2 mouse device common for all mice
i2c /dev entries driver
at91_i2c at91_i2c: AT91 i2c bus driver.
TCP cubic registered
NET: Registered protocol family 17
VFS: Mounted root (cramfs filesystem) readonly on device 31:0.
Freeing init memory: 124K
init started: BusyBox v1.15.3 (2014-06-24 22:48:57 PDT)
starting pid 360, tty '/dev/console': '-/etc/init.d/rcS'
mount /var as ramfs
mount /proc as proc
mount /sys as sysfs
mount /dev as tmpfs
Starting mdev...
mount /mnt/local as yaffs2
yaffs: dev is 32505857 name is "mtdblock1"
yaffs: passed flags ""
yaffs: Attempting MTD mount on 31.1, "mtdblock1"
yaffs_read_super: isCheckpointed 0
uncorrectable error : 
uncorrectable error : 
end_request: I/O error, dev mtdblock0, sector 512
Buffer I/O error on device mtdblock0, logical block 64
uncorrectable error : 
uncorrectable error : 
end_request: I/O error, dev mtdblock0, sector 512
Buffer I/O error on device mtdblock0, logical block 64
uncorrectable error : 
uncorrectable error : 
end_request: I/O error, dev mtdblock0, sector 520
Buffer I/O error on device mtdblock0, logical block 65
uncorrectable error : 
uncorrectable error : 
end_request: I/O error, dev mtdblock0, sector 520
Buffer I/O error on device mtdblock0, logical block 65
Error -5 while decompressing!
802fd844(853)->81526000(4096)

然后在网上找了一下原因,是由于在nandflash上的cramfs分区上存在坏块,造成linux内核启动失败,于是根据网上的提示在fs/cramfs/inode.c文件中添加如下代码:

struct cramfs_nand_info {
	unsigned int erasesize_shift;
	uint32_t *block_map;
	uint32_t size;
};
static unsigned int cramfs_nand_transfer_offset(struct super_block *sb, unsigned int offset)
{
    struct cramfs_sb_info *sbi = sb->s_fs_info;
    struct cramfs_nand_info *nandinfo;
    nandinfo = (struct cramfs_nand_info *)(sbi + 1);
    if (!nandinfo->erasesize_shift || !nandinfo->block_map)
        return offset;
    if (offset > nandinfo->size)
        return offset;
    return  (offset + nandinfo->block_map[offset >>nandinfo->erasesize_shift]);
}
static void cramfs_fill_nand(struct super_block *sb)
{
    struct cramfs_sb_info *sbi = sb->s_fs_info;
    struct cramfs_nand_info *nandinfo;
    uint32_t *block_map = NULL;
    uint32_t offset = 0;
    nandinfo = (struct cramfs_nand_info *)(sbi + 1);
    if(MAJOR(sb->s_dev) == MTD_BLOCK_MAJOR){
        struct mtd_info *mtd;
        int blocks, i;
        mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
        if(!mtd)
            return;
        if(mtd->type != MTD_NANDFLASH)
            return;
        blocks = mtd->size>>mtd->erasesize_shift;
        block_map = kmalloc(blocks*sizeof(uint32_t), GFP_KERNEL);
        if (!block_map)
            return ;
        for(i = 0; i < blocks; i++){
            if (mtd->block_isbad(mtd, (loff_t)i*mtd->erasesize))
                 offset += mtd->erasesize;
	         block_map[i] = offset;
        }
        nandinfo->erasesize_shift = mtd->erasesize_shift;
        nandinfo->block_map = block_map;
        nandinfo->size = (uint32_t) mtd->size;
    }
}

//------------------------------ 在cramfs_read函数中进行如下修改 --------------------------------

-  sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
+ sbi = kzalloc(sizeof(struct cramfs_sb_info) + sizeof(struct cramfs_nand_info), GFP_KERNEL);
    if (!sbi)
	return -ENOMEM;
    sb->s_fs_info = sbi;
+    
+ cramfs_fill_nand(sb);
+ mutex_lock(&read_mutex);
for (i = 0; i < READ_BUFFERS; i++)
        buffer_blocknr[i] = -1;
if (!len)
        return NULL;
+ offset = cramfs_nand_transfer_offset(sb, offset);
blocknr = offset >> PAGE_CACHE_SHIFT;
offset &= PAGE_CACHE_SIZE - 1;
但是发现修改完成之后,能够启动cramfs文件系统,但是在shell中使用一部分命令时会提示如下错误:

Error -5 while decompressing!
802fd844(853)->81526000(4096)
于是分析cramfs文件系统的内部机制,发现cramfs文件系统将以页(4K)为单位进行寻址,如果我们使用的nandflash为1GB的,则1block = 32page,而cramfs在读取文件数据时,通过文件的偏移位置offset和文件长度len,去读取文件,然后解压数据。
根据上述的修改,我们只会去判断文件的起始位置是否在nandflash上是否是坏块,如果一个文件起始位置在block N的最后一个页,但是占用10个页,而block N+1为坏块,那么这个时候我们读取的数据将会出错。如图1所示:
文件与坏块位置
因此,我们好需要增加一个页机制,判断页是否在nandflash的坏块上,同时也改进了上述的连续坏块处理算法,改进之后的inode.c文件源代码如下:

/*
 * Compressed rom filesystem for Linux.
 *
 * Copyright (C) 1999 Linus Torvalds.
 *
 * This file is released under the GPL.
 */

/*
	 * These are the VFS interfaces to the compressed rom filesystem.
 * The actual compression is based on zlib, see the other files.
 */
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/blkdev.h>
#include <linux/cramfs_fs.h>
#include <linux/slab.h>
#include <linux/cramfs_fs_sb.h>
#include <linux/buffer_head.h>
#include <linux/vfs.h>
#include <linux/mutex.h>

#include <asm/uaccess.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/super.h>

static const struct super_operations cramfs_ops;
static const struct inode_operations cramfs_dir_inode_operations;
static const struct file_operations cramfs_directory_operations;
static const struct address_space_operations cramfs_aops;
static uint8_t *page_isbad_blocks;//add by pengrui,used to mark this pages whether in bad blocks

static DEFINE_MUTEX(read_mutex);


/* These two macros may change in future, to provide better st_ino
   semantics. */
#define CRAMINO(x)	(((x)->offset && (x)->size)?(x)->offset<<2:1)
#define OFFSET(x)	((x)->i_ino)
//add zhjch
struct cramfs_nand_info
{
    unsigned int erasesize_shift;
    unsigned int *block_map;
    unsigned int size;
};

static unsigned int cramfs_nand_transfer_offset(struct super_block *sb, unsigned int offset)
{
    struct cramfs_sb_info *sbi = sb->s_fs_info;
    struct cramfs_nand_info *nandinfo;
    nandinfo = (struct cramfs_nand_info *)(sbi + 1);
    if (!nandinfo->erasesize_shift || !nandinfo->block_map)
        return offset;
    if (offset > nandinfo->size)
        return offset;
    return  (offset + nandinfo->block_map[offset >> nandinfo->erasesize_shift]);
}
static void cramfs_fill_nand(struct super_block *sb)
{
    struct cramfs_sb_info *sbi = sb->s_fs_info;
    struct cramfs_nand_info *nandinfo;
    unsigned int *block_map = NULL;
    unsigned int offset = 0;
    unsigned int offset_t = 0;
    unsigned int flag = 0x0;
    unsigned int pagenr;//add by pengrui
    nandinfo = (struct cramfs_nand_info *)(sbi + 1);
    if(MAJOR(sb->s_dev) == MTD_BLOCK_MAJOR)
    {
        struct mtd_info *mtd;
        int blocks, i;
        mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
        if(!mtd)
            return;
        if(mtd->type != MTD_NANDFLASH)
            return;
        blocks = mtd->size >> mtd->erasesize_shift;
	pagenr = 1<<(mtd->erasesize_shift-PAGE_CACHE_SHIFT);//add by pengrui
        block_map = kmalloc(blocks * sizeof(uint32_t), GFP_KERNEL);
        if (!block_map)
            return ;
	//add by pengrui
	page_isbad_blocks = kmalloc(pagenr*blocks*sizeof(uint8_t),GFP_KERNEL);
        if (!page_isbad_blocks)
            return ;
	memset(page_isbad_blocks,0x0,pagenr*blocks);
	
        for(i = 0; i < blocks; i++)
        {
	    //new to zhjch
	    offset = 0x0;
	    flag = 0x0;
	    while (mtd->block_isbad(mtd, ((loff_t)i * mtd->erasesize)+offset))
            {   
		int m;
		offset += mtd->erasesize;
		//offset_t += offset;
		//add by pengrui,fill this pages flag
		for( m=0; m<pagenr; m++)
			page_isbad_blocks[i*pagenr + m] = 0x1;
		if((i*mtd->erasesize + offset) >mtd->size)
		{
			break;
		} 
		flag++;
	    }
	    offset_t +=offset;
            block_map[i] = offset_t;
	    if(flag>=2)
		offset_t-=(flag-1)*mtd->erasesize;
        }
        nandinfo->erasesize_shift = mtd->erasesize_shift;
        nandinfo->block_map = block_map;
        nandinfo->size = (uint32_t) mtd->size;
    }
}
//add by zhjch end
static int cramfs_iget5_test(struct inode *inode, void *opaque)
{
	struct cramfs_inode *cramfs_inode = opaque;
	return inode->i_ino == CRAMINO(cramfs_inode) && inode->i_ino != 1;
}

static int cramfs_iget5_set(struct inode *inode, void *opaque)
{
	struct cramfs_inode *cramfs_inode = opaque;
	inode->i_ino = CRAMINO(cramfs_inode);
	return 0;
}

static struct inode *get_cramfs_inode(struct super_block *sb,
				struct cramfs_inode * cramfs_inode)
{
	struct inode *inode = iget5_locked(sb, CRAMINO(cramfs_inode),
					    cramfs_iget5_test, cramfs_iget5_set,
					    cramfs_inode);
	static struct timespec zerotime;

	if (inode && (inode->i_state & I_NEW)) {
		inode->i_mode = cramfs_inode->mode;
		inode->i_uid = cramfs_inode->uid;
		inode->i_size = cramfs_inode->size;
		inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
		inode->i_gid = cramfs_inode->gid;
		/* Struct copy intentional */
		inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
		/* inode->i_nlink is left 1 - arguably wrong for directories,
		   but it's the best we can do without reading the directory
		   contents.  1 yields the right result in GNU find, even
		   without -noleaf option. */
		if (S_ISREG(inode->i_mode)) {
			inode->i_fop = &generic_ro_fops;
			inode->i_data.a_ops = &cramfs_aops;
		} else if (S_ISDIR(inode->i_mode)) {
			inode->i_op = &cramfs_dir_inode_operations;
			inode->i_fop = &cramfs_directory_operations;
		} else if (S_ISLNK(inode->i_mode)) {
			inode->i_op = &page_symlink_inode_operations;
			inode->i_data.a_ops = &cramfs_aops;
		} else {
			init_special_inode(inode, inode->i_mode,
				old_decode_dev(cramfs_inode->size));
		}
		unlock_new_inode(inode);
	}
	return inode;
}

static void cramfs_drop_inode(struct inode *inode)
{
	if (inode->i_ino == 1)
		generic_delete_inode(inode);
	else
		generic_drop_inode(inode);
}

/*
 * We have our own block cache: don't fill up the buffer cache
 * with the rom-image, because the way the filesystem is set
 * up the accesses should be fairly regular and cached in the
 * page cache and dentry tree anyway..
 *
 * This also acts as a way to guarantee contiguous areas of up to
 * BLKS_PER_BUF*PAGE_CACHE_SIZE, so that the caller doesn't need to
 * worry about end-of-buffer issues even when decompressing a full
 * page cache.
 */
#define READ_BUFFERS (2)
/* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
#define NEXT_BUFFER(_ix) ((_ix) ^ 1)

/*
 * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed"
 * data that takes up more space than the original and with unlucky
 * alignment.
 */
#define BLKS_PER_BUF_SHIFT	(2)
#define BLKS_PER_BUF		(1 << BLKS_PER_BUF_SHIFT)
#define BUFFER_SIZE		(BLKS_PER_BUF*PAGE_CACHE_SIZE)

static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
static unsigned buffer_blocknr[READ_BUFFERS];
static struct super_block * buffer_dev[READ_BUFFERS];
static int next_buffer;

/*
 * Returns a pointer to a buffer containing at least LEN bytes of
 * filesystem starting at byte offset OFFSET into the filesystem.
 */
static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned int len)
{
	struct mtd_info *mtd;
	struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
	struct page *pages[BLKS_PER_BUF];
	unsigned i, blocknr, pagenr, buffer;
	unsigned long devsize;
	char *data;
	int flag = 0;
	
	if (!len)
		return NULL;
        //add by pengrui
	mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
        if(!mtd)
            return NULL;
        if(mtd->type != MTD_NANDFLASH)
            return NULL;
	offset = cramfs_nand_transfer_offset(sb, offset);
	
	blocknr = offset >> PAGE_CACHE_SHIFT;
	pagenr = 1<<(mtd->erasesize_shift-PAGE_CACHE_SHIFT);//add by pengrui
	offset &= PAGE_CACHE_SIZE - 1;
	
	/* Check if an existing buffer already has the data.. */
	for (i = 0; i < READ_BUFFERS; i++) {
		unsigned int blk_offset;

		if (buffer_dev[i] != sb)
			continue;
		if (blocknr < buffer_blocknr[i])
			continue;
		blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_CACHE_SHIFT;
		blk_offset += offset;
		if (blk_offset + len > BUFFER_SIZE)
			continue;
		return read_buffers[i] + blk_offset;
	}
	devsize = mapping->host->i_size >> PAGE_CACHE_SHIFT;

	/* Ok, read in BLKS_PER_BUF pages completely first. */
	for (i = 0; i < BLKS_PER_BUF; i++) {
		struct page *page = NULL;

		if (blocknr + i < devsize) {//old
			//add by pengrui,just check page whether in bad block
			unsigned int pagenum = 0x0;
			pagenum = blocknr+i;
			while(page_isbad_blocks[pagenum])
			{
				pagenum+=pagenr;
			}
			page = read_mapping_page_async(mapping, pagenum, NULL);
			/* synchronous error? */
			if (IS_ERR(page))
				page = NULL;
		}
		pages[i] = page;
	}

	for (i = 0; i < BLKS_PER_BUF; i++) {
		struct page *page = pages[i];
		if (page) {
			wait_on_page_locked(page);
			if (!PageUptodate(page)) {
				/* asynchronous error */
				page_cache_release(page);
				pages[i] = NULL;
			}
		}
	}

	buffer = next_buffer;
	next_buffer = NEXT_BUFFER(buffer);
	buffer_blocknr[buffer] = blocknr;
	buffer_dev[buffer] = sb;

	data = read_buffers[buffer];
	for (i = 0; i < BLKS_PER_BUF; i++) {
		struct page *page = pages[i];
		if (page) {
			memcpy(data, kmap(page), PAGE_CACHE_SIZE);
			kunmap(page);
			page_cache_release(page);
		} else
			memset(data, 0, PAGE_CACHE_SIZE);
		data += PAGE_CACHE_SIZE;
	}
	return read_buffers[buffer] + offset;
}

static void cramfs_put_super(struct super_block *sb)
{
	kfree(sb->s_fs_info);
	sb->s_fs_info = NULL;
}

static int cramfs_remount(struct super_block *sb, int *flags, char *data)
{
	*flags |= MS_RDONLY;
	return 0;
}

static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
{
	int i;
	struct cramfs_super super;
	unsigned long root_offset;
	struct cramfs_sb_info *sbi;
	struct inode *root;

	sb->s_flags |= MS_RDONLY;
	//old
	//sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
	//new
	sbi = kzalloc(sizeof(struct cramfs_sb_info) + sizeof(struct cramfs_nand_info), GFP_KERNEL);
	if (!sbi)
		return -ENOMEM;
	sb->s_fs_info = sbi;
	//new
	/*add patch for skipping bad nand*/
	cramfs_fill_nand(sb);
	/* Invalidate the read buffers on mount: think disk change.. */
	mutex_lock(&read_mutex);
	for (i = 0; i < READ_BUFFERS; i++)
		buffer_blocknr[i] = -1;

	/* Read the first block and get the superblock from it */
	memcpy(&super, cramfs_read(sb, 0, sizeof(super)), sizeof(super));
	mutex_unlock(&read_mutex);

	/* Do sanity checks on the superblock */
	if (super.magic != CRAMFS_MAGIC) {
		/* check for wrong endianess */
		if (super.magic == CRAMFS_MAGIC_WEND) {
			if (!silent)
				printk(KERN_ERR "cramfs: wrong endianess\n");
			goto out;
		}

		/* check at 512 byte offset */
		mutex_lock(&read_mutex);
		memcpy(&super, cramfs_read(sb, 512, sizeof(super)), sizeof(super));
		mutex_unlock(&read_mutex);
		if (super.magic != CRAMFS_MAGIC) {
			if (super.magic == CRAMFS_MAGIC_WEND && !silent)
				printk(KERN_ERR "cramfs: wrong endianess\n");
			else if (!silent)
				printk(KERN_ERR "cramfs: wrong magic\n");
			goto out;
		}
	}

	/* get feature flags first */
	if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
		printk(KERN_ERR "cramfs: unsupported filesystem features\n");
		goto out;
	}

	/* Check that the root inode is in a sane state */
	if (!S_ISDIR(super.root.mode)) {
		printk(KERN_ERR "cramfs: root is not a directory\n");
		goto out;
	}
	root_offset = super.root.offset << 2;
	if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
		sbi->size=super.size;
		sbi->blocks=super.fsid.blocks;
		sbi->files=super.fsid.files;
	} else {
		sbi->size=1<<28;
		sbi->blocks=0;
		sbi->files=0;
	}
	sbi->magic=super.magic;
	sbi->flags=super.flags;
	if (root_offset == 0)
		printk(KERN_INFO "cramfs: empty filesystem");
	else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
		 ((root_offset != sizeof(struct cramfs_super)) &&
		  (root_offset != 512 + sizeof(struct cramfs_super))))
	{
		printk(KERN_ERR "cramfs: bad root offset %lu\n", root_offset);
		goto out;
	}

	/* Set it all up.. */
	sb->s_op = &cramfs_ops;
	root = get_cramfs_inode(sb, &super.root);
	if (!root)
		goto out;
	sb->s_root = d_alloc_root(root);
	if (!sb->s_root) {
		iput(root);
		goto out;
	}
	return 0;
out:
	kfree(sbi);
	sb->s_fs_info = NULL;
	return -EINVAL;
}

static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
{
	struct super_block *sb = dentry->d_sb;
	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);

	buf->f_type = CRAMFS_MAGIC;
	buf->f_bsize = PAGE_CACHE_SIZE;
	buf->f_blocks = CRAMFS_SB(sb)->blocks;
	buf->f_bfree = 0;
	buf->f_bavail = 0;
	buf->f_files = CRAMFS_SB(sb)->files;
	buf->f_ffree = 0;
	buf->f_fsid.val[0] = (u32)id;
	buf->f_fsid.val[1] = (u32)(id >> 32);
	buf->f_namelen = CRAMFS_MAXPATHLEN;
	return 0;
}

/*
 * Read a cramfs directory entry.
 */
static int cramfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
	struct inode *inode = filp->f_path.dentry->d_inode;
	struct super_block *sb = inode->i_sb;
	char *buf;
	unsigned int offset;
	int copied;

	/* Offset within the thing. */
	offset = filp->f_pos;
	if (offset >= inode->i_size)
		return 0;
	/* Directory entries are always 4-byte aligned */
	if (offset & 3)
		return -EINVAL;

	buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	copied = 0;
	while (offset < inode->i_size) {
		struct cramfs_inode *de;
		unsigned long nextoffset;
		char *name;
		ino_t ino;
		mode_t mode;
		int namelen, error;

		mutex_lock(&read_mutex);
		de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
		name = (char *)(de+1);

		/*
		 * Namelengths on disk are shifted by two
		 * and the name padded out to 4-byte boundaries
		 * with zeroes.
		 */
		namelen = de->namelen << 2;
		memcpy(buf, name, namelen);
		ino = CRAMINO(de);
		mode = de->mode;
		mutex_unlock(&read_mutex);
		nextoffset = offset + sizeof(*de) + namelen;
		for (;;) {
			if (!namelen) {
				kfree(buf);
				return -EIO;
			}
			if (buf[namelen-1])
				break;
			namelen--;
		}
		error = filldir(dirent, buf, namelen, offset, ino, mode >> 12);
		if (error)
			break;

		offset = nextoffset;
		filp->f_pos = offset;
		copied++;
	}
	kfree(buf);
	return 0;
}

/*
 * Lookup and fill in the inode data..
 */
static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
{
	unsigned int offset = 0;
	int sorted;

	mutex_lock(&read_mutex);
	sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS;
	while (offset < dir->i_size) {
		struct cramfs_inode *de;
		char *name;
		int namelen, retval;

		de = cramfs_read(dir->i_sb, OFFSET(dir) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
		name = (char *)(de+1);

		/* Try to take advantage of sorted directories */
		if (sorted && (dentry->d_name.name[0] < name[0]))
			break;

		namelen = de->namelen << 2;
		offset += sizeof(*de) + namelen;

		/* Quick check that the name is roughly the right length */
		if (((dentry->d_name.len + 3) & ~3) != namelen)
			continue;

		for (;;) {
			if (!namelen) {
				mutex_unlock(&read_mutex);
				return ERR_PTR(-EIO);
			}
			if (name[namelen-1])
				break;
			namelen--;
		}
		if (namelen != dentry->d_name.len)
			continue;
		retval = memcmp(dentry->d_name.name, name, namelen);
		if (retval > 0)
			continue;
		if (!retval) {
			struct cramfs_inode entry = *de;
			mutex_unlock(&read_mutex);
			d_add(dentry, get_cramfs_inode(dir->i_sb, &entry));
			return NULL;
		}
		/* else (retval < 0) */
		if (sorted)
			break;
	}
	mutex_unlock(&read_mutex);
	d_add(dentry, NULL);
	return NULL;
}

static int cramfs_readpage(struct file *file, struct page * page)
{
	struct inode *inode = page->mapping->host;
	u32 maxblock;
	int bytes_filled;
	void *pgdata;

	maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
	bytes_filled = 0;
	pgdata = kmap(page);

	if (page->index < maxblock) {
		struct super_block *sb = inode->i_sb;
		u32 blkptr_offset = OFFSET(inode) + page->index*4;
		u32 start_offset, compr_len;

		start_offset = OFFSET(inode) + maxblock*4;
		mutex_lock(&read_mutex);
		if (page->index)
			start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4,
				4);
		compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4) -
			start_offset);
		mutex_unlock(&read_mutex);

		if (compr_len == 0)
			; /* hole */
		else if (unlikely(compr_len > (PAGE_CACHE_SIZE << 1))) {
			pr_err("cramfs: bad compressed blocksize %u\n",
				compr_len);
			goto err;
		} else {
			mutex_lock(&read_mutex);
			bytes_filled = cramfs_uncompress_block(pgdata,
				 PAGE_CACHE_SIZE,
				 cramfs_read(sb, start_offset, compr_len),
				 compr_len);
			mutex_unlock(&read_mutex);
			if (unlikely(bytes_filled < 0))
				goto err;
		}
	}

	memset(pgdata + bytes_filled, 0, PAGE_CACHE_SIZE - bytes_filled);
	flush_dcache_page(page);
	kunmap(page);
	SetPageUptodate(page);
	unlock_page(page);
	return 0;

err:
	kunmap(page);
	ClearPageUptodate(page);
	SetPageError(page);
	unlock_page(page);
	return 0;
}

static const struct address_space_operations cramfs_aops = {
	.readpage = cramfs_readpage
};

/*
 * Our operations:
 */

/*
 * A directory can only readdir
 */
static const struct file_operations cramfs_directory_operations = {
	.llseek		= generic_file_llseek,
	.read		= generic_read_dir,
	.readdir	= cramfs_readdir,
};

static const struct inode_operations cramfs_dir_inode_operations = {
	.lookup		= cramfs_lookup,
};

static const struct super_operations cramfs_ops = {
	.put_super	= cramfs_put_super,
	.remount_fs	= cramfs_remount,
	.statfs		= cramfs_statfs,
	.drop_inode	= cramfs_drop_inode,
};

static int cramfs_get_sb(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
	return get_sb_bdev(fs_type, flags, dev_name, data, cramfs_fill_super,
			   mnt);
}

static struct file_system_type cramfs_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "cramfs",
	.get_sb		= cramfs_get_sb,
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
};

static int __init init_cramfs_fs(void)
{
	int rv;

	rv = cramfs_uncompress_init();
	if (rv < 0)
		return rv;
	rv = register_filesystem(&cramfs_fs_type);
	if (rv < 0)
		cramfs_uncompress_exit();
	return rv;
}

static void __exit exit_cramfs_fs(void)
{
	kfree(page_isbad_blocks);
	cramfs_uncompress_exit();
	unregister_filesystem(&cramfs_fs_type);
}

module_init(init_cramfs_fs)
module_exit(exit_cramfs_fs)
MODULE_LICENSE("GPL");
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值