file-item.c 19 KB

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