file-item.c 27 KB

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