file.c 4.8 KB

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