Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 14 | #include "ext2.h" |
| 15 | #include "xip.h" |
| 16 | |
| 17 | static inline int |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 18 | __inode_direct_access(struct inode *inode, sector_t sector, |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame^] | 19 | void **kaddr, unsigned long *pfn) |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 20 | { |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame^] | 21 | struct block_device *bdev = inode->i_sb->s_bdev; |
| 22 | struct block_device_operations *ops = bdev->bd_disk->fops; |
| 23 | |
| 24 | BUG_ON(!ops->direct_access); |
| 25 | return ops->direct_access(bdev, sector, kaddr, pfn); |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 28 | static inline int |
| 29 | __ext2_get_sector(struct inode *inode, sector_t offset, int create, |
| 30 | sector_t *result) |
| 31 | { |
| 32 | struct buffer_head tmp; |
| 33 | int rc; |
| 34 | |
| 35 | memset(&tmp, 0, sizeof(struct buffer_head)); |
| 36 | rc = ext2_get_block(inode, offset/ (PAGE_SIZE/512), &tmp, |
| 37 | create); |
| 38 | *result = tmp.b_blocknr; |
| 39 | |
| 40 | /* did we get a sparse block (hole in the file)? */ |
Carsten Otte | 0cfc11e | 2005-07-27 11:43:52 -0700 | [diff] [blame] | 41 | if (!tmp.b_blocknr && !rc) { |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 42 | BUG_ON(create); |
| 43 | rc = -ENODATA; |
| 44 | } |
| 45 | |
| 46 | return rc; |
| 47 | } |
| 48 | |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 49 | int |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 50 | ext2_clear_xip_target(struct inode *inode, int block) |
| 51 | { |
| 52 | sector_t sector = block * (PAGE_SIZE/512); |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame^] | 53 | void *kaddr; |
| 54 | unsigned long pfn; |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 55 | int rc; |
| 56 | |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame^] | 57 | rc = __inode_direct_access(inode, sector, &kaddr, &pfn); |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 58 | if (!rc) |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame^] | 59 | clear_page(kaddr); |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 60 | return rc; |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void ext2_xip_verify_sb(struct super_block *sb) |
| 64 | { |
| 65 | struct ext2_sb_info *sbi = EXT2_SB(sb); |
| 66 | |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 67 | if ((sbi->s_mount_opt & EXT2_MOUNT_XIP) && |
| 68 | !sb->s_bdev->bd_disk->fops->direct_access) { |
| 69 | sbi->s_mount_opt &= (~EXT2_MOUNT_XIP); |
| 70 | ext2_warning(sb, __FUNCTION__, |
| 71 | "ignoring xip option - not supported by bdev"); |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 75 | struct page * |
| 76 | ext2_get_xip_page(struct address_space *mapping, sector_t offset, |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 77 | int create) |
| 78 | { |
| 79 | int rc; |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame^] | 80 | void *kaddr; |
| 81 | unsigned long pfn; |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 82 | sector_t sector; |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 83 | |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 84 | /* first, retrieve the sector number */ |
| 85 | rc = __ext2_get_sector(mapping->host, offset, create, §or); |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 86 | if (rc) |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 87 | goto error; |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 88 | |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 89 | /* retrieve address of the target data */ |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 90 | rc = __inode_direct_access |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame^] | 91 | (mapping->host, sector * (PAGE_SIZE/512), &kaddr, &pfn); |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 92 | if (!rc) |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame^] | 93 | return pfn_to_page(pfn); |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 94 | |
Carsten Otte | afa597b | 2005-07-15 03:56:30 -0700 | [diff] [blame] | 95 | error: |
| 96 | return ERR_PTR(rc); |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 97 | } |