tail_conversion.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 n_blk_size, n_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. n_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_warning(sb, "PAP-14030: direct2indirect: "
  43. "pasted or inserted byte exists in the tree %K. "
  44. "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. n_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. n_retval = reiserfs_paste_into_item(th, path, &end_key, inode,
  61. (char *)&unfm_ptr,
  62. UNFM_P_SIZE);
  63. }
  64. if (n_retval) {
  65. return n_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,
  81. "PAP-14050: direct2indirect: "
  82. "direct item (%K) not found", &end_key);
  83. p_le_ih = PATH_PITEM_HEAD(path);
  84. RFALSE(!is_direct_le_ih(p_le_ih),
  85. "vs-14055: direct item expected(%K), found %h",
  86. &end_key, p_le_ih);
  87. tail_size = (le_ih_k_offset(p_le_ih) & (n_blk_size - 1))
  88. + ih_item_len(p_le_ih) - 1;
  89. /* we only send the unbh pointer if the buffer is not up to date.
  90. ** this avoids overwriting good data from writepage() with old data
  91. ** from the disk or buffer cache
  92. ** Special case: unbh->b_page will be NULL if we are coming through
  93. ** DIRECT_IO handler here.
  94. */
  95. if (!unbh->b_page || buffer_uptodate(unbh)
  96. || PageUptodate(unbh->b_page)) {
  97. up_to_date_bh = NULL;
  98. } else {
  99. up_to_date_bh = unbh;
  100. }
  101. n_retval = reiserfs_delete_item(th, path, &end_key, inode,
  102. up_to_date_bh);
  103. total_tail += n_retval;
  104. if (tail_size == n_retval)
  105. // done: file does not have direct items anymore
  106. break;
  107. }
  108. /* if we've copied bytes from disk into the page, we need to zero
  109. ** out the unused part of the block (it was not up to date before)
  110. */
  111. if (up_to_date_bh) {
  112. unsigned pgoff =
  113. (tail_offset + total_tail - 1) & (PAGE_CACHE_SIZE - 1);
  114. char *kaddr = kmap_atomic(up_to_date_bh->b_page, KM_USER0);
  115. memset(kaddr + pgoff, 0, n_blk_size - total_tail);
  116. kunmap_atomic(kaddr, KM_USER0);
  117. }
  118. REISERFS_I(inode)->i_first_direct_byte = U32_MAX;
  119. return 0;
  120. }
  121. /* stolen from fs/buffer.c */
  122. void reiserfs_unmap_buffer(struct buffer_head *bh)
  123. {
  124. lock_buffer(bh);
  125. if (buffer_journaled(bh) || buffer_journal_dirty(bh)) {
  126. BUG();
  127. }
  128. clear_buffer_dirty(bh);
  129. /* Remove the buffer from whatever list it belongs to. We are mostly
  130. interested in removing it from per-sb j_dirty_buffers list, to avoid
  131. BUG() on attempt to write not mapped buffer */
  132. if ((!list_empty(&bh->b_assoc_buffers) || bh->b_private) && bh->b_page) {
  133. struct inode *inode = bh->b_page->mapping->host;
  134. struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
  135. spin_lock(&j->j_dirty_buffers_lock);
  136. list_del_init(&bh->b_assoc_buffers);
  137. reiserfs_free_jh(bh);
  138. spin_unlock(&j->j_dirty_buffers_lock);
  139. }
  140. clear_buffer_mapped(bh);
  141. clear_buffer_req(bh);
  142. clear_buffer_new(bh);
  143. bh->b_bdev = NULL;
  144. unlock_buffer(bh);
  145. }
  146. /* this first locks inode (neither reads nor sync are permitted),
  147. reads tail through page cache, insert direct item. When direct item
  148. inserted successfully inode is left locked. Return value is always
  149. what we expect from it (number of cut bytes). But when tail remains
  150. in the unformatted node, we set mode to SKIP_BALANCING and unlock
  151. inode */
  152. int indirect2direct(struct reiserfs_transaction_handle *th, struct inode *p_s_inode, struct page *page, struct treepath *p_s_path, /* path to the indirect item. */
  153. const struct cpu_key *p_s_item_key, /* Key to look for unformatted node pointer to be cut. */
  154. loff_t n_new_file_size, /* New file size. */
  155. char *p_c_mode)
  156. {
  157. struct super_block *p_s_sb = p_s_inode->i_sb;
  158. struct item_head s_ih;
  159. unsigned long n_block_size = p_s_sb->s_blocksize;
  160. char *tail;
  161. int tail_len, round_tail_len;
  162. loff_t pos, pos1; /* position of first byte of the tail */
  163. struct cpu_key key;
  164. BUG_ON(!th->t_trans_id);
  165. REISERFS_SB(p_s_sb)->s_indirect2direct++;
  166. *p_c_mode = M_SKIP_BALANCING;
  167. /* store item head path points to. */
  168. copy_item_head(&s_ih, PATH_PITEM_HEAD(p_s_path));
  169. tail_len = (n_new_file_size & (n_block_size - 1));
  170. if (get_inode_sd_version(p_s_inode) == STAT_DATA_V2)
  171. round_tail_len = ROUND_UP(tail_len);
  172. else
  173. round_tail_len = tail_len;
  174. pos =
  175. le_ih_k_offset(&s_ih) - 1 + (ih_item_len(&s_ih) / UNFM_P_SIZE -
  176. 1) * p_s_sb->s_blocksize;
  177. pos1 = pos;
  178. // we are protected by i_mutex. The tail can not disapper, not
  179. // append can be done either
  180. // we are in truncate or packing tail in file_release
  181. tail = (char *)kmap(page); /* this can schedule */
  182. if (path_changed(&s_ih, p_s_path)) {
  183. /* re-search indirect item */
  184. if (search_for_position_by_key(p_s_sb, p_s_item_key, p_s_path)
  185. == POSITION_NOT_FOUND)
  186. reiserfs_panic(p_s_sb,
  187. "PAP-5520: indirect2direct: "
  188. "item to be converted %K does not exist",
  189. p_s_item_key);
  190. copy_item_head(&s_ih, PATH_PITEM_HEAD(p_s_path));
  191. #ifdef CONFIG_REISERFS_CHECK
  192. pos = le_ih_k_offset(&s_ih) - 1 +
  193. (ih_item_len(&s_ih) / UNFM_P_SIZE -
  194. 1) * p_s_sb->s_blocksize;
  195. if (pos != pos1)
  196. reiserfs_panic(p_s_sb, "vs-5530: indirect2direct: "
  197. "tail position changed while we were reading it");
  198. #endif
  199. }
  200. /* Set direct item header to insert. */
  201. make_le_item_head(&s_ih, NULL, get_inode_item_key_version(p_s_inode),
  202. pos1 + 1, TYPE_DIRECT, round_tail_len,
  203. 0xffff /*ih_free_space */ );
  204. /* we want a pointer to the first byte of the tail in the page.
  205. ** the page was locked and this part of the page was up to date when
  206. ** indirect2direct was called, so we know the bytes are still valid
  207. */
  208. tail = tail + (pos & (PAGE_CACHE_SIZE - 1));
  209. PATH_LAST_POSITION(p_s_path)++;
  210. key = *p_s_item_key;
  211. set_cpu_key_k_type(&key, TYPE_DIRECT);
  212. key.key_length = 4;
  213. /* Insert tail as new direct item in the tree */
  214. if (reiserfs_insert_item(th, p_s_path, &key, &s_ih, p_s_inode,
  215. tail ? tail : NULL) < 0) {
  216. /* No disk memory. So we can not convert last unformatted node
  217. to the direct item. In this case we used to adjust
  218. indirect items's ih_free_space. Now ih_free_space is not
  219. used, it would be ideal to write zeros to corresponding
  220. unformatted node. For now i_size is considered as guard for
  221. going out of file size */
  222. kunmap(page);
  223. return n_block_size - round_tail_len;
  224. }
  225. kunmap(page);
  226. /* make sure to get the i_blocks changes from reiserfs_insert_item */
  227. reiserfs_update_sd(th, p_s_inode);
  228. // note: we have now the same as in above direct2indirect
  229. // conversion: there are two keys which have matching first three
  230. // key components. They only differ by the fouhth one.
  231. /* We have inserted new direct item and must remove last
  232. unformatted node. */
  233. *p_c_mode = M_CUT;
  234. /* we store position of first direct item in the in-core inode */
  235. //mark_file_with_tail (p_s_inode, pos1 + 1);
  236. REISERFS_I(p_s_inode)->i_first_direct_byte = pos1 + 1;
  237. return n_block_size - round_tail_len;
  238. }