file.c 4.8 KB

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