file-item.c 24 KB

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