file-item.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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 <linux/sched/mm.h>
  10. #include <crypto/hash.h>
  11. #include "messages.h"
  12. #include "ctree.h"
  13. #include "disk-io.h"
  14. #include "transaction.h"
  15. #include "bio.h"
  16. #include "compression.h"
  17. #include "fs.h"
  18. #include "accessors.h"
  19. #include "file-item.h"
  20. #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \
  21. sizeof(struct btrfs_item) * 2) / \
  22. size) - 1))
  23. #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \
  24. PAGE_SIZE))
  25. /*
  26. * Set inode's size according to filesystem options.
  27. *
  28. * @inode: inode we want to update the disk_i_size for
  29. * @new_i_size: i_size we want to set to, 0 if we use i_size
  30. *
  31. * With NO_HOLES set this simply sets the disk_is_size to whatever i_size_read()
  32. * returns as it is perfectly fine with a file that has holes without hole file
  33. * extent items.
  34. *
  35. * However without NO_HOLES we need to only return the area that is contiguous
  36. * from the 0 offset of the file. Otherwise we could end up adjust i_size up
  37. * to an extent that has a gap in between.
  38. *
  39. * Finally new_i_size should only be set in the case of truncate where we're not
  40. * ready to use i_size_read() as the limiter yet.
  41. */
  42. void btrfs_inode_safe_disk_i_size_write(struct btrfs_inode *inode, u64 new_i_size)
  43. {
  44. u64 start, end, i_size;
  45. int ret;
  46. spin_lock(&inode->lock);
  47. i_size = new_i_size ?: i_size_read(&inode->vfs_inode);
  48. if (!inode->file_extent_tree) {
  49. inode->disk_i_size = i_size;
  50. goto out_unlock;
  51. }
  52. ret = find_contiguous_extent_bit(inode->file_extent_tree, 0, &start,
  53. &end, EXTENT_DIRTY);
  54. if (!ret && start == 0)
  55. i_size = min(i_size, end + 1);
  56. else
  57. i_size = 0;
  58. inode->disk_i_size = i_size;
  59. out_unlock:
  60. spin_unlock(&inode->lock);
  61. }
  62. /*
  63. * Mark range within a file as having a new extent inserted.
  64. *
  65. * @inode: inode being modified
  66. * @start: start file offset of the file extent we've inserted
  67. * @len: logical length of the file extent item
  68. *
  69. * Call when we are inserting a new file extent where there was none before.
  70. * Does not need to call this in the case where we're replacing an existing file
  71. * extent, however if not sure it's fine to call this multiple times.
  72. *
  73. * The start and len must match the file extent item, so thus must be sectorsize
  74. * aligned.
  75. */
  76. int btrfs_inode_set_file_extent_range(struct btrfs_inode *inode, u64 start,
  77. u64 len)
  78. {
  79. if (!inode->file_extent_tree)
  80. return 0;
  81. if (len == 0)
  82. return 0;
  83. ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize));
  84. return set_extent_bit(inode->file_extent_tree, start, start + len - 1,
  85. EXTENT_DIRTY, NULL);
  86. }
  87. /*
  88. * Mark an inode range as not having a backing extent.
  89. *
  90. * @inode: inode being modified
  91. * @start: start file offset of the file extent we've inserted
  92. * @len: logical length of the file extent item
  93. *
  94. * Called when we drop a file extent, for example when we truncate. Doesn't
  95. * need to be called for cases where we're replacing a file extent, like when
  96. * we've COWed a file extent.
  97. *
  98. * The start and len must match the file extent item, so thus must be sectorsize
  99. * aligned.
  100. */
  101. int btrfs_inode_clear_file_extent_range(struct btrfs_inode *inode, u64 start,
  102. u64 len)
  103. {
  104. if (!inode->file_extent_tree)
  105. return 0;
  106. if (len == 0)
  107. return 0;
  108. ASSERT(IS_ALIGNED(start + len, inode->root->fs_info->sectorsize) ||
  109. len == (u64)-1);
  110. return clear_extent_bit(inode->file_extent_tree, start,
  111. start + len - 1, EXTENT_DIRTY, NULL);
  112. }
  113. static size_t bytes_to_csum_size(const struct btrfs_fs_info *fs_info, u32 bytes)
  114. {
  115. ASSERT(IS_ALIGNED(bytes, fs_info->sectorsize));
  116. return (bytes >> fs_info->sectorsize_bits) * fs_info->csum_size;
  117. }
  118. static size_t csum_size_to_bytes(const struct btrfs_fs_info *fs_info, u32 csum_size)
  119. {
  120. ASSERT(IS_ALIGNED(csum_size, fs_info->csum_size));
  121. return (csum_size / fs_info->csum_size) << fs_info->sectorsize_bits;
  122. }
  123. static inline u32 max_ordered_sum_bytes(const struct btrfs_fs_info *fs_info)
  124. {
  125. u32 max_csum_size = round_down(PAGE_SIZE - sizeof(struct btrfs_ordered_sum),
  126. fs_info->csum_size);
  127. return csum_size_to_bytes(fs_info, max_csum_size);
  128. }
  129. /*
  130. * Calculate the total size needed to allocate for an ordered sum structure
  131. * spanning @bytes in the file.
  132. */
  133. static int btrfs_ordered_sum_size(const struct btrfs_fs_info *fs_info, unsigned long bytes)
  134. {
  135. return sizeof(struct btrfs_ordered_sum) + bytes_to_csum_size(fs_info, bytes);
  136. }
  137. int btrfs_insert_hole_extent(struct btrfs_trans_handle *trans,
  138. struct btrfs_root *root,
  139. u64 objectid, u64 pos, u64 num_bytes)
  140. {
  141. int ret = 0;
  142. struct btrfs_file_extent_item *item;
  143. struct btrfs_key file_key;
  144. struct btrfs_path *path;
  145. struct extent_buffer *leaf;
  146. path = btrfs_alloc_path();
  147. if (!path)
  148. return -ENOMEM;
  149. file_key.objectid = objectid;
  150. file_key.offset = pos;
  151. file_key.type = BTRFS_EXTENT_DATA_KEY;
  152. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  153. sizeof(*item));
  154. if (ret < 0)
  155. goto out;
  156. leaf = path->nodes[0];
  157. item = btrfs_item_ptr(leaf, path->slots[0],
  158. struct btrfs_file_extent_item);
  159. btrfs_set_file_extent_disk_bytenr(leaf, item, 0);
  160. btrfs_set_file_extent_disk_num_bytes(leaf, item, 0);
  161. btrfs_set_file_extent_offset(leaf, item, 0);
  162. btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
  163. btrfs_set_file_extent_ram_bytes(leaf, item, num_bytes);
  164. btrfs_set_file_extent_generation(leaf, item, trans->transid);
  165. btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
  166. btrfs_set_file_extent_compression(leaf, item, 0);
  167. btrfs_set_file_extent_encryption(leaf, item, 0);
  168. btrfs_set_file_extent_other_encoding(leaf, item, 0);
  169. btrfs_mark_buffer_dirty(trans, leaf);
  170. out:
  171. btrfs_free_path(path);
  172. return ret;
  173. }
  174. static struct btrfs_csum_item *
  175. btrfs_lookup_csum(struct btrfs_trans_handle *trans,
  176. struct btrfs_root *root,
  177. struct btrfs_path *path,
  178. u64 bytenr, int cow)
  179. {
  180. struct btrfs_fs_info *fs_info = root->fs_info;
  181. int ret;
  182. struct btrfs_key file_key;
  183. struct btrfs_key found_key;
  184. struct btrfs_csum_item *item;
  185. struct extent_buffer *leaf;
  186. u64 csum_offset = 0;
  187. const u32 csum_size = fs_info->csum_size;
  188. int csums_in_item;
  189. file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  190. file_key.offset = bytenr;
  191. file_key.type = BTRFS_EXTENT_CSUM_KEY;
  192. ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
  193. if (ret < 0)
  194. goto fail;
  195. leaf = path->nodes[0];
  196. if (ret > 0) {
  197. ret = 1;
  198. if (path->slots[0] == 0)
  199. goto fail;
  200. path->slots[0]--;
  201. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  202. if (found_key.type != BTRFS_EXTENT_CSUM_KEY)
  203. goto fail;
  204. csum_offset = (bytenr - found_key.offset) >>
  205. fs_info->sectorsize_bits;
  206. csums_in_item = btrfs_item_size(leaf, path->slots[0]);
  207. csums_in_item /= csum_size;
  208. if (csum_offset == csums_in_item) {
  209. ret = -EFBIG;
  210. goto fail;
  211. } else if (csum_offset > csums_in_item) {
  212. goto fail;
  213. }
  214. }
  215. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  216. item = (struct btrfs_csum_item *)((unsigned char *)item +
  217. csum_offset * csum_size);
  218. return item;
  219. fail:
  220. if (ret > 0)
  221. ret = -ENOENT;
  222. return ERR_PTR(ret);
  223. }
  224. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  225. struct btrfs_root *root,
  226. struct btrfs_path *path, u64 objectid,
  227. u64 offset, int mod)
  228. {
  229. struct btrfs_key file_key;
  230. int ins_len = mod < 0 ? -1 : 0;
  231. int cow = mod != 0;
  232. file_key.objectid = objectid;
  233. file_key.offset = offset;
  234. file_key.type = BTRFS_EXTENT_DATA_KEY;
  235. return btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
  236. }
  237. /*
  238. * Find checksums for logical bytenr range [disk_bytenr, disk_bytenr + len) and
  239. * store the result to @dst.
  240. *
  241. * Return >0 for the number of sectors we found.
  242. * Return 0 for the range [disk_bytenr, disk_bytenr + sectorsize) has no csum
  243. * for it. Caller may want to try next sector until one range is hit.
  244. * Return <0 for fatal error.
  245. */
  246. static int search_csum_tree(struct btrfs_fs_info *fs_info,
  247. struct btrfs_path *path, u64 disk_bytenr,
  248. u64 len, u8 *dst)
  249. {
  250. struct btrfs_root *csum_root;
  251. struct btrfs_csum_item *item = NULL;
  252. struct btrfs_key key;
  253. const u32 sectorsize = fs_info->sectorsize;
  254. const u32 csum_size = fs_info->csum_size;
  255. u32 itemsize;
  256. int ret;
  257. u64 csum_start;
  258. u64 csum_len;
  259. ASSERT(IS_ALIGNED(disk_bytenr, sectorsize) &&
  260. IS_ALIGNED(len, sectorsize));
  261. /* Check if the current csum item covers disk_bytenr */
  262. if (path->nodes[0]) {
  263. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  264. struct btrfs_csum_item);
  265. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  266. itemsize = btrfs_item_size(path->nodes[0], path->slots[0]);
  267. csum_start = key.offset;
  268. csum_len = (itemsize / csum_size) * sectorsize;
  269. if (in_range(disk_bytenr, csum_start, csum_len))
  270. goto found;
  271. }
  272. /* Current item doesn't contain the desired range, search again */
  273. btrfs_release_path(path);
  274. csum_root = btrfs_csum_root(fs_info, disk_bytenr);
  275. item = btrfs_lookup_csum(NULL, csum_root, path, disk_bytenr, 0);
  276. if (IS_ERR(item)) {
  277. ret = PTR_ERR(item);
  278. goto out;
  279. }
  280. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  281. itemsize = btrfs_item_size(path->nodes[0], path->slots[0]);
  282. csum_start = key.offset;
  283. csum_len = (itemsize / csum_size) * sectorsize;
  284. ASSERT(in_range(disk_bytenr, csum_start, csum_len));
  285. found:
  286. ret = (min(csum_start + csum_len, disk_bytenr + len) -
  287. disk_bytenr) >> fs_info->sectorsize_bits;
  288. read_extent_buffer(path->nodes[0], dst, (unsigned long)item,
  289. ret * csum_size);
  290. out:
  291. if (ret == -ENOENT || ret == -EFBIG)
  292. ret = 0;
  293. return ret;
  294. }
  295. /*
  296. * Lookup the checksum for the read bio in csum tree.
  297. *
  298. * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise.
  299. */
  300. blk_status_t btrfs_lookup_bio_sums(struct btrfs_bio *bbio)
  301. {
  302. struct btrfs_inode *inode = bbio->inode;
  303. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  304. struct bio *bio = &bbio->bio;
  305. struct btrfs_path *path;
  306. const u32 sectorsize = fs_info->sectorsize;
  307. const u32 csum_size = fs_info->csum_size;
  308. u32 orig_len = bio->bi_iter.bi_size;
  309. u64 orig_disk_bytenr = bio->bi_iter.bi_sector << SECTOR_SHIFT;
  310. const unsigned int nblocks = orig_len >> fs_info->sectorsize_bits;
  311. blk_status_t ret = BLK_STS_OK;
  312. u32 bio_offset = 0;
  313. if ((inode->flags & BTRFS_INODE_NODATASUM) ||
  314. test_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state))
  315. return BLK_STS_OK;
  316. /*
  317. * This function is only called for read bio.
  318. *
  319. * This means two things:
  320. * - All our csums should only be in csum tree
  321. * No ordered extents csums, as ordered extents are only for write
  322. * path.
  323. * - No need to bother any other info from bvec
  324. * Since we're looking up csums, the only important info is the
  325. * disk_bytenr and the length, which can be extracted from bi_iter
  326. * directly.
  327. */
  328. ASSERT(bio_op(bio) == REQ_OP_READ);
  329. path = btrfs_alloc_path();
  330. if (!path)
  331. return BLK_STS_RESOURCE;
  332. if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
  333. bbio->csum = kmalloc_array(nblocks, csum_size, GFP_NOFS);
  334. if (!bbio->csum) {
  335. btrfs_free_path(path);
  336. return BLK_STS_RESOURCE;
  337. }
  338. } else {
  339. bbio->csum = bbio->csum_inline;
  340. }
  341. /*
  342. * If requested number of sectors is larger than one leaf can contain,
  343. * kick the readahead for csum tree.
  344. */
  345. if (nblocks > fs_info->csums_per_leaf)
  346. path->reada = READA_FORWARD;
  347. /*
  348. * the free space stuff is only read when it hasn't been
  349. * updated in the current transaction. So, we can safely
  350. * read from the commit root and sidestep a nasty deadlock
  351. * between reading the free space cache and updating the csum tree.
  352. */
  353. if (btrfs_is_free_space_inode(inode)) {
  354. path->search_commit_root = 1;
  355. path->skip_locking = 1;
  356. }
  357. while (bio_offset < orig_len) {
  358. int count;
  359. u64 cur_disk_bytenr = orig_disk_bytenr + bio_offset;
  360. u8 *csum_dst = bbio->csum +
  361. (bio_offset >> fs_info->sectorsize_bits) * csum_size;
  362. count = search_csum_tree(fs_info, path, cur_disk_bytenr,
  363. orig_len - bio_offset, csum_dst);
  364. if (count < 0) {
  365. ret = errno_to_blk_status(count);
  366. if (bbio->csum != bbio->csum_inline)
  367. kfree(bbio->csum);
  368. bbio->csum = NULL;
  369. break;
  370. }
  371. /*
  372. * We didn't find a csum for this range. We need to make sure
  373. * we complain loudly about this, because we are not NODATASUM.
  374. *
  375. * However for the DATA_RELOC inode we could potentially be
  376. * relocating data extents for a NODATASUM inode, so the inode
  377. * itself won't be marked with NODATASUM, but the extent we're
  378. * copying is in fact NODATASUM. If we don't find a csum we
  379. * assume this is the case.
  380. */
  381. if (count == 0) {
  382. memset(csum_dst, 0, csum_size);
  383. count = 1;
  384. if (btrfs_root_id(inode->root) == BTRFS_DATA_RELOC_TREE_OBJECTID) {
  385. u64 file_offset = bbio->file_offset + bio_offset;
  386. set_extent_bit(&inode->io_tree, file_offset,
  387. file_offset + sectorsize - 1,
  388. EXTENT_NODATASUM, NULL);
  389. } else {
  390. btrfs_warn_rl(fs_info,
  391. "csum hole found for disk bytenr range [%llu, %llu)",
  392. cur_disk_bytenr, cur_disk_bytenr + sectorsize);
  393. }
  394. }
  395. bio_offset += count * sectorsize;
  396. }
  397. btrfs_free_path(path);
  398. return ret;
  399. }
  400. /*
  401. * Search for checksums for a given logical range.
  402. *
  403. * @root: The root where to look for checksums.
  404. * @start: Logical address of target checksum range.
  405. * @end: End offset (inclusive) of the target checksum range.
  406. * @list: List for adding each checksum that was found.
  407. * Can be NULL in case the caller only wants to check if
  408. * there any checksums for the range.
  409. * @nowait: Indicate if the search must be non-blocking or not.
  410. *
  411. * Return < 0 on error, 0 if no checksums were found, or 1 if checksums were
  412. * found.
  413. */
  414. int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end,
  415. struct list_head *list, bool nowait)
  416. {
  417. struct btrfs_fs_info *fs_info = root->fs_info;
  418. struct btrfs_key key;
  419. struct btrfs_path *path;
  420. struct extent_buffer *leaf;
  421. struct btrfs_ordered_sum *sums;
  422. struct btrfs_csum_item *item;
  423. int ret;
  424. bool found_csums = false;
  425. ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
  426. IS_ALIGNED(end + 1, fs_info->sectorsize));
  427. path = btrfs_alloc_path();
  428. if (!path)
  429. return -ENOMEM;
  430. path->nowait = nowait;
  431. key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  432. key.offset = start;
  433. key.type = BTRFS_EXTENT_CSUM_KEY;
  434. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  435. if (ret < 0)
  436. goto out;
  437. if (ret > 0 && path->slots[0] > 0) {
  438. leaf = path->nodes[0];
  439. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  440. /*
  441. * There are two cases we can hit here for the previous csum
  442. * item:
  443. *
  444. * |<- search range ->|
  445. * |<- csum item ->|
  446. *
  447. * Or
  448. * |<- search range ->|
  449. * |<- csum item ->|
  450. *
  451. * Check if the previous csum item covers the leading part of
  452. * the search range. If so we have to start from previous csum
  453. * item.
  454. */
  455. if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
  456. key.type == BTRFS_EXTENT_CSUM_KEY) {
  457. if (bytes_to_csum_size(fs_info, start - key.offset) <
  458. btrfs_item_size(leaf, path->slots[0] - 1))
  459. path->slots[0]--;
  460. }
  461. }
  462. while (start <= end) {
  463. u64 csum_end;
  464. leaf = path->nodes[0];
  465. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  466. ret = btrfs_next_leaf(root, path);
  467. if (ret < 0)
  468. goto out;
  469. if (ret > 0)
  470. break;
  471. leaf = path->nodes[0];
  472. }
  473. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  474. if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  475. key.type != BTRFS_EXTENT_CSUM_KEY ||
  476. key.offset > end)
  477. break;
  478. if (key.offset > start)
  479. start = key.offset;
  480. csum_end = key.offset + csum_size_to_bytes(fs_info,
  481. btrfs_item_size(leaf, path->slots[0]));
  482. if (csum_end <= start) {
  483. path->slots[0]++;
  484. continue;
  485. }
  486. found_csums = true;
  487. if (!list)
  488. goto out;
  489. csum_end = min(csum_end, end + 1);
  490. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  491. struct btrfs_csum_item);
  492. while (start < csum_end) {
  493. unsigned long offset;
  494. size_t size;
  495. size = min_t(size_t, csum_end - start,
  496. max_ordered_sum_bytes(fs_info));
  497. sums = kzalloc(btrfs_ordered_sum_size(fs_info, size),
  498. GFP_NOFS);
  499. if (!sums) {
  500. ret = -ENOMEM;
  501. goto out;
  502. }
  503. sums->logical = start;
  504. sums->len = size;
  505. offset = bytes_to_csum_size(fs_info, start - key.offset);
  506. read_extent_buffer(path->nodes[0],
  507. sums->sums,
  508. ((unsigned long)item) + offset,
  509. bytes_to_csum_size(fs_info, size));
  510. start += size;
  511. list_add_tail(&sums->list, list);
  512. }
  513. path->slots[0]++;
  514. }
  515. out:
  516. btrfs_free_path(path);
  517. if (ret < 0) {
  518. if (list) {
  519. struct btrfs_ordered_sum *tmp_sums;
  520. list_for_each_entry_safe(sums, tmp_sums, list, list)
  521. kfree(sums);
  522. }
  523. return ret;
  524. }
  525. return found_csums ? 1 : 0;
  526. }
  527. /*
  528. * Do the same work as btrfs_lookup_csums_list(), the difference is in how
  529. * we return the result.
  530. *
  531. * This version will set the corresponding bits in @csum_bitmap to represent
  532. * that there is a csum found.
  533. * Each bit represents a sector. Thus caller should ensure @csum_buf passed
  534. * in is large enough to contain all csums.
  535. */
  536. int btrfs_lookup_csums_bitmap(struct btrfs_root *root, struct btrfs_path *path,
  537. u64 start, u64 end, u8 *csum_buf,
  538. unsigned long *csum_bitmap)
  539. {
  540. struct btrfs_fs_info *fs_info = root->fs_info;
  541. struct btrfs_key key;
  542. struct extent_buffer *leaf;
  543. struct btrfs_csum_item *item;
  544. const u64 orig_start = start;
  545. bool free_path = false;
  546. int ret;
  547. ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
  548. IS_ALIGNED(end + 1, fs_info->sectorsize));
  549. if (!path) {
  550. path = btrfs_alloc_path();
  551. if (!path)
  552. return -ENOMEM;
  553. free_path = true;
  554. }
  555. /* Check if we can reuse the previous path. */
  556. if (path->nodes[0]) {
  557. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  558. if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
  559. key.type == BTRFS_EXTENT_CSUM_KEY &&
  560. key.offset <= start)
  561. goto search_forward;
  562. btrfs_release_path(path);
  563. }
  564. key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  565. key.type = BTRFS_EXTENT_CSUM_KEY;
  566. key.offset = start;
  567. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  568. if (ret < 0)
  569. goto fail;
  570. if (ret > 0 && path->slots[0] > 0) {
  571. leaf = path->nodes[0];
  572. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  573. /*
  574. * There are two cases we can hit here for the previous csum
  575. * item:
  576. *
  577. * |<- search range ->|
  578. * |<- csum item ->|
  579. *
  580. * Or
  581. * |<- search range ->|
  582. * |<- csum item ->|
  583. *
  584. * Check if the previous csum item covers the leading part of
  585. * the search range. If so we have to start from previous csum
  586. * item.
  587. */
  588. if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
  589. key.type == BTRFS_EXTENT_CSUM_KEY) {
  590. if (bytes_to_csum_size(fs_info, start - key.offset) <
  591. btrfs_item_size(leaf, path->slots[0] - 1))
  592. path->slots[0]--;
  593. }
  594. }
  595. search_forward:
  596. while (start <= end) {
  597. u64 csum_end;
  598. leaf = path->nodes[0];
  599. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  600. ret = btrfs_next_leaf(root, path);
  601. if (ret < 0)
  602. goto fail;
  603. if (ret > 0)
  604. break;
  605. leaf = path->nodes[0];
  606. }
  607. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  608. if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  609. key.type != BTRFS_EXTENT_CSUM_KEY ||
  610. key.offset > end)
  611. break;
  612. if (key.offset > start)
  613. start = key.offset;
  614. csum_end = key.offset + csum_size_to_bytes(fs_info,
  615. btrfs_item_size(leaf, path->slots[0]));
  616. if (csum_end <= start) {
  617. path->slots[0]++;
  618. continue;
  619. }
  620. csum_end = min(csum_end, end + 1);
  621. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  622. struct btrfs_csum_item);
  623. while (start < csum_end) {
  624. unsigned long offset;
  625. size_t size;
  626. u8 *csum_dest = csum_buf + bytes_to_csum_size(fs_info,
  627. start - orig_start);
  628. size = min_t(size_t, csum_end - start, end + 1 - start);
  629. offset = bytes_to_csum_size(fs_info, start - key.offset);
  630. read_extent_buffer(path->nodes[0], csum_dest,
  631. ((unsigned long)item) + offset,
  632. bytes_to_csum_size(fs_info, size));
  633. bitmap_set(csum_bitmap,
  634. (start - orig_start) >> fs_info->sectorsize_bits,
  635. size >> fs_info->sectorsize_bits);
  636. start += size;
  637. }
  638. path->slots[0]++;
  639. }
  640. ret = 0;
  641. fail:
  642. if (free_path)
  643. btrfs_free_path(path);
  644. return ret;
  645. }
  646. /*
  647. * Calculate checksums of the data contained inside a bio.
  648. */
  649. blk_status_t btrfs_csum_one_bio(struct btrfs_bio *bbio)
  650. {
  651. struct btrfs_ordered_extent *ordered = bbio->ordered;
  652. struct btrfs_inode *inode = bbio->inode;
  653. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  654. SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
  655. struct bio *bio = &bbio->bio;
  656. struct btrfs_ordered_sum *sums;
  657. char *data;
  658. struct bvec_iter iter;
  659. struct bio_vec bvec;
  660. int index;
  661. unsigned int blockcount;
  662. int i;
  663. unsigned nofs_flag;
  664. nofs_flag = memalloc_nofs_save();
  665. sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size),
  666. GFP_KERNEL);
  667. memalloc_nofs_restore(nofs_flag);
  668. if (!sums)
  669. return BLK_STS_RESOURCE;
  670. sums->len = bio->bi_iter.bi_size;
  671. INIT_LIST_HEAD(&sums->list);
  672. sums->logical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
  673. index = 0;
  674. shash->tfm = fs_info->csum_shash;
  675. bio_for_each_segment(bvec, bio, iter) {
  676. blockcount = BTRFS_BYTES_TO_BLKS(fs_info,
  677. bvec.bv_len + fs_info->sectorsize
  678. - 1);
  679. for (i = 0; i < blockcount; i++) {
  680. data = bvec_kmap_local(&bvec);
  681. crypto_shash_digest(shash,
  682. data + (i * fs_info->sectorsize),
  683. fs_info->sectorsize,
  684. sums->sums + index);
  685. kunmap_local(data);
  686. index += fs_info->csum_size;
  687. }
  688. }
  689. bbio->sums = sums;
  690. btrfs_add_ordered_sum(ordered, sums);
  691. return 0;
  692. }
  693. /*
  694. * Nodatasum I/O on zoned file systems still requires an btrfs_ordered_sum to
  695. * record the updated logical address on Zone Append completion.
  696. * Allocate just the structure with an empty sums array here for that case.
  697. */
  698. blk_status_t btrfs_alloc_dummy_sum(struct btrfs_bio *bbio)
  699. {
  700. bbio->sums = kmalloc(sizeof(*bbio->sums), GFP_NOFS);
  701. if (!bbio->sums)
  702. return BLK_STS_RESOURCE;
  703. bbio->sums->len = bbio->bio.bi_iter.bi_size;
  704. bbio->sums->logical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
  705. btrfs_add_ordered_sum(bbio->ordered, bbio->sums);
  706. return 0;
  707. }
  708. /*
  709. * Remove one checksum overlapping a range.
  710. *
  711. * This expects the key to describe the csum pointed to by the path, and it
  712. * expects the csum to overlap the range [bytenr, len]
  713. *
  714. * The csum should not be entirely contained in the range and the range should
  715. * not be entirely contained in the csum.
  716. *
  717. * This calls btrfs_truncate_item with the correct args based on the overlap,
  718. * and fixes up the key as required.
  719. */
  720. static noinline void truncate_one_csum(struct btrfs_trans_handle *trans,
  721. struct btrfs_path *path,
  722. struct btrfs_key *key,
  723. u64 bytenr, u64 len)
  724. {
  725. struct btrfs_fs_info *fs_info = trans->fs_info;
  726. struct extent_buffer *leaf;
  727. const u32 csum_size = fs_info->csum_size;
  728. u64 csum_end;
  729. u64 end_byte = bytenr + len;
  730. u32 blocksize_bits = fs_info->sectorsize_bits;
  731. leaf = path->nodes[0];
  732. csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size;
  733. csum_end <<= blocksize_bits;
  734. csum_end += key->offset;
  735. if (key->offset < bytenr && csum_end <= end_byte) {
  736. /*
  737. * [ bytenr - len ]
  738. * [ ]
  739. * [csum ]
  740. * A simple truncate off the end of the item
  741. */
  742. u32 new_size = (bytenr - key->offset) >> blocksize_bits;
  743. new_size *= csum_size;
  744. btrfs_truncate_item(trans, path, new_size, 1);
  745. } else if (key->offset >= bytenr && csum_end > end_byte &&
  746. end_byte > key->offset) {
  747. /*
  748. * [ bytenr - len ]
  749. * [ ]
  750. * [csum ]
  751. * we need to truncate from the beginning of the csum
  752. */
  753. u32 new_size = (csum_end - end_byte) >> blocksize_bits;
  754. new_size *= csum_size;
  755. btrfs_truncate_item(trans, path, new_size, 0);
  756. key->offset = end_byte;
  757. btrfs_set_item_key_safe(trans, path, key);
  758. } else {
  759. BUG();
  760. }
  761. }
  762. /*
  763. * Delete the csum items from the csum tree for a given range of bytes.
  764. */
  765. int btrfs_del_csums(struct btrfs_trans_handle *trans,
  766. struct btrfs_root *root, u64 bytenr, u64 len)
  767. {
  768. struct btrfs_fs_info *fs_info = trans->fs_info;
  769. struct btrfs_path *path;
  770. struct btrfs_key key;
  771. u64 end_byte = bytenr + len;
  772. u64 csum_end;
  773. struct extent_buffer *leaf;
  774. int ret = 0;
  775. const u32 csum_size = fs_info->csum_size;
  776. u32 blocksize_bits = fs_info->sectorsize_bits;
  777. ASSERT(btrfs_root_id(root) == BTRFS_CSUM_TREE_OBJECTID ||
  778. btrfs_root_id(root) == BTRFS_TREE_LOG_OBJECTID);
  779. path = btrfs_alloc_path();
  780. if (!path)
  781. return -ENOMEM;
  782. while (1) {
  783. key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  784. key.offset = end_byte - 1;
  785. key.type = BTRFS_EXTENT_CSUM_KEY;
  786. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  787. if (ret > 0) {
  788. ret = 0;
  789. if (path->slots[0] == 0)
  790. break;
  791. path->slots[0]--;
  792. } else if (ret < 0) {
  793. break;
  794. }
  795. leaf = path->nodes[0];
  796. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  797. if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  798. key.type != BTRFS_EXTENT_CSUM_KEY) {
  799. break;
  800. }
  801. if (key.offset >= end_byte)
  802. break;
  803. csum_end = btrfs_item_size(leaf, path->slots[0]) / csum_size;
  804. csum_end <<= blocksize_bits;
  805. csum_end += key.offset;
  806. /* this csum ends before we start, we're done */
  807. if (csum_end <= bytenr)
  808. break;
  809. /* delete the entire item, it is inside our range */
  810. if (key.offset >= bytenr && csum_end <= end_byte) {
  811. int del_nr = 1;
  812. /*
  813. * Check how many csum items preceding this one in this
  814. * leaf correspond to our range and then delete them all
  815. * at once.
  816. */
  817. if (key.offset > bytenr && path->slots[0] > 0) {
  818. int slot = path->slots[0] - 1;
  819. while (slot >= 0) {
  820. struct btrfs_key pk;
  821. btrfs_item_key_to_cpu(leaf, &pk, slot);
  822. if (pk.offset < bytenr ||
  823. pk.type != BTRFS_EXTENT_CSUM_KEY ||
  824. pk.objectid !=
  825. BTRFS_EXTENT_CSUM_OBJECTID)
  826. break;
  827. path->slots[0] = slot;
  828. del_nr++;
  829. key.offset = pk.offset;
  830. slot--;
  831. }
  832. }
  833. ret = btrfs_del_items(trans, root, path,
  834. path->slots[0], del_nr);
  835. if (ret)
  836. break;
  837. if (key.offset == bytenr)
  838. break;
  839. } else if (key.offset < bytenr && csum_end > end_byte) {
  840. unsigned long offset;
  841. unsigned long shift_len;
  842. unsigned long item_offset;
  843. /*
  844. * [ bytenr - len ]
  845. * [csum ]
  846. *
  847. * Our bytes are in the middle of the csum,
  848. * we need to split this item and insert a new one.
  849. *
  850. * But we can't drop the path because the
  851. * csum could change, get removed, extended etc.
  852. *
  853. * The trick here is the max size of a csum item leaves
  854. * enough room in the tree block for a single
  855. * item header. So, we split the item in place,
  856. * adding a new header pointing to the existing
  857. * bytes. Then we loop around again and we have
  858. * a nicely formed csum item that we can neatly
  859. * truncate.
  860. */
  861. offset = (bytenr - key.offset) >> blocksize_bits;
  862. offset *= csum_size;
  863. shift_len = (len >> blocksize_bits) * csum_size;
  864. item_offset = btrfs_item_ptr_offset(leaf,
  865. path->slots[0]);
  866. memzero_extent_buffer(leaf, item_offset + offset,
  867. shift_len);
  868. key.offset = bytenr;
  869. /*
  870. * btrfs_split_item returns -EAGAIN when the
  871. * item changed size or key
  872. */
  873. ret = btrfs_split_item(trans, root, path, &key, offset);
  874. if (ret && ret != -EAGAIN) {
  875. btrfs_abort_transaction(trans, ret);
  876. break;
  877. }
  878. ret = 0;
  879. key.offset = end_byte - 1;
  880. } else {
  881. truncate_one_csum(trans, path, &key, bytenr, len);
  882. if (key.offset < bytenr)
  883. break;
  884. }
  885. btrfs_release_path(path);
  886. }
  887. btrfs_free_path(path);
  888. return ret;
  889. }
  890. static int find_next_csum_offset(struct btrfs_root *root,
  891. struct btrfs_path *path,
  892. u64 *next_offset)
  893. {
  894. const u32 nritems = btrfs_header_nritems(path->nodes[0]);
  895. struct btrfs_key found_key;
  896. int slot = path->slots[0] + 1;
  897. int ret;
  898. if (nritems == 0 || slot >= nritems) {
  899. ret = btrfs_next_leaf(root, path);
  900. if (ret < 0) {
  901. return ret;
  902. } else if (ret > 0) {
  903. *next_offset = (u64)-1;
  904. return 0;
  905. }
  906. slot = path->slots[0];
  907. }
  908. btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
  909. if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  910. found_key.type != BTRFS_EXTENT_CSUM_KEY)
  911. *next_offset = (u64)-1;
  912. else
  913. *next_offset = found_key.offset;
  914. return 0;
  915. }
  916. int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
  917. struct btrfs_root *root,
  918. struct btrfs_ordered_sum *sums)
  919. {
  920. struct btrfs_fs_info *fs_info = root->fs_info;
  921. struct btrfs_key file_key;
  922. struct btrfs_key found_key;
  923. struct btrfs_path *path;
  924. struct btrfs_csum_item *item;
  925. struct btrfs_csum_item *item_end;
  926. struct extent_buffer *leaf = NULL;
  927. u64 next_offset;
  928. u64 total_bytes = 0;
  929. u64 csum_offset;
  930. u64 bytenr;
  931. u32 ins_size;
  932. int index = 0;
  933. int found_next;
  934. int ret;
  935. const u32 csum_size = fs_info->csum_size;
  936. path = btrfs_alloc_path();
  937. if (!path)
  938. return -ENOMEM;
  939. again:
  940. next_offset = (u64)-1;
  941. found_next = 0;
  942. bytenr = sums->logical + total_bytes;
  943. file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  944. file_key.offset = bytenr;
  945. file_key.type = BTRFS_EXTENT_CSUM_KEY;
  946. item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
  947. if (!IS_ERR(item)) {
  948. ret = 0;
  949. leaf = path->nodes[0];
  950. item_end = btrfs_item_ptr(leaf, path->slots[0],
  951. struct btrfs_csum_item);
  952. item_end = (struct btrfs_csum_item *)((char *)item_end +
  953. btrfs_item_size(leaf, path->slots[0]));
  954. goto found;
  955. }
  956. ret = PTR_ERR(item);
  957. if (ret != -EFBIG && ret != -ENOENT)
  958. goto out;
  959. if (ret == -EFBIG) {
  960. u32 item_size;
  961. /* we found one, but it isn't big enough yet */
  962. leaf = path->nodes[0];
  963. item_size = btrfs_item_size(leaf, path->slots[0]);
  964. if ((item_size / csum_size) >=
  965. MAX_CSUM_ITEMS(fs_info, csum_size)) {
  966. /* already at max size, make a new one */
  967. goto insert;
  968. }
  969. } else {
  970. /* We didn't find a csum item, insert one. */
  971. ret = find_next_csum_offset(root, path, &next_offset);
  972. if (ret < 0)
  973. goto out;
  974. found_next = 1;
  975. goto insert;
  976. }
  977. /*
  978. * At this point, we know the tree has a checksum item that ends at an
  979. * offset matching the start of the checksum range we want to insert.
  980. * We try to extend that item as much as possible and then add as many
  981. * checksums to it as they fit.
  982. *
  983. * First check if the leaf has enough free space for at least one
  984. * checksum. If it has go directly to the item extension code, otherwise
  985. * release the path and do a search for insertion before the extension.
  986. */
  987. if (btrfs_leaf_free_space(leaf) >= csum_size) {
  988. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  989. csum_offset = (bytenr - found_key.offset) >>
  990. fs_info->sectorsize_bits;
  991. goto extend_csum;
  992. }
  993. btrfs_release_path(path);
  994. path->search_for_extension = 1;
  995. ret = btrfs_search_slot(trans, root, &file_key, path,
  996. csum_size, 1);
  997. path->search_for_extension = 0;
  998. if (ret < 0)
  999. goto out;
  1000. if (ret > 0) {
  1001. if (path->slots[0] == 0)
  1002. goto insert;
  1003. path->slots[0]--;
  1004. }
  1005. leaf = path->nodes[0];
  1006. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1007. csum_offset = (bytenr - found_key.offset) >> fs_info->sectorsize_bits;
  1008. if (found_key.type != BTRFS_EXTENT_CSUM_KEY ||
  1009. found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  1010. csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) {
  1011. goto insert;
  1012. }
  1013. extend_csum:
  1014. if (csum_offset == btrfs_item_size(leaf, path->slots[0]) /
  1015. csum_size) {
  1016. int extend_nr;
  1017. u64 tmp;
  1018. u32 diff;
  1019. tmp = sums->len - total_bytes;
  1020. tmp >>= fs_info->sectorsize_bits;
  1021. WARN_ON(tmp < 1);
  1022. extend_nr = max_t(int, 1, tmp);
  1023. /*
  1024. * A log tree can already have checksum items with a subset of
  1025. * the checksums we are trying to log. This can happen after
  1026. * doing a sequence of partial writes into prealloc extents and
  1027. * fsyncs in between, with a full fsync logging a larger subrange
  1028. * of an extent for which a previous fast fsync logged a smaller
  1029. * subrange. And this happens in particular due to merging file
  1030. * extent items when we complete an ordered extent for a range
  1031. * covered by a prealloc extent - this is done at
  1032. * btrfs_mark_extent_written().
  1033. *
  1034. * So if we try to extend the previous checksum item, which has
  1035. * a range that ends at the start of the range we want to insert,
  1036. * make sure we don't extend beyond the start offset of the next
  1037. * checksum item. If we are at the last item in the leaf, then
  1038. * forget the optimization of extending and add a new checksum
  1039. * item - it is not worth the complexity of releasing the path,
  1040. * getting the first key for the next leaf, repeat the btree
  1041. * search, etc, because log trees are temporary anyway and it
  1042. * would only save a few bytes of leaf space.
  1043. */
  1044. if (btrfs_root_id(root) == BTRFS_TREE_LOG_OBJECTID) {
  1045. if (path->slots[0] + 1 >=
  1046. btrfs_header_nritems(path->nodes[0])) {
  1047. ret = find_next_csum_offset(root, path, &next_offset);
  1048. if (ret < 0)
  1049. goto out;
  1050. found_next = 1;
  1051. goto insert;
  1052. }
  1053. ret = find_next_csum_offset(root, path, &next_offset);
  1054. if (ret < 0)
  1055. goto out;
  1056. tmp = (next_offset - bytenr) >> fs_info->sectorsize_bits;
  1057. if (tmp <= INT_MAX)
  1058. extend_nr = min_t(int, extend_nr, tmp);
  1059. }
  1060. diff = (csum_offset + extend_nr) * csum_size;
  1061. diff = min(diff,
  1062. MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size);
  1063. diff = diff - btrfs_item_size(leaf, path->slots[0]);
  1064. diff = min_t(u32, btrfs_leaf_free_space(leaf), diff);
  1065. diff /= csum_size;
  1066. diff *= csum_size;
  1067. btrfs_extend_item(trans, path, diff);
  1068. ret = 0;
  1069. goto csum;
  1070. }
  1071. insert:
  1072. btrfs_release_path(path);
  1073. csum_offset = 0;
  1074. if (found_next) {
  1075. u64 tmp;
  1076. tmp = sums->len - total_bytes;
  1077. tmp >>= fs_info->sectorsize_bits;
  1078. tmp = min(tmp, (next_offset - file_key.offset) >>
  1079. fs_info->sectorsize_bits);
  1080. tmp = max_t(u64, 1, tmp);
  1081. tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size));
  1082. ins_size = csum_size * tmp;
  1083. } else {
  1084. ins_size = csum_size;
  1085. }
  1086. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  1087. ins_size);
  1088. if (ret < 0)
  1089. goto out;
  1090. leaf = path->nodes[0];
  1091. csum:
  1092. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  1093. item_end = (struct btrfs_csum_item *)((unsigned char *)item +
  1094. btrfs_item_size(leaf, path->slots[0]));
  1095. item = (struct btrfs_csum_item *)((unsigned char *)item +
  1096. csum_offset * csum_size);
  1097. found:
  1098. ins_size = (u32)(sums->len - total_bytes) >> fs_info->sectorsize_bits;
  1099. ins_size *= csum_size;
  1100. ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item,
  1101. ins_size);
  1102. write_extent_buffer(leaf, sums->sums + index, (unsigned long)item,
  1103. ins_size);
  1104. index += ins_size;
  1105. ins_size /= csum_size;
  1106. total_bytes += ins_size * fs_info->sectorsize;
  1107. btrfs_mark_buffer_dirty(trans, path->nodes[0]);
  1108. if (total_bytes < sums->len) {
  1109. btrfs_release_path(path);
  1110. cond_resched();
  1111. goto again;
  1112. }
  1113. out:
  1114. btrfs_free_path(path);
  1115. return ret;
  1116. }
  1117. void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
  1118. const struct btrfs_path *path,
  1119. const struct btrfs_file_extent_item *fi,
  1120. struct extent_map *em)
  1121. {
  1122. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  1123. struct btrfs_root *root = inode->root;
  1124. struct extent_buffer *leaf = path->nodes[0];
  1125. const int slot = path->slots[0];
  1126. struct btrfs_key key;
  1127. u64 extent_start;
  1128. u8 type = btrfs_file_extent_type(leaf, fi);
  1129. int compress_type = btrfs_file_extent_compression(leaf, fi);
  1130. btrfs_item_key_to_cpu(leaf, &key, slot);
  1131. extent_start = key.offset;
  1132. em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  1133. em->generation = btrfs_file_extent_generation(leaf, fi);
  1134. if (type == BTRFS_FILE_EXTENT_REG ||
  1135. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1136. const u64 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1137. em->start = extent_start;
  1138. em->len = btrfs_file_extent_end(path) - extent_start;
  1139. if (disk_bytenr == 0) {
  1140. em->disk_bytenr = EXTENT_MAP_HOLE;
  1141. em->disk_num_bytes = 0;
  1142. em->offset = 0;
  1143. return;
  1144. }
  1145. em->disk_bytenr = disk_bytenr;
  1146. em->disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  1147. em->offset = btrfs_file_extent_offset(leaf, fi);
  1148. if (compress_type != BTRFS_COMPRESS_NONE) {
  1149. extent_map_set_compression(em, compress_type);
  1150. } else {
  1151. /*
  1152. * Older kernels can create regular non-hole data
  1153. * extents with ram_bytes smaller than disk_num_bytes.
  1154. * Not a big deal, just always use disk_num_bytes
  1155. * for ram_bytes.
  1156. */
  1157. em->ram_bytes = em->disk_num_bytes;
  1158. if (type == BTRFS_FILE_EXTENT_PREALLOC)
  1159. em->flags |= EXTENT_FLAG_PREALLOC;
  1160. }
  1161. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1162. /* Tree-checker has ensured this. */
  1163. ASSERT(extent_start == 0);
  1164. em->disk_bytenr = EXTENT_MAP_INLINE;
  1165. em->start = 0;
  1166. em->len = fs_info->sectorsize;
  1167. em->offset = 0;
  1168. extent_map_set_compression(em, compress_type);
  1169. } else {
  1170. btrfs_err(fs_info,
  1171. "unknown file extent item type %d, inode %llu, offset %llu, "
  1172. "root %llu", type, btrfs_ino(inode), extent_start,
  1173. btrfs_root_id(root));
  1174. }
  1175. }
  1176. /*
  1177. * Returns the end offset (non inclusive) of the file extent item the given path
  1178. * points to. If it points to an inline extent, the returned offset is rounded
  1179. * up to the sector size.
  1180. */
  1181. u64 btrfs_file_extent_end(const struct btrfs_path *path)
  1182. {
  1183. const struct extent_buffer *leaf = path->nodes[0];
  1184. const int slot = path->slots[0];
  1185. struct btrfs_file_extent_item *fi;
  1186. struct btrfs_key key;
  1187. u64 end;
  1188. btrfs_item_key_to_cpu(leaf, &key, slot);
  1189. ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
  1190. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  1191. if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE)
  1192. end = leaf->fs_info->sectorsize;
  1193. else
  1194. end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  1195. return end;
  1196. }