file-item.c 23 KB

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