file-item.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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(struct btrfs_sector_sum) * \
  34. (r)->sectorsize - (r)->sectorsize)
  35. int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
  36. struct btrfs_root *root,
  37. u64 objectid, u64 pos,
  38. u64 disk_offset, u64 disk_num_bytes,
  39. u64 num_bytes, u64 offset, u64 ram_bytes,
  40. u8 compression, u8 encryption, u16 other_encoding)
  41. {
  42. int ret = 0;
  43. struct btrfs_file_extent_item *item;
  44. struct btrfs_key file_key;
  45. struct btrfs_path *path;
  46. struct extent_buffer *leaf;
  47. path = btrfs_alloc_path();
  48. if (!path)
  49. return -ENOMEM;
  50. file_key.objectid = objectid;
  51. file_key.offset = pos;
  52. btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
  53. path->leave_spinning = 1;
  54. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  55. sizeof(*item));
  56. if (ret < 0)
  57. goto out;
  58. BUG_ON(ret); /* Can't happen */
  59. leaf = path->nodes[0];
  60. item = btrfs_item_ptr(leaf, path->slots[0],
  61. struct btrfs_file_extent_item);
  62. btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
  63. btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
  64. btrfs_set_file_extent_offset(leaf, item, offset);
  65. btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
  66. btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
  67. btrfs_set_file_extent_generation(leaf, item, trans->transid);
  68. btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
  69. btrfs_set_file_extent_compression(leaf, item, compression);
  70. btrfs_set_file_extent_encryption(leaf, item, encryption);
  71. btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
  72. btrfs_mark_buffer_dirty(leaf);
  73. out:
  74. btrfs_free_path(path);
  75. return ret;
  76. }
  77. struct btrfs_csum_item *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. }
  113. }
  114. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  115. item = (struct btrfs_csum_item *)((unsigned char *)item +
  116. csum_offset * csum_size);
  117. return item;
  118. fail:
  119. if (ret > 0)
  120. ret = -ENOENT;
  121. return ERR_PTR(ret);
  122. }
  123. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  124. struct btrfs_root *root,
  125. struct btrfs_path *path, u64 objectid,
  126. u64 offset, int mod)
  127. {
  128. int ret;
  129. struct btrfs_key file_key;
  130. int ins_len = mod < 0 ? -1 : 0;
  131. int cow = mod != 0;
  132. file_key.objectid = objectid;
  133. file_key.offset = offset;
  134. btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
  135. ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
  136. return ret;
  137. }
  138. static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
  139. struct inode *inode, struct bio *bio,
  140. u64 logical_offset, u32 *dst, int dio)
  141. {
  142. u32 sum;
  143. struct bio_vec *bvec = bio->bi_io_vec;
  144. int bio_index = 0;
  145. u64 offset = 0;
  146. u64 item_start_offset = 0;
  147. u64 item_last_offset = 0;
  148. u64 disk_bytenr;
  149. u32 diff;
  150. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  151. int ret;
  152. struct btrfs_path *path;
  153. struct btrfs_csum_item *item = NULL;
  154. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  155. path = btrfs_alloc_path();
  156. if (!path)
  157. return -ENOMEM;
  158. if (bio->bi_size > PAGE_CACHE_SIZE * 8)
  159. path->reada = 2;
  160. WARN_ON(bio->bi_vcnt <= 0);
  161. /*
  162. * the free space stuff is only read when it hasn't been
  163. * updated in the current transaction. So, we can safely
  164. * read from the commit root and sidestep a nasty deadlock
  165. * between reading the free space cache and updating the csum tree.
  166. */
  167. if (btrfs_is_free_space_inode(inode)) {
  168. path->search_commit_root = 1;
  169. path->skip_locking = 1;
  170. }
  171. disk_bytenr = (u64)bio->bi_sector << 9;
  172. if (dio)
  173. offset = logical_offset;
  174. while (bio_index < bio->bi_vcnt) {
  175. if (!dio)
  176. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  177. ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum);
  178. if (ret == 0)
  179. goto found;
  180. if (!item || disk_bytenr < item_start_offset ||
  181. disk_bytenr >= item_last_offset) {
  182. struct btrfs_key found_key;
  183. u32 item_size;
  184. if (item)
  185. btrfs_release_path(path);
  186. item = btrfs_lookup_csum(NULL, root->fs_info->csum_root,
  187. path, disk_bytenr, 0);
  188. if (IS_ERR(item)) {
  189. ret = PTR_ERR(item);
  190. if (ret == -ENOENT || ret == -EFBIG)
  191. ret = 0;
  192. sum = 0;
  193. if (BTRFS_I(inode)->root->root_key.objectid ==
  194. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  195. set_extent_bits(io_tree, offset,
  196. offset + bvec->bv_len - 1,
  197. EXTENT_NODATASUM, GFP_NOFS);
  198. } else {
  199. printk(KERN_INFO "btrfs no csum found "
  200. "for inode %llu start %llu\n",
  201. (unsigned long long)
  202. btrfs_ino(inode),
  203. (unsigned long long)offset);
  204. }
  205. item = NULL;
  206. btrfs_release_path(path);
  207. goto found;
  208. }
  209. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  210. path->slots[0]);
  211. item_start_offset = found_key.offset;
  212. item_size = btrfs_item_size_nr(path->nodes[0],
  213. path->slots[0]);
  214. item_last_offset = item_start_offset +
  215. (item_size / csum_size) *
  216. root->sectorsize;
  217. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  218. struct btrfs_csum_item);
  219. }
  220. /*
  221. * this byte range must be able to fit inside
  222. * a single leaf so it will also fit inside a u32
  223. */
  224. diff = disk_bytenr - item_start_offset;
  225. diff = diff / root->sectorsize;
  226. diff = diff * csum_size;
  227. read_extent_buffer(path->nodes[0], &sum,
  228. ((unsigned long)item) + diff,
  229. csum_size);
  230. found:
  231. if (dst)
  232. *dst++ = sum;
  233. else
  234. set_state_private(io_tree, offset, sum);
  235. disk_bytenr += bvec->bv_len;
  236. offset += bvec->bv_len;
  237. bio_index++;
  238. bvec++;
  239. }
  240. btrfs_free_path(path);
  241. return 0;
  242. }
  243. int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
  244. struct bio *bio, u32 *dst)
  245. {
  246. return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0);
  247. }
  248. int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
  249. struct bio *bio, u64 offset)
  250. {
  251. return __btrfs_lookup_bio_sums(root, inode, bio, offset, NULL, 1);
  252. }
  253. int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
  254. struct list_head *list, int search_commit)
  255. {
  256. struct btrfs_key key;
  257. struct btrfs_path *path;
  258. struct extent_buffer *leaf;
  259. struct btrfs_ordered_sum *sums;
  260. struct btrfs_sector_sum *sector_sum;
  261. struct btrfs_csum_item *item;
  262. LIST_HEAD(tmplist);
  263. unsigned long offset;
  264. int ret;
  265. size_t size;
  266. u64 csum_end;
  267. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  268. path = btrfs_alloc_path();
  269. if (!path)
  270. return -ENOMEM;
  271. if (search_commit) {
  272. path->skip_locking = 1;
  273. path->reada = 2;
  274. path->search_commit_root = 1;
  275. }
  276. key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  277. key.offset = start;
  278. key.type = BTRFS_EXTENT_CSUM_KEY;
  279. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  280. if (ret < 0)
  281. goto fail;
  282. if (ret > 0 && path->slots[0] > 0) {
  283. leaf = path->nodes[0];
  284. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  285. if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
  286. key.type == BTRFS_EXTENT_CSUM_KEY) {
  287. offset = (start - key.offset) >>
  288. root->fs_info->sb->s_blocksize_bits;
  289. if (offset * csum_size <
  290. btrfs_item_size_nr(leaf, path->slots[0] - 1))
  291. path->slots[0]--;
  292. }
  293. }
  294. while (start <= end) {
  295. leaf = path->nodes[0];
  296. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  297. ret = btrfs_next_leaf(root, path);
  298. if (ret < 0)
  299. goto fail;
  300. if (ret > 0)
  301. break;
  302. leaf = path->nodes[0];
  303. }
  304. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  305. if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  306. key.type != BTRFS_EXTENT_CSUM_KEY)
  307. break;
  308. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  309. if (key.offset > end)
  310. break;
  311. if (key.offset > start)
  312. start = key.offset;
  313. size = btrfs_item_size_nr(leaf, path->slots[0]);
  314. csum_end = key.offset + (size / csum_size) * root->sectorsize;
  315. if (csum_end <= start) {
  316. path->slots[0]++;
  317. continue;
  318. }
  319. csum_end = min(csum_end, end + 1);
  320. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  321. struct btrfs_csum_item);
  322. while (start < csum_end) {
  323. size = min_t(size_t, csum_end - start,
  324. MAX_ORDERED_SUM_BYTES(root));
  325. sums = kzalloc(btrfs_ordered_sum_size(root, size),
  326. GFP_NOFS);
  327. if (!sums) {
  328. ret = -ENOMEM;
  329. goto fail;
  330. }
  331. sector_sum = sums->sums;
  332. sums->bytenr = start;
  333. sums->len = size;
  334. offset = (start - key.offset) >>
  335. root->fs_info->sb->s_blocksize_bits;
  336. offset *= csum_size;
  337. while (size > 0) {
  338. read_extent_buffer(path->nodes[0],
  339. &sector_sum->sum,
  340. ((unsigned long)item) +
  341. offset, csum_size);
  342. sector_sum->bytenr = start;
  343. size -= root->sectorsize;
  344. start += root->sectorsize;
  345. offset += csum_size;
  346. sector_sum++;
  347. }
  348. list_add_tail(&sums->list, &tmplist);
  349. }
  350. path->slots[0]++;
  351. }
  352. ret = 0;
  353. fail:
  354. while (ret < 0 && !list_empty(&tmplist)) {
  355. sums = list_entry(&tmplist, struct btrfs_ordered_sum, list);
  356. list_del(&sums->list);
  357. kfree(sums);
  358. }
  359. list_splice_tail(&tmplist, list);
  360. btrfs_free_path(path);
  361. return ret;
  362. }
  363. int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
  364. struct bio *bio, u64 file_start, int contig)
  365. {
  366. struct btrfs_ordered_sum *sums;
  367. struct btrfs_sector_sum *sector_sum;
  368. struct btrfs_ordered_extent *ordered;
  369. char *data;
  370. struct bio_vec *bvec = bio->bi_io_vec;
  371. int bio_index = 0;
  372. unsigned long total_bytes = 0;
  373. unsigned long this_sum_bytes = 0;
  374. u64 offset;
  375. u64 disk_bytenr;
  376. WARN_ON(bio->bi_vcnt <= 0);
  377. sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
  378. if (!sums)
  379. return -ENOMEM;
  380. sector_sum = sums->sums;
  381. disk_bytenr = (u64)bio->bi_sector << 9;
  382. sums->len = bio->bi_size;
  383. INIT_LIST_HEAD(&sums->list);
  384. if (contig)
  385. offset = file_start;
  386. else
  387. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  388. ordered = btrfs_lookup_ordered_extent(inode, offset);
  389. BUG_ON(!ordered); /* Logic error */
  390. sums->bytenr = ordered->start;
  391. while (bio_index < bio->bi_vcnt) {
  392. if (!contig)
  393. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  394. if (!contig && (offset >= ordered->file_offset + ordered->len ||
  395. offset < ordered->file_offset)) {
  396. unsigned long bytes_left;
  397. sums->len = this_sum_bytes;
  398. this_sum_bytes = 0;
  399. btrfs_add_ordered_sum(inode, ordered, sums);
  400. btrfs_put_ordered_extent(ordered);
  401. bytes_left = bio->bi_size - total_bytes;
  402. sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
  403. GFP_NOFS);
  404. BUG_ON(!sums); /* -ENOMEM */
  405. sector_sum = sums->sums;
  406. sums->len = bytes_left;
  407. ordered = btrfs_lookup_ordered_extent(inode, offset);
  408. BUG_ON(!ordered); /* Logic error */
  409. sums->bytenr = ordered->start;
  410. }
  411. data = kmap_atomic(bvec->bv_page);
  412. sector_sum->sum = ~(u32)0;
  413. sector_sum->sum = btrfs_csum_data(root,
  414. data + bvec->bv_offset,
  415. sector_sum->sum,
  416. bvec->bv_len);
  417. kunmap_atomic(data);
  418. btrfs_csum_final(sector_sum->sum,
  419. (char *)&sector_sum->sum);
  420. sector_sum->bytenr = disk_bytenr;
  421. sector_sum++;
  422. bio_index++;
  423. total_bytes += bvec->bv_len;
  424. this_sum_bytes += bvec->bv_len;
  425. disk_bytenr += bvec->bv_len;
  426. offset += bvec->bv_len;
  427. bvec++;
  428. }
  429. this_sum_bytes = 0;
  430. btrfs_add_ordered_sum(inode, ordered, sums);
  431. btrfs_put_ordered_extent(ordered);
  432. return 0;
  433. }
  434. /*
  435. * helper function for csum removal, this expects the
  436. * key to describe the csum pointed to by the path, and it expects
  437. * the csum to overlap the range [bytenr, len]
  438. *
  439. * The csum should not be entirely contained in the range and the
  440. * range should not be entirely contained in the csum.
  441. *
  442. * This calls btrfs_truncate_item with the correct args based on the
  443. * overlap, and fixes up the key as required.
  444. */
  445. static noinline void truncate_one_csum(struct btrfs_trans_handle *trans,
  446. 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(trans, 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(trans, root, path, new_size, 0);
  481. key->offset = end_byte;
  482. btrfs_set_item_key_safe(trans, 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(trans, 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. u64 bytenr;
  597. int ret;
  598. struct btrfs_key file_key;
  599. struct btrfs_key found_key;
  600. u64 next_offset;
  601. u64 total_bytes = 0;
  602. int found_next;
  603. struct btrfs_path *path;
  604. struct btrfs_csum_item *item;
  605. struct btrfs_csum_item *item_end;
  606. struct extent_buffer *leaf = NULL;
  607. u64 csum_offset;
  608. struct btrfs_sector_sum *sector_sum;
  609. u32 nritems;
  610. u32 ins_size;
  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. sector_sum = sums->sums;
  616. trans->adding_csums = 1;
  617. again:
  618. next_offset = (u64)-1;
  619. found_next = 0;
  620. file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  621. file_key.offset = sector_sum->bytenr;
  622. bytenr = sector_sum->bytenr;
  623. btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
  624. item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1);
  625. if (!IS_ERR(item)) {
  626. leaf = path->nodes[0];
  627. ret = 0;
  628. goto found;
  629. }
  630. ret = PTR_ERR(item);
  631. if (ret != -EFBIG && ret != -ENOENT)
  632. goto fail_unlock;
  633. if (ret == -EFBIG) {
  634. u32 item_size;
  635. /* we found one, but it isn't big enough yet */
  636. leaf = path->nodes[0];
  637. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  638. if ((item_size / csum_size) >=
  639. MAX_CSUM_ITEMS(root, csum_size)) {
  640. /* already at max size, make a new one */
  641. goto insert;
  642. }
  643. } else {
  644. int slot = path->slots[0] + 1;
  645. /* we didn't find a csum item, insert one */
  646. nritems = btrfs_header_nritems(path->nodes[0]);
  647. if (path->slots[0] >= nritems - 1) {
  648. ret = btrfs_next_leaf(root, path);
  649. if (ret == 1)
  650. found_next = 1;
  651. if (ret != 0)
  652. goto insert;
  653. slot = 0;
  654. }
  655. btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
  656. if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  657. found_key.type != BTRFS_EXTENT_CSUM_KEY) {
  658. found_next = 1;
  659. goto insert;
  660. }
  661. next_offset = found_key.offset;
  662. found_next = 1;
  663. goto insert;
  664. }
  665. /*
  666. * at this point, we know the tree has an item, but it isn't big
  667. * enough yet to put our csum in. Grow it
  668. */
  669. btrfs_release_path(path);
  670. ret = btrfs_search_slot(trans, root, &file_key, path,
  671. csum_size, 1);
  672. if (ret < 0)
  673. goto fail_unlock;
  674. if (ret > 0) {
  675. if (path->slots[0] == 0)
  676. goto insert;
  677. path->slots[0]--;
  678. }
  679. leaf = path->nodes[0];
  680. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  681. csum_offset = (bytenr - found_key.offset) >>
  682. root->fs_info->sb->s_blocksize_bits;
  683. if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY ||
  684. found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  685. csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
  686. goto insert;
  687. }
  688. if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
  689. csum_size) {
  690. u32 diff = (csum_offset + 1) * csum_size;
  691. /*
  692. * is the item big enough already? we dropped our lock
  693. * before and need to recheck
  694. */
  695. if (diff < btrfs_item_size_nr(leaf, path->slots[0]))
  696. goto csum;
  697. diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
  698. if (diff != csum_size)
  699. goto insert;
  700. btrfs_extend_item(trans, root, path, diff);
  701. goto csum;
  702. }
  703. insert:
  704. btrfs_release_path(path);
  705. csum_offset = 0;
  706. if (found_next) {
  707. u64 tmp = total_bytes + root->sectorsize;
  708. u64 next_sector = sector_sum->bytenr;
  709. struct btrfs_sector_sum *next = sector_sum + 1;
  710. while (tmp < sums->len) {
  711. if (next_sector + root->sectorsize != next->bytenr)
  712. break;
  713. tmp += root->sectorsize;
  714. next_sector = next->bytenr;
  715. next++;
  716. }
  717. tmp = min(tmp, next_offset - file_key.offset);
  718. tmp >>= root->fs_info->sb->s_blocksize_bits;
  719. tmp = max((u64)1, tmp);
  720. tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
  721. ins_size = csum_size * tmp;
  722. } else {
  723. ins_size = csum_size;
  724. }
  725. path->leave_spinning = 1;
  726. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  727. ins_size);
  728. path->leave_spinning = 0;
  729. if (ret < 0)
  730. goto fail_unlock;
  731. if (ret != 0) {
  732. WARN_ON(1);
  733. goto fail_unlock;
  734. }
  735. csum:
  736. leaf = path->nodes[0];
  737. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  738. ret = 0;
  739. item = (struct btrfs_csum_item *)((unsigned char *)item +
  740. csum_offset * csum_size);
  741. found:
  742. item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  743. item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
  744. btrfs_item_size_nr(leaf, path->slots[0]));
  745. next_sector:
  746. write_extent_buffer(leaf, &sector_sum->sum, (unsigned long)item, csum_size);
  747. total_bytes += root->sectorsize;
  748. sector_sum++;
  749. if (total_bytes < sums->len) {
  750. item = (struct btrfs_csum_item *)((char *)item +
  751. csum_size);
  752. if (item < item_end && bytenr + PAGE_CACHE_SIZE ==
  753. sector_sum->bytenr) {
  754. bytenr = sector_sum->bytenr;
  755. goto next_sector;
  756. }
  757. }
  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. trans->adding_csums = 0;
  766. btrfs_free_path(path);
  767. return ret;
  768. fail_unlock:
  769. goto out;
  770. }