file-item.c 26 KB

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