btrfs-progs: Fix NULL pointer when receive clone operation
[btrfs-progs-unstable/devel.git] / file.c
blobdd0b04b85126876cb4c199b1ce3ea6db66f89065
1 /*
2 * Copyright (C) 2014 Fujitsu. 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 <sys/stat.h>
20 #include "ctree.h"
21 #include "transaction.h"
22 #include "kerncompat.h"
25 * Get the first file extent that covers (part of) the given range
26 * Unlike kernel using extent_map to handle hole even no-hole is enabled,
27 * progs don't have such infrastructure, so caller should do extra care
28 * for no-hole.
30 * return 0 for found, and path points to the file extent.
31 * return >0 for not found, and path points to the insert position.
32 * return <0 for error.
34 int btrfs_get_extent(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct btrfs_path *path,
37 u64 ino, u64 offset, u64 len, int ins_len)
39 struct btrfs_key key;
40 struct btrfs_key found_key;
41 struct btrfs_file_extent_item *fi_item;
42 u64 end = 0;
43 int ret = 0;
44 int not_found = 1;
46 key.objectid = ino;
47 key.type = BTRFS_EXTENT_DATA_KEY;
48 key.offset = offset;
50 ret = btrfs_search_slot(trans, root, &key, path, ins_len,
51 ins_len ? 1 : 0);
52 if (ret <= 0)
53 goto out;
54 if (ret > 0) {
55 /* Check previous file extent */
56 ret = btrfs_previous_item(root, path, ino,
57 BTRFS_EXTENT_DATA_KEY);
58 if (ret < 0)
59 goto out;
60 if (ret > 0)
61 goto check_next;
63 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
64 if (found_key.objectid != ino ||
65 found_key.type != BTRFS_EXTENT_DATA_KEY)
66 goto check_next;
68 fi_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
69 struct btrfs_file_extent_item);
70 end = found_key.offset +
71 btrfs_file_extent_ram_bytes(path->nodes[0], fi_item);
73 * existing file extent
74 * |--------| |----|
75 * |-------|
76 * offset + len
77 * OR
78 * |---------------|
79 * |-------|
81 if (end > offset) {
82 not_found = 0;
83 goto out;
85 check_next:
86 ret = btrfs_next_item(root, path);
87 if (ret)
88 goto out;
90 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
91 if (found_key.objectid != ino ||
92 found_key.type != BTRFS_EXTENT_DATA_KEY) {
93 ret = 1;
94 goto out;
96 if (found_key.offset < offset + len)
98 * existing file extent
99 * |---| |------|
100 * |-------|
101 * offset + len
103 not_found = 0;
104 else
106 * existing file extent
107 * |----| |----|
108 * |----|
109 * offset + len
111 not_found = 1;
114 * To keep the search behavior consistent with search_slot(),
115 * we need to go back to the prev leaf's nritem slot if
116 * we are at the first slot of the leaf.
118 if (path->slots[0] == 0) {
119 ret = btrfs_prev_leaf(root, path);
120 /* Not possible */
121 if (ret)
122 goto out;
123 path->slots[0] = btrfs_header_nritems(path->nodes[0]);
126 out:
127 if (ret == 0)
128 ret = not_found;
129 return ret;
133 * Punch hole ranged [offset,len) for the file given by ino and root.
135 * Unlink kernel punch_hole, which will not zero/free existing extent,
136 * instead it will return -EEXIST if there is any extents in the hole
137 * range.
139 int btrfs_punch_hole(struct btrfs_trans_handle *trans,
140 struct btrfs_root *root,
141 u64 ino, u64 offset, u64 len)
143 struct btrfs_path *path;
144 int ret = 0;
146 path = btrfs_alloc_path();
147 if (!path)
148 return -ENOMEM;
150 ret = btrfs_get_extent(NULL, root, path, ino, offset, len, 0);
151 if (ret < 0)
152 goto out;
153 if (ret == 0) {
154 ret = -EEXIST;
155 goto out;
158 ret = btrfs_insert_file_extent(trans, root, ino, offset, 0, 0, len);
159 out:
160 btrfs_free_path(path);
161 return ret;