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 16
#include "ext2.h"
#include "xip.h"

static inline int
17
__inode_direct_access(struct inode *inode, sector_t block,
18
		      void **kaddr, unsigned long *pfn)
C
Carsten Otte 已提交
19
{
20
	struct block_device *bdev = inode->i_sb->s_bdev;
21
	const struct block_device_operations *ops = bdev->bd_disk->fops;
22 23 24
	sector_t sector;

	sector = block * (PAGE_SIZE / 512); /* ext2 block to bdev sector */
25 26 27

	BUG_ON(!ops->direct_access);
	return ops->direct_access(bdev, sector, kaddr, pfn);
28 29
}

C
Carsten Otte 已提交
30
static inline int
31
__ext2_get_block(struct inode *inode, pgoff_t pgoff, int create,
C
Carsten Otte 已提交
32 33 34 35 36 37
		   sector_t *result)
{
	struct buffer_head tmp;
	int rc;

	memset(&tmp, 0, sizeof(struct buffer_head));
38
	rc = ext2_get_block(inode, pgoff, &tmp, create);
C
Carsten Otte 已提交
39 40 41
	*result = tmp.b_blocknr;

	/* did we get a sparse block (hole in the file)? */
42
	if (!tmp.b_blocknr && !rc) {
C
Carsten Otte 已提交
43 44 45 46 47 48 49
		BUG_ON(create);
		rc = -ENODATA;
	}

	return rc;
}

50
int
51
ext2_clear_xip_target(struct inode *inode, sector_t block)
C
Carsten Otte 已提交
52
{
53 54
	void *kaddr;
	unsigned long pfn;
55 56
	int rc;

57
	rc = __inode_direct_access(inode, block, &kaddr, &pfn);
C
Carsten Otte 已提交
58
	if (!rc)
59
		clear_page(kaddr);
C
Carsten Otte 已提交
60
	return rc;
61 62 63 64 65 66
}

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

C
Carsten Otte 已提交
67 68 69
	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 已提交
70 71 72
		ext2_msg(sb, KERN_WARNING,
			     "warning: ignoring xip option - "
			     "not supported by bdev");
73 74 75
	}
}

76 77
int ext2_get_xip_mem(struct address_space *mapping, pgoff_t pgoff, int create,
				void **kmem, unsigned long *pfn)
78 79
{
	int rc;
80
	sector_t block;
81

C
Carsten Otte 已提交
82
	/* first, retrieve the sector number */
83
	rc = __ext2_get_block(mapping->host, pgoff, create, &block);
84
	if (rc)
85
		return rc;
86

C
Carsten Otte 已提交
87
	/* retrieve address of the target data */
88 89
	rc = __inode_direct_access(mapping->host, block, kmem, pfn);
	return rc;
90
}