xip.c 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 *  linux/fs/ext2/xip.c
 *
 * Copyright (C) 2005 IBM Corporation
 * Author: Carsten Otte (cotte@de.ibm.com)
 */

#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/genhd.h>
#include <linux/buffer_head.h>
12
#include <linux/blkdev.h>
13 14 15
#include "ext2.h"
#include "xip.h"

16 17
static inline long __inode_direct_access(struct inode *inode, sector_t block,
				void **kaddr, unsigned long *pfn, long size)
C
Carsten Otte 已提交
18
{
19
	struct block_device *bdev = inode->i_sb->s_bdev;
20 21
	sector_t sector = block * (PAGE_SIZE / 512);
	return bdev_direct_access(bdev, sector, kaddr, pfn, size);
22 23
}

C
Carsten Otte 已提交
24
static inline int
25
__ext2_get_block(struct inode *inode, pgoff_t pgoff, int create,
C
Carsten Otte 已提交
26 27 28 29 30 31
		   sector_t *result)
{
	struct buffer_head tmp;
	int rc;

	memset(&tmp, 0, sizeof(struct buffer_head));
32
	tmp.b_size = 1 << inode->i_blkbits;
33
	rc = ext2_get_block(inode, pgoff, &tmp, create);
C
Carsten Otte 已提交
34 35 36
	*result = tmp.b_blocknr;

	/* did we get a sparse block (hole in the file)? */
37
	if (!tmp.b_blocknr && !rc) {
C
Carsten Otte 已提交
38 39 40 41 42 43 44
		BUG_ON(create);
		rc = -ENODATA;
	}

	return rc;
}

45
int
46
ext2_clear_xip_target(struct inode *inode, sector_t block)
C
Carsten Otte 已提交
47
{
48 49
	void *kaddr;
	unsigned long pfn;
50
	long size;
51

52 53 54 55 56
	size = __inode_direct_access(inode, block, &kaddr, &pfn, PAGE_SIZE);
	if (size < 0)
		return size;
	clear_page(kaddr);
	return 0;
57 58 59 60 61 62
}

void ext2_xip_verify_sb(struct super_block *sb)
{
	struct ext2_sb_info *sbi = EXT2_SB(sb);

C
Carsten Otte 已提交
63 64 65
	if ((sbi->s_mount_opt & EXT2_MOUNT_XIP) &&
	    !sb->s_bdev->bd_disk->fops->direct_access) {
		sbi->s_mount_opt &= (~EXT2_MOUNT_XIP);
A
Alexey Fisher 已提交
66 67 68
		ext2_msg(sb, KERN_WARNING,
			     "warning: ignoring xip option - "
			     "not supported by bdev");
69 70 71
	}
}

72 73
int ext2_get_xip_mem(struct address_space *mapping, pgoff_t pgoff, int create,
				void **kmem, unsigned long *pfn)
74
{
75
	long rc;
76
	sector_t block;
77

C
Carsten Otte 已提交
78
	/* first, retrieve the sector number */
79
	rc = __ext2_get_block(mapping->host, pgoff, create, &block);
80
	if (rc)
81
		return rc;
82

C
Carsten Otte 已提交
83
	/* retrieve address of the target data */
84 85
	rc = __inode_direct_access(mapping->host, block, kmem, pfn, PAGE_SIZE);
	return (rc < 0) ? rc : 0;
86
}