file.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * fs/bfs/file.c
  3. * BFS file operations.
  4. * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/buffer_head.h>
  8. #include <linux/smp_lock.h>
  9. #include "bfs.h"
  10. #undef DEBUG
  11. #ifdef DEBUG
  12. #define dprintf(x...) printf(x)
  13. #else
  14. #define dprintf(x...)
  15. #endif
  16. struct file_operations bfs_file_operations = {
  17. .llseek = generic_file_llseek,
  18. .read = generic_file_read,
  19. .write = generic_file_write,
  20. .mmap = generic_file_mmap,
  21. .sendfile = generic_file_sendfile,
  22. };
  23. static int bfs_move_block(unsigned long from, unsigned long to, struct super_block *sb)
  24. {
  25. struct buffer_head *bh, *new;
  26. bh = sb_bread(sb, from);
  27. if (!bh)
  28. return -EIO;
  29. new = sb_getblk(sb, to);
  30. memcpy(new->b_data, bh->b_data, bh->b_size);
  31. mark_buffer_dirty(new);
  32. bforget(bh);
  33. brelse(new);
  34. return 0;
  35. }
  36. static int bfs_move_blocks(struct super_block *sb, unsigned long start, unsigned long end,
  37. unsigned long where)
  38. {
  39. unsigned long i;
  40. dprintf("%08lx-%08lx->%08lx\n", start, end, where);
  41. for (i = start; i <= end; i++)
  42. if(bfs_move_block(i, where + i, sb)) {
  43. dprintf("failed to move block %08lx -> %08lx\n", i, where + i);
  44. return -EIO;
  45. }
  46. return 0;
  47. }
  48. static int bfs_get_block(struct inode * inode, sector_t block,
  49. struct buffer_head * bh_result, int create)
  50. {
  51. long phys;
  52. int err;
  53. struct super_block *sb = inode->i_sb;
  54. struct bfs_sb_info *info = BFS_SB(sb);
  55. struct bfs_inode_info *bi = BFS_I(inode);
  56. struct buffer_head *sbh = info->si_sbh;
  57. if (block < 0 || block > info->si_blocks)
  58. return -EIO;
  59. phys = bi->i_sblock + block;
  60. if (!create) {
  61. if (phys <= bi->i_eblock) {
  62. dprintf("c=%d, b=%08lx, phys=%08lx (granted)\n", create, block, phys);
  63. map_bh(bh_result, sb, phys);
  64. }
  65. return 0;
  66. }
  67. /* if the file is not empty and the requested block is within the range
  68. of blocks allocated for this file, we can grant it */
  69. if (inode->i_size && phys <= bi->i_eblock) {
  70. dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
  71. create, block, phys);
  72. map_bh(bh_result, sb, phys);
  73. return 0;
  74. }
  75. /* the rest has to be protected against itself */
  76. lock_kernel();
  77. /* if the last data block for this file is the last allocated block, we can
  78. extend the file trivially, without moving it anywhere */
  79. if (bi->i_eblock == info->si_lf_eblk) {
  80. dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
  81. create, block, phys);
  82. map_bh(bh_result, sb, phys);
  83. info->si_freeb -= phys - bi->i_eblock;
  84. info->si_lf_eblk = bi->i_eblock = phys;
  85. mark_inode_dirty(inode);
  86. mark_buffer_dirty(sbh);
  87. err = 0;
  88. goto out;
  89. }
  90. /* Ok, we have to move this entire file to the next free block */
  91. phys = info->si_lf_eblk + 1;
  92. if (bi->i_sblock) { /* if data starts on block 0 then there is no data */
  93. err = bfs_move_blocks(inode->i_sb, bi->i_sblock,
  94. bi->i_eblock, phys);
  95. if (err) {
  96. dprintf("failed to move ino=%08lx -> fs corruption\n", inode->i_ino);
  97. goto out;
  98. }
  99. } else
  100. err = 0;
  101. dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n", create, block, phys);
  102. bi->i_sblock = phys;
  103. phys += block;
  104. info->si_lf_eblk = bi->i_eblock = phys;
  105. /* this assumes nothing can write the inode back while we are here
  106. * and thus update inode->i_blocks! (XXX)*/
  107. info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks;
  108. mark_inode_dirty(inode);
  109. mark_buffer_dirty(sbh);
  110. map_bh(bh_result, sb, phys);
  111. out:
  112. unlock_kernel();
  113. return err;
  114. }
  115. static int bfs_writepage(struct page *page, struct writeback_control *wbc)
  116. {
  117. return block_write_full_page(page, bfs_get_block, wbc);
  118. }
  119. static int bfs_readpage(struct file *file, struct page *page)
  120. {
  121. return block_read_full_page(page, bfs_get_block);
  122. }
  123. static int bfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
  124. {
  125. return block_prepare_write(page, from, to, bfs_get_block);
  126. }
  127. static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
  128. {
  129. return generic_block_bmap(mapping, block, bfs_get_block);
  130. }
  131. struct address_space_operations bfs_aops = {
  132. .readpage = bfs_readpage,
  133. .writepage = bfs_writepage,
  134. .sync_page = block_sync_page,
  135. .prepare_write = bfs_prepare_write,
  136. .commit_write = generic_commit_write,
  137. .bmap = bfs_bmap,
  138. };
  139. struct inode_operations bfs_file_inops;