file.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * linux/fs/hpfs/file.c
  3. *
  4. * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
  5. *
  6. * file VFS functions
  7. */
  8. #include "hpfs_fn.h"
  9. #define BLOCKS(size) (((size) + 511) >> 9)
  10. static int hpfs_file_release(struct inode *inode, struct file *file)
  11. {
  12. lock_kernel();
  13. hpfs_write_if_changed(inode);
  14. unlock_kernel();
  15. return 0;
  16. }
  17. int hpfs_file_fsync(struct file *file, struct dentry *dentry, int datasync)
  18. {
  19. /*return file_fsync(file, dentry);*/
  20. return 0; /* Don't fsync :-) */
  21. }
  22. /*
  23. * generic_file_read often calls bmap with non-existing sector,
  24. * so we must ignore such errors.
  25. */
  26. static secno hpfs_bmap(struct inode *inode, unsigned file_secno)
  27. {
  28. struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
  29. unsigned n, disk_secno;
  30. struct fnode *fnode;
  31. struct buffer_head *bh;
  32. if (BLOCKS(hpfs_i(inode)->mmu_private) <= file_secno) return 0;
  33. n = file_secno - hpfs_inode->i_file_sec;
  34. if (n < hpfs_inode->i_n_secs) return hpfs_inode->i_disk_sec + n;
  35. if (!(fnode = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) return 0;
  36. disk_secno = hpfs_bplus_lookup(inode->i_sb, inode, &fnode->btree, file_secno, bh);
  37. if (disk_secno == -1) return 0;
  38. if (hpfs_chk_sectors(inode->i_sb, disk_secno, 1, "bmap")) return 0;
  39. return disk_secno;
  40. }
  41. static void hpfs_truncate(struct inode *i)
  42. {
  43. if (IS_IMMUTABLE(i)) return /*-EPERM*/;
  44. lock_kernel();
  45. hpfs_i(i)->i_n_secs = 0;
  46. i->i_blocks = 1 + ((i->i_size + 511) >> 9);
  47. hpfs_i(i)->mmu_private = i->i_size;
  48. hpfs_truncate_btree(i->i_sb, i->i_ino, 1, ((i->i_size + 511) >> 9));
  49. hpfs_write_inode(i);
  50. hpfs_i(i)->i_n_secs = 0;
  51. unlock_kernel();
  52. }
  53. static int hpfs_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
  54. {
  55. secno s;
  56. s = hpfs_bmap(inode, iblock);
  57. if (s) {
  58. map_bh(bh_result, inode->i_sb, s);
  59. return 0;
  60. }
  61. if (!create) return 0;
  62. if (iblock<<9 != hpfs_i(inode)->mmu_private) {
  63. BUG();
  64. return -EIO;
  65. }
  66. if ((s = hpfs_add_sector_to_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1)) == -1) {
  67. hpfs_truncate_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1);
  68. return -ENOSPC;
  69. }
  70. inode->i_blocks++;
  71. hpfs_i(inode)->mmu_private += 512;
  72. set_buffer_new(bh_result);
  73. map_bh(bh_result, inode->i_sb, s);
  74. return 0;
  75. }
  76. static int hpfs_writepage(struct page *page, struct writeback_control *wbc)
  77. {
  78. return block_write_full_page(page,hpfs_get_block, wbc);
  79. }
  80. static int hpfs_readpage(struct file *file, struct page *page)
  81. {
  82. return block_read_full_page(page,hpfs_get_block);
  83. }
  84. static int hpfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
  85. {
  86. return cont_prepare_write(page,from,to,hpfs_get_block,
  87. &hpfs_i(page->mapping->host)->mmu_private);
  88. }
  89. static sector_t _hpfs_bmap(struct address_space *mapping, sector_t block)
  90. {
  91. return generic_block_bmap(mapping,block,hpfs_get_block);
  92. }
  93. const struct address_space_operations hpfs_aops = {
  94. .readpage = hpfs_readpage,
  95. .writepage = hpfs_writepage,
  96. .sync_page = block_sync_page,
  97. .prepare_write = hpfs_prepare_write,
  98. .commit_write = generic_commit_write,
  99. .bmap = _hpfs_bmap
  100. };
  101. static ssize_t hpfs_file_write(struct file *file, const char __user *buf,
  102. size_t count, loff_t *ppos)
  103. {
  104. ssize_t retval;
  105. retval = do_sync_write(file, buf, count, ppos);
  106. if (retval > 0)
  107. hpfs_i(file->f_path.dentry->d_inode)->i_dirty = 1;
  108. return retval;
  109. }
  110. const struct file_operations hpfs_file_ops =
  111. {
  112. .llseek = generic_file_llseek,
  113. .read = do_sync_read,
  114. .aio_read = generic_file_aio_read,
  115. .write = hpfs_file_write,
  116. .aio_write = generic_file_aio_write,
  117. .mmap = generic_file_mmap,
  118. .release = hpfs_file_release,
  119. .fsync = hpfs_file_fsync,
  120. .sendfile = generic_file_sendfile,
  121. };
  122. const struct inode_operations hpfs_file_iops =
  123. {
  124. .truncate = hpfs_truncate,
  125. .setattr = hpfs_notify_change,
  126. };