file-item.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_csum_one_bio(struct btrfs_root *root,
  127. struct bio *bio, struct btrfs_ordered_sum **sums_ret)
  128. {
  129. struct btrfs_ordered_sum *sums;
  130. struct btrfs_sector_sum *sector_sum;
  131. char *data;
  132. struct bio_vec *bvec = bio->bi_io_vec;
  133. int bio_index = 0;
  134. WARN_ON(bio->bi_vcnt <= 0);
  135. sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
  136. if (!sums)
  137. return -ENOMEM;
  138. *sums_ret = sums;
  139. sector_sum = &sums->sums;
  140. sums->file_offset = page_offset(bvec->bv_page);
  141. sums->len = bio->bi_size;
  142. INIT_LIST_HEAD(&sums->list);
  143. while(bio_index < bio->bi_vcnt) {
  144. data = kmap_atomic(bvec->bv_page, KM_USER0);
  145. sector_sum->sum = ~(u32)0;
  146. sector_sum->sum = btrfs_csum_data(root,
  147. data + bvec->bv_offset,
  148. sector_sum->sum,
  149. bvec->bv_len);
  150. kunmap_atomic(data, KM_USER0);
  151. btrfs_csum_final(sector_sum->sum,
  152. (char *)&sector_sum->sum);
  153. sector_sum->offset = page_offset(bvec->bv_page) +
  154. bvec->bv_offset;
  155. sector_sum++;
  156. bio_index++;
  157. bvec++;
  158. }
  159. return 0;
  160. }
  161. int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
  162. struct btrfs_root *root, struct inode *inode,
  163. struct btrfs_ordered_sum *sums)
  164. {
  165. u64 objectid = inode->i_ino;
  166. u64 offset;
  167. int ret;
  168. struct btrfs_key file_key;
  169. struct btrfs_key found_key;
  170. u64 next_offset;
  171. u64 total_bytes = 0;
  172. int found_next;
  173. struct btrfs_path *path;
  174. struct btrfs_csum_item *item;
  175. struct btrfs_csum_item *item_end;
  176. struct extent_buffer *leaf = NULL;
  177. u64 csum_offset;
  178. struct btrfs_sector_sum *sector_sum;
  179. u32 nritems;
  180. u32 ins_size;
  181. char *eb_map;
  182. char *eb_token;
  183. unsigned long map_len;
  184. unsigned long map_start;
  185. path = btrfs_alloc_path();
  186. BUG_ON(!path);
  187. sector_sum = &sums->sums;
  188. again:
  189. next_offset = (u64)-1;
  190. found_next = 0;
  191. offset = sector_sum->offset;
  192. file_key.objectid = objectid;
  193. file_key.offset = offset;
  194. btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
  195. item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
  196. if (!IS_ERR(item)) {
  197. leaf = path->nodes[0];
  198. goto found;
  199. }
  200. ret = PTR_ERR(item);
  201. if (ret == -EFBIG) {
  202. u32 item_size;
  203. /* we found one, but it isn't big enough yet */
  204. leaf = path->nodes[0];
  205. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  206. if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
  207. /* already at max size, make a new one */
  208. goto insert;
  209. }
  210. } else {
  211. int slot = path->slots[0] + 1;
  212. /* we didn't find a csum item, insert one */
  213. nritems = btrfs_header_nritems(path->nodes[0]);
  214. if (path->slots[0] >= nritems - 1) {
  215. ret = btrfs_next_leaf(root, path);
  216. if (ret == 1)
  217. found_next = 1;
  218. if (ret != 0)
  219. goto insert;
  220. slot = 0;
  221. }
  222. btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
  223. if (found_key.objectid != objectid ||
  224. found_key.type != BTRFS_CSUM_ITEM_KEY) {
  225. found_next = 1;
  226. goto insert;
  227. }
  228. next_offset = found_key.offset;
  229. found_next = 1;
  230. goto insert;
  231. }
  232. /*
  233. * at this point, we know the tree has an item, but it isn't big
  234. * enough yet to put our csum in. Grow it
  235. */
  236. btrfs_release_path(root, path);
  237. ret = btrfs_search_slot(trans, root, &file_key, path,
  238. BTRFS_CRC32_SIZE, 1);
  239. if (ret < 0)
  240. goto fail;
  241. if (ret == 0) {
  242. BUG();
  243. }
  244. if (path->slots[0] == 0) {
  245. goto insert;
  246. }
  247. path->slots[0]--;
  248. leaf = path->nodes[0];
  249. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  250. csum_offset = (offset - found_key.offset) >>
  251. root->fs_info->sb->s_blocksize_bits;
  252. if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
  253. found_key.objectid != objectid ||
  254. csum_offset >= MAX_CSUM_ITEMS(root)) {
  255. goto insert;
  256. }
  257. if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
  258. BTRFS_CRC32_SIZE) {
  259. u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
  260. diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
  261. if (diff != BTRFS_CRC32_SIZE)
  262. goto insert;
  263. ret = btrfs_extend_item(trans, root, path, diff);
  264. BUG_ON(ret);
  265. goto csum;
  266. }
  267. insert:
  268. btrfs_release_path(root, path);
  269. csum_offset = 0;
  270. if (found_next) {
  271. u64 tmp = min((u64)i_size_read(inode), next_offset);
  272. tmp -= offset & ~((u64)root->sectorsize -1);
  273. tmp >>= root->fs_info->sb->s_blocksize_bits;
  274. tmp = max((u64)1, tmp);
  275. tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
  276. ins_size = BTRFS_CRC32_SIZE * tmp;
  277. } else {
  278. ins_size = BTRFS_CRC32_SIZE;
  279. }
  280. ret = btrfs_insert_empty_item(trans, root, path, &file_key,
  281. ins_size);
  282. if (ret < 0)
  283. goto fail;
  284. if (ret != 0) {
  285. WARN_ON(1);
  286. goto fail;
  287. }
  288. csum:
  289. leaf = path->nodes[0];
  290. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  291. ret = 0;
  292. item = (struct btrfs_csum_item *)((unsigned char *)item +
  293. csum_offset * BTRFS_CRC32_SIZE);
  294. found:
  295. item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
  296. item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
  297. btrfs_item_size_nr(leaf, path->slots[0]));
  298. eb_token = NULL;
  299. next_sector:
  300. if (!eb_token ||
  301. (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) {
  302. int err;
  303. if (eb_token)
  304. unmap_extent_buffer(leaf, eb_token, KM_USER1);
  305. eb_token = NULL;
  306. err = map_private_extent_buffer(leaf, (unsigned long)item,
  307. BTRFS_CRC32_SIZE,
  308. &eb_token, &eb_map,
  309. &map_start, &map_len, KM_USER1);
  310. if (err)
  311. eb_token = NULL;
  312. }
  313. if (eb_token) {
  314. memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
  315. &sector_sum->sum, BTRFS_CRC32_SIZE);
  316. } else {
  317. write_extent_buffer(leaf, &sector_sum->sum,
  318. (unsigned long)item, BTRFS_CRC32_SIZE);
  319. }
  320. total_bytes += root->sectorsize;
  321. sector_sum++;
  322. if (total_bytes < sums->len) {
  323. item = (struct btrfs_csum_item *)((char *)item +
  324. BTRFS_CRC32_SIZE);
  325. if (item < item_end && offset + PAGE_CACHE_SIZE ==
  326. sector_sum->offset) {
  327. offset = sector_sum->offset;
  328. goto next_sector;
  329. }
  330. }
  331. if (eb_token) {
  332. unmap_extent_buffer(leaf, eb_token, KM_USER1);
  333. eb_token = NULL;
  334. }
  335. btrfs_mark_buffer_dirty(path->nodes[0]);
  336. if (total_bytes < sums->len) {
  337. btrfs_release_path(root, path);
  338. goto again;
  339. }
  340. fail:
  341. btrfs_free_path(path);
  342. return ret;
  343. }
  344. int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
  345. struct btrfs_root *root, struct btrfs_path *path,
  346. u64 isize)
  347. {
  348. struct btrfs_key key;
  349. struct extent_buffer *leaf = path->nodes[0];
  350. int slot = path->slots[0];
  351. int ret;
  352. u32 new_item_size;
  353. u64 new_item_span;
  354. u64 blocks;
  355. btrfs_item_key_to_cpu(leaf, &key, slot);
  356. if (isize <= key.offset)
  357. return 0;
  358. new_item_span = isize - key.offset;
  359. blocks = (new_item_span + root->sectorsize - 1) >>
  360. root->fs_info->sb->s_blocksize_bits;
  361. new_item_size = blocks * BTRFS_CRC32_SIZE;
  362. if (new_item_size >= btrfs_item_size_nr(leaf, slot))
  363. return 0;
  364. ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
  365. BUG_ON(ret);
  366. return ret;
  367. }