file-item.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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/pagemap.h>
  20. #include <linux/highmem.h>
  21. #include "ctree.h"
  22. #include "disk-io.h"
  23. #include "transaction.h"
  24. #include "print-tree.h"
  25. #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
  26. sizeof(struct btrfs_item) * 2) / \
  27. BTRFS_CRC32_SIZE) - 1))
  28. int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
  29. struct btrfs_root *root,
  30. u64 objectid, u64 pos,
  31. u64 disk_offset, u64 disk_num_bytes,
  32. u64 num_bytes, u64 offset)
  33. {
  34. int ret = 0;
  35. struct btrfs_file_extent_item *item;
  36. struct btrfs_key file_key;
  37. struct btrfs_path *path;
  38. struct extent_buffer *leaf;
  39. path = btrfs_alloc_path();
  40. BUG_ON(!path);
  41. file_key.objectid = objectid;
  42. file_key.offset = pos;
  43. btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
  44. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  45. sizeof(*item));
  46. if (ret < 0)
  47. goto out;
  48. BUG_ON(ret);
  49. leaf = path->nodes[0];
  50. item = btrfs_item_ptr(leaf, path->slots[0],
  51. struct btrfs_file_extent_item);
  52. btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
  53. btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
  54. btrfs_set_file_extent_offset(leaf, item, offset);
  55. btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
  56. btrfs_set_file_extent_generation(leaf, item, trans->transid);
  57. btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
  58. btrfs_mark_buffer_dirty(leaf);
  59. out:
  60. btrfs_free_path(path);
  61. return ret;
  62. }
  63. struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
  64. struct btrfs_root *root,
  65. struct btrfs_path *path,
  66. u64 objectid, u64 offset,
  67. int cow)
  68. {
  69. int ret;
  70. struct btrfs_key file_key;
  71. struct btrfs_key found_key;
  72. struct btrfs_csum_item *item;
  73. struct extent_buffer *leaf;
  74. u64 csum_offset = 0;
  75. int csums_in_item;
  76. file_key.objectid = objectid;
  77. file_key.offset = offset;
  78. btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
  79. ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
  80. if (ret < 0)
  81. goto fail;
  82. leaf = path->nodes[0];
  83. if (ret > 0) {
  84. ret = 1;
  85. if (path->slots[0] == 0)
  86. goto fail;
  87. path->slots[0]--;
  88. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  89. if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
  90. found_key.objectid != objectid) {
  91. goto fail;
  92. }
  93. csum_offset = (offset - found_key.offset) >>
  94. root->fs_info->sb->s_blocksize_bits;
  95. csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
  96. csums_in_item /= BTRFS_CRC32_SIZE;
  97. if (csum_offset >= csums_in_item) {
  98. ret = -EFBIG;
  99. goto fail;
  100. }
  101. }
  102. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  103. item = (struct btrfs_csum_item *)((unsigned char *)item +
  104. csum_offset * BTRFS_CRC32_SIZE);
  105. return item;
  106. fail:
  107. if (ret > 0)
  108. ret = -ENOENT;
  109. return ERR_PTR(ret);
  110. }
  111. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  112. struct btrfs_root *root,
  113. struct btrfs_path *path, u64 objectid,
  114. u64 offset, int mod)
  115. {
  116. int ret;
  117. struct btrfs_key file_key;
  118. int ins_len = mod < 0 ? -1 : 0;
  119. int cow = mod != 0;
  120. file_key.objectid = objectid;
  121. file_key.offset = offset;
  122. btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
  123. ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
  124. return ret;
  125. }
  126. int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
  127. struct bio *bio)
  128. {
  129. u32 sum;
  130. struct bio_vec *bvec = bio->bi_io_vec;
  131. int bio_index = 0;
  132. u64 offset;
  133. u64 item_start_offset = 0;
  134. u64 item_last_offset = 0;
  135. u32 diff;
  136. int ret;
  137. struct btrfs_path *path;
  138. struct btrfs_csum_item *item = NULL;
  139. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  140. path = btrfs_alloc_path();
  141. if (bio->bi_size > PAGE_CACHE_SIZE * 8)
  142. path->reada = 2;
  143. WARN_ON(bio->bi_vcnt <= 0);
  144. while(bio_index < bio->bi_vcnt) {
  145. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  146. ret = btrfs_find_ordered_sum(inode, offset, &sum);
  147. if (ret == 0)
  148. goto found;
  149. if (!item || offset < item_start_offset ||
  150. offset >= item_last_offset) {
  151. struct btrfs_key found_key;
  152. u32 item_size;
  153. if (item)
  154. btrfs_release_path(root, path);
  155. item = btrfs_lookup_csum(NULL, root, path,
  156. inode->i_ino, offset, 0);
  157. if (IS_ERR(item)) {
  158. ret = PTR_ERR(item);
  159. if (ret == -ENOENT || ret == -EFBIG)
  160. ret = 0;
  161. sum = 0;
  162. printk("no csum found for inode %lu start "
  163. "%llu\n", inode->i_ino,
  164. (unsigned long long)offset);
  165. item = NULL;
  166. goto found;
  167. }
  168. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  169. path->slots[0]);
  170. item_start_offset = found_key.offset;
  171. item_size = btrfs_item_size_nr(path->nodes[0],
  172. path->slots[0]);
  173. item_last_offset = item_start_offset +
  174. (item_size / BTRFS_CRC32_SIZE) *
  175. root->sectorsize;
  176. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  177. struct btrfs_csum_item);
  178. }
  179. /*
  180. * this byte range must be able to fit inside
  181. * a single leaf so it will also fit inside a u32
  182. */
  183. diff = offset - item_start_offset;
  184. diff = diff / root->sectorsize;
  185. diff = diff * BTRFS_CRC32_SIZE;
  186. read_extent_buffer(path->nodes[0], &sum,
  187. ((unsigned long)item) + diff,
  188. BTRFS_CRC32_SIZE);
  189. found:
  190. set_state_private(io_tree, offset, sum);
  191. bio_index++;
  192. bvec++;
  193. }
  194. btrfs_free_path(path);
  195. return 0;
  196. }
  197. int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
  198. struct bio *bio)
  199. {
  200. struct btrfs_ordered_sum *sums;
  201. struct btrfs_sector_sum *sector_sum;
  202. struct btrfs_ordered_extent *ordered;
  203. char *data;
  204. struct bio_vec *bvec = bio->bi_io_vec;
  205. int bio_index = 0;
  206. unsigned long total_bytes = 0;
  207. unsigned long this_sum_bytes = 0;
  208. u64 offset;
  209. WARN_ON(bio->bi_vcnt <= 0);
  210. sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
  211. if (!sums)
  212. return -ENOMEM;
  213. sector_sum = sums->sums;
  214. sums->file_offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  215. sums->len = bio->bi_size;
  216. INIT_LIST_HEAD(&sums->list);
  217. ordered = btrfs_lookup_ordered_extent(inode, sums->file_offset);
  218. BUG_ON(!ordered);
  219. while(bio_index < bio->bi_vcnt) {
  220. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  221. if (offset >= ordered->file_offset + ordered->len ||
  222. offset < ordered->file_offset) {
  223. unsigned long bytes_left;
  224. sums->len = this_sum_bytes;
  225. this_sum_bytes = 0;
  226. btrfs_add_ordered_sum(inode, ordered, sums);
  227. btrfs_put_ordered_extent(ordered);
  228. bytes_left = bio->bi_size - total_bytes;
  229. sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
  230. GFP_NOFS);
  231. BUG_ON(!sums);
  232. sector_sum = sums->sums;
  233. sums->len = bytes_left;
  234. sums->file_offset = offset;
  235. ordered = btrfs_lookup_ordered_extent(inode,
  236. sums->file_offset);
  237. BUG_ON(!ordered);
  238. }
  239. data = kmap_atomic(bvec->bv_page, KM_USER0);
  240. sector_sum->sum = ~(u32)0;
  241. sector_sum->sum = btrfs_csum_data(root,
  242. data + bvec->bv_offset,
  243. sector_sum->sum,
  244. bvec->bv_len);
  245. kunmap_atomic(data, KM_USER0);
  246. btrfs_csum_final(sector_sum->sum,
  247. (char *)&sector_sum->sum);
  248. sector_sum->offset = page_offset(bvec->bv_page) +
  249. bvec->bv_offset;
  250. sector_sum++;
  251. bio_index++;
  252. total_bytes += bvec->bv_len;
  253. this_sum_bytes += bvec->bv_len;
  254. bvec++;
  255. }
  256. this_sum_bytes = 0;
  257. btrfs_add_ordered_sum(inode, ordered, sums);
  258. btrfs_put_ordered_extent(ordered);
  259. return 0;
  260. }
  261. int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
  262. struct btrfs_root *root, struct inode *inode,
  263. struct btrfs_ordered_sum *sums)
  264. {
  265. u64 objectid = inode->i_ino;
  266. u64 offset;
  267. int ret;
  268. struct btrfs_key file_key;
  269. struct btrfs_key found_key;
  270. u64 next_offset;
  271. u64 total_bytes = 0;
  272. int found_next;
  273. struct btrfs_path *path;
  274. struct btrfs_csum_item *item;
  275. struct btrfs_csum_item *item_end;
  276. struct extent_buffer *leaf = NULL;
  277. u64 csum_offset;
  278. struct btrfs_sector_sum *sector_sum;
  279. u32 nritems;
  280. u32 ins_size;
  281. char *eb_map;
  282. char *eb_token;
  283. unsigned long map_len;
  284. unsigned long map_start;
  285. path = btrfs_alloc_path();
  286. BUG_ON(!path);
  287. sector_sum = sums->sums;
  288. again:
  289. next_offset = (u64)-1;
  290. found_next = 0;
  291. offset = sector_sum->offset;
  292. file_key.objectid = objectid;
  293. file_key.offset = offset;
  294. btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
  295. mutex_lock(&BTRFS_I(inode)->csum_mutex);
  296. item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
  297. if (!IS_ERR(item)) {
  298. leaf = path->nodes[0];
  299. ret = 0;
  300. goto found;
  301. }
  302. ret = PTR_ERR(item);
  303. if (ret == -EFBIG) {
  304. u32 item_size;
  305. /* we found one, but it isn't big enough yet */
  306. leaf = path->nodes[0];
  307. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  308. if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
  309. /* already at max size, make a new one */
  310. goto insert;
  311. }
  312. } else {
  313. int slot = path->slots[0] + 1;
  314. /* we didn't find a csum item, insert one */
  315. nritems = btrfs_header_nritems(path->nodes[0]);
  316. if (path->slots[0] >= nritems - 1) {
  317. ret = btrfs_next_leaf(root, path);
  318. if (ret == 1)
  319. found_next = 1;
  320. if (ret != 0)
  321. goto insert;
  322. slot = 0;
  323. }
  324. btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
  325. if (found_key.objectid != objectid ||
  326. found_key.type != BTRFS_CSUM_ITEM_KEY) {
  327. found_next = 1;
  328. goto insert;
  329. }
  330. next_offset = found_key.offset;
  331. found_next = 1;
  332. goto insert;
  333. }
  334. /*
  335. * at this point, we know the tree has an item, but it isn't big
  336. * enough yet to put our csum in. Grow it
  337. */
  338. btrfs_release_path(root, path);
  339. ret = btrfs_search_slot(trans, root, &file_key, path,
  340. BTRFS_CRC32_SIZE, 1);
  341. if (ret < 0)
  342. goto fail_unlock;
  343. if (ret == 0) {
  344. BUG();
  345. }
  346. if (path->slots[0] == 0) {
  347. goto insert;
  348. }
  349. path->slots[0]--;
  350. leaf = path->nodes[0];
  351. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  352. csum_offset = (offset - found_key.offset) >>
  353. root->fs_info->sb->s_blocksize_bits;
  354. if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
  355. found_key.objectid != objectid ||
  356. csum_offset >= MAX_CSUM_ITEMS(root)) {
  357. goto insert;
  358. }
  359. if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
  360. BTRFS_CRC32_SIZE) {
  361. u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
  362. diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
  363. if (diff != BTRFS_CRC32_SIZE)
  364. goto insert;
  365. ret = btrfs_extend_item(trans, root, path, diff);
  366. BUG_ON(ret);
  367. goto csum;
  368. }
  369. insert:
  370. btrfs_release_path(root, path);
  371. csum_offset = 0;
  372. if (found_next) {
  373. u64 tmp = min((u64)i_size_read(inode), next_offset);
  374. tmp -= offset & ~((u64)root->sectorsize -1);
  375. tmp >>= root->fs_info->sb->s_blocksize_bits;
  376. tmp = max((u64)1, tmp);
  377. tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
  378. ins_size = BTRFS_CRC32_SIZE * tmp;
  379. } else {
  380. ins_size = BTRFS_CRC32_SIZE;
  381. }
  382. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  383. ins_size);
  384. if (ret < 0)
  385. goto fail_unlock;
  386. if (ret != 0) {
  387. WARN_ON(1);
  388. goto fail_unlock;
  389. }
  390. csum:
  391. leaf = path->nodes[0];
  392. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  393. ret = 0;
  394. item = (struct btrfs_csum_item *)((unsigned char *)item +
  395. csum_offset * BTRFS_CRC32_SIZE);
  396. found:
  397. item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  398. item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
  399. btrfs_item_size_nr(leaf, path->slots[0]));
  400. eb_token = NULL;
  401. mutex_unlock(&BTRFS_I(inode)->csum_mutex);
  402. cond_resched();
  403. next_sector:
  404. if (!eb_token ||
  405. (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) {
  406. int err;
  407. if (eb_token)
  408. unmap_extent_buffer(leaf, eb_token, KM_USER1);
  409. eb_token = NULL;
  410. err = map_private_extent_buffer(leaf, (unsigned long)item,
  411. BTRFS_CRC32_SIZE,
  412. &eb_token, &eb_map,
  413. &map_start, &map_len, KM_USER1);
  414. if (err)
  415. eb_token = NULL;
  416. }
  417. if (eb_token) {
  418. memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
  419. &sector_sum->sum, BTRFS_CRC32_SIZE);
  420. } else {
  421. write_extent_buffer(leaf, &sector_sum->sum,
  422. (unsigned long)item, BTRFS_CRC32_SIZE);
  423. }
  424. total_bytes += root->sectorsize;
  425. sector_sum++;
  426. if (total_bytes < sums->len) {
  427. item = (struct btrfs_csum_item *)((char *)item +
  428. BTRFS_CRC32_SIZE);
  429. if (item < item_end && offset + PAGE_CACHE_SIZE ==
  430. sector_sum->offset) {
  431. offset = sector_sum->offset;
  432. goto next_sector;
  433. }
  434. }
  435. if (eb_token) {
  436. unmap_extent_buffer(leaf, eb_token, KM_USER1);
  437. eb_token = NULL;
  438. }
  439. btrfs_mark_buffer_dirty(path->nodes[0]);
  440. cond_resched();
  441. if (total_bytes < sums->len) {
  442. btrfs_release_path(root, path);
  443. goto again;
  444. }
  445. out:
  446. btrfs_free_path(path);
  447. return ret;
  448. fail_unlock:
  449. mutex_unlock(&BTRFS_I(inode)->csum_mutex);
  450. goto out;
  451. }
  452. int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
  453. struct btrfs_root *root, struct btrfs_path *path,
  454. u64 isize)
  455. {
  456. struct btrfs_key key;
  457. struct extent_buffer *leaf = path->nodes[0];
  458. int slot = path->slots[0];
  459. int ret;
  460. u32 new_item_size;
  461. u64 new_item_span;
  462. u64 blocks;
  463. btrfs_item_key_to_cpu(leaf, &key, slot);
  464. if (isize <= key.offset)
  465. return 0;
  466. new_item_span = isize - key.offset;
  467. blocks = (new_item_span + root->sectorsize - 1) >>
  468. root->fs_info->sb->s_blocksize_bits;
  469. new_item_size = blocks * BTRFS_CRC32_SIZE;
  470. if (new_item_size >= btrfs_item_size_nr(leaf, slot))
  471. return 0;
  472. ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
  473. BUG_ON(ret);
  474. return ret;
  475. }