file-item.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. path->reada = 2;
  142. WARN_ON(bio->bi_vcnt <= 0);
  143. while(bio_index < bio->bi_vcnt) {
  144. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  145. ret = btrfs_find_ordered_sum(inode, offset, &sum);
  146. if (ret == 0)
  147. goto found;
  148. if (!item || offset < item_start_offset ||
  149. offset >= item_last_offset) {
  150. struct btrfs_key found_key;
  151. u32 item_size;
  152. if (item)
  153. btrfs_release_path(root, path);
  154. item = btrfs_lookup_csum(NULL, root, path,
  155. inode->i_ino, offset, 0);
  156. if (IS_ERR(item)) {
  157. ret = PTR_ERR(item);
  158. if (ret == -ENOENT || ret == -EFBIG)
  159. ret = 0;
  160. sum = 0;
  161. printk("no csum found for inode %lu start "
  162. "%llu\n", inode->i_ino,
  163. (unsigned long long)offset);
  164. goto found;
  165. }
  166. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  167. path->slots[0]);
  168. item_start_offset = found_key.offset;
  169. item_size = btrfs_item_size_nr(path->nodes[0],
  170. path->slots[0]);
  171. item_last_offset = item_start_offset +
  172. (item_size / BTRFS_CRC32_SIZE) *
  173. root->sectorsize;
  174. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  175. struct btrfs_csum_item);
  176. }
  177. /*
  178. * this byte range must be able to fit inside
  179. * a single leaf so it will also fit inside a u32
  180. */
  181. diff = offset - item_start_offset;
  182. diff = diff / root->sectorsize;
  183. diff = diff * BTRFS_CRC32_SIZE;
  184. read_extent_buffer(path->nodes[0], &sum,
  185. (unsigned long)item + diff,
  186. BTRFS_CRC32_SIZE);
  187. found:
  188. set_state_private(io_tree, offset, sum);
  189. bio_index++;
  190. bvec++;
  191. }
  192. btrfs_free_path(path);
  193. return 0;
  194. }
  195. int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
  196. struct bio *bio)
  197. {
  198. struct btrfs_ordered_sum *sums;
  199. struct btrfs_sector_sum *sector_sum;
  200. struct btrfs_ordered_extent *ordered;
  201. char *data;
  202. struct bio_vec *bvec = bio->bi_io_vec;
  203. int bio_index = 0;
  204. unsigned long total_bytes = 0;
  205. unsigned long this_sum_bytes = 0;
  206. u64 offset;
  207. WARN_ON(bio->bi_vcnt <= 0);
  208. sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
  209. if (!sums)
  210. return -ENOMEM;
  211. sector_sum = sums->sums;
  212. sums->file_offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  213. sums->len = bio->bi_size;
  214. INIT_LIST_HEAD(&sums->list);
  215. ordered = btrfs_lookup_ordered_extent(inode, sums->file_offset);
  216. BUG_ON(!ordered);
  217. while(bio_index < bio->bi_vcnt) {
  218. offset = page_offset(bvec->bv_page) + bvec->bv_offset;
  219. if (offset >= ordered->file_offset + ordered->len ||
  220. offset < ordered->file_offset) {
  221. unsigned long bytes_left;
  222. sums->len = this_sum_bytes;
  223. this_sum_bytes = 0;
  224. btrfs_add_ordered_sum(inode, ordered, sums);
  225. btrfs_put_ordered_extent(ordered);
  226. bytes_left = bio->bi_size - total_bytes;
  227. sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
  228. GFP_NOFS);
  229. BUG_ON(!sums);
  230. sector_sum = sums->sums;
  231. sums->len = bytes_left;
  232. sums->file_offset = offset;
  233. ordered = btrfs_lookup_ordered_extent(inode,
  234. sums->file_offset);
  235. BUG_ON(!ordered);
  236. }
  237. data = kmap_atomic(bvec->bv_page, KM_USER0);
  238. sector_sum->sum = ~(u32)0;
  239. sector_sum->sum = btrfs_csum_data(root,
  240. data + bvec->bv_offset,
  241. sector_sum->sum,
  242. bvec->bv_len);
  243. kunmap_atomic(data, KM_USER0);
  244. btrfs_csum_final(sector_sum->sum,
  245. (char *)&sector_sum->sum);
  246. sector_sum->offset = page_offset(bvec->bv_page) +
  247. bvec->bv_offset;
  248. sector_sum++;
  249. bio_index++;
  250. total_bytes += bvec->bv_len;
  251. this_sum_bytes += bvec->bv_len;
  252. bvec++;
  253. }
  254. this_sum_bytes = 0;
  255. btrfs_add_ordered_sum(inode, ordered, sums);
  256. btrfs_put_ordered_extent(ordered);
  257. return 0;
  258. }
  259. int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
  260. struct btrfs_root *root, struct inode *inode,
  261. struct btrfs_ordered_sum *sums)
  262. {
  263. u64 objectid = inode->i_ino;
  264. u64 offset;
  265. int ret;
  266. struct btrfs_key file_key;
  267. struct btrfs_key found_key;
  268. u64 next_offset;
  269. u64 total_bytes = 0;
  270. int found_next;
  271. struct btrfs_path *path;
  272. struct btrfs_csum_item *item;
  273. struct btrfs_csum_item *item_end;
  274. struct extent_buffer *leaf = NULL;
  275. u64 csum_offset;
  276. struct btrfs_sector_sum *sector_sum;
  277. u32 nritems;
  278. u32 ins_size;
  279. char *eb_map;
  280. char *eb_token;
  281. unsigned long map_len;
  282. unsigned long map_start;
  283. path = btrfs_alloc_path();
  284. BUG_ON(!path);
  285. sector_sum = sums->sums;
  286. again:
  287. next_offset = (u64)-1;
  288. found_next = 0;
  289. offset = sector_sum->offset;
  290. file_key.objectid = objectid;
  291. file_key.offset = offset;
  292. btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
  293. item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
  294. if (!IS_ERR(item)) {
  295. leaf = path->nodes[0];
  296. goto found;
  297. }
  298. ret = PTR_ERR(item);
  299. if (ret == -EFBIG) {
  300. u32 item_size;
  301. /* we found one, but it isn't big enough yet */
  302. leaf = path->nodes[0];
  303. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  304. if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
  305. /* already at max size, make a new one */
  306. goto insert;
  307. }
  308. } else {
  309. int slot = path->slots[0] + 1;
  310. /* we didn't find a csum item, insert one */
  311. nritems = btrfs_header_nritems(path->nodes[0]);
  312. if (path->slots[0] >= nritems - 1) {
  313. ret = btrfs_next_leaf(root, path);
  314. if (ret == 1)
  315. found_next = 1;
  316. if (ret != 0)
  317. goto insert;
  318. slot = 0;
  319. }
  320. btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
  321. if (found_key.objectid != objectid ||
  322. found_key.type != BTRFS_CSUM_ITEM_KEY) {
  323. found_next = 1;
  324. goto insert;
  325. }
  326. next_offset = found_key.offset;
  327. found_next = 1;
  328. goto insert;
  329. }
  330. /*
  331. * at this point, we know the tree has an item, but it isn't big
  332. * enough yet to put our csum in. Grow it
  333. */
  334. btrfs_release_path(root, path);
  335. ret = btrfs_search_slot(trans, root, &file_key, path,
  336. BTRFS_CRC32_SIZE, 1);
  337. if (ret < 0)
  338. goto fail;
  339. if (ret == 0) {
  340. BUG();
  341. }
  342. if (path->slots[0] == 0) {
  343. goto insert;
  344. }
  345. path->slots[0]--;
  346. leaf = path->nodes[0];
  347. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  348. csum_offset = (offset - found_key.offset) >>
  349. root->fs_info->sb->s_blocksize_bits;
  350. if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
  351. found_key.objectid != objectid ||
  352. csum_offset >= MAX_CSUM_ITEMS(root)) {
  353. goto insert;
  354. }
  355. if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
  356. BTRFS_CRC32_SIZE) {
  357. u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
  358. diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
  359. if (diff != BTRFS_CRC32_SIZE)
  360. goto insert;
  361. ret = btrfs_extend_item(trans, root, path, diff);
  362. BUG_ON(ret);
  363. goto csum;
  364. }
  365. insert:
  366. btrfs_release_path(root, path);
  367. csum_offset = 0;
  368. if (found_next) {
  369. u64 tmp = min((u64)i_size_read(inode), next_offset);
  370. tmp -= offset & ~((u64)root->sectorsize -1);
  371. tmp >>= root->fs_info->sb->s_blocksize_bits;
  372. tmp = max((u64)1, tmp);
  373. tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
  374. ins_size = BTRFS_CRC32_SIZE * tmp;
  375. } else {
  376. ins_size = BTRFS_CRC32_SIZE;
  377. }
  378. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  379. ins_size);
  380. if (ret < 0)
  381. goto fail;
  382. if (ret != 0) {
  383. WARN_ON(1);
  384. goto fail;
  385. }
  386. csum:
  387. leaf = path->nodes[0];
  388. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  389. ret = 0;
  390. item = (struct btrfs_csum_item *)((unsigned char *)item +
  391. csum_offset * BTRFS_CRC32_SIZE);
  392. found:
  393. item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  394. item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
  395. btrfs_item_size_nr(leaf, path->slots[0]));
  396. eb_token = NULL;
  397. next_sector:
  398. if (!eb_token ||
  399. (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) {
  400. int err;
  401. if (eb_token)
  402. unmap_extent_buffer(leaf, eb_token, KM_USER1);
  403. eb_token = NULL;
  404. err = map_private_extent_buffer(leaf, (unsigned long)item,
  405. BTRFS_CRC32_SIZE,
  406. &eb_token, &eb_map,
  407. &map_start, &map_len, KM_USER1);
  408. if (err)
  409. eb_token = NULL;
  410. }
  411. if (eb_token) {
  412. memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
  413. &sector_sum->sum, BTRFS_CRC32_SIZE);
  414. } else {
  415. write_extent_buffer(leaf, &sector_sum->sum,
  416. (unsigned long)item, BTRFS_CRC32_SIZE);
  417. }
  418. total_bytes += root->sectorsize;
  419. sector_sum++;
  420. if (total_bytes < sums->len) {
  421. item = (struct btrfs_csum_item *)((char *)item +
  422. BTRFS_CRC32_SIZE);
  423. if (item < item_end && offset + PAGE_CACHE_SIZE ==
  424. sector_sum->offset) {
  425. offset = sector_sum->offset;
  426. goto next_sector;
  427. }
  428. }
  429. if (eb_token) {
  430. unmap_extent_buffer(leaf, eb_token, KM_USER1);
  431. eb_token = NULL;
  432. }
  433. btrfs_mark_buffer_dirty(path->nodes[0]);
  434. if (total_bytes < sums->len) {
  435. btrfs_release_path(root, path);
  436. goto again;
  437. }
  438. fail:
  439. btrfs_free_path(path);
  440. return ret;
  441. }
  442. int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
  443. struct btrfs_root *root, struct btrfs_path *path,
  444. u64 isize)
  445. {
  446. struct btrfs_key key;
  447. struct extent_buffer *leaf = path->nodes[0];
  448. int slot = path->slots[0];
  449. int ret;
  450. u32 new_item_size;
  451. u64 new_item_span;
  452. u64 blocks;
  453. btrfs_item_key_to_cpu(leaf, &key, slot);
  454. if (isize <= key.offset)
  455. return 0;
  456. new_item_span = isize - key.offset;
  457. blocks = (new_item_span + root->sectorsize - 1) >>
  458. root->fs_info->sb->s_blocksize_bits;
  459. new_item_size = blocks * BTRFS_CRC32_SIZE;
  460. if (new_item_size >= btrfs_item_size_nr(leaf, slot))
  461. return 0;
  462. ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
  463. BUG_ON(ret);
  464. return ret;
  465. }