Use device uuids when scanning devices
[btrfs-progs-unstable.git] / file-item.c
blob4cbc2a5750c827993068fb710ea841713d32a209
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "transaction.h"
26 #include "print-tree.h"
27 #include "crc32c.h"
29 #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
30 sizeof(struct btrfs_item) * 2) / \
31 BTRFS_CRC32_SIZE) - 1))
32 int btrfs_create_file(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, u64 dirid, u64 *objectid)
35 return 0;
38 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
39 struct btrfs_root *root,
40 u64 objectid, u64 pos, u64 offset,
41 u64 disk_num_bytes, u64 num_bytes)
43 int ret = 0;
44 struct btrfs_file_extent_item *item;
45 struct btrfs_key file_key;
46 struct btrfs_path *path;
47 struct extent_buffer *leaf;
49 path = btrfs_alloc_path();
50 BUG_ON(!path);
51 file_key.objectid = objectid;
52 file_key.offset = pos;
53 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
55 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
56 sizeof(*item));
57 if (ret < 0)
58 goto out;
59 BUG_ON(ret);
60 leaf = path->nodes[0];
61 item = btrfs_item_ptr(leaf, path->slots[0],
62 struct btrfs_file_extent_item);
63 btrfs_set_file_extent_disk_bytenr(leaf, item, offset);
64 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
65 btrfs_set_file_extent_offset(leaf, item, 0);
66 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
67 btrfs_set_file_extent_generation(leaf, item, trans->transid);
68 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
69 btrfs_mark_buffer_dirty(leaf);
70 out:
71 btrfs_free_path(path);
72 return ret;
75 int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
76 struct btrfs_root *root, u64 objectid,
77 u64 offset, char *buffer, size_t size)
79 struct btrfs_key key;
80 struct btrfs_path *path;
81 struct extent_buffer *leaf;
82 unsigned long ptr;
83 struct btrfs_file_extent_item *ei;
84 u32 datasize;
85 int err = 0;
86 int ret;
88 path = btrfs_alloc_path();
89 if (!path)
90 return -ENOMEM;
92 key.objectid = objectid;
93 key.offset = offset;
94 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
96 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
97 if (ret < 0) {
98 err = ret;
99 goto fail;
101 if (ret == 1) {
102 struct btrfs_key found_key;
104 if (path->slots[0] == 0)
105 goto insert;
107 path->slots[0]--;
108 leaf = path->nodes[0];
109 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
111 if (found_key.objectid != objectid)
112 goto insert;
114 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
115 goto insert;
116 ei = btrfs_item_ptr(leaf, path->slots[0],
117 struct btrfs_file_extent_item);
119 if (btrfs_file_extent_type(leaf, ei) !=
120 BTRFS_FILE_EXTENT_INLINE) {
121 goto insert;
123 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
124 ret = 0;
126 if (ret == 0) {
127 u32 found_size;
128 u64 found_end;
130 leaf = path->nodes[0];
131 ei = btrfs_item_ptr(leaf, path->slots[0],
132 struct btrfs_file_extent_item);
134 if (btrfs_file_extent_type(leaf, ei) !=
135 BTRFS_FILE_EXTENT_INLINE) {
136 err = ret;
137 btrfs_print_leaf(root, leaf);
138 printk("found wasn't inline offset %llu inode %llu\n",
139 (unsigned long long)offset,
140 (unsigned long long)objectid);
141 goto fail;
143 found_size = btrfs_file_extent_inline_len(leaf,
144 btrfs_item_nr(leaf, path->slots[0]));
145 found_end = key.offset + found_size;
147 if (found_end < offset + size) {
148 btrfs_release_path(root, path);
149 ret = btrfs_search_slot(trans, root, &key, path,
150 offset + size - found_end, 1);
151 BUG_ON(ret != 0);
153 ret = btrfs_extend_item(trans, root, path,
154 offset + size - found_end);
155 if (ret) {
156 err = ret;
157 goto fail;
159 leaf = path->nodes[0];
160 ei = btrfs_item_ptr(leaf, path->slots[0],
161 struct btrfs_file_extent_item);
163 if (found_end < offset) {
164 ptr = btrfs_file_extent_inline_start(ei) + found_size;
165 memset_extent_buffer(leaf, 0, ptr, offset - found_end);
167 } else {
168 insert:
169 btrfs_release_path(root, path);
170 datasize = offset + size - key.offset;
171 datasize = btrfs_file_extent_calc_inline_size(datasize);
172 ret = btrfs_insert_empty_item(trans, root, path, &key,
173 datasize);
174 if (ret) {
175 err = ret;
176 printk("got bad ret %d\n", ret);
177 goto fail;
179 leaf = path->nodes[0];
180 ei = btrfs_item_ptr(leaf, path->slots[0],
181 struct btrfs_file_extent_item);
182 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
183 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
185 ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
186 write_extent_buffer(leaf, buffer, ptr, size);
187 btrfs_mark_buffer_dirty(leaf);
188 fail:
189 btrfs_free_path(path);
190 return err;
193 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
194 struct btrfs_root *root,
195 struct btrfs_path *path,
196 u64 objectid, u64 offset, int cow)
198 int ret;
199 struct btrfs_key file_key;
200 struct btrfs_key found_key;
201 struct btrfs_csum_item *item;
202 struct extent_buffer *leaf;
203 u64 csum_offset = 0;
204 int csums_in_item;
206 file_key.objectid = objectid;
207 file_key.offset = offset;
208 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
209 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
210 if (ret < 0)
211 goto fail;
212 leaf = path->nodes[0];
213 if (ret > 0) {
214 ret = 1;
215 if (path->slots[0] == 0)
216 goto fail;
217 path->slots[0]--;
218 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
219 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
220 found_key.objectid != objectid) {
221 goto fail;
223 csum_offset = (offset - found_key.offset) >> root->sectorsize;
224 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
225 csums_in_item /= BTRFS_CRC32_SIZE;
227 if (csum_offset >= csums_in_item) {
228 ret = -EFBIG;
229 goto fail;
232 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
233 item = (struct btrfs_csum_item *)((unsigned char *)item +
234 csum_offset * BTRFS_CRC32_SIZE);
235 return item;
236 fail:
237 if (ret > 0)
238 ret = -ENOENT;
239 return ERR_PTR(ret);
243 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
244 struct btrfs_root *root,
245 struct btrfs_path *path, u64 objectid,
246 u64 offset, int mod)
248 int ret;
249 struct btrfs_key file_key;
250 int ins_len = mod < 0 ? -1 : 0;
251 int cow = mod != 0;
253 file_key.objectid = objectid;
254 file_key.offset = offset;
255 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
256 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
257 return ret;
260 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
261 struct btrfs_root *root,
262 struct btrfs_inode_item *inode,
263 u64 objectid, u64 offset,
264 char *data, size_t len)
266 int ret;
267 struct btrfs_key file_key;
268 struct btrfs_key found_key;
269 u64 next_offset = (u64)-1;
270 int found_next = 0;
271 struct btrfs_path *path;
272 struct btrfs_csum_item *item;
273 struct extent_buffer *leaf = NULL;
274 u64 csum_offset;
275 u32 csum_result = ~(u32)0;
276 u32 nritems;
277 u32 ins_size;
279 path = btrfs_alloc_path();
280 BUG_ON(!path);
282 file_key.objectid = objectid;
283 file_key.offset = offset;
284 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
286 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
287 if (!IS_ERR(item)) {
288 leaf = path->nodes[0];
289 goto found;
291 ret = PTR_ERR(item);
292 if (ret == -EFBIG) {
293 u32 item_size;
294 /* we found one, but it isn't big enough yet */
295 leaf = path->nodes[0];
296 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
297 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
298 /* already at max size, make a new one */
299 goto insert;
301 } else {
302 int slot = path->slots[0] + 1;
303 /* we didn't find a csum item, insert one */
304 nritems = btrfs_header_nritems(path->nodes[0]);
305 if (path->slots[0] >= nritems - 1) {
306 ret = btrfs_next_leaf(root, path);
307 if (ret == 1)
308 found_next = 1;
309 if (ret != 0)
310 goto insert;
311 slot = 0;
313 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
314 if (found_key.objectid != objectid ||
315 found_key.type != BTRFS_CSUM_ITEM_KEY) {
316 found_next = 1;
317 goto insert;
319 next_offset = found_key.offset;
320 found_next = 1;
321 goto insert;
325 * at this point, we know the tree has an item, but it isn't big
326 * enough yet to put our csum in. Grow it
328 btrfs_release_path(root, path);
329 ret = btrfs_search_slot(trans, root, &file_key, path,
330 BTRFS_CRC32_SIZE, 1);
331 if (ret < 0)
332 goto fail;
333 if (ret == 0) {
334 BUG();
336 if (path->slots[0] == 0) {
337 goto insert;
339 path->slots[0]--;
340 leaf = path->nodes[0];
341 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
342 csum_offset = (offset - found_key.offset) / root->sectorsize;
343 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
344 found_key.objectid != objectid ||
345 csum_offset >= MAX_CSUM_ITEMS(root)) {
346 goto insert;
348 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
349 BTRFS_CRC32_SIZE) {
350 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
351 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
352 if (diff != BTRFS_CRC32_SIZE)
353 goto insert;
354 ret = btrfs_extend_item(trans, root, path, diff);
355 BUG_ON(ret);
356 goto csum;
359 insert:
360 btrfs_release_path(root, path);
361 csum_offset = 0;
362 if (found_next) {
363 u64 tmp = min(btrfs_stack_inode_size(inode), next_offset);
364 tmp -= offset & ~((u64)root->sectorsize -1);
365 tmp /= root->sectorsize;
366 tmp = max((u64)1, tmp);
367 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
368 ins_size = BTRFS_CRC32_SIZE * tmp;
369 } else {
370 ins_size = BTRFS_CRC32_SIZE;
372 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
373 ins_size);
374 if (ret < 0)
375 goto fail;
376 if (ret != 0) {
377 WARN_ON(1);
378 goto fail;
380 csum:
381 leaf = path->nodes[0];
382 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
383 ret = 0;
384 item = (struct btrfs_csum_item *)((unsigned char *)item +
385 csum_offset * BTRFS_CRC32_SIZE);
386 found:
387 csum_result = btrfs_csum_data(root, data, csum_result, len);
388 btrfs_csum_final(csum_result, (char *)&csum_result);
389 if (csum_result == 0) {
390 printk("csum result is 0 for inode %llu offset %llu\n",
391 (unsigned long long)objectid,
392 (unsigned long long)offset);
395 write_extent_buffer(leaf, &csum_result, (unsigned long)item,
396 BTRFS_CRC32_SIZE);
397 btrfs_mark_buffer_dirty(path->nodes[0]);
398 fail:
399 btrfs_release_path(root, path);
400 btrfs_free_path(path);
401 return ret;
404 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
405 struct btrfs_root *root, struct btrfs_path *path,
406 u64 isize)
408 struct btrfs_key key;
409 struct extent_buffer *leaf = path->nodes[0];
410 int slot = path->slots[0];
411 int ret;
412 u32 new_item_size;
413 u64 new_item_span;
414 u64 blocks;
416 btrfs_item_key_to_cpu(leaf, &key, slot);
417 if (isize <= key.offset)
418 return 0;
419 new_item_span = isize - key.offset;
420 blocks = (new_item_span + root->sectorsize - 1) / root->sectorsize;
421 new_item_size = blocks * BTRFS_CRC32_SIZE;
422 if (new_item_size >= btrfs_item_size_nr(leaf, slot))
423 return 0;
424 ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
425 BUG_ON(ret);
426 return ret;