]>
Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
f46b5a66 CH |
2 | /* |
3 | * Copyright (C) 2007 Oracle. All rights reserved. | |
f46b5a66 CH |
4 | */ |
5 | ||
6 | #include <linux/kernel.h> | |
7 | #include <linux/bio.h> | |
f46b5a66 CH |
8 | #include <linux/file.h> |
9 | #include <linux/fs.h> | |
cb8e7090 | 10 | #include <linux/fsnotify.h> |
f46b5a66 CH |
11 | #include <linux/pagemap.h> |
12 | #include <linux/highmem.h> | |
13 | #include <linux/time.h> | |
f46b5a66 | 14 | #include <linux/string.h> |
f46b5a66 | 15 | #include <linux/backing-dev.h> |
cb8e7090 | 16 | #include <linux/mount.h> |
cb8e7090 | 17 | #include <linux/namei.h> |
f46b5a66 | 18 | #include <linux/writeback.h> |
f46b5a66 | 19 | #include <linux/compat.h> |
cb8e7090 | 20 | #include <linux/security.h> |
f46b5a66 | 21 | #include <linux/xattr.h> |
f54de068 | 22 | #include <linux/mm.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
f7039b1d | 24 | #include <linux/blkdev.h> |
8ea05e3a | 25 | #include <linux/uuid.h> |
55e301fd | 26 | #include <linux/btrfs.h> |
416161db | 27 | #include <linux/uaccess.h> |
ae5e165d | 28 | #include <linux/iversion.h> |
f46b5a66 CH |
29 | #include "ctree.h" |
30 | #include "disk-io.h" | |
31 | #include "transaction.h" | |
32 | #include "btrfs_inode.h" | |
f46b5a66 CH |
33 | #include "print-tree.h" |
34 | #include "volumes.h" | |
925baedd | 35 | #include "locking.h" |
581bb050 | 36 | #include "inode-map.h" |
d7728c96 | 37 | #include "backref.h" |
606686ee | 38 | #include "rcu-string.h" |
31db9f7c | 39 | #include "send.h" |
3f6bcfbd | 40 | #include "dev-replace.h" |
63541927 | 41 | #include "props.h" |
3b02a68a | 42 | #include "sysfs.h" |
fcebe456 | 43 | #include "qgroup.h" |
1ec9a1ae | 44 | #include "tree-log.h" |
ebb8765b | 45 | #include "compression.h" |
f46b5a66 | 46 | |
abccd00f HM |
47 | #ifdef CONFIG_64BIT |
48 | /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI | |
49 | * structures are incorrect, as the timespec structure from userspace | |
50 | * is 4 bytes too small. We define these alternatives here to teach | |
51 | * the kernel about the 32-bit struct packing. | |
52 | */ | |
53 | struct btrfs_ioctl_timespec_32 { | |
54 | __u64 sec; | |
55 | __u32 nsec; | |
56 | } __attribute__ ((__packed__)); | |
57 | ||
58 | struct btrfs_ioctl_received_subvol_args_32 { | |
59 | char uuid[BTRFS_UUID_SIZE]; /* in */ | |
60 | __u64 stransid; /* in */ | |
61 | __u64 rtransid; /* out */ | |
62 | struct btrfs_ioctl_timespec_32 stime; /* in */ | |
63 | struct btrfs_ioctl_timespec_32 rtime; /* out */ | |
64 | __u64 flags; /* in */ | |
65 | __u64 reserved[16]; /* in */ | |
66 | } __attribute__ ((__packed__)); | |
67 | ||
68 | #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ | |
69 | struct btrfs_ioctl_received_subvol_args_32) | |
70 | #endif | |
71 | ||
2351f431 JB |
72 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) |
73 | struct btrfs_ioctl_send_args_32 { | |
74 | __s64 send_fd; /* in */ | |
75 | __u64 clone_sources_count; /* in */ | |
76 | compat_uptr_t clone_sources; /* in */ | |
77 | __u64 parent_root; /* in */ | |
78 | __u64 flags; /* in */ | |
79 | __u64 reserved[4]; /* in */ | |
80 | } __attribute__ ((__packed__)); | |
81 | ||
82 | #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \ | |
83 | struct btrfs_ioctl_send_args_32) | |
84 | #endif | |
abccd00f | 85 | |
416161db | 86 | static int btrfs_clone(struct inode *src, struct inode *inode, |
1c919a5e MF |
87 | u64 off, u64 olen, u64 olen_aligned, u64 destoff, |
88 | int no_time_update); | |
416161db | 89 | |
6cbff00f | 90 | /* Mask out flags that are inappropriate for the given type of inode. */ |
1905a0f7 DS |
91 | static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode, |
92 | unsigned int flags) | |
6cbff00f | 93 | { |
1905a0f7 | 94 | if (S_ISDIR(inode->i_mode)) |
6cbff00f | 95 | return flags; |
1905a0f7 | 96 | else if (S_ISREG(inode->i_mode)) |
6cbff00f CH |
97 | return flags & ~FS_DIRSYNC_FL; |
98 | else | |
99 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); | |
100 | } | |
101 | ||
102 | /* | |
a157d4fd DS |
103 | * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS |
104 | * ioctl. | |
6cbff00f | 105 | */ |
a157d4fd | 106 | static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags) |
6cbff00f CH |
107 | { |
108 | unsigned int iflags = 0; | |
109 | ||
110 | if (flags & BTRFS_INODE_SYNC) | |
111 | iflags |= FS_SYNC_FL; | |
112 | if (flags & BTRFS_INODE_IMMUTABLE) | |
113 | iflags |= FS_IMMUTABLE_FL; | |
114 | if (flags & BTRFS_INODE_APPEND) | |
115 | iflags |= FS_APPEND_FL; | |
116 | if (flags & BTRFS_INODE_NODUMP) | |
117 | iflags |= FS_NODUMP_FL; | |
118 | if (flags & BTRFS_INODE_NOATIME) | |
119 | iflags |= FS_NOATIME_FL; | |
120 | if (flags & BTRFS_INODE_DIRSYNC) | |
121 | iflags |= FS_DIRSYNC_FL; | |
d0092bdd LZ |
122 | if (flags & BTRFS_INODE_NODATACOW) |
123 | iflags |= FS_NOCOW_FL; | |
124 | ||
13f48dc9 | 125 | if (flags & BTRFS_INODE_NOCOMPRESS) |
d0092bdd | 126 | iflags |= FS_NOCOMP_FL; |
13f48dc9 ST |
127 | else if (flags & BTRFS_INODE_COMPRESS) |
128 | iflags |= FS_COMPR_FL; | |
6cbff00f CH |
129 | |
130 | return iflags; | |
131 | } | |
132 | ||
133 | /* | |
134 | * Update inode->i_flags based on the btrfs internal flags. | |
135 | */ | |
7b6a221e | 136 | void btrfs_sync_inode_flags_to_i_flags(struct inode *inode) |
6cbff00f | 137 | { |
5c57b8b6 | 138 | struct btrfs_inode *binode = BTRFS_I(inode); |
3cc79392 | 139 | unsigned int new_fl = 0; |
6cbff00f | 140 | |
5c57b8b6 | 141 | if (binode->flags & BTRFS_INODE_SYNC) |
3cc79392 | 142 | new_fl |= S_SYNC; |
5c57b8b6 | 143 | if (binode->flags & BTRFS_INODE_IMMUTABLE) |
3cc79392 | 144 | new_fl |= S_IMMUTABLE; |
5c57b8b6 | 145 | if (binode->flags & BTRFS_INODE_APPEND) |
3cc79392 | 146 | new_fl |= S_APPEND; |
5c57b8b6 | 147 | if (binode->flags & BTRFS_INODE_NOATIME) |
3cc79392 | 148 | new_fl |= S_NOATIME; |
5c57b8b6 | 149 | if (binode->flags & BTRFS_INODE_DIRSYNC) |
3cc79392 FM |
150 | new_fl |= S_DIRSYNC; |
151 | ||
152 | set_mask_bits(&inode->i_flags, | |
153 | S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC, | |
154 | new_fl); | |
6cbff00f CH |
155 | } |
156 | ||
6cbff00f CH |
157 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) |
158 | { | |
5c57b8b6 DS |
159 | struct btrfs_inode *binode = BTRFS_I(file_inode(file)); |
160 | unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags); | |
6cbff00f CH |
161 | |
162 | if (copy_to_user(arg, &flags, sizeof(flags))) | |
163 | return -EFAULT; | |
164 | return 0; | |
165 | } | |
166 | ||
5ba76abf DS |
167 | /* Check if @flags are a supported and valid set of FS_*_FL flags */ |
168 | static int check_fsflags(unsigned int flags) | |
75e7cb7f LB |
169 | { |
170 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ | |
171 | FS_NOATIME_FL | FS_NODUMP_FL | \ | |
172 | FS_SYNC_FL | FS_DIRSYNC_FL | \ | |
e1e8fb6a LZ |
173 | FS_NOCOMP_FL | FS_COMPR_FL | |
174 | FS_NOCOW_FL)) | |
75e7cb7f LB |
175 | return -EOPNOTSUPP; |
176 | ||
177 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) | |
178 | return -EINVAL; | |
179 | ||
75e7cb7f LB |
180 | return 0; |
181 | } | |
182 | ||
6cbff00f CH |
183 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) |
184 | { | |
496ad9aa | 185 | struct inode *inode = file_inode(file); |
0b246afa | 186 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5c57b8b6 DS |
187 | struct btrfs_inode *binode = BTRFS_I(inode); |
188 | struct btrfs_root *root = binode->root; | |
6cbff00f | 189 | struct btrfs_trans_handle *trans; |
5c57b8b6 | 190 | unsigned int fsflags, old_fsflags; |
6cbff00f | 191 | int ret; |
5c57b8b6 DS |
192 | u64 old_flags; |
193 | unsigned int old_i_flags; | |
7e97b8da | 194 | umode_t mode; |
6cbff00f | 195 | |
bd60ea0f DS |
196 | if (!inode_owner_or_capable(inode)) |
197 | return -EPERM; | |
198 | ||
b83cc969 LZ |
199 | if (btrfs_root_readonly(root)) |
200 | return -EROFS; | |
201 | ||
5c57b8b6 | 202 | if (copy_from_user(&fsflags, arg, sizeof(fsflags))) |
6cbff00f CH |
203 | return -EFAULT; |
204 | ||
5c57b8b6 | 205 | ret = check_fsflags(fsflags); |
75e7cb7f LB |
206 | if (ret) |
207 | return ret; | |
f46b5a66 | 208 | |
e7848683 JK |
209 | ret = mnt_want_write_file(file); |
210 | if (ret) | |
211 | return ret; | |
212 | ||
5955102c | 213 | inode_lock(inode); |
6cbff00f | 214 | |
5c57b8b6 DS |
215 | old_flags = binode->flags; |
216 | old_i_flags = inode->i_flags; | |
7e97b8da | 217 | mode = inode->i_mode; |
f062abf0 | 218 | |
5c57b8b6 DS |
219 | fsflags = btrfs_mask_fsflags_for_type(inode, fsflags); |
220 | old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags); | |
221 | if ((fsflags ^ old_fsflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { | |
6cbff00f CH |
222 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
223 | ret = -EPERM; | |
224 | goto out_unlock; | |
225 | } | |
226 | } | |
227 | ||
5c57b8b6 DS |
228 | if (fsflags & FS_SYNC_FL) |
229 | binode->flags |= BTRFS_INODE_SYNC; | |
6cbff00f | 230 | else |
5c57b8b6 DS |
231 | binode->flags &= ~BTRFS_INODE_SYNC; |
232 | if (fsflags & FS_IMMUTABLE_FL) | |
233 | binode->flags |= BTRFS_INODE_IMMUTABLE; | |
6cbff00f | 234 | else |
5c57b8b6 DS |
235 | binode->flags &= ~BTRFS_INODE_IMMUTABLE; |
236 | if (fsflags & FS_APPEND_FL) | |
237 | binode->flags |= BTRFS_INODE_APPEND; | |
6cbff00f | 238 | else |
5c57b8b6 DS |
239 | binode->flags &= ~BTRFS_INODE_APPEND; |
240 | if (fsflags & FS_NODUMP_FL) | |
241 | binode->flags |= BTRFS_INODE_NODUMP; | |
6cbff00f | 242 | else |
5c57b8b6 DS |
243 | binode->flags &= ~BTRFS_INODE_NODUMP; |
244 | if (fsflags & FS_NOATIME_FL) | |
245 | binode->flags |= BTRFS_INODE_NOATIME; | |
6cbff00f | 246 | else |
5c57b8b6 DS |
247 | binode->flags &= ~BTRFS_INODE_NOATIME; |
248 | if (fsflags & FS_DIRSYNC_FL) | |
249 | binode->flags |= BTRFS_INODE_DIRSYNC; | |
6cbff00f | 250 | else |
5c57b8b6 DS |
251 | binode->flags &= ~BTRFS_INODE_DIRSYNC; |
252 | if (fsflags & FS_NOCOW_FL) { | |
7e97b8da DS |
253 | if (S_ISREG(mode)) { |
254 | /* | |
255 | * It's safe to turn csums off here, no extents exist. | |
256 | * Otherwise we want the flag to reflect the real COW | |
257 | * status of the file and will not set it. | |
258 | */ | |
259 | if (inode->i_size == 0) | |
5c57b8b6 DS |
260 | binode->flags |= BTRFS_INODE_NODATACOW |
261 | | BTRFS_INODE_NODATASUM; | |
7e97b8da | 262 | } else { |
5c57b8b6 | 263 | binode->flags |= BTRFS_INODE_NODATACOW; |
7e97b8da DS |
264 | } |
265 | } else { | |
266 | /* | |
01327610 | 267 | * Revert back under same assumptions as above |
7e97b8da DS |
268 | */ |
269 | if (S_ISREG(mode)) { | |
270 | if (inode->i_size == 0) | |
5c57b8b6 | 271 | binode->flags &= ~(BTRFS_INODE_NODATACOW |
7e97b8da DS |
272 | | BTRFS_INODE_NODATASUM); |
273 | } else { | |
5c57b8b6 | 274 | binode->flags &= ~BTRFS_INODE_NODATACOW; |
7e97b8da DS |
275 | } |
276 | } | |
6cbff00f | 277 | |
75e7cb7f LB |
278 | /* |
279 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS | |
280 | * flag may be changed automatically if compression code won't make | |
281 | * things smaller. | |
282 | */ | |
5c57b8b6 DS |
283 | if (fsflags & FS_NOCOMP_FL) { |
284 | binode->flags &= ~BTRFS_INODE_COMPRESS; | |
285 | binode->flags |= BTRFS_INODE_NOCOMPRESS; | |
63541927 FDBM |
286 | |
287 | ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); | |
288 | if (ret && ret != -ENODATA) | |
289 | goto out_drop; | |
5c57b8b6 | 290 | } else if (fsflags & FS_COMPR_FL) { |
63541927 FDBM |
291 | const char *comp; |
292 | ||
5c57b8b6 DS |
293 | binode->flags |= BTRFS_INODE_COMPRESS; |
294 | binode->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
63541927 | 295 | |
93370509 DS |
296 | comp = btrfs_compress_type2str(fs_info->compress_type); |
297 | if (!comp || comp[0] == 0) | |
298 | comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); | |
299 | ||
63541927 FDBM |
300 | ret = btrfs_set_prop(inode, "btrfs.compression", |
301 | comp, strlen(comp), 0); | |
302 | if (ret) | |
303 | goto out_drop; | |
304 | ||
ebcb904d | 305 | } else { |
78a017a2 FM |
306 | ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); |
307 | if (ret && ret != -ENODATA) | |
308 | goto out_drop; | |
5c57b8b6 | 309 | binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); |
75e7cb7f | 310 | } |
6cbff00f | 311 | |
4da6f1a3 | 312 | trans = btrfs_start_transaction(root, 1); |
f062abf0 LZ |
313 | if (IS_ERR(trans)) { |
314 | ret = PTR_ERR(trans); | |
315 | goto out_drop; | |
316 | } | |
6cbff00f | 317 | |
7b6a221e | 318 | btrfs_sync_inode_flags_to_i_flags(inode); |
0c4d2d95 | 319 | inode_inc_iversion(inode); |
c2050a45 | 320 | inode->i_ctime = current_time(inode); |
6cbff00f | 321 | ret = btrfs_update_inode(trans, root, inode); |
6cbff00f | 322 | |
3a45bb20 | 323 | btrfs_end_transaction(trans); |
f062abf0 LZ |
324 | out_drop: |
325 | if (ret) { | |
5c57b8b6 DS |
326 | binode->flags = old_flags; |
327 | inode->i_flags = old_i_flags; | |
f062abf0 | 328 | } |
6cbff00f | 329 | |
6cbff00f | 330 | out_unlock: |
5955102c | 331 | inode_unlock(inode); |
e7848683 | 332 | mnt_drop_write_file(file); |
2d4e6f6a | 333 | return ret; |
6cbff00f CH |
334 | } |
335 | ||
19f93b3c DS |
336 | /* |
337 | * Translate btrfs internal inode flags to xflags as expected by the | |
338 | * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are | |
339 | * silently dropped. | |
340 | */ | |
341 | static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags) | |
342 | { | |
343 | unsigned int xflags = 0; | |
344 | ||
345 | if (flags & BTRFS_INODE_APPEND) | |
346 | xflags |= FS_XFLAG_APPEND; | |
347 | if (flags & BTRFS_INODE_IMMUTABLE) | |
348 | xflags |= FS_XFLAG_IMMUTABLE; | |
349 | if (flags & BTRFS_INODE_NOATIME) | |
350 | xflags |= FS_XFLAG_NOATIME; | |
351 | if (flags & BTRFS_INODE_NODUMP) | |
352 | xflags |= FS_XFLAG_NODUMP; | |
353 | if (flags & BTRFS_INODE_SYNC) | |
354 | xflags |= FS_XFLAG_SYNC; | |
355 | ||
356 | return xflags; | |
357 | } | |
358 | ||
359 | /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */ | |
360 | static int check_xflags(unsigned int flags) | |
361 | { | |
362 | if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME | | |
363 | FS_XFLAG_NODUMP | FS_XFLAG_SYNC)) | |
364 | return -EOPNOTSUPP; | |
365 | return 0; | |
366 | } | |
367 | ||
e4202ac9 DS |
368 | /* |
369 | * Set the xflags from the internal inode flags. The remaining items of fsxattr | |
370 | * are zeroed. | |
371 | */ | |
372 | static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg) | |
373 | { | |
374 | struct btrfs_inode *binode = BTRFS_I(file_inode(file)); | |
375 | struct fsxattr fa; | |
376 | ||
377 | memset(&fa, 0, sizeof(fa)); | |
378 | fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags); | |
379 | ||
380 | if (copy_to_user(arg, &fa, sizeof(fa))) | |
381 | return -EFAULT; | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
025f2121 DS |
386 | static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg) |
387 | { | |
388 | struct inode *inode = file_inode(file); | |
389 | struct btrfs_inode *binode = BTRFS_I(inode); | |
390 | struct btrfs_root *root = binode->root; | |
391 | struct btrfs_trans_handle *trans; | |
392 | struct fsxattr fa; | |
393 | unsigned old_flags; | |
394 | unsigned old_i_flags; | |
395 | int ret = 0; | |
396 | ||
397 | if (!inode_owner_or_capable(inode)) | |
398 | return -EPERM; | |
399 | ||
400 | if (btrfs_root_readonly(root)) | |
401 | return -EROFS; | |
402 | ||
403 | memset(&fa, 0, sizeof(fa)); | |
404 | if (copy_from_user(&fa, arg, sizeof(fa))) | |
405 | return -EFAULT; | |
406 | ||
407 | ret = check_xflags(fa.fsx_xflags); | |
408 | if (ret) | |
409 | return ret; | |
410 | ||
411 | if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0) | |
412 | return -EOPNOTSUPP; | |
413 | ||
414 | ret = mnt_want_write_file(file); | |
415 | if (ret) | |
416 | return ret; | |
417 | ||
418 | inode_lock(inode); | |
419 | ||
420 | old_flags = binode->flags; | |
421 | old_i_flags = inode->i_flags; | |
422 | ||
423 | /* We need the capabilities to change append-only or immutable inode */ | |
424 | if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) || | |
425 | (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) && | |
426 | !capable(CAP_LINUX_IMMUTABLE)) { | |
427 | ret = -EPERM; | |
428 | goto out_unlock; | |
429 | } | |
430 | ||
431 | if (fa.fsx_xflags & FS_XFLAG_SYNC) | |
432 | binode->flags |= BTRFS_INODE_SYNC; | |
433 | else | |
434 | binode->flags &= ~BTRFS_INODE_SYNC; | |
435 | if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE) | |
436 | binode->flags |= BTRFS_INODE_IMMUTABLE; | |
437 | else | |
438 | binode->flags &= ~BTRFS_INODE_IMMUTABLE; | |
439 | if (fa.fsx_xflags & FS_XFLAG_APPEND) | |
440 | binode->flags |= BTRFS_INODE_APPEND; | |
441 | else | |
442 | binode->flags &= ~BTRFS_INODE_APPEND; | |
443 | if (fa.fsx_xflags & FS_XFLAG_NODUMP) | |
444 | binode->flags |= BTRFS_INODE_NODUMP; | |
445 | else | |
446 | binode->flags &= ~BTRFS_INODE_NODUMP; | |
447 | if (fa.fsx_xflags & FS_XFLAG_NOATIME) | |
448 | binode->flags |= BTRFS_INODE_NOATIME; | |
449 | else | |
450 | binode->flags &= ~BTRFS_INODE_NOATIME; | |
451 | ||
452 | /* 1 item for the inode */ | |
453 | trans = btrfs_start_transaction(root, 1); | |
454 | if (IS_ERR(trans)) { | |
455 | ret = PTR_ERR(trans); | |
456 | goto out_unlock; | |
457 | } | |
458 | ||
459 | btrfs_sync_inode_flags_to_i_flags(inode); | |
460 | inode_inc_iversion(inode); | |
461 | inode->i_ctime = current_time(inode); | |
462 | ret = btrfs_update_inode(trans, root, inode); | |
463 | ||
464 | btrfs_end_transaction(trans); | |
465 | ||
466 | out_unlock: | |
467 | if (ret) { | |
468 | binode->flags = old_flags; | |
469 | inode->i_flags = old_i_flags; | |
470 | } | |
471 | ||
472 | inode_unlock(inode); | |
473 | mnt_drop_write_file(file); | |
474 | ||
475 | return ret; | |
476 | } | |
477 | ||
6cbff00f CH |
478 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) |
479 | { | |
496ad9aa | 480 | struct inode *inode = file_inode(file); |
6cbff00f CH |
481 | |
482 | return put_user(inode->i_generation, arg); | |
483 | } | |
f46b5a66 | 484 | |
f7039b1d LD |
485 | static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) |
486 | { | |
0b246afa JM |
487 | struct inode *inode = file_inode(file); |
488 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
f7039b1d LD |
489 | struct btrfs_device *device; |
490 | struct request_queue *q; | |
491 | struct fstrim_range range; | |
492 | u64 minlen = ULLONG_MAX; | |
493 | u64 num_devices = 0; | |
815745cf | 494 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
f7039b1d LD |
495 | int ret; |
496 | ||
497 | if (!capable(CAP_SYS_ADMIN)) | |
498 | return -EPERM; | |
499 | ||
1f78160c XG |
500 | rcu_read_lock(); |
501 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, | |
502 | dev_list) { | |
f7039b1d LD |
503 | if (!device->bdev) |
504 | continue; | |
505 | q = bdev_get_queue(device->bdev); | |
506 | if (blk_queue_discard(q)) { | |
507 | num_devices++; | |
50d0446e | 508 | minlen = min_t(u64, q->limits.discard_granularity, |
f7039b1d LD |
509 | minlen); |
510 | } | |
511 | } | |
1f78160c | 512 | rcu_read_unlock(); |
f4c697e6 | 513 | |
f7039b1d LD |
514 | if (!num_devices) |
515 | return -EOPNOTSUPP; | |
f7039b1d LD |
516 | if (copy_from_user(&range, arg, sizeof(range))) |
517 | return -EFAULT; | |
e515c18b LC |
518 | if (range.start > total_bytes || |
519 | range.len < fs_info->sb->s_blocksize) | |
f4c697e6 | 520 | return -EINVAL; |
f7039b1d | 521 | |
f4c697e6 | 522 | range.len = min(range.len, total_bytes - range.start); |
f7039b1d | 523 | range.minlen = max(range.minlen, minlen); |
2ff7e61e | 524 | ret = btrfs_trim_fs(fs_info, &range); |
f7039b1d LD |
525 | if (ret < 0) |
526 | return ret; | |
527 | ||
528 | if (copy_to_user(arg, &range, sizeof(range))) | |
529 | return -EFAULT; | |
530 | ||
531 | return 0; | |
532 | } | |
533 | ||
dd5f9615 SB |
534 | int btrfs_is_empty_uuid(u8 *uuid) |
535 | { | |
46e0f66a CM |
536 | int i; |
537 | ||
538 | for (i = 0; i < BTRFS_UUID_SIZE; i++) { | |
539 | if (uuid[i]) | |
540 | return 0; | |
541 | } | |
542 | return 1; | |
dd5f9615 SB |
543 | } |
544 | ||
d5c12070 | 545 | static noinline int create_subvol(struct inode *dir, |
cb8e7090 | 546 | struct dentry *dentry, |
52f75f4f | 547 | const char *name, int namelen, |
6f72c7e2 | 548 | u64 *async_transid, |
8696c533 | 549 | struct btrfs_qgroup_inherit *inherit) |
f46b5a66 | 550 | { |
0b246afa | 551 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
f46b5a66 CH |
552 | struct btrfs_trans_handle *trans; |
553 | struct btrfs_key key; | |
49a3c4d9 | 554 | struct btrfs_root_item *root_item; |
f46b5a66 CH |
555 | struct btrfs_inode_item *inode_item; |
556 | struct extent_buffer *leaf; | |
d5c12070 | 557 | struct btrfs_root *root = BTRFS_I(dir)->root; |
76dda93c | 558 | struct btrfs_root *new_root; |
d5c12070 | 559 | struct btrfs_block_rsv block_rsv; |
95582b00 | 560 | struct timespec64 cur_time = current_time(dir); |
5662344b | 561 | struct inode *inode; |
f46b5a66 CH |
562 | int ret; |
563 | int err; | |
564 | u64 objectid; | |
565 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | |
3de4586c | 566 | u64 index = 0; |
8ea05e3a | 567 | uuid_le new_uuid; |
f46b5a66 | 568 | |
49a3c4d9 DS |
569 | root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); |
570 | if (!root_item) | |
571 | return -ENOMEM; | |
572 | ||
0b246afa | 573 | ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid); |
2fbe8c8a | 574 | if (ret) |
49a3c4d9 | 575 | goto fail_free; |
6a912213 | 576 | |
e09fe2d2 QW |
577 | /* |
578 | * Don't create subvolume whose level is not zero. Or qgroup will be | |
01327610 | 579 | * screwed up since it assumes subvolume qgroup's level to be 0. |
e09fe2d2 | 580 | */ |
49a3c4d9 DS |
581 | if (btrfs_qgroup_level(objectid)) { |
582 | ret = -ENOSPC; | |
583 | goto fail_free; | |
584 | } | |
e09fe2d2 | 585 | |
d5c12070 | 586 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
9ed74f2d | 587 | /* |
d5c12070 MX |
588 | * The same as the snapshot creation, please see the comment |
589 | * of create_snapshot(). | |
9ed74f2d | 590 | */ |
c4c129db | 591 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false); |
d5c12070 | 592 | if (ret) |
49a3c4d9 | 593 | goto fail_free; |
d5c12070 MX |
594 | |
595 | trans = btrfs_start_transaction(root, 0); | |
596 | if (IS_ERR(trans)) { | |
597 | ret = PTR_ERR(trans); | |
7775c818 | 598 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); |
49a3c4d9 | 599 | goto fail_free; |
d5c12070 MX |
600 | } |
601 | trans->block_rsv = &block_rsv; | |
602 | trans->bytes_reserved = block_rsv.size; | |
f46b5a66 | 603 | |
a9377422 | 604 | ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit); |
6f72c7e2 AJ |
605 | if (ret) |
606 | goto fail; | |
607 | ||
4d75f8a9 | 608 | leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); |
8e8a1e31 JB |
609 | if (IS_ERR(leaf)) { |
610 | ret = PTR_ERR(leaf); | |
611 | goto fail; | |
612 | } | |
f46b5a66 | 613 | |
f46b5a66 CH |
614 | btrfs_mark_buffer_dirty(leaf); |
615 | ||
49a3c4d9 | 616 | inode_item = &root_item->inode; |
3cae210f QW |
617 | btrfs_set_stack_inode_generation(inode_item, 1); |
618 | btrfs_set_stack_inode_size(inode_item, 3); | |
619 | btrfs_set_stack_inode_nlink(inode_item, 1); | |
da17066c | 620 | btrfs_set_stack_inode_nbytes(inode_item, |
0b246afa | 621 | fs_info->nodesize); |
3cae210f | 622 | btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); |
f46b5a66 | 623 | |
49a3c4d9 DS |
624 | btrfs_set_root_flags(root_item, 0); |
625 | btrfs_set_root_limit(root_item, 0); | |
3cae210f | 626 | btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); |
08fe4db1 | 627 | |
49a3c4d9 DS |
628 | btrfs_set_root_bytenr(root_item, leaf->start); |
629 | btrfs_set_root_generation(root_item, trans->transid); | |
630 | btrfs_set_root_level(root_item, 0); | |
631 | btrfs_set_root_refs(root_item, 1); | |
632 | btrfs_set_root_used(root_item, leaf->len); | |
633 | btrfs_set_root_last_snapshot(root_item, 0); | |
f46b5a66 | 634 | |
49a3c4d9 DS |
635 | btrfs_set_root_generation_v2(root_item, |
636 | btrfs_root_generation(root_item)); | |
8ea05e3a | 637 | uuid_le_gen(&new_uuid); |
49a3c4d9 DS |
638 | memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); |
639 | btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); | |
640 | btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); | |
641 | root_item->ctime = root_item->otime; | |
642 | btrfs_set_root_ctransid(root_item, trans->transid); | |
643 | btrfs_set_root_otransid(root_item, trans->transid); | |
f46b5a66 | 644 | |
925baedd | 645 | btrfs_tree_unlock(leaf); |
f46b5a66 CH |
646 | free_extent_buffer(leaf); |
647 | leaf = NULL; | |
648 | ||
49a3c4d9 | 649 | btrfs_set_root_dirid(root_item, new_dirid); |
f46b5a66 CH |
650 | |
651 | key.objectid = objectid; | |
5d4f98a2 | 652 | key.offset = 0; |
962a298f | 653 | key.type = BTRFS_ROOT_ITEM_KEY; |
0b246afa | 654 | ret = btrfs_insert_root(trans, fs_info->tree_root, &key, |
49a3c4d9 | 655 | root_item); |
f46b5a66 CH |
656 | if (ret) |
657 | goto fail; | |
658 | ||
76dda93c | 659 | key.offset = (u64)-1; |
0b246afa | 660 | new_root = btrfs_read_fs_root_no_name(fs_info, &key); |
79787eaa | 661 | if (IS_ERR(new_root)) { |
79787eaa | 662 | ret = PTR_ERR(new_root); |
66642832 | 663 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
664 | goto fail; |
665 | } | |
76dda93c YZ |
666 | |
667 | btrfs_record_root_in_trans(trans, new_root); | |
668 | ||
63541927 | 669 | ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); |
ce598979 MF |
670 | if (ret) { |
671 | /* We potentially lose an unused inode item here */ | |
66642832 | 672 | btrfs_abort_transaction(trans, ret); |
ce598979 MF |
673 | goto fail; |
674 | } | |
675 | ||
f32e48e9 CR |
676 | mutex_lock(&new_root->objectid_mutex); |
677 | new_root->highest_objectid = new_dirid; | |
678 | mutex_unlock(&new_root->objectid_mutex); | |
679 | ||
f46b5a66 CH |
680 | /* |
681 | * insert the directory item | |
682 | */ | |
877574e2 | 683 | ret = btrfs_set_inode_index(BTRFS_I(dir), &index); |
79787eaa | 684 | if (ret) { |
66642832 | 685 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
686 | goto fail; |
687 | } | |
3de4586c | 688 | |
684572df | 689 | ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, |
3de4586c | 690 | BTRFS_FT_DIR, index); |
79787eaa | 691 | if (ret) { |
66642832 | 692 | btrfs_abort_transaction(trans, ret); |
f46b5a66 | 693 | goto fail; |
79787eaa | 694 | } |
0660b5af | 695 | |
6ef06d27 | 696 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); |
52c26179 YZ |
697 | ret = btrfs_update_inode(trans, root, dir); |
698 | BUG_ON(ret); | |
699 | ||
6025c19f | 700 | ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, |
4a0cc7ca | 701 | btrfs_ino(BTRFS_I(dir)), index, name, namelen); |
76dda93c | 702 | BUG_ON(ret); |
f46b5a66 | 703 | |
cdb345a8 | 704 | ret = btrfs_uuid_tree_add(trans, root_item->uuid, |
6bccf3ab | 705 | BTRFS_UUID_KEY_SUBVOL, objectid); |
dd5f9615 | 706 | if (ret) |
66642832 | 707 | btrfs_abort_transaction(trans, ret); |
dd5f9615 | 708 | |
f46b5a66 | 709 | fail: |
49a3c4d9 | 710 | kfree(root_item); |
d5c12070 MX |
711 | trans->block_rsv = NULL; |
712 | trans->bytes_reserved = 0; | |
7775c818 | 713 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); |
de6e8200 | 714 | |
72fd032e SW |
715 | if (async_transid) { |
716 | *async_transid = trans->transid; | |
3a45bb20 | 717 | err = btrfs_commit_transaction_async(trans, 1); |
00d71c9c | 718 | if (err) |
3a45bb20 | 719 | err = btrfs_commit_transaction(trans); |
72fd032e | 720 | } else { |
3a45bb20 | 721 | err = btrfs_commit_transaction(trans); |
72fd032e | 722 | } |
f46b5a66 CH |
723 | if (err && !ret) |
724 | ret = err; | |
1a65e24b | 725 | |
5662344b TI |
726 | if (!ret) { |
727 | inode = btrfs_lookup_dentry(dir, dentry); | |
de6e8200 LB |
728 | if (IS_ERR(inode)) |
729 | return PTR_ERR(inode); | |
5662344b TI |
730 | d_instantiate(dentry, inode); |
731 | } | |
f46b5a66 | 732 | return ret; |
49a3c4d9 DS |
733 | |
734 | fail_free: | |
735 | kfree(root_item); | |
736 | return ret; | |
f46b5a66 CH |
737 | } |
738 | ||
e9662f70 | 739 | static int create_snapshot(struct btrfs_root *root, struct inode *dir, |
61d7e4cb | 740 | struct dentry *dentry, |
e9662f70 MX |
741 | u64 *async_transid, bool readonly, |
742 | struct btrfs_qgroup_inherit *inherit) | |
f46b5a66 | 743 | { |
0b246afa | 744 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
2e4bfab9 | 745 | struct inode *inode; |
f46b5a66 CH |
746 | struct btrfs_pending_snapshot *pending_snapshot; |
747 | struct btrfs_trans_handle *trans; | |
2e4bfab9 | 748 | int ret; |
8ecebf4d | 749 | bool snapshot_force_cow = false; |
f46b5a66 | 750 | |
27cdeb70 | 751 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
f46b5a66 CH |
752 | return -EINVAL; |
753 | ||
23269bf5 | 754 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); |
a1ee7362 DS |
755 | if (!pending_snapshot) |
756 | return -ENOMEM; | |
757 | ||
b0c0ea63 | 758 | pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), |
23269bf5 | 759 | GFP_KERNEL); |
8546b570 DS |
760 | pending_snapshot->path = btrfs_alloc_path(); |
761 | if (!pending_snapshot->root_item || !pending_snapshot->path) { | |
b0c0ea63 DS |
762 | ret = -ENOMEM; |
763 | goto free_pending; | |
764 | } | |
765 | ||
8ecebf4d RK |
766 | /* |
767 | * Force new buffered writes to reserve space even when NOCOW is | |
768 | * possible. This is to avoid later writeback (running dealloc) to | |
769 | * fallback to COW mode and unexpectedly fail with ENOSPC. | |
770 | */ | |
ea14b57f | 771 | atomic_inc(&root->will_be_snapshotted); |
4e857c58 | 772 | smp_mb__after_atomic(); |
45bac0f3 LB |
773 | /* wait for no snapshot writes */ |
774 | wait_event(root->subv_writers->wait, | |
775 | percpu_counter_sum(&root->subv_writers->counter) == 0); | |
8257b2dc | 776 | |
76f32e24 | 777 | ret = btrfs_start_delalloc_inodes(root); |
6a03843d | 778 | if (ret) |
a1ee7362 | 779 | goto dec_and_free; |
6a03843d | 780 | |
8ecebf4d RK |
781 | /* |
782 | * All previous writes have started writeback in NOCOW mode, so now | |
783 | * we force future writes to fallback to COW mode during snapshot | |
784 | * creation. | |
785 | */ | |
786 | atomic_inc(&root->snapshot_force_cow); | |
787 | snapshot_force_cow = true; | |
788 | ||
6374e57a | 789 | btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); |
6a03843d | 790 | |
66d8f3dd MX |
791 | btrfs_init_block_rsv(&pending_snapshot->block_rsv, |
792 | BTRFS_BLOCK_RSV_TEMP); | |
d5c12070 MX |
793 | /* |
794 | * 1 - parent dir inode | |
795 | * 2 - dir entries | |
796 | * 1 - root item | |
797 | * 2 - root ref/backref | |
798 | * 1 - root of snapshot | |
dd5f9615 | 799 | * 1 - UUID item |
d5c12070 MX |
800 | */ |
801 | ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, | |
dd5f9615 | 802 | &pending_snapshot->block_rsv, 8, |
ee3441b4 | 803 | false); |
d5c12070 | 804 | if (ret) |
a1ee7362 | 805 | goto dec_and_free; |
d5c12070 | 806 | |
3de4586c | 807 | pending_snapshot->dentry = dentry; |
f46b5a66 | 808 | pending_snapshot->root = root; |
b83cc969 | 809 | pending_snapshot->readonly = readonly; |
e9662f70 | 810 | pending_snapshot->dir = dir; |
8696c533 | 811 | pending_snapshot->inherit = inherit; |
a22285a6 | 812 | |
d5c12070 | 813 | trans = btrfs_start_transaction(root, 0); |
a22285a6 YZ |
814 | if (IS_ERR(trans)) { |
815 | ret = PTR_ERR(trans); | |
816 | goto fail; | |
817 | } | |
818 | ||
0b246afa | 819 | spin_lock(&fs_info->trans_lock); |
f46b5a66 CH |
820 | list_add(&pending_snapshot->list, |
821 | &trans->transaction->pending_snapshots); | |
0b246afa | 822 | spin_unlock(&fs_info->trans_lock); |
72fd032e SW |
823 | if (async_transid) { |
824 | *async_transid = trans->transid; | |
3a45bb20 | 825 | ret = btrfs_commit_transaction_async(trans, 1); |
00d71c9c | 826 | if (ret) |
3a45bb20 | 827 | ret = btrfs_commit_transaction(trans); |
72fd032e | 828 | } else { |
3a45bb20 | 829 | ret = btrfs_commit_transaction(trans); |
72fd032e | 830 | } |
aec8030a | 831 | if (ret) |
c37b2b62 | 832 | goto fail; |
a22285a6 YZ |
833 | |
834 | ret = pending_snapshot->error; | |
835 | if (ret) | |
836 | goto fail; | |
837 | ||
d3797308 CM |
838 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
839 | if (ret) | |
840 | goto fail; | |
841 | ||
2b0143b5 | 842 | inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); |
2e4bfab9 YZ |
843 | if (IS_ERR(inode)) { |
844 | ret = PTR_ERR(inode); | |
845 | goto fail; | |
846 | } | |
5662344b | 847 | |
2e4bfab9 YZ |
848 | d_instantiate(dentry, inode); |
849 | ret = 0; | |
850 | fail: | |
7775c818 | 851 | btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv); |
a1ee7362 | 852 | dec_and_free: |
8ecebf4d RK |
853 | if (snapshot_force_cow) |
854 | atomic_dec(&root->snapshot_force_cow); | |
ea14b57f | 855 | if (atomic_dec_and_test(&root->will_be_snapshotted)) |
4625956a | 856 | wake_up_var(&root->will_be_snapshotted); |
b0c0ea63 DS |
857 | free_pending: |
858 | kfree(pending_snapshot->root_item); | |
8546b570 | 859 | btrfs_free_path(pending_snapshot->path); |
a1ee7362 DS |
860 | kfree(pending_snapshot); |
861 | ||
f46b5a66 CH |
862 | return ret; |
863 | } | |
864 | ||
4260f7c7 SW |
865 | /* copy of may_delete in fs/namei.c() |
866 | * Check whether we can remove a link victim from directory dir, check | |
867 | * whether the type of victim is right. | |
868 | * 1. We can't do it if dir is read-only (done in permission()) | |
869 | * 2. We should have write and exec permissions on dir | |
870 | * 3. We can't remove anything from append-only dir | |
871 | * 4. We can't do anything with immutable dir (done in permission()) | |
872 | * 5. If the sticky bit on dir is set we should either | |
873 | * a. be owner of dir, or | |
874 | * b. be owner of victim, or | |
875 | * c. have CAP_FOWNER capability | |
01327610 | 876 | * 6. If the victim is append-only or immutable we can't do anything with |
4260f7c7 SW |
877 | * links pointing to it. |
878 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
879 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
880 | * 9. We can't remove a root or mountpoint. | |
881 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by | |
882 | * nfs_async_unlink(). | |
883 | */ | |
884 | ||
67871254 | 885 | static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) |
4260f7c7 SW |
886 | { |
887 | int error; | |
888 | ||
2b0143b5 | 889 | if (d_really_is_negative(victim)) |
4260f7c7 SW |
890 | return -ENOENT; |
891 | ||
2b0143b5 | 892 | BUG_ON(d_inode(victim->d_parent) != dir); |
4fa6b5ec | 893 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
4260f7c7 SW |
894 | |
895 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
896 | if (error) | |
897 | return error; | |
898 | if (IS_APPEND(dir)) | |
899 | return -EPERM; | |
2b0143b5 DH |
900 | if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) || |
901 | IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim))) | |
4260f7c7 SW |
902 | return -EPERM; |
903 | if (isdir) { | |
e36cb0b8 | 904 | if (!d_is_dir(victim)) |
4260f7c7 SW |
905 | return -ENOTDIR; |
906 | if (IS_ROOT(victim)) | |
907 | return -EBUSY; | |
e36cb0b8 | 908 | } else if (d_is_dir(victim)) |
4260f7c7 SW |
909 | return -EISDIR; |
910 | if (IS_DEADDIR(dir)) | |
911 | return -ENOENT; | |
912 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
913 | return -EBUSY; | |
914 | return 0; | |
915 | } | |
916 | ||
cb8e7090 CH |
917 | /* copy of may_create in fs/namei.c() */ |
918 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) | |
919 | { | |
2b0143b5 | 920 | if (d_really_is_positive(child)) |
cb8e7090 CH |
921 | return -EEXIST; |
922 | if (IS_DEADDIR(dir)) | |
923 | return -ENOENT; | |
924 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
925 | } | |
926 | ||
927 | /* | |
928 | * Create a new subvolume below @parent. This is largely modeled after | |
929 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup | |
930 | * inside this filesystem so it's quite a bit simpler. | |
931 | */ | |
92872094 | 932 | static noinline int btrfs_mksubvol(const struct path *parent, |
52f75f4f | 933 | const char *name, int namelen, |
72fd032e | 934 | struct btrfs_root *snap_src, |
6f72c7e2 | 935 | u64 *async_transid, bool readonly, |
8696c533 | 936 | struct btrfs_qgroup_inherit *inherit) |
cb8e7090 | 937 | { |
0b246afa JM |
938 | struct inode *dir = d_inode(parent->dentry); |
939 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); | |
cb8e7090 CH |
940 | struct dentry *dentry; |
941 | int error; | |
942 | ||
00235411 AV |
943 | error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
944 | if (error == -EINTR) | |
945 | return error; | |
cb8e7090 CH |
946 | |
947 | dentry = lookup_one_len(name, parent->dentry, namelen); | |
948 | error = PTR_ERR(dentry); | |
949 | if (IS_ERR(dentry)) | |
950 | goto out_unlock; | |
951 | ||
76dda93c | 952 | error = btrfs_may_create(dir, dentry); |
cb8e7090 | 953 | if (error) |
a874a63e | 954 | goto out_dput; |
cb8e7090 | 955 | |
9c52057c CM |
956 | /* |
957 | * even if this name doesn't exist, we may get hash collisions. | |
958 | * check for them now when we can safely fail | |
959 | */ | |
960 | error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, | |
961 | dir->i_ino, name, | |
962 | namelen); | |
963 | if (error) | |
964 | goto out_dput; | |
965 | ||
0b246afa | 966 | down_read(&fs_info->subvol_sem); |
76dda93c YZ |
967 | |
968 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) | |
969 | goto out_up_read; | |
970 | ||
3de4586c | 971 | if (snap_src) { |
61d7e4cb | 972 | error = create_snapshot(snap_src, dir, dentry, |
6f72c7e2 | 973 | async_transid, readonly, inherit); |
3de4586c | 974 | } else { |
d5c12070 MX |
975 | error = create_subvol(dir, dentry, name, namelen, |
976 | async_transid, inherit); | |
3de4586c | 977 | } |
76dda93c YZ |
978 | if (!error) |
979 | fsnotify_mkdir(dir, dentry); | |
980 | out_up_read: | |
0b246afa | 981 | up_read(&fs_info->subvol_sem); |
cb8e7090 CH |
982 | out_dput: |
983 | dput(dentry); | |
984 | out_unlock: | |
5955102c | 985 | inode_unlock(dir); |
cb8e7090 CH |
986 | return error; |
987 | } | |
988 | ||
4cb5300b CM |
989 | /* |
990 | * When we're defragging a range, we don't want to kick it off again | |
991 | * if it is really just waiting for delalloc to send it down. | |
992 | * If we find a nice big extent or delalloc range for the bytes in the | |
993 | * file you want to defrag, we return 0 to let you know to skip this | |
994 | * part of the file | |
995 | */ | |
aab110ab | 996 | static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh) |
4cb5300b CM |
997 | { |
998 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
999 | struct extent_map *em = NULL; | |
1000 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
1001 | u64 end; | |
1002 | ||
1003 | read_lock(&em_tree->lock); | |
09cbfeaf | 1004 | em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE); |
4cb5300b CM |
1005 | read_unlock(&em_tree->lock); |
1006 | ||
1007 | if (em) { | |
1008 | end = extent_map_end(em); | |
1009 | free_extent_map(em); | |
1010 | if (end - offset > thresh) | |
1011 | return 0; | |
1012 | } | |
1013 | /* if we already have a nice delalloc here, just stop */ | |
1014 | thresh /= 2; | |
1015 | end = count_range_bits(io_tree, &offset, offset + thresh, | |
1016 | thresh, EXTENT_DELALLOC, 1); | |
1017 | if (end >= thresh) | |
1018 | return 0; | |
1019 | return 1; | |
1020 | } | |
1021 | ||
1022 | /* | |
1023 | * helper function to walk through a file and find extents | |
1024 | * newer than a specific transid, and smaller than thresh. | |
1025 | * | |
1026 | * This is used by the defragging code to find new and small | |
1027 | * extents | |
1028 | */ | |
1029 | static int find_new_extents(struct btrfs_root *root, | |
1030 | struct inode *inode, u64 newer_than, | |
aab110ab | 1031 | u64 *off, u32 thresh) |
4cb5300b CM |
1032 | { |
1033 | struct btrfs_path *path; | |
1034 | struct btrfs_key min_key; | |
4cb5300b CM |
1035 | struct extent_buffer *leaf; |
1036 | struct btrfs_file_extent_item *extent; | |
1037 | int type; | |
1038 | int ret; | |
4a0cc7ca | 1039 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
4cb5300b CM |
1040 | |
1041 | path = btrfs_alloc_path(); | |
1042 | if (!path) | |
1043 | return -ENOMEM; | |
1044 | ||
a4689d2b | 1045 | min_key.objectid = ino; |
4cb5300b CM |
1046 | min_key.type = BTRFS_EXTENT_DATA_KEY; |
1047 | min_key.offset = *off; | |
1048 | ||
67871254 | 1049 | while (1) { |
6174d3cb | 1050 | ret = btrfs_search_forward(root, &min_key, path, newer_than); |
4cb5300b CM |
1051 | if (ret != 0) |
1052 | goto none; | |
f094c9bd | 1053 | process_slot: |
a4689d2b | 1054 | if (min_key.objectid != ino) |
4cb5300b CM |
1055 | goto none; |
1056 | if (min_key.type != BTRFS_EXTENT_DATA_KEY) | |
1057 | goto none; | |
1058 | ||
1059 | leaf = path->nodes[0]; | |
1060 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1061 | struct btrfs_file_extent_item); | |
1062 | ||
1063 | type = btrfs_file_extent_type(leaf, extent); | |
1064 | if (type == BTRFS_FILE_EXTENT_REG && | |
1065 | btrfs_file_extent_num_bytes(leaf, extent) < thresh && | |
1066 | check_defrag_in_cache(inode, min_key.offset, thresh)) { | |
1067 | *off = min_key.offset; | |
1068 | btrfs_free_path(path); | |
1069 | return 0; | |
1070 | } | |
1071 | ||
f094c9bd FM |
1072 | path->slots[0]++; |
1073 | if (path->slots[0] < btrfs_header_nritems(leaf)) { | |
1074 | btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); | |
1075 | goto process_slot; | |
1076 | } | |
1077 | ||
4cb5300b CM |
1078 | if (min_key.offset == (u64)-1) |
1079 | goto none; | |
1080 | ||
1081 | min_key.offset++; | |
1082 | btrfs_release_path(path); | |
1083 | } | |
1084 | none: | |
1085 | btrfs_free_path(path); | |
1086 | return -ENOENT; | |
1087 | } | |
1088 | ||
6c282eb4 | 1089 | static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) |
17ce6ef8 LB |
1090 | { |
1091 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
6c282eb4 LZ |
1092 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
1093 | struct extent_map *em; | |
09cbfeaf | 1094 | u64 len = PAGE_SIZE; |
17ce6ef8 | 1095 | |
6c282eb4 LZ |
1096 | /* |
1097 | * hopefully we have this extent in the tree already, try without | |
1098 | * the full extent lock | |
1099 | */ | |
17ce6ef8 | 1100 | read_lock(&em_tree->lock); |
6c282eb4 | 1101 | em = lookup_extent_mapping(em_tree, start, len); |
17ce6ef8 LB |
1102 | read_unlock(&em_tree->lock); |
1103 | ||
6c282eb4 | 1104 | if (!em) { |
308d9800 FM |
1105 | struct extent_state *cached = NULL; |
1106 | u64 end = start + len - 1; | |
1107 | ||
6c282eb4 | 1108 | /* get the big lock and read metadata off disk */ |
ff13db41 | 1109 | lock_extent_bits(io_tree, start, end, &cached); |
fc4f21b1 | 1110 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
e43bbe5e | 1111 | unlock_extent_cached(io_tree, start, end, &cached); |
6c282eb4 LZ |
1112 | |
1113 | if (IS_ERR(em)) | |
1114 | return NULL; | |
1115 | } | |
1116 | ||
1117 | return em; | |
1118 | } | |
17ce6ef8 | 1119 | |
6c282eb4 LZ |
1120 | static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) |
1121 | { | |
1122 | struct extent_map *next; | |
1123 | bool ret = true; | |
1124 | ||
1125 | /* this is the last extent */ | |
1126 | if (em->start + em->len >= i_size_read(inode)) | |
1127 | return false; | |
1128 | ||
1129 | next = defrag_lookup_extent(inode, em->start + em->len); | |
e9512d72 CM |
1130 | if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) |
1131 | ret = false; | |
1132 | else if ((em->block_start + em->block_len == next->block_start) && | |
ee22184b | 1133 | (em->block_len > SZ_128K && next->block_len > SZ_128K)) |
6c282eb4 LZ |
1134 | ret = false; |
1135 | ||
1136 | free_extent_map(next); | |
17ce6ef8 LB |
1137 | return ret; |
1138 | } | |
1139 | ||
aab110ab | 1140 | static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, |
a43a2111 AM |
1141 | u64 *last_len, u64 *skip, u64 *defrag_end, |
1142 | int compress) | |
940100a4 | 1143 | { |
6c282eb4 | 1144 | struct extent_map *em; |
940100a4 | 1145 | int ret = 1; |
6c282eb4 | 1146 | bool next_mergeable = true; |
4a3560c4 | 1147 | bool prev_mergeable = true; |
940100a4 CM |
1148 | |
1149 | /* | |
008873ea | 1150 | * make sure that once we start defragging an extent, we keep on |
940100a4 CM |
1151 | * defragging it |
1152 | */ | |
1153 | if (start < *defrag_end) | |
1154 | return 1; | |
1155 | ||
1156 | *skip = 0; | |
1157 | ||
6c282eb4 LZ |
1158 | em = defrag_lookup_extent(inode, start); |
1159 | if (!em) | |
1160 | return 0; | |
940100a4 CM |
1161 | |
1162 | /* this will cover holes, and inline extents */ | |
17ce6ef8 | 1163 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
940100a4 | 1164 | ret = 0; |
17ce6ef8 LB |
1165 | goto out; |
1166 | } | |
1167 | ||
4a3560c4 LB |
1168 | if (!*defrag_end) |
1169 | prev_mergeable = false; | |
1170 | ||
6c282eb4 | 1171 | next_mergeable = defrag_check_next_extent(inode, em); |
940100a4 | 1172 | /* |
6c282eb4 LZ |
1173 | * we hit a real extent, if it is big or the next extent is not a |
1174 | * real extent, don't bother defragging it | |
940100a4 | 1175 | */ |
a43a2111 | 1176 | if (!compress && (*last_len == 0 || *last_len >= thresh) && |
4a3560c4 | 1177 | (em->len >= thresh || (!next_mergeable && !prev_mergeable))) |
940100a4 | 1178 | ret = 0; |
17ce6ef8 | 1179 | out: |
940100a4 CM |
1180 | /* |
1181 | * last_len ends up being a counter of how many bytes we've defragged. | |
1182 | * every time we choose not to defrag an extent, we reset *last_len | |
1183 | * so that the next tiny extent will force a defrag. | |
1184 | * | |
1185 | * The end result of this is that tiny extents before a single big | |
1186 | * extent will force at least part of that big extent to be defragged. | |
1187 | */ | |
1188 | if (ret) { | |
940100a4 CM |
1189 | *defrag_end = extent_map_end(em); |
1190 | } else { | |
1191 | *last_len = 0; | |
1192 | *skip = extent_map_end(em); | |
1193 | *defrag_end = 0; | |
1194 | } | |
1195 | ||
1196 | free_extent_map(em); | |
1197 | return ret; | |
1198 | } | |
1199 | ||
4cb5300b CM |
1200 | /* |
1201 | * it doesn't do much good to defrag one or two pages | |
1202 | * at a time. This pulls in a nice chunk of pages | |
1203 | * to COW and defrag. | |
1204 | * | |
1205 | * It also makes sure the delalloc code has enough | |
1206 | * dirty data to avoid making new small extents as part | |
1207 | * of the defrag | |
1208 | * | |
1209 | * It's a good idea to start RA on this range | |
1210 | * before calling this. | |
1211 | */ | |
1212 | static int cluster_pages_for_defrag(struct inode *inode, | |
1213 | struct page **pages, | |
1214 | unsigned long start_index, | |
c41570c9 | 1215 | unsigned long num_pages) |
f46b5a66 | 1216 | { |
4cb5300b CM |
1217 | unsigned long file_end; |
1218 | u64 isize = i_size_read(inode); | |
1219 | u64 page_start; | |
1220 | u64 page_end; | |
1f12bd06 | 1221 | u64 page_cnt; |
4cb5300b CM |
1222 | int ret; |
1223 | int i; | |
1224 | int i_done; | |
3eaa2885 | 1225 | struct btrfs_ordered_extent *ordered; |
4cb5300b | 1226 | struct extent_state *cached_state = NULL; |
600a45e1 | 1227 | struct extent_io_tree *tree; |
364ecf36 | 1228 | struct extent_changeset *data_reserved = NULL; |
3b16a4e3 | 1229 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
4cb5300b | 1230 | |
09cbfeaf | 1231 | file_end = (isize - 1) >> PAGE_SHIFT; |
1f12bd06 LB |
1232 | if (!isize || start_index > file_end) |
1233 | return 0; | |
1234 | ||
1235 | page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); | |
4cb5300b | 1236 | |
364ecf36 | 1237 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
09cbfeaf KS |
1238 | start_index << PAGE_SHIFT, |
1239 | page_cnt << PAGE_SHIFT); | |
4cb5300b CM |
1240 | if (ret) |
1241 | return ret; | |
4cb5300b | 1242 | i_done = 0; |
600a45e1 | 1243 | tree = &BTRFS_I(inode)->io_tree; |
4cb5300b CM |
1244 | |
1245 | /* step one, lock all the pages */ | |
1f12bd06 | 1246 | for (i = 0; i < page_cnt; i++) { |
4cb5300b | 1247 | struct page *page; |
600a45e1 | 1248 | again: |
a94733d0 | 1249 | page = find_or_create_page(inode->i_mapping, |
600a45e1 | 1250 | start_index + i, mask); |
4cb5300b CM |
1251 | if (!page) |
1252 | break; | |
1253 | ||
600a45e1 | 1254 | page_start = page_offset(page); |
09cbfeaf | 1255 | page_end = page_start + PAGE_SIZE - 1; |
600a45e1 | 1256 | while (1) { |
308d9800 | 1257 | lock_extent_bits(tree, page_start, page_end, |
ff13db41 | 1258 | &cached_state); |
600a45e1 MX |
1259 | ordered = btrfs_lookup_ordered_extent(inode, |
1260 | page_start); | |
308d9800 | 1261 | unlock_extent_cached(tree, page_start, page_end, |
e43bbe5e | 1262 | &cached_state); |
600a45e1 MX |
1263 | if (!ordered) |
1264 | break; | |
1265 | ||
1266 | unlock_page(page); | |
1267 | btrfs_start_ordered_extent(inode, ordered, 1); | |
1268 | btrfs_put_ordered_extent(ordered); | |
1269 | lock_page(page); | |
1f12bd06 LB |
1270 | /* |
1271 | * we unlocked the page above, so we need check if | |
1272 | * it was released or not. | |
1273 | */ | |
1274 | if (page->mapping != inode->i_mapping) { | |
1275 | unlock_page(page); | |
09cbfeaf | 1276 | put_page(page); |
1f12bd06 LB |
1277 | goto again; |
1278 | } | |
600a45e1 MX |
1279 | } |
1280 | ||
4cb5300b CM |
1281 | if (!PageUptodate(page)) { |
1282 | btrfs_readpage(NULL, page); | |
1283 | lock_page(page); | |
1284 | if (!PageUptodate(page)) { | |
1285 | unlock_page(page); | |
09cbfeaf | 1286 | put_page(page); |
4cb5300b CM |
1287 | ret = -EIO; |
1288 | break; | |
1289 | } | |
1290 | } | |
600a45e1 | 1291 | |
600a45e1 MX |
1292 | if (page->mapping != inode->i_mapping) { |
1293 | unlock_page(page); | |
09cbfeaf | 1294 | put_page(page); |
600a45e1 MX |
1295 | goto again; |
1296 | } | |
1297 | ||
4cb5300b CM |
1298 | pages[i] = page; |
1299 | i_done++; | |
1300 | } | |
1301 | if (!i_done || ret) | |
1302 | goto out; | |
1303 | ||
1751e8a6 | 1304 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) |
4cb5300b CM |
1305 | goto out; |
1306 | ||
1307 | /* | |
1308 | * so now we have a nice long stream of locked | |
1309 | * and up to date pages, lets wait on them | |
1310 | */ | |
1311 | for (i = 0; i < i_done; i++) | |
1312 | wait_on_page_writeback(pages[i]); | |
1313 | ||
1314 | page_start = page_offset(pages[0]); | |
09cbfeaf | 1315 | page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE; |
4cb5300b CM |
1316 | |
1317 | lock_extent_bits(&BTRFS_I(inode)->io_tree, | |
ff13db41 | 1318 | page_start, page_end - 1, &cached_state); |
4cb5300b CM |
1319 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, |
1320 | page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | | |
9e8a4a8b | 1321 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, |
ae0f1625 | 1322 | &cached_state); |
4cb5300b | 1323 | |
1f12bd06 | 1324 | if (i_done != page_cnt) { |
9e0baf60 | 1325 | spin_lock(&BTRFS_I(inode)->lock); |
28c4a3e2 | 1326 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
9e0baf60 | 1327 | spin_unlock(&BTRFS_I(inode)->lock); |
bc42bda2 | 1328 | btrfs_delalloc_release_space(inode, data_reserved, |
09cbfeaf | 1329 | start_index << PAGE_SHIFT, |
43b18595 | 1330 | (page_cnt - i_done) << PAGE_SHIFT, true); |
4cb5300b CM |
1331 | } |
1332 | ||
1333 | ||
9e8a4a8b | 1334 | set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, |
018ed4f7 | 1335 | &cached_state); |
4cb5300b CM |
1336 | |
1337 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, | |
e43bbe5e | 1338 | page_start, page_end - 1, &cached_state); |
4cb5300b CM |
1339 | |
1340 | for (i = 0; i < i_done; i++) { | |
1341 | clear_page_dirty_for_io(pages[i]); | |
1342 | ClearPageChecked(pages[i]); | |
1343 | set_page_extent_mapped(pages[i]); | |
1344 | set_page_dirty(pages[i]); | |
1345 | unlock_page(pages[i]); | |
09cbfeaf | 1346 | put_page(pages[i]); |
4cb5300b | 1347 | } |
43b18595 QW |
1348 | btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT, |
1349 | false); | |
364ecf36 | 1350 | extent_changeset_free(data_reserved); |
4cb5300b CM |
1351 | return i_done; |
1352 | out: | |
1353 | for (i = 0; i < i_done; i++) { | |
1354 | unlock_page(pages[i]); | |
09cbfeaf | 1355 | put_page(pages[i]); |
4cb5300b | 1356 | } |
bc42bda2 | 1357 | btrfs_delalloc_release_space(inode, data_reserved, |
09cbfeaf | 1358 | start_index << PAGE_SHIFT, |
43b18595 QW |
1359 | page_cnt << PAGE_SHIFT, true); |
1360 | btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT, | |
1361 | true); | |
364ecf36 | 1362 | extent_changeset_free(data_reserved); |
4cb5300b CM |
1363 | return ret; |
1364 | ||
1365 | } | |
1366 | ||
1367 | int btrfs_defrag_file(struct inode *inode, struct file *file, | |
1368 | struct btrfs_ioctl_defrag_range_args *range, | |
1369 | u64 newer_than, unsigned long max_to_defrag) | |
1370 | { | |
0b246afa | 1371 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4cb5300b | 1372 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4cb5300b | 1373 | struct file_ra_state *ra = NULL; |
f46b5a66 | 1374 | unsigned long last_index; |
151a31b2 | 1375 | u64 isize = i_size_read(inode); |
940100a4 CM |
1376 | u64 last_len = 0; |
1377 | u64 skip = 0; | |
1378 | u64 defrag_end = 0; | |
4cb5300b | 1379 | u64 newer_off = range->start; |
f46b5a66 | 1380 | unsigned long i; |
008873ea | 1381 | unsigned long ra_index = 0; |
f46b5a66 | 1382 | int ret; |
4cb5300b | 1383 | int defrag_count = 0; |
1a419d85 | 1384 | int compress_type = BTRFS_COMPRESS_ZLIB; |
aab110ab | 1385 | u32 extent_thresh = range->extent_thresh; |
09cbfeaf | 1386 | unsigned long max_cluster = SZ_256K >> PAGE_SHIFT; |
c41570c9 | 1387 | unsigned long cluster = max_cluster; |
ee22184b | 1388 | u64 new_align = ~((u64)SZ_128K - 1); |
4cb5300b | 1389 | struct page **pages = NULL; |
1e2ef46d | 1390 | bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; |
4cb5300b | 1391 | |
0abd5b17 LB |
1392 | if (isize == 0) |
1393 | return 0; | |
1394 | ||
1395 | if (range->start >= isize) | |
1396 | return -EINVAL; | |
1a419d85 | 1397 | |
1e2ef46d | 1398 | if (do_compress) { |
1a419d85 LZ |
1399 | if (range->compress_type > BTRFS_COMPRESS_TYPES) |
1400 | return -EINVAL; | |
1401 | if (range->compress_type) | |
1402 | compress_type = range->compress_type; | |
1403 | } | |
f46b5a66 | 1404 | |
0abd5b17 | 1405 | if (extent_thresh == 0) |
ee22184b | 1406 | extent_thresh = SZ_256K; |
940100a4 | 1407 | |
4cb5300b | 1408 | /* |
0a52d108 DS |
1409 | * If we were not given a file, allocate a readahead context. As |
1410 | * readahead is just an optimization, defrag will work without it so | |
1411 | * we don't error out. | |
4cb5300b CM |
1412 | */ |
1413 | if (!file) { | |
63e727ec | 1414 | ra = kzalloc(sizeof(*ra), GFP_KERNEL); |
0a52d108 DS |
1415 | if (ra) |
1416 | file_ra_state_init(ra, inode->i_mapping); | |
4cb5300b CM |
1417 | } else { |
1418 | ra = &file->f_ra; | |
1419 | } | |
1420 | ||
63e727ec | 1421 | pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL); |
4cb5300b CM |
1422 | if (!pages) { |
1423 | ret = -ENOMEM; | |
1424 | goto out_ra; | |
1425 | } | |
1426 | ||
1427 | /* find the last page to defrag */ | |
1e701a32 | 1428 | if (range->start + range->len > range->start) { |
151a31b2 | 1429 | last_index = min_t(u64, isize - 1, |
09cbfeaf | 1430 | range->start + range->len - 1) >> PAGE_SHIFT; |
1e701a32 | 1431 | } else { |
09cbfeaf | 1432 | last_index = (isize - 1) >> PAGE_SHIFT; |
1e701a32 CM |
1433 | } |
1434 | ||
4cb5300b CM |
1435 | if (newer_than) { |
1436 | ret = find_new_extents(root, inode, newer_than, | |
ee22184b | 1437 | &newer_off, SZ_64K); |
4cb5300b CM |
1438 | if (!ret) { |
1439 | range->start = newer_off; | |
1440 | /* | |
1441 | * we always align our defrag to help keep | |
1442 | * the extents in the file evenly spaced | |
1443 | */ | |
09cbfeaf | 1444 | i = (newer_off & new_align) >> PAGE_SHIFT; |
4cb5300b CM |
1445 | } else |
1446 | goto out_ra; | |
1447 | } else { | |
09cbfeaf | 1448 | i = range->start >> PAGE_SHIFT; |
4cb5300b CM |
1449 | } |
1450 | if (!max_to_defrag) | |
070034bd | 1451 | max_to_defrag = last_index - i + 1; |
4cb5300b | 1452 | |
2a0f7f57 LZ |
1453 | /* |
1454 | * make writeback starts from i, so the defrag range can be | |
1455 | * written sequentially. | |
1456 | */ | |
1457 | if (i < inode->i_mapping->writeback_index) | |
1458 | inode->i_mapping->writeback_index = i; | |
1459 | ||
f7f43cc8 | 1460 | while (i <= last_index && defrag_count < max_to_defrag && |
09cbfeaf | 1461 | (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) { |
4cb5300b CM |
1462 | /* |
1463 | * make sure we stop running if someone unmounts | |
1464 | * the FS | |
1465 | */ | |
1751e8a6 | 1466 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) |
4cb5300b CM |
1467 | break; |
1468 | ||
0b246afa JM |
1469 | if (btrfs_defrag_cancelled(fs_info)) { |
1470 | btrfs_debug(fs_info, "defrag_file cancelled"); | |
210549eb DS |
1471 | ret = -EAGAIN; |
1472 | break; | |
1473 | } | |
1474 | ||
09cbfeaf | 1475 | if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, |
6c282eb4 | 1476 | extent_thresh, &last_len, &skip, |
1e2ef46d | 1477 | &defrag_end, do_compress)){ |
940100a4 CM |
1478 | unsigned long next; |
1479 | /* | |
1480 | * the should_defrag function tells us how much to skip | |
1481 | * bump our counter by the suggested amount | |
1482 | */ | |
09cbfeaf | 1483 | next = DIV_ROUND_UP(skip, PAGE_SIZE); |
940100a4 CM |
1484 | i = max(i + 1, next); |
1485 | continue; | |
1486 | } | |
008873ea LZ |
1487 | |
1488 | if (!newer_than) { | |
09cbfeaf KS |
1489 | cluster = (PAGE_ALIGN(defrag_end) >> |
1490 | PAGE_SHIFT) - i; | |
008873ea LZ |
1491 | cluster = min(cluster, max_cluster); |
1492 | } else { | |
1493 | cluster = max_cluster; | |
1494 | } | |
1495 | ||
008873ea LZ |
1496 | if (i + cluster > ra_index) { |
1497 | ra_index = max(i, ra_index); | |
0a52d108 | 1498 | if (ra) |
d3c0bab5 DS |
1499 | page_cache_sync_readahead(inode->i_mapping, ra, |
1500 | file, ra_index, cluster); | |
e4826a5b | 1501 | ra_index += cluster; |
008873ea | 1502 | } |
940100a4 | 1503 | |
5955102c | 1504 | inode_lock(inode); |
1e2ef46d | 1505 | if (do_compress) |
eec63c65 | 1506 | BTRFS_I(inode)->defrag_compress = compress_type; |
008873ea | 1507 | ret = cluster_pages_for_defrag(inode, pages, i, cluster); |
ecb8bea8 | 1508 | if (ret < 0) { |
5955102c | 1509 | inode_unlock(inode); |
4cb5300b | 1510 | goto out_ra; |
ecb8bea8 | 1511 | } |
4cb5300b CM |
1512 | |
1513 | defrag_count += ret; | |
d0e1d66b | 1514 | balance_dirty_pages_ratelimited(inode->i_mapping); |
5955102c | 1515 | inode_unlock(inode); |
4cb5300b CM |
1516 | |
1517 | if (newer_than) { | |
1518 | if (newer_off == (u64)-1) | |
1519 | break; | |
1520 | ||
e1f041e1 LB |
1521 | if (ret > 0) |
1522 | i += ret; | |
1523 | ||
4cb5300b | 1524 | newer_off = max(newer_off + 1, |
09cbfeaf | 1525 | (u64)i << PAGE_SHIFT); |
4cb5300b | 1526 | |
ee22184b BL |
1527 | ret = find_new_extents(root, inode, newer_than, |
1528 | &newer_off, SZ_64K); | |
4cb5300b CM |
1529 | if (!ret) { |
1530 | range->start = newer_off; | |
09cbfeaf | 1531 | i = (newer_off & new_align) >> PAGE_SHIFT; |
4cb5300b CM |
1532 | } else { |
1533 | break; | |
f46b5a66 | 1534 | } |
4cb5300b | 1535 | } else { |
008873ea | 1536 | if (ret > 0) { |
cbcc8326 | 1537 | i += ret; |
09cbfeaf | 1538 | last_len += ret << PAGE_SHIFT; |
008873ea | 1539 | } else { |
cbcc8326 | 1540 | i++; |
008873ea LZ |
1541 | last_len = 0; |
1542 | } | |
f46b5a66 | 1543 | } |
f46b5a66 CH |
1544 | } |
1545 | ||
dec8ef90 | 1546 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { |
1e701a32 | 1547 | filemap_flush(inode->i_mapping); |
dec8ef90 FM |
1548 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
1549 | &BTRFS_I(inode)->runtime_flags)) | |
1550 | filemap_flush(inode->i_mapping); | |
1551 | } | |
1e701a32 | 1552 | |
1a419d85 | 1553 | if (range->compress_type == BTRFS_COMPRESS_LZO) { |
0b246afa | 1554 | btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); |
5c1aab1d NT |
1555 | } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) { |
1556 | btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); | |
1a419d85 LZ |
1557 | } |
1558 | ||
60ccf82f | 1559 | ret = defrag_count; |
940100a4 | 1560 | |
4cb5300b | 1561 | out_ra: |
1e2ef46d | 1562 | if (do_compress) { |
5955102c | 1563 | inode_lock(inode); |
eec63c65 | 1564 | BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; |
5955102c | 1565 | inode_unlock(inode); |
633085c7 | 1566 | } |
4cb5300b CM |
1567 | if (!file) |
1568 | kfree(ra); | |
1569 | kfree(pages); | |
940100a4 | 1570 | return ret; |
f46b5a66 CH |
1571 | } |
1572 | ||
198605a8 | 1573 | static noinline int btrfs_ioctl_resize(struct file *file, |
76dda93c | 1574 | void __user *arg) |
f46b5a66 | 1575 | { |
0b246afa JM |
1576 | struct inode *inode = file_inode(file); |
1577 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
f46b5a66 CH |
1578 | u64 new_size; |
1579 | u64 old_size; | |
1580 | u64 devid = 1; | |
0b246afa | 1581 | struct btrfs_root *root = BTRFS_I(inode)->root; |
f46b5a66 CH |
1582 | struct btrfs_ioctl_vol_args *vol_args; |
1583 | struct btrfs_trans_handle *trans; | |
1584 | struct btrfs_device *device = NULL; | |
1585 | char *sizestr; | |
9a40f122 | 1586 | char *retptr; |
f46b5a66 CH |
1587 | char *devstr = NULL; |
1588 | int ret = 0; | |
f46b5a66 CH |
1589 | int mod = 0; |
1590 | ||
e441d54d CM |
1591 | if (!capable(CAP_SYS_ADMIN)) |
1592 | return -EPERM; | |
1593 | ||
198605a8 MX |
1594 | ret = mnt_want_write_file(file); |
1595 | if (ret) | |
1596 | return ret; | |
1597 | ||
171938e5 | 1598 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
97547676 | 1599 | mnt_drop_write_file(file); |
e57138b3 | 1600 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
c9e9f97b ID |
1601 | } |
1602 | ||
dae7b665 | 1603 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
1604 | if (IS_ERR(vol_args)) { |
1605 | ret = PTR_ERR(vol_args); | |
1606 | goto out; | |
1607 | } | |
5516e595 MF |
1608 | |
1609 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
f46b5a66 | 1610 | |
f46b5a66 CH |
1611 | sizestr = vol_args->name; |
1612 | devstr = strchr(sizestr, ':'); | |
1613 | if (devstr) { | |
f46b5a66 CH |
1614 | sizestr = devstr + 1; |
1615 | *devstr = '\0'; | |
1616 | devstr = vol_args->name; | |
58dfae63 Z |
1617 | ret = kstrtoull(devstr, 10, &devid); |
1618 | if (ret) | |
1619 | goto out_free; | |
dfd79829 MX |
1620 | if (!devid) { |
1621 | ret = -EINVAL; | |
1622 | goto out_free; | |
1623 | } | |
0b246afa | 1624 | btrfs_info(fs_info, "resizing devid %llu", devid); |
f46b5a66 | 1625 | } |
dba60f3f | 1626 | |
0b246afa | 1627 | device = btrfs_find_device(fs_info, devid, NULL, NULL); |
f46b5a66 | 1628 | if (!device) { |
0b246afa JM |
1629 | btrfs_info(fs_info, "resizer unable to find device %llu", |
1630 | devid); | |
dfd79829 | 1631 | ret = -ENODEV; |
c9e9f97b | 1632 | goto out_free; |
f46b5a66 | 1633 | } |
dba60f3f | 1634 | |
ebbede42 | 1635 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
0b246afa | 1636 | btrfs_info(fs_info, |
efe120a0 | 1637 | "resizer unable to apply on readonly device %llu", |
c1c9ff7c | 1638 | devid); |
dfd79829 | 1639 | ret = -EPERM; |
4e42ae1b LB |
1640 | goto out_free; |
1641 | } | |
1642 | ||
f46b5a66 CH |
1643 | if (!strcmp(sizestr, "max")) |
1644 | new_size = device->bdev->bd_inode->i_size; | |
1645 | else { | |
1646 | if (sizestr[0] == '-') { | |
1647 | mod = -1; | |
1648 | sizestr++; | |
1649 | } else if (sizestr[0] == '+') { | |
1650 | mod = 1; | |
1651 | sizestr++; | |
1652 | } | |
9a40f122 GH |
1653 | new_size = memparse(sizestr, &retptr); |
1654 | if (*retptr != '\0' || new_size == 0) { | |
f46b5a66 | 1655 | ret = -EINVAL; |
c9e9f97b | 1656 | goto out_free; |
f46b5a66 CH |
1657 | } |
1658 | } | |
1659 | ||
401e29c1 | 1660 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
dfd79829 | 1661 | ret = -EPERM; |
63a212ab SB |
1662 | goto out_free; |
1663 | } | |
1664 | ||
7cc8e58d | 1665 | old_size = btrfs_device_get_total_bytes(device); |
f46b5a66 CH |
1666 | |
1667 | if (mod < 0) { | |
1668 | if (new_size > old_size) { | |
1669 | ret = -EINVAL; | |
c9e9f97b | 1670 | goto out_free; |
f46b5a66 CH |
1671 | } |
1672 | new_size = old_size - new_size; | |
1673 | } else if (mod > 0) { | |
eb8052e0 | 1674 | if (new_size > ULLONG_MAX - old_size) { |
902c68a4 | 1675 | ret = -ERANGE; |
eb8052e0 WF |
1676 | goto out_free; |
1677 | } | |
f46b5a66 CH |
1678 | new_size = old_size + new_size; |
1679 | } | |
1680 | ||
ee22184b | 1681 | if (new_size < SZ_256M) { |
f46b5a66 | 1682 | ret = -EINVAL; |
c9e9f97b | 1683 | goto out_free; |
f46b5a66 CH |
1684 | } |
1685 | if (new_size > device->bdev->bd_inode->i_size) { | |
1686 | ret = -EFBIG; | |
c9e9f97b | 1687 | goto out_free; |
f46b5a66 CH |
1688 | } |
1689 | ||
47f08b96 | 1690 | new_size = round_down(new_size, fs_info->sectorsize); |
f46b5a66 | 1691 | |
0b246afa JM |
1692 | btrfs_info_in_rcu(fs_info, "new size for %s is %llu", |
1693 | rcu_str_deref(device->name), new_size); | |
f46b5a66 CH |
1694 | |
1695 | if (new_size > old_size) { | |
a22285a6 | 1696 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
1697 | if (IS_ERR(trans)) { |
1698 | ret = PTR_ERR(trans); | |
c9e9f97b | 1699 | goto out_free; |
98d5dc13 | 1700 | } |
f46b5a66 | 1701 | ret = btrfs_grow_device(trans, device, new_size); |
3a45bb20 | 1702 | btrfs_commit_transaction(trans); |
ece7d20e | 1703 | } else if (new_size < old_size) { |
f46b5a66 | 1704 | ret = btrfs_shrink_device(device, new_size); |
0253f40e | 1705 | } /* equal, nothing need to do */ |
f46b5a66 | 1706 | |
c9e9f97b | 1707 | out_free: |
f46b5a66 | 1708 | kfree(vol_args); |
c9e9f97b | 1709 | out: |
171938e5 | 1710 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
18f39c41 | 1711 | mnt_drop_write_file(file); |
f46b5a66 CH |
1712 | return ret; |
1713 | } | |
1714 | ||
72fd032e | 1715 | static noinline int btrfs_ioctl_snap_create_transid(struct file *file, |
52f75f4f | 1716 | const char *name, unsigned long fd, int subvol, |
6f72c7e2 | 1717 | u64 *transid, bool readonly, |
8696c533 | 1718 | struct btrfs_qgroup_inherit *inherit) |
f46b5a66 | 1719 | { |
f46b5a66 | 1720 | int namelen; |
3de4586c | 1721 | int ret = 0; |
f46b5a66 | 1722 | |
325c50e3 JM |
1723 | if (!S_ISDIR(file_inode(file)->i_mode)) |
1724 | return -ENOTDIR; | |
1725 | ||
a874a63e LB |
1726 | ret = mnt_want_write_file(file); |
1727 | if (ret) | |
1728 | goto out; | |
1729 | ||
72fd032e SW |
1730 | namelen = strlen(name); |
1731 | if (strchr(name, '/')) { | |
f46b5a66 | 1732 | ret = -EINVAL; |
a874a63e | 1733 | goto out_drop_write; |
f46b5a66 CH |
1734 | } |
1735 | ||
16780cab CM |
1736 | if (name[0] == '.' && |
1737 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { | |
1738 | ret = -EEXIST; | |
a874a63e | 1739 | goto out_drop_write; |
16780cab CM |
1740 | } |
1741 | ||
3de4586c | 1742 | if (subvol) { |
72fd032e | 1743 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
6f72c7e2 | 1744 | NULL, transid, readonly, inherit); |
cb8e7090 | 1745 | } else { |
2903ff01 | 1746 | struct fd src = fdget(fd); |
3de4586c | 1747 | struct inode *src_inode; |
2903ff01 | 1748 | if (!src.file) { |
3de4586c | 1749 | ret = -EINVAL; |
a874a63e | 1750 | goto out_drop_write; |
3de4586c CM |
1751 | } |
1752 | ||
496ad9aa AV |
1753 | src_inode = file_inode(src.file); |
1754 | if (src_inode->i_sb != file_inode(file)->i_sb) { | |
c79b4713 | 1755 | btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, |
efe120a0 | 1756 | "Snapshot src from another FS"); |
23ad5b17 | 1757 | ret = -EXDEV; |
d0242061 DS |
1758 | } else if (!inode_owner_or_capable(src_inode)) { |
1759 | /* | |
1760 | * Subvolume creation is not restricted, but snapshots | |
1761 | * are limited to own subvolumes only | |
1762 | */ | |
1763 | ret = -EPERM; | |
ecd18815 AV |
1764 | } else { |
1765 | ret = btrfs_mksubvol(&file->f_path, name, namelen, | |
1766 | BTRFS_I(src_inode)->root, | |
1767 | transid, readonly, inherit); | |
3de4586c | 1768 | } |
2903ff01 | 1769 | fdput(src); |
cb8e7090 | 1770 | } |
a874a63e LB |
1771 | out_drop_write: |
1772 | mnt_drop_write_file(file); | |
f46b5a66 | 1773 | out: |
72fd032e SW |
1774 | return ret; |
1775 | } | |
1776 | ||
1777 | static noinline int btrfs_ioctl_snap_create(struct file *file, | |
fa0d2b9b | 1778 | void __user *arg, int subvol) |
72fd032e | 1779 | { |
fa0d2b9b | 1780 | struct btrfs_ioctl_vol_args *vol_args; |
72fd032e SW |
1781 | int ret; |
1782 | ||
325c50e3 JM |
1783 | if (!S_ISDIR(file_inode(file)->i_mode)) |
1784 | return -ENOTDIR; | |
1785 | ||
fa0d2b9b LZ |
1786 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1787 | if (IS_ERR(vol_args)) | |
1788 | return PTR_ERR(vol_args); | |
1789 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
72fd032e | 1790 | |
fa0d2b9b | 1791 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
b83cc969 | 1792 | vol_args->fd, subvol, |
6f72c7e2 | 1793 | NULL, false, NULL); |
fdfb1e4f | 1794 | |
fa0d2b9b LZ |
1795 | kfree(vol_args); |
1796 | return ret; | |
1797 | } | |
fdfb1e4f | 1798 | |
fa0d2b9b LZ |
1799 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
1800 | void __user *arg, int subvol) | |
1801 | { | |
1802 | struct btrfs_ioctl_vol_args_v2 *vol_args; | |
1803 | int ret; | |
1804 | u64 transid = 0; | |
1805 | u64 *ptr = NULL; | |
b83cc969 | 1806 | bool readonly = false; |
6f72c7e2 | 1807 | struct btrfs_qgroup_inherit *inherit = NULL; |
75eaa0e2 | 1808 | |
325c50e3 JM |
1809 | if (!S_ISDIR(file_inode(file)->i_mode)) |
1810 | return -ENOTDIR; | |
1811 | ||
fa0d2b9b LZ |
1812 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1813 | if (IS_ERR(vol_args)) | |
1814 | return PTR_ERR(vol_args); | |
1815 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; | |
75eaa0e2 | 1816 | |
b83cc969 | 1817 | if (vol_args->flags & |
6f72c7e2 AJ |
1818 | ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | |
1819 | BTRFS_SUBVOL_QGROUP_INHERIT)) { | |
b83cc969 | 1820 | ret = -EOPNOTSUPP; |
c47ca32d | 1821 | goto free_args; |
72fd032e | 1822 | } |
fa0d2b9b LZ |
1823 | |
1824 | if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) | |
1825 | ptr = &transid; | |
b83cc969 LZ |
1826 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
1827 | readonly = true; | |
6f72c7e2 | 1828 | if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { |
09cbfeaf | 1829 | if (vol_args->size > PAGE_SIZE) { |
6f72c7e2 | 1830 | ret = -EINVAL; |
c47ca32d | 1831 | goto free_args; |
6f72c7e2 AJ |
1832 | } |
1833 | inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); | |
1834 | if (IS_ERR(inherit)) { | |
1835 | ret = PTR_ERR(inherit); | |
c47ca32d | 1836 | goto free_args; |
6f72c7e2 AJ |
1837 | } |
1838 | } | |
fa0d2b9b LZ |
1839 | |
1840 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, | |
6f72c7e2 | 1841 | vol_args->fd, subvol, ptr, |
8696c533 | 1842 | readonly, inherit); |
c47ca32d DC |
1843 | if (ret) |
1844 | goto free_inherit; | |
fa0d2b9b | 1845 | |
c47ca32d DC |
1846 | if (ptr && copy_to_user(arg + |
1847 | offsetof(struct btrfs_ioctl_vol_args_v2, | |
1848 | transid), | |
1849 | ptr, sizeof(*ptr))) | |
fa0d2b9b | 1850 | ret = -EFAULT; |
c47ca32d DC |
1851 | |
1852 | free_inherit: | |
6f72c7e2 | 1853 | kfree(inherit); |
c47ca32d DC |
1854 | free_args: |
1855 | kfree(vol_args); | |
f46b5a66 CH |
1856 | return ret; |
1857 | } | |
1858 | ||
0caa102d LZ |
1859 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
1860 | void __user *arg) | |
1861 | { | |
496ad9aa | 1862 | struct inode *inode = file_inode(file); |
0b246afa | 1863 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0caa102d LZ |
1864 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1865 | int ret = 0; | |
1866 | u64 flags = 0; | |
1867 | ||
4a0cc7ca | 1868 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) |
0caa102d LZ |
1869 | return -EINVAL; |
1870 | ||
0b246afa | 1871 | down_read(&fs_info->subvol_sem); |
0caa102d LZ |
1872 | if (btrfs_root_readonly(root)) |
1873 | flags |= BTRFS_SUBVOL_RDONLY; | |
0b246afa | 1874 | up_read(&fs_info->subvol_sem); |
0caa102d LZ |
1875 | |
1876 | if (copy_to_user(arg, &flags, sizeof(flags))) | |
1877 | ret = -EFAULT; | |
1878 | ||
1879 | return ret; | |
1880 | } | |
1881 | ||
1882 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, | |
1883 | void __user *arg) | |
1884 | { | |
496ad9aa | 1885 | struct inode *inode = file_inode(file); |
0b246afa | 1886 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0caa102d LZ |
1887 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1888 | struct btrfs_trans_handle *trans; | |
1889 | u64 root_flags; | |
1890 | u64 flags; | |
1891 | int ret = 0; | |
1892 | ||
bd60ea0f DS |
1893 | if (!inode_owner_or_capable(inode)) |
1894 | return -EPERM; | |
1895 | ||
b9ca0664 LB |
1896 | ret = mnt_want_write_file(file); |
1897 | if (ret) | |
1898 | goto out; | |
0caa102d | 1899 | |
4a0cc7ca | 1900 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
b9ca0664 LB |
1901 | ret = -EINVAL; |
1902 | goto out_drop_write; | |
1903 | } | |
0caa102d | 1904 | |
b9ca0664 LB |
1905 | if (copy_from_user(&flags, arg, sizeof(flags))) { |
1906 | ret = -EFAULT; | |
1907 | goto out_drop_write; | |
1908 | } | |
0caa102d | 1909 | |
b9ca0664 LB |
1910 | if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { |
1911 | ret = -EINVAL; | |
1912 | goto out_drop_write; | |
1913 | } | |
0caa102d | 1914 | |
b9ca0664 LB |
1915 | if (flags & ~BTRFS_SUBVOL_RDONLY) { |
1916 | ret = -EOPNOTSUPP; | |
1917 | goto out_drop_write; | |
1918 | } | |
0caa102d | 1919 | |
0b246afa | 1920 | down_write(&fs_info->subvol_sem); |
0caa102d LZ |
1921 | |
1922 | /* nothing to do */ | |
1923 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) | |
b9ca0664 | 1924 | goto out_drop_sem; |
0caa102d LZ |
1925 | |
1926 | root_flags = btrfs_root_flags(&root->root_item); | |
2c686537 | 1927 | if (flags & BTRFS_SUBVOL_RDONLY) { |
0caa102d LZ |
1928 | btrfs_set_root_flags(&root->root_item, |
1929 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); | |
2c686537 DS |
1930 | } else { |
1931 | /* | |
1932 | * Block RO -> RW transition if this subvolume is involved in | |
1933 | * send | |
1934 | */ | |
1935 | spin_lock(&root->root_item_lock); | |
1936 | if (root->send_in_progress == 0) { | |
1937 | btrfs_set_root_flags(&root->root_item, | |
0caa102d | 1938 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); |
2c686537 DS |
1939 | spin_unlock(&root->root_item_lock); |
1940 | } else { | |
1941 | spin_unlock(&root->root_item_lock); | |
0b246afa JM |
1942 | btrfs_warn(fs_info, |
1943 | "Attempt to set subvolume %llu read-write during send", | |
1944 | root->root_key.objectid); | |
2c686537 DS |
1945 | ret = -EPERM; |
1946 | goto out_drop_sem; | |
1947 | } | |
1948 | } | |
0caa102d LZ |
1949 | |
1950 | trans = btrfs_start_transaction(root, 1); | |
1951 | if (IS_ERR(trans)) { | |
1952 | ret = PTR_ERR(trans); | |
1953 | goto out_reset; | |
1954 | } | |
1955 | ||
0b246afa | 1956 | ret = btrfs_update_root(trans, fs_info->tree_root, |
0caa102d | 1957 | &root->root_key, &root->root_item); |
9417ebc8 NB |
1958 | if (ret < 0) { |
1959 | btrfs_end_transaction(trans); | |
1960 | goto out_reset; | |
1961 | } | |
1962 | ||
1963 | ret = btrfs_commit_transaction(trans); | |
0caa102d | 1964 | |
0caa102d LZ |
1965 | out_reset: |
1966 | if (ret) | |
1967 | btrfs_set_root_flags(&root->root_item, root_flags); | |
b9ca0664 | 1968 | out_drop_sem: |
0b246afa | 1969 | up_write(&fs_info->subvol_sem); |
b9ca0664 LB |
1970 | out_drop_write: |
1971 | mnt_drop_write_file(file); | |
1972 | out: | |
0caa102d LZ |
1973 | return ret; |
1974 | } | |
1975 | ||
ac8e9819 CM |
1976 | static noinline int key_in_sk(struct btrfs_key *key, |
1977 | struct btrfs_ioctl_search_key *sk) | |
1978 | { | |
abc6e134 CM |
1979 | struct btrfs_key test; |
1980 | int ret; | |
1981 | ||
1982 | test.objectid = sk->min_objectid; | |
1983 | test.type = sk->min_type; | |
1984 | test.offset = sk->min_offset; | |
1985 | ||
1986 | ret = btrfs_comp_cpu_keys(key, &test); | |
1987 | if (ret < 0) | |
ac8e9819 | 1988 | return 0; |
abc6e134 CM |
1989 | |
1990 | test.objectid = sk->max_objectid; | |
1991 | test.type = sk->max_type; | |
1992 | test.offset = sk->max_offset; | |
1993 | ||
1994 | ret = btrfs_comp_cpu_keys(key, &test); | |
1995 | if (ret > 0) | |
ac8e9819 CM |
1996 | return 0; |
1997 | return 1; | |
1998 | } | |
1999 | ||
df397565 | 2000 | static noinline int copy_to_sk(struct btrfs_path *path, |
ac8e9819 CM |
2001 | struct btrfs_key *key, |
2002 | struct btrfs_ioctl_search_key *sk, | |
9b6e817d | 2003 | size_t *buf_size, |
ba346b35 | 2004 | char __user *ubuf, |
ac8e9819 CM |
2005 | unsigned long *sk_offset, |
2006 | int *num_found) | |
2007 | { | |
2008 | u64 found_transid; | |
2009 | struct extent_buffer *leaf; | |
2010 | struct btrfs_ioctl_search_header sh; | |
dd81d459 | 2011 | struct btrfs_key test; |
ac8e9819 CM |
2012 | unsigned long item_off; |
2013 | unsigned long item_len; | |
2014 | int nritems; | |
2015 | int i; | |
2016 | int slot; | |
ac8e9819 CM |
2017 | int ret = 0; |
2018 | ||
2019 | leaf = path->nodes[0]; | |
2020 | slot = path->slots[0]; | |
2021 | nritems = btrfs_header_nritems(leaf); | |
2022 | ||
2023 | if (btrfs_header_generation(leaf) > sk->max_transid) { | |
2024 | i = nritems; | |
2025 | goto advance_key; | |
2026 | } | |
2027 | found_transid = btrfs_header_generation(leaf); | |
2028 | ||
2029 | for (i = slot; i < nritems; i++) { | |
2030 | item_off = btrfs_item_ptr_offset(leaf, i); | |
2031 | item_len = btrfs_item_size_nr(leaf, i); | |
2032 | ||
03b71c6c GP |
2033 | btrfs_item_key_to_cpu(leaf, key, i); |
2034 | if (!key_in_sk(key, sk)) | |
2035 | continue; | |
2036 | ||
9b6e817d | 2037 | if (sizeof(sh) + item_len > *buf_size) { |
8f5f6178 GH |
2038 | if (*num_found) { |
2039 | ret = 1; | |
2040 | goto out; | |
2041 | } | |
2042 | ||
2043 | /* | |
2044 | * return one empty item back for v1, which does not | |
2045 | * handle -EOVERFLOW | |
2046 | */ | |
2047 | ||
9b6e817d | 2048 | *buf_size = sizeof(sh) + item_len; |
ac8e9819 | 2049 | item_len = 0; |
8f5f6178 GH |
2050 | ret = -EOVERFLOW; |
2051 | } | |
ac8e9819 | 2052 | |
9b6e817d | 2053 | if (sizeof(sh) + item_len + *sk_offset > *buf_size) { |
ac8e9819 | 2054 | ret = 1; |
25c9bc2e | 2055 | goto out; |
ac8e9819 CM |
2056 | } |
2057 | ||
ac8e9819 CM |
2058 | sh.objectid = key->objectid; |
2059 | sh.offset = key->offset; | |
2060 | sh.type = key->type; | |
2061 | sh.len = item_len; | |
2062 | sh.transid = found_transid; | |
2063 | ||
2064 | /* copy search result header */ | |
ba346b35 GH |
2065 | if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { |
2066 | ret = -EFAULT; | |
2067 | goto out; | |
2068 | } | |
2069 | ||
ac8e9819 CM |
2070 | *sk_offset += sizeof(sh); |
2071 | ||
2072 | if (item_len) { | |
ba346b35 | 2073 | char __user *up = ubuf + *sk_offset; |
ac8e9819 | 2074 | /* copy the item */ |
ba346b35 GH |
2075 | if (read_extent_buffer_to_user(leaf, up, |
2076 | item_off, item_len)) { | |
2077 | ret = -EFAULT; | |
2078 | goto out; | |
2079 | } | |
2080 | ||
ac8e9819 | 2081 | *sk_offset += item_len; |
ac8e9819 | 2082 | } |
e2156867 | 2083 | (*num_found)++; |
ac8e9819 | 2084 | |
8f5f6178 GH |
2085 | if (ret) /* -EOVERFLOW from above */ |
2086 | goto out; | |
2087 | ||
25c9bc2e GH |
2088 | if (*num_found >= sk->nr_items) { |
2089 | ret = 1; | |
2090 | goto out; | |
2091 | } | |
ac8e9819 CM |
2092 | } |
2093 | advance_key: | |
abc6e134 | 2094 | ret = 0; |
dd81d459 NA |
2095 | test.objectid = sk->max_objectid; |
2096 | test.type = sk->max_type; | |
2097 | test.offset = sk->max_offset; | |
2098 | if (btrfs_comp_cpu_keys(key, &test) >= 0) | |
2099 | ret = 1; | |
2100 | else if (key->offset < (u64)-1) | |
ac8e9819 | 2101 | key->offset++; |
dd81d459 | 2102 | else if (key->type < (u8)-1) { |
abc6e134 | 2103 | key->offset = 0; |
ac8e9819 | 2104 | key->type++; |
dd81d459 | 2105 | } else if (key->objectid < (u64)-1) { |
abc6e134 CM |
2106 | key->offset = 0; |
2107 | key->type = 0; | |
ac8e9819 | 2108 | key->objectid++; |
abc6e134 CM |
2109 | } else |
2110 | ret = 1; | |
25c9bc2e | 2111 | out: |
ba346b35 GH |
2112 | /* |
2113 | * 0: all items from this leaf copied, continue with next | |
2114 | * 1: * more items can be copied, but unused buffer is too small | |
2115 | * * all items were found | |
2116 | * Either way, it will stops the loop which iterates to the next | |
2117 | * leaf | |
2118 | * -EOVERFLOW: item was to large for buffer | |
2119 | * -EFAULT: could not copy extent buffer back to userspace | |
2120 | */ | |
ac8e9819 CM |
2121 | return ret; |
2122 | } | |
2123 | ||
2124 | static noinline int search_ioctl(struct inode *inode, | |
12544442 | 2125 | struct btrfs_ioctl_search_key *sk, |
9b6e817d | 2126 | size_t *buf_size, |
ba346b35 | 2127 | char __user *ubuf) |
ac8e9819 | 2128 | { |
0b246afa | 2129 | struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); |
ac8e9819 CM |
2130 | struct btrfs_root *root; |
2131 | struct btrfs_key key; | |
ac8e9819 | 2132 | struct btrfs_path *path; |
ac8e9819 CM |
2133 | int ret; |
2134 | int num_found = 0; | |
2135 | unsigned long sk_offset = 0; | |
2136 | ||
9b6e817d GH |
2137 | if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { |
2138 | *buf_size = sizeof(struct btrfs_ioctl_search_header); | |
12544442 | 2139 | return -EOVERFLOW; |
9b6e817d | 2140 | } |
12544442 | 2141 | |
ac8e9819 CM |
2142 | path = btrfs_alloc_path(); |
2143 | if (!path) | |
2144 | return -ENOMEM; | |
2145 | ||
2146 | if (sk->tree_id == 0) { | |
2147 | /* search the root of the inode that was passed */ | |
2148 | root = BTRFS_I(inode)->root; | |
2149 | } else { | |
2150 | key.objectid = sk->tree_id; | |
2151 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2152 | key.offset = (u64)-1; | |
2153 | root = btrfs_read_fs_root_no_name(info, &key); | |
2154 | if (IS_ERR(root)) { | |
ac8e9819 | 2155 | btrfs_free_path(path); |
ad1e3d56 | 2156 | return PTR_ERR(root); |
ac8e9819 CM |
2157 | } |
2158 | } | |
2159 | ||
2160 | key.objectid = sk->min_objectid; | |
2161 | key.type = sk->min_type; | |
2162 | key.offset = sk->min_offset; | |
2163 | ||
67871254 | 2164 | while (1) { |
6174d3cb | 2165 | ret = btrfs_search_forward(root, &key, path, sk->min_transid); |
ac8e9819 CM |
2166 | if (ret != 0) { |
2167 | if (ret > 0) | |
2168 | ret = 0; | |
2169 | goto err; | |
2170 | } | |
df397565 | 2171 | ret = copy_to_sk(path, &key, sk, buf_size, ubuf, |
ac8e9819 | 2172 | &sk_offset, &num_found); |
b3b4aa74 | 2173 | btrfs_release_path(path); |
25c9bc2e | 2174 | if (ret) |
ac8e9819 CM |
2175 | break; |
2176 | ||
2177 | } | |
8f5f6178 GH |
2178 | if (ret > 0) |
2179 | ret = 0; | |
ac8e9819 CM |
2180 | err: |
2181 | sk->nr_items = num_found; | |
2182 | btrfs_free_path(path); | |
2183 | return ret; | |
2184 | } | |
2185 | ||
2186 | static noinline int btrfs_ioctl_tree_search(struct file *file, | |
2187 | void __user *argp) | |
2188 | { | |
ba346b35 GH |
2189 | struct btrfs_ioctl_search_args __user *uargs; |
2190 | struct btrfs_ioctl_search_key sk; | |
9b6e817d GH |
2191 | struct inode *inode; |
2192 | int ret; | |
2193 | size_t buf_size; | |
ac8e9819 CM |
2194 | |
2195 | if (!capable(CAP_SYS_ADMIN)) | |
2196 | return -EPERM; | |
2197 | ||
ba346b35 GH |
2198 | uargs = (struct btrfs_ioctl_search_args __user *)argp; |
2199 | ||
2200 | if (copy_from_user(&sk, &uargs->key, sizeof(sk))) | |
2201 | return -EFAULT; | |
ac8e9819 | 2202 | |
ba346b35 | 2203 | buf_size = sizeof(uargs->buf); |
ac8e9819 | 2204 | |
496ad9aa | 2205 | inode = file_inode(file); |
ba346b35 | 2206 | ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); |
8f5f6178 GH |
2207 | |
2208 | /* | |
2209 | * In the origin implementation an overflow is handled by returning a | |
2210 | * search header with a len of zero, so reset ret. | |
2211 | */ | |
2212 | if (ret == -EOVERFLOW) | |
2213 | ret = 0; | |
2214 | ||
ba346b35 | 2215 | if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) |
ac8e9819 | 2216 | ret = -EFAULT; |
ac8e9819 CM |
2217 | return ret; |
2218 | } | |
2219 | ||
cc68a8a5 GH |
2220 | static noinline int btrfs_ioctl_tree_search_v2(struct file *file, |
2221 | void __user *argp) | |
2222 | { | |
2223 | struct btrfs_ioctl_search_args_v2 __user *uarg; | |
2224 | struct btrfs_ioctl_search_args_v2 args; | |
2225 | struct inode *inode; | |
2226 | int ret; | |
2227 | size_t buf_size; | |
ee22184b | 2228 | const size_t buf_limit = SZ_16M; |
cc68a8a5 GH |
2229 | |
2230 | if (!capable(CAP_SYS_ADMIN)) | |
2231 | return -EPERM; | |
2232 | ||
2233 | /* copy search header and buffer size */ | |
2234 | uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; | |
2235 | if (copy_from_user(&args, uarg, sizeof(args))) | |
2236 | return -EFAULT; | |
2237 | ||
2238 | buf_size = args.buf_size; | |
2239 | ||
cc68a8a5 GH |
2240 | /* limit result size to 16MB */ |
2241 | if (buf_size > buf_limit) | |
2242 | buf_size = buf_limit; | |
2243 | ||
2244 | inode = file_inode(file); | |
2245 | ret = search_ioctl(inode, &args.key, &buf_size, | |
718dc5fa | 2246 | (char __user *)(&uarg->buf[0])); |
cc68a8a5 GH |
2247 | if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) |
2248 | ret = -EFAULT; | |
2249 | else if (ret == -EOVERFLOW && | |
2250 | copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) | |
2251 | ret = -EFAULT; | |
2252 | ||
ac8e9819 CM |
2253 | return ret; |
2254 | } | |
2255 | ||
98d377a0 | 2256 | /* |
ac8e9819 CM |
2257 | * Search INODE_REFs to identify path name of 'dirid' directory |
2258 | * in a 'tree_id' tree. and sets path name to 'name'. | |
2259 | */ | |
98d377a0 TH |
2260 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
2261 | u64 tree_id, u64 dirid, char *name) | |
2262 | { | |
2263 | struct btrfs_root *root; | |
2264 | struct btrfs_key key; | |
ac8e9819 | 2265 | char *ptr; |
98d377a0 TH |
2266 | int ret = -1; |
2267 | int slot; | |
2268 | int len; | |
2269 | int total_len = 0; | |
2270 | struct btrfs_inode_ref *iref; | |
2271 | struct extent_buffer *l; | |
2272 | struct btrfs_path *path; | |
2273 | ||
2274 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { | |
2275 | name[0]='\0'; | |
2276 | return 0; | |
2277 | } | |
2278 | ||
2279 | path = btrfs_alloc_path(); | |
2280 | if (!path) | |
2281 | return -ENOMEM; | |
2282 | ||
c8bcbfbd | 2283 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; |
98d377a0 TH |
2284 | |
2285 | key.objectid = tree_id; | |
2286 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2287 | key.offset = (u64)-1; | |
2288 | root = btrfs_read_fs_root_no_name(info, &key); | |
2289 | if (IS_ERR(root)) { | |
ad1e3d56 | 2290 | ret = PTR_ERR(root); |
8ad6fcab | 2291 | goto out; |
98d377a0 TH |
2292 | } |
2293 | ||
2294 | key.objectid = dirid; | |
2295 | key.type = BTRFS_INODE_REF_KEY; | |
8ad6fcab | 2296 | key.offset = (u64)-1; |
98d377a0 | 2297 | |
67871254 | 2298 | while (1) { |
98d377a0 TH |
2299 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
2300 | if (ret < 0) | |
2301 | goto out; | |
18674c6c FDBM |
2302 | else if (ret > 0) { |
2303 | ret = btrfs_previous_item(root, path, dirid, | |
2304 | BTRFS_INODE_REF_KEY); | |
2305 | if (ret < 0) | |
2306 | goto out; | |
2307 | else if (ret > 0) { | |
2308 | ret = -ENOENT; | |
2309 | goto out; | |
2310 | } | |
2311 | } | |
98d377a0 TH |
2312 | |
2313 | l = path->nodes[0]; | |
2314 | slot = path->slots[0]; | |
2315 | btrfs_item_key_to_cpu(l, &key, slot); | |
2316 | ||
98d377a0 TH |
2317 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); |
2318 | len = btrfs_inode_ref_name_len(l, iref); | |
2319 | ptr -= len + 1; | |
2320 | total_len += len + 1; | |
a696cf35 FDBM |
2321 | if (ptr < name) { |
2322 | ret = -ENAMETOOLONG; | |
98d377a0 | 2323 | goto out; |
a696cf35 | 2324 | } |
98d377a0 TH |
2325 | |
2326 | *(ptr + len) = '/'; | |
67871254 | 2327 | read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); |
98d377a0 TH |
2328 | |
2329 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) | |
2330 | break; | |
2331 | ||
b3b4aa74 | 2332 | btrfs_release_path(path); |
98d377a0 | 2333 | key.objectid = key.offset; |
8ad6fcab | 2334 | key.offset = (u64)-1; |
98d377a0 | 2335 | dirid = key.objectid; |
98d377a0 | 2336 | } |
77906a50 | 2337 | memmove(name, ptr, total_len); |
67871254 | 2338 | name[total_len] = '\0'; |
98d377a0 TH |
2339 | ret = 0; |
2340 | out: | |
2341 | btrfs_free_path(path); | |
ac8e9819 CM |
2342 | return ret; |
2343 | } | |
2344 | ||
23d0b79d TM |
2345 | static int btrfs_search_path_in_tree_user(struct inode *inode, |
2346 | struct btrfs_ioctl_ino_lookup_user_args *args) | |
2347 | { | |
2348 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
2349 | struct super_block *sb = inode->i_sb; | |
2350 | struct btrfs_key upper_limit = BTRFS_I(inode)->location; | |
2351 | u64 treeid = BTRFS_I(inode)->root->root_key.objectid; | |
2352 | u64 dirid = args->dirid; | |
2353 | unsigned long item_off; | |
2354 | unsigned long item_len; | |
2355 | struct btrfs_inode_ref *iref; | |
2356 | struct btrfs_root_ref *rref; | |
2357 | struct btrfs_root *root; | |
2358 | struct btrfs_path *path; | |
2359 | struct btrfs_key key, key2; | |
2360 | struct extent_buffer *leaf; | |
2361 | struct inode *temp_inode; | |
2362 | char *ptr; | |
2363 | int slot; | |
2364 | int len; | |
2365 | int total_len = 0; | |
2366 | int ret; | |
2367 | ||
2368 | path = btrfs_alloc_path(); | |
2369 | if (!path) | |
2370 | return -ENOMEM; | |
2371 | ||
2372 | /* | |
2373 | * If the bottom subvolume does not exist directly under upper_limit, | |
2374 | * construct the path in from the bottom up. | |
2375 | */ | |
2376 | if (dirid != upper_limit.objectid) { | |
2377 | ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1]; | |
2378 | ||
2379 | key.objectid = treeid; | |
2380 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2381 | key.offset = (u64)-1; | |
2382 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2383 | if (IS_ERR(root)) { | |
2384 | ret = PTR_ERR(root); | |
2385 | goto out; | |
2386 | } | |
2387 | ||
2388 | key.objectid = dirid; | |
2389 | key.type = BTRFS_INODE_REF_KEY; | |
2390 | key.offset = (u64)-1; | |
2391 | while (1) { | |
2392 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2393 | if (ret < 0) { | |
2394 | goto out; | |
2395 | } else if (ret > 0) { | |
2396 | ret = btrfs_previous_item(root, path, dirid, | |
2397 | BTRFS_INODE_REF_KEY); | |
2398 | if (ret < 0) { | |
2399 | goto out; | |
2400 | } else if (ret > 0) { | |
2401 | ret = -ENOENT; | |
2402 | goto out; | |
2403 | } | |
2404 | } | |
2405 | ||
2406 | leaf = path->nodes[0]; | |
2407 | slot = path->slots[0]; | |
2408 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2409 | ||
2410 | iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref); | |
2411 | len = btrfs_inode_ref_name_len(leaf, iref); | |
2412 | ptr -= len + 1; | |
2413 | total_len += len + 1; | |
2414 | if (ptr < args->path) { | |
2415 | ret = -ENAMETOOLONG; | |
2416 | goto out; | |
2417 | } | |
2418 | ||
2419 | *(ptr + len) = '/'; | |
2420 | read_extent_buffer(leaf, ptr, | |
2421 | (unsigned long)(iref + 1), len); | |
2422 | ||
2423 | /* Check the read+exec permission of this directory */ | |
2424 | ret = btrfs_previous_item(root, path, dirid, | |
2425 | BTRFS_INODE_ITEM_KEY); | |
2426 | if (ret < 0) { | |
2427 | goto out; | |
2428 | } else if (ret > 0) { | |
2429 | ret = -ENOENT; | |
2430 | goto out; | |
2431 | } | |
2432 | ||
2433 | leaf = path->nodes[0]; | |
2434 | slot = path->slots[0]; | |
2435 | btrfs_item_key_to_cpu(leaf, &key2, slot); | |
2436 | if (key2.objectid != dirid) { | |
2437 | ret = -ENOENT; | |
2438 | goto out; | |
2439 | } | |
2440 | ||
2441 | temp_inode = btrfs_iget(sb, &key2, root, NULL); | |
3ca57bd6 MT |
2442 | if (IS_ERR(temp_inode)) { |
2443 | ret = PTR_ERR(temp_inode); | |
2444 | goto out; | |
2445 | } | |
23d0b79d TM |
2446 | ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC); |
2447 | iput(temp_inode); | |
2448 | if (ret) { | |
2449 | ret = -EACCES; | |
2450 | goto out; | |
2451 | } | |
2452 | ||
2453 | if (key.offset == upper_limit.objectid) | |
2454 | break; | |
2455 | if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) { | |
2456 | ret = -EACCES; | |
2457 | goto out; | |
2458 | } | |
2459 | ||
2460 | btrfs_release_path(path); | |
2461 | key.objectid = key.offset; | |
2462 | key.offset = (u64)-1; | |
2463 | dirid = key.objectid; | |
2464 | } | |
2465 | ||
2466 | memmove(args->path, ptr, total_len); | |
2467 | args->path[total_len] = '\0'; | |
2468 | btrfs_release_path(path); | |
2469 | } | |
2470 | ||
2471 | /* Get the bottom subvolume's name from ROOT_REF */ | |
2472 | root = fs_info->tree_root; | |
2473 | key.objectid = treeid; | |
2474 | key.type = BTRFS_ROOT_REF_KEY; | |
2475 | key.offset = args->treeid; | |
2476 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2477 | if (ret < 0) { | |
2478 | goto out; | |
2479 | } else if (ret > 0) { | |
2480 | ret = -ENOENT; | |
2481 | goto out; | |
2482 | } | |
2483 | ||
2484 | leaf = path->nodes[0]; | |
2485 | slot = path->slots[0]; | |
2486 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2487 | ||
2488 | item_off = btrfs_item_ptr_offset(leaf, slot); | |
2489 | item_len = btrfs_item_size_nr(leaf, slot); | |
2490 | /* Check if dirid in ROOT_REF corresponds to passed dirid */ | |
2491 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); | |
2492 | if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) { | |
2493 | ret = -EINVAL; | |
2494 | goto out; | |
2495 | } | |
2496 | ||
2497 | /* Copy subvolume's name */ | |
2498 | item_off += sizeof(struct btrfs_root_ref); | |
2499 | item_len -= sizeof(struct btrfs_root_ref); | |
2500 | read_extent_buffer(leaf, args->name, item_off, item_len); | |
2501 | args->name[item_len] = 0; | |
2502 | ||
2503 | out: | |
2504 | btrfs_free_path(path); | |
2505 | return ret; | |
2506 | } | |
2507 | ||
ac8e9819 CM |
2508 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, |
2509 | void __user *argp) | |
2510 | { | |
bece2e82 BVA |
2511 | struct btrfs_ioctl_ino_lookup_args *args; |
2512 | struct inode *inode; | |
01b810b8 | 2513 | int ret = 0; |
ac8e9819 | 2514 | |
2354d08f JL |
2515 | args = memdup_user(argp, sizeof(*args)); |
2516 | if (IS_ERR(args)) | |
2517 | return PTR_ERR(args); | |
c2b96929 | 2518 | |
496ad9aa | 2519 | inode = file_inode(file); |
ac8e9819 | 2520 | |
01b810b8 DS |
2521 | /* |
2522 | * Unprivileged query to obtain the containing subvolume root id. The | |
2523 | * path is reset so it's consistent with btrfs_search_path_in_tree. | |
2524 | */ | |
1b53ac4d CM |
2525 | if (args->treeid == 0) |
2526 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; | |
2527 | ||
01b810b8 DS |
2528 | if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { |
2529 | args->name[0] = 0; | |
2530 | goto out; | |
2531 | } | |
2532 | ||
2533 | if (!capable(CAP_SYS_ADMIN)) { | |
2534 | ret = -EPERM; | |
2535 | goto out; | |
2536 | } | |
2537 | ||
ac8e9819 CM |
2538 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
2539 | args->treeid, args->objectid, | |
2540 | args->name); | |
2541 | ||
01b810b8 | 2542 | out: |
ac8e9819 CM |
2543 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
2544 | ret = -EFAULT; | |
2545 | ||
2546 | kfree(args); | |
98d377a0 TH |
2547 | return ret; |
2548 | } | |
2549 | ||
23d0b79d TM |
2550 | /* |
2551 | * Version of ino_lookup ioctl (unprivileged) | |
2552 | * | |
2553 | * The main differences from ino_lookup ioctl are: | |
2554 | * | |
2555 | * 1. Read + Exec permission will be checked using inode_permission() during | |
2556 | * path construction. -EACCES will be returned in case of failure. | |
2557 | * 2. Path construction will be stopped at the inode number which corresponds | |
2558 | * to the fd with which this ioctl is called. If constructed path does not | |
2559 | * exist under fd's inode, -EACCES will be returned. | |
2560 | * 3. The name of bottom subvolume is also searched and filled. | |
2561 | */ | |
2562 | static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) | |
2563 | { | |
2564 | struct btrfs_ioctl_ino_lookup_user_args *args; | |
2565 | struct inode *inode; | |
2566 | int ret; | |
2567 | ||
2568 | args = memdup_user(argp, sizeof(*args)); | |
2569 | if (IS_ERR(args)) | |
2570 | return PTR_ERR(args); | |
2571 | ||
2572 | inode = file_inode(file); | |
2573 | ||
2574 | if (args->dirid == BTRFS_FIRST_FREE_OBJECTID && | |
2575 | BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) { | |
2576 | /* | |
2577 | * The subvolume does not exist under fd with which this is | |
2578 | * called | |
2579 | */ | |
2580 | kfree(args); | |
2581 | return -EACCES; | |
2582 | } | |
2583 | ||
2584 | ret = btrfs_search_path_in_tree_user(inode, args); | |
2585 | ||
2586 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) | |
2587 | ret = -EFAULT; | |
2588 | ||
2589 | kfree(args); | |
2590 | return ret; | |
2591 | } | |
2592 | ||
b64ec075 TM |
2593 | /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ |
2594 | static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) | |
2595 | { | |
2596 | struct btrfs_ioctl_get_subvol_info_args *subvol_info; | |
2597 | struct btrfs_fs_info *fs_info; | |
2598 | struct btrfs_root *root; | |
2599 | struct btrfs_path *path; | |
2600 | struct btrfs_key key; | |
2601 | struct btrfs_root_item *root_item; | |
2602 | struct btrfs_root_ref *rref; | |
2603 | struct extent_buffer *leaf; | |
2604 | unsigned long item_off; | |
2605 | unsigned long item_len; | |
2606 | struct inode *inode; | |
2607 | int slot; | |
2608 | int ret = 0; | |
2609 | ||
2610 | path = btrfs_alloc_path(); | |
2611 | if (!path) | |
2612 | return -ENOMEM; | |
2613 | ||
2614 | subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL); | |
2615 | if (!subvol_info) { | |
2616 | btrfs_free_path(path); | |
2617 | return -ENOMEM; | |
2618 | } | |
2619 | ||
2620 | inode = file_inode(file); | |
2621 | fs_info = BTRFS_I(inode)->root->fs_info; | |
2622 | ||
2623 | /* Get root_item of inode's subvolume */ | |
2624 | key.objectid = BTRFS_I(inode)->root->root_key.objectid; | |
2625 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2626 | key.offset = (u64)-1; | |
2627 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2628 | if (IS_ERR(root)) { | |
2629 | ret = PTR_ERR(root); | |
2630 | goto out; | |
2631 | } | |
2632 | root_item = &root->root_item; | |
2633 | ||
2634 | subvol_info->treeid = key.objectid; | |
2635 | ||
2636 | subvol_info->generation = btrfs_root_generation(root_item); | |
2637 | subvol_info->flags = btrfs_root_flags(root_item); | |
2638 | ||
2639 | memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE); | |
2640 | memcpy(subvol_info->parent_uuid, root_item->parent_uuid, | |
2641 | BTRFS_UUID_SIZE); | |
2642 | memcpy(subvol_info->received_uuid, root_item->received_uuid, | |
2643 | BTRFS_UUID_SIZE); | |
2644 | ||
2645 | subvol_info->ctransid = btrfs_root_ctransid(root_item); | |
2646 | subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime); | |
2647 | subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime); | |
2648 | ||
2649 | subvol_info->otransid = btrfs_root_otransid(root_item); | |
2650 | subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime); | |
2651 | subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime); | |
2652 | ||
2653 | subvol_info->stransid = btrfs_root_stransid(root_item); | |
2654 | subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime); | |
2655 | subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime); | |
2656 | ||
2657 | subvol_info->rtransid = btrfs_root_rtransid(root_item); | |
2658 | subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime); | |
2659 | subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime); | |
2660 | ||
2661 | if (key.objectid != BTRFS_FS_TREE_OBJECTID) { | |
2662 | /* Search root tree for ROOT_BACKREF of this subvolume */ | |
2663 | root = fs_info->tree_root; | |
2664 | ||
2665 | key.type = BTRFS_ROOT_BACKREF_KEY; | |
2666 | key.offset = 0; | |
2667 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2668 | if (ret < 0) { | |
2669 | goto out; | |
2670 | } else if (path->slots[0] >= | |
2671 | btrfs_header_nritems(path->nodes[0])) { | |
2672 | ret = btrfs_next_leaf(root, path); | |
2673 | if (ret < 0) { | |
2674 | goto out; | |
2675 | } else if (ret > 0) { | |
2676 | ret = -EUCLEAN; | |
2677 | goto out; | |
2678 | } | |
2679 | } | |
2680 | ||
2681 | leaf = path->nodes[0]; | |
2682 | slot = path->slots[0]; | |
2683 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2684 | if (key.objectid == subvol_info->treeid && | |
2685 | key.type == BTRFS_ROOT_BACKREF_KEY) { | |
2686 | subvol_info->parent_id = key.offset; | |
2687 | ||
2688 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); | |
2689 | subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref); | |
2690 | ||
2691 | item_off = btrfs_item_ptr_offset(leaf, slot) | |
2692 | + sizeof(struct btrfs_root_ref); | |
2693 | item_len = btrfs_item_size_nr(leaf, slot) | |
2694 | - sizeof(struct btrfs_root_ref); | |
2695 | read_extent_buffer(leaf, subvol_info->name, | |
2696 | item_off, item_len); | |
2697 | } else { | |
2698 | ret = -ENOENT; | |
2699 | goto out; | |
2700 | } | |
2701 | } | |
2702 | ||
2703 | if (copy_to_user(argp, subvol_info, sizeof(*subvol_info))) | |
2704 | ret = -EFAULT; | |
2705 | ||
2706 | out: | |
2707 | btrfs_free_path(path); | |
2708 | kzfree(subvol_info); | |
2709 | return ret; | |
2710 | } | |
2711 | ||
42e4b520 TM |
2712 | /* |
2713 | * Return ROOT_REF information of the subvolume containing this inode | |
2714 | * except the subvolume name. | |
2715 | */ | |
2716 | static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp) | |
2717 | { | |
2718 | struct btrfs_ioctl_get_subvol_rootref_args *rootrefs; | |
2719 | struct btrfs_root_ref *rref; | |
2720 | struct btrfs_root *root; | |
2721 | struct btrfs_path *path; | |
2722 | struct btrfs_key key; | |
2723 | struct extent_buffer *leaf; | |
2724 | struct inode *inode; | |
2725 | u64 objectid; | |
2726 | int slot; | |
2727 | int ret; | |
2728 | u8 found; | |
2729 | ||
2730 | path = btrfs_alloc_path(); | |
2731 | if (!path) | |
2732 | return -ENOMEM; | |
2733 | ||
2734 | rootrefs = memdup_user(argp, sizeof(*rootrefs)); | |
2735 | if (IS_ERR(rootrefs)) { | |
2736 | btrfs_free_path(path); | |
2737 | return PTR_ERR(rootrefs); | |
2738 | } | |
2739 | ||
2740 | inode = file_inode(file); | |
2741 | root = BTRFS_I(inode)->root->fs_info->tree_root; | |
2742 | objectid = BTRFS_I(inode)->root->root_key.objectid; | |
2743 | ||
2744 | key.objectid = objectid; | |
2745 | key.type = BTRFS_ROOT_REF_KEY; | |
2746 | key.offset = rootrefs->min_treeid; | |
2747 | found = 0; | |
2748 | ||
2749 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2750 | if (ret < 0) { | |
2751 | goto out; | |
2752 | } else if (path->slots[0] >= | |
2753 | btrfs_header_nritems(path->nodes[0])) { | |
2754 | ret = btrfs_next_leaf(root, path); | |
2755 | if (ret < 0) { | |
2756 | goto out; | |
2757 | } else if (ret > 0) { | |
2758 | ret = -EUCLEAN; | |
2759 | goto out; | |
2760 | } | |
2761 | } | |
2762 | while (1) { | |
2763 | leaf = path->nodes[0]; | |
2764 | slot = path->slots[0]; | |
2765 | ||
2766 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2767 | if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) { | |
2768 | ret = 0; | |
2769 | goto out; | |
2770 | } | |
2771 | ||
2772 | if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { | |
2773 | ret = -EOVERFLOW; | |
2774 | goto out; | |
2775 | } | |
2776 | ||
2777 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); | |
2778 | rootrefs->rootref[found].treeid = key.offset; | |
2779 | rootrefs->rootref[found].dirid = | |
2780 | btrfs_root_ref_dirid(leaf, rref); | |
2781 | found++; | |
2782 | ||
2783 | ret = btrfs_next_item(root, path); | |
2784 | if (ret < 0) { | |
2785 | goto out; | |
2786 | } else if (ret > 0) { | |
2787 | ret = -EUCLEAN; | |
2788 | goto out; | |
2789 | } | |
2790 | } | |
2791 | ||
2792 | out: | |
2793 | if (!ret || ret == -EOVERFLOW) { | |
2794 | rootrefs->num_items = found; | |
2795 | /* update min_treeid for next search */ | |
2796 | if (found) | |
2797 | rootrefs->min_treeid = | |
2798 | rootrefs->rootref[found - 1].treeid + 1; | |
2799 | if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) | |
2800 | ret = -EFAULT; | |
2801 | } | |
2802 | ||
2803 | kfree(rootrefs); | |
2804 | btrfs_free_path(path); | |
2805 | ||
2806 | return ret; | |
2807 | } | |
2808 | ||
76dda93c YZ |
2809 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
2810 | void __user *arg) | |
2811 | { | |
54563d41 | 2812 | struct dentry *parent = file->f_path.dentry; |
0b246afa | 2813 | struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); |
76dda93c | 2814 | struct dentry *dentry; |
2b0143b5 | 2815 | struct inode *dir = d_inode(parent); |
76dda93c YZ |
2816 | struct inode *inode; |
2817 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
2818 | struct btrfs_root *dest = NULL; | |
2819 | struct btrfs_ioctl_vol_args *vol_args; | |
76dda93c | 2820 | int namelen; |
76dda93c YZ |
2821 | int err = 0; |
2822 | ||
325c50e3 JM |
2823 | if (!S_ISDIR(dir->i_mode)) |
2824 | return -ENOTDIR; | |
2825 | ||
76dda93c YZ |
2826 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
2827 | if (IS_ERR(vol_args)) | |
2828 | return PTR_ERR(vol_args); | |
2829 | ||
2830 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
2831 | namelen = strlen(vol_args->name); | |
2832 | if (strchr(vol_args->name, '/') || | |
2833 | strncmp(vol_args->name, "..", namelen) == 0) { | |
2834 | err = -EINVAL; | |
2835 | goto out; | |
2836 | } | |
2837 | ||
a561be71 | 2838 | err = mnt_want_write_file(file); |
76dda93c YZ |
2839 | if (err) |
2840 | goto out; | |
2841 | ||
521e0546 | 2842 | |
00235411 AV |
2843 | err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
2844 | if (err == -EINTR) | |
2845 | goto out_drop_write; | |
76dda93c YZ |
2846 | dentry = lookup_one_len(vol_args->name, parent, namelen); |
2847 | if (IS_ERR(dentry)) { | |
2848 | err = PTR_ERR(dentry); | |
2849 | goto out_unlock_dir; | |
2850 | } | |
2851 | ||
2b0143b5 | 2852 | if (d_really_is_negative(dentry)) { |
76dda93c YZ |
2853 | err = -ENOENT; |
2854 | goto out_dput; | |
2855 | } | |
2856 | ||
2b0143b5 | 2857 | inode = d_inode(dentry); |
4260f7c7 | 2858 | dest = BTRFS_I(inode)->root; |
67871254 | 2859 | if (!capable(CAP_SYS_ADMIN)) { |
4260f7c7 SW |
2860 | /* |
2861 | * Regular user. Only allow this with a special mount | |
2862 | * option, when the user has write+exec access to the | |
2863 | * subvol root, and when rmdir(2) would have been | |
2864 | * allowed. | |
2865 | * | |
2866 | * Note that this is _not_ check that the subvol is | |
2867 | * empty or doesn't contain data that we wouldn't | |
2868 | * otherwise be able to delete. | |
2869 | * | |
2870 | * Users who want to delete empty subvols should try | |
2871 | * rmdir(2). | |
2872 | */ | |
2873 | err = -EPERM; | |
0b246afa | 2874 | if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) |
4260f7c7 SW |
2875 | goto out_dput; |
2876 | ||
2877 | /* | |
2878 | * Do not allow deletion if the parent dir is the same | |
2879 | * as the dir to be deleted. That means the ioctl | |
2880 | * must be called on the dentry referencing the root | |
2881 | * of the subvol, not a random directory contained | |
2882 | * within it. | |
2883 | */ | |
2884 | err = -EINVAL; | |
2885 | if (root == dest) | |
2886 | goto out_dput; | |
2887 | ||
2888 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); | |
2889 | if (err) | |
2890 | goto out_dput; | |
4260f7c7 SW |
2891 | } |
2892 | ||
5c39da5b MX |
2893 | /* check if subvolume may be deleted by a user */ |
2894 | err = btrfs_may_delete(dir, dentry, 1); | |
2895 | if (err) | |
2896 | goto out_dput; | |
2897 | ||
4a0cc7ca | 2898 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
76dda93c YZ |
2899 | err = -EINVAL; |
2900 | goto out_dput; | |
2901 | } | |
2902 | ||
5955102c | 2903 | inode_lock(inode); |
f60a2364 | 2904 | err = btrfs_delete_subvolume(dir, dentry); |
5955102c | 2905 | inode_unlock(inode); |
f60a2364 | 2906 | if (!err) |
76dda93c | 2907 | d_delete(dentry); |
fa6ac876 | 2908 | |
76dda93c YZ |
2909 | out_dput: |
2910 | dput(dentry); | |
2911 | out_unlock_dir: | |
5955102c | 2912 | inode_unlock(dir); |
00235411 | 2913 | out_drop_write: |
2a79f17e | 2914 | mnt_drop_write_file(file); |
76dda93c YZ |
2915 | out: |
2916 | kfree(vol_args); | |
2917 | return err; | |
2918 | } | |
2919 | ||
1e701a32 | 2920 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
f46b5a66 | 2921 | { |
496ad9aa | 2922 | struct inode *inode = file_inode(file); |
f46b5a66 | 2923 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1e701a32 | 2924 | struct btrfs_ioctl_defrag_range_args *range; |
c146afad YZ |
2925 | int ret; |
2926 | ||
25122d15 ID |
2927 | ret = mnt_want_write_file(file); |
2928 | if (ret) | |
2929 | return ret; | |
b83cc969 | 2930 | |
25122d15 ID |
2931 | if (btrfs_root_readonly(root)) { |
2932 | ret = -EROFS; | |
2933 | goto out; | |
5ac00add | 2934 | } |
f46b5a66 CH |
2935 | |
2936 | switch (inode->i_mode & S_IFMT) { | |
2937 | case S_IFDIR: | |
e441d54d CM |
2938 | if (!capable(CAP_SYS_ADMIN)) { |
2939 | ret = -EPERM; | |
2940 | goto out; | |
2941 | } | |
de78b51a | 2942 | ret = btrfs_defrag_root(root); |
f46b5a66 CH |
2943 | break; |
2944 | case S_IFREG: | |
616d374e AB |
2945 | /* |
2946 | * Note that this does not check the file descriptor for write | |
2947 | * access. This prevents defragmenting executables that are | |
2948 | * running and allows defrag on files open in read-only mode. | |
2949 | */ | |
2950 | if (!capable(CAP_SYS_ADMIN) && | |
2951 | inode_permission(inode, MAY_WRITE)) { | |
2952 | ret = -EPERM; | |
e441d54d CM |
2953 | goto out; |
2954 | } | |
1e701a32 CM |
2955 | |
2956 | range = kzalloc(sizeof(*range), GFP_KERNEL); | |
2957 | if (!range) { | |
2958 | ret = -ENOMEM; | |
2959 | goto out; | |
2960 | } | |
2961 | ||
2962 | if (argp) { | |
2963 | if (copy_from_user(range, argp, | |
2964 | sizeof(*range))) { | |
2965 | ret = -EFAULT; | |
2966 | kfree(range); | |
683be16e | 2967 | goto out; |
1e701a32 CM |
2968 | } |
2969 | /* compression requires us to start the IO */ | |
2970 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { | |
2971 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; | |
2972 | range->extent_thresh = (u32)-1; | |
2973 | } | |
2974 | } else { | |
2975 | /* the rest are all set to zero by kzalloc */ | |
2976 | range->len = (u64)-1; | |
2977 | } | |
496ad9aa | 2978 | ret = btrfs_defrag_file(file_inode(file), file, |
7c829b72 | 2979 | range, BTRFS_OLDEST_GENERATION, 0); |
4cb5300b CM |
2980 | if (ret > 0) |
2981 | ret = 0; | |
1e701a32 | 2982 | kfree(range); |
f46b5a66 | 2983 | break; |
8929ecfa YZ |
2984 | default: |
2985 | ret = -EINVAL; | |
f46b5a66 | 2986 | } |
e441d54d | 2987 | out: |
25122d15 | 2988 | mnt_drop_write_file(file); |
e441d54d | 2989 | return ret; |
f46b5a66 CH |
2990 | } |
2991 | ||
2ff7e61e | 2992 | static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) |
f46b5a66 CH |
2993 | { |
2994 | struct btrfs_ioctl_vol_args *vol_args; | |
2995 | int ret; | |
2996 | ||
e441d54d CM |
2997 | if (!capable(CAP_SYS_ADMIN)) |
2998 | return -EPERM; | |
2999 | ||
171938e5 | 3000 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
e57138b3 | 3001 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
c9e9f97b | 3002 | |
dae7b665 | 3003 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
3004 | if (IS_ERR(vol_args)) { |
3005 | ret = PTR_ERR(vol_args); | |
3006 | goto out; | |
3007 | } | |
f46b5a66 | 3008 | |
5516e595 | 3009 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
2ff7e61e | 3010 | ret = btrfs_init_new_device(fs_info, vol_args->name); |
f46b5a66 | 3011 | |
43d20761 | 3012 | if (!ret) |
0b246afa | 3013 | btrfs_info(fs_info, "disk added %s", vol_args->name); |
43d20761 | 3014 | |
f46b5a66 | 3015 | kfree(vol_args); |
c9e9f97b | 3016 | out: |
171938e5 | 3017 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
f46b5a66 CH |
3018 | return ret; |
3019 | } | |
3020 | ||
6b526ed7 | 3021 | static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) |
f46b5a66 | 3022 | { |
0b246afa JM |
3023 | struct inode *inode = file_inode(file); |
3024 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
6b526ed7 | 3025 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
f46b5a66 CH |
3026 | int ret; |
3027 | ||
e441d54d CM |
3028 | if (!capable(CAP_SYS_ADMIN)) |
3029 | return -EPERM; | |
3030 | ||
da24927b MX |
3031 | ret = mnt_want_write_file(file); |
3032 | if (ret) | |
3033 | return ret; | |
c146afad | 3034 | |
dae7b665 | 3035 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
3036 | if (IS_ERR(vol_args)) { |
3037 | ret = PTR_ERR(vol_args); | |
c47ca32d | 3038 | goto err_drop; |
c9e9f97b | 3039 | } |
f46b5a66 | 3040 | |
6b526ed7 | 3041 | /* Check for compatibility reject unknown flags */ |
fd4e994b OS |
3042 | if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) { |
3043 | ret = -EOPNOTSUPP; | |
3044 | goto out; | |
3045 | } | |
f46b5a66 | 3046 | |
171938e5 | 3047 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
183860f6 AJ |
3048 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
3049 | goto out; | |
3050 | } | |
3051 | ||
735654ea | 3052 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { |
2ff7e61e | 3053 | ret = btrfs_rm_device(fs_info, NULL, vol_args->devid); |
6b526ed7 AJ |
3054 | } else { |
3055 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; | |
2ff7e61e | 3056 | ret = btrfs_rm_device(fs_info, vol_args->name, 0); |
6b526ed7 | 3057 | } |
171938e5 | 3058 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
183860f6 | 3059 | |
6b526ed7 | 3060 | if (!ret) { |
735654ea | 3061 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) |
0b246afa | 3062 | btrfs_info(fs_info, "device deleted: id %llu", |
6b526ed7 AJ |
3063 | vol_args->devid); |
3064 | else | |
0b246afa | 3065 | btrfs_info(fs_info, "device deleted: %s", |
6b526ed7 AJ |
3066 | vol_args->name); |
3067 | } | |
183860f6 AJ |
3068 | out: |
3069 | kfree(vol_args); | |
c47ca32d | 3070 | err_drop: |
4ac20c70 | 3071 | mnt_drop_write_file(file); |
f46b5a66 CH |
3072 | return ret; |
3073 | } | |
3074 | ||
da24927b | 3075 | static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) |
f46b5a66 | 3076 | { |
0b246afa JM |
3077 | struct inode *inode = file_inode(file); |
3078 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
f46b5a66 CH |
3079 | struct btrfs_ioctl_vol_args *vol_args; |
3080 | int ret; | |
3081 | ||
e441d54d CM |
3082 | if (!capable(CAP_SYS_ADMIN)) |
3083 | return -EPERM; | |
3084 | ||
da24927b MX |
3085 | ret = mnt_want_write_file(file); |
3086 | if (ret) | |
3087 | return ret; | |
c146afad | 3088 | |
171938e5 | 3089 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
183860f6 | 3090 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
58d7bbf8 DS |
3091 | goto out_drop_write; |
3092 | } | |
3093 | ||
3094 | vol_args = memdup_user(arg, sizeof(*vol_args)); | |
3095 | if (IS_ERR(vol_args)) { | |
3096 | ret = PTR_ERR(vol_args); | |
183860f6 AJ |
3097 | goto out; |
3098 | } | |
3099 | ||
58d7bbf8 | 3100 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
2ff7e61e | 3101 | ret = btrfs_rm_device(fs_info, vol_args->name, 0); |
183860f6 | 3102 | |
ec95d491 | 3103 | if (!ret) |
0b246afa | 3104 | btrfs_info(fs_info, "disk deleted %s", vol_args->name); |
183860f6 | 3105 | kfree(vol_args); |
58d7bbf8 | 3106 | out: |
171938e5 | 3107 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
58d7bbf8 | 3108 | out_drop_write: |
4ac20c70 | 3109 | mnt_drop_write_file(file); |
58d7bbf8 | 3110 | |
f46b5a66 CH |
3111 | return ret; |
3112 | } | |
3113 | ||
2ff7e61e JM |
3114 | static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, |
3115 | void __user *arg) | |
475f6387 | 3116 | { |
027ed2f0 | 3117 | struct btrfs_ioctl_fs_info_args *fi_args; |
475f6387 | 3118 | struct btrfs_device *device; |
0b246afa | 3119 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
027ed2f0 | 3120 | int ret = 0; |
475f6387 | 3121 | |
027ed2f0 LZ |
3122 | fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); |
3123 | if (!fi_args) | |
3124 | return -ENOMEM; | |
3125 | ||
d03262c7 | 3126 | rcu_read_lock(); |
027ed2f0 | 3127 | fi_args->num_devices = fs_devices->num_devices; |
475f6387 | 3128 | |
d03262c7 | 3129 | list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { |
027ed2f0 LZ |
3130 | if (device->devid > fi_args->max_id) |
3131 | fi_args->max_id = device->devid; | |
475f6387 | 3132 | } |
d03262c7 | 3133 | rcu_read_unlock(); |
475f6387 | 3134 | |
d03262c7 | 3135 | memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid)); |
bea7eafd OS |
3136 | fi_args->nodesize = fs_info->nodesize; |
3137 | fi_args->sectorsize = fs_info->sectorsize; | |
3138 | fi_args->clone_alignment = fs_info->sectorsize; | |
80a773fb | 3139 | |
027ed2f0 LZ |
3140 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) |
3141 | ret = -EFAULT; | |
475f6387 | 3142 | |
027ed2f0 LZ |
3143 | kfree(fi_args); |
3144 | return ret; | |
475f6387 JS |
3145 | } |
3146 | ||
2ff7e61e JM |
3147 | static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, |
3148 | void __user *arg) | |
475f6387 JS |
3149 | { |
3150 | struct btrfs_ioctl_dev_info_args *di_args; | |
3151 | struct btrfs_device *dev; | |
475f6387 JS |
3152 | int ret = 0; |
3153 | char *s_uuid = NULL; | |
475f6387 | 3154 | |
475f6387 JS |
3155 | di_args = memdup_user(arg, sizeof(*di_args)); |
3156 | if (IS_ERR(di_args)) | |
3157 | return PTR_ERR(di_args); | |
3158 | ||
dd5f9615 | 3159 | if (!btrfs_is_empty_uuid(di_args->uuid)) |
475f6387 JS |
3160 | s_uuid = di_args->uuid; |
3161 | ||
c5593ca3 | 3162 | rcu_read_lock(); |
0b246afa | 3163 | dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL); |
475f6387 JS |
3164 | |
3165 | if (!dev) { | |
3166 | ret = -ENODEV; | |
3167 | goto out; | |
3168 | } | |
3169 | ||
3170 | di_args->devid = dev->devid; | |
7cc8e58d MX |
3171 | di_args->bytes_used = btrfs_device_get_bytes_used(dev); |
3172 | di_args->total_bytes = btrfs_device_get_total_bytes(dev); | |
475f6387 | 3173 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); |
a27202fb | 3174 | if (dev->name) { |
672d5990 MT |
3175 | strncpy(di_args->path, rcu_str_deref(dev->name), |
3176 | sizeof(di_args->path) - 1); | |
a27202fb JM |
3177 | di_args->path[sizeof(di_args->path) - 1] = 0; |
3178 | } else { | |
99ba55ad | 3179 | di_args->path[0] = '\0'; |
a27202fb | 3180 | } |
475f6387 JS |
3181 | |
3182 | out: | |
c5593ca3 | 3183 | rcu_read_unlock(); |
475f6387 JS |
3184 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) |
3185 | ret = -EFAULT; | |
3186 | ||
3187 | kfree(di_args); | |
3188 | return ret; | |
3189 | } | |
3190 | ||
f4414602 | 3191 | static struct page *extent_same_get_page(struct inode *inode, pgoff_t index) |
416161db MF |
3192 | { |
3193 | struct page *page; | |
416161db | 3194 | |
416161db MF |
3195 | page = grab_cache_page(inode->i_mapping, index); |
3196 | if (!page) | |
31314002 | 3197 | return ERR_PTR(-ENOMEM); |
416161db MF |
3198 | |
3199 | if (!PageUptodate(page)) { | |
31314002 FM |
3200 | int ret; |
3201 | ||
3202 | ret = btrfs_readpage(NULL, page); | |
3203 | if (ret) | |
3204 | return ERR_PTR(ret); | |
416161db MF |
3205 | lock_page(page); |
3206 | if (!PageUptodate(page)) { | |
3207 | unlock_page(page); | |
09cbfeaf | 3208 | put_page(page); |
31314002 FM |
3209 | return ERR_PTR(-EIO); |
3210 | } | |
3211 | if (page->mapping != inode->i_mapping) { | |
3212 | unlock_page(page); | |
09cbfeaf | 3213 | put_page(page); |
31314002 | 3214 | return ERR_PTR(-EAGAIN); |
416161db MF |
3215 | } |
3216 | } | |
416161db MF |
3217 | |
3218 | return page; | |
3219 | } | |
3220 | ||
f4414602 MF |
3221 | static int gather_extent_pages(struct inode *inode, struct page **pages, |
3222 | int num_pages, u64 off) | |
3223 | { | |
3224 | int i; | |
09cbfeaf | 3225 | pgoff_t index = off >> PAGE_SHIFT; |
f4414602 MF |
3226 | |
3227 | for (i = 0; i < num_pages; i++) { | |
31314002 | 3228 | again: |
f4414602 | 3229 | pages[i] = extent_same_get_page(inode, index + i); |
31314002 FM |
3230 | if (IS_ERR(pages[i])) { |
3231 | int err = PTR_ERR(pages[i]); | |
3232 | ||
3233 | if (err == -EAGAIN) | |
3234 | goto again; | |
3235 | pages[i] = NULL; | |
3236 | return err; | |
3237 | } | |
f4414602 MF |
3238 | } |
3239 | return 0; | |
3240 | } | |
3241 | ||
e0bd70c6 FM |
3242 | static int lock_extent_range(struct inode *inode, u64 off, u64 len, |
3243 | bool retry_range_locking) | |
77fe20dc | 3244 | { |
e0bd70c6 FM |
3245 | /* |
3246 | * Do any pending delalloc/csum calculations on inode, one way or | |
3247 | * another, and lock file content. | |
3248 | * The locking order is: | |
3249 | * | |
3250 | * 1) pages | |
3251 | * 2) range in the inode's io tree | |
3252 | */ | |
77fe20dc MF |
3253 | while (1) { |
3254 | struct btrfs_ordered_extent *ordered; | |
3255 | lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); | |
3256 | ordered = btrfs_lookup_first_ordered_extent(inode, | |
3257 | off + len - 1); | |
ff5df9b8 FM |
3258 | if ((!ordered || |
3259 | ordered->file_offset + ordered->len <= off || | |
3260 | ordered->file_offset >= off + len) && | |
77fe20dc | 3261 | !test_range_bit(&BTRFS_I(inode)->io_tree, off, |
ff5df9b8 FM |
3262 | off + len - 1, EXTENT_DELALLOC, 0, NULL)) { |
3263 | if (ordered) | |
3264 | btrfs_put_ordered_extent(ordered); | |
77fe20dc | 3265 | break; |
ff5df9b8 | 3266 | } |
77fe20dc MF |
3267 | unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); |
3268 | if (ordered) | |
3269 | btrfs_put_ordered_extent(ordered); | |
e0bd70c6 FM |
3270 | if (!retry_range_locking) |
3271 | return -EAGAIN; | |
77fe20dc MF |
3272 | btrfs_wait_ordered_range(inode, off, len); |
3273 | } | |
e0bd70c6 | 3274 | return 0; |
77fe20dc MF |
3275 | } |
3276 | ||
f4414602 | 3277 | static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2) |
416161db | 3278 | { |
5955102c AV |
3279 | inode_unlock(inode1); |
3280 | inode_unlock(inode2); | |
416161db MF |
3281 | } |
3282 | ||
f4414602 MF |
3283 | static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2) |
3284 | { | |
3285 | if (inode1 < inode2) | |
3286 | swap(inode1, inode2); | |
3287 | ||
5955102c AV |
3288 | inode_lock_nested(inode1, I_MUTEX_PARENT); |
3289 | inode_lock_nested(inode2, I_MUTEX_CHILD); | |
f4414602 MF |
3290 | } |
3291 | ||
3292 | static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, | |
3293 | struct inode *inode2, u64 loff2, u64 len) | |
3294 | { | |
3295 | unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); | |
3296 | unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); | |
3297 | } | |
3298 | ||
e0bd70c6 FM |
3299 | static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1, |
3300 | struct inode *inode2, u64 loff2, u64 len, | |
3301 | bool retry_range_locking) | |
416161db | 3302 | { |
e0bd70c6 FM |
3303 | int ret; |
3304 | ||
416161db MF |
3305 | if (inode1 < inode2) { |
3306 | swap(inode1, inode2); | |
3307 | swap(loff1, loff2); | |
3308 | } | |
e0bd70c6 FM |
3309 | ret = lock_extent_range(inode1, loff1, len, retry_range_locking); |
3310 | if (ret) | |
3311 | return ret; | |
3312 | ret = lock_extent_range(inode2, loff2, len, retry_range_locking); | |
3313 | if (ret) | |
3314 | unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, | |
3315 | loff1 + len - 1); | |
3316 | return ret; | |
f4414602 MF |
3317 | } |
3318 | ||
3319 | struct cmp_pages { | |
3320 | int num_pages; | |
3321 | struct page **src_pages; | |
3322 | struct page **dst_pages; | |
3323 | }; | |
3324 | ||
3325 | static void btrfs_cmp_data_free(struct cmp_pages *cmp) | |
3326 | { | |
3327 | int i; | |
3328 | struct page *pg; | |
3329 | ||
3330 | for (i = 0; i < cmp->num_pages; i++) { | |
3331 | pg = cmp->src_pages[i]; | |
e0bd70c6 FM |
3332 | if (pg) { |
3333 | unlock_page(pg); | |
09cbfeaf | 3334 | put_page(pg); |
97b19170 | 3335 | cmp->src_pages[i] = NULL; |
e0bd70c6 | 3336 | } |
f4414602 | 3337 | pg = cmp->dst_pages[i]; |
e0bd70c6 FM |
3338 | if (pg) { |
3339 | unlock_page(pg); | |
09cbfeaf | 3340 | put_page(pg); |
97b19170 | 3341 | cmp->dst_pages[i] = NULL; |
e0bd70c6 | 3342 | } |
f4414602 | 3343 | } |
f4414602 MF |
3344 | } |
3345 | ||
3346 | static int btrfs_cmp_data_prepare(struct inode *src, u64 loff, | |
3347 | struct inode *dst, u64 dst_loff, | |
3348 | u64 len, struct cmp_pages *cmp) | |
3349 | { | |
3350 | int ret; | |
09cbfeaf | 3351 | int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT; |
f4414602 | 3352 | |
f4414602 | 3353 | cmp->num_pages = num_pages; |
b1517622 | 3354 | |
67b07bd4 | 3355 | ret = gather_extent_pages(src, cmp->src_pages, num_pages, loff); |
f4414602 MF |
3356 | if (ret) |
3357 | goto out; | |
3358 | ||
67b07bd4 | 3359 | ret = gather_extent_pages(dst, cmp->dst_pages, num_pages, dst_loff); |
f4414602 MF |
3360 | |
3361 | out: | |
3362 | if (ret) | |
3363 | btrfs_cmp_data_free(cmp); | |
78ad4ce0 | 3364 | return ret; |
416161db MF |
3365 | } |
3366 | ||
1a287cfe | 3367 | static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp) |
416161db MF |
3368 | { |
3369 | int ret = 0; | |
f4414602 | 3370 | int i; |
416161db | 3371 | struct page *src_page, *dst_page; |
09cbfeaf | 3372 | unsigned int cmp_len = PAGE_SIZE; |
416161db MF |
3373 | void *addr, *dst_addr; |
3374 | ||
f4414602 | 3375 | i = 0; |
416161db | 3376 | while (len) { |
09cbfeaf | 3377 | if (len < PAGE_SIZE) |
416161db MF |
3378 | cmp_len = len; |
3379 | ||
f4414602 MF |
3380 | BUG_ON(i >= cmp->num_pages); |
3381 | ||
3382 | src_page = cmp->src_pages[i]; | |
3383 | dst_page = cmp->dst_pages[i]; | |
e0bd70c6 FM |
3384 | ASSERT(PageLocked(src_page)); |
3385 | ASSERT(PageLocked(dst_page)); | |
f4414602 | 3386 | |
416161db MF |
3387 | addr = kmap_atomic(src_page); |
3388 | dst_addr = kmap_atomic(dst_page); | |
3389 | ||
3390 | flush_dcache_page(src_page); | |
3391 | flush_dcache_page(dst_page); | |
3392 | ||
3393 | if (memcmp(addr, dst_addr, cmp_len)) | |
2b3909f8 | 3394 | ret = -EBADE; |
416161db MF |
3395 | |
3396 | kunmap_atomic(addr); | |
3397 | kunmap_atomic(dst_addr); | |
416161db MF |
3398 | |
3399 | if (ret) | |
3400 | break; | |
3401 | ||
416161db | 3402 | len -= cmp_len; |
f4414602 | 3403 | i++; |
416161db MF |
3404 | } |
3405 | ||
3406 | return ret; | |
3407 | } | |
3408 | ||
e1d227a4 MF |
3409 | static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen, |
3410 | u64 olen) | |
416161db | 3411 | { |
e1d227a4 | 3412 | u64 len = *plen; |
416161db MF |
3413 | u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; |
3414 | ||
e1d227a4 | 3415 | if (off + olen > inode->i_size || off + olen < off) |
416161db | 3416 | return -EINVAL; |
e1d227a4 MF |
3417 | |
3418 | /* if we extend to eof, continue to block boundary */ | |
3419 | if (off + len == inode->i_size) | |
3420 | *plen = len = ALIGN(inode->i_size, bs) - off; | |
3421 | ||
416161db MF |
3422 | /* Check that we are block aligned - btrfs_clone() requires this */ |
3423 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) | |
3424 | return -EINVAL; | |
3425 | ||
3426 | return 0; | |
3427 | } | |
3428 | ||
3973909d | 3429 | static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen, |
67b07bd4 TT |
3430 | struct inode *dst, u64 dst_loff, |
3431 | struct cmp_pages *cmp) | |
416161db MF |
3432 | { |
3433 | int ret; | |
e1d227a4 | 3434 | u64 len = olen; |
fc4badd9 | 3435 | bool same_inode = (src == dst); |
0efa9f48 MF |
3436 | u64 same_lock_start = 0; |
3437 | u64 same_lock_len = 0; | |
416161db | 3438 | |
fc4badd9 OS |
3439 | ret = extent_same_check_offsets(src, loff, &len, olen); |
3440 | if (ret) | |
3973909d | 3441 | return ret; |
416161db | 3442 | |
fc4badd9 OS |
3443 | ret = extent_same_check_offsets(dst, dst_loff, &len, olen); |
3444 | if (ret) | |
3973909d | 3445 | return ret; |
fc4badd9 OS |
3446 | |
3447 | if (same_inode) { | |
0efa9f48 MF |
3448 | /* |
3449 | * Single inode case wants the same checks, except we | |
3450 | * don't want our length pushed out past i_size as | |
3451 | * comparing that data range makes no sense. | |
3452 | * | |
3453 | * extent_same_check_offsets() will do this for an | |
3454 | * unaligned length at i_size, so catch it here and | |
3455 | * reject the request. | |
3456 | * | |
3457 | * This effectively means we require aligned extents | |
3458 | * for the single-inode case, whereas the other cases | |
3459 | * allow an unaligned length so long as it ends at | |
3460 | * i_size. | |
3461 | */ | |
3973909d TT |
3462 | if (len != olen) |
3463 | return -EINVAL; | |
0efa9f48 MF |
3464 | |
3465 | /* Check for overlapping ranges */ | |
3973909d TT |
3466 | if (dst_loff + len > loff && dst_loff < loff + len) |
3467 | return -EINVAL; | |
0efa9f48 MF |
3468 | |
3469 | same_lock_start = min_t(u64, loff, dst_loff); | |
3470 | same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start; | |
de02b9f6 FM |
3471 | } else { |
3472 | /* | |
3473 | * If the source and destination inodes are different, the | |
3474 | * source's range end offset matches the source's i_size, that | |
3475 | * i_size is not a multiple of the sector size, and the | |
3476 | * destination range does not go past the destination's i_size, | |
3477 | * we must round down the length to the nearest sector size | |
3478 | * multiple. If we don't do this adjustment we end replacing | |
3479 | * with zeroes the bytes in the range that starts at the | |
3480 | * deduplication range's end offset and ends at the next sector | |
3481 | * size multiple. | |
3482 | */ | |
3483 | if (loff + olen == i_size_read(src) && | |
3484 | dst_loff + len < i_size_read(dst)) { | |
3485 | const u64 sz = BTRFS_I(src)->root->fs_info->sectorsize; | |
3486 | ||
3487 | len = round_down(i_size_read(src), sz) - loff; | |
3488 | olen = len; | |
3489 | } | |
0efa9f48 | 3490 | } |
416161db | 3491 | |
e0bd70c6 | 3492 | again: |
67b07bd4 | 3493 | ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, cmp); |
f4414602 | 3494 | if (ret) |
3973909d | 3495 | return ret; |
f4414602 | 3496 | |
0efa9f48 | 3497 | if (same_inode) |
e0bd70c6 FM |
3498 | ret = lock_extent_range(src, same_lock_start, same_lock_len, |
3499 | false); | |
0efa9f48 | 3500 | else |
e0bd70c6 FM |
3501 | ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len, |
3502 | false); | |
3503 | /* | |
3504 | * If one of the inodes has dirty pages in the respective range or | |
3505 | * ordered extents, we need to flush dellaloc and wait for all ordered | |
3506 | * extents in the range. We must unlock the pages and the ranges in the | |
3507 | * io trees to avoid deadlocks when flushing delalloc (requires locking | |
3508 | * pages) and when waiting for ordered extents to complete (they require | |
3509 | * range locking). | |
3510 | */ | |
3511 | if (ret == -EAGAIN) { | |
3512 | /* | |
3513 | * Ranges in the io trees already unlocked. Now unlock all | |
3514 | * pages before waiting for all IO to complete. | |
3515 | */ | |
67b07bd4 | 3516 | btrfs_cmp_data_free(cmp); |
e0bd70c6 FM |
3517 | if (same_inode) { |
3518 | btrfs_wait_ordered_range(src, same_lock_start, | |
3519 | same_lock_len); | |
3520 | } else { | |
3521 | btrfs_wait_ordered_range(src, loff, len); | |
3522 | btrfs_wait_ordered_range(dst, dst_loff, len); | |
3523 | } | |
3524 | goto again; | |
3525 | } | |
3526 | ASSERT(ret == 0); | |
3527 | if (WARN_ON(ret)) { | |
3528 | /* ranges in the io trees already unlocked */ | |
67b07bd4 | 3529 | btrfs_cmp_data_free(cmp); |
e0bd70c6 FM |
3530 | return ret; |
3531 | } | |
f4414602 | 3532 | |
207910dd | 3533 | /* pass original length for comparison so we stay within i_size */ |
67b07bd4 | 3534 | ret = btrfs_cmp_data(olen, cmp); |
416161db | 3535 | if (ret == 0) |
1c919a5e | 3536 | ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1); |
416161db | 3537 | |
0efa9f48 MF |
3538 | if (same_inode) |
3539 | unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start, | |
3540 | same_lock_start + same_lock_len - 1); | |
3541 | else | |
3542 | btrfs_double_extent_unlock(src, loff, dst, dst_loff, len); | |
f4414602 | 3543 | |
67b07bd4 | 3544 | btrfs_cmp_data_free(cmp); |
3973909d TT |
3545 | |
3546 | return ret; | |
3547 | } | |
3548 | ||
b6728768 TT |
3549 | #define BTRFS_MAX_DEDUPE_LEN SZ_16M |
3550 | ||
3973909d TT |
3551 | static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, |
3552 | struct inode *dst, u64 dst_loff) | |
3553 | { | |
3554 | int ret; | |
67b07bd4 TT |
3555 | struct cmp_pages cmp; |
3556 | int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT; | |
3973909d | 3557 | bool same_inode = (src == dst); |
b6728768 | 3558 | u64 i, tail_len, chunk_count; |
3973909d TT |
3559 | |
3560 | if (olen == 0) | |
3561 | return 0; | |
3562 | ||
3563 | if (same_inode) | |
3564 | inode_lock(src); | |
3565 | else | |
3566 | btrfs_double_inode_lock(src, dst); | |
3567 | ||
3568 | /* don't make the dst file partly checksummed */ | |
3569 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != | |
3570 | (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { | |
3571 | ret = -EINVAL; | |
3572 | goto out_unlock; | |
3573 | } | |
3574 | ||
b6728768 TT |
3575 | tail_len = olen % BTRFS_MAX_DEDUPE_LEN; |
3576 | chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN); | |
67b07bd4 TT |
3577 | if (chunk_count == 0) |
3578 | num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT; | |
3579 | ||
3580 | /* | |
3581 | * If deduping ranges in the same inode, locking rules make it | |
3582 | * mandatory to always lock pages in ascending order to avoid deadlocks | |
3583 | * with concurrent tasks (such as starting writeback/delalloc). | |
3584 | */ | |
3585 | if (same_inode && dst_loff < loff) | |
3586 | swap(loff, dst_loff); | |
3587 | ||
3588 | /* | |
3589 | * We must gather up all the pages before we initiate our extent | |
3590 | * locking. We use an array for the page pointers. Size of the array is | |
3591 | * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN. | |
3592 | */ | |
bf5091c8 DS |
3593 | cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *), |
3594 | GFP_KERNEL | __GFP_ZERO); | |
3595 | cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *), | |
3596 | GFP_KERNEL | __GFP_ZERO); | |
67b07bd4 | 3597 | if (!cmp.src_pages || !cmp.dst_pages) { |
bf5091c8 DS |
3598 | ret = -ENOMEM; |
3599 | goto out_free; | |
67b07bd4 | 3600 | } |
b6728768 TT |
3601 | |
3602 | for (i = 0; i < chunk_count; i++) { | |
3603 | ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN, | |
67b07bd4 | 3604 | dst, dst_loff, &cmp); |
b6728768 | 3605 | if (ret) |
22883ddc | 3606 | goto out_free; |
b6728768 TT |
3607 | |
3608 | loff += BTRFS_MAX_DEDUPE_LEN; | |
3609 | dst_loff += BTRFS_MAX_DEDUPE_LEN; | |
3610 | } | |
3611 | ||
3612 | if (tail_len > 0) | |
67b07bd4 TT |
3613 | ret = btrfs_extent_same_range(src, loff, tail_len, dst, |
3614 | dst_loff, &cmp); | |
3973909d | 3615 | |
22883ddc LF |
3616 | out_free: |
3617 | kvfree(cmp.src_pages); | |
3618 | kvfree(cmp.dst_pages); | |
3619 | ||
416161db | 3620 | out_unlock: |
0efa9f48 | 3621 | if (same_inode) |
5955102c | 3622 | inode_unlock(src); |
0efa9f48 MF |
3623 | else |
3624 | btrfs_double_inode_unlock(src, dst); | |
416161db MF |
3625 | |
3626 | return ret; | |
3627 | } | |
3628 | ||
87eb5eb2 MS |
3629 | int btrfs_dedupe_file_range(struct file *src_file, loff_t src_loff, |
3630 | struct file *dst_file, loff_t dst_loff, | |
3631 | u64 olen) | |
416161db | 3632 | { |
2b3909f8 DW |
3633 | struct inode *src = file_inode(src_file); |
3634 | struct inode *dst = file_inode(dst_file); | |
416161db | 3635 | u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; |
416161db | 3636 | |
09cbfeaf | 3637 | if (WARN_ON_ONCE(bs < PAGE_SIZE)) { |
416161db MF |
3638 | /* |
3639 | * Btrfs does not support blocksize < page_size. As a | |
3640 | * result, btrfs_cmp_data() won't correctly handle | |
3641 | * this situation without an update. | |
3642 | */ | |
2b3909f8 | 3643 | return -EINVAL; |
416161db MF |
3644 | } |
3645 | ||
87eb5eb2 | 3646 | return btrfs_extent_same(src, src_loff, olen, dst, dst_loff); |
416161db MF |
3647 | } |
3648 | ||
f82a9901 FM |
3649 | static int clone_finish_inode_update(struct btrfs_trans_handle *trans, |
3650 | struct inode *inode, | |
3651 | u64 endoff, | |
3652 | const u64 destoff, | |
1c919a5e MF |
3653 | const u64 olen, |
3654 | int no_time_update) | |
f82a9901 FM |
3655 | { |
3656 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3657 | int ret; | |
3658 | ||
3659 | inode_inc_iversion(inode); | |
1c919a5e | 3660 | if (!no_time_update) |
c2050a45 | 3661 | inode->i_mtime = inode->i_ctime = current_time(inode); |
f82a9901 FM |
3662 | /* |
3663 | * We round up to the block size at eof when determining which | |
3664 | * extents to clone above, but shouldn't round up the file size. | |
3665 | */ | |
3666 | if (endoff > destoff + olen) | |
3667 | endoff = destoff + olen; | |
3668 | if (endoff > inode->i_size) | |
6ef06d27 | 3669 | btrfs_i_size_write(BTRFS_I(inode), endoff); |
f82a9901 FM |
3670 | |
3671 | ret = btrfs_update_inode(trans, root, inode); | |
3672 | if (ret) { | |
66642832 | 3673 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 3674 | btrfs_end_transaction(trans); |
f82a9901 FM |
3675 | goto out; |
3676 | } | |
3a45bb20 | 3677 | ret = btrfs_end_transaction(trans); |
f82a9901 FM |
3678 | out: |
3679 | return ret; | |
3680 | } | |
3681 | ||
a2f392e4 | 3682 | static void clone_update_extent_map(struct btrfs_inode *inode, |
7ffbb598 FM |
3683 | const struct btrfs_trans_handle *trans, |
3684 | const struct btrfs_path *path, | |
7ffbb598 FM |
3685 | const u64 hole_offset, |
3686 | const u64 hole_len) | |
3687 | { | |
a2f392e4 | 3688 | struct extent_map_tree *em_tree = &inode->extent_tree; |
7ffbb598 FM |
3689 | struct extent_map *em; |
3690 | int ret; | |
3691 | ||
3692 | em = alloc_extent_map(); | |
3693 | if (!em) { | |
a2f392e4 | 3694 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
7ffbb598 FM |
3695 | return; |
3696 | } | |
3697 | ||
14f59796 FM |
3698 | if (path) { |
3699 | struct btrfs_file_extent_item *fi; | |
3700 | ||
3701 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3702 | struct btrfs_file_extent_item); | |
a2f392e4 | 3703 | btrfs_extent_item_to_extent_map(inode, path, fi, false, em); |
7ffbb598 FM |
3704 | em->generation = -1; |
3705 | if (btrfs_file_extent_type(path->nodes[0], fi) == | |
3706 | BTRFS_FILE_EXTENT_INLINE) | |
3707 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
a2f392e4 | 3708 | &inode->runtime_flags); |
7ffbb598 FM |
3709 | } else { |
3710 | em->start = hole_offset; | |
3711 | em->len = hole_len; | |
3712 | em->ram_bytes = em->len; | |
3713 | em->orig_start = hole_offset; | |
3714 | em->block_start = EXTENT_MAP_HOLE; | |
3715 | em->block_len = 0; | |
3716 | em->orig_block_len = 0; | |
3717 | em->compress_type = BTRFS_COMPRESS_NONE; | |
3718 | em->generation = trans->transid; | |
3719 | } | |
3720 | ||
3721 | while (1) { | |
3722 | write_lock(&em_tree->lock); | |
3723 | ret = add_extent_mapping(em_tree, em, 1); | |
3724 | write_unlock(&em_tree->lock); | |
3725 | if (ret != -EEXIST) { | |
3726 | free_extent_map(em); | |
3727 | break; | |
3728 | } | |
a2f392e4 | 3729 | btrfs_drop_extent_cache(inode, em->start, |
7ffbb598 FM |
3730 | em->start + em->len - 1, 0); |
3731 | } | |
3732 | ||
ee39b432 | 3733 | if (ret) |
a2f392e4 | 3734 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
7ffbb598 FM |
3735 | } |
3736 | ||
8039d87d FM |
3737 | /* |
3738 | * Make sure we do not end up inserting an inline extent into a file that has | |
3739 | * already other (non-inline) extents. If a file has an inline extent it can | |
3740 | * not have any other extents and the (single) inline extent must start at the | |
3741 | * file offset 0. Failing to respect these rules will lead to file corruption, | |
3742 | * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc | |
3743 | * | |
3744 | * We can have extents that have been already written to disk or we can have | |
3745 | * dirty ranges still in delalloc, in which case the extent maps and items are | |
3746 | * created only when we run delalloc, and the delalloc ranges might fall outside | |
3747 | * the range we are currently locking in the inode's io tree. So we check the | |
3748 | * inode's i_size because of that (i_size updates are done while holding the | |
3749 | * i_mutex, which we are holding here). | |
3750 | * We also check to see if the inode has a size not greater than "datal" but has | |
3751 | * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are | |
3752 | * protected against such concurrent fallocate calls by the i_mutex). | |
3753 | * | |
3754 | * If the file has no extents but a size greater than datal, do not allow the | |
3755 | * copy because we would need turn the inline extent into a non-inline one (even | |
3756 | * with NO_HOLES enabled). If we find our destination inode only has one inline | |
3757 | * extent, just overwrite it with the source inline extent if its size is less | |
3758 | * than the source extent's size, or we could copy the source inline extent's | |
3759 | * data into the destination inode's inline extent if the later is greater then | |
3760 | * the former. | |
3761 | */ | |
4a0ab9d7 | 3762 | static int clone_copy_inline_extent(struct inode *dst, |
8039d87d FM |
3763 | struct btrfs_trans_handle *trans, |
3764 | struct btrfs_path *path, | |
3765 | struct btrfs_key *new_key, | |
3766 | const u64 drop_start, | |
3767 | const u64 datal, | |
3768 | const u64 skip, | |
3769 | const u64 size, | |
3770 | char *inline_data) | |
3771 | { | |
0b246afa | 3772 | struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb); |
8039d87d FM |
3773 | struct btrfs_root *root = BTRFS_I(dst)->root; |
3774 | const u64 aligned_end = ALIGN(new_key->offset + datal, | |
0b246afa | 3775 | fs_info->sectorsize); |
8039d87d FM |
3776 | int ret; |
3777 | struct btrfs_key key; | |
3778 | ||
3779 | if (new_key->offset > 0) | |
3780 | return -EOPNOTSUPP; | |
3781 | ||
4a0cc7ca | 3782 | key.objectid = btrfs_ino(BTRFS_I(dst)); |
8039d87d FM |
3783 | key.type = BTRFS_EXTENT_DATA_KEY; |
3784 | key.offset = 0; | |
3785 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
3786 | if (ret < 0) { | |
3787 | return ret; | |
3788 | } else if (ret > 0) { | |
3789 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { | |
3790 | ret = btrfs_next_leaf(root, path); | |
3791 | if (ret < 0) | |
3792 | return ret; | |
3793 | else if (ret > 0) | |
3794 | goto copy_inline_extent; | |
3795 | } | |
3796 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
4a0cc7ca | 3797 | if (key.objectid == btrfs_ino(BTRFS_I(dst)) && |
8039d87d FM |
3798 | key.type == BTRFS_EXTENT_DATA_KEY) { |
3799 | ASSERT(key.offset > 0); | |
3800 | return -EOPNOTSUPP; | |
3801 | } | |
3802 | } else if (i_size_read(dst) <= datal) { | |
3803 | struct btrfs_file_extent_item *ei; | |
3804 | u64 ext_len; | |
3805 | ||
3806 | /* | |
3807 | * If the file size is <= datal, make sure there are no other | |
3808 | * extents following (can happen do to an fallocate call with | |
3809 | * the flag FALLOC_FL_KEEP_SIZE). | |
3810 | */ | |
3811 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3812 | struct btrfs_file_extent_item); | |
3813 | /* | |
3814 | * If it's an inline extent, it can not have other extents | |
3815 | * following it. | |
3816 | */ | |
3817 | if (btrfs_file_extent_type(path->nodes[0], ei) == | |
3818 | BTRFS_FILE_EXTENT_INLINE) | |
3819 | goto copy_inline_extent; | |
3820 | ||
3821 | ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei); | |
3822 | if (ext_len > aligned_end) | |
3823 | return -EOPNOTSUPP; | |
3824 | ||
3825 | ret = btrfs_next_item(root, path); | |
3826 | if (ret < 0) { | |
3827 | return ret; | |
3828 | } else if (ret == 0) { | |
3829 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
3830 | path->slots[0]); | |
4a0cc7ca | 3831 | if (key.objectid == btrfs_ino(BTRFS_I(dst)) && |
8039d87d FM |
3832 | key.type == BTRFS_EXTENT_DATA_KEY) |
3833 | return -EOPNOTSUPP; | |
3834 | } | |
3835 | } | |
3836 | ||
3837 | copy_inline_extent: | |
3838 | /* | |
3839 | * We have no extent items, or we have an extent at offset 0 which may | |
3840 | * or may not be inlined. All these cases are dealt the same way. | |
3841 | */ | |
3842 | if (i_size_read(dst) > datal) { | |
3843 | /* | |
3844 | * If the destination inode has an inline extent... | |
3845 | * This would require copying the data from the source inline | |
3846 | * extent into the beginning of the destination's inline extent. | |
3847 | * But this is really complex, both extents can be compressed | |
3848 | * or just one of them, which would require decompressing and | |
3849 | * re-compressing data (which could increase the new compressed | |
3850 | * size, not allowing the compressed data to fit anymore in an | |
3851 | * inline extent). | |
3852 | * So just don't support this case for now (it should be rare, | |
3853 | * we are not really saving space when cloning inline extents). | |
3854 | */ | |
3855 | return -EOPNOTSUPP; | |
3856 | } | |
3857 | ||
3858 | btrfs_release_path(path); | |
3859 | ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1); | |
3860 | if (ret) | |
3861 | return ret; | |
3862 | ret = btrfs_insert_empty_item(trans, root, path, new_key, size); | |
3863 | if (ret) | |
3864 | return ret; | |
3865 | ||
3866 | if (skip) { | |
3867 | const u32 start = btrfs_file_extent_calc_inline_size(0); | |
3868 | ||
3869 | memmove(inline_data + start, inline_data + start + skip, datal); | |
3870 | } | |
3871 | ||
3872 | write_extent_buffer(path->nodes[0], inline_data, | |
3873 | btrfs_item_ptr_offset(path->nodes[0], | |
3874 | path->slots[0]), | |
3875 | size); | |
3876 | inode_add_bytes(dst, datal); | |
3877 | ||
3878 | return 0; | |
3879 | } | |
3880 | ||
32b7c687 MF |
3881 | /** |
3882 | * btrfs_clone() - clone a range from inode file to another | |
3883 | * | |
3884 | * @src: Inode to clone from | |
3885 | * @inode: Inode to clone to | |
3886 | * @off: Offset within source to start clone from | |
3887 | * @olen: Original length, passed by user, of range to clone | |
1c919a5e | 3888 | * @olen_aligned: Block-aligned value of olen |
32b7c687 | 3889 | * @destoff: Offset within @inode to start clone |
1c919a5e | 3890 | * @no_time_update: Whether to update mtime/ctime on the target inode |
32b7c687 MF |
3891 | */ |
3892 | static int btrfs_clone(struct inode *src, struct inode *inode, | |
f82a9901 | 3893 | const u64 off, const u64 olen, const u64 olen_aligned, |
1c919a5e | 3894 | const u64 destoff, int no_time_update) |
f46b5a66 | 3895 | { |
0b246afa | 3896 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
f46b5a66 | 3897 | struct btrfs_root *root = BTRFS_I(inode)->root; |
32b7c687 | 3898 | struct btrfs_path *path = NULL; |
f46b5a66 | 3899 | struct extent_buffer *leaf; |
32b7c687 MF |
3900 | struct btrfs_trans_handle *trans; |
3901 | char *buf = NULL; | |
ae01a0ab | 3902 | struct btrfs_key key; |
f46b5a66 CH |
3903 | u32 nritems; |
3904 | int slot; | |
ae01a0ab | 3905 | int ret; |
f82a9901 | 3906 | const u64 len = olen_aligned; |
f82a9901 | 3907 | u64 last_dest_end = destoff; |
ae01a0ab YZ |
3908 | |
3909 | ret = -ENOMEM; | |
752ade68 MH |
3910 | buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); |
3911 | if (!buf) | |
3912 | return ret; | |
ae01a0ab YZ |
3913 | |
3914 | path = btrfs_alloc_path(); | |
3915 | if (!path) { | |
15351955 | 3916 | kvfree(buf); |
32b7c687 | 3917 | return ret; |
d525e8ab LZ |
3918 | } |
3919 | ||
e4058b54 | 3920 | path->reada = READA_FORWARD; |
c5c9cd4d | 3921 | /* clone data */ |
4a0cc7ca | 3922 | key.objectid = btrfs_ino(BTRFS_I(src)); |
ae01a0ab | 3923 | key.type = BTRFS_EXTENT_DATA_KEY; |
2c463823 | 3924 | key.offset = off; |
f46b5a66 CH |
3925 | |
3926 | while (1) { | |
de249e66 | 3927 | u64 next_key_min_offset = key.offset + 1; |
df858e76 | 3928 | |
f46b5a66 CH |
3929 | /* |
3930 | * note the key will change type as we walk through the | |
3931 | * tree. | |
3932 | */ | |
e4355f34 | 3933 | path->leave_spinning = 1; |
362a20c5 DS |
3934 | ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, |
3935 | 0, 0); | |
f46b5a66 CH |
3936 | if (ret < 0) |
3937 | goto out; | |
2c463823 FM |
3938 | /* |
3939 | * First search, if no extent item that starts at offset off was | |
3940 | * found but the previous item is an extent item, it's possible | |
3941 | * it might overlap our target range, therefore process it. | |
3942 | */ | |
3943 | if (key.offset == off && ret > 0 && path->slots[0] > 0) { | |
3944 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
3945 | path->slots[0] - 1); | |
3946 | if (key.type == BTRFS_EXTENT_DATA_KEY) | |
3947 | path->slots[0]--; | |
3948 | } | |
f46b5a66 | 3949 | |
ae01a0ab | 3950 | nritems = btrfs_header_nritems(path->nodes[0]); |
e4355f34 | 3951 | process_slot: |
ae01a0ab | 3952 | if (path->slots[0] >= nritems) { |
362a20c5 | 3953 | ret = btrfs_next_leaf(BTRFS_I(src)->root, path); |
f46b5a66 CH |
3954 | if (ret < 0) |
3955 | goto out; | |
3956 | if (ret > 0) | |
3957 | break; | |
ae01a0ab | 3958 | nritems = btrfs_header_nritems(path->nodes[0]); |
f46b5a66 CH |
3959 | } |
3960 | leaf = path->nodes[0]; | |
3961 | slot = path->slots[0]; | |
f46b5a66 | 3962 | |
ae01a0ab | 3963 | btrfs_item_key_to_cpu(leaf, &key, slot); |
962a298f | 3964 | if (key.type > BTRFS_EXTENT_DATA_KEY || |
4a0cc7ca | 3965 | key.objectid != btrfs_ino(BTRFS_I(src))) |
f46b5a66 CH |
3966 | break; |
3967 | ||
962a298f | 3968 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
c5c9cd4d SW |
3969 | struct btrfs_file_extent_item *extent; |
3970 | int type; | |
31840ae1 ZY |
3971 | u32 size; |
3972 | struct btrfs_key new_key; | |
c5c9cd4d SW |
3973 | u64 disko = 0, diskl = 0; |
3974 | u64 datao = 0, datal = 0; | |
3975 | u8 comp; | |
f82a9901 | 3976 | u64 drop_start; |
31840ae1 | 3977 | |
c5c9cd4d SW |
3978 | extent = btrfs_item_ptr(leaf, slot, |
3979 | struct btrfs_file_extent_item); | |
3980 | comp = btrfs_file_extent_compression(leaf, extent); | |
3981 | type = btrfs_file_extent_type(leaf, extent); | |
c8a894d7 CM |
3982 | if (type == BTRFS_FILE_EXTENT_REG || |
3983 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d397712b CM |
3984 | disko = btrfs_file_extent_disk_bytenr(leaf, |
3985 | extent); | |
3986 | diskl = btrfs_file_extent_disk_num_bytes(leaf, | |
3987 | extent); | |
c5c9cd4d | 3988 | datao = btrfs_file_extent_offset(leaf, extent); |
d397712b CM |
3989 | datal = btrfs_file_extent_num_bytes(leaf, |
3990 | extent); | |
c5c9cd4d SW |
3991 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
3992 | /* take upper bound, may be compressed */ | |
3993 | datal = btrfs_file_extent_ram_bytes(leaf, | |
3994 | extent); | |
3995 | } | |
31840ae1 | 3996 | |
2c463823 FM |
3997 | /* |
3998 | * The first search might have left us at an extent | |
3999 | * item that ends before our target range's start, can | |
4000 | * happen if we have holes and NO_HOLES feature enabled. | |
4001 | */ | |
4002 | if (key.offset + datal <= off) { | |
e4355f34 FDBM |
4003 | path->slots[0]++; |
4004 | goto process_slot; | |
2c463823 FM |
4005 | } else if (key.offset >= off + len) { |
4006 | break; | |
e4355f34 | 4007 | } |
df858e76 | 4008 | next_key_min_offset = key.offset + datal; |
e4355f34 FDBM |
4009 | size = btrfs_item_size_nr(leaf, slot); |
4010 | read_extent_buffer(leaf, buf, | |
4011 | btrfs_item_ptr_offset(leaf, slot), | |
4012 | size); | |
4013 | ||
4014 | btrfs_release_path(path); | |
4015 | path->leave_spinning = 0; | |
c5c9cd4d | 4016 | |
31840ae1 | 4017 | memcpy(&new_key, &key, sizeof(new_key)); |
4a0cc7ca | 4018 | new_key.objectid = btrfs_ino(BTRFS_I(inode)); |
4d728ec7 LZ |
4019 | if (off <= key.offset) |
4020 | new_key.offset = key.offset + destoff - off; | |
4021 | else | |
4022 | new_key.offset = destoff; | |
31840ae1 | 4023 | |
f82a9901 FM |
4024 | /* |
4025 | * Deal with a hole that doesn't have an extent item | |
4026 | * that represents it (NO_HOLES feature enabled). | |
4027 | * This hole is either in the middle of the cloning | |
4028 | * range or at the beginning (fully overlaps it or | |
4029 | * partially overlaps it). | |
4030 | */ | |
4031 | if (new_key.offset != last_dest_end) | |
4032 | drop_start = last_dest_end; | |
4033 | else | |
4034 | drop_start = new_key.offset; | |
4035 | ||
b6f3409b SW |
4036 | /* |
4037 | * 1 - adjusting old extent (we may have to split it) | |
4038 | * 1 - add new extent | |
4039 | * 1 - inode update | |
4040 | */ | |
4041 | trans = btrfs_start_transaction(root, 3); | |
a22285a6 YZ |
4042 | if (IS_ERR(trans)) { |
4043 | ret = PTR_ERR(trans); | |
4044 | goto out; | |
4045 | } | |
4046 | ||
c8a894d7 CM |
4047 | if (type == BTRFS_FILE_EXTENT_REG || |
4048 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d72c0842 LZ |
4049 | /* |
4050 | * a | --- range to clone ---| b | |
4051 | * | ------------- extent ------------- | | |
4052 | */ | |
4053 | ||
93915584 | 4054 | /* subtract range b */ |
d72c0842 LZ |
4055 | if (key.offset + datal > off + len) |
4056 | datal = off + len - key.offset; | |
4057 | ||
93915584 | 4058 | /* subtract range a */ |
a22285a6 YZ |
4059 | if (off > key.offset) { |
4060 | datao += off - key.offset; | |
4061 | datal -= off - key.offset; | |
4062 | } | |
4063 | ||
5dc562c5 | 4064 | ret = btrfs_drop_extents(trans, root, inode, |
f82a9901 | 4065 | drop_start, |
a22285a6 | 4066 | new_key.offset + datal, |
2671485d | 4067 | 1); |
79787eaa | 4068 | if (ret) { |
3f9e3df8 | 4069 | if (ret != -EOPNOTSUPP) |
00fdf13a | 4070 | btrfs_abort_transaction(trans, |
66642832 | 4071 | ret); |
3a45bb20 | 4072 | btrfs_end_transaction(trans); |
79787eaa JM |
4073 | goto out; |
4074 | } | |
a22285a6 | 4075 | |
c5c9cd4d SW |
4076 | ret = btrfs_insert_empty_item(trans, root, path, |
4077 | &new_key, size); | |
79787eaa | 4078 | if (ret) { |
66642832 | 4079 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4080 | btrfs_end_transaction(trans); |
79787eaa JM |
4081 | goto out; |
4082 | } | |
c5c9cd4d SW |
4083 | |
4084 | leaf = path->nodes[0]; | |
4085 | slot = path->slots[0]; | |
4086 | write_extent_buffer(leaf, buf, | |
31840ae1 ZY |
4087 | btrfs_item_ptr_offset(leaf, slot), |
4088 | size); | |
ae01a0ab | 4089 | |
c5c9cd4d | 4090 | extent = btrfs_item_ptr(leaf, slot, |
f46b5a66 | 4091 | struct btrfs_file_extent_item); |
c5c9cd4d | 4092 | |
c5c9cd4d SW |
4093 | /* disko == 0 means it's a hole */ |
4094 | if (!disko) | |
4095 | datao = 0; | |
c5c9cd4d SW |
4096 | |
4097 | btrfs_set_file_extent_offset(leaf, extent, | |
4098 | datao); | |
4099 | btrfs_set_file_extent_num_bytes(leaf, extent, | |
4100 | datal); | |
fcebe456 | 4101 | |
c5c9cd4d SW |
4102 | if (disko) { |
4103 | inode_add_bytes(inode, datal); | |
2ff7e61e | 4104 | ret = btrfs_inc_extent_ref(trans, |
84f7d8e6 | 4105 | root, |
5d4f98a2 YZ |
4106 | disko, diskl, 0, |
4107 | root->root_key.objectid, | |
4a0cc7ca | 4108 | btrfs_ino(BTRFS_I(inode)), |
b06c4bf5 | 4109 | new_key.offset - datao); |
79787eaa JM |
4110 | if (ret) { |
4111 | btrfs_abort_transaction(trans, | |
79787eaa | 4112 | ret); |
3a45bb20 | 4113 | btrfs_end_transaction(trans); |
79787eaa JM |
4114 | goto out; |
4115 | ||
4116 | } | |
f46b5a66 | 4117 | } |
c5c9cd4d SW |
4118 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
4119 | u64 skip = 0; | |
4120 | u64 trim = 0; | |
ed958762 | 4121 | |
c5c9cd4d SW |
4122 | if (off > key.offset) { |
4123 | skip = off - key.offset; | |
4124 | new_key.offset += skip; | |
4125 | } | |
d397712b | 4126 | |
aa42ffd9 LB |
4127 | if (key.offset + datal > off + len) |
4128 | trim = key.offset + datal - (off + len); | |
d397712b | 4129 | |
c5c9cd4d | 4130 | if (comp && (skip || trim)) { |
c5c9cd4d | 4131 | ret = -EINVAL; |
3a45bb20 | 4132 | btrfs_end_transaction(trans); |
c5c9cd4d SW |
4133 | goto out; |
4134 | } | |
4135 | size -= skip + trim; | |
4136 | datal -= skip + trim; | |
a22285a6 | 4137 | |
4a0ab9d7 | 4138 | ret = clone_copy_inline_extent(inode, |
8039d87d FM |
4139 | trans, path, |
4140 | &new_key, | |
4141 | drop_start, | |
4142 | datal, | |
4143 | skip, size, buf); | |
79787eaa | 4144 | if (ret) { |
3f9e3df8 | 4145 | if (ret != -EOPNOTSUPP) |
3a29bc09 | 4146 | btrfs_abort_transaction(trans, |
8039d87d | 4147 | ret); |
3a45bb20 | 4148 | btrfs_end_transaction(trans); |
79787eaa JM |
4149 | goto out; |
4150 | } | |
c5c9cd4d SW |
4151 | leaf = path->nodes[0]; |
4152 | slot = path->slots[0]; | |
f46b5a66 | 4153 | } |
c5c9cd4d | 4154 | |
7ffbb598 FM |
4155 | /* If we have an implicit hole (NO_HOLES feature). */ |
4156 | if (drop_start < new_key.offset) | |
a2f392e4 | 4157 | clone_update_extent_map(BTRFS_I(inode), trans, |
14f59796 | 4158 | NULL, drop_start, |
7ffbb598 FM |
4159 | new_key.offset - drop_start); |
4160 | ||
a2f392e4 NB |
4161 | clone_update_extent_map(BTRFS_I(inode), trans, |
4162 | path, 0, 0); | |
7ffbb598 | 4163 | |
c5c9cd4d | 4164 | btrfs_mark_buffer_dirty(leaf); |
b3b4aa74 | 4165 | btrfs_release_path(path); |
c5c9cd4d | 4166 | |
62e2390e | 4167 | last_dest_end = ALIGN(new_key.offset + datal, |
0b246afa | 4168 | fs_info->sectorsize); |
f82a9901 FM |
4169 | ret = clone_finish_inode_update(trans, inode, |
4170 | last_dest_end, | |
1c919a5e MF |
4171 | destoff, olen, |
4172 | no_time_update); | |
f82a9901 | 4173 | if (ret) |
79787eaa | 4174 | goto out; |
2c463823 FM |
4175 | if (new_key.offset + datal >= destoff + len) |
4176 | break; | |
a22285a6 | 4177 | } |
b3b4aa74 | 4178 | btrfs_release_path(path); |
df858e76 | 4179 | key.offset = next_key_min_offset; |
69ae5e44 WX |
4180 | |
4181 | if (fatal_signal_pending(current)) { | |
4182 | ret = -EINTR; | |
4183 | goto out; | |
4184 | } | |
f46b5a66 | 4185 | } |
f46b5a66 | 4186 | ret = 0; |
32b7c687 | 4187 | |
f82a9901 FM |
4188 | if (last_dest_end < destoff + len) { |
4189 | /* | |
4190 | * We have an implicit hole (NO_HOLES feature is enabled) that | |
4191 | * fully or partially overlaps our cloning range at its end. | |
4192 | */ | |
4193 | btrfs_release_path(path); | |
4194 | ||
4195 | /* | |
4196 | * 1 - remove extent(s) | |
4197 | * 1 - inode update | |
4198 | */ | |
4199 | trans = btrfs_start_transaction(root, 2); | |
4200 | if (IS_ERR(trans)) { | |
4201 | ret = PTR_ERR(trans); | |
4202 | goto out; | |
4203 | } | |
4204 | ret = btrfs_drop_extents(trans, root, inode, | |
4205 | last_dest_end, destoff + len, 1); | |
4206 | if (ret) { | |
4207 | if (ret != -EOPNOTSUPP) | |
66642832 | 4208 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4209 | btrfs_end_transaction(trans); |
f82a9901 FM |
4210 | goto out; |
4211 | } | |
a2f392e4 NB |
4212 | clone_update_extent_map(BTRFS_I(inode), trans, NULL, |
4213 | last_dest_end, | |
4214 | destoff + len - last_dest_end); | |
f82a9901 | 4215 | ret = clone_finish_inode_update(trans, inode, destoff + len, |
1c919a5e | 4216 | destoff, olen, no_time_update); |
f82a9901 FM |
4217 | } |
4218 | ||
f46b5a66 | 4219 | out: |
32b7c687 | 4220 | btrfs_free_path(path); |
15351955 | 4221 | kvfree(buf); |
32b7c687 MF |
4222 | return ret; |
4223 | } | |
4224 | ||
3db11b2e ZB |
4225 | static noinline int btrfs_clone_files(struct file *file, struct file *file_src, |
4226 | u64 off, u64 olen, u64 destoff) | |
32b7c687 | 4227 | { |
54563d41 | 4228 | struct inode *inode = file_inode(file); |
3db11b2e | 4229 | struct inode *src = file_inode(file_src); |
0b246afa | 4230 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
32b7c687 | 4231 | struct btrfs_root *root = BTRFS_I(inode)->root; |
32b7c687 MF |
4232 | int ret; |
4233 | u64 len = olen; | |
0b246afa | 4234 | u64 bs = fs_info->sb->s_blocksize; |
3db11b2e | 4235 | int same_inode = src == inode; |
32b7c687 MF |
4236 | |
4237 | /* | |
4238 | * TODO: | |
4239 | * - split compressed inline extents. annoying: we need to | |
4240 | * decompress into destination's address_space (the file offset | |
4241 | * may change, so source mapping won't do), then recompress (or | |
4242 | * otherwise reinsert) a subrange. | |
00fdf13a LB |
4243 | * |
4244 | * - split destination inode's inline extents. The inline extents can | |
4245 | * be either compressed or non-compressed. | |
32b7c687 MF |
4246 | */ |
4247 | ||
32b7c687 MF |
4248 | if (btrfs_root_readonly(root)) |
4249 | return -EROFS; | |
4250 | ||
3db11b2e ZB |
4251 | if (file_src->f_path.mnt != file->f_path.mnt || |
4252 | src->i_sb != inode->i_sb) | |
4253 | return -EXDEV; | |
32b7c687 | 4254 | |
32b7c687 | 4255 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
3db11b2e | 4256 | return -EISDIR; |
32b7c687 MF |
4257 | |
4258 | if (!same_inode) { | |
293a8489 | 4259 | btrfs_double_inode_lock(src, inode); |
32b7c687 | 4260 | } else { |
5955102c | 4261 | inode_lock(src); |
32b7c687 MF |
4262 | } |
4263 | ||
b5c40d59 OS |
4264 | /* don't make the dst file partly checksummed */ |
4265 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != | |
4266 | (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
4267 | ret = -EINVAL; | |
4268 | goto out_unlock; | |
4269 | } | |
4270 | ||
32b7c687 MF |
4271 | /* determine range to clone */ |
4272 | ret = -EINVAL; | |
4273 | if (off + len > src->i_size || off + len < off) | |
4274 | goto out_unlock; | |
4275 | if (len == 0) | |
4276 | olen = len = src->i_size - off; | |
4277 | /* if we extend to eof, continue to block boundary */ | |
4278 | if (off + len == src->i_size) | |
4279 | len = ALIGN(src->i_size, bs) - off; | |
4280 | ||
ccccf3d6 FM |
4281 | if (len == 0) { |
4282 | ret = 0; | |
4283 | goto out_unlock; | |
4284 | } | |
4285 | ||
32b7c687 MF |
4286 | /* verify the end result is block aligned */ |
4287 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || | |
4288 | !IS_ALIGNED(destoff, bs)) | |
4289 | goto out_unlock; | |
4290 | ||
4291 | /* verify if ranges are overlapped within the same file */ | |
4292 | if (same_inode) { | |
4293 | if (destoff + len > off && destoff < off + len) | |
4294 | goto out_unlock; | |
4295 | } | |
4296 | ||
4297 | if (destoff > inode->i_size) { | |
4298 | ret = btrfs_cont_expand(inode, inode->i_size, destoff); | |
4299 | if (ret) | |
4300 | goto out_unlock; | |
4301 | } | |
4302 | ||
c125b8bf FM |
4303 | /* |
4304 | * Lock the target range too. Right after we replace the file extent | |
4305 | * items in the fs tree (which now point to the cloned data), we might | |
4306 | * have a worker replace them with extent items relative to a write | |
4307 | * operation that was issued before this clone operation (i.e. confront | |
4308 | * with inode.c:btrfs_finish_ordered_io). | |
4309 | */ | |
4310 | if (same_inode) { | |
4311 | u64 lock_start = min_t(u64, off, destoff); | |
4312 | u64 lock_len = max_t(u64, off, destoff) + len - lock_start; | |
32b7c687 | 4313 | |
e0bd70c6 | 4314 | ret = lock_extent_range(src, lock_start, lock_len, true); |
c125b8bf | 4315 | } else { |
e0bd70c6 FM |
4316 | ret = btrfs_double_extent_lock(src, off, inode, destoff, len, |
4317 | true); | |
4318 | } | |
4319 | ASSERT(ret == 0); | |
4320 | if (WARN_ON(ret)) { | |
4321 | /* ranges in the io trees already unlocked */ | |
4322 | goto out_unlock; | |
c125b8bf | 4323 | } |
32b7c687 | 4324 | |
1c919a5e | 4325 | ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); |
32b7c687 | 4326 | |
c125b8bf FM |
4327 | if (same_inode) { |
4328 | u64 lock_start = min_t(u64, off, destoff); | |
4329 | u64 lock_end = max_t(u64, off, destoff) + len - 1; | |
4330 | ||
4331 | unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end); | |
4332 | } else { | |
293a8489 | 4333 | btrfs_double_extent_unlock(src, off, inode, destoff, len); |
c125b8bf FM |
4334 | } |
4335 | /* | |
4336 | * Truncate page cache pages so that future reads will see the cloned | |
4337 | * data immediately and not the previous data. | |
4338 | */ | |
65bfa658 | 4339 | truncate_inode_pages_range(&inode->i_data, |
09cbfeaf KS |
4340 | round_down(destoff, PAGE_SIZE), |
4341 | round_up(destoff + len, PAGE_SIZE) - 1); | |
f46b5a66 | 4342 | out_unlock: |
293a8489 MF |
4343 | if (!same_inode) |
4344 | btrfs_double_inode_unlock(src, inode); | |
4345 | else | |
5955102c | 4346 | inode_unlock(src); |
3db11b2e ZB |
4347 | return ret; |
4348 | } | |
4349 | ||
04b38d60 CH |
4350 | int btrfs_clone_file_range(struct file *src_file, loff_t off, |
4351 | struct file *dst_file, loff_t destoff, u64 len) | |
3db11b2e | 4352 | { |
04b38d60 | 4353 | return btrfs_clone_files(dst_file, src_file, off, len, destoff); |
c5c9cd4d SW |
4354 | } |
4355 | ||
6ef5ed0d JB |
4356 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
4357 | { | |
496ad9aa | 4358 | struct inode *inode = file_inode(file); |
0b246afa | 4359 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
6ef5ed0d JB |
4360 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4361 | struct btrfs_root *new_root; | |
4362 | struct btrfs_dir_item *di; | |
4363 | struct btrfs_trans_handle *trans; | |
4364 | struct btrfs_path *path; | |
4365 | struct btrfs_key location; | |
4366 | struct btrfs_disk_key disk_key; | |
6ef5ed0d JB |
4367 | u64 objectid = 0; |
4368 | u64 dir_id; | |
3c04ce01 | 4369 | int ret; |
6ef5ed0d JB |
4370 | |
4371 | if (!capable(CAP_SYS_ADMIN)) | |
4372 | return -EPERM; | |
4373 | ||
3c04ce01 MX |
4374 | ret = mnt_want_write_file(file); |
4375 | if (ret) | |
4376 | return ret; | |
4377 | ||
4378 | if (copy_from_user(&objectid, argp, sizeof(objectid))) { | |
4379 | ret = -EFAULT; | |
4380 | goto out; | |
4381 | } | |
6ef5ed0d JB |
4382 | |
4383 | if (!objectid) | |
1cecf579 | 4384 | objectid = BTRFS_FS_TREE_OBJECTID; |
6ef5ed0d JB |
4385 | |
4386 | location.objectid = objectid; | |
4387 | location.type = BTRFS_ROOT_ITEM_KEY; | |
4388 | location.offset = (u64)-1; | |
4389 | ||
0b246afa | 4390 | new_root = btrfs_read_fs_root_no_name(fs_info, &location); |
3c04ce01 MX |
4391 | if (IS_ERR(new_root)) { |
4392 | ret = PTR_ERR(new_root); | |
4393 | goto out; | |
4394 | } | |
4fd786e6 | 4395 | if (!is_fstree(new_root->root_key.objectid)) { |
6d6d2829 | 4396 | ret = -ENOENT; |
4397 | goto out; | |
4398 | } | |
6ef5ed0d | 4399 | |
6ef5ed0d | 4400 | path = btrfs_alloc_path(); |
3c04ce01 MX |
4401 | if (!path) { |
4402 | ret = -ENOMEM; | |
4403 | goto out; | |
4404 | } | |
6ef5ed0d JB |
4405 | path->leave_spinning = 1; |
4406 | ||
4407 | trans = btrfs_start_transaction(root, 1); | |
98d5dc13 | 4408 | if (IS_ERR(trans)) { |
6ef5ed0d | 4409 | btrfs_free_path(path); |
3c04ce01 MX |
4410 | ret = PTR_ERR(trans); |
4411 | goto out; | |
6ef5ed0d JB |
4412 | } |
4413 | ||
0b246afa JM |
4414 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
4415 | di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, | |
6ef5ed0d | 4416 | dir_id, "default", 7, 1); |
cf1e99a4 | 4417 | if (IS_ERR_OR_NULL(di)) { |
6ef5ed0d | 4418 | btrfs_free_path(path); |
3a45bb20 | 4419 | btrfs_end_transaction(trans); |
0b246afa | 4420 | btrfs_err(fs_info, |
5d163e0e | 4421 | "Umm, you don't have the default diritem, this isn't going to work"); |
3c04ce01 MX |
4422 | ret = -ENOENT; |
4423 | goto out; | |
6ef5ed0d JB |
4424 | } |
4425 | ||
4426 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); | |
4427 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); | |
4428 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
4429 | btrfs_free_path(path); | |
4430 | ||
0b246afa | 4431 | btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); |
3a45bb20 | 4432 | btrfs_end_transaction(trans); |
3c04ce01 MX |
4433 | out: |
4434 | mnt_drop_write_file(file); | |
4435 | return ret; | |
6ef5ed0d JB |
4436 | } |
4437 | ||
c065f5b1 SY |
4438 | static void get_block_group_info(struct list_head *groups_list, |
4439 | struct btrfs_ioctl_space_info *space) | |
bf5fc093 JB |
4440 | { |
4441 | struct btrfs_block_group_cache *block_group; | |
4442 | ||
4443 | space->total_bytes = 0; | |
4444 | space->used_bytes = 0; | |
4445 | space->flags = 0; | |
4446 | list_for_each_entry(block_group, groups_list, list) { | |
4447 | space->flags = block_group->flags; | |
4448 | space->total_bytes += block_group->key.offset; | |
4449 | space->used_bytes += | |
4450 | btrfs_block_group_used(&block_group->item); | |
4451 | } | |
4452 | } | |
4453 | ||
2ff7e61e JM |
4454 | static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, |
4455 | void __user *arg) | |
1406e432 JB |
4456 | { |
4457 | struct btrfs_ioctl_space_args space_args; | |
4458 | struct btrfs_ioctl_space_info space; | |
4459 | struct btrfs_ioctl_space_info *dest; | |
7fde62bf | 4460 | struct btrfs_ioctl_space_info *dest_orig; |
13f2696f | 4461 | struct btrfs_ioctl_space_info __user *user_dest; |
1406e432 | 4462 | struct btrfs_space_info *info; |
315d8e98 CIK |
4463 | static const u64 types[] = { |
4464 | BTRFS_BLOCK_GROUP_DATA, | |
4465 | BTRFS_BLOCK_GROUP_SYSTEM, | |
4466 | BTRFS_BLOCK_GROUP_METADATA, | |
4467 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA | |
4468 | }; | |
bf5fc093 | 4469 | int num_types = 4; |
7fde62bf | 4470 | int alloc_size; |
1406e432 | 4471 | int ret = 0; |
51788b1b | 4472 | u64 slot_count = 0; |
bf5fc093 | 4473 | int i, c; |
1406e432 JB |
4474 | |
4475 | if (copy_from_user(&space_args, | |
4476 | (struct btrfs_ioctl_space_args __user *)arg, | |
4477 | sizeof(space_args))) | |
4478 | return -EFAULT; | |
4479 | ||
bf5fc093 JB |
4480 | for (i = 0; i < num_types; i++) { |
4481 | struct btrfs_space_info *tmp; | |
4482 | ||
4483 | info = NULL; | |
4484 | rcu_read_lock(); | |
0b246afa | 4485 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
bf5fc093 JB |
4486 | list) { |
4487 | if (tmp->flags == types[i]) { | |
4488 | info = tmp; | |
4489 | break; | |
4490 | } | |
4491 | } | |
4492 | rcu_read_unlock(); | |
4493 | ||
4494 | if (!info) | |
4495 | continue; | |
4496 | ||
4497 | down_read(&info->groups_sem); | |
4498 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
4499 | if (!list_empty(&info->block_groups[c])) | |
4500 | slot_count++; | |
4501 | } | |
4502 | up_read(&info->groups_sem); | |
4503 | } | |
7fde62bf | 4504 | |
36523e95 DS |
4505 | /* |
4506 | * Global block reserve, exported as a space_info | |
4507 | */ | |
4508 | slot_count++; | |
4509 | ||
7fde62bf CM |
4510 | /* space_slots == 0 means they are asking for a count */ |
4511 | if (space_args.space_slots == 0) { | |
4512 | space_args.total_spaces = slot_count; | |
4513 | goto out; | |
4514 | } | |
bf5fc093 | 4515 | |
51788b1b | 4516 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
bf5fc093 | 4517 | |
7fde62bf | 4518 | alloc_size = sizeof(*dest) * slot_count; |
bf5fc093 | 4519 | |
7fde62bf CM |
4520 | /* we generally have at most 6 or so space infos, one for each raid |
4521 | * level. So, a whole page should be more than enough for everyone | |
4522 | */ | |
09cbfeaf | 4523 | if (alloc_size > PAGE_SIZE) |
7fde62bf CM |
4524 | return -ENOMEM; |
4525 | ||
1406e432 | 4526 | space_args.total_spaces = 0; |
8d2db785 | 4527 | dest = kmalloc(alloc_size, GFP_KERNEL); |
7fde62bf CM |
4528 | if (!dest) |
4529 | return -ENOMEM; | |
4530 | dest_orig = dest; | |
1406e432 | 4531 | |
7fde62bf | 4532 | /* now we have a buffer to copy into */ |
bf5fc093 JB |
4533 | for (i = 0; i < num_types; i++) { |
4534 | struct btrfs_space_info *tmp; | |
4535 | ||
51788b1b DR |
4536 | if (!slot_count) |
4537 | break; | |
4538 | ||
bf5fc093 JB |
4539 | info = NULL; |
4540 | rcu_read_lock(); | |
0b246afa | 4541 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
bf5fc093 JB |
4542 | list) { |
4543 | if (tmp->flags == types[i]) { | |
4544 | info = tmp; | |
4545 | break; | |
4546 | } | |
4547 | } | |
4548 | rcu_read_unlock(); | |
7fde62bf | 4549 | |
bf5fc093 JB |
4550 | if (!info) |
4551 | continue; | |
4552 | down_read(&info->groups_sem); | |
4553 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
4554 | if (!list_empty(&info->block_groups[c])) { | |
c065f5b1 SY |
4555 | get_block_group_info(&info->block_groups[c], |
4556 | &space); | |
bf5fc093 JB |
4557 | memcpy(dest, &space, sizeof(space)); |
4558 | dest++; | |
4559 | space_args.total_spaces++; | |
51788b1b | 4560 | slot_count--; |
bf5fc093 | 4561 | } |
51788b1b DR |
4562 | if (!slot_count) |
4563 | break; | |
bf5fc093 JB |
4564 | } |
4565 | up_read(&info->groups_sem); | |
1406e432 | 4566 | } |
1406e432 | 4567 | |
36523e95 DS |
4568 | /* |
4569 | * Add global block reserve | |
4570 | */ | |
4571 | if (slot_count) { | |
0b246afa | 4572 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
36523e95 DS |
4573 | |
4574 | spin_lock(&block_rsv->lock); | |
4575 | space.total_bytes = block_rsv->size; | |
4576 | space.used_bytes = block_rsv->size - block_rsv->reserved; | |
4577 | spin_unlock(&block_rsv->lock); | |
4578 | space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; | |
4579 | memcpy(dest, &space, sizeof(space)); | |
4580 | space_args.total_spaces++; | |
4581 | } | |
4582 | ||
2eec6c81 | 4583 | user_dest = (struct btrfs_ioctl_space_info __user *) |
7fde62bf CM |
4584 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
4585 | ||
4586 | if (copy_to_user(user_dest, dest_orig, alloc_size)) | |
4587 | ret = -EFAULT; | |
4588 | ||
4589 | kfree(dest_orig); | |
4590 | out: | |
4591 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) | |
1406e432 JB |
4592 | ret = -EFAULT; |
4593 | ||
4594 | return ret; | |
4595 | } | |
4596 | ||
9a8c28be MX |
4597 | static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, |
4598 | void __user *argp) | |
46204592 | 4599 | { |
46204592 SW |
4600 | struct btrfs_trans_handle *trans; |
4601 | u64 transid; | |
db5b493a | 4602 | int ret; |
46204592 | 4603 | |
d4edf39b | 4604 | trans = btrfs_attach_transaction_barrier(root); |
ff7c1d33 MX |
4605 | if (IS_ERR(trans)) { |
4606 | if (PTR_ERR(trans) != -ENOENT) | |
4607 | return PTR_ERR(trans); | |
4608 | ||
4609 | /* No running transaction, don't bother */ | |
4610 | transid = root->fs_info->last_trans_committed; | |
4611 | goto out; | |
4612 | } | |
46204592 | 4613 | transid = trans->transid; |
3a45bb20 | 4614 | ret = btrfs_commit_transaction_async(trans, 0); |
8b2b2d3c | 4615 | if (ret) { |
3a45bb20 | 4616 | btrfs_end_transaction(trans); |
db5b493a | 4617 | return ret; |
8b2b2d3c | 4618 | } |
ff7c1d33 | 4619 | out: |
46204592 SW |
4620 | if (argp) |
4621 | if (copy_to_user(argp, &transid, sizeof(transid))) | |
4622 | return -EFAULT; | |
4623 | return 0; | |
4624 | } | |
4625 | ||
2ff7e61e | 4626 | static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, |
9a8c28be | 4627 | void __user *argp) |
46204592 | 4628 | { |
46204592 SW |
4629 | u64 transid; |
4630 | ||
4631 | if (argp) { | |
4632 | if (copy_from_user(&transid, argp, sizeof(transid))) | |
4633 | return -EFAULT; | |
4634 | } else { | |
4635 | transid = 0; /* current trans */ | |
4636 | } | |
2ff7e61e | 4637 | return btrfs_wait_for_commit(fs_info, transid); |
46204592 SW |
4638 | } |
4639 | ||
b8e95489 | 4640 | static long btrfs_ioctl_scrub(struct file *file, void __user *arg) |
475f6387 | 4641 | { |
0b246afa | 4642 | struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); |
475f6387 | 4643 | struct btrfs_ioctl_scrub_args *sa; |
b8e95489 | 4644 | int ret; |
475f6387 JS |
4645 | |
4646 | if (!capable(CAP_SYS_ADMIN)) | |
4647 | return -EPERM; | |
4648 | ||
4649 | sa = memdup_user(arg, sizeof(*sa)); | |
4650 | if (IS_ERR(sa)) | |
4651 | return PTR_ERR(sa); | |
4652 | ||
b8e95489 MX |
4653 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) { |
4654 | ret = mnt_want_write_file(file); | |
4655 | if (ret) | |
4656 | goto out; | |
4657 | } | |
4658 | ||
0b246afa | 4659 | ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, |
63a212ab SB |
4660 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, |
4661 | 0); | |
475f6387 JS |
4662 | |
4663 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
4664 | ret = -EFAULT; | |
4665 | ||
b8e95489 MX |
4666 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) |
4667 | mnt_drop_write_file(file); | |
4668 | out: | |
475f6387 JS |
4669 | kfree(sa); |
4670 | return ret; | |
4671 | } | |
4672 | ||
2ff7e61e | 4673 | static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) |
475f6387 JS |
4674 | { |
4675 | if (!capable(CAP_SYS_ADMIN)) | |
4676 | return -EPERM; | |
4677 | ||
2ff7e61e | 4678 | return btrfs_scrub_cancel(fs_info); |
475f6387 JS |
4679 | } |
4680 | ||
2ff7e61e | 4681 | static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, |
475f6387 JS |
4682 | void __user *arg) |
4683 | { | |
4684 | struct btrfs_ioctl_scrub_args *sa; | |
4685 | int ret; | |
4686 | ||
4687 | if (!capable(CAP_SYS_ADMIN)) | |
4688 | return -EPERM; | |
4689 | ||
4690 | sa = memdup_user(arg, sizeof(*sa)); | |
4691 | if (IS_ERR(sa)) | |
4692 | return PTR_ERR(sa); | |
4693 | ||
2ff7e61e | 4694 | ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); |
475f6387 JS |
4695 | |
4696 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
4697 | ret = -EFAULT; | |
4698 | ||
4699 | kfree(sa); | |
4700 | return ret; | |
4701 | } | |
4702 | ||
2ff7e61e | 4703 | static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, |
b27f7c0c | 4704 | void __user *arg) |
c11d2c23 SB |
4705 | { |
4706 | struct btrfs_ioctl_get_dev_stats *sa; | |
4707 | int ret; | |
4708 | ||
c11d2c23 SB |
4709 | sa = memdup_user(arg, sizeof(*sa)); |
4710 | if (IS_ERR(sa)) | |
4711 | return PTR_ERR(sa); | |
4712 | ||
b27f7c0c DS |
4713 | if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { |
4714 | kfree(sa); | |
4715 | return -EPERM; | |
4716 | } | |
4717 | ||
2ff7e61e | 4718 | ret = btrfs_get_dev_stats(fs_info, sa); |
c11d2c23 SB |
4719 | |
4720 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
4721 | ret = -EFAULT; | |
4722 | ||
4723 | kfree(sa); | |
4724 | return ret; | |
4725 | } | |
4726 | ||
2ff7e61e JM |
4727 | static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, |
4728 | void __user *arg) | |
3f6bcfbd SB |
4729 | { |
4730 | struct btrfs_ioctl_dev_replace_args *p; | |
4731 | int ret; | |
4732 | ||
4733 | if (!capable(CAP_SYS_ADMIN)) | |
4734 | return -EPERM; | |
4735 | ||
4736 | p = memdup_user(arg, sizeof(*p)); | |
4737 | if (IS_ERR(p)) | |
4738 | return PTR_ERR(p); | |
4739 | ||
4740 | switch (p->cmd) { | |
4741 | case BTRFS_IOCTL_DEV_REPLACE_CMD_START: | |
bc98a42c | 4742 | if (sb_rdonly(fs_info->sb)) { |
adfa97cb ID |
4743 | ret = -EROFS; |
4744 | goto out; | |
4745 | } | |
171938e5 | 4746 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
e57138b3 | 4747 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
3f6bcfbd | 4748 | } else { |
2ff7e61e | 4749 | ret = btrfs_dev_replace_by_ioctl(fs_info, p); |
171938e5 | 4750 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
3f6bcfbd SB |
4751 | } |
4752 | break; | |
4753 | case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: | |
0b246afa | 4754 | btrfs_dev_replace_status(fs_info, p); |
3f6bcfbd SB |
4755 | ret = 0; |
4756 | break; | |
4757 | case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: | |
17d202b9 | 4758 | p->result = btrfs_dev_replace_cancel(fs_info); |
97282031 | 4759 | ret = 0; |
3f6bcfbd SB |
4760 | break; |
4761 | default: | |
4762 | ret = -EINVAL; | |
4763 | break; | |
4764 | } | |
4765 | ||
4766 | if (copy_to_user(arg, p, sizeof(*p))) | |
4767 | ret = -EFAULT; | |
adfa97cb | 4768 | out: |
3f6bcfbd SB |
4769 | kfree(p); |
4770 | return ret; | |
4771 | } | |
4772 | ||
d7728c96 JS |
4773 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
4774 | { | |
4775 | int ret = 0; | |
4776 | int i; | |
740c3d22 | 4777 | u64 rel_ptr; |
d7728c96 | 4778 | int size; |
806468f8 | 4779 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
d7728c96 JS |
4780 | struct inode_fs_paths *ipath = NULL; |
4781 | struct btrfs_path *path; | |
4782 | ||
82b22ac8 | 4783 | if (!capable(CAP_DAC_READ_SEARCH)) |
d7728c96 JS |
4784 | return -EPERM; |
4785 | ||
4786 | path = btrfs_alloc_path(); | |
4787 | if (!path) { | |
4788 | ret = -ENOMEM; | |
4789 | goto out; | |
4790 | } | |
4791 | ||
4792 | ipa = memdup_user(arg, sizeof(*ipa)); | |
4793 | if (IS_ERR(ipa)) { | |
4794 | ret = PTR_ERR(ipa); | |
4795 | ipa = NULL; | |
4796 | goto out; | |
4797 | } | |
4798 | ||
4799 | size = min_t(u32, ipa->size, 4096); | |
4800 | ipath = init_ipath(size, root, path); | |
4801 | if (IS_ERR(ipath)) { | |
4802 | ret = PTR_ERR(ipath); | |
4803 | ipath = NULL; | |
4804 | goto out; | |
4805 | } | |
4806 | ||
4807 | ret = paths_from_inode(ipa->inum, ipath); | |
4808 | if (ret < 0) | |
4809 | goto out; | |
4810 | ||
4811 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { | |
745c4d8e JM |
4812 | rel_ptr = ipath->fspath->val[i] - |
4813 | (u64)(unsigned long)ipath->fspath->val; | |
740c3d22 | 4814 | ipath->fspath->val[i] = rel_ptr; |
d7728c96 JS |
4815 | } |
4816 | ||
718dc5fa OS |
4817 | ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, |
4818 | ipath->fspath, size); | |
d7728c96 JS |
4819 | if (ret) { |
4820 | ret = -EFAULT; | |
4821 | goto out; | |
4822 | } | |
4823 | ||
4824 | out: | |
4825 | btrfs_free_path(path); | |
4826 | free_ipath(ipath); | |
4827 | kfree(ipa); | |
4828 | ||
4829 | return ret; | |
4830 | } | |
4831 | ||
4832 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) | |
4833 | { | |
4834 | struct btrfs_data_container *inodes = ctx; | |
4835 | const size_t c = 3 * sizeof(u64); | |
4836 | ||
4837 | if (inodes->bytes_left >= c) { | |
4838 | inodes->bytes_left -= c; | |
4839 | inodes->val[inodes->elem_cnt] = inum; | |
4840 | inodes->val[inodes->elem_cnt + 1] = offset; | |
4841 | inodes->val[inodes->elem_cnt + 2] = root; | |
4842 | inodes->elem_cnt += 3; | |
4843 | } else { | |
4844 | inodes->bytes_missing += c - inodes->bytes_left; | |
4845 | inodes->bytes_left = 0; | |
4846 | inodes->elem_missed += 3; | |
4847 | } | |
4848 | ||
4849 | return 0; | |
4850 | } | |
4851 | ||
2ff7e61e | 4852 | static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, |
d24a67b2 | 4853 | void __user *arg, int version) |
d7728c96 JS |
4854 | { |
4855 | int ret = 0; | |
4856 | int size; | |
d7728c96 JS |
4857 | struct btrfs_ioctl_logical_ino_args *loi; |
4858 | struct btrfs_data_container *inodes = NULL; | |
4859 | struct btrfs_path *path = NULL; | |
d24a67b2 | 4860 | bool ignore_offset; |
d7728c96 JS |
4861 | |
4862 | if (!capable(CAP_SYS_ADMIN)) | |
4863 | return -EPERM; | |
4864 | ||
4865 | loi = memdup_user(arg, sizeof(*loi)); | |
7b9ea627 SV |
4866 | if (IS_ERR(loi)) |
4867 | return PTR_ERR(loi); | |
d7728c96 | 4868 | |
d24a67b2 ZB |
4869 | if (version == 1) { |
4870 | ignore_offset = false; | |
b115e3bc | 4871 | size = min_t(u32, loi->size, SZ_64K); |
d24a67b2 ZB |
4872 | } else { |
4873 | /* All reserved bits must be 0 for now */ | |
4874 | if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { | |
4875 | ret = -EINVAL; | |
4876 | goto out_loi; | |
4877 | } | |
4878 | /* Only accept flags we have defined so far */ | |
4879 | if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { | |
4880 | ret = -EINVAL; | |
4881 | goto out_loi; | |
4882 | } | |
4883 | ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; | |
b115e3bc | 4884 | size = min_t(u32, loi->size, SZ_16M); |
d24a67b2 ZB |
4885 | } |
4886 | ||
d7728c96 JS |
4887 | path = btrfs_alloc_path(); |
4888 | if (!path) { | |
4889 | ret = -ENOMEM; | |
4890 | goto out; | |
4891 | } | |
4892 | ||
d7728c96 JS |
4893 | inodes = init_data_container(size); |
4894 | if (IS_ERR(inodes)) { | |
4895 | ret = PTR_ERR(inodes); | |
4896 | inodes = NULL; | |
4897 | goto out; | |
4898 | } | |
4899 | ||
2ff7e61e | 4900 | ret = iterate_inodes_from_logical(loi->logical, fs_info, path, |
d24a67b2 | 4901 | build_ino_list, inodes, ignore_offset); |
df031f07 | 4902 | if (ret == -EINVAL) |
d7728c96 JS |
4903 | ret = -ENOENT; |
4904 | if (ret < 0) | |
4905 | goto out; | |
4906 | ||
718dc5fa OS |
4907 | ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, |
4908 | size); | |
d7728c96 JS |
4909 | if (ret) |
4910 | ret = -EFAULT; | |
4911 | ||
4912 | out: | |
4913 | btrfs_free_path(path); | |
f54de068 | 4914 | kvfree(inodes); |
d24a67b2 | 4915 | out_loi: |
d7728c96 JS |
4916 | kfree(loi); |
4917 | ||
4918 | return ret; | |
4919 | } | |
4920 | ||
008ef096 | 4921 | void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, |
c9e9f97b ID |
4922 | struct btrfs_ioctl_balance_args *bargs) |
4923 | { | |
4924 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
4925 | ||
4926 | bargs->flags = bctl->flags; | |
4927 | ||
3009a62f | 4928 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) |
837d5b6e ID |
4929 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; |
4930 | if (atomic_read(&fs_info->balance_pause_req)) | |
4931 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; | |
a7e99c69 ID |
4932 | if (atomic_read(&fs_info->balance_cancel_req)) |
4933 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; | |
837d5b6e | 4934 | |
c9e9f97b ID |
4935 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
4936 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); | |
4937 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); | |
19a39dce | 4938 | |
008ef096 DS |
4939 | spin_lock(&fs_info->balance_lock); |
4940 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); | |
4941 | spin_unlock(&fs_info->balance_lock); | |
c9e9f97b ID |
4942 | } |
4943 | ||
9ba1f6e4 | 4944 | static long btrfs_ioctl_balance(struct file *file, void __user *arg) |
c9e9f97b | 4945 | { |
496ad9aa | 4946 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
c9e9f97b ID |
4947 | struct btrfs_fs_info *fs_info = root->fs_info; |
4948 | struct btrfs_ioctl_balance_args *bargs; | |
4949 | struct btrfs_balance_control *bctl; | |
ed0fb78f | 4950 | bool need_unlock; /* for mut. excl. ops lock */ |
c9e9f97b ID |
4951 | int ret; |
4952 | ||
4953 | if (!capable(CAP_SYS_ADMIN)) | |
4954 | return -EPERM; | |
4955 | ||
e54bfa31 | 4956 | ret = mnt_want_write_file(file); |
9ba1f6e4 LB |
4957 | if (ret) |
4958 | return ret; | |
4959 | ||
ed0fb78f | 4960 | again: |
171938e5 | 4961 | if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
ed0fb78f ID |
4962 | mutex_lock(&fs_info->balance_mutex); |
4963 | need_unlock = true; | |
4964 | goto locked; | |
4965 | } | |
4966 | ||
4967 | /* | |
01327610 | 4968 | * mut. excl. ops lock is locked. Three possibilities: |
ed0fb78f ID |
4969 | * (1) some other op is running |
4970 | * (2) balance is running | |
4971 | * (3) balance is paused -- special case (think resume) | |
4972 | */ | |
c9e9f97b | 4973 | mutex_lock(&fs_info->balance_mutex); |
ed0fb78f ID |
4974 | if (fs_info->balance_ctl) { |
4975 | /* this is either (2) or (3) */ | |
3009a62f | 4976 | if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
ed0fb78f | 4977 | mutex_unlock(&fs_info->balance_mutex); |
dccdb07b DS |
4978 | /* |
4979 | * Lock released to allow other waiters to continue, | |
4980 | * we'll reexamine the status again. | |
4981 | */ | |
ed0fb78f ID |
4982 | mutex_lock(&fs_info->balance_mutex); |
4983 | ||
4984 | if (fs_info->balance_ctl && | |
3009a62f | 4985 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
ed0fb78f ID |
4986 | /* this is (3) */ |
4987 | need_unlock = false; | |
4988 | goto locked; | |
4989 | } | |
4990 | ||
4991 | mutex_unlock(&fs_info->balance_mutex); | |
ed0fb78f ID |
4992 | goto again; |
4993 | } else { | |
4994 | /* this is (2) */ | |
4995 | mutex_unlock(&fs_info->balance_mutex); | |
4996 | ret = -EINPROGRESS; | |
4997 | goto out; | |
4998 | } | |
4999 | } else { | |
5000 | /* this is (1) */ | |
5001 | mutex_unlock(&fs_info->balance_mutex); | |
e57138b3 | 5002 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
ed0fb78f ID |
5003 | goto out; |
5004 | } | |
5005 | ||
5006 | locked: | |
171938e5 | 5007 | BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); |
c9e9f97b ID |
5008 | |
5009 | if (arg) { | |
5010 | bargs = memdup_user(arg, sizeof(*bargs)); | |
5011 | if (IS_ERR(bargs)) { | |
5012 | ret = PTR_ERR(bargs); | |
ed0fb78f | 5013 | goto out_unlock; |
c9e9f97b | 5014 | } |
de322263 ID |
5015 | |
5016 | if (bargs->flags & BTRFS_BALANCE_RESUME) { | |
5017 | if (!fs_info->balance_ctl) { | |
5018 | ret = -ENOTCONN; | |
5019 | goto out_bargs; | |
5020 | } | |
5021 | ||
5022 | bctl = fs_info->balance_ctl; | |
5023 | spin_lock(&fs_info->balance_lock); | |
5024 | bctl->flags |= BTRFS_BALANCE_RESUME; | |
5025 | spin_unlock(&fs_info->balance_lock); | |
5026 | ||
5027 | goto do_balance; | |
5028 | } | |
c9e9f97b ID |
5029 | } else { |
5030 | bargs = NULL; | |
5031 | } | |
5032 | ||
ed0fb78f | 5033 | if (fs_info->balance_ctl) { |
837d5b6e ID |
5034 | ret = -EINPROGRESS; |
5035 | goto out_bargs; | |
5036 | } | |
5037 | ||
8d2db785 | 5038 | bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); |
c9e9f97b ID |
5039 | if (!bctl) { |
5040 | ret = -ENOMEM; | |
5041 | goto out_bargs; | |
5042 | } | |
5043 | ||
c9e9f97b ID |
5044 | if (arg) { |
5045 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); | |
5046 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); | |
5047 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); | |
5048 | ||
5049 | bctl->flags = bargs->flags; | |
f43ffb60 ID |
5050 | } else { |
5051 | /* balance everything - no filters */ | |
5052 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; | |
c9e9f97b ID |
5053 | } |
5054 | ||
8eb93459 DS |
5055 | if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { |
5056 | ret = -EINVAL; | |
0f89abf5 | 5057 | goto out_bctl; |
8eb93459 DS |
5058 | } |
5059 | ||
de322263 | 5060 | do_balance: |
c9e9f97b | 5061 | /* |
149196a2 DS |
5062 | * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to |
5063 | * btrfs_balance. bctl is freed in reset_balance_state, or, if | |
5064 | * restriper was paused all the way until unmount, in free_fs_info. | |
5065 | * The flag should be cleared after reset_balance_state. | |
c9e9f97b | 5066 | */ |
ed0fb78f ID |
5067 | need_unlock = false; |
5068 | ||
6fcf6e2b | 5069 | ret = btrfs_balance(fs_info, bctl, bargs); |
0f89abf5 | 5070 | bctl = NULL; |
ed0fb78f | 5071 | |
c9e9f97b ID |
5072 | if (arg) { |
5073 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | |
5074 | ret = -EFAULT; | |
5075 | } | |
5076 | ||
0f89abf5 CE |
5077 | out_bctl: |
5078 | kfree(bctl); | |
c9e9f97b ID |
5079 | out_bargs: |
5080 | kfree(bargs); | |
ed0fb78f | 5081 | out_unlock: |
c9e9f97b | 5082 | mutex_unlock(&fs_info->balance_mutex); |
ed0fb78f | 5083 | if (need_unlock) |
171938e5 | 5084 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
ed0fb78f | 5085 | out: |
e54bfa31 | 5086 | mnt_drop_write_file(file); |
c9e9f97b ID |
5087 | return ret; |
5088 | } | |
5089 | ||
2ff7e61e | 5090 | static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) |
837d5b6e ID |
5091 | { |
5092 | if (!capable(CAP_SYS_ADMIN)) | |
5093 | return -EPERM; | |
5094 | ||
5095 | switch (cmd) { | |
5096 | case BTRFS_BALANCE_CTL_PAUSE: | |
0b246afa | 5097 | return btrfs_pause_balance(fs_info); |
a7e99c69 | 5098 | case BTRFS_BALANCE_CTL_CANCEL: |
0b246afa | 5099 | return btrfs_cancel_balance(fs_info); |
837d5b6e ID |
5100 | } |
5101 | ||
5102 | return -EINVAL; | |
5103 | } | |
5104 | ||
2ff7e61e | 5105 | static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, |
19a39dce ID |
5106 | void __user *arg) |
5107 | { | |
19a39dce ID |
5108 | struct btrfs_ioctl_balance_args *bargs; |
5109 | int ret = 0; | |
5110 | ||
5111 | if (!capable(CAP_SYS_ADMIN)) | |
5112 | return -EPERM; | |
5113 | ||
5114 | mutex_lock(&fs_info->balance_mutex); | |
5115 | if (!fs_info->balance_ctl) { | |
5116 | ret = -ENOTCONN; | |
5117 | goto out; | |
5118 | } | |
5119 | ||
8d2db785 | 5120 | bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); |
19a39dce ID |
5121 | if (!bargs) { |
5122 | ret = -ENOMEM; | |
5123 | goto out; | |
5124 | } | |
5125 | ||
008ef096 | 5126 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
19a39dce ID |
5127 | |
5128 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | |
5129 | ret = -EFAULT; | |
5130 | ||
5131 | kfree(bargs); | |
5132 | out: | |
5133 | mutex_unlock(&fs_info->balance_mutex); | |
5134 | return ret; | |
5135 | } | |
5136 | ||
905b0dda | 5137 | static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) |
5d13a37b | 5138 | { |
0b246afa JM |
5139 | struct inode *inode = file_inode(file); |
5140 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5d13a37b | 5141 | struct btrfs_ioctl_quota_ctl_args *sa; |
5d13a37b | 5142 | int ret; |
5d13a37b AJ |
5143 | |
5144 | if (!capable(CAP_SYS_ADMIN)) | |
5145 | return -EPERM; | |
5146 | ||
905b0dda MX |
5147 | ret = mnt_want_write_file(file); |
5148 | if (ret) | |
5149 | return ret; | |
5d13a37b AJ |
5150 | |
5151 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
5152 | if (IS_ERR(sa)) { |
5153 | ret = PTR_ERR(sa); | |
5154 | goto drop_write; | |
5155 | } | |
5d13a37b | 5156 | |
0b246afa | 5157 | down_write(&fs_info->subvol_sem); |
5d13a37b AJ |
5158 | |
5159 | switch (sa->cmd) { | |
5160 | case BTRFS_QUOTA_CTL_ENABLE: | |
340f1aa2 | 5161 | ret = btrfs_quota_enable(fs_info); |
5d13a37b AJ |
5162 | break; |
5163 | case BTRFS_QUOTA_CTL_DISABLE: | |
340f1aa2 | 5164 | ret = btrfs_quota_disable(fs_info); |
5d13a37b | 5165 | break; |
5d13a37b AJ |
5166 | default: |
5167 | ret = -EINVAL; | |
5168 | break; | |
5169 | } | |
5170 | ||
5d13a37b | 5171 | kfree(sa); |
0b246afa | 5172 | up_write(&fs_info->subvol_sem); |
905b0dda MX |
5173 | drop_write: |
5174 | mnt_drop_write_file(file); | |
5d13a37b AJ |
5175 | return ret; |
5176 | } | |
5177 | ||
905b0dda | 5178 | static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) |
5d13a37b | 5179 | { |
0b246afa JM |
5180 | struct inode *inode = file_inode(file); |
5181 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5182 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5d13a37b AJ |
5183 | struct btrfs_ioctl_qgroup_assign_args *sa; |
5184 | struct btrfs_trans_handle *trans; | |
5185 | int ret; | |
5186 | int err; | |
5187 | ||
5188 | if (!capable(CAP_SYS_ADMIN)) | |
5189 | return -EPERM; | |
5190 | ||
905b0dda MX |
5191 | ret = mnt_want_write_file(file); |
5192 | if (ret) | |
5193 | return ret; | |
5d13a37b AJ |
5194 | |
5195 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
5196 | if (IS_ERR(sa)) { |
5197 | ret = PTR_ERR(sa); | |
5198 | goto drop_write; | |
5199 | } | |
5d13a37b AJ |
5200 | |
5201 | trans = btrfs_join_transaction(root); | |
5202 | if (IS_ERR(trans)) { | |
5203 | ret = PTR_ERR(trans); | |
5204 | goto out; | |
5205 | } | |
5206 | ||
5d13a37b | 5207 | if (sa->assign) { |
9f8a6ce6 | 5208 | ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst); |
5d13a37b | 5209 | } else { |
39616c27 | 5210 | ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst); |
5d13a37b AJ |
5211 | } |
5212 | ||
e082f563 | 5213 | /* update qgroup status and info */ |
280f8bd2 | 5214 | err = btrfs_run_qgroups(trans); |
e082f563 | 5215 | if (err < 0) |
0b246afa JM |
5216 | btrfs_handle_fs_error(fs_info, err, |
5217 | "failed to update qgroup status and info"); | |
3a45bb20 | 5218 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
5219 | if (err && !ret) |
5220 | ret = err; | |
5221 | ||
5222 | out: | |
5223 | kfree(sa); | |
905b0dda MX |
5224 | drop_write: |
5225 | mnt_drop_write_file(file); | |
5d13a37b AJ |
5226 | return ret; |
5227 | } | |
5228 | ||
905b0dda | 5229 | static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) |
5d13a37b | 5230 | { |
0b246afa | 5231 | struct inode *inode = file_inode(file); |
0b246afa | 5232 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5d13a37b AJ |
5233 | struct btrfs_ioctl_qgroup_create_args *sa; |
5234 | struct btrfs_trans_handle *trans; | |
5235 | int ret; | |
5236 | int err; | |
5237 | ||
5238 | if (!capable(CAP_SYS_ADMIN)) | |
5239 | return -EPERM; | |
5240 | ||
905b0dda MX |
5241 | ret = mnt_want_write_file(file); |
5242 | if (ret) | |
5243 | return ret; | |
5d13a37b AJ |
5244 | |
5245 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
5246 | if (IS_ERR(sa)) { |
5247 | ret = PTR_ERR(sa); | |
5248 | goto drop_write; | |
5249 | } | |
5d13a37b | 5250 | |
d86e56cf MX |
5251 | if (!sa->qgroupid) { |
5252 | ret = -EINVAL; | |
5253 | goto out; | |
5254 | } | |
5255 | ||
5d13a37b AJ |
5256 | trans = btrfs_join_transaction(root); |
5257 | if (IS_ERR(trans)) { | |
5258 | ret = PTR_ERR(trans); | |
5259 | goto out; | |
5260 | } | |
5261 | ||
5d13a37b | 5262 | if (sa->create) { |
49a05ecd | 5263 | ret = btrfs_create_qgroup(trans, sa->qgroupid); |
5d13a37b | 5264 | } else { |
3efbee1d | 5265 | ret = btrfs_remove_qgroup(trans, sa->qgroupid); |
5d13a37b AJ |
5266 | } |
5267 | ||
3a45bb20 | 5268 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
5269 | if (err && !ret) |
5270 | ret = err; | |
5271 | ||
5272 | out: | |
5273 | kfree(sa); | |
905b0dda MX |
5274 | drop_write: |
5275 | mnt_drop_write_file(file); | |
5d13a37b AJ |
5276 | return ret; |
5277 | } | |
5278 | ||
905b0dda | 5279 | static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) |
5d13a37b | 5280 | { |
0b246afa | 5281 | struct inode *inode = file_inode(file); |
0b246afa | 5282 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5d13a37b AJ |
5283 | struct btrfs_ioctl_qgroup_limit_args *sa; |
5284 | struct btrfs_trans_handle *trans; | |
5285 | int ret; | |
5286 | int err; | |
5287 | u64 qgroupid; | |
5288 | ||
5289 | if (!capable(CAP_SYS_ADMIN)) | |
5290 | return -EPERM; | |
5291 | ||
905b0dda MX |
5292 | ret = mnt_want_write_file(file); |
5293 | if (ret) | |
5294 | return ret; | |
5d13a37b AJ |
5295 | |
5296 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
5297 | if (IS_ERR(sa)) { |
5298 | ret = PTR_ERR(sa); | |
5299 | goto drop_write; | |
5300 | } | |
5d13a37b AJ |
5301 | |
5302 | trans = btrfs_join_transaction(root); | |
5303 | if (IS_ERR(trans)) { | |
5304 | ret = PTR_ERR(trans); | |
5305 | goto out; | |
5306 | } | |
5307 | ||
5308 | qgroupid = sa->qgroupid; | |
5309 | if (!qgroupid) { | |
5310 | /* take the current subvol as qgroup */ | |
5311 | qgroupid = root->root_key.objectid; | |
5312 | } | |
5313 | ||
f0042d5e | 5314 | ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim); |
5d13a37b | 5315 | |
3a45bb20 | 5316 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
5317 | if (err && !ret) |
5318 | ret = err; | |
5319 | ||
5320 | out: | |
5321 | kfree(sa); | |
905b0dda MX |
5322 | drop_write: |
5323 | mnt_drop_write_file(file); | |
5d13a37b AJ |
5324 | return ret; |
5325 | } | |
5326 | ||
2f232036 JS |
5327 | static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) |
5328 | { | |
0b246afa JM |
5329 | struct inode *inode = file_inode(file); |
5330 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2f232036 JS |
5331 | struct btrfs_ioctl_quota_rescan_args *qsa; |
5332 | int ret; | |
5333 | ||
5334 | if (!capable(CAP_SYS_ADMIN)) | |
5335 | return -EPERM; | |
5336 | ||
5337 | ret = mnt_want_write_file(file); | |
5338 | if (ret) | |
5339 | return ret; | |
5340 | ||
5341 | qsa = memdup_user(arg, sizeof(*qsa)); | |
5342 | if (IS_ERR(qsa)) { | |
5343 | ret = PTR_ERR(qsa); | |
5344 | goto drop_write; | |
5345 | } | |
5346 | ||
5347 | if (qsa->flags) { | |
5348 | ret = -EINVAL; | |
5349 | goto out; | |
5350 | } | |
5351 | ||
0b246afa | 5352 | ret = btrfs_qgroup_rescan(fs_info); |
2f232036 JS |
5353 | |
5354 | out: | |
5355 | kfree(qsa); | |
5356 | drop_write: | |
5357 | mnt_drop_write_file(file); | |
5358 | return ret; | |
5359 | } | |
5360 | ||
5361 | static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) | |
5362 | { | |
0b246afa JM |
5363 | struct inode *inode = file_inode(file); |
5364 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2f232036 JS |
5365 | struct btrfs_ioctl_quota_rescan_args *qsa; |
5366 | int ret = 0; | |
5367 | ||
5368 | if (!capable(CAP_SYS_ADMIN)) | |
5369 | return -EPERM; | |
5370 | ||
8d2db785 | 5371 | qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); |
2f232036 JS |
5372 | if (!qsa) |
5373 | return -ENOMEM; | |
5374 | ||
0b246afa | 5375 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
2f232036 | 5376 | qsa->flags = 1; |
0b246afa | 5377 | qsa->progress = fs_info->qgroup_rescan_progress.objectid; |
2f232036 JS |
5378 | } |
5379 | ||
5380 | if (copy_to_user(arg, qsa, sizeof(*qsa))) | |
5381 | ret = -EFAULT; | |
5382 | ||
5383 | kfree(qsa); | |
5384 | return ret; | |
5385 | } | |
5386 | ||
57254b6e JS |
5387 | static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) |
5388 | { | |
0b246afa JM |
5389 | struct inode *inode = file_inode(file); |
5390 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
57254b6e JS |
5391 | |
5392 | if (!capable(CAP_SYS_ADMIN)) | |
5393 | return -EPERM; | |
5394 | ||
0b246afa | 5395 | return btrfs_qgroup_wait_for_completion(fs_info, true); |
57254b6e JS |
5396 | } |
5397 | ||
abccd00f HM |
5398 | static long _btrfs_ioctl_set_received_subvol(struct file *file, |
5399 | struct btrfs_ioctl_received_subvol_args *sa) | |
8ea05e3a | 5400 | { |
496ad9aa | 5401 | struct inode *inode = file_inode(file); |
0b246afa | 5402 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8ea05e3a AB |
5403 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5404 | struct btrfs_root_item *root_item = &root->root_item; | |
5405 | struct btrfs_trans_handle *trans; | |
95582b00 | 5406 | struct timespec64 ct = current_time(inode); |
8ea05e3a | 5407 | int ret = 0; |
dd5f9615 | 5408 | int received_uuid_changed; |
8ea05e3a | 5409 | |
bd60ea0f DS |
5410 | if (!inode_owner_or_capable(inode)) |
5411 | return -EPERM; | |
5412 | ||
8ea05e3a AB |
5413 | ret = mnt_want_write_file(file); |
5414 | if (ret < 0) | |
5415 | return ret; | |
5416 | ||
0b246afa | 5417 | down_write(&fs_info->subvol_sem); |
8ea05e3a | 5418 | |
4a0cc7ca | 5419 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
8ea05e3a AB |
5420 | ret = -EINVAL; |
5421 | goto out; | |
5422 | } | |
5423 | ||
5424 | if (btrfs_root_readonly(root)) { | |
5425 | ret = -EROFS; | |
5426 | goto out; | |
5427 | } | |
5428 | ||
dd5f9615 SB |
5429 | /* |
5430 | * 1 - root item | |
5431 | * 2 - uuid items (received uuid + subvol uuid) | |
5432 | */ | |
5433 | trans = btrfs_start_transaction(root, 3); | |
8ea05e3a AB |
5434 | if (IS_ERR(trans)) { |
5435 | ret = PTR_ERR(trans); | |
5436 | trans = NULL; | |
5437 | goto out; | |
5438 | } | |
5439 | ||
5440 | sa->rtransid = trans->transid; | |
5441 | sa->rtime.sec = ct.tv_sec; | |
5442 | sa->rtime.nsec = ct.tv_nsec; | |
5443 | ||
dd5f9615 SB |
5444 | received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, |
5445 | BTRFS_UUID_SIZE); | |
5446 | if (received_uuid_changed && | |
d87ff758 | 5447 | !btrfs_is_empty_uuid(root_item->received_uuid)) { |
d1957791 | 5448 | ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid, |
d87ff758 NB |
5449 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
5450 | root->root_key.objectid); | |
5451 | if (ret && ret != -ENOENT) { | |
5452 | btrfs_abort_transaction(trans, ret); | |
5453 | btrfs_end_transaction(trans); | |
5454 | goto out; | |
5455 | } | |
5456 | } | |
8ea05e3a AB |
5457 | memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); |
5458 | btrfs_set_root_stransid(root_item, sa->stransid); | |
5459 | btrfs_set_root_rtransid(root_item, sa->rtransid); | |
3cae210f QW |
5460 | btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); |
5461 | btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); | |
5462 | btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); | |
5463 | btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); | |
8ea05e3a | 5464 | |
0b246afa | 5465 | ret = btrfs_update_root(trans, fs_info->tree_root, |
8ea05e3a AB |
5466 | &root->root_key, &root->root_item); |
5467 | if (ret < 0) { | |
3a45bb20 | 5468 | btrfs_end_transaction(trans); |
8ea05e3a | 5469 | goto out; |
dd5f9615 SB |
5470 | } |
5471 | if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { | |
cdb345a8 | 5472 | ret = btrfs_uuid_tree_add(trans, sa->uuid, |
dd5f9615 SB |
5473 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
5474 | root->root_key.objectid); | |
5475 | if (ret < 0 && ret != -EEXIST) { | |
66642832 | 5476 | btrfs_abort_transaction(trans, ret); |
efd38150 | 5477 | btrfs_end_transaction(trans); |
8ea05e3a | 5478 | goto out; |
dd5f9615 SB |
5479 | } |
5480 | } | |
3a45bb20 | 5481 | ret = btrfs_commit_transaction(trans); |
abccd00f | 5482 | out: |
0b246afa | 5483 | up_write(&fs_info->subvol_sem); |
abccd00f HM |
5484 | mnt_drop_write_file(file); |
5485 | return ret; | |
5486 | } | |
5487 | ||
5488 | #ifdef CONFIG_64BIT | |
5489 | static long btrfs_ioctl_set_received_subvol_32(struct file *file, | |
5490 | void __user *arg) | |
5491 | { | |
5492 | struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; | |
5493 | struct btrfs_ioctl_received_subvol_args *args64 = NULL; | |
5494 | int ret = 0; | |
5495 | ||
5496 | args32 = memdup_user(arg, sizeof(*args32)); | |
7b9ea627 SV |
5497 | if (IS_ERR(args32)) |
5498 | return PTR_ERR(args32); | |
abccd00f | 5499 | |
8d2db785 | 5500 | args64 = kmalloc(sizeof(*args64), GFP_KERNEL); |
84dbeb87 DC |
5501 | if (!args64) { |
5502 | ret = -ENOMEM; | |
abccd00f HM |
5503 | goto out; |
5504 | } | |
5505 | ||
5506 | memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); | |
5507 | args64->stransid = args32->stransid; | |
5508 | args64->rtransid = args32->rtransid; | |
5509 | args64->stime.sec = args32->stime.sec; | |
5510 | args64->stime.nsec = args32->stime.nsec; | |
5511 | args64->rtime.sec = args32->rtime.sec; | |
5512 | args64->rtime.nsec = args32->rtime.nsec; | |
5513 | args64->flags = args32->flags; | |
5514 | ||
5515 | ret = _btrfs_ioctl_set_received_subvol(file, args64); | |
5516 | if (ret) | |
5517 | goto out; | |
5518 | ||
5519 | memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); | |
5520 | args32->stransid = args64->stransid; | |
5521 | args32->rtransid = args64->rtransid; | |
5522 | args32->stime.sec = args64->stime.sec; | |
5523 | args32->stime.nsec = args64->stime.nsec; | |
5524 | args32->rtime.sec = args64->rtime.sec; | |
5525 | args32->rtime.nsec = args64->rtime.nsec; | |
5526 | args32->flags = args64->flags; | |
5527 | ||
5528 | ret = copy_to_user(arg, args32, sizeof(*args32)); | |
5529 | if (ret) | |
5530 | ret = -EFAULT; | |
5531 | ||
5532 | out: | |
5533 | kfree(args32); | |
5534 | kfree(args64); | |
5535 | return ret; | |
5536 | } | |
5537 | #endif | |
5538 | ||
5539 | static long btrfs_ioctl_set_received_subvol(struct file *file, | |
5540 | void __user *arg) | |
5541 | { | |
5542 | struct btrfs_ioctl_received_subvol_args *sa = NULL; | |
5543 | int ret = 0; | |
5544 | ||
5545 | sa = memdup_user(arg, sizeof(*sa)); | |
7b9ea627 SV |
5546 | if (IS_ERR(sa)) |
5547 | return PTR_ERR(sa); | |
abccd00f HM |
5548 | |
5549 | ret = _btrfs_ioctl_set_received_subvol(file, sa); | |
5550 | ||
5551 | if (ret) | |
5552 | goto out; | |
5553 | ||
8ea05e3a AB |
5554 | ret = copy_to_user(arg, sa, sizeof(*sa)); |
5555 | if (ret) | |
5556 | ret = -EFAULT; | |
5557 | ||
5558 | out: | |
5559 | kfree(sa); | |
8ea05e3a AB |
5560 | return ret; |
5561 | } | |
5562 | ||
867ab667 | 5563 | static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) |
5564 | { | |
0b246afa JM |
5565 | struct inode *inode = file_inode(file); |
5566 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
a1b83ac5 | 5567 | size_t len; |
867ab667 | 5568 | int ret; |
a1b83ac5 AJ |
5569 | char label[BTRFS_LABEL_SIZE]; |
5570 | ||
0b246afa JM |
5571 | spin_lock(&fs_info->super_lock); |
5572 | memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); | |
5573 | spin_unlock(&fs_info->super_lock); | |
a1b83ac5 AJ |
5574 | |
5575 | len = strnlen(label, BTRFS_LABEL_SIZE); | |
867ab667 | 5576 | |
5577 | if (len == BTRFS_LABEL_SIZE) { | |
0b246afa JM |
5578 | btrfs_warn(fs_info, |
5579 | "label is too long, return the first %zu bytes", | |
5580 | --len); | |
867ab667 | 5581 | } |
5582 | ||
867ab667 | 5583 | ret = copy_to_user(arg, label, len); |
867ab667 | 5584 | |
5585 | return ret ? -EFAULT : 0; | |
5586 | } | |
5587 | ||
a8bfd4ab | 5588 | static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) |
5589 | { | |
0b246afa JM |
5590 | struct inode *inode = file_inode(file); |
5591 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5592 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5593 | struct btrfs_super_block *super_block = fs_info->super_copy; | |
a8bfd4ab | 5594 | struct btrfs_trans_handle *trans; |
5595 | char label[BTRFS_LABEL_SIZE]; | |
5596 | int ret; | |
5597 | ||
5598 | if (!capable(CAP_SYS_ADMIN)) | |
5599 | return -EPERM; | |
5600 | ||
5601 | if (copy_from_user(label, arg, sizeof(label))) | |
5602 | return -EFAULT; | |
5603 | ||
5604 | if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { | |
0b246afa | 5605 | btrfs_err(fs_info, |
5d163e0e JM |
5606 | "unable to set label with more than %d bytes", |
5607 | BTRFS_LABEL_SIZE - 1); | |
a8bfd4ab | 5608 | return -EINVAL; |
5609 | } | |
5610 | ||
5611 | ret = mnt_want_write_file(file); | |
5612 | if (ret) | |
5613 | return ret; | |
5614 | ||
a8bfd4ab | 5615 | trans = btrfs_start_transaction(root, 0); |
5616 | if (IS_ERR(trans)) { | |
5617 | ret = PTR_ERR(trans); | |
5618 | goto out_unlock; | |
5619 | } | |
5620 | ||
0b246afa | 5621 | spin_lock(&fs_info->super_lock); |
a8bfd4ab | 5622 | strcpy(super_block->label, label); |
0b246afa | 5623 | spin_unlock(&fs_info->super_lock); |
3a45bb20 | 5624 | ret = btrfs_commit_transaction(trans); |
a8bfd4ab | 5625 | |
5626 | out_unlock: | |
a8bfd4ab | 5627 | mnt_drop_write_file(file); |
5628 | return ret; | |
5629 | } | |
5630 | ||
2eaa055f JM |
5631 | #define INIT_FEATURE_FLAGS(suffix) \ |
5632 | { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ | |
5633 | .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ | |
5634 | .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } | |
5635 | ||
d5131b65 | 5636 | int btrfs_ioctl_get_supported_features(void __user *arg) |
2eaa055f | 5637 | { |
4d4ab6d6 | 5638 | static const struct btrfs_ioctl_feature_flags features[3] = { |
2eaa055f JM |
5639 | INIT_FEATURE_FLAGS(SUPP), |
5640 | INIT_FEATURE_FLAGS(SAFE_SET), | |
5641 | INIT_FEATURE_FLAGS(SAFE_CLEAR) | |
5642 | }; | |
5643 | ||
5644 | if (copy_to_user(arg, &features, sizeof(features))) | |
5645 | return -EFAULT; | |
5646 | ||
5647 | return 0; | |
5648 | } | |
5649 | ||
5650 | static int btrfs_ioctl_get_features(struct file *file, void __user *arg) | |
5651 | { | |
0b246afa JM |
5652 | struct inode *inode = file_inode(file); |
5653 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5654 | struct btrfs_super_block *super_block = fs_info->super_copy; | |
2eaa055f JM |
5655 | struct btrfs_ioctl_feature_flags features; |
5656 | ||
5657 | features.compat_flags = btrfs_super_compat_flags(super_block); | |
5658 | features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); | |
5659 | features.incompat_flags = btrfs_super_incompat_flags(super_block); | |
5660 | ||
5661 | if (copy_to_user(arg, &features, sizeof(features))) | |
5662 | return -EFAULT; | |
5663 | ||
5664 | return 0; | |
5665 | } | |
5666 | ||
2ff7e61e | 5667 | static int check_feature_bits(struct btrfs_fs_info *fs_info, |
3b02a68a | 5668 | enum btrfs_feature_set set, |
2eaa055f JM |
5669 | u64 change_mask, u64 flags, u64 supported_flags, |
5670 | u64 safe_set, u64 safe_clear) | |
5671 | { | |
3b02a68a JM |
5672 | const char *type = btrfs_feature_set_names[set]; |
5673 | char *names; | |
2eaa055f JM |
5674 | u64 disallowed, unsupported; |
5675 | u64 set_mask = flags & change_mask; | |
5676 | u64 clear_mask = ~flags & change_mask; | |
5677 | ||
5678 | unsupported = set_mask & ~supported_flags; | |
5679 | if (unsupported) { | |
3b02a68a JM |
5680 | names = btrfs_printable_features(set, unsupported); |
5681 | if (names) { | |
0b246afa JM |
5682 | btrfs_warn(fs_info, |
5683 | "this kernel does not support the %s feature bit%s", | |
5684 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
5685 | kfree(names); |
5686 | } else | |
0b246afa JM |
5687 | btrfs_warn(fs_info, |
5688 | "this kernel does not support %s bits 0x%llx", | |
5689 | type, unsupported); | |
2eaa055f JM |
5690 | return -EOPNOTSUPP; |
5691 | } | |
5692 | ||
5693 | disallowed = set_mask & ~safe_set; | |
5694 | if (disallowed) { | |
3b02a68a JM |
5695 | names = btrfs_printable_features(set, disallowed); |
5696 | if (names) { | |
0b246afa JM |
5697 | btrfs_warn(fs_info, |
5698 | "can't set the %s feature bit%s while mounted", | |
5699 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
5700 | kfree(names); |
5701 | } else | |
0b246afa JM |
5702 | btrfs_warn(fs_info, |
5703 | "can't set %s bits 0x%llx while mounted", | |
5704 | type, disallowed); | |
2eaa055f JM |
5705 | return -EPERM; |
5706 | } | |
5707 | ||
5708 | disallowed = clear_mask & ~safe_clear; | |
5709 | if (disallowed) { | |
3b02a68a JM |
5710 | names = btrfs_printable_features(set, disallowed); |
5711 | if (names) { | |
0b246afa JM |
5712 | btrfs_warn(fs_info, |
5713 | "can't clear the %s feature bit%s while mounted", | |
5714 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
5715 | kfree(names); |
5716 | } else | |
0b246afa JM |
5717 | btrfs_warn(fs_info, |
5718 | "can't clear %s bits 0x%llx while mounted", | |
5719 | type, disallowed); | |
2eaa055f JM |
5720 | return -EPERM; |
5721 | } | |
5722 | ||
5723 | return 0; | |
5724 | } | |
5725 | ||
2ff7e61e JM |
5726 | #define check_feature(fs_info, change_mask, flags, mask_base) \ |
5727 | check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ | |
2eaa055f JM |
5728 | BTRFS_FEATURE_ ## mask_base ## _SUPP, \ |
5729 | BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ | |
5730 | BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) | |
5731 | ||
5732 | static int btrfs_ioctl_set_features(struct file *file, void __user *arg) | |
5733 | { | |
0b246afa JM |
5734 | struct inode *inode = file_inode(file); |
5735 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5736 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5737 | struct btrfs_super_block *super_block = fs_info->super_copy; | |
2eaa055f JM |
5738 | struct btrfs_ioctl_feature_flags flags[2]; |
5739 | struct btrfs_trans_handle *trans; | |
5740 | u64 newflags; | |
5741 | int ret; | |
5742 | ||
5743 | if (!capable(CAP_SYS_ADMIN)) | |
5744 | return -EPERM; | |
5745 | ||
5746 | if (copy_from_user(flags, arg, sizeof(flags))) | |
5747 | return -EFAULT; | |
5748 | ||
5749 | /* Nothing to do */ | |
5750 | if (!flags[0].compat_flags && !flags[0].compat_ro_flags && | |
5751 | !flags[0].incompat_flags) | |
5752 | return 0; | |
5753 | ||
2ff7e61e | 5754 | ret = check_feature(fs_info, flags[0].compat_flags, |
2eaa055f JM |
5755 | flags[1].compat_flags, COMPAT); |
5756 | if (ret) | |
5757 | return ret; | |
5758 | ||
2ff7e61e | 5759 | ret = check_feature(fs_info, flags[0].compat_ro_flags, |
2eaa055f JM |
5760 | flags[1].compat_ro_flags, COMPAT_RO); |
5761 | if (ret) | |
5762 | return ret; | |
5763 | ||
2ff7e61e | 5764 | ret = check_feature(fs_info, flags[0].incompat_flags, |
2eaa055f JM |
5765 | flags[1].incompat_flags, INCOMPAT); |
5766 | if (ret) | |
5767 | return ret; | |
5768 | ||
7ab19625 DS |
5769 | ret = mnt_want_write_file(file); |
5770 | if (ret) | |
5771 | return ret; | |
5772 | ||
8051aa1a | 5773 | trans = btrfs_start_transaction(root, 0); |
7ab19625 DS |
5774 | if (IS_ERR(trans)) { |
5775 | ret = PTR_ERR(trans); | |
5776 | goto out_drop_write; | |
5777 | } | |
2eaa055f | 5778 | |
0b246afa | 5779 | spin_lock(&fs_info->super_lock); |
2eaa055f JM |
5780 | newflags = btrfs_super_compat_flags(super_block); |
5781 | newflags |= flags[0].compat_flags & flags[1].compat_flags; | |
5782 | newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); | |
5783 | btrfs_set_super_compat_flags(super_block, newflags); | |
5784 | ||
5785 | newflags = btrfs_super_compat_ro_flags(super_block); | |
5786 | newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; | |
5787 | newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); | |
5788 | btrfs_set_super_compat_ro_flags(super_block, newflags); | |
5789 | ||
5790 | newflags = btrfs_super_incompat_flags(super_block); | |
5791 | newflags |= flags[0].incompat_flags & flags[1].incompat_flags; | |
5792 | newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); | |
5793 | btrfs_set_super_incompat_flags(super_block, newflags); | |
0b246afa | 5794 | spin_unlock(&fs_info->super_lock); |
2eaa055f | 5795 | |
3a45bb20 | 5796 | ret = btrfs_commit_transaction(trans); |
7ab19625 DS |
5797 | out_drop_write: |
5798 | mnt_drop_write_file(file); | |
5799 | ||
5800 | return ret; | |
2eaa055f JM |
5801 | } |
5802 | ||
2351f431 JB |
5803 | static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) |
5804 | { | |
5805 | struct btrfs_ioctl_send_args *arg; | |
5806 | int ret; | |
5807 | ||
5808 | if (compat) { | |
5809 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) | |
5810 | struct btrfs_ioctl_send_args_32 args32; | |
5811 | ||
5812 | ret = copy_from_user(&args32, argp, sizeof(args32)); | |
5813 | if (ret) | |
5814 | return -EFAULT; | |
5815 | arg = kzalloc(sizeof(*arg), GFP_KERNEL); | |
5816 | if (!arg) | |
5817 | return -ENOMEM; | |
5818 | arg->send_fd = args32.send_fd; | |
5819 | arg->clone_sources_count = args32.clone_sources_count; | |
5820 | arg->clone_sources = compat_ptr(args32.clone_sources); | |
5821 | arg->parent_root = args32.parent_root; | |
5822 | arg->flags = args32.flags; | |
5823 | memcpy(arg->reserved, args32.reserved, | |
5824 | sizeof(args32.reserved)); | |
5825 | #else | |
5826 | return -ENOTTY; | |
5827 | #endif | |
5828 | } else { | |
5829 | arg = memdup_user(argp, sizeof(*arg)); | |
5830 | if (IS_ERR(arg)) | |
5831 | return PTR_ERR(arg); | |
5832 | } | |
5833 | ret = btrfs_ioctl_send(file, arg); | |
5834 | kfree(arg); | |
5835 | return ret; | |
5836 | } | |
5837 | ||
f46b5a66 CH |
5838 | long btrfs_ioctl(struct file *file, unsigned int |
5839 | cmd, unsigned long arg) | |
5840 | { | |
0b246afa JM |
5841 | struct inode *inode = file_inode(file); |
5842 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5843 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4bcabaa3 | 5844 | void __user *argp = (void __user *)arg; |
f46b5a66 CH |
5845 | |
5846 | switch (cmd) { | |
6cbff00f CH |
5847 | case FS_IOC_GETFLAGS: |
5848 | return btrfs_ioctl_getflags(file, argp); | |
5849 | case FS_IOC_SETFLAGS: | |
5850 | return btrfs_ioctl_setflags(file, argp); | |
5851 | case FS_IOC_GETVERSION: | |
5852 | return btrfs_ioctl_getversion(file, argp); | |
f7039b1d LD |
5853 | case FITRIM: |
5854 | return btrfs_ioctl_fitrim(file, argp); | |
f46b5a66 | 5855 | case BTRFS_IOC_SNAP_CREATE: |
fa0d2b9b | 5856 | return btrfs_ioctl_snap_create(file, argp, 0); |
fdfb1e4f | 5857 | case BTRFS_IOC_SNAP_CREATE_V2: |
fa0d2b9b | 5858 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
3de4586c | 5859 | case BTRFS_IOC_SUBVOL_CREATE: |
fa0d2b9b | 5860 | return btrfs_ioctl_snap_create(file, argp, 1); |
6f72c7e2 AJ |
5861 | case BTRFS_IOC_SUBVOL_CREATE_V2: |
5862 | return btrfs_ioctl_snap_create_v2(file, argp, 1); | |
76dda93c YZ |
5863 | case BTRFS_IOC_SNAP_DESTROY: |
5864 | return btrfs_ioctl_snap_destroy(file, argp); | |
0caa102d LZ |
5865 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
5866 | return btrfs_ioctl_subvol_getflags(file, argp); | |
5867 | case BTRFS_IOC_SUBVOL_SETFLAGS: | |
5868 | return btrfs_ioctl_subvol_setflags(file, argp); | |
6ef5ed0d JB |
5869 | case BTRFS_IOC_DEFAULT_SUBVOL: |
5870 | return btrfs_ioctl_default_subvol(file, argp); | |
f46b5a66 | 5871 | case BTRFS_IOC_DEFRAG: |
1e701a32 CM |
5872 | return btrfs_ioctl_defrag(file, NULL); |
5873 | case BTRFS_IOC_DEFRAG_RANGE: | |
5874 | return btrfs_ioctl_defrag(file, argp); | |
f46b5a66 | 5875 | case BTRFS_IOC_RESIZE: |
198605a8 | 5876 | return btrfs_ioctl_resize(file, argp); |
f46b5a66 | 5877 | case BTRFS_IOC_ADD_DEV: |
2ff7e61e | 5878 | return btrfs_ioctl_add_dev(fs_info, argp); |
f46b5a66 | 5879 | case BTRFS_IOC_RM_DEV: |
da24927b | 5880 | return btrfs_ioctl_rm_dev(file, argp); |
6b526ed7 AJ |
5881 | case BTRFS_IOC_RM_DEV_V2: |
5882 | return btrfs_ioctl_rm_dev_v2(file, argp); | |
475f6387 | 5883 | case BTRFS_IOC_FS_INFO: |
2ff7e61e | 5884 | return btrfs_ioctl_fs_info(fs_info, argp); |
475f6387 | 5885 | case BTRFS_IOC_DEV_INFO: |
2ff7e61e | 5886 | return btrfs_ioctl_dev_info(fs_info, argp); |
f46b5a66 | 5887 | case BTRFS_IOC_BALANCE: |
9ba1f6e4 | 5888 | return btrfs_ioctl_balance(file, NULL); |
ac8e9819 CM |
5889 | case BTRFS_IOC_TREE_SEARCH: |
5890 | return btrfs_ioctl_tree_search(file, argp); | |
cc68a8a5 GH |
5891 | case BTRFS_IOC_TREE_SEARCH_V2: |
5892 | return btrfs_ioctl_tree_search_v2(file, argp); | |
ac8e9819 CM |
5893 | case BTRFS_IOC_INO_LOOKUP: |
5894 | return btrfs_ioctl_ino_lookup(file, argp); | |
d7728c96 JS |
5895 | case BTRFS_IOC_INO_PATHS: |
5896 | return btrfs_ioctl_ino_to_path(root, argp); | |
5897 | case BTRFS_IOC_LOGICAL_INO: | |
d24a67b2 ZB |
5898 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); |
5899 | case BTRFS_IOC_LOGICAL_INO_V2: | |
5900 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); | |
1406e432 | 5901 | case BTRFS_IOC_SPACE_INFO: |
2ff7e61e | 5902 | return btrfs_ioctl_space_info(fs_info, argp); |
9b199859 FDBM |
5903 | case BTRFS_IOC_SYNC: { |
5904 | int ret; | |
5905 | ||
82b3e53b | 5906 | ret = btrfs_start_delalloc_roots(fs_info, -1); |
9b199859 FDBM |
5907 | if (ret) |
5908 | return ret; | |
0b246afa | 5909 | ret = btrfs_sync_fs(inode->i_sb, 1); |
2fad4e83 DS |
5910 | /* |
5911 | * The transaction thread may want to do more work, | |
01327610 | 5912 | * namely it pokes the cleaner kthread that will start |
2fad4e83 DS |
5913 | * processing uncleaned subvols. |
5914 | */ | |
0b246afa | 5915 | wake_up_process(fs_info->transaction_kthread); |
9b199859 FDBM |
5916 | return ret; |
5917 | } | |
46204592 | 5918 | case BTRFS_IOC_START_SYNC: |
9a8c28be | 5919 | return btrfs_ioctl_start_sync(root, argp); |
46204592 | 5920 | case BTRFS_IOC_WAIT_SYNC: |
2ff7e61e | 5921 | return btrfs_ioctl_wait_sync(fs_info, argp); |
475f6387 | 5922 | case BTRFS_IOC_SCRUB: |
b8e95489 | 5923 | return btrfs_ioctl_scrub(file, argp); |
475f6387 | 5924 | case BTRFS_IOC_SCRUB_CANCEL: |
2ff7e61e | 5925 | return btrfs_ioctl_scrub_cancel(fs_info); |
475f6387 | 5926 | case BTRFS_IOC_SCRUB_PROGRESS: |
2ff7e61e | 5927 | return btrfs_ioctl_scrub_progress(fs_info, argp); |
c9e9f97b | 5928 | case BTRFS_IOC_BALANCE_V2: |
9ba1f6e4 | 5929 | return btrfs_ioctl_balance(file, argp); |
837d5b6e | 5930 | case BTRFS_IOC_BALANCE_CTL: |
2ff7e61e | 5931 | return btrfs_ioctl_balance_ctl(fs_info, arg); |
19a39dce | 5932 | case BTRFS_IOC_BALANCE_PROGRESS: |
2ff7e61e | 5933 | return btrfs_ioctl_balance_progress(fs_info, argp); |
8ea05e3a AB |
5934 | case BTRFS_IOC_SET_RECEIVED_SUBVOL: |
5935 | return btrfs_ioctl_set_received_subvol(file, argp); | |
abccd00f HM |
5936 | #ifdef CONFIG_64BIT |
5937 | case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: | |
5938 | return btrfs_ioctl_set_received_subvol_32(file, argp); | |
5939 | #endif | |
31db9f7c | 5940 | case BTRFS_IOC_SEND: |
2351f431 JB |
5941 | return _btrfs_ioctl_send(file, argp, false); |
5942 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) | |
5943 | case BTRFS_IOC_SEND_32: | |
5944 | return _btrfs_ioctl_send(file, argp, true); | |
5945 | #endif | |
c11d2c23 | 5946 | case BTRFS_IOC_GET_DEV_STATS: |
2ff7e61e | 5947 | return btrfs_ioctl_get_dev_stats(fs_info, argp); |
5d13a37b | 5948 | case BTRFS_IOC_QUOTA_CTL: |
905b0dda | 5949 | return btrfs_ioctl_quota_ctl(file, argp); |
5d13a37b | 5950 | case BTRFS_IOC_QGROUP_ASSIGN: |
905b0dda | 5951 | return btrfs_ioctl_qgroup_assign(file, argp); |
5d13a37b | 5952 | case BTRFS_IOC_QGROUP_CREATE: |
905b0dda | 5953 | return btrfs_ioctl_qgroup_create(file, argp); |
5d13a37b | 5954 | case BTRFS_IOC_QGROUP_LIMIT: |
905b0dda | 5955 | return btrfs_ioctl_qgroup_limit(file, argp); |
2f232036 JS |
5956 | case BTRFS_IOC_QUOTA_RESCAN: |
5957 | return btrfs_ioctl_quota_rescan(file, argp); | |
5958 | case BTRFS_IOC_QUOTA_RESCAN_STATUS: | |
5959 | return btrfs_ioctl_quota_rescan_status(file, argp); | |
57254b6e JS |
5960 | case BTRFS_IOC_QUOTA_RESCAN_WAIT: |
5961 | return btrfs_ioctl_quota_rescan_wait(file, argp); | |
3f6bcfbd | 5962 | case BTRFS_IOC_DEV_REPLACE: |
2ff7e61e | 5963 | return btrfs_ioctl_dev_replace(fs_info, argp); |
867ab667 | 5964 | case BTRFS_IOC_GET_FSLABEL: |
5965 | return btrfs_ioctl_get_fslabel(file, argp); | |
a8bfd4ab | 5966 | case BTRFS_IOC_SET_FSLABEL: |
5967 | return btrfs_ioctl_set_fslabel(file, argp); | |
2eaa055f | 5968 | case BTRFS_IOC_GET_SUPPORTED_FEATURES: |
d5131b65 | 5969 | return btrfs_ioctl_get_supported_features(argp); |
2eaa055f JM |
5970 | case BTRFS_IOC_GET_FEATURES: |
5971 | return btrfs_ioctl_get_features(file, argp); | |
5972 | case BTRFS_IOC_SET_FEATURES: | |
5973 | return btrfs_ioctl_set_features(file, argp); | |
e4202ac9 DS |
5974 | case FS_IOC_FSGETXATTR: |
5975 | return btrfs_ioctl_fsgetxattr(file, argp); | |
025f2121 DS |
5976 | case FS_IOC_FSSETXATTR: |
5977 | return btrfs_ioctl_fssetxattr(file, argp); | |
b64ec075 TM |
5978 | case BTRFS_IOC_GET_SUBVOL_INFO: |
5979 | return btrfs_ioctl_get_subvol_info(file, argp); | |
42e4b520 TM |
5980 | case BTRFS_IOC_GET_SUBVOL_ROOTREF: |
5981 | return btrfs_ioctl_get_subvol_rootref(file, argp); | |
23d0b79d TM |
5982 | case BTRFS_IOC_INO_LOOKUP_USER: |
5983 | return btrfs_ioctl_ino_lookup_user(file, argp); | |
f46b5a66 CH |
5984 | } |
5985 | ||
5986 | return -ENOTTY; | |
5987 | } | |
4c63c245 LD |
5988 | |
5989 | #ifdef CONFIG_COMPAT | |
5990 | long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
5991 | { | |
2a362249 JM |
5992 | /* |
5993 | * These all access 32-bit values anyway so no further | |
5994 | * handling is necessary. | |
5995 | */ | |
4c63c245 LD |
5996 | switch (cmd) { |
5997 | case FS_IOC32_GETFLAGS: | |
5998 | cmd = FS_IOC_GETFLAGS; | |
5999 | break; | |
6000 | case FS_IOC32_SETFLAGS: | |
6001 | cmd = FS_IOC_SETFLAGS; | |
6002 | break; | |
6003 | case FS_IOC32_GETVERSION: | |
6004 | cmd = FS_IOC_GETVERSION; | |
6005 | break; | |
4c63c245 LD |
6006 | } |
6007 | ||
6008 | return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); | |
6009 | } | |
6010 | #endif |