xip.c 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  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>
#include <linux/ext2_fs_sb.h>
#include <linux/ext2_fs.h>
#include "ext2.h"
#include "xip.h"

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

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

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

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

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

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

	return rc;
}

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

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

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

C
Carsten Otte 已提交
68 69 70 71 72
	if ((sbi->s_mount_opt & EXT2_MOUNT_XIP) &&
	    !sb->s_bdev->bd_disk->fops->direct_access) {
		sbi->s_mount_opt &= (~EXT2_MOUNT_XIP);
		ext2_warning(sb, __FUNCTION__,
			     "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
}