file-item.c 27 KB

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