blob: c18fbf3e4068c33429c6c8e3bf3ebcc1844fe505 [file] [log] [blame]
Carsten Otte6d791252005-06-23 22:05:26 -07001/*
2 * linux/fs/ext2/xip.c
3 *
4 * Copyright (C) 2005 IBM Corporation
5 * Author: Carsten Otte (cotte@de.ibm.com)
6 */
7
8#include <linux/mm.h>
9#include <linux/fs.h>
10#include <linux/genhd.h>
11#include <linux/buffer_head.h>
12#include <linux/ext2_fs_sb.h>
13#include <linux/ext2_fs.h>
Al Viro08f85852007-10-08 13:26:20 -040014#include <linux/blkdev.h>
Carsten Otte6d791252005-06-23 22:05:26 -070015#include "ext2.h"
16#include "xip.h"
17
18static inline int
Nick Piggin70688e42008-04-28 02:13:02 -070019__inode_direct_access(struct inode *inode, sector_t block,
Jared Hulbert30afcb42008-04-28 02:13:02 -070020 void **kaddr, unsigned long *pfn)
Carsten Otteafa597b2005-07-15 03:56:30 -070021{
Jared Hulbert30afcb42008-04-28 02:13:02 -070022 struct block_device *bdev = inode->i_sb->s_bdev;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070023 const struct block_device_operations *ops = bdev->bd_disk->fops;
Nick Piggin70688e42008-04-28 02:13:02 -070024 sector_t sector;
25
26 sector = block * (PAGE_SIZE / 512); /* ext2 block to bdev sector */
Jared Hulbert30afcb42008-04-28 02:13:02 -070027
28 BUG_ON(!ops->direct_access);
29 return ops->direct_access(bdev, sector, kaddr, pfn);
Carsten Otte6d791252005-06-23 22:05:26 -070030}
31
Carsten Otteafa597b2005-07-15 03:56:30 -070032static inline int
Nick Piggin70688e42008-04-28 02:13:02 -070033__ext2_get_block(struct inode *inode, pgoff_t pgoff, int create,
Carsten Otteafa597b2005-07-15 03:56:30 -070034 sector_t *result)
35{
36 struct buffer_head tmp;
37 int rc;
38
39 memset(&tmp, 0, sizeof(struct buffer_head));
Nick Piggin70688e42008-04-28 02:13:02 -070040 rc = ext2_get_block(inode, pgoff, &tmp, create);
Carsten Otteafa597b2005-07-15 03:56:30 -070041 *result = tmp.b_blocknr;
42
43 /* did we get a sparse block (hole in the file)? */
Carsten Otte0cfc11e2005-07-27 11:43:52 -070044 if (!tmp.b_blocknr && !rc) {
Carsten Otteafa597b2005-07-15 03:56:30 -070045 BUG_ON(create);
46 rc = -ENODATA;
47 }
48
49 return rc;
50}
51
Carsten Otte6d791252005-06-23 22:05:26 -070052int
Nick Piggin70688e42008-04-28 02:13:02 -070053ext2_clear_xip_target(struct inode *inode, sector_t block)
Carsten Otteafa597b2005-07-15 03:56:30 -070054{
Jared Hulbert30afcb42008-04-28 02:13:02 -070055 void *kaddr;
56 unsigned long pfn;
Carsten Otte6d791252005-06-23 22:05:26 -070057 int rc;
58
Nick Piggin70688e42008-04-28 02:13:02 -070059 rc = __inode_direct_access(inode, block, &kaddr, &pfn);
Carsten Otteafa597b2005-07-15 03:56:30 -070060 if (!rc)
Jared Hulbert30afcb42008-04-28 02:13:02 -070061 clear_page(kaddr);
Carsten Otteafa597b2005-07-15 03:56:30 -070062 return rc;
Carsten Otte6d791252005-06-23 22:05:26 -070063}
64
65void ext2_xip_verify_sb(struct super_block *sb)
66{
67 struct ext2_sb_info *sbi = EXT2_SB(sb);
68
Carsten Otteafa597b2005-07-15 03:56:30 -070069 if ((sbi->s_mount_opt & EXT2_MOUNT_XIP) &&
70 !sb->s_bdev->bd_disk->fops->direct_access) {
71 sbi->s_mount_opt &= (~EXT2_MOUNT_XIP);
Harvey Harrison605afd62008-04-28 02:16:03 -070072 ext2_warning(sb, __func__,
Carsten Otteafa597b2005-07-15 03:56:30 -070073 "ignoring xip option - not supported by bdev");
Carsten Otte6d791252005-06-23 22:05:26 -070074 }
75}
76
Nick Piggin70688e42008-04-28 02:13:02 -070077int ext2_get_xip_mem(struct address_space *mapping, pgoff_t pgoff, int create,
78 void **kmem, unsigned long *pfn)
Carsten Otte6d791252005-06-23 22:05:26 -070079{
80 int rc;
Nick Piggin70688e42008-04-28 02:13:02 -070081 sector_t block;
Carsten Otte6d791252005-06-23 22:05:26 -070082
Carsten Otteafa597b2005-07-15 03:56:30 -070083 /* first, retrieve the sector number */
Nick Piggin70688e42008-04-28 02:13:02 -070084 rc = __ext2_get_block(mapping->host, pgoff, create, &block);
Carsten Otte6d791252005-06-23 22:05:26 -070085 if (rc)
Nick Piggin70688e42008-04-28 02:13:02 -070086 return rc;
Carsten Otte6d791252005-06-23 22:05:26 -070087
Carsten Otteafa597b2005-07-15 03:56:30 -070088 /* retrieve address of the target data */
Nick Piggin70688e42008-04-28 02:13:02 -070089 rc = __inode_direct_access(mapping->host, block, kmem, pfn);
90 return rc;
Carsten Otte6d791252005-06-23 22:05:26 -070091}