btrfs-progs: convert: Rework rollback
[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_key key;
187         struct btrfs_path path;
188         struct extent_buffer *leaf;
189         struct btrfs_inode_item *ii;
190         u64 isize;
191         int no_holes = btrfs_fs_incompat(root->fs_info, NO_HOLES);
192         int slot;
193         int read = 0;
194         int ret;
195
196         if (!IS_ALIGNED(start, root->sectorsize) ||
197             !IS_ALIGNED(len, root->sectorsize)) {
198                 warning("@start and @len must be aligned to %u for function %s",
199                         root->sectorsize, __func__);
200                 return -EINVAL;
201         }
202
203         btrfs_init_path(&path);
204         key.objectid = ino;
205         key.offset = start;
206         key.type = BTRFS_EXTENT_DATA_KEY;
207
208         ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
209         if (ret < 0)
210                 goto out;
211
212         if (ret > 0) {
213                 ret = btrfs_previous_item(root, &path, ino, BTRFS_EXTENT_DATA_KEY);
214                 if (ret > 0) {
215                         ret = -ENOENT;
216                         goto out;
217                 }
218         }
219
220         /*
221          * Reset @dest to all 0, so we don't need to care about holes in
222          * no_hole mode, but focus on reading non-hole part.
223          */
224         memset(dest, 0, len);
225         while (1) {
226                 struct btrfs_file_extent_item *fi;
227                 u64 extent_start;
228                 u64 extent_len;
229                 u64 read_start;
230                 u64 read_len;
231                 u64 read_len_ret;
232                 u64 disk_bytenr;
233
234                 leaf = path.nodes[0];
235                 slot = path.slots[0];
236
237                 btrfs_item_key_to_cpu(leaf, &key, slot);
238                 if (key.objectid > ino)
239                         break;
240                 if (key.type != BTRFS_EXTENT_DATA_KEY || key.objectid != ino)
241                         goto next;
242
243                 extent_start = key.offset;
244                 if (extent_start >= start + len)
245                         break;
246
247                 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
248                 if (btrfs_file_extent_compression(leaf, fi) !=
249                     BTRFS_COMPRESS_NONE) {
250                         ret = -ENOTTY;
251                         break;
252                 }
253
254                 /* Inline extent, one inode should only one inline extent */
255                 if (btrfs_file_extent_type(leaf, fi) ==
256                     BTRFS_FILE_EXTENT_INLINE) {
257                         extent_len = btrfs_file_extent_inline_len(leaf, slot,
258                                                                   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, root->sectorsize);
264                         break;
265                 }
266
267                 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
268                 if (extent_start + extent_len <= start)
269                         goto next;
270
271                 read_start = max(start, extent_start);
272                 read_len = min(start + len, extent_start + extent_len) -
273                            read_start;
274
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;
281                 }
282
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(root, 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;
299                 }
300         }
301
302         /*
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
306          */
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;
318                 }
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                                  root->sectorsize);
323                 read = min_t(u64, isize - start, len);
324         }
325 out:
326         btrfs_release_path(&path);
327         if (!ret)
328                 ret = read;
329         return ret;
330 }