file-item.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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/slab.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/highmem.h>
  22. #include "ctree.h"
  23. #include "disk-io.h"
  24. #include "transaction.h"
  25. #include "print-tree.h"
  26. #define MAX_CSUM_ITEMS(r, size) ((((BTRFS_LEAF_DATA_SIZE(r) - \
  27. sizeof(struct btrfs_item) * 2) / \
  28. size) - 1))
  29. #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \
  30. sizeof(struct btrfs_ordered_sum)) / \
  31. sizeof(struct btrfs_sector_sum) * \
  32. (r)->sectorsize - (r)->sectorsize)
  33. int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
  34. struct btrfs_root *root,
  35. u64 objectid, u64 pos,
  36. u64 disk_offset, u64 disk_num_bytes,
  37. u64 num_bytes, u64 offset, u64 ram_bytes,
  38. u8 compression, u8 encryption, u16 other_encoding)
  39. {
  40. int ret = 0;
  41. struct btrfs_file_extent_item *item;
  42. struct btrfs_key file_key;
  43. struct btrfs_path *path;
  44. struct extent_buffer *leaf;
  45. path = btrfs_alloc_path();
  46. if (!path)
  47. return -ENOMEM;
  48. file_key.objectid = objectid;
  49. file_key.offset = pos;
  50. btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
  51. path->leave_spinning = 1;
  52. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  53. sizeof(*item));
  54. if (ret < 0)
  55. goto out;
  56. BUG_ON(ret); /* Can't happen */
  57. leaf = path->nodes[0];
  58. item = btrfs_item_ptr(leaf, path->slots[0],
  59. struct btrfs_file_extent_item);
  60. btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
  61. btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
  62. btrfs_set_file_extent_offset(leaf, item, offset);
  63. btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
  64. btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
  65. btrfs_set_file_extent_generation(leaf, item, trans->transid);
  66. btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
  67. btrfs_set_file_extent_compression(leaf, item, compression);
  68. btrfs_set_file_extent_encryption(leaf, item, encryption);
  69. btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
  70. btrfs_mark_buffer_dirty(leaf);
  71. out:
  72. btrfs_free_path(path);
  73. return ret;
  74. }
  75. struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
  76. struct btrfs_root *root,
  77. struct btrfs_path *path,
  78. u64 bytenr, int cow)
  79. {
  80. int ret;
  81. struct btrfs_key file_key;
  82. struct btrfs_key found_key;
  83. struct btrfs_csum_item *item;
  84. struct extent_buffer *leaf;
  85. u64 csum_offset = 0;
  86. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  87. int csums_in_item;
  88. file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  89. file_key.offset = bytenr;
  90. btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
  91. ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
  92. if (ret < 0)
  93. goto fail;
  94. leaf = path->nodes[0];
  95. if (ret > 0) {
  96. ret = 1;
  97. if (path->slots[0] == 0)
  98. goto fail;
  99. path->slots[0]--;
  100. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  101. if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY)
  102. goto fail;
  103. csum_offset = (bytenr - found_key.offset) >>
  104. root->fs_info->sb->s_blocksize_bits;
  105. csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
  106. csums_in_item /= csum_size;
  107. if (csum_offset >= csums_in_item) {
  108. ret = -EFBIG;
  109. goto fail;
  110. }
  111. }
  112. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  113. item = (struct btrfs_csum_item *)((unsigned char *)item +
  114. csum_offset * csum_size);
  115. return item;
  116. fail:
  117. if (ret > 0)
  118. ret = -ENOENT;
  119. return ERR_PTR(ret);
  120. }
  121. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  122. struct btrfs_root *root,
  123. struct btrfs_path *path, u64 objectid,
  124. u64 offset, int mod)
  125. {
  126. int ret;
  127. struct btrfs_key file_key;
  128. int ins_len = mod < 0 ? -1 : 0;
  129. int cow = mod != 0;
  130. file_key.objectid = objectid;
  131. file_key.offset = offset;
  132. btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
  133. ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
  134. return ret;
  135. }
  136. static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
  137. struct inode *inode, struct bio *bio,
  138. u64 logical_offset, u32 *dst, int dio)
  139. {
  140. u32 sum;
  141. struct bio_vec *bvec = bio->bi_io_vec;
  142. int bio_index = 0;
  143. u64 offset = 0;
  144. u64 item_start_offset = 0;
  145. u64 item_last_offset = 0;
  146. u64 disk_bytenr;
  147. u32 diff;
  148. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  149. int ret;
  150. struct btrfs_path *path;
  151. struct btrfs_csum_item *item = NULL;
  152. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  153. path = btrfs_alloc_path();
  154. if (!path)
  155. return -ENOMEM;
  156. if (bio->bi_size > PAGE_CACHE_SIZE * 8)
  157. path->reada = 2;
  158. WARN_ON(bio->bi_vcnt <= 0);
  159. /*
  160. * the free space stuff is only read when it hasn't been
  161. * updated in the current transaction. So, we can safely
  162. * read from the commit root and sidestep a nasty deadlock
  163. * between reading the free space cache and updating the csum tree.
  164. */
  165. if (btrfs_is_free_space_inode(root, inode)) {
  166. path->search_commit_root = 1;
  167. path->skip_locking = 1;
  168. }
  169. disk_bytenr = (u64)bio->bi_sector << 9;
  170. if (dio)
  171. offset = logical_offset;
  172. while (bio_index < bio->bi_vcnt) {
  173. if (!dio)
  174. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  175. ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum);
  176. if (ret == 0)
  177. goto found;
  178. if (!item || disk_bytenr < item_start_offset ||
  179. disk_bytenr >= item_last_offset) {
  180. struct btrfs_key found_key;
  181. u32 item_size;
  182. if (item)
  183. btrfs_release_path(path);
  184. item = btrfs_lookup_csum(NULL, root->fs_info->csum_root,
  185. path, disk_bytenr, 0);
  186. if (IS_ERR(item)) {
  187. ret = PTR_ERR(item);
  188. if (ret == -ENOENT || ret == -EFBIG)
  189. ret = 0;
  190. sum = 0;
  191. if (BTRFS_I(inode)->root->root_key.objectid ==
  192. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  193. set_extent_bits(io_tree, offset,
  194. offset + bvec->bv_len - 1,
  195. EXTENT_NODATASUM, GFP_NOFS);
  196. } else {
  197. printk(KERN_INFO "btrfs no csum found "
  198. "for inode %llu start %llu\n",
  199. (unsigned long long)
  200. btrfs_ino(inode),
  201. (unsigned long long)offset);
  202. }
  203. item = NULL;
  204. btrfs_release_path(path);
  205. goto found;
  206. }
  207. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  208. path->slots[0]);
  209. item_start_offset = found_key.offset;
  210. item_size = btrfs_item_size_nr(path->nodes[0],
  211. path->slots[0]);
  212. item_last_offset = item_start_offset +
  213. (item_size / csum_size) *
  214. root->sectorsize;
  215. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  216. struct btrfs_csum_item);
  217. }
  218. /*
  219. * this byte range must be able to fit inside
  220. * a single leaf so it will also fit inside a u32
  221. */
  222. diff = disk_bytenr - item_start_offset;
  223. diff = diff / root->sectorsize;
  224. diff = diff * csum_size;
  225. read_extent_buffer(path->nodes[0], &sum,
  226. ((unsigned long)item) + diff,
  227. csum_size);
  228. found:
  229. if (dst)
  230. *dst++ = sum;
  231. else
  232. set_state_private(io_tree, offset, sum);
  233. disk_bytenr += bvec->bv_len;
  234. offset += bvec->bv_len;
  235. bio_index++;
  236. bvec++;
  237. }
  238. btrfs_free_path(path);
  239. return 0;
  240. }
  241. int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
  242. struct bio *bio, u32 *dst)
  243. {
  244. return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0);
  245. }
  246. int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
  247. struct bio *bio, u64 offset, u32 *dst)
  248. {
  249. return __btrfs_lookup_bio_sums(root, inode, bio, offset, dst, 1);
  250. }
  251. int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
  252. struct list_head *list, int search_commit)
  253. {
  254. struct btrfs_key key;
  255. struct btrfs_path *path;
  256. struct extent_buffer *leaf;
  257. struct btrfs_ordered_sum *sums;
  258. struct btrfs_sector_sum *sector_sum;
  259. struct btrfs_csum_item *item;
  260. LIST_HEAD(tmplist);
  261. unsigned long offset;
  262. int ret;
  263. size_t size;
  264. u64 csum_end;
  265. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  266. path = btrfs_alloc_path();
  267. if (!path)
  268. return -ENOMEM;
  269. if (search_commit) {
  270. path->skip_locking = 1;
  271. path->reada = 2;
  272. path->search_commit_root = 1;
  273. }
  274. key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  275. key.offset = start;
  276. key.type = BTRFS_EXTENT_CSUM_KEY;
  277. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  278. if (ret < 0)
  279. goto fail;
  280. if (ret > 0 && path->slots[0] > 0) {
  281. leaf = path->nodes[0];
  282. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  283. if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
  284. key.type == BTRFS_EXTENT_CSUM_KEY) {
  285. offset = (start - key.offset) >>
  286. root->fs_info->sb->s_blocksize_bits;
  287. if (offset * csum_size <
  288. btrfs_item_size_nr(leaf, path->slots[0] - 1))
  289. path->slots[0]--;
  290. }
  291. }
  292. while (start <= end) {
  293. leaf = path->nodes[0];
  294. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  295. ret = btrfs_next_leaf(root, path);
  296. if (ret < 0)
  297. goto fail;
  298. if (ret > 0)
  299. break;
  300. leaf = path->nodes[0];
  301. }
  302. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  303. if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  304. key.type != BTRFS_EXTENT_CSUM_KEY)
  305. break;
  306. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  307. if (key.offset > end)
  308. break;
  309. if (key.offset > start)
  310. start = key.offset;
  311. size = btrfs_item_size_nr(leaf, path->slots[0]);
  312. csum_end = key.offset + (size / csum_size) * root->sectorsize;
  313. if (csum_end <= start) {
  314. path->slots[0]++;
  315. continue;
  316. }
  317. csum_end = min(csum_end, end + 1);
  318. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  319. struct btrfs_csum_item);
  320. while (start < csum_end) {
  321. size = min_t(size_t, csum_end - start,
  322. MAX_ORDERED_SUM_BYTES(root));
  323. sums = kzalloc(btrfs_ordered_sum_size(root, size),
  324. GFP_NOFS);
  325. if (!sums) {
  326. ret = -ENOMEM;
  327. goto fail;
  328. }
  329. sector_sum = sums->sums;
  330. sums->bytenr = start;
  331. sums->len = size;
  332. offset = (start - key.offset) >>
  333. root->fs_info->sb->s_blocksize_bits;
  334. offset *= csum_size;
  335. while (size > 0) {
  336. read_extent_buffer(path->nodes[0],
  337. &sector_sum->sum,
  338. ((unsigned long)item) +
  339. offset, csum_size);
  340. sector_sum->bytenr = start;
  341. size -= root->sectorsize;
  342. start += root->sectorsize;
  343. offset += csum_size;
  344. sector_sum++;
  345. }
  346. list_add_tail(&sums->list, &tmplist);
  347. }
  348. path->slots[0]++;
  349. }
  350. ret = 0;
  351. fail:
  352. while (ret < 0 && !list_empty(&tmplist)) {
  353. sums = list_entry(&tmplist, struct btrfs_ordered_sum, list);
  354. list_del(&sums->list);
  355. kfree(sums);
  356. }
  357. list_splice_tail(&tmplist, list);
  358. btrfs_free_path(path);
  359. return ret;
  360. }
  361. int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
  362. struct bio *bio, u64 file_start, int contig)
  363. {
  364. struct btrfs_ordered_sum *sums;
  365. struct btrfs_sector_sum *sector_sum;
  366. struct btrfs_ordered_extent *ordered;
  367. char *data;
  368. struct bio_vec *bvec = bio->bi_io_vec;
  369. int bio_index = 0;
  370. unsigned long total_bytes = 0;
  371. unsigned long this_sum_bytes = 0;
  372. u64 offset;
  373. u64 disk_bytenr;
  374. WARN_ON(bio->bi_vcnt <= 0);
  375. sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
  376. if (!sums)
  377. return -ENOMEM;
  378. sector_sum = sums->sums;
  379. disk_bytenr = (u64)bio->bi_sector << 9;
  380. sums->len = bio->bi_size;
  381. INIT_LIST_HEAD(&sums->list);
  382. if (contig)
  383. offset = file_start;
  384. else
  385. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  386. ordered = btrfs_lookup_ordered_extent(inode, offset);
  387. BUG_ON(!ordered); /* Logic error */
  388. sums->bytenr = ordered->start;
  389. while (bio_index < bio->bi_vcnt) {
  390. if (!contig)
  391. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  392. if (!contig && (offset >= ordered->file_offset + ordered->len ||
  393. offset < ordered->file_offset)) {
  394. unsigned long bytes_left;
  395. sums->len = this_sum_bytes;
  396. this_sum_bytes = 0;
  397. btrfs_add_ordered_sum(inode, ordered, sums);
  398. btrfs_put_ordered_extent(ordered);
  399. bytes_left = bio->bi_size - total_bytes;
  400. sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
  401. GFP_NOFS);
  402. BUG_ON(!sums); /* -ENOMEM */
  403. sector_sum = sums->sums;
  404. sums->len = bytes_left;
  405. ordered = btrfs_lookup_ordered_extent(inode, offset);
  406. BUG_ON(!ordered); /* Logic error */
  407. sums->bytenr = ordered->start;
  408. }
  409. data = kmap_atomic(bvec->bv_page, KM_USER0);
  410. sector_sum->sum = ~(u32)0;
  411. sector_sum->sum = btrfs_csum_data(root,
  412. data + bvec->bv_offset,
  413. sector_sum->sum,
  414. bvec->bv_len);
  415. kunmap_atomic(data, KM_USER0);
  416. btrfs_csum_final(sector_sum->sum,
  417. (char *)&sector_sum->sum);
  418. sector_sum->bytenr = disk_bytenr;
  419. sector_sum++;
  420. bio_index++;
  421. total_bytes += bvec->bv_len;
  422. this_sum_bytes += bvec->bv_len;
  423. disk_bytenr += bvec->bv_len;
  424. offset += bvec->bv_len;
  425. bvec++;
  426. }
  427. this_sum_bytes = 0;
  428. btrfs_add_ordered_sum(inode, ordered, sums);
  429. btrfs_put_ordered_extent(ordered);
  430. return 0;
  431. }
  432. /*
  433. * helper function for csum removal, this expects the
  434. * key to describe the csum pointed to by the path, and it expects
  435. * the csum to overlap the range [bytenr, len]
  436. *
  437. * The csum should not be entirely contained in the range and the
  438. * range should not be entirely contained in the csum.
  439. *
  440. * This calls btrfs_truncate_item with the correct args based on the
  441. * overlap, and fixes up the key as required.
  442. */
  443. static noinline void truncate_one_csum(struct btrfs_trans_handle *trans,
  444. struct btrfs_root *root,
  445. struct btrfs_path *path,
  446. struct btrfs_key *key,
  447. u64 bytenr, u64 len)
  448. {
  449. struct extent_buffer *leaf;
  450. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  451. u64 csum_end;
  452. u64 end_byte = bytenr + len;
  453. u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits;
  454. leaf = path->nodes[0];
  455. csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
  456. csum_end <<= root->fs_info->sb->s_blocksize_bits;
  457. csum_end += key->offset;
  458. if (key->offset < bytenr && csum_end <= end_byte) {
  459. /*
  460. * [ bytenr - len ]
  461. * [ ]
  462. * [csum ]
  463. * A simple truncate off the end of the item
  464. */
  465. u32 new_size = (bytenr - key->offset) >> blocksize_bits;
  466. new_size *= csum_size;
  467. btrfs_truncate_item(trans, root, path, new_size, 1);
  468. } else if (key->offset >= bytenr && csum_end > end_byte &&
  469. end_byte > key->offset) {
  470. /*
  471. * [ bytenr - len ]
  472. * [ ]
  473. * [csum ]
  474. * we need to truncate from the beginning of the csum
  475. */
  476. u32 new_size = (csum_end - end_byte) >> blocksize_bits;
  477. new_size *= csum_size;
  478. btrfs_truncate_item(trans, root, path, new_size, 0);
  479. key->offset = end_byte;
  480. btrfs_set_item_key_safe(trans, root, path, key);
  481. } else {
  482. BUG();
  483. }
  484. }
  485. /*
  486. * deletes the csum items from the csum tree for a given
  487. * range of bytes.
  488. */
  489. int btrfs_del_csums(struct btrfs_trans_handle *trans,
  490. struct btrfs_root *root, u64 bytenr, u64 len)
  491. {
  492. struct btrfs_path *path;
  493. struct btrfs_key key;
  494. u64 end_byte = bytenr + len;
  495. u64 csum_end;
  496. struct extent_buffer *leaf;
  497. int ret;
  498. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  499. int blocksize_bits = root->fs_info->sb->s_blocksize_bits;
  500. root = root->fs_info->csum_root;
  501. path = btrfs_alloc_path();
  502. if (!path)
  503. return -ENOMEM;
  504. while (1) {
  505. key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  506. key.offset = end_byte - 1;
  507. key.type = BTRFS_EXTENT_CSUM_KEY;
  508. path->leave_spinning = 1;
  509. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  510. if (ret > 0) {
  511. if (path->slots[0] == 0)
  512. break;
  513. path->slots[0]--;
  514. } else if (ret < 0) {
  515. break;
  516. }
  517. leaf = path->nodes[0];
  518. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  519. if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  520. key.type != BTRFS_EXTENT_CSUM_KEY) {
  521. break;
  522. }
  523. if (key.offset >= end_byte)
  524. break;
  525. csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
  526. csum_end <<= blocksize_bits;
  527. csum_end += key.offset;
  528. /* this csum ends before we start, we're done */
  529. if (csum_end <= bytenr)
  530. break;
  531. /* delete the entire item, it is inside our range */
  532. if (key.offset >= bytenr && csum_end <= end_byte) {
  533. ret = btrfs_del_item(trans, root, path);
  534. if (ret)
  535. goto out;
  536. if (key.offset == bytenr)
  537. break;
  538. } else if (key.offset < bytenr && csum_end > end_byte) {
  539. unsigned long offset;
  540. unsigned long shift_len;
  541. unsigned long item_offset;
  542. /*
  543. * [ bytenr - len ]
  544. * [csum ]
  545. *
  546. * Our bytes are in the middle of the csum,
  547. * we need to split this item and insert a new one.
  548. *
  549. * But we can't drop the path because the
  550. * csum could change, get removed, extended etc.
  551. *
  552. * The trick here is the max size of a csum item leaves
  553. * enough room in the tree block for a single
  554. * item header. So, we split the item in place,
  555. * adding a new header pointing to the existing
  556. * bytes. Then we loop around again and we have
  557. * a nicely formed csum item that we can neatly
  558. * truncate.
  559. */
  560. offset = (bytenr - key.offset) >> blocksize_bits;
  561. offset *= csum_size;
  562. shift_len = (len >> blocksize_bits) * csum_size;
  563. item_offset = btrfs_item_ptr_offset(leaf,
  564. path->slots[0]);
  565. memset_extent_buffer(leaf, 0, item_offset + offset,
  566. shift_len);
  567. key.offset = bytenr;
  568. /*
  569. * btrfs_split_item returns -EAGAIN when the
  570. * item changed size or key
  571. */
  572. ret = btrfs_split_item(trans, root, path, &key, offset);
  573. if (ret && ret != -EAGAIN) {
  574. btrfs_abort_transaction(trans, root, ret);
  575. goto out;
  576. }
  577. key.offset = end_byte - 1;
  578. } else {
  579. truncate_one_csum(trans, root, path, &key, bytenr, len);
  580. if (key.offset < bytenr)
  581. break;
  582. }
  583. btrfs_release_path(path);
  584. }
  585. ret = 0;
  586. out:
  587. btrfs_free_path(path);
  588. return ret;
  589. }
  590. int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
  591. struct btrfs_root *root,
  592. struct btrfs_ordered_sum *sums)
  593. {
  594. u64 bytenr;
  595. int ret;
  596. struct btrfs_key file_key;
  597. struct btrfs_key found_key;
  598. u64 next_offset;
  599. u64 total_bytes = 0;
  600. int found_next;
  601. struct btrfs_path *path;
  602. struct btrfs_csum_item *item;
  603. struct btrfs_csum_item *item_end;
  604. struct extent_buffer *leaf = NULL;
  605. u64 csum_offset;
  606. struct btrfs_sector_sum *sector_sum;
  607. u32 nritems;
  608. u32 ins_size;
  609. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  610. path = btrfs_alloc_path();
  611. if (!path)
  612. return -ENOMEM;
  613. sector_sum = sums->sums;
  614. again:
  615. next_offset = (u64)-1;
  616. found_next = 0;
  617. file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  618. file_key.offset = sector_sum->bytenr;
  619. bytenr = sector_sum->bytenr;
  620. btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
  621. item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1);
  622. if (!IS_ERR(item)) {
  623. leaf = path->nodes[0];
  624. ret = 0;
  625. goto found;
  626. }
  627. ret = PTR_ERR(item);
  628. if (ret != -EFBIG && ret != -ENOENT)
  629. goto fail_unlock;
  630. if (ret == -EFBIG) {
  631. u32 item_size;
  632. /* we found one, but it isn't big enough yet */
  633. leaf = path->nodes[0];
  634. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  635. if ((item_size / csum_size) >=
  636. MAX_CSUM_ITEMS(root, csum_size)) {
  637. /* already at max size, make a new one */
  638. goto insert;
  639. }
  640. } else {
  641. int slot = path->slots[0] + 1;
  642. /* we didn't find a csum item, insert one */
  643. nritems = btrfs_header_nritems(path->nodes[0]);
  644. if (path->slots[0] >= nritems - 1) {
  645. ret = btrfs_next_leaf(root, path);
  646. if (ret == 1)
  647. found_next = 1;
  648. if (ret != 0)
  649. goto insert;
  650. slot = 0;
  651. }
  652. btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
  653. if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  654. found_key.type != BTRFS_EXTENT_CSUM_KEY) {
  655. found_next = 1;
  656. goto insert;
  657. }
  658. next_offset = found_key.offset;
  659. found_next = 1;
  660. goto insert;
  661. }
  662. /*
  663. * at this point, we know the tree has an item, but it isn't big
  664. * enough yet to put our csum in. Grow it
  665. */
  666. btrfs_release_path(path);
  667. ret = btrfs_search_slot(trans, root, &file_key, path,
  668. csum_size, 1);
  669. if (ret < 0)
  670. goto fail_unlock;
  671. if (ret > 0) {
  672. if (path->slots[0] == 0)
  673. goto insert;
  674. path->slots[0]--;
  675. }
  676. leaf = path->nodes[0];
  677. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  678. csum_offset = (bytenr - found_key.offset) >>
  679. root->fs_info->sb->s_blocksize_bits;
  680. if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY ||
  681. found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  682. csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
  683. goto insert;
  684. }
  685. if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
  686. csum_size) {
  687. u32 diff = (csum_offset + 1) * csum_size;
  688. /*
  689. * is the item big enough already? we dropped our lock
  690. * before and need to recheck
  691. */
  692. if (diff < btrfs_item_size_nr(leaf, path->slots[0]))
  693. goto csum;
  694. diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
  695. if (diff != csum_size)
  696. goto insert;
  697. btrfs_extend_item(trans, root, path, diff);
  698. goto csum;
  699. }
  700. insert:
  701. btrfs_release_path(path);
  702. csum_offset = 0;
  703. if (found_next) {
  704. u64 tmp = total_bytes + root->sectorsize;
  705. u64 next_sector = sector_sum->bytenr;
  706. struct btrfs_sector_sum *next = sector_sum + 1;
  707. while (tmp < sums->len) {
  708. if (next_sector + root->sectorsize != next->bytenr)
  709. break;
  710. tmp += root->sectorsize;
  711. next_sector = next->bytenr;
  712. next++;
  713. }
  714. tmp = min(tmp, next_offset - file_key.offset);
  715. tmp >>= root->fs_info->sb->s_blocksize_bits;
  716. tmp = max((u64)1, tmp);
  717. tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
  718. ins_size = csum_size * tmp;
  719. } else {
  720. ins_size = csum_size;
  721. }
  722. path->leave_spinning = 1;
  723. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  724. ins_size);
  725. path->leave_spinning = 0;
  726. if (ret < 0)
  727. goto fail_unlock;
  728. if (ret != 0) {
  729. WARN_ON(1);
  730. goto fail_unlock;
  731. }
  732. csum:
  733. leaf = path->nodes[0];
  734. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  735. ret = 0;
  736. item = (struct btrfs_csum_item *)((unsigned char *)item +
  737. csum_offset * csum_size);
  738. found:
  739. item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  740. item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
  741. btrfs_item_size_nr(leaf, path->slots[0]));
  742. next_sector:
  743. write_extent_buffer(leaf, &sector_sum->sum, (unsigned long)item, csum_size);
  744. total_bytes += root->sectorsize;
  745. sector_sum++;
  746. if (total_bytes < sums->len) {
  747. item = (struct btrfs_csum_item *)((char *)item +
  748. csum_size);
  749. if (item < item_end && bytenr + PAGE_CACHE_SIZE ==
  750. sector_sum->bytenr) {
  751. bytenr = sector_sum->bytenr;
  752. goto next_sector;
  753. }
  754. }
  755. btrfs_mark_buffer_dirty(path->nodes[0]);
  756. if (total_bytes < sums->len) {
  757. btrfs_release_path(path);
  758. cond_resched();
  759. goto again;
  760. }
  761. out:
  762. btrfs_free_path(path);
  763. return ret;
  764. fail_unlock:
  765. goto out;
  766. }