xip.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * linux/fs/ext2/xip.c
  3. *
  4. * Copyright (C) 2005 IBM Corporation
  5. * Author: Carsten Otte (cotte@de.ibm.com)
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/fs.h>
  9. #include <linux/genhd.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/ext2_fs_sb.h>
  12. #include <linux/ext2_fs.h>
  13. #include <linux/blkdev.h>
  14. #include "ext2.h"
  15. #include "xip.h"
  16. static inline int
  17. __inode_direct_access(struct inode *inode, sector_t block,
  18. void **kaddr, unsigned long *pfn)
  19. {
  20. struct block_device *bdev = inode->i_sb->s_bdev;
  21. struct block_device_operations *ops = bdev->bd_disk->fops;
  22. sector_t sector;
  23. sector = block * (PAGE_SIZE / 512); /* ext2 block to bdev sector */
  24. BUG_ON(!ops->direct_access);
  25. return ops->direct_access(bdev, sector, kaddr, pfn);
  26. }
  27. static inline int
  28. __ext2_get_block(struct inode *inode, pgoff_t pgoff, int create,
  29. sector_t *result)
  30. {
  31. struct buffer_head tmp;
  32. int rc;
  33. memset(&tmp, 0, sizeof(struct buffer_head));
  34. rc = ext2_get_block(inode, pgoff, &tmp, create);
  35. *result = tmp.b_blocknr;
  36. /* did we get a sparse block (hole in the file)? */
  37. if (!tmp.b_blocknr && !rc) {
  38. BUG_ON(create);
  39. rc = -ENODATA;
  40. }
  41. return rc;
  42. }
  43. int
  44. ext2_clear_xip_target(struct inode *inode, sector_t block)
  45. {
  46. void *kaddr;
  47. unsigned long pfn;
  48. int rc;
  49. rc = __inode_direct_access(inode, block, &kaddr, &pfn);
  50. if (!rc)
  51. clear_page(kaddr);
  52. return rc;
  53. }
  54. void ext2_xip_verify_sb(struct super_block *sb)
  55. {
  56. struct ext2_sb_info *sbi = EXT2_SB(sb);
  57. if ((sbi->s_mount_opt & EXT2_MOUNT_XIP) &&
  58. !sb->s_bdev->bd_disk->fops->direct_access) {
  59. sbi->s_mount_opt &= (~EXT2_MOUNT_XIP);
  60. ext2_warning(sb, __func__,
  61. "ignoring xip option - not supported by bdev");
  62. }
  63. }
  64. int ext2_get_xip_mem(struct address_space *mapping, pgoff_t pgoff, int create,
  65. void **kmem, unsigned long *pfn)
  66. {
  67. int rc;
  68. sector_t block;
  69. /* first, retrieve the sector number */
  70. rc = __ext2_get_block(mapping->host, pgoff, create, &block);
  71. if (rc)
  72. return rc;
  73. /* retrieve address of the target data */
  74. rc = __inode_direct_access(mapping->host, block, kmem, pfn);
  75. return rc;
  76. }