tail_conversion.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 1999 Hans Reiser, see reiserfs/README for licensing and copyright
  4. * details
  5. */
  6. #include <linux/time.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/buffer_head.h>
  9. #include "reiserfs.h"
  10. /*
  11. * access to tail : when one is going to read tail it must make sure, that is
  12. * not running. direct2indirect and indirect2direct can not run concurrently
  13. */
  14. /*
  15. * Converts direct items to an unformatted node. Panics if file has no
  16. * tail. -ENOSPC if no disk space for conversion
  17. */
  18. /*
  19. * path points to first direct item of the file regardless of how many of
  20. * them are there
  21. */
  22. int direct2indirect(struct reiserfs_transaction_handle *th, struct inode *inode,
  23. struct treepath *path, struct buffer_head *unbh,
  24. loff_t tail_offset)
  25. {
  26. struct super_block *sb = inode->i_sb;
  27. struct buffer_head *up_to_date_bh;
  28. struct item_head *p_le_ih = tp_item_head(path);
  29. unsigned long total_tail = 0;
  30. /* Key to search for the last byte of the converted item. */
  31. struct cpu_key end_key;
  32. /*
  33. * new indirect item to be inserted or key
  34. * of unfm pointer to be pasted
  35. */
  36. struct item_head ind_ih;
  37. int blk_size;
  38. /* returned value for reiserfs_insert_item and clones */
  39. int retval;
  40. /* Handle on an unformatted node that will be inserted in the tree. */
  41. unp_t unfm_ptr;
  42. BUG_ON(!th->t_trans_id);
  43. REISERFS_SB(sb)->s_direct2indirect++;
  44. blk_size = sb->s_blocksize;
  45. /*
  46. * and key to search for append or insert pointer to the new
  47. * unformatted node.
  48. */
  49. copy_item_head(&ind_ih, p_le_ih);
  50. set_le_ih_k_offset(&ind_ih, tail_offset);
  51. set_le_ih_k_type(&ind_ih, TYPE_INDIRECT);
  52. /* Set the key to search for the place for new unfm pointer */
  53. make_cpu_key(&end_key, inode, tail_offset, TYPE_INDIRECT, 4);
  54. /* FIXME: we could avoid this */
  55. if (search_for_position_by_key(sb, &end_key, path) == POSITION_FOUND) {
  56. reiserfs_error(sb, "PAP-14030",
  57. "pasted or inserted byte exists in "
  58. "the tree %K. Use fsck to repair.", &end_key);
  59. pathrelse(path);
  60. return -EIO;
  61. }
  62. p_le_ih = tp_item_head(path);
  63. unfm_ptr = cpu_to_le32(unbh->b_blocknr);
  64. if (is_statdata_le_ih(p_le_ih)) {
  65. /* Insert new indirect item. */
  66. set_ih_free_space(&ind_ih, 0); /* delete at nearest future */
  67. put_ih_item_len(&ind_ih, UNFM_P_SIZE);
  68. PATH_LAST_POSITION(path)++;
  69. retval =
  70. reiserfs_insert_item(th, path, &end_key, &ind_ih, inode,
  71. (char *)&unfm_ptr);
  72. } else {
  73. /* Paste into last indirect item of an object. */
  74. retval = reiserfs_paste_into_item(th, path, &end_key, inode,
  75. (char *)&unfm_ptr,
  76. UNFM_P_SIZE);
  77. }
  78. if (retval) {
  79. return retval;
  80. }
  81. /*
  82. * note: from here there are two keys which have matching first
  83. * three key components. They only differ by the fourth one.
  84. */
  85. /* Set the key to search for the direct items of the file */
  86. make_cpu_key(&end_key, inode, max_reiserfs_offset(inode), TYPE_DIRECT,
  87. 4);
  88. /*
  89. * Move bytes from the direct items to the new unformatted node
  90. * and delete them.
  91. */
  92. while (1) {
  93. int tail_size;
  94. /*
  95. * end_key.k_offset is set so, that we will always have found
  96. * last item of the file
  97. */
  98. if (search_for_position_by_key(sb, &end_key, path) ==
  99. POSITION_FOUND)
  100. reiserfs_panic(sb, "PAP-14050",
  101. "direct item (%K) not found", &end_key);
  102. p_le_ih = tp_item_head(path);
  103. RFALSE(!is_direct_le_ih(p_le_ih),
  104. "vs-14055: direct item expected(%K), found %h",
  105. &end_key, p_le_ih);
  106. tail_size = (le_ih_k_offset(p_le_ih) & (blk_size - 1))
  107. + ih_item_len(p_le_ih) - 1;
  108. /*
  109. * we only send the unbh pointer if the buffer is not
  110. * up to date. this avoids overwriting good data from
  111. * writepage() with old data from the disk or buffer cache
  112. * Special case: unbh->b_page will be NULL if we are coming
  113. * through DIRECT_IO handler here.
  114. */
  115. if (!unbh->b_page || buffer_uptodate(unbh)
  116. || PageUptodate(unbh->b_page)) {
  117. up_to_date_bh = NULL;
  118. } else {
  119. up_to_date_bh = unbh;
  120. }
  121. retval = reiserfs_delete_item(th, path, &end_key, inode,
  122. up_to_date_bh);
  123. total_tail += retval;
  124. /* done: file does not have direct items anymore */
  125. if (tail_size == retval)
  126. break;
  127. }
  128. /*
  129. * if we've copied bytes from disk into the page, we need to zero
  130. * out the unused part of the block (it was not up to date before)
  131. */
  132. if (up_to_date_bh) {
  133. unsigned pgoff =
  134. (tail_offset + total_tail - 1) & (PAGE_SIZE - 1);
  135. char *kaddr = kmap_atomic(up_to_date_bh->b_page);
  136. memset(kaddr + pgoff, 0, blk_size - total_tail);
  137. kunmap_atomic(kaddr);
  138. }
  139. REISERFS_I(inode)->i_first_direct_byte = U32_MAX;
  140. return 0;
  141. }
  142. /* stolen from fs/buffer.c */
  143. void reiserfs_unmap_buffer(struct buffer_head *bh)
  144. {
  145. lock_buffer(bh);
  146. if (buffer_journaled(bh) || buffer_journal_dirty(bh)) {
  147. BUG();
  148. }
  149. clear_buffer_dirty(bh);
  150. /*
  151. * Remove the buffer from whatever list it belongs to. We are mostly
  152. * interested in removing it from per-sb j_dirty_buffers list, to avoid
  153. * BUG() on attempt to write not mapped buffer
  154. */
  155. if ((!list_empty(&bh->b_assoc_buffers) || bh->b_private) && bh->b_page) {
  156. struct inode *inode = bh->b_page->mapping->host;
  157. struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
  158. spin_lock(&j->j_dirty_buffers_lock);
  159. list_del_init(&bh->b_assoc_buffers);
  160. reiserfs_free_jh(bh);
  161. spin_unlock(&j->j_dirty_buffers_lock);
  162. }
  163. clear_buffer_mapped(bh);
  164. clear_buffer_req(bh);
  165. clear_buffer_new(bh);
  166. bh->b_bdev = NULL;
  167. unlock_buffer(bh);
  168. }
  169. /*
  170. * this first locks inode (neither reads nor sync are permitted),
  171. * reads tail through page cache, insert direct item. When direct item
  172. * inserted successfully inode is left locked. Return value is always
  173. * what we expect from it (number of cut bytes). But when tail remains
  174. * in the unformatted node, we set mode to SKIP_BALANCING and unlock
  175. * inode
  176. */
  177. int indirect2direct(struct reiserfs_transaction_handle *th,
  178. struct inode *inode, struct page *page,
  179. struct treepath *path, /* path to the indirect item. */
  180. const struct cpu_key *item_key, /* Key to look for
  181. * unformatted node
  182. * pointer to be cut. */
  183. loff_t n_new_file_size, /* New file size. */
  184. char *mode)
  185. {
  186. struct super_block *sb = inode->i_sb;
  187. struct item_head s_ih;
  188. unsigned long block_size = sb->s_blocksize;
  189. char *tail;
  190. int tail_len, round_tail_len;
  191. loff_t pos, pos1; /* position of first byte of the tail */
  192. struct cpu_key key;
  193. BUG_ON(!th->t_trans_id);
  194. REISERFS_SB(sb)->s_indirect2direct++;
  195. *mode = M_SKIP_BALANCING;
  196. /* store item head path points to. */
  197. copy_item_head(&s_ih, tp_item_head(path));
  198. tail_len = (n_new_file_size & (block_size - 1));
  199. if (get_inode_sd_version(inode) == STAT_DATA_V2)
  200. round_tail_len = ROUND_UP(tail_len);
  201. else
  202. round_tail_len = tail_len;
  203. pos =
  204. le_ih_k_offset(&s_ih) - 1 + (ih_item_len(&s_ih) / UNFM_P_SIZE -
  205. 1) * sb->s_blocksize;
  206. pos1 = pos;
  207. /*
  208. * we are protected by i_mutex. The tail can not disapper, not
  209. * append can be done either
  210. * we are in truncate or packing tail in file_release
  211. */
  212. tail = (char *)kmap(page); /* this can schedule */
  213. if (path_changed(&s_ih, path)) {
  214. /* re-search indirect item */
  215. if (search_for_position_by_key(sb, item_key, path)
  216. == POSITION_NOT_FOUND)
  217. reiserfs_panic(sb, "PAP-5520",
  218. "item to be converted %K does not exist",
  219. item_key);
  220. copy_item_head(&s_ih, tp_item_head(path));
  221. #ifdef CONFIG_REISERFS_CHECK
  222. pos = le_ih_k_offset(&s_ih) - 1 +
  223. (ih_item_len(&s_ih) / UNFM_P_SIZE -
  224. 1) * sb->s_blocksize;
  225. if (pos != pos1)
  226. reiserfs_panic(sb, "vs-5530", "tail position "
  227. "changed while we were reading it");
  228. #endif
  229. }
  230. /* Set direct item header to insert. */
  231. make_le_item_head(&s_ih, NULL, get_inode_item_key_version(inode),
  232. pos1 + 1, TYPE_DIRECT, round_tail_len,
  233. 0xffff /*ih_free_space */ );
  234. /*
  235. * we want a pointer to the first byte of the tail in the page.
  236. * the page was locked and this part of the page was up to date when
  237. * indirect2direct was called, so we know the bytes are still valid
  238. */
  239. tail = tail + (pos & (PAGE_SIZE - 1));
  240. PATH_LAST_POSITION(path)++;
  241. key = *item_key;
  242. set_cpu_key_k_type(&key, TYPE_DIRECT);
  243. key.key_length = 4;
  244. /* Insert tail as new direct item in the tree */
  245. if (reiserfs_insert_item(th, path, &key, &s_ih, inode,
  246. tail ? tail : NULL) < 0) {
  247. /*
  248. * No disk memory. So we can not convert last unformatted node
  249. * to the direct item. In this case we used to adjust
  250. * indirect items's ih_free_space. Now ih_free_space is not
  251. * used, it would be ideal to write zeros to corresponding
  252. * unformatted node. For now i_size is considered as guard for
  253. * going out of file size
  254. */
  255. kunmap(page);
  256. return block_size - round_tail_len;
  257. }
  258. kunmap(page);
  259. /* make sure to get the i_blocks changes from reiserfs_insert_item */
  260. reiserfs_update_sd(th, inode);
  261. /*
  262. * note: we have now the same as in above direct2indirect
  263. * conversion: there are two keys which have matching first three
  264. * key components. They only differ by the fourth one.
  265. */
  266. /*
  267. * We have inserted new direct item and must remove last
  268. * unformatted node.
  269. */
  270. *mode = M_CUT;
  271. /* we store position of first direct item in the in-core inode */
  272. /* mark_file_with_tail (inode, pos1 + 1); */
  273. REISERFS_I(inode)->i_first_direct_byte = pos1 + 1;
  274. return block_size - round_tail_len;
  275. }