file-item.c 23 KB

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