file.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. const 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,
  37. unsigned long end, 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. unsigned 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 > 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=%09lx (granted)\n",
  63. create, (unsigned long)block, phys);
  64. map_bh(bh_result, sb, phys);
  65. }
  66. return 0;
  67. }
  68. /* if the file is not empty and the requested block is within the range
  69. of blocks allocated for this file, we can grant it */
  70. if (inode->i_size && phys <= bi->i_eblock) {
  71. dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
  72. create, (unsigned long)block, phys);
  73. map_bh(bh_result, sb, phys);
  74. return 0;
  75. }
  76. /* the rest has to be protected against itself */
  77. lock_kernel();
  78. /* if the last data block for this file is the last allocated
  79. block, we can extend the file trivially, without moving it
  80. anywhere */
  81. if (bi->i_eblock == info->si_lf_eblk) {
  82. dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
  83. create, (unsigned long)block, phys);
  84. map_bh(bh_result, sb, phys);
  85. info->si_freeb -= phys - bi->i_eblock;
  86. info->si_lf_eblk = bi->i_eblock = phys;
  87. mark_inode_dirty(inode);
  88. mark_buffer_dirty(sbh);
  89. err = 0;
  90. goto out;
  91. }
  92. /* Ok, we have to move this entire file to the next free block */
  93. phys = info->si_lf_eblk + 1;
  94. if (bi->i_sblock) { /* if data starts on block 0 then there is no data */
  95. err = bfs_move_blocks(inode->i_sb, bi->i_sblock,
  96. bi->i_eblock, phys);
  97. if (err) {
  98. dprintf("failed to move ino=%08lx -> fs corruption\n", inode->i_ino);
  99. goto out;
  100. }
  101. } else
  102. err = 0;
  103. dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n",
  104. create, (unsigned long)block, phys);
  105. bi->i_sblock = phys;
  106. phys += block;
  107. info->si_lf_eblk = bi->i_eblock = phys;
  108. /* this assumes nothing can write the inode back while we are here
  109. * and thus update inode->i_blocks! (XXX)*/
  110. info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks;
  111. mark_inode_dirty(inode);
  112. mark_buffer_dirty(sbh);
  113. map_bh(bh_result, sb, phys);
  114. out:
  115. unlock_kernel();
  116. return err;
  117. }
  118. static int bfs_writepage(struct page *page, struct writeback_control *wbc)
  119. {
  120. return block_write_full_page(page, bfs_get_block, wbc);
  121. }
  122. static int bfs_readpage(struct file *file, struct page *page)
  123. {
  124. return block_read_full_page(page, bfs_get_block);
  125. }
  126. static int bfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
  127. {
  128. return block_prepare_write(page, from, to, bfs_get_block);
  129. }
  130. static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
  131. {
  132. return generic_block_bmap(mapping, block, bfs_get_block);
  133. }
  134. struct address_space_operations bfs_aops = {
  135. .readpage = bfs_readpage,
  136. .writepage = bfs_writepage,
  137. .sync_page = block_sync_page,
  138. .prepare_write = bfs_prepare_write,
  139. .commit_write = generic_commit_write,
  140. .bmap = bfs_bmap,
  141. };
  142. struct inode_operations bfs_file_inops;