file-item.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/bio.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/highmem.h>
  21. #include "ctree.h"
  22. #include "disk-io.h"
  23. #include "transaction.h"
  24. #include "print-tree.h"
  25. #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
  26. sizeof(struct btrfs_item) * 2) / \
  27. BTRFS_CRC32_SIZE) - 1))
  28. int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
  29. struct btrfs_root *root,
  30. u64 objectid, u64 pos,
  31. u64 offset, u64 disk_num_bytes,
  32. u64 num_bytes)
  33. {
  34. int ret = 0;
  35. struct btrfs_file_extent_item *item;
  36. struct btrfs_key file_key;
  37. struct btrfs_path *path;
  38. struct extent_buffer *leaf;
  39. path = btrfs_alloc_path();
  40. BUG_ON(!path);
  41. file_key.objectid = objectid;
  42. file_key.offset = pos;
  43. btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
  44. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  45. sizeof(*item));
  46. if (ret < 0)
  47. goto out;
  48. BUG_ON(ret);
  49. leaf = path->nodes[0];
  50. item = btrfs_item_ptr(leaf, path->slots[0],
  51. struct btrfs_file_extent_item);
  52. btrfs_set_file_extent_disk_bytenr(leaf, item, offset);
  53. btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
  54. btrfs_set_file_extent_offset(leaf, item, 0);
  55. btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
  56. btrfs_set_file_extent_generation(leaf, item, trans->transid);
  57. btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
  58. btrfs_mark_buffer_dirty(leaf);
  59. out:
  60. btrfs_free_path(path);
  61. return ret;
  62. }
  63. struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
  64. struct btrfs_root *root,
  65. struct btrfs_path *path,
  66. u64 objectid, u64 offset,
  67. int cow)
  68. {
  69. int ret;
  70. struct btrfs_key file_key;
  71. struct btrfs_key found_key;
  72. struct btrfs_csum_item *item;
  73. struct extent_buffer *leaf;
  74. u64 csum_offset = 0;
  75. int csums_in_item;
  76. file_key.objectid = objectid;
  77. file_key.offset = offset;
  78. btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
  79. ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
  80. if (ret < 0)
  81. goto fail;
  82. leaf = path->nodes[0];
  83. if (ret > 0) {
  84. ret = 1;
  85. if (path->slots[0] == 0)
  86. goto fail;
  87. path->slots[0]--;
  88. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  89. if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
  90. found_key.objectid != objectid) {
  91. goto fail;
  92. }
  93. csum_offset = (offset - found_key.offset) >>
  94. root->fs_info->sb->s_blocksize_bits;
  95. csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
  96. csums_in_item /= BTRFS_CRC32_SIZE;
  97. if (csum_offset >= csums_in_item) {
  98. ret = -EFBIG;
  99. goto fail;
  100. }
  101. }
  102. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  103. item = (struct btrfs_csum_item *)((unsigned char *)item +
  104. csum_offset * BTRFS_CRC32_SIZE);
  105. return item;
  106. fail:
  107. if (ret > 0)
  108. ret = -ENOENT;
  109. return ERR_PTR(ret);
  110. }
  111. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  112. struct btrfs_root *root,
  113. struct btrfs_path *path, u64 objectid,
  114. u64 offset, int mod)
  115. {
  116. int ret;
  117. struct btrfs_key file_key;
  118. int ins_len = mod < 0 ? -1 : 0;
  119. int cow = mod != 0;
  120. file_key.objectid = objectid;
  121. file_key.offset = offset;
  122. btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
  123. ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
  124. return ret;
  125. }
  126. int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
  127. struct btrfs_root *root, struct inode *inode,
  128. struct bio *bio)
  129. {
  130. u64 objectid = inode->i_ino;
  131. u64 offset;
  132. int ret;
  133. struct btrfs_key file_key;
  134. struct btrfs_key found_key;
  135. u64 next_offset;
  136. int found_next;
  137. struct btrfs_path *path;
  138. struct btrfs_csum_item *item;
  139. struct btrfs_csum_item *item_end;
  140. struct extent_buffer *leaf = NULL;
  141. u64 csum_offset;
  142. u32 csum_result;
  143. u32 nritems;
  144. u32 ins_size;
  145. int bio_index = 0;
  146. struct bio_vec *bvec = bio->bi_io_vec;
  147. char *data;
  148. char *eb_map;
  149. char *eb_token;
  150. unsigned long map_len;
  151. unsigned long map_start;
  152. path = btrfs_alloc_path();
  153. BUG_ON(!path);
  154. again:
  155. next_offset = (u64)-1;
  156. found_next = 0;
  157. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  158. file_key.objectid = objectid;
  159. file_key.offset = offset;
  160. btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
  161. item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
  162. if (!IS_ERR(item)) {
  163. leaf = path->nodes[0];
  164. goto found;
  165. }
  166. ret = PTR_ERR(item);
  167. if (ret == -EFBIG) {
  168. u32 item_size;
  169. /* we found one, but it isn't big enough yet */
  170. leaf = path->nodes[0];
  171. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  172. if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
  173. /* already at max size, make a new one */
  174. goto insert;
  175. }
  176. } else {
  177. int slot = path->slots[0] + 1;
  178. /* we didn't find a csum item, insert one */
  179. nritems = btrfs_header_nritems(path->nodes[0]);
  180. if (path->slots[0] >= nritems - 1) {
  181. ret = btrfs_next_leaf(root, path);
  182. if (ret == 1)
  183. found_next = 1;
  184. if (ret != 0)
  185. goto insert;
  186. slot = 0;
  187. }
  188. btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
  189. if (found_key.objectid != objectid ||
  190. found_key.type != BTRFS_CSUM_ITEM_KEY) {
  191. found_next = 1;
  192. goto insert;
  193. }
  194. next_offset = found_key.offset;
  195. found_next = 1;
  196. goto insert;
  197. }
  198. /*
  199. * at this point, we know the tree has an item, but it isn't big
  200. * enough yet to put our csum in. Grow it
  201. */
  202. btrfs_release_path(root, path);
  203. ret = btrfs_search_slot(trans, root, &file_key, path,
  204. BTRFS_CRC32_SIZE, 1);
  205. if (ret < 0)
  206. goto fail;
  207. if (ret == 0) {
  208. BUG();
  209. }
  210. if (path->slots[0] == 0) {
  211. goto insert;
  212. }
  213. path->slots[0]--;
  214. leaf = path->nodes[0];
  215. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  216. csum_offset = (offset - found_key.offset) >>
  217. root->fs_info->sb->s_blocksize_bits;
  218. if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
  219. found_key.objectid != objectid ||
  220. csum_offset >= MAX_CSUM_ITEMS(root)) {
  221. goto insert;
  222. }
  223. if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
  224. BTRFS_CRC32_SIZE) {
  225. u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
  226. diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
  227. if (diff != BTRFS_CRC32_SIZE)
  228. goto insert;
  229. ret = btrfs_extend_item(trans, root, path, diff);
  230. BUG_ON(ret);
  231. goto csum;
  232. }
  233. insert:
  234. btrfs_release_path(root, path);
  235. csum_offset = 0;
  236. if (found_next) {
  237. u64 tmp = min((u64)i_size_read(inode), next_offset);
  238. tmp -= offset & ~((u64)root->sectorsize -1);
  239. tmp >>= root->fs_info->sb->s_blocksize_bits;
  240. tmp = max((u64)1, tmp);
  241. tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
  242. ins_size = BTRFS_CRC32_SIZE * tmp;
  243. } else {
  244. ins_size = BTRFS_CRC32_SIZE;
  245. }
  246. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  247. ins_size);
  248. if (ret < 0)
  249. goto fail;
  250. if (ret != 0) {
  251. WARN_ON(1);
  252. goto fail;
  253. }
  254. csum:
  255. leaf = path->nodes[0];
  256. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  257. ret = 0;
  258. item = (struct btrfs_csum_item *)((unsigned char *)item +
  259. csum_offset * BTRFS_CRC32_SIZE);
  260. found:
  261. item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  262. item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
  263. btrfs_item_size_nr(leaf, path->slots[0]));
  264. eb_token = NULL;
  265. next_bvec:
  266. data = kmap_atomic(bvec->bv_page, KM_USER0);
  267. csum_result = ~(u32)0;
  268. csum_result = btrfs_csum_data(root, data + bvec->bv_offset,
  269. csum_result, bvec->bv_len);
  270. kunmap_atomic(data, KM_USER0);
  271. btrfs_csum_final(csum_result, (char *)&csum_result);
  272. if (csum_result == 0) {
  273. printk("csum result is 0 for inode %lu offset %Lu\n", inode->i_ino, offset);
  274. }
  275. if (!eb_token ||
  276. (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) {
  277. int err;
  278. if (eb_token)
  279. unmap_extent_buffer(leaf, eb_token, KM_USER1);
  280. eb_token = NULL;
  281. err = map_private_extent_buffer(leaf, (unsigned long)item,
  282. BTRFS_CRC32_SIZE,
  283. &eb_token, &eb_map,
  284. &map_start, &map_len, KM_USER1);
  285. if (err)
  286. eb_token = NULL;
  287. }
  288. if (eb_token) {
  289. memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
  290. &csum_result, BTRFS_CRC32_SIZE);
  291. } else {
  292. write_extent_buffer(leaf, &csum_result, (unsigned long)item,
  293. BTRFS_CRC32_SIZE);
  294. }
  295. bio_index++;
  296. bvec++;
  297. if (bio_index < bio->bi_vcnt) {
  298. item = (struct btrfs_csum_item *)((char *)item +
  299. BTRFS_CRC32_SIZE);
  300. if (item < item_end && offset + PAGE_CACHE_SIZE ==
  301. page_offset(bvec->bv_page)) {
  302. offset = page_offset(bvec->bv_page);
  303. goto next_bvec;
  304. }
  305. }
  306. if (eb_token) {
  307. unmap_extent_buffer(leaf, eb_token, KM_USER1);
  308. eb_token = NULL;
  309. }
  310. btrfs_mark_buffer_dirty(path->nodes[0]);
  311. if (bio_index < bio->bi_vcnt) {
  312. btrfs_release_path(root, path);
  313. goto again;
  314. }
  315. fail:
  316. btrfs_free_path(path);
  317. return ret;
  318. }
  319. int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
  320. struct btrfs_root *root, struct btrfs_path *path,
  321. u64 isize)
  322. {
  323. struct btrfs_key key;
  324. struct extent_buffer *leaf = path->nodes[0];
  325. int slot = path->slots[0];
  326. int ret;
  327. u32 new_item_size;
  328. u64 new_item_span;
  329. u64 blocks;
  330. btrfs_item_key_to_cpu(leaf, &key, slot);
  331. if (isize <= key.offset)
  332. return 0;
  333. new_item_span = isize - key.offset;
  334. blocks = (new_item_span + root->sectorsize - 1) >>
  335. root->fs_info->sb->s_blocksize_bits;
  336. new_item_size = blocks * BTRFS_CRC32_SIZE;
  337. if (new_item_size >= btrfs_item_size_nr(leaf, slot))
  338. return 0;
  339. ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
  340. BUG_ON(ret);
  341. return ret;
  342. }