file-item.c 24 KB

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