xip.c 2.0 KB

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