file-item.c 10 KB

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