file.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. phys = bi->i_sblock + block;
  65. if (!create) {
  66. if (phys <= bi->i_eblock) {
  67. dprintf("c=%d, b=%08lx, phys=%09lx (granted)\n",
  68. create, (unsigned long)block, phys);
  69. map_bh(bh_result, sb, phys);
  70. }
  71. return 0;
  72. }
  73. /*
  74. * If the file is not empty and the requested block is within the
  75. * range of blocks allocated for this file, we can grant it.
  76. */
  77. if (bi->i_sblock && (phys <= bi->i_eblock)) {
  78. dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
  79. create, (unsigned long)block, phys);
  80. map_bh(bh_result, sb, phys);
  81. return 0;
  82. }
  83. /* The file will be extended, so let's see if there is enough space. */
  84. if (phys >= info->si_blocks)
  85. return -ENOSPC;
  86. /* The rest has to be protected against itself. */
  87. mutex_lock(&info->bfs_lock);
  88. /*
  89. * If the last data block for this file is the last allocated
  90. * block, we can extend the file trivially, without moving it
  91. * anywhere.
  92. */
  93. if (bi->i_eblock == info->si_lf_eblk) {
  94. dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
  95. create, (unsigned long)block, phys);
  96. map_bh(bh_result, sb, phys);
  97. info->si_freeb -= phys - bi->i_eblock;
  98. info->si_lf_eblk = bi->i_eblock = phys;
  99. mark_inode_dirty(inode);
  100. err = 0;
  101. goto out;
  102. }
  103. /* Ok, we have to move this entire file to the next free block. */
  104. phys = info->si_lf_eblk + 1;
  105. if (phys + block >= info->si_blocks) {
  106. err = -ENOSPC;
  107. goto out;
  108. }
  109. if (bi->i_sblock) {
  110. err = bfs_move_blocks(inode->i_sb, bi->i_sblock,
  111. bi->i_eblock, phys);
  112. if (err) {
  113. dprintf("failed to move ino=%08lx -> fs corruption\n",
  114. inode->i_ino);
  115. goto out;
  116. }
  117. } else
  118. err = 0;
  119. dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n",
  120. create, (unsigned long)block, phys);
  121. bi->i_sblock = phys;
  122. phys += block;
  123. info->si_lf_eblk = bi->i_eblock = phys;
  124. /*
  125. * This assumes nothing can write the inode back while we are here
  126. * and thus update inode->i_blocks! (XXX)
  127. */
  128. info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks;
  129. mark_inode_dirty(inode);
  130. map_bh(bh_result, sb, phys);
  131. out:
  132. mutex_unlock(&info->bfs_lock);
  133. return err;
  134. }
  135. static int bfs_writepage(struct page *page, struct writeback_control *wbc)
  136. {
  137. return block_write_full_page(page, bfs_get_block, wbc);
  138. }
  139. static int bfs_readpage(struct file *file, struct page *page)
  140. {
  141. return block_read_full_page(page, bfs_get_block);
  142. }
  143. static int bfs_write_begin(struct file *file, struct address_space *mapping,
  144. loff_t pos, unsigned len, unsigned flags,
  145. struct page **pagep, void **fsdata)
  146. {
  147. int ret;
  148. ret = block_write_begin(mapping, pos, len, flags, pagep,
  149. bfs_get_block);
  150. if (unlikely(ret)) {
  151. loff_t isize = mapping->host->i_size;
  152. if (pos + len > isize)
  153. vmtruncate(mapping->host, isize);
  154. }
  155. return ret;
  156. }
  157. static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
  158. {
  159. return generic_block_bmap(mapping, block, bfs_get_block);
  160. }
  161. const struct address_space_operations bfs_aops = {
  162. .readpage = bfs_readpage,
  163. .writepage = bfs_writepage,
  164. .write_begin = bfs_write_begin,
  165. .write_end = generic_write_end,
  166. .bmap = bfs_bmap,
  167. };
  168. const struct inode_operations bfs_file_inops;