tail_conversion.c 9.1 KB

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