file-item.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. 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. 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. btrfs_set_key_type(&file_key, 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 (btrfs_key_type(&found_key) != 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. btrfs_set_key_type(&file_key, 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_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(nblocks * csum_size,
  171. 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_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_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. printk(KERN_INFO "btrfs no csum found "
  226. "for inode %llu start %llu\n",
  227. (unsigned long long)
  228. btrfs_ino(inode),
  229. (unsigned long long)offset);
  230. }
  231. item = NULL;
  232. btrfs_release_path(path);
  233. goto found;
  234. }
  235. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  236. path->slots[0]);
  237. item_start_offset = found_key.offset;
  238. item_size = btrfs_item_size_nr(path->nodes[0],
  239. path->slots[0]);
  240. item_last_offset = item_start_offset +
  241. (item_size / csum_size) *
  242. root->sectorsize;
  243. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  244. struct btrfs_csum_item);
  245. }
  246. /*
  247. * this byte range must be able to fit inside
  248. * a single leaf so it will also fit inside a u32
  249. */
  250. diff = disk_bytenr - item_start_offset;
  251. diff = diff / root->sectorsize;
  252. diff = diff * csum_size;
  253. count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >>
  254. inode->i_sb->s_blocksize_bits);
  255. read_extent_buffer(path->nodes[0], csum,
  256. ((unsigned long)item) + diff,
  257. csum_size * count);
  258. found:
  259. csum += count * csum_size;
  260. nblocks -= count;
  261. while (count--) {
  262. disk_bytenr += bvec->bv_len;
  263. offset += bvec->bv_len;
  264. bio_index++;
  265. bvec++;
  266. }
  267. }
  268. btrfs_free_path(path);
  269. return 0;
  270. }
  271. int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
  272. struct bio *bio, u32 *dst)
  273. {
  274. return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0);
  275. }
  276. int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
  277. struct btrfs_dio_private *dip, struct bio *bio,
  278. u64 offset)
  279. {
  280. int len = (bio->bi_sector << 9) - dip->disk_bytenr;
  281. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  282. int ret;
  283. len >>= inode->i_sb->s_blocksize_bits;
  284. len *= csum_size;
  285. ret = __btrfs_lookup_bio_sums(root, inode, bio, offset,
  286. (u32 *)(dip->csum + len), 1);
  287. return ret;
  288. }
  289. int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
  290. struct list_head *list, int search_commit)
  291. {
  292. struct btrfs_key key;
  293. struct btrfs_path *path;
  294. struct extent_buffer *leaf;
  295. struct btrfs_ordered_sum *sums;
  296. struct btrfs_csum_item *item;
  297. LIST_HEAD(tmplist);
  298. unsigned long offset;
  299. int ret;
  300. size_t size;
  301. u64 csum_end;
  302. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  303. path = btrfs_alloc_path();
  304. if (!path)
  305. return -ENOMEM;
  306. if (search_commit) {
  307. path->skip_locking = 1;
  308. path->reada = 2;
  309. path->search_commit_root = 1;
  310. }
  311. key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  312. key.offset = start;
  313. key.type = BTRFS_EXTENT_CSUM_KEY;
  314. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  315. if (ret < 0)
  316. goto fail;
  317. if (ret > 0 && path->slots[0] > 0) {
  318. leaf = path->nodes[0];
  319. btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
  320. if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
  321. key.type == BTRFS_EXTENT_CSUM_KEY) {
  322. offset = (start - key.offset) >>
  323. root->fs_info->sb->s_blocksize_bits;
  324. if (offset * csum_size <
  325. btrfs_item_size_nr(leaf, path->slots[0] - 1))
  326. path->slots[0]--;
  327. }
  328. }
  329. while (start <= end) {
  330. leaf = path->nodes[0];
  331. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  332. ret = btrfs_next_leaf(root, path);
  333. if (ret < 0)
  334. goto fail;
  335. if (ret > 0)
  336. break;
  337. leaf = path->nodes[0];
  338. }
  339. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  340. if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  341. key.type != BTRFS_EXTENT_CSUM_KEY ||
  342. key.offset > end)
  343. break;
  344. if (key.offset > start)
  345. start = key.offset;
  346. size = btrfs_item_size_nr(leaf, path->slots[0]);
  347. csum_end = key.offset + (size / csum_size) * root->sectorsize;
  348. if (csum_end <= start) {
  349. path->slots[0]++;
  350. continue;
  351. }
  352. csum_end = min(csum_end, end + 1);
  353. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  354. struct btrfs_csum_item);
  355. while (start < csum_end) {
  356. size = min_t(size_t, csum_end - start,
  357. MAX_ORDERED_SUM_BYTES(root));
  358. sums = kzalloc(btrfs_ordered_sum_size(root, size),
  359. GFP_NOFS);
  360. if (!sums) {
  361. ret = -ENOMEM;
  362. goto fail;
  363. }
  364. sums->bytenr = start;
  365. sums->len = (int)size;
  366. offset = (start - key.offset) >>
  367. root->fs_info->sb->s_blocksize_bits;
  368. offset *= csum_size;
  369. size >>= root->fs_info->sb->s_blocksize_bits;
  370. read_extent_buffer(path->nodes[0],
  371. sums->sums,
  372. ((unsigned long)item) + offset,
  373. csum_size * size);
  374. start += root->sectorsize * size;
  375. list_add_tail(&sums->list, &tmplist);
  376. }
  377. path->slots[0]++;
  378. }
  379. ret = 0;
  380. fail:
  381. while (ret < 0 && !list_empty(&tmplist)) {
  382. sums = list_entry(&tmplist, struct btrfs_ordered_sum, list);
  383. list_del(&sums->list);
  384. kfree(sums);
  385. }
  386. list_splice_tail(&tmplist, list);
  387. btrfs_free_path(path);
  388. return ret;
  389. }
  390. int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
  391. struct bio *bio, u64 file_start, int contig)
  392. {
  393. struct btrfs_ordered_sum *sums;
  394. struct btrfs_ordered_extent *ordered;
  395. char *data;
  396. struct bio_vec *bvec = bio->bi_io_vec;
  397. int bio_index = 0;
  398. int index;
  399. unsigned long total_bytes = 0;
  400. unsigned long this_sum_bytes = 0;
  401. u64 offset;
  402. WARN_ON(bio->bi_vcnt <= 0);
  403. sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
  404. if (!sums)
  405. return -ENOMEM;
  406. sums->len = bio->bi_size;
  407. INIT_LIST_HEAD(&sums->list);
  408. if (contig)
  409. offset = file_start;
  410. else
  411. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  412. ordered = btrfs_lookup_ordered_extent(inode, offset);
  413. BUG_ON(!ordered); /* Logic error */
  414. sums->bytenr = (u64)bio->bi_sector << 9;
  415. index = 0;
  416. while (bio_index < bio->bi_vcnt) {
  417. if (!contig)
  418. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  419. if (offset >= ordered->file_offset + ordered->len ||
  420. offset < ordered->file_offset) {
  421. unsigned long bytes_left;
  422. sums->len = this_sum_bytes;
  423. this_sum_bytes = 0;
  424. btrfs_add_ordered_sum(inode, ordered, sums);
  425. btrfs_put_ordered_extent(ordered);
  426. bytes_left = bio->bi_size - total_bytes;
  427. sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
  428. GFP_NOFS);
  429. BUG_ON(!sums); /* -ENOMEM */
  430. sums->len = bytes_left;
  431. ordered = btrfs_lookup_ordered_extent(inode, offset);
  432. BUG_ON(!ordered); /* Logic error */
  433. sums->bytenr = ((u64)bio->bi_sector << 9) +
  434. total_bytes;
  435. index = 0;
  436. }
  437. data = kmap_atomic(bvec->bv_page);
  438. sums->sums[index] = ~(u32)0;
  439. sums->sums[index] = btrfs_csum_data(data + bvec->bv_offset,
  440. sums->sums[index],
  441. bvec->bv_len);
  442. kunmap_atomic(data);
  443. btrfs_csum_final(sums->sums[index],
  444. (char *)(sums->sums + index));
  445. bio_index++;
  446. index++;
  447. total_bytes += bvec->bv_len;
  448. this_sum_bytes += bvec->bv_len;
  449. offset += bvec->bv_len;
  450. bvec++;
  451. }
  452. this_sum_bytes = 0;
  453. btrfs_add_ordered_sum(inode, ordered, sums);
  454. btrfs_put_ordered_extent(ordered);
  455. return 0;
  456. }
  457. /*
  458. * helper function for csum removal, this expects the
  459. * key to describe the csum pointed to by the path, and it expects
  460. * the csum to overlap the range [bytenr, len]
  461. *
  462. * The csum should not be entirely contained in the range and the
  463. * range should not be entirely contained in the csum.
  464. *
  465. * This calls btrfs_truncate_item with the correct args based on the
  466. * overlap, and fixes up the key as required.
  467. */
  468. static noinline void truncate_one_csum(struct btrfs_root *root,
  469. struct btrfs_path *path,
  470. struct btrfs_key *key,
  471. u64 bytenr, u64 len)
  472. {
  473. struct extent_buffer *leaf;
  474. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  475. u64 csum_end;
  476. u64 end_byte = bytenr + len;
  477. u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits;
  478. leaf = path->nodes[0];
  479. csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
  480. csum_end <<= root->fs_info->sb->s_blocksize_bits;
  481. csum_end += key->offset;
  482. if (key->offset < bytenr && csum_end <= end_byte) {
  483. /*
  484. * [ bytenr - len ]
  485. * [ ]
  486. * [csum ]
  487. * A simple truncate off the end of the item
  488. */
  489. u32 new_size = (bytenr - key->offset) >> blocksize_bits;
  490. new_size *= csum_size;
  491. btrfs_truncate_item(root, path, new_size, 1);
  492. } else if (key->offset >= bytenr && csum_end > end_byte &&
  493. end_byte > key->offset) {
  494. /*
  495. * [ bytenr - len ]
  496. * [ ]
  497. * [csum ]
  498. * we need to truncate from the beginning of the csum
  499. */
  500. u32 new_size = (csum_end - end_byte) >> blocksize_bits;
  501. new_size *= csum_size;
  502. btrfs_truncate_item(root, path, new_size, 0);
  503. key->offset = end_byte;
  504. btrfs_set_item_key_safe(root, path, key);
  505. } else {
  506. BUG();
  507. }
  508. }
  509. /*
  510. * deletes the csum items from the csum tree for a given
  511. * range of bytes.
  512. */
  513. int btrfs_del_csums(struct btrfs_trans_handle *trans,
  514. struct btrfs_root *root, u64 bytenr, u64 len)
  515. {
  516. struct btrfs_path *path;
  517. struct btrfs_key key;
  518. u64 end_byte = bytenr + len;
  519. u64 csum_end;
  520. struct extent_buffer *leaf;
  521. int ret;
  522. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  523. int blocksize_bits = root->fs_info->sb->s_blocksize_bits;
  524. root = root->fs_info->csum_root;
  525. path = btrfs_alloc_path();
  526. if (!path)
  527. return -ENOMEM;
  528. while (1) {
  529. key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  530. key.offset = end_byte - 1;
  531. key.type = BTRFS_EXTENT_CSUM_KEY;
  532. path->leave_spinning = 1;
  533. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  534. if (ret > 0) {
  535. if (path->slots[0] == 0)
  536. break;
  537. path->slots[0]--;
  538. } else if (ret < 0) {
  539. break;
  540. }
  541. leaf = path->nodes[0];
  542. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  543. if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  544. key.type != BTRFS_EXTENT_CSUM_KEY) {
  545. break;
  546. }
  547. if (key.offset >= end_byte)
  548. break;
  549. csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
  550. csum_end <<= blocksize_bits;
  551. csum_end += key.offset;
  552. /* this csum ends before we start, we're done */
  553. if (csum_end <= bytenr)
  554. break;
  555. /* delete the entire item, it is inside our range */
  556. if (key.offset >= bytenr && csum_end <= end_byte) {
  557. ret = btrfs_del_item(trans, root, path);
  558. if (ret)
  559. goto out;
  560. if (key.offset == bytenr)
  561. break;
  562. } else if (key.offset < bytenr && csum_end > end_byte) {
  563. unsigned long offset;
  564. unsigned long shift_len;
  565. unsigned long item_offset;
  566. /*
  567. * [ bytenr - len ]
  568. * [csum ]
  569. *
  570. * Our bytes are in the middle of the csum,
  571. * we need to split this item and insert a new one.
  572. *
  573. * But we can't drop the path because the
  574. * csum could change, get removed, extended etc.
  575. *
  576. * The trick here is the max size of a csum item leaves
  577. * enough room in the tree block for a single
  578. * item header. So, we split the item in place,
  579. * adding a new header pointing to the existing
  580. * bytes. Then we loop around again and we have
  581. * a nicely formed csum item that we can neatly
  582. * truncate.
  583. */
  584. offset = (bytenr - key.offset) >> blocksize_bits;
  585. offset *= csum_size;
  586. shift_len = (len >> blocksize_bits) * csum_size;
  587. item_offset = btrfs_item_ptr_offset(leaf,
  588. path->slots[0]);
  589. memset_extent_buffer(leaf, 0, item_offset + offset,
  590. shift_len);
  591. key.offset = bytenr;
  592. /*
  593. * btrfs_split_item returns -EAGAIN when the
  594. * item changed size or key
  595. */
  596. ret = btrfs_split_item(trans, root, path, &key, offset);
  597. if (ret && ret != -EAGAIN) {
  598. btrfs_abort_transaction(trans, root, ret);
  599. goto out;
  600. }
  601. key.offset = end_byte - 1;
  602. } else {
  603. truncate_one_csum(root, path, &key, bytenr, len);
  604. if (key.offset < bytenr)
  605. break;
  606. }
  607. btrfs_release_path(path);
  608. }
  609. ret = 0;
  610. out:
  611. btrfs_free_path(path);
  612. return ret;
  613. }
  614. int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
  615. struct btrfs_root *root,
  616. struct btrfs_ordered_sum *sums)
  617. {
  618. struct btrfs_key file_key;
  619. struct btrfs_key found_key;
  620. struct btrfs_path *path;
  621. struct btrfs_csum_item *item;
  622. struct btrfs_csum_item *item_end;
  623. struct extent_buffer *leaf = NULL;
  624. u64 next_offset;
  625. u64 total_bytes = 0;
  626. u64 csum_offset;
  627. u64 bytenr;
  628. u32 nritems;
  629. u32 ins_size;
  630. int index = 0;
  631. int found_next;
  632. int ret;
  633. u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
  634. path = btrfs_alloc_path();
  635. if (!path)
  636. return -ENOMEM;
  637. again:
  638. next_offset = (u64)-1;
  639. found_next = 0;
  640. bytenr = sums->bytenr + total_bytes;
  641. file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
  642. file_key.offset = bytenr;
  643. btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
  644. item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
  645. if (!IS_ERR(item)) {
  646. ret = 0;
  647. leaf = path->nodes[0];
  648. item_end = btrfs_item_ptr(leaf, path->slots[0],
  649. struct btrfs_csum_item);
  650. item_end = (struct btrfs_csum_item *)((char *)item_end +
  651. btrfs_item_size_nr(leaf, path->slots[0]));
  652. goto found;
  653. }
  654. ret = PTR_ERR(item);
  655. if (ret != -EFBIG && ret != -ENOENT)
  656. goto fail_unlock;
  657. if (ret == -EFBIG) {
  658. u32 item_size;
  659. /* we found one, but it isn't big enough yet */
  660. leaf = path->nodes[0];
  661. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  662. if ((item_size / csum_size) >=
  663. MAX_CSUM_ITEMS(root, csum_size)) {
  664. /* already at max size, make a new one */
  665. goto insert;
  666. }
  667. } else {
  668. int slot = path->slots[0] + 1;
  669. /* we didn't find a csum item, insert one */
  670. nritems = btrfs_header_nritems(path->nodes[0]);
  671. if (path->slots[0] >= nritems - 1) {
  672. ret = btrfs_next_leaf(root, path);
  673. if (ret == 1)
  674. found_next = 1;
  675. if (ret != 0)
  676. goto insert;
  677. slot = 0;
  678. }
  679. btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
  680. if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  681. found_key.type != BTRFS_EXTENT_CSUM_KEY) {
  682. found_next = 1;
  683. goto insert;
  684. }
  685. next_offset = found_key.offset;
  686. found_next = 1;
  687. goto insert;
  688. }
  689. /*
  690. * at this point, we know the tree has an item, but it isn't big
  691. * enough yet to put our csum in. Grow it
  692. */
  693. btrfs_release_path(path);
  694. ret = btrfs_search_slot(trans, root, &file_key, path,
  695. csum_size, 1);
  696. if (ret < 0)
  697. goto fail_unlock;
  698. if (ret > 0) {
  699. if (path->slots[0] == 0)
  700. goto insert;
  701. path->slots[0]--;
  702. }
  703. leaf = path->nodes[0];
  704. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  705. csum_offset = (bytenr - found_key.offset) >>
  706. root->fs_info->sb->s_blocksize_bits;
  707. if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY ||
  708. found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
  709. csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
  710. goto insert;
  711. }
  712. if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) /
  713. csum_size) {
  714. int extend_nr;
  715. u64 tmp;
  716. u32 diff;
  717. u32 free_space;
  718. if (btrfs_leaf_free_space(root, leaf) <
  719. sizeof(struct btrfs_item) + csum_size * 2)
  720. goto insert;
  721. free_space = btrfs_leaf_free_space(root, leaf) -
  722. sizeof(struct btrfs_item) - csum_size;
  723. tmp = sums->len - total_bytes;
  724. tmp >>= root->fs_info->sb->s_blocksize_bits;
  725. WARN_ON(tmp < 1);
  726. extend_nr = max_t(int, 1, (int)tmp);
  727. diff = (csum_offset + extend_nr) * csum_size;
  728. diff = min(diff, MAX_CSUM_ITEMS(root, csum_size) * csum_size);
  729. diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
  730. diff = min(free_space, diff);
  731. diff /= csum_size;
  732. diff *= csum_size;
  733. btrfs_extend_item(root, path, diff);
  734. ret = 0;
  735. goto csum;
  736. }
  737. insert:
  738. btrfs_release_path(path);
  739. csum_offset = 0;
  740. if (found_next) {
  741. u64 tmp;
  742. tmp = sums->len - total_bytes;
  743. tmp >>= root->fs_info->sb->s_blocksize_bits;
  744. tmp = min(tmp, (next_offset - file_key.offset) >>
  745. root->fs_info->sb->s_blocksize_bits);
  746. tmp = max((u64)1, tmp);
  747. tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
  748. ins_size = csum_size * tmp;
  749. } else {
  750. ins_size = csum_size;
  751. }
  752. path->leave_spinning = 1;
  753. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  754. ins_size);
  755. path->leave_spinning = 0;
  756. if (ret < 0)
  757. goto fail_unlock;
  758. if (ret != 0) {
  759. WARN_ON(1);
  760. goto fail_unlock;
  761. }
  762. leaf = path->nodes[0];
  763. csum:
  764. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  765. item_end = (struct btrfs_csum_item *)((unsigned char *)item +
  766. btrfs_item_size_nr(leaf, path->slots[0]));
  767. item = (struct btrfs_csum_item *)((unsigned char *)item +
  768. csum_offset * csum_size);
  769. found:
  770. ins_size = (u32)(sums->len - total_bytes) >>
  771. root->fs_info->sb->s_blocksize_bits;
  772. ins_size *= csum_size;
  773. ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item,
  774. ins_size);
  775. write_extent_buffer(leaf, sums->sums + index, (unsigned long)item,
  776. ins_size);
  777. ins_size /= csum_size;
  778. total_bytes += ins_size * root->sectorsize;
  779. index += ins_size;
  780. btrfs_mark_buffer_dirty(path->nodes[0]);
  781. if (total_bytes < sums->len) {
  782. btrfs_release_path(path);
  783. cond_resched();
  784. goto again;
  785. }
  786. out:
  787. btrfs_free_path(path);
  788. return ret;
  789. fail_unlock:
  790. goto out;
  791. }