tail_conversion.c 9.2 KB

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