file.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/bfs/file.c
  4. * BFS file operations.
  5. * Copyright (C) 1999-2018 Tigran Aivazian <aivazian.tigran@gmail.com>
  6. *
  7. * Make the file block allocation algorithm understand the size
  8. * of the underlying block device.
  9. * Copyright (C) 2007 Dmitri Vorobiev <dmitri.vorobiev@gmail.com>
  10. *
  11. */
  12. #include <linux/fs.h>
  13. #include <linux/mpage.h>
  14. #include <linux/buffer_head.h>
  15. #include "bfs.h"
  16. #undef DEBUG
  17. #ifdef DEBUG
  18. #define dprintf(x...) printf(x)
  19. #else
  20. #define dprintf(x...)
  21. #endif
  22. const struct file_operations bfs_file_operations = {
  23. .llseek = generic_file_llseek,
  24. .read_iter = generic_file_read_iter,
  25. .write_iter = generic_file_write_iter,
  26. .mmap_prepare = generic_file_mmap_prepare,
  27. .splice_read = filemap_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_writepages(struct address_space *mapping,
  136. struct writeback_control *wbc)
  137. {
  138. return mpage_writepages(mapping, wbc, bfs_get_block);
  139. }
  140. static int bfs_read_folio(struct file *file, struct folio *folio)
  141. {
  142. return block_read_full_folio(folio, bfs_get_block);
  143. }
  144. static void bfs_write_failed(struct address_space *mapping, loff_t to)
  145. {
  146. struct inode *inode = mapping->host;
  147. if (to > inode->i_size)
  148. truncate_pagecache(inode, inode->i_size);
  149. }
  150. static int bfs_write_begin(const struct kiocb *iocb,
  151. struct address_space *mapping,
  152. loff_t pos, unsigned len,
  153. struct folio **foliop, void **fsdata)
  154. {
  155. int ret;
  156. ret = block_write_begin(mapping, pos, len, foliop, bfs_get_block);
  157. if (unlikely(ret))
  158. bfs_write_failed(mapping, pos + len);
  159. return ret;
  160. }
  161. static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
  162. {
  163. return generic_block_bmap(mapping, block, bfs_get_block);
  164. }
  165. const struct address_space_operations bfs_aops = {
  166. .dirty_folio = block_dirty_folio,
  167. .invalidate_folio = block_invalidate_folio,
  168. .read_folio = bfs_read_folio,
  169. .writepages = bfs_writepages,
  170. .write_begin = bfs_write_begin,
  171. .write_end = generic_write_end,
  172. .migrate_folio = buffer_migrate_folio,
  173. .bmap = bfs_bmap,
  174. };
  175. const struct inode_operations bfs_file_inops;