file.c 4.8 KB

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