xip.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. const 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_msg(sb, KERN_WARNING,
  61. "warning: ignoring xip option - "
  62. "not supported by bdev");
  63. }
  64. }
  65. int ext2_get_xip_mem(struct address_space *mapping, pgoff_t pgoff, int create,
  66. void **kmem, unsigned long *pfn)
  67. {
  68. int rc;
  69. sector_t block;
  70. /* first, retrieve the sector number */
  71. rc = __ext2_get_block(mapping->host, pgoff, create, &block);
  72. if (rc)
  73. return rc;
  74. /* retrieve address of the target data */
  75. rc = __inode_direct_access(mapping->host, block, kmem, pfn);
  76. return rc;
  77. }