file.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * fs/bfs/file.c
  3. * BFS file operations.
  4. * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
  5. *
  6. * Make the file block allocation algorithm understand the size
  7. * of the underlying block device.
  8. * Copyright (C) 2007 Dmitri Vorobiev <dmitri.vorobiev@gmail.com>
  9. *
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/buffer_head.h>
  13. #include "bfs.h"
  14. #undef DEBUG
  15. #ifdef DEBUG
  16. #define dprintf(x...) printf(x)
  17. #else
  18. #define dprintf(x...)
  19. #endif
  20. const struct file_operations bfs_file_operations = {
  21. .llseek = generic_file_llseek,
  22. .read = do_sync_read,
  23. .aio_read = generic_file_aio_read,
  24. .write = do_sync_write,
  25. .aio_write = generic_file_aio_write,
  26. .mmap = generic_file_mmap,
  27. .splice_read = generic_file_splice_read,
  28. };
  29. static int bfs_move_block(unsigned long from, unsigned long to,
  30. struct super_block *sb)
  31. {
  32. struct buffer_head *bh, *new;
  33. bh = sb_bread(sb, from);
  34. if (!bh)
  35. return -EIO;
  36. new = sb_getblk(sb, to);
  37. memcpy(new->b_data, bh->b_data, bh->b_size);
  38. mark_buffer_dirty(new);
  39. bforget(bh);
  40. brelse(new);
  41. return 0;
  42. }
  43. static int bfs_move_blocks(struct super_block *sb, unsigned long start,
  44. unsigned long end, unsigned long where)
  45. {
  46. unsigned long i;
  47. dprintf("%08lx-%08lx->%08lx\n", start, end, where);
  48. for (i = start; i <= end; i++)
  49. if(bfs_move_block(i, where + i, sb)) {
  50. dprintf("failed to move block %08lx -> %08lx\n", i,
  51. where + i);
  52. return -EIO;
  53. }
  54. return 0;
  55. }
  56. static int bfs_get_block(struct inode *inode, sector_t block,
  57. struct buffer_head *bh_result, int create)
  58. {
  59. unsigned long phys;
  60. int err;
  61. struct super_block *sb = inode->i_sb;
  62. struct bfs_sb_info *info = BFS_SB(sb);
  63. struct bfs_inode_info *bi = BFS_I(inode);
  64. struct buffer_head *sbh = info->si_sbh;
  65. phys = bi->i_sblock + block;
  66. if (!create) {
  67. if (phys <= bi->i_eblock) {
  68. dprintf("c=%d, b=%08lx, phys=%09lx (granted)\n",
  69. create, (unsigned long)block, phys);
  70. map_bh(bh_result, sb, phys);
  71. }
  72. return 0;
  73. }
  74. /*
  75. * If the file is not empty and the requested block is within the
  76. * range of blocks allocated for this file, we can grant it.
  77. */
  78. if (bi->i_sblock && (phys <= bi->i_eblock)) {
  79. dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
  80. create, (unsigned long)block, phys);
  81. map_bh(bh_result, sb, phys);
  82. return 0;
  83. }
  84. /* The file will be extended, so let's see if there is enough space. */
  85. if (phys >= info->si_blocks)
  86. return -ENOSPC;
  87. /* The rest has to be protected against itself. */
  88. mutex_lock(&info->bfs_lock);
  89. /*
  90. * If the last data block for this file is the last allocated
  91. * block, we can extend the file trivially, without moving it
  92. * anywhere.
  93. */
  94. if (bi->i_eblock == info->si_lf_eblk) {
  95. dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
  96. create, (unsigned long)block, phys);
  97. map_bh(bh_result, sb, phys);
  98. info->si_freeb -= phys - bi->i_eblock;
  99. info->si_lf_eblk = bi->i_eblock = phys;
  100. mark_inode_dirty(inode);
  101. mark_buffer_dirty(sbh);
  102. err = 0;
  103. goto out;
  104. }
  105. /* Ok, we have to move this entire file to the next free block. */
  106. phys = info->si_lf_eblk + 1;
  107. if (phys + block >= info->si_blocks) {
  108. err = -ENOSPC;
  109. goto out;
  110. }
  111. if (bi->i_sblock) {
  112. err = bfs_move_blocks(inode->i_sb, bi->i_sblock,
  113. bi->i_eblock, phys);
  114. if (err) {
  115. dprintf("failed to move ino=%08lx -> fs corruption\n",
  116. inode->i_ino);
  117. goto out;
  118. }
  119. } else
  120. err = 0;
  121. dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n",
  122. create, (unsigned long)block, phys);
  123. bi->i_sblock = phys;
  124. phys += block;
  125. info->si_lf_eblk = bi->i_eblock = phys;
  126. /*
  127. * This assumes nothing can write the inode back while we are here
  128. * and thus update inode->i_blocks! (XXX)
  129. */
  130. info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks;
  131. mark_inode_dirty(inode);
  132. mark_buffer_dirty(sbh);
  133. map_bh(bh_result, sb, phys);
  134. out:
  135. mutex_unlock(&info->bfs_lock);
  136. return err;
  137. }
  138. static int bfs_writepage(struct page *page, struct writeback_control *wbc)
  139. {
  140. return block_write_full_page(page, bfs_get_block, wbc);
  141. }
  142. static int bfs_readpage(struct file *file, struct page *page)
  143. {
  144. return block_read_full_page(page, bfs_get_block);
  145. }
  146. static int bfs_write_begin(struct file *file, struct address_space *mapping,
  147. loff_t pos, unsigned len, unsigned flags,
  148. struct page **pagep, void **fsdata)
  149. {
  150. *pagep = NULL;
  151. return block_write_begin(file, mapping, pos, len, flags,
  152. pagep, fsdata, bfs_get_block);
  153. }
  154. static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
  155. {
  156. return generic_block_bmap(mapping, block, bfs_get_block);
  157. }
  158. const struct address_space_operations bfs_aops = {
  159. .readpage = bfs_readpage,
  160. .writepage = bfs_writepage,
  161. .sync_page = block_sync_page,
  162. .write_begin = bfs_write_begin,
  163. .write_end = generic_write_end,
  164. .bmap = bfs_bmap,
  165. };
  166. const struct inode_operations bfs_file_inops;