xref: /openbmc/linux/fs/btrfs/file-item.c (revision 5f39d397)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
191e1d2701SChris Mason #include "ctree.h"
20dee26a9fSChris Mason #include "disk-io.h"
219f5fae2fSChris Mason #include "transaction.h"
221de037a4SChris Mason #include "print-tree.h"
231e1d2701SChris Mason 
246567e837SChris Mason #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
25a429e513SChris Mason 			       sizeof(struct btrfs_item) * 2) / \
26509659cdSChris Mason 			       BTRFS_CRC32_SIZE) - 1))
27b18c6685SChris Mason int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
28dee26a9fSChris Mason 			       struct btrfs_root *root,
29b18c6685SChris Mason 			       u64 objectid, u64 pos,
303a686375SChris Mason 			       u64 offset, u64 disk_num_blocks,
313a686375SChris Mason 			       u64 num_blocks)
329f5fae2fSChris Mason {
33dee26a9fSChris Mason 	int ret = 0;
34dee26a9fSChris Mason 	struct btrfs_file_extent_item *item;
35dee26a9fSChris Mason 	struct btrfs_key file_key;
365caf2a00SChris Mason 	struct btrfs_path *path;
375f39d397SChris Mason 	struct extent_buffer *leaf;
38dee26a9fSChris Mason 
395caf2a00SChris Mason 	path = btrfs_alloc_path();
405caf2a00SChris Mason 	BUG_ON(!path);
41dee26a9fSChris Mason 	file_key.objectid = objectid;
42b18c6685SChris Mason 	file_key.offset = pos;
43dee26a9fSChris Mason 	btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
44dee26a9fSChris Mason 
455caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
46dee26a9fSChris Mason 				      sizeof(*item));
4754aa1f4dSChris Mason 	if (ret < 0)
4854aa1f4dSChris Mason 		goto out;
499773a788SChris Mason 	BUG_ON(ret);
505f39d397SChris Mason 	leaf = path->nodes[0];
515f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0],
52dee26a9fSChris Mason 			      struct btrfs_file_extent_item);
535f39d397SChris Mason 	btrfs_set_file_extent_disk_blocknr(leaf, item, offset);
545f39d397SChris Mason 	btrfs_set_file_extent_disk_num_blocks(leaf, item, disk_num_blocks);
555f39d397SChris Mason 	btrfs_set_file_extent_offset(leaf, item, 0);
565f39d397SChris Mason 	btrfs_set_file_extent_num_blocks(leaf, item, num_blocks);
575f39d397SChris Mason 	btrfs_set_file_extent_generation(leaf, item, trans->transid);
585f39d397SChris Mason 	btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
595f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
6054aa1f4dSChris Mason out:
615caf2a00SChris Mason 	btrfs_free_path(path);
6254aa1f4dSChris Mason 	return ret;
639f5fae2fSChris Mason }
64dee26a9fSChris Mason 
65b18c6685SChris Mason struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
66b18c6685SChris Mason 					  struct btrfs_root *root,
676567e837SChris Mason 					  struct btrfs_path *path,
68b18c6685SChris Mason 					  u64 objectid, u64 offset,
69b18c6685SChris Mason 					  int cow)
706567e837SChris Mason {
716567e837SChris Mason 	int ret;
726567e837SChris Mason 	struct btrfs_key file_key;
736567e837SChris Mason 	struct btrfs_key found_key;
746567e837SChris Mason 	struct btrfs_csum_item *item;
755f39d397SChris Mason 	struct extent_buffer *leaf;
766567e837SChris Mason 	u64 csum_offset = 0;
77a429e513SChris Mason 	int csums_in_item;
786567e837SChris Mason 
796567e837SChris Mason 	file_key.objectid = objectid;
806567e837SChris Mason 	file_key.offset = offset;
816567e837SChris Mason 	btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
82b18c6685SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
836567e837SChris Mason 	if (ret < 0)
846567e837SChris Mason 		goto fail;
855f39d397SChris Mason 	leaf = path->nodes[0];
866567e837SChris Mason 	if (ret > 0) {
876567e837SChris Mason 		ret = 1;
8870b2befdSChris Mason 		if (path->slots[0] == 0)
896567e837SChris Mason 			goto fail;
906567e837SChris Mason 		path->slots[0]--;
915f39d397SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
926567e837SChris Mason 		if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
936567e837SChris Mason 		    found_key.objectid != objectid) {
946567e837SChris Mason 			goto fail;
956567e837SChris Mason 		}
966567e837SChris Mason 		csum_offset = (offset - found_key.offset) >>
976567e837SChris Mason 				root->fs_info->sb->s_blocksize_bits;
985f39d397SChris Mason 		csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
99509659cdSChris Mason 		csums_in_item /= BTRFS_CRC32_SIZE;
100a429e513SChris Mason 
101a429e513SChris Mason 		if (csum_offset >= csums_in_item) {
102a429e513SChris Mason 			ret = -EFBIG;
1036567e837SChris Mason 			goto fail;
1046567e837SChris Mason 		}
1056567e837SChris Mason 	}
1066567e837SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
107509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
108509659cdSChris Mason 					  csum_offset * BTRFS_CRC32_SIZE);
1096567e837SChris Mason 	return item;
1106567e837SChris Mason fail:
1116567e837SChris Mason 	if (ret > 0)
112b18c6685SChris Mason 		ret = -ENOENT;
1136567e837SChris Mason 	return ERR_PTR(ret);
1146567e837SChris Mason }
1156567e837SChris Mason 
1166567e837SChris Mason 
117dee26a9fSChris Mason int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
118dee26a9fSChris Mason 			     struct btrfs_root *root,
119dee26a9fSChris Mason 			     struct btrfs_path *path, u64 objectid,
1209773a788SChris Mason 			     u64 offset, int mod)
121dee26a9fSChris Mason {
122dee26a9fSChris Mason 	int ret;
123dee26a9fSChris Mason 	struct btrfs_key file_key;
124dee26a9fSChris Mason 	int ins_len = mod < 0 ? -1 : 0;
125dee26a9fSChris Mason 	int cow = mod != 0;
126dee26a9fSChris Mason 
127dee26a9fSChris Mason 	file_key.objectid = objectid;
12870b2befdSChris Mason 	file_key.offset = offset;
129dee26a9fSChris Mason 	btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
130dee26a9fSChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
131dee26a9fSChris Mason 	return ret;
132dee26a9fSChris Mason }
133f254e52cSChris Mason 
134f254e52cSChris Mason int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
135f254e52cSChris Mason 			  struct btrfs_root *root,
136f254e52cSChris Mason 			  u64 objectid, u64 offset,
137f254e52cSChris Mason 			  char *data, size_t len)
138f254e52cSChris Mason {
1395f39d397SChris Mason 	return 0;
1405f39d397SChris Mason #if 0
141f254e52cSChris Mason 	int ret;
142f254e52cSChris Mason 	struct btrfs_key file_key;
1436567e837SChris Mason 	struct btrfs_key found_key;
1445caf2a00SChris Mason 	struct btrfs_path *path;
145f254e52cSChris Mason 	struct btrfs_csum_item *item;
1465f39d397SChris Mason 	struct extent_buffer *leaf;
1476567e837SChris Mason 	u64 csum_offset;
148f254e52cSChris Mason 
1495caf2a00SChris Mason 	path = btrfs_alloc_path();
1505caf2a00SChris Mason 	BUG_ON(!path);
151b18c6685SChris Mason 
152f254e52cSChris Mason 	file_key.objectid = objectid;
153f254e52cSChris Mason 	file_key.offset = offset;
154f254e52cSChris Mason 	file_key.flags = 0;
155f254e52cSChris Mason 	btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
156a429e513SChris Mason 
157a429e513SChris Mason 	item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
158a429e513SChris Mason 	if (!IS_ERR(item))
159a429e513SChris Mason 		goto found;
160a429e513SChris Mason 	ret = PTR_ERR(item);
161a429e513SChris Mason 	if (ret == -EFBIG) {
162a429e513SChris Mason 		u32 item_size;
163a429e513SChris Mason 		/* we found one, but it isn't big enough yet */
1645f39d397SChris Mason 		leaf = path->nodes[0];
1655f39d397SChris Mason 		item_size = btrfs_item_size_nr(leaf, path->slots[0]);
166509659cdSChris Mason 		if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
167a429e513SChris Mason 			/* already at max size, make a new one */
168a429e513SChris Mason 			goto insert;
169a429e513SChris Mason 		}
170a429e513SChris Mason 	} else {
171a429e513SChris Mason 		/* we didn't find a csum item, insert one */
172a429e513SChris Mason 		goto insert;
173a429e513SChris Mason 	}
174a429e513SChris Mason 
175a429e513SChris Mason 	/*
176a429e513SChris Mason 	 * at this point, we know the tree has an item, but it isn't big
177a429e513SChris Mason 	 * enough yet to put our csum in.  Grow it
178a429e513SChris Mason 	 */
179a429e513SChris Mason 	btrfs_release_path(root, path);
1806567e837SChris Mason 	ret = btrfs_search_slot(trans, root, &file_key, path,
181509659cdSChris Mason 				BTRFS_CRC32_SIZE, 1);
1826567e837SChris Mason 	if (ret < 0)
1836567e837SChris Mason 		goto fail;
1846567e837SChris Mason 	if (ret == 0) {
185b18c6685SChris Mason 		BUG();
1866567e837SChris Mason 	}
1876567e837SChris Mason 	if (path->slots[0] == 0) {
1886567e837SChris Mason 		goto insert;
1896567e837SChris Mason 	}
1906567e837SChris Mason 	path->slots[0]--;
1915f39d397SChris Mason 	leaf = path->nodes[0];
1925f39d397SChris Mason 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1936567e837SChris Mason 	csum_offset = (offset - found_key.offset) >>
1946567e837SChris Mason 			root->fs_info->sb->s_blocksize_bits;
1956567e837SChris Mason 	if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
1966567e837SChris Mason 	    found_key.objectid != objectid ||
1976567e837SChris Mason 	    csum_offset >= MAX_CSUM_ITEMS(root)) {
1986567e837SChris Mason 		goto insert;
1996567e837SChris Mason 	}
2005f39d397SChris Mason 	if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
201509659cdSChris Mason 	    BTRFS_CRC32_SIZE) {
202509659cdSChris Mason 		u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
2035f39d397SChris Mason 		diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
2043a686375SChris Mason 		if (diff != BTRFS_CRC32_SIZE)
2053a686375SChris Mason 			goto insert;
206a429e513SChris Mason 		ret = btrfs_extend_item(trans, root, path, diff);
2076567e837SChris Mason 		BUG_ON(ret);
2086567e837SChris Mason 		goto csum;
2096567e837SChris Mason 	}
2106567e837SChris Mason 
2116567e837SChris Mason insert:
212a429e513SChris Mason 	btrfs_release_path(root, path);
2136567e837SChris Mason 	csum_offset = 0;
2145caf2a00SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
215509659cdSChris Mason 				      BTRFS_CRC32_SIZE);
21654aa1f4dSChris Mason 	if (ret < 0)
21754aa1f4dSChris Mason 		goto fail;
218a429e513SChris Mason 	if (ret != 0) {
219a429e513SChris Mason 		WARN_ON(1);
220f254e52cSChris Mason 		goto fail;
221a429e513SChris Mason 	}
2226567e837SChris Mason csum:
2235f39d397SChris Mason 	leaf = path->nodes[0];
2245f39d397SChris Mason 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
225f254e52cSChris Mason 	ret = 0;
226509659cdSChris Mason 	item = (struct btrfs_csum_item *)((unsigned char *)item +
227509659cdSChris Mason 					  csum_offset * BTRFS_CRC32_SIZE);
228b18c6685SChris Mason found:
2295f39d397SChris Mason 	/* FIXME!!!!!!!!!!!! */
230509659cdSChris Mason 	ret = btrfs_csum_data(root, data, len, &item->csum);
2315caf2a00SChris Mason 	btrfs_mark_buffer_dirty(path->nodes[0]);
232f254e52cSChris Mason fail:
2335caf2a00SChris Mason 	btrfs_release_path(root, path);
2345caf2a00SChris Mason 	btrfs_free_path(path);
235f254e52cSChris Mason 	return ret;
2365f39d397SChris Mason #endif
237f254e52cSChris Mason }
238f254e52cSChris Mason 
2391de037a4SChris Mason int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
2401de037a4SChris Mason 			struct btrfs_root *root, struct btrfs_path *path,
2411de037a4SChris Mason 			u64 isize)
2421de037a4SChris Mason {
2431de037a4SChris Mason 	struct btrfs_key key;
2445f39d397SChris Mason 	struct extent_buffer *leaf = path->nodes[0];
2451de037a4SChris Mason 	int slot = path->slots[0];
2461de037a4SChris Mason 	int ret;
2471de037a4SChris Mason 	u32 new_item_size;
2481de037a4SChris Mason 	u64 new_item_span;
2491de037a4SChris Mason 	u64 blocks;
2501de037a4SChris Mason 
2515f39d397SChris Mason 	btrfs_item_key_to_cpu(leaf, &key, slot);
2521de037a4SChris Mason 	if (isize <= key.offset)
2531de037a4SChris Mason 		return 0;
2541de037a4SChris Mason 	new_item_span = isize - key.offset;
2555f39d397SChris Mason 	blocks = (new_item_span + root->sectorsize - 1) >>
25684f54cfaSChris Mason 		root->fs_info->sb->s_blocksize_bits;
2571de037a4SChris Mason 	new_item_size = blocks * BTRFS_CRC32_SIZE;
2585f39d397SChris Mason 	if (new_item_size >= btrfs_item_size_nr(leaf, slot))
2591de037a4SChris Mason 		return 0;
2601de037a4SChris Mason 	ret = btrfs_truncate_item(trans, root, path, new_item_size);
2611de037a4SChris Mason 	BUG_ON(ret);
2621de037a4SChris Mason 	return ret;
2631de037a4SChris Mason }
2641de037a4SChris Mason 
265