Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / file.c
blob056be1045814cd387f0f1631d345a9e5e6135d6e
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 "utils.h"
22 #include "disk-io.h"
23 #include "transaction.h"
24 #include "kerncompat.h"
27 * Get the first file extent that covers (part of) the given range
28 * Unlike kernel using extent_map to handle hole even no-hole is enabled,
29 * progs don't have such infrastructure, so caller should do extra care
30 * for no-hole.
32 * return 0 for found, and path points to the file extent.
33 * return >0 for not found, and path points to the insert position.
34 * return <0 for error.
36 int btrfs_get_extent(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct btrfs_path *path,
39 u64 ino, u64 offset, u64 len, int ins_len)
41 struct btrfs_key key;
42 struct btrfs_key found_key;
43 struct btrfs_file_extent_item *fi_item;
44 u64 end = 0;
45 int ret = 0;
46 int not_found = 1;
48 key.objectid = ino;
49 key.type = BTRFS_EXTENT_DATA_KEY;
50 key.offset = offset;
52 ret = btrfs_search_slot(trans, root, &key, path, ins_len,
53 ins_len ? 1 : 0);
54 if (ret <= 0)
55 goto out;
56 if (ret > 0) {
57 /* Check previous file extent */
58 ret = btrfs_previous_item(root, path, ino,
59 BTRFS_EXTENT_DATA_KEY);
60 if (ret < 0)
61 goto out;
62 if (ret > 0)
63 goto check_next;
65 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
66 if (found_key.objectid != ino ||
67 found_key.type != BTRFS_EXTENT_DATA_KEY)
68 goto check_next;
70 fi_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
71 struct btrfs_file_extent_item);
72 end = found_key.offset +
73 btrfs_file_extent_ram_bytes(path->nodes[0], fi_item);
75 * existing file extent
76 * |--------| |----|
77 * |-------|
78 * offset + len
79 * OR
80 * |---------------|
81 * |-------|
83 if (end > offset) {
84 not_found = 0;
85 goto out;
87 check_next:
88 ret = btrfs_next_item(root, path);
89 if (ret)
90 goto out;
92 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
93 if (found_key.objectid != ino ||
94 found_key.type != BTRFS_EXTENT_DATA_KEY) {
95 ret = 1;
96 goto out;
98 if (found_key.offset < offset + len)
100 * existing file extent
101 * |---| |------|
102 * |-------|
103 * offset + len
105 not_found = 0;
106 else
108 * existing file extent
109 * |----| |----|
110 * |----|
111 * offset + len
113 not_found = 1;
116 * To keep the search behavior consistent with search_slot(),
117 * we need to go back to the prev leaf's nritem slot if
118 * we are at the first slot of the leaf.
120 if (path->slots[0] == 0) {
121 ret = btrfs_prev_leaf(root, path);
122 /* Not possible */
123 if (ret)
124 goto out;
125 path->slots[0] = btrfs_header_nritems(path->nodes[0]);
128 out:
129 if (ret == 0)
130 ret = not_found;
131 return ret;
135 * Punch hole ranged [offset,len) for the file given by ino and root.
137 * Unlink kernel punch_hole, which will not zero/free existing extent,
138 * instead it will return -EEXIST if there is any extents in the hole
139 * range.
141 int btrfs_punch_hole(struct btrfs_trans_handle *trans,
142 struct btrfs_root *root,
143 u64 ino, u64 offset, u64 len)
145 struct btrfs_path *path;
146 int ret = 0;
148 path = btrfs_alloc_path();
149 if (!path)
150 return -ENOMEM;
152 ret = btrfs_get_extent(NULL, root, path, ino, offset, len, 0);
153 if (ret < 0)
154 goto out;
155 if (ret == 0) {
156 ret = -EEXIST;
157 goto out;
160 ret = btrfs_insert_file_extent(trans, root, ino, offset, 0, 0, len);
161 out:
162 btrfs_free_path(path);
163 return ret;
167 * Read out content of one inode.
169 * @root: fs/subvolume root containing the inode
170 * @ino: inode number
171 * @start: offset inside the file, aligned to sectorsize
172 * @len: length to read, aligned to sectorisize
173 * @dest: where data will be stored
175 * NOTE:
176 * 1) compression data is not supported yet
177 * 2) @start and @len must be aligned to sectorsize
178 * 3) data read out is also aligned to sectorsize, not truncated to inode size
180 * Return < 0 for fatal error during read.
181 * Otherwise return the number of succesfully read data in bytes.
183 int btrfs_read_file(struct btrfs_root *root, u64 ino, u64 start, int len,
184 char *dest)
186 struct btrfs_fs_info *fs_info = root->fs_info;
187 struct btrfs_key key;
188 struct btrfs_path path;
189 struct extent_buffer *leaf;
190 struct btrfs_inode_item *ii;
191 u64 isize;
192 int no_holes = btrfs_fs_incompat(fs_info, NO_HOLES);
193 int slot;
194 int read = 0;
195 int ret;
197 if (!IS_ALIGNED(start, fs_info->sectorsize) ||
198 !IS_ALIGNED(len, fs_info->sectorsize)) {
199 warning("@start and @len must be aligned to %u for function %s",
200 fs_info->sectorsize, __func__);
201 return -EINVAL;
204 btrfs_init_path(&path);
205 key.objectid = ino;
206 key.offset = start;
207 key.type = BTRFS_EXTENT_DATA_KEY;
209 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
210 if (ret < 0)
211 goto out;
213 if (ret > 0) {
214 ret = btrfs_previous_item(root, &path, ino, BTRFS_EXTENT_DATA_KEY);
215 if (ret > 0) {
216 ret = -ENOENT;
217 goto out;
222 * Reset @dest to all 0, so we don't need to care about holes in
223 * no_hole mode, but focus on reading non-hole part.
225 memset(dest, 0, len);
226 while (1) {
227 struct btrfs_file_extent_item *fi;
228 u64 extent_start;
229 u64 extent_len;
230 u64 read_start;
231 u64 read_len;
232 u64 read_len_ret;
233 u64 disk_bytenr;
235 leaf = path.nodes[0];
236 slot = path.slots[0];
238 btrfs_item_key_to_cpu(leaf, &key, slot);
239 if (key.objectid > ino)
240 break;
241 if (key.type != BTRFS_EXTENT_DATA_KEY || key.objectid != ino)
242 goto next;
244 extent_start = key.offset;
245 if (extent_start >= start + len)
246 break;
248 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
249 if (btrfs_file_extent_compression(leaf, fi) !=
250 BTRFS_COMPRESS_NONE) {
251 ret = -ENOTTY;
252 break;
255 /* Inline extent, one inode should only one inline extent */
256 if (btrfs_file_extent_type(leaf, fi) ==
257 BTRFS_FILE_EXTENT_INLINE) {
258 extent_len = btrfs_file_extent_ram_bytes(leaf, fi);
259 if (extent_start + extent_len <= start)
260 goto next;
261 read_extent_buffer(leaf, dest,
262 btrfs_file_extent_inline_start(fi), extent_len);
263 read += round_up(extent_len, fs_info->sectorsize);
264 break;
267 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
268 if (extent_start + extent_len <= start)
269 goto next;
271 read_start = max(start, extent_start);
272 read_len = min(start + len, extent_start + extent_len) -
273 read_start;
275 /* We have already zeroed @dest, nothing to do */
276 if (btrfs_file_extent_type(leaf, fi) ==
277 BTRFS_FILE_EXTENT_PREALLOC ||
278 btrfs_file_extent_disk_num_bytes(leaf, fi) == 0) {
279 read += read_len;
280 goto next;
283 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi) +
284 btrfs_file_extent_offset(leaf, fi);
285 read_len_ret = read_len;
286 ret = read_extent_data(fs_info, dest + read_start - start, disk_bytenr,
287 &read_len_ret, 0);
288 if (ret < 0)
289 break;
290 /* Short read, something went wrong */
291 if (read_len_ret != read_len)
292 return -EIO;
293 read += read_len;
294 next:
295 ret = btrfs_next_item(root, &path);
296 if (ret > 0) {
297 ret = 0;
298 break;
303 * Special trick for no_holes, since for no_holes we don't have good
304 * method to account skipped and tailling holes, we used
305 * min(inode size, len) as return value
307 if (no_holes) {
308 btrfs_release_path(&path);
309 key.objectid = ino;
310 key.offset = 0;
311 key.type = BTRFS_INODE_ITEM_KEY;
312 ret = btrfs_lookup_inode(NULL, root, &path, &key, 0);
313 if (ret < 0)
314 goto out;
315 if (ret > 0) {
316 ret = -ENOENT;
317 goto out;
319 ii = btrfs_item_ptr(path.nodes[0], path.slots[0],
320 struct btrfs_inode_item);
321 isize = round_up(btrfs_inode_size(path.nodes[0], ii),
322 fs_info->sectorsize);
323 read = min_t(u64, isize - start, len);
325 out:
326 btrfs_release_path(&path);
327 if (!ret)
328 ret = read;
329 return ret;