btrfs-progs: mkfs: precreate the uuid tree
[platform/upstream/btrfs-progs.git] / file.c
1 /*
2  * Copyright (C) 2014 Fujitsu.  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
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"
25
26 /*
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.
31  *
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.
35  */
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)
40 {
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;
47
48         key.objectid = ino;
49         key.type = BTRFS_EXTENT_DATA_KEY;
50         key.offset = offset;
51
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;
64         }
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;
69
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);
74         /*
75          * existing file extent
76          * |--------|     |----|
77          *      |-------|
78          *      offset + len
79          * OR
80          * |---------------|
81          *      |-------|
82          */
83         if (end > offset) {
84                 not_found = 0;
85                 goto out;
86         }
87 check_next:
88         ret = btrfs_next_item(root, path);
89         if (ret)
90                 goto out;
91
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;
97         }
98         if (found_key.offset < offset + len)
99                 /*
100                  * existing file extent
101                  * |---|        |------|
102                  *      |-------|
103                  *      offset + len
104                  */
105                 not_found = 0;
106         else
107                 /*
108                  * existing file extent
109                  * |----|               |----|
110                  *              |----|
111                  *              offset + len
112                  */
113                 not_found = 1;
114
115         /*
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.
119          */
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]);
126         }
127
128 out:
129         if (ret == 0)
130                 ret = not_found;
131         return ret;
132 }
133
134 /*
135  * Punch hole ranged [offset,len) for the file given by ino and root.
136  *
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.
140  */
141 int btrfs_punch_hole(struct btrfs_trans_handle *trans,
142                      struct btrfs_root *root,
143                      u64 ino, u64 offset, u64 len)
144 {
145         struct btrfs_path *path;
146         int ret = 0;
147
148         path = btrfs_alloc_path();
149         if (!path)
150                 return -ENOMEM;
151
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;
158         }
159
160         ret = btrfs_insert_file_extent(trans, root, ino, offset, 0, 0, len);
161 out:
162         btrfs_free_path(path);
163         return ret;
164 }
165
166 /*
167  * Read out content of one inode.
168  *
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
174  *
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
179  *
180  * Return < 0 for fatal error during read.
181  * Otherwise return the number of succesfully read data in bytes.
182  */
183 int btrfs_read_file(struct btrfs_root *root, u64 ino, u64 start, int len,
184                     char *dest)
185 {
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;
196
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;
202         }
203
204         btrfs_init_path(&path);
205         key.objectid = ino;
206         key.offset = start;
207         key.type = BTRFS_EXTENT_DATA_KEY;
208
209         ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
210         if (ret < 0)
211                 goto out;
212
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;
218                 }
219         }
220
221         /*
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.
224          */
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;
234
235                 leaf = path.nodes[0];
236                 slot = path.slots[0];
237
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;
243
244                 extent_start = key.offset;
245                 if (extent_start >= start + len)
246                         break;
247
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;
253                 }
254
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_inline_len(leaf, slot,
259                                                                   fi);
260                         if (extent_start + extent_len <= start)
261                                 goto next;
262                         read_extent_buffer(leaf, dest,
263                                 btrfs_file_extent_inline_start(fi), extent_len);
264                         read += round_up(extent_len, fs_info->sectorsize);
265                         break;
266                 }
267
268                 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
269                 if (extent_start + extent_len <= start)
270                         goto next;
271
272                 read_start = max(start, extent_start);
273                 read_len = min(start + len, extent_start + extent_len) -
274                            read_start;
275
276                 /* We have already zeroed @dest, nothing to do */
277                 if (btrfs_file_extent_type(leaf, fi) ==
278                     BTRFS_FILE_EXTENT_PREALLOC ||
279                     btrfs_file_extent_disk_num_bytes(leaf, fi) == 0) {
280                         read += read_len;
281                         goto next;
282                 }
283
284                 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi) +
285                               btrfs_file_extent_offset(leaf, fi);
286                 read_len_ret = read_len;
287                 ret = read_extent_data(fs_info, dest + read_start - start, disk_bytenr,
288                                        &read_len_ret, 0);
289                 if (ret < 0)
290                         break;
291                 /* Short read, something went wrong */
292                 if (read_len_ret != read_len)
293                         return -EIO;
294                 read += read_len;
295 next:
296                 ret = btrfs_next_item(root, &path);
297                 if (ret > 0) {
298                         ret = 0;
299                         break;
300                 }
301         }
302
303         /*
304          * Special trick for no_holes, since for no_holes we don't have good
305          * method to account skipped and tailling holes, we used
306          * min(inode size, len) as return value
307          */
308         if (no_holes) {
309                 btrfs_release_path(&path);
310                 key.objectid = ino;
311                 key.offset = 0;
312                 key.type = BTRFS_INODE_ITEM_KEY;
313                 ret = btrfs_lookup_inode(NULL, root, &path, &key, 0);
314                 if (ret < 0)
315                         goto out;
316                 if (ret > 0) {
317                         ret = -ENOENT;
318                         goto out;
319                 }
320                 ii = btrfs_item_ptr(path.nodes[0], path.slots[0],
321                                     struct btrfs_inode_item);
322                 isize = round_up(btrfs_inode_size(path.nodes[0], ii),
323                                  fs_info->sectorsize);
324                 read = min_t(u64, isize - start, len);
325         }
326 out:
327         btrfs_release_path(&path);
328         if (!ret)
329                 ret = read;
330         return ret;
331 }