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