]>
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" | |
949964c9 | 31 | #include "export.h" |
f46b5a66 CH |
32 | #include "transaction.h" |
33 | #include "btrfs_inode.h" | |
f46b5a66 CH |
34 | #include "print-tree.h" |
35 | #include "volumes.h" | |
925baedd | 36 | #include "locking.h" |
581bb050 | 37 | #include "inode-map.h" |
d7728c96 | 38 | #include "backref.h" |
606686ee | 39 | #include "rcu-string.h" |
31db9f7c | 40 | #include "send.h" |
3f6bcfbd | 41 | #include "dev-replace.h" |
63541927 | 42 | #include "props.h" |
3b02a68a | 43 | #include "sysfs.h" |
fcebe456 | 44 | #include "qgroup.h" |
1ec9a1ae | 45 | #include "tree-log.h" |
ebb8765b | 46 | #include "compression.h" |
8719aaae | 47 | #include "space-info.h" |
86736342 | 48 | #include "delalloc-space.h" |
aac0023c | 49 | #include "block-group.h" |
f46b5a66 | 50 | |
abccd00f HM |
51 | #ifdef CONFIG_64BIT |
52 | /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI | |
53 | * structures are incorrect, as the timespec structure from userspace | |
54 | * is 4 bytes too small. We define these alternatives here to teach | |
55 | * the kernel about the 32-bit struct packing. | |
56 | */ | |
57 | struct btrfs_ioctl_timespec_32 { | |
58 | __u64 sec; | |
59 | __u32 nsec; | |
60 | } __attribute__ ((__packed__)); | |
61 | ||
62 | struct btrfs_ioctl_received_subvol_args_32 { | |
63 | char uuid[BTRFS_UUID_SIZE]; /* in */ | |
64 | __u64 stransid; /* in */ | |
65 | __u64 rtransid; /* out */ | |
66 | struct btrfs_ioctl_timespec_32 stime; /* in */ | |
67 | struct btrfs_ioctl_timespec_32 rtime; /* out */ | |
68 | __u64 flags; /* in */ | |
69 | __u64 reserved[16]; /* in */ | |
70 | } __attribute__ ((__packed__)); | |
71 | ||
72 | #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ | |
73 | struct btrfs_ioctl_received_subvol_args_32) | |
74 | #endif | |
75 | ||
2351f431 JB |
76 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) |
77 | struct btrfs_ioctl_send_args_32 { | |
78 | __s64 send_fd; /* in */ | |
79 | __u64 clone_sources_count; /* in */ | |
80 | compat_uptr_t clone_sources; /* in */ | |
81 | __u64 parent_root; /* in */ | |
82 | __u64 flags; /* in */ | |
83 | __u64 reserved[4]; /* in */ | |
84 | } __attribute__ ((__packed__)); | |
85 | ||
86 | #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \ | |
87 | struct btrfs_ioctl_send_args_32) | |
88 | #endif | |
abccd00f | 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; |
5aca2842 | 190 | unsigned int fsflags, old_fsflags; |
6cbff00f | 191 | int ret; |
ff9fef55 | 192 | const char *comp = NULL; |
d2b8fcfe | 193 | u32 binode_flags = binode->flags; |
6cbff00f | 194 | |
bd60ea0f DS |
195 | if (!inode_owner_or_capable(inode)) |
196 | return -EPERM; | |
197 | ||
b83cc969 LZ |
198 | if (btrfs_root_readonly(root)) |
199 | return -EROFS; | |
200 | ||
5c57b8b6 | 201 | if (copy_from_user(&fsflags, arg, sizeof(fsflags))) |
6cbff00f CH |
202 | return -EFAULT; |
203 | ||
5c57b8b6 | 204 | ret = check_fsflags(fsflags); |
75e7cb7f LB |
205 | if (ret) |
206 | return ret; | |
f46b5a66 | 207 | |
e7848683 JK |
208 | ret = mnt_want_write_file(file); |
209 | if (ret) | |
210 | return ret; | |
211 | ||
5955102c | 212 | inode_lock(inode); |
6cbff00f | 213 | |
5c57b8b6 | 214 | fsflags = btrfs_mask_fsflags_for_type(inode, fsflags); |
5aca2842 DW |
215 | old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags); |
216 | ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags); | |
217 | if (ret) | |
218 | goto out_unlock; | |
6cbff00f | 219 | |
5c57b8b6 | 220 | if (fsflags & FS_SYNC_FL) |
d2b8fcfe | 221 | binode_flags |= BTRFS_INODE_SYNC; |
6cbff00f | 222 | else |
d2b8fcfe | 223 | binode_flags &= ~BTRFS_INODE_SYNC; |
5c57b8b6 | 224 | if (fsflags & FS_IMMUTABLE_FL) |
d2b8fcfe | 225 | binode_flags |= BTRFS_INODE_IMMUTABLE; |
6cbff00f | 226 | else |
d2b8fcfe | 227 | binode_flags &= ~BTRFS_INODE_IMMUTABLE; |
5c57b8b6 | 228 | if (fsflags & FS_APPEND_FL) |
d2b8fcfe | 229 | binode_flags |= BTRFS_INODE_APPEND; |
6cbff00f | 230 | else |
d2b8fcfe | 231 | binode_flags &= ~BTRFS_INODE_APPEND; |
5c57b8b6 | 232 | if (fsflags & FS_NODUMP_FL) |
d2b8fcfe | 233 | binode_flags |= BTRFS_INODE_NODUMP; |
6cbff00f | 234 | else |
d2b8fcfe | 235 | binode_flags &= ~BTRFS_INODE_NODUMP; |
5c57b8b6 | 236 | if (fsflags & FS_NOATIME_FL) |
d2b8fcfe | 237 | binode_flags |= BTRFS_INODE_NOATIME; |
6cbff00f | 238 | else |
d2b8fcfe | 239 | binode_flags &= ~BTRFS_INODE_NOATIME; |
5c57b8b6 | 240 | if (fsflags & FS_DIRSYNC_FL) |
d2b8fcfe | 241 | binode_flags |= BTRFS_INODE_DIRSYNC; |
6cbff00f | 242 | else |
d2b8fcfe | 243 | binode_flags &= ~BTRFS_INODE_DIRSYNC; |
5c57b8b6 | 244 | if (fsflags & FS_NOCOW_FL) { |
44e5194b | 245 | if (S_ISREG(inode->i_mode)) { |
7e97b8da DS |
246 | /* |
247 | * It's safe to turn csums off here, no extents exist. | |
248 | * Otherwise we want the flag to reflect the real COW | |
249 | * status of the file and will not set it. | |
250 | */ | |
251 | if (inode->i_size == 0) | |
d2b8fcfe AJ |
252 | binode_flags |= BTRFS_INODE_NODATACOW | |
253 | BTRFS_INODE_NODATASUM; | |
7e97b8da | 254 | } else { |
d2b8fcfe | 255 | binode_flags |= BTRFS_INODE_NODATACOW; |
7e97b8da DS |
256 | } |
257 | } else { | |
258 | /* | |
01327610 | 259 | * Revert back under same assumptions as above |
7e97b8da | 260 | */ |
44e5194b | 261 | if (S_ISREG(inode->i_mode)) { |
7e97b8da | 262 | if (inode->i_size == 0) |
d2b8fcfe AJ |
263 | binode_flags &= ~(BTRFS_INODE_NODATACOW | |
264 | BTRFS_INODE_NODATASUM); | |
7e97b8da | 265 | } else { |
d2b8fcfe | 266 | binode_flags &= ~BTRFS_INODE_NODATACOW; |
7e97b8da DS |
267 | } |
268 | } | |
6cbff00f | 269 | |
75e7cb7f LB |
270 | /* |
271 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS | |
272 | * flag may be changed automatically if compression code won't make | |
273 | * things smaller. | |
274 | */ | |
5c57b8b6 | 275 | if (fsflags & FS_NOCOMP_FL) { |
d2b8fcfe AJ |
276 | binode_flags &= ~BTRFS_INODE_COMPRESS; |
277 | binode_flags |= BTRFS_INODE_NOCOMPRESS; | |
5c57b8b6 | 278 | } else if (fsflags & FS_COMPR_FL) { |
63541927 | 279 | |
eede2bf3 OS |
280 | if (IS_SWAPFILE(inode)) { |
281 | ret = -ETXTBSY; | |
282 | goto out_unlock; | |
283 | } | |
284 | ||
d2b8fcfe AJ |
285 | binode_flags |= BTRFS_INODE_COMPRESS; |
286 | binode_flags &= ~BTRFS_INODE_NOCOMPRESS; | |
63541927 | 287 | |
93370509 DS |
288 | comp = btrfs_compress_type2str(fs_info->compress_type); |
289 | if (!comp || comp[0] == 0) | |
290 | comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); | |
ebcb904d | 291 | } else { |
d2b8fcfe | 292 | binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); |
75e7cb7f | 293 | } |
6cbff00f | 294 | |
ff9fef55 AJ |
295 | /* |
296 | * 1 for inode item | |
297 | * 2 for properties | |
298 | */ | |
299 | trans = btrfs_start_transaction(root, 3); | |
f062abf0 LZ |
300 | if (IS_ERR(trans)) { |
301 | ret = PTR_ERR(trans); | |
d2b8fcfe | 302 | goto out_unlock; |
f062abf0 | 303 | } |
6cbff00f | 304 | |
ff9fef55 AJ |
305 | if (comp) { |
306 | ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp, | |
307 | strlen(comp), 0); | |
308 | if (ret) { | |
309 | btrfs_abort_transaction(trans, ret); | |
310 | goto out_end_trans; | |
311 | } | |
ff9fef55 AJ |
312 | } else { |
313 | ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL, | |
314 | 0, 0); | |
315 | if (ret && ret != -ENODATA) { | |
316 | btrfs_abort_transaction(trans, ret); | |
317 | goto out_end_trans; | |
318 | } | |
319 | } | |
320 | ||
d2b8fcfe | 321 | binode->flags = binode_flags; |
7b6a221e | 322 | btrfs_sync_inode_flags_to_i_flags(inode); |
0c4d2d95 | 323 | inode_inc_iversion(inode); |
c2050a45 | 324 | inode->i_ctime = current_time(inode); |
6cbff00f | 325 | ret = btrfs_update_inode(trans, root, inode); |
6cbff00f | 326 | |
ff9fef55 | 327 | out_end_trans: |
3a45bb20 | 328 | btrfs_end_transaction(trans); |
6cbff00f | 329 | out_unlock: |
5955102c | 330 | inode_unlock(inode); |
e7848683 | 331 | mnt_drop_write_file(file); |
2d4e6f6a | 332 | return ret; |
6cbff00f CH |
333 | } |
334 | ||
19f93b3c DS |
335 | /* |
336 | * Translate btrfs internal inode flags to xflags as expected by the | |
337 | * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are | |
338 | * silently dropped. | |
339 | */ | |
340 | static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags) | |
341 | { | |
342 | unsigned int xflags = 0; | |
343 | ||
344 | if (flags & BTRFS_INODE_APPEND) | |
345 | xflags |= FS_XFLAG_APPEND; | |
346 | if (flags & BTRFS_INODE_IMMUTABLE) | |
347 | xflags |= FS_XFLAG_IMMUTABLE; | |
348 | if (flags & BTRFS_INODE_NOATIME) | |
349 | xflags |= FS_XFLAG_NOATIME; | |
350 | if (flags & BTRFS_INODE_NODUMP) | |
351 | xflags |= FS_XFLAG_NODUMP; | |
352 | if (flags & BTRFS_INODE_SYNC) | |
353 | xflags |= FS_XFLAG_SYNC; | |
354 | ||
355 | return xflags; | |
356 | } | |
357 | ||
358 | /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */ | |
359 | static int check_xflags(unsigned int flags) | |
360 | { | |
361 | if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME | | |
362 | FS_XFLAG_NODUMP | FS_XFLAG_SYNC)) | |
363 | return -EOPNOTSUPP; | |
364 | return 0; | |
365 | } | |
366 | ||
e4202ac9 DS |
367 | /* |
368 | * Set the xflags from the internal inode flags. The remaining items of fsxattr | |
369 | * are zeroed. | |
370 | */ | |
371 | static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg) | |
372 | { | |
373 | struct btrfs_inode *binode = BTRFS_I(file_inode(file)); | |
374 | struct fsxattr fa; | |
375 | ||
7b0e492e | 376 | simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags)); |
e4202ac9 DS |
377 | if (copy_to_user(arg, &fa, sizeof(fa))) |
378 | return -EFAULT; | |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
025f2121 DS |
383 | static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg) |
384 | { | |
385 | struct inode *inode = file_inode(file); | |
386 | struct btrfs_inode *binode = BTRFS_I(inode); | |
387 | struct btrfs_root *root = binode->root; | |
388 | struct btrfs_trans_handle *trans; | |
7b0e492e | 389 | struct fsxattr fa, old_fa; |
025f2121 DS |
390 | unsigned old_flags; |
391 | unsigned old_i_flags; | |
392 | int ret = 0; | |
393 | ||
394 | if (!inode_owner_or_capable(inode)) | |
395 | return -EPERM; | |
396 | ||
397 | if (btrfs_root_readonly(root)) | |
398 | return -EROFS; | |
399 | ||
025f2121 DS |
400 | if (copy_from_user(&fa, arg, sizeof(fa))) |
401 | return -EFAULT; | |
402 | ||
403 | ret = check_xflags(fa.fsx_xflags); | |
404 | if (ret) | |
405 | return ret; | |
406 | ||
407 | if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0) | |
408 | return -EOPNOTSUPP; | |
409 | ||
410 | ret = mnt_want_write_file(file); | |
411 | if (ret) | |
412 | return ret; | |
413 | ||
414 | inode_lock(inode); | |
415 | ||
416 | old_flags = binode->flags; | |
417 | old_i_flags = inode->i_flags; | |
418 | ||
7b0e492e DW |
419 | simple_fill_fsxattr(&old_fa, |
420 | btrfs_inode_flags_to_xflags(binode->flags)); | |
421 | ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa); | |
422 | if (ret) | |
025f2121 | 423 | goto out_unlock; |
025f2121 DS |
424 | |
425 | if (fa.fsx_xflags & FS_XFLAG_SYNC) | |
426 | binode->flags |= BTRFS_INODE_SYNC; | |
427 | else | |
428 | binode->flags &= ~BTRFS_INODE_SYNC; | |
429 | if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE) | |
430 | binode->flags |= BTRFS_INODE_IMMUTABLE; | |
431 | else | |
432 | binode->flags &= ~BTRFS_INODE_IMMUTABLE; | |
433 | if (fa.fsx_xflags & FS_XFLAG_APPEND) | |
434 | binode->flags |= BTRFS_INODE_APPEND; | |
435 | else | |
436 | binode->flags &= ~BTRFS_INODE_APPEND; | |
437 | if (fa.fsx_xflags & FS_XFLAG_NODUMP) | |
438 | binode->flags |= BTRFS_INODE_NODUMP; | |
439 | else | |
440 | binode->flags &= ~BTRFS_INODE_NODUMP; | |
441 | if (fa.fsx_xflags & FS_XFLAG_NOATIME) | |
442 | binode->flags |= BTRFS_INODE_NOATIME; | |
443 | else | |
444 | binode->flags &= ~BTRFS_INODE_NOATIME; | |
445 | ||
446 | /* 1 item for the inode */ | |
447 | trans = btrfs_start_transaction(root, 1); | |
448 | if (IS_ERR(trans)) { | |
449 | ret = PTR_ERR(trans); | |
450 | goto out_unlock; | |
451 | } | |
452 | ||
453 | btrfs_sync_inode_flags_to_i_flags(inode); | |
454 | inode_inc_iversion(inode); | |
455 | inode->i_ctime = current_time(inode); | |
456 | ret = btrfs_update_inode(trans, root, inode); | |
457 | ||
458 | btrfs_end_transaction(trans); | |
459 | ||
460 | out_unlock: | |
461 | if (ret) { | |
462 | binode->flags = old_flags; | |
463 | inode->i_flags = old_i_flags; | |
464 | } | |
465 | ||
466 | inode_unlock(inode); | |
467 | mnt_drop_write_file(file); | |
468 | ||
469 | return ret; | |
470 | } | |
471 | ||
6cbff00f CH |
472 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) |
473 | { | |
496ad9aa | 474 | struct inode *inode = file_inode(file); |
6cbff00f CH |
475 | |
476 | return put_user(inode->i_generation, arg); | |
477 | } | |
f46b5a66 | 478 | |
b929c1d8 MPS |
479 | static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info, |
480 | void __user *arg) | |
f7039b1d | 481 | { |
f7039b1d LD |
482 | struct btrfs_device *device; |
483 | struct request_queue *q; | |
484 | struct fstrim_range range; | |
485 | u64 minlen = ULLONG_MAX; | |
486 | u64 num_devices = 0; | |
487 | int ret; | |
488 | ||
489 | if (!capable(CAP_SYS_ADMIN)) | |
490 | return -EPERM; | |
491 | ||
f35f06c3 FM |
492 | /* |
493 | * If the fs is mounted with nologreplay, which requires it to be | |
494 | * mounted in RO mode as well, we can not allow discard on free space | |
495 | * inside block groups, because log trees refer to extents that are not | |
496 | * pinned in a block group's free space cache (pinning the extents is | |
497 | * precisely the first phase of replaying a log tree). | |
498 | */ | |
499 | if (btrfs_test_opt(fs_info, NOLOGREPLAY)) | |
500 | return -EROFS; | |
501 | ||
1f78160c XG |
502 | rcu_read_lock(); |
503 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, | |
504 | dev_list) { | |
f7039b1d LD |
505 | if (!device->bdev) |
506 | continue; | |
507 | q = bdev_get_queue(device->bdev); | |
508 | if (blk_queue_discard(q)) { | |
509 | num_devices++; | |
50d0446e | 510 | minlen = min_t(u64, q->limits.discard_granularity, |
f7039b1d LD |
511 | minlen); |
512 | } | |
513 | } | |
1f78160c | 514 | rcu_read_unlock(); |
f4c697e6 | 515 | |
f7039b1d LD |
516 | if (!num_devices) |
517 | return -EOPNOTSUPP; | |
f7039b1d LD |
518 | if (copy_from_user(&range, arg, sizeof(range))) |
519 | return -EFAULT; | |
6ba9fc8e QW |
520 | |
521 | /* | |
522 | * NOTE: Don't truncate the range using super->total_bytes. Bytenr of | |
523 | * block group is in the logical address space, which can be any | |
524 | * sectorsize aligned bytenr in the range [0, U64_MAX]. | |
525 | */ | |
526 | if (range.len < fs_info->sb->s_blocksize) | |
f4c697e6 | 527 | return -EINVAL; |
f7039b1d LD |
528 | |
529 | range.minlen = max(range.minlen, minlen); | |
2ff7e61e | 530 | ret = btrfs_trim_fs(fs_info, &range); |
f7039b1d LD |
531 | if (ret < 0) |
532 | return ret; | |
533 | ||
534 | if (copy_to_user(arg, &range, sizeof(range))) | |
535 | return -EFAULT; | |
536 | ||
537 | return 0; | |
538 | } | |
539 | ||
e1f60a65 | 540 | int __pure btrfs_is_empty_uuid(u8 *uuid) |
dd5f9615 | 541 | { |
46e0f66a CM |
542 | int i; |
543 | ||
544 | for (i = 0; i < BTRFS_UUID_SIZE; i++) { | |
545 | if (uuid[i]) | |
546 | return 0; | |
547 | } | |
548 | return 1; | |
dd5f9615 SB |
549 | } |
550 | ||
d5c12070 | 551 | static noinline int create_subvol(struct inode *dir, |
cb8e7090 | 552 | struct dentry *dentry, |
52f75f4f | 553 | const char *name, int namelen, |
8696c533 | 554 | struct btrfs_qgroup_inherit *inherit) |
f46b5a66 | 555 | { |
0b246afa | 556 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
f46b5a66 CH |
557 | struct btrfs_trans_handle *trans; |
558 | struct btrfs_key key; | |
49a3c4d9 | 559 | struct btrfs_root_item *root_item; |
f46b5a66 CH |
560 | struct btrfs_inode_item *inode_item; |
561 | struct extent_buffer *leaf; | |
d5c12070 | 562 | struct btrfs_root *root = BTRFS_I(dir)->root; |
76dda93c | 563 | struct btrfs_root *new_root; |
d5c12070 | 564 | struct btrfs_block_rsv block_rsv; |
95582b00 | 565 | struct timespec64 cur_time = current_time(dir); |
5662344b | 566 | struct inode *inode; |
f46b5a66 CH |
567 | int ret; |
568 | int err; | |
2dfb1e43 | 569 | dev_t anon_dev = 0; |
f46b5a66 CH |
570 | u64 objectid; |
571 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | |
3de4586c | 572 | u64 index = 0; |
f46b5a66 | 573 | |
49a3c4d9 DS |
574 | root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); |
575 | if (!root_item) | |
576 | return -ENOMEM; | |
577 | ||
0b246afa | 578 | ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid); |
2fbe8c8a | 579 | if (ret) |
49a3c4d9 | 580 | goto fail_free; |
6a912213 | 581 | |
2dfb1e43 QW |
582 | ret = get_anon_bdev(&anon_dev); |
583 | if (ret < 0) | |
584 | goto fail_free; | |
585 | ||
e09fe2d2 QW |
586 | /* |
587 | * Don't create subvolume whose level is not zero. Or qgroup will be | |
01327610 | 588 | * screwed up since it assumes subvolume qgroup's level to be 0. |
e09fe2d2 | 589 | */ |
49a3c4d9 DS |
590 | if (btrfs_qgroup_level(objectid)) { |
591 | ret = -ENOSPC; | |
592 | goto fail_free; | |
593 | } | |
e09fe2d2 | 594 | |
d5c12070 | 595 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
9ed74f2d | 596 | /* |
d5c12070 MX |
597 | * The same as the snapshot creation, please see the comment |
598 | * of create_snapshot(). | |
9ed74f2d | 599 | */ |
c4c129db | 600 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false); |
d5c12070 | 601 | if (ret) |
49a3c4d9 | 602 | goto fail_free; |
d5c12070 MX |
603 | |
604 | trans = btrfs_start_transaction(root, 0); | |
605 | if (IS_ERR(trans)) { | |
606 | ret = PTR_ERR(trans); | |
7775c818 | 607 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); |
49a3c4d9 | 608 | goto fail_free; |
d5c12070 MX |
609 | } |
610 | trans->block_rsv = &block_rsv; | |
611 | trans->bytes_reserved = block_rsv.size; | |
f46b5a66 | 612 | |
a9377422 | 613 | ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit); |
6f72c7e2 AJ |
614 | if (ret) |
615 | goto fail; | |
616 | ||
4d75f8a9 | 617 | leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); |
8e8a1e31 JB |
618 | if (IS_ERR(leaf)) { |
619 | ret = PTR_ERR(leaf); | |
620 | goto fail; | |
621 | } | |
f46b5a66 | 622 | |
f46b5a66 CH |
623 | btrfs_mark_buffer_dirty(leaf); |
624 | ||
49a3c4d9 | 625 | inode_item = &root_item->inode; |
3cae210f QW |
626 | btrfs_set_stack_inode_generation(inode_item, 1); |
627 | btrfs_set_stack_inode_size(inode_item, 3); | |
628 | btrfs_set_stack_inode_nlink(inode_item, 1); | |
da17066c | 629 | btrfs_set_stack_inode_nbytes(inode_item, |
0b246afa | 630 | fs_info->nodesize); |
3cae210f | 631 | btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); |
f46b5a66 | 632 | |
49a3c4d9 DS |
633 | btrfs_set_root_flags(root_item, 0); |
634 | btrfs_set_root_limit(root_item, 0); | |
3cae210f | 635 | btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); |
08fe4db1 | 636 | |
49a3c4d9 DS |
637 | btrfs_set_root_bytenr(root_item, leaf->start); |
638 | btrfs_set_root_generation(root_item, trans->transid); | |
639 | btrfs_set_root_level(root_item, 0); | |
640 | btrfs_set_root_refs(root_item, 1); | |
641 | btrfs_set_root_used(root_item, leaf->len); | |
642 | btrfs_set_root_last_snapshot(root_item, 0); | |
f46b5a66 | 643 | |
49a3c4d9 DS |
644 | btrfs_set_root_generation_v2(root_item, |
645 | btrfs_root_generation(root_item)); | |
807fc790 | 646 | generate_random_guid(root_item->uuid); |
49a3c4d9 DS |
647 | btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); |
648 | btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); | |
649 | root_item->ctime = root_item->otime; | |
650 | btrfs_set_root_ctransid(root_item, trans->transid); | |
651 | btrfs_set_root_otransid(root_item, trans->transid); | |
f46b5a66 | 652 | |
925baedd | 653 | btrfs_tree_unlock(leaf); |
f46b5a66 CH |
654 | free_extent_buffer(leaf); |
655 | leaf = NULL; | |
656 | ||
49a3c4d9 | 657 | btrfs_set_root_dirid(root_item, new_dirid); |
f46b5a66 CH |
658 | |
659 | key.objectid = objectid; | |
5d4f98a2 | 660 | key.offset = 0; |
962a298f | 661 | key.type = BTRFS_ROOT_ITEM_KEY; |
0b246afa | 662 | ret = btrfs_insert_root(trans, fs_info->tree_root, &key, |
49a3c4d9 | 663 | root_item); |
f46b5a66 CH |
664 | if (ret) |
665 | goto fail; | |
666 | ||
76dda93c | 667 | key.offset = (u64)-1; |
2dfb1e43 | 668 | new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev); |
79787eaa | 669 | if (IS_ERR(new_root)) { |
2dfb1e43 | 670 | free_anon_bdev(anon_dev); |
79787eaa | 671 | ret = PTR_ERR(new_root); |
66642832 | 672 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
673 | goto fail; |
674 | } | |
2dfb1e43 QW |
675 | /* Freeing will be done in btrfs_put_root() of new_root */ |
676 | anon_dev = 0; | |
76dda93c YZ |
677 | |
678 | btrfs_record_root_in_trans(trans, new_root); | |
679 | ||
63541927 | 680 | ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); |
00246528 | 681 | btrfs_put_root(new_root); |
ce598979 MF |
682 | if (ret) { |
683 | /* We potentially lose an unused inode item here */ | |
66642832 | 684 | btrfs_abort_transaction(trans, ret); |
ce598979 MF |
685 | goto fail; |
686 | } | |
687 | ||
f32e48e9 CR |
688 | mutex_lock(&new_root->objectid_mutex); |
689 | new_root->highest_objectid = new_dirid; | |
690 | mutex_unlock(&new_root->objectid_mutex); | |
691 | ||
f46b5a66 CH |
692 | /* |
693 | * insert the directory item | |
694 | */ | |
877574e2 | 695 | ret = btrfs_set_inode_index(BTRFS_I(dir), &index); |
79787eaa | 696 | if (ret) { |
66642832 | 697 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
698 | goto fail; |
699 | } | |
3de4586c | 700 | |
684572df | 701 | ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, |
3de4586c | 702 | BTRFS_FT_DIR, index); |
79787eaa | 703 | if (ret) { |
66642832 | 704 | btrfs_abort_transaction(trans, ret); |
f46b5a66 | 705 | goto fail; |
79787eaa | 706 | } |
0660b5af | 707 | |
6ef06d27 | 708 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); |
52c26179 | 709 | ret = btrfs_update_inode(trans, root, dir); |
c7e54b51 JB |
710 | if (ret) { |
711 | btrfs_abort_transaction(trans, ret); | |
712 | goto fail; | |
713 | } | |
52c26179 | 714 | |
6025c19f | 715 | ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, |
4a0cc7ca | 716 | btrfs_ino(BTRFS_I(dir)), index, name, namelen); |
c7e54b51 JB |
717 | if (ret) { |
718 | btrfs_abort_transaction(trans, ret); | |
719 | goto fail; | |
720 | } | |
f46b5a66 | 721 | |
cdb345a8 | 722 | ret = btrfs_uuid_tree_add(trans, root_item->uuid, |
6bccf3ab | 723 | BTRFS_UUID_KEY_SUBVOL, objectid); |
dd5f9615 | 724 | if (ret) |
66642832 | 725 | btrfs_abort_transaction(trans, ret); |
dd5f9615 | 726 | |
f46b5a66 | 727 | fail: |
49a3c4d9 | 728 | kfree(root_item); |
d5c12070 MX |
729 | trans->block_rsv = NULL; |
730 | trans->bytes_reserved = 0; | |
7775c818 | 731 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); |
de6e8200 | 732 | |
9babda9f | 733 | err = btrfs_commit_transaction(trans); |
f46b5a66 CH |
734 | if (err && !ret) |
735 | ret = err; | |
1a65e24b | 736 | |
5662344b TI |
737 | if (!ret) { |
738 | inode = btrfs_lookup_dentry(dir, dentry); | |
de6e8200 LB |
739 | if (IS_ERR(inode)) |
740 | return PTR_ERR(inode); | |
5662344b TI |
741 | d_instantiate(dentry, inode); |
742 | } | |
f46b5a66 | 743 | return ret; |
49a3c4d9 DS |
744 | |
745 | fail_free: | |
2dfb1e43 QW |
746 | if (anon_dev) |
747 | free_anon_bdev(anon_dev); | |
49a3c4d9 DS |
748 | kfree(root_item); |
749 | return ret; | |
f46b5a66 CH |
750 | } |
751 | ||
e9662f70 | 752 | static int create_snapshot(struct btrfs_root *root, struct inode *dir, |
9babda9f | 753 | struct dentry *dentry, bool readonly, |
e9662f70 | 754 | struct btrfs_qgroup_inherit *inherit) |
f46b5a66 | 755 | { |
0b246afa | 756 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
2e4bfab9 | 757 | struct inode *inode; |
f46b5a66 CH |
758 | struct btrfs_pending_snapshot *pending_snapshot; |
759 | struct btrfs_trans_handle *trans; | |
2e4bfab9 | 760 | int ret; |
f46b5a66 | 761 | |
92a7cc42 | 762 | if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
f46b5a66 CH |
763 | return -EINVAL; |
764 | ||
eede2bf3 OS |
765 | if (atomic_read(&root->nr_swapfiles)) { |
766 | btrfs_warn(fs_info, | |
767 | "cannot snapshot subvolume with active swapfile"); | |
768 | return -ETXTBSY; | |
769 | } | |
770 | ||
23269bf5 | 771 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); |
a1ee7362 DS |
772 | if (!pending_snapshot) |
773 | return -ENOMEM; | |
774 | ||
2dfb1e43 QW |
775 | ret = get_anon_bdev(&pending_snapshot->anon_dev); |
776 | if (ret < 0) | |
777 | goto free_pending; | |
b0c0ea63 | 778 | pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), |
23269bf5 | 779 | GFP_KERNEL); |
8546b570 DS |
780 | pending_snapshot->path = btrfs_alloc_path(); |
781 | if (!pending_snapshot->root_item || !pending_snapshot->path) { | |
b0c0ea63 DS |
782 | ret = -ENOMEM; |
783 | goto free_pending; | |
784 | } | |
785 | ||
66d8f3dd MX |
786 | btrfs_init_block_rsv(&pending_snapshot->block_rsv, |
787 | BTRFS_BLOCK_RSV_TEMP); | |
d5c12070 MX |
788 | /* |
789 | * 1 - parent dir inode | |
790 | * 2 - dir entries | |
791 | * 1 - root item | |
792 | * 2 - root ref/backref | |
793 | * 1 - root of snapshot | |
dd5f9615 | 794 | * 1 - UUID item |
d5c12070 MX |
795 | */ |
796 | ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, | |
dd5f9615 | 797 | &pending_snapshot->block_rsv, 8, |
ee3441b4 | 798 | false); |
d5c12070 | 799 | if (ret) |
c11fbb6e | 800 | goto free_pending; |
d5c12070 | 801 | |
3de4586c | 802 | pending_snapshot->dentry = dentry; |
f46b5a66 | 803 | pending_snapshot->root = root; |
b83cc969 | 804 | pending_snapshot->readonly = readonly; |
e9662f70 | 805 | pending_snapshot->dir = dir; |
8696c533 | 806 | pending_snapshot->inherit = inherit; |
a22285a6 | 807 | |
d5c12070 | 808 | trans = btrfs_start_transaction(root, 0); |
a22285a6 YZ |
809 | if (IS_ERR(trans)) { |
810 | ret = PTR_ERR(trans); | |
811 | goto fail; | |
812 | } | |
813 | ||
0b246afa | 814 | spin_lock(&fs_info->trans_lock); |
f46b5a66 CH |
815 | list_add(&pending_snapshot->list, |
816 | &trans->transaction->pending_snapshots); | |
0b246afa | 817 | spin_unlock(&fs_info->trans_lock); |
9babda9f NB |
818 | |
819 | ret = btrfs_commit_transaction(trans); | |
aec8030a | 820 | if (ret) |
c37b2b62 | 821 | goto fail; |
a22285a6 YZ |
822 | |
823 | ret = pending_snapshot->error; | |
824 | if (ret) | |
825 | goto fail; | |
826 | ||
d3797308 CM |
827 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
828 | if (ret) | |
829 | goto fail; | |
830 | ||
2b0143b5 | 831 | inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); |
2e4bfab9 YZ |
832 | if (IS_ERR(inode)) { |
833 | ret = PTR_ERR(inode); | |
834 | goto fail; | |
835 | } | |
5662344b | 836 | |
2e4bfab9 YZ |
837 | d_instantiate(dentry, inode); |
838 | ret = 0; | |
2dfb1e43 | 839 | pending_snapshot->anon_dev = 0; |
2e4bfab9 | 840 | fail: |
2dfb1e43 QW |
841 | /* Prevent double freeing of anon_dev */ |
842 | if (ret && pending_snapshot->snap) | |
843 | pending_snapshot->snap->anon_dev = 0; | |
00246528 | 844 | btrfs_put_root(pending_snapshot->snap); |
7775c818 | 845 | btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv); |
b0c0ea63 | 846 | free_pending: |
2dfb1e43 QW |
847 | if (pending_snapshot->anon_dev) |
848 | free_anon_bdev(pending_snapshot->anon_dev); | |
b0c0ea63 | 849 | kfree(pending_snapshot->root_item); |
8546b570 | 850 | btrfs_free_path(pending_snapshot->path); |
a1ee7362 DS |
851 | kfree(pending_snapshot); |
852 | ||
f46b5a66 CH |
853 | return ret; |
854 | } | |
855 | ||
4260f7c7 SW |
856 | /* copy of may_delete in fs/namei.c() |
857 | * Check whether we can remove a link victim from directory dir, check | |
858 | * whether the type of victim is right. | |
859 | * 1. We can't do it if dir is read-only (done in permission()) | |
860 | * 2. We should have write and exec permissions on dir | |
861 | * 3. We can't remove anything from append-only dir | |
862 | * 4. We can't do anything with immutable dir (done in permission()) | |
863 | * 5. If the sticky bit on dir is set we should either | |
864 | * a. be owner of dir, or | |
865 | * b. be owner of victim, or | |
866 | * c. have CAP_FOWNER capability | |
01327610 | 867 | * 6. If the victim is append-only or immutable we can't do anything with |
4260f7c7 SW |
868 | * links pointing to it. |
869 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
870 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
871 | * 9. We can't remove a root or mountpoint. | |
872 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by | |
873 | * nfs_async_unlink(). | |
874 | */ | |
875 | ||
67871254 | 876 | static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) |
4260f7c7 SW |
877 | { |
878 | int error; | |
879 | ||
2b0143b5 | 880 | if (d_really_is_negative(victim)) |
4260f7c7 SW |
881 | return -ENOENT; |
882 | ||
2b0143b5 | 883 | BUG_ON(d_inode(victim->d_parent) != dir); |
4fa6b5ec | 884 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
4260f7c7 SW |
885 | |
886 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
887 | if (error) | |
888 | return error; | |
889 | if (IS_APPEND(dir)) | |
890 | return -EPERM; | |
2b0143b5 DH |
891 | if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) || |
892 | IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim))) | |
4260f7c7 SW |
893 | return -EPERM; |
894 | if (isdir) { | |
e36cb0b8 | 895 | if (!d_is_dir(victim)) |
4260f7c7 SW |
896 | return -ENOTDIR; |
897 | if (IS_ROOT(victim)) | |
898 | return -EBUSY; | |
e36cb0b8 | 899 | } else if (d_is_dir(victim)) |
4260f7c7 SW |
900 | return -EISDIR; |
901 | if (IS_DEADDIR(dir)) | |
902 | return -ENOENT; | |
903 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
904 | return -EBUSY; | |
905 | return 0; | |
906 | } | |
907 | ||
cb8e7090 CH |
908 | /* copy of may_create in fs/namei.c() */ |
909 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) | |
910 | { | |
2b0143b5 | 911 | if (d_really_is_positive(child)) |
cb8e7090 CH |
912 | return -EEXIST; |
913 | if (IS_DEADDIR(dir)) | |
914 | return -ENOENT; | |
915 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
916 | } | |
917 | ||
918 | /* | |
919 | * Create a new subvolume below @parent. This is largely modeled after | |
920 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup | |
921 | * inside this filesystem so it's quite a bit simpler. | |
922 | */ | |
92872094 | 923 | static noinline int btrfs_mksubvol(const struct path *parent, |
52f75f4f | 924 | const char *name, int namelen, |
72fd032e | 925 | struct btrfs_root *snap_src, |
9babda9f | 926 | bool readonly, |
8696c533 | 927 | struct btrfs_qgroup_inherit *inherit) |
cb8e7090 | 928 | { |
0b246afa JM |
929 | struct inode *dir = d_inode(parent->dentry); |
930 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); | |
cb8e7090 CH |
931 | struct dentry *dentry; |
932 | int error; | |
933 | ||
00235411 AV |
934 | error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
935 | if (error == -EINTR) | |
936 | return error; | |
cb8e7090 CH |
937 | |
938 | dentry = lookup_one_len(name, parent->dentry, namelen); | |
939 | error = PTR_ERR(dentry); | |
940 | if (IS_ERR(dentry)) | |
941 | goto out_unlock; | |
942 | ||
76dda93c | 943 | error = btrfs_may_create(dir, dentry); |
cb8e7090 | 944 | if (error) |
a874a63e | 945 | goto out_dput; |
cb8e7090 | 946 | |
9c52057c CM |
947 | /* |
948 | * even if this name doesn't exist, we may get hash collisions. | |
949 | * check for them now when we can safely fail | |
950 | */ | |
951 | error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, | |
952 | dir->i_ino, name, | |
953 | namelen); | |
954 | if (error) | |
955 | goto out_dput; | |
956 | ||
0b246afa | 957 | down_read(&fs_info->subvol_sem); |
76dda93c YZ |
958 | |
959 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) | |
960 | goto out_up_read; | |
961 | ||
9babda9f NB |
962 | if (snap_src) |
963 | error = create_snapshot(snap_src, dir, dentry, readonly, inherit); | |
964 | else | |
965 | error = create_subvol(dir, dentry, name, namelen, inherit); | |
966 | ||
76dda93c YZ |
967 | if (!error) |
968 | fsnotify_mkdir(dir, dentry); | |
969 | out_up_read: | |
0b246afa | 970 | up_read(&fs_info->subvol_sem); |
cb8e7090 CH |
971 | out_dput: |
972 | dput(dentry); | |
973 | out_unlock: | |
5955102c | 974 | inode_unlock(dir); |
cb8e7090 CH |
975 | return error; |
976 | } | |
977 | ||
c11fbb6e RK |
978 | static noinline int btrfs_mksnapshot(const struct path *parent, |
979 | const char *name, int namelen, | |
980 | struct btrfs_root *root, | |
981 | bool readonly, | |
982 | struct btrfs_qgroup_inherit *inherit) | |
983 | { | |
984 | int ret; | |
985 | bool snapshot_force_cow = false; | |
986 | ||
987 | /* | |
988 | * Force new buffered writes to reserve space even when NOCOW is | |
989 | * possible. This is to avoid later writeback (running dealloc) to | |
990 | * fallback to COW mode and unexpectedly fail with ENOSPC. | |
991 | */ | |
992 | btrfs_drew_read_lock(&root->snapshot_lock); | |
993 | ||
994 | ret = btrfs_start_delalloc_snapshot(root); | |
995 | if (ret) | |
996 | goto out; | |
997 | ||
998 | /* | |
999 | * All previous writes have started writeback in NOCOW mode, so now | |
1000 | * we force future writes to fallback to COW mode during snapshot | |
1001 | * creation. | |
1002 | */ | |
1003 | atomic_inc(&root->snapshot_force_cow); | |
1004 | snapshot_force_cow = true; | |
1005 | ||
1006 | btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); | |
1007 | ||
1008 | ret = btrfs_mksubvol(parent, name, namelen, | |
1009 | root, readonly, inherit); | |
1010 | out: | |
1011 | if (snapshot_force_cow) | |
1012 | atomic_dec(&root->snapshot_force_cow); | |
1013 | btrfs_drew_read_unlock(&root->snapshot_lock); | |
1014 | return ret; | |
1015 | } | |
1016 | ||
4cb5300b CM |
1017 | /* |
1018 | * When we're defragging a range, we don't want to kick it off again | |
1019 | * if it is really just waiting for delalloc to send it down. | |
1020 | * If we find a nice big extent or delalloc range for the bytes in the | |
1021 | * file you want to defrag, we return 0 to let you know to skip this | |
1022 | * part of the file | |
1023 | */ | |
aab110ab | 1024 | static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh) |
4cb5300b CM |
1025 | { |
1026 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
1027 | struct extent_map *em = NULL; | |
1028 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
1029 | u64 end; | |
1030 | ||
1031 | read_lock(&em_tree->lock); | |
09cbfeaf | 1032 | em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE); |
4cb5300b CM |
1033 | read_unlock(&em_tree->lock); |
1034 | ||
1035 | if (em) { | |
1036 | end = extent_map_end(em); | |
1037 | free_extent_map(em); | |
1038 | if (end - offset > thresh) | |
1039 | return 0; | |
1040 | } | |
1041 | /* if we already have a nice delalloc here, just stop */ | |
1042 | thresh /= 2; | |
1043 | end = count_range_bits(io_tree, &offset, offset + thresh, | |
1044 | thresh, EXTENT_DELALLOC, 1); | |
1045 | if (end >= thresh) | |
1046 | return 0; | |
1047 | return 1; | |
1048 | } | |
1049 | ||
1050 | /* | |
1051 | * helper function to walk through a file and find extents | |
1052 | * newer than a specific transid, and smaller than thresh. | |
1053 | * | |
1054 | * This is used by the defragging code to find new and small | |
1055 | * extents | |
1056 | */ | |
1057 | static int find_new_extents(struct btrfs_root *root, | |
1058 | struct inode *inode, u64 newer_than, | |
aab110ab | 1059 | u64 *off, u32 thresh) |
4cb5300b CM |
1060 | { |
1061 | struct btrfs_path *path; | |
1062 | struct btrfs_key min_key; | |
4cb5300b CM |
1063 | struct extent_buffer *leaf; |
1064 | struct btrfs_file_extent_item *extent; | |
1065 | int type; | |
1066 | int ret; | |
4a0cc7ca | 1067 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
4cb5300b CM |
1068 | |
1069 | path = btrfs_alloc_path(); | |
1070 | if (!path) | |
1071 | return -ENOMEM; | |
1072 | ||
a4689d2b | 1073 | min_key.objectid = ino; |
4cb5300b CM |
1074 | min_key.type = BTRFS_EXTENT_DATA_KEY; |
1075 | min_key.offset = *off; | |
1076 | ||
67871254 | 1077 | while (1) { |
6174d3cb | 1078 | ret = btrfs_search_forward(root, &min_key, path, newer_than); |
4cb5300b CM |
1079 | if (ret != 0) |
1080 | goto none; | |
f094c9bd | 1081 | process_slot: |
a4689d2b | 1082 | if (min_key.objectid != ino) |
4cb5300b CM |
1083 | goto none; |
1084 | if (min_key.type != BTRFS_EXTENT_DATA_KEY) | |
1085 | goto none; | |
1086 | ||
1087 | leaf = path->nodes[0]; | |
1088 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1089 | struct btrfs_file_extent_item); | |
1090 | ||
1091 | type = btrfs_file_extent_type(leaf, extent); | |
1092 | if (type == BTRFS_FILE_EXTENT_REG && | |
1093 | btrfs_file_extent_num_bytes(leaf, extent) < thresh && | |
1094 | check_defrag_in_cache(inode, min_key.offset, thresh)) { | |
1095 | *off = min_key.offset; | |
1096 | btrfs_free_path(path); | |
1097 | return 0; | |
1098 | } | |
1099 | ||
f094c9bd FM |
1100 | path->slots[0]++; |
1101 | if (path->slots[0] < btrfs_header_nritems(leaf)) { | |
1102 | btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); | |
1103 | goto process_slot; | |
1104 | } | |
1105 | ||
4cb5300b CM |
1106 | if (min_key.offset == (u64)-1) |
1107 | goto none; | |
1108 | ||
1109 | min_key.offset++; | |
1110 | btrfs_release_path(path); | |
1111 | } | |
1112 | none: | |
1113 | btrfs_free_path(path); | |
1114 | return -ENOENT; | |
1115 | } | |
1116 | ||
6c282eb4 | 1117 | static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) |
17ce6ef8 LB |
1118 | { |
1119 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
6c282eb4 LZ |
1120 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
1121 | struct extent_map *em; | |
09cbfeaf | 1122 | u64 len = PAGE_SIZE; |
17ce6ef8 | 1123 | |
6c282eb4 LZ |
1124 | /* |
1125 | * hopefully we have this extent in the tree already, try without | |
1126 | * the full extent lock | |
1127 | */ | |
17ce6ef8 | 1128 | read_lock(&em_tree->lock); |
6c282eb4 | 1129 | em = lookup_extent_mapping(em_tree, start, len); |
17ce6ef8 LB |
1130 | read_unlock(&em_tree->lock); |
1131 | ||
6c282eb4 | 1132 | if (!em) { |
308d9800 FM |
1133 | struct extent_state *cached = NULL; |
1134 | u64 end = start + len - 1; | |
1135 | ||
6c282eb4 | 1136 | /* get the big lock and read metadata off disk */ |
ff13db41 | 1137 | lock_extent_bits(io_tree, start, end, &cached); |
39b07b5d | 1138 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
e43bbe5e | 1139 | unlock_extent_cached(io_tree, start, end, &cached); |
6c282eb4 LZ |
1140 | |
1141 | if (IS_ERR(em)) | |
1142 | return NULL; | |
1143 | } | |
1144 | ||
1145 | return em; | |
1146 | } | |
17ce6ef8 | 1147 | |
6c282eb4 LZ |
1148 | static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) |
1149 | { | |
1150 | struct extent_map *next; | |
1151 | bool ret = true; | |
1152 | ||
1153 | /* this is the last extent */ | |
1154 | if (em->start + em->len >= i_size_read(inode)) | |
1155 | return false; | |
1156 | ||
1157 | next = defrag_lookup_extent(inode, em->start + em->len); | |
e9512d72 CM |
1158 | if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) |
1159 | ret = false; | |
1160 | else if ((em->block_start + em->block_len == next->block_start) && | |
ee22184b | 1161 | (em->block_len > SZ_128K && next->block_len > SZ_128K)) |
6c282eb4 LZ |
1162 | ret = false; |
1163 | ||
1164 | free_extent_map(next); | |
17ce6ef8 LB |
1165 | return ret; |
1166 | } | |
1167 | ||
aab110ab | 1168 | static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, |
a43a2111 AM |
1169 | u64 *last_len, u64 *skip, u64 *defrag_end, |
1170 | int compress) | |
940100a4 | 1171 | { |
6c282eb4 | 1172 | struct extent_map *em; |
940100a4 | 1173 | int ret = 1; |
6c282eb4 | 1174 | bool next_mergeable = true; |
4a3560c4 | 1175 | bool prev_mergeable = true; |
940100a4 CM |
1176 | |
1177 | /* | |
008873ea | 1178 | * make sure that once we start defragging an extent, we keep on |
940100a4 CM |
1179 | * defragging it |
1180 | */ | |
1181 | if (start < *defrag_end) | |
1182 | return 1; | |
1183 | ||
1184 | *skip = 0; | |
1185 | ||
6c282eb4 LZ |
1186 | em = defrag_lookup_extent(inode, start); |
1187 | if (!em) | |
1188 | return 0; | |
940100a4 CM |
1189 | |
1190 | /* this will cover holes, and inline extents */ | |
17ce6ef8 | 1191 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
940100a4 | 1192 | ret = 0; |
17ce6ef8 LB |
1193 | goto out; |
1194 | } | |
1195 | ||
4a3560c4 LB |
1196 | if (!*defrag_end) |
1197 | prev_mergeable = false; | |
1198 | ||
6c282eb4 | 1199 | next_mergeable = defrag_check_next_extent(inode, em); |
940100a4 | 1200 | /* |
6c282eb4 LZ |
1201 | * we hit a real extent, if it is big or the next extent is not a |
1202 | * real extent, don't bother defragging it | |
940100a4 | 1203 | */ |
a43a2111 | 1204 | if (!compress && (*last_len == 0 || *last_len >= thresh) && |
4a3560c4 | 1205 | (em->len >= thresh || (!next_mergeable && !prev_mergeable))) |
940100a4 | 1206 | ret = 0; |
17ce6ef8 | 1207 | out: |
940100a4 CM |
1208 | /* |
1209 | * last_len ends up being a counter of how many bytes we've defragged. | |
1210 | * every time we choose not to defrag an extent, we reset *last_len | |
1211 | * so that the next tiny extent will force a defrag. | |
1212 | * | |
1213 | * The end result of this is that tiny extents before a single big | |
1214 | * extent will force at least part of that big extent to be defragged. | |
1215 | */ | |
1216 | if (ret) { | |
940100a4 CM |
1217 | *defrag_end = extent_map_end(em); |
1218 | } else { | |
1219 | *last_len = 0; | |
1220 | *skip = extent_map_end(em); | |
1221 | *defrag_end = 0; | |
1222 | } | |
1223 | ||
1224 | free_extent_map(em); | |
1225 | return ret; | |
1226 | } | |
1227 | ||
4cb5300b CM |
1228 | /* |
1229 | * it doesn't do much good to defrag one or two pages | |
1230 | * at a time. This pulls in a nice chunk of pages | |
1231 | * to COW and defrag. | |
1232 | * | |
1233 | * It also makes sure the delalloc code has enough | |
1234 | * dirty data to avoid making new small extents as part | |
1235 | * of the defrag | |
1236 | * | |
1237 | * It's a good idea to start RA on this range | |
1238 | * before calling this. | |
1239 | */ | |
1240 | static int cluster_pages_for_defrag(struct inode *inode, | |
1241 | struct page **pages, | |
1242 | unsigned long start_index, | |
c41570c9 | 1243 | unsigned long num_pages) |
f46b5a66 | 1244 | { |
4cb5300b CM |
1245 | unsigned long file_end; |
1246 | u64 isize = i_size_read(inode); | |
1247 | u64 page_start; | |
1248 | u64 page_end; | |
1f12bd06 | 1249 | u64 page_cnt; |
4cb5300b CM |
1250 | int ret; |
1251 | int i; | |
1252 | int i_done; | |
3eaa2885 | 1253 | struct btrfs_ordered_extent *ordered; |
4cb5300b | 1254 | struct extent_state *cached_state = NULL; |
600a45e1 | 1255 | struct extent_io_tree *tree; |
364ecf36 | 1256 | struct extent_changeset *data_reserved = NULL; |
3b16a4e3 | 1257 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
4cb5300b | 1258 | |
09cbfeaf | 1259 | file_end = (isize - 1) >> PAGE_SHIFT; |
1f12bd06 LB |
1260 | if (!isize || start_index > file_end) |
1261 | return 0; | |
1262 | ||
1263 | page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); | |
4cb5300b | 1264 | |
e5b7231e | 1265 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
09cbfeaf KS |
1266 | start_index << PAGE_SHIFT, |
1267 | page_cnt << PAGE_SHIFT); | |
4cb5300b CM |
1268 | if (ret) |
1269 | return ret; | |
4cb5300b | 1270 | i_done = 0; |
600a45e1 | 1271 | tree = &BTRFS_I(inode)->io_tree; |
4cb5300b CM |
1272 | |
1273 | /* step one, lock all the pages */ | |
1f12bd06 | 1274 | for (i = 0; i < page_cnt; i++) { |
4cb5300b | 1275 | struct page *page; |
600a45e1 | 1276 | again: |
a94733d0 | 1277 | page = find_or_create_page(inode->i_mapping, |
600a45e1 | 1278 | start_index + i, mask); |
4cb5300b CM |
1279 | if (!page) |
1280 | break; | |
1281 | ||
600a45e1 | 1282 | page_start = page_offset(page); |
09cbfeaf | 1283 | page_end = page_start + PAGE_SIZE - 1; |
600a45e1 | 1284 | while (1) { |
308d9800 | 1285 | lock_extent_bits(tree, page_start, page_end, |
ff13db41 | 1286 | &cached_state); |
c3504372 | 1287 | ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), |
600a45e1 | 1288 | page_start); |
308d9800 | 1289 | unlock_extent_cached(tree, page_start, page_end, |
e43bbe5e | 1290 | &cached_state); |
600a45e1 MX |
1291 | if (!ordered) |
1292 | break; | |
1293 | ||
1294 | unlock_page(page); | |
1295 | btrfs_start_ordered_extent(inode, ordered, 1); | |
1296 | btrfs_put_ordered_extent(ordered); | |
1297 | lock_page(page); | |
1f12bd06 LB |
1298 | /* |
1299 | * we unlocked the page above, so we need check if | |
1300 | * it was released or not. | |
1301 | */ | |
1302 | if (page->mapping != inode->i_mapping) { | |
1303 | unlock_page(page); | |
09cbfeaf | 1304 | put_page(page); |
1f12bd06 LB |
1305 | goto again; |
1306 | } | |
600a45e1 MX |
1307 | } |
1308 | ||
4cb5300b CM |
1309 | if (!PageUptodate(page)) { |
1310 | btrfs_readpage(NULL, page); | |
1311 | lock_page(page); | |
1312 | if (!PageUptodate(page)) { | |
1313 | unlock_page(page); | |
09cbfeaf | 1314 | put_page(page); |
4cb5300b CM |
1315 | ret = -EIO; |
1316 | break; | |
1317 | } | |
1318 | } | |
600a45e1 | 1319 | |
600a45e1 MX |
1320 | if (page->mapping != inode->i_mapping) { |
1321 | unlock_page(page); | |
09cbfeaf | 1322 | put_page(page); |
600a45e1 MX |
1323 | goto again; |
1324 | } | |
1325 | ||
4cb5300b CM |
1326 | pages[i] = page; |
1327 | i_done++; | |
1328 | } | |
1329 | if (!i_done || ret) | |
1330 | goto out; | |
1331 | ||
1751e8a6 | 1332 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) |
4cb5300b CM |
1333 | goto out; |
1334 | ||
1335 | /* | |
1336 | * so now we have a nice long stream of locked | |
1337 | * and up to date pages, lets wait on them | |
1338 | */ | |
1339 | for (i = 0; i < i_done; i++) | |
1340 | wait_on_page_writeback(pages[i]); | |
1341 | ||
1342 | page_start = page_offset(pages[0]); | |
09cbfeaf | 1343 | page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE; |
4cb5300b CM |
1344 | |
1345 | lock_extent_bits(&BTRFS_I(inode)->io_tree, | |
ff13db41 | 1346 | page_start, page_end - 1, &cached_state); |
4cb5300b | 1347 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, |
e182163d OS |
1348 | page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
1349 | EXTENT_DEFRAG, 0, 0, &cached_state); | |
4cb5300b | 1350 | |
1f12bd06 | 1351 | if (i_done != page_cnt) { |
9e0baf60 | 1352 | spin_lock(&BTRFS_I(inode)->lock); |
28c4a3e2 | 1353 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
9e0baf60 | 1354 | spin_unlock(&BTRFS_I(inode)->lock); |
86d52921 | 1355 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
09cbfeaf | 1356 | start_index << PAGE_SHIFT, |
43b18595 | 1357 | (page_cnt - i_done) << PAGE_SHIFT, true); |
4cb5300b CM |
1358 | } |
1359 | ||
1360 | ||
9e8a4a8b | 1361 | set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, |
018ed4f7 | 1362 | &cached_state); |
4cb5300b CM |
1363 | |
1364 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, | |
e43bbe5e | 1365 | page_start, page_end - 1, &cached_state); |
4cb5300b CM |
1366 | |
1367 | for (i = 0; i < i_done; i++) { | |
1368 | clear_page_dirty_for_io(pages[i]); | |
1369 | ClearPageChecked(pages[i]); | |
1370 | set_page_extent_mapped(pages[i]); | |
1371 | set_page_dirty(pages[i]); | |
1372 | unlock_page(pages[i]); | |
09cbfeaf | 1373 | put_page(pages[i]); |
4cb5300b | 1374 | } |
8702ba93 | 1375 | btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); |
364ecf36 | 1376 | extent_changeset_free(data_reserved); |
4cb5300b CM |
1377 | return i_done; |
1378 | out: | |
1379 | for (i = 0; i < i_done; i++) { | |
1380 | unlock_page(pages[i]); | |
09cbfeaf | 1381 | put_page(pages[i]); |
4cb5300b | 1382 | } |
86d52921 | 1383 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
09cbfeaf | 1384 | start_index << PAGE_SHIFT, |
43b18595 | 1385 | page_cnt << PAGE_SHIFT, true); |
8702ba93 | 1386 | btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); |
364ecf36 | 1387 | extent_changeset_free(data_reserved); |
4cb5300b CM |
1388 | return ret; |
1389 | ||
1390 | } | |
1391 | ||
1392 | int btrfs_defrag_file(struct inode *inode, struct file *file, | |
1393 | struct btrfs_ioctl_defrag_range_args *range, | |
1394 | u64 newer_than, unsigned long max_to_defrag) | |
1395 | { | |
0b246afa | 1396 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4cb5300b | 1397 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4cb5300b | 1398 | struct file_ra_state *ra = NULL; |
f46b5a66 | 1399 | unsigned long last_index; |
151a31b2 | 1400 | u64 isize = i_size_read(inode); |
940100a4 CM |
1401 | u64 last_len = 0; |
1402 | u64 skip = 0; | |
1403 | u64 defrag_end = 0; | |
4cb5300b | 1404 | u64 newer_off = range->start; |
f46b5a66 | 1405 | unsigned long i; |
008873ea | 1406 | unsigned long ra_index = 0; |
f46b5a66 | 1407 | int ret; |
4cb5300b | 1408 | int defrag_count = 0; |
1a419d85 | 1409 | int compress_type = BTRFS_COMPRESS_ZLIB; |
aab110ab | 1410 | u32 extent_thresh = range->extent_thresh; |
09cbfeaf | 1411 | unsigned long max_cluster = SZ_256K >> PAGE_SHIFT; |
c41570c9 | 1412 | unsigned long cluster = max_cluster; |
ee22184b | 1413 | u64 new_align = ~((u64)SZ_128K - 1); |
4cb5300b | 1414 | struct page **pages = NULL; |
1e2ef46d | 1415 | bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; |
4cb5300b | 1416 | |
0abd5b17 LB |
1417 | if (isize == 0) |
1418 | return 0; | |
1419 | ||
1420 | if (range->start >= isize) | |
1421 | return -EINVAL; | |
1a419d85 | 1422 | |
1e2ef46d | 1423 | if (do_compress) { |
ce96b7ff | 1424 | if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES) |
1a419d85 LZ |
1425 | return -EINVAL; |
1426 | if (range->compress_type) | |
1427 | compress_type = range->compress_type; | |
1428 | } | |
f46b5a66 | 1429 | |
0abd5b17 | 1430 | if (extent_thresh == 0) |
ee22184b | 1431 | extent_thresh = SZ_256K; |
940100a4 | 1432 | |
4cb5300b | 1433 | /* |
0a52d108 DS |
1434 | * If we were not given a file, allocate a readahead context. As |
1435 | * readahead is just an optimization, defrag will work without it so | |
1436 | * we don't error out. | |
4cb5300b CM |
1437 | */ |
1438 | if (!file) { | |
63e727ec | 1439 | ra = kzalloc(sizeof(*ra), GFP_KERNEL); |
0a52d108 DS |
1440 | if (ra) |
1441 | file_ra_state_init(ra, inode->i_mapping); | |
4cb5300b CM |
1442 | } else { |
1443 | ra = &file->f_ra; | |
1444 | } | |
1445 | ||
63e727ec | 1446 | pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL); |
4cb5300b CM |
1447 | if (!pages) { |
1448 | ret = -ENOMEM; | |
1449 | goto out_ra; | |
1450 | } | |
1451 | ||
1452 | /* find the last page to defrag */ | |
1e701a32 | 1453 | if (range->start + range->len > range->start) { |
151a31b2 | 1454 | last_index = min_t(u64, isize - 1, |
09cbfeaf | 1455 | range->start + range->len - 1) >> PAGE_SHIFT; |
1e701a32 | 1456 | } else { |
09cbfeaf | 1457 | last_index = (isize - 1) >> PAGE_SHIFT; |
1e701a32 CM |
1458 | } |
1459 | ||
4cb5300b CM |
1460 | if (newer_than) { |
1461 | ret = find_new_extents(root, inode, newer_than, | |
ee22184b | 1462 | &newer_off, SZ_64K); |
4cb5300b CM |
1463 | if (!ret) { |
1464 | range->start = newer_off; | |
1465 | /* | |
1466 | * we always align our defrag to help keep | |
1467 | * the extents in the file evenly spaced | |
1468 | */ | |
09cbfeaf | 1469 | i = (newer_off & new_align) >> PAGE_SHIFT; |
4cb5300b CM |
1470 | } else |
1471 | goto out_ra; | |
1472 | } else { | |
09cbfeaf | 1473 | i = range->start >> PAGE_SHIFT; |
4cb5300b CM |
1474 | } |
1475 | if (!max_to_defrag) | |
070034bd | 1476 | max_to_defrag = last_index - i + 1; |
4cb5300b | 1477 | |
2a0f7f57 LZ |
1478 | /* |
1479 | * make writeback starts from i, so the defrag range can be | |
1480 | * written sequentially. | |
1481 | */ | |
1482 | if (i < inode->i_mapping->writeback_index) | |
1483 | inode->i_mapping->writeback_index = i; | |
1484 | ||
f7f43cc8 | 1485 | while (i <= last_index && defrag_count < max_to_defrag && |
09cbfeaf | 1486 | (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) { |
4cb5300b CM |
1487 | /* |
1488 | * make sure we stop running if someone unmounts | |
1489 | * the FS | |
1490 | */ | |
1751e8a6 | 1491 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) |
4cb5300b CM |
1492 | break; |
1493 | ||
0b246afa JM |
1494 | if (btrfs_defrag_cancelled(fs_info)) { |
1495 | btrfs_debug(fs_info, "defrag_file cancelled"); | |
210549eb DS |
1496 | ret = -EAGAIN; |
1497 | break; | |
1498 | } | |
1499 | ||
09cbfeaf | 1500 | if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, |
6c282eb4 | 1501 | extent_thresh, &last_len, &skip, |
1e2ef46d | 1502 | &defrag_end, do_compress)){ |
940100a4 CM |
1503 | unsigned long next; |
1504 | /* | |
1505 | * the should_defrag function tells us how much to skip | |
1506 | * bump our counter by the suggested amount | |
1507 | */ | |
09cbfeaf | 1508 | next = DIV_ROUND_UP(skip, PAGE_SIZE); |
940100a4 CM |
1509 | i = max(i + 1, next); |
1510 | continue; | |
1511 | } | |
008873ea LZ |
1512 | |
1513 | if (!newer_than) { | |
09cbfeaf KS |
1514 | cluster = (PAGE_ALIGN(defrag_end) >> |
1515 | PAGE_SHIFT) - i; | |
008873ea LZ |
1516 | cluster = min(cluster, max_cluster); |
1517 | } else { | |
1518 | cluster = max_cluster; | |
1519 | } | |
1520 | ||
008873ea LZ |
1521 | if (i + cluster > ra_index) { |
1522 | ra_index = max(i, ra_index); | |
0a52d108 | 1523 | if (ra) |
d3c0bab5 DS |
1524 | page_cache_sync_readahead(inode->i_mapping, ra, |
1525 | file, ra_index, cluster); | |
e4826a5b | 1526 | ra_index += cluster; |
008873ea | 1527 | } |
940100a4 | 1528 | |
5955102c | 1529 | inode_lock(inode); |
eede2bf3 OS |
1530 | if (IS_SWAPFILE(inode)) { |
1531 | ret = -ETXTBSY; | |
1532 | } else { | |
1533 | if (do_compress) | |
1534 | BTRFS_I(inode)->defrag_compress = compress_type; | |
1535 | ret = cluster_pages_for_defrag(inode, pages, i, cluster); | |
1536 | } | |
ecb8bea8 | 1537 | if (ret < 0) { |
5955102c | 1538 | inode_unlock(inode); |
4cb5300b | 1539 | goto out_ra; |
ecb8bea8 | 1540 | } |
4cb5300b CM |
1541 | |
1542 | defrag_count += ret; | |
d0e1d66b | 1543 | balance_dirty_pages_ratelimited(inode->i_mapping); |
5955102c | 1544 | inode_unlock(inode); |
4cb5300b CM |
1545 | |
1546 | if (newer_than) { | |
1547 | if (newer_off == (u64)-1) | |
1548 | break; | |
1549 | ||
e1f041e1 LB |
1550 | if (ret > 0) |
1551 | i += ret; | |
1552 | ||
4cb5300b | 1553 | newer_off = max(newer_off + 1, |
09cbfeaf | 1554 | (u64)i << PAGE_SHIFT); |
4cb5300b | 1555 | |
ee22184b BL |
1556 | ret = find_new_extents(root, inode, newer_than, |
1557 | &newer_off, SZ_64K); | |
4cb5300b CM |
1558 | if (!ret) { |
1559 | range->start = newer_off; | |
09cbfeaf | 1560 | i = (newer_off & new_align) >> PAGE_SHIFT; |
4cb5300b CM |
1561 | } else { |
1562 | break; | |
f46b5a66 | 1563 | } |
4cb5300b | 1564 | } else { |
008873ea | 1565 | if (ret > 0) { |
cbcc8326 | 1566 | i += ret; |
09cbfeaf | 1567 | last_len += ret << PAGE_SHIFT; |
008873ea | 1568 | } else { |
cbcc8326 | 1569 | i++; |
008873ea LZ |
1570 | last_len = 0; |
1571 | } | |
f46b5a66 | 1572 | } |
f46b5a66 CH |
1573 | } |
1574 | ||
dec8ef90 | 1575 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { |
1e701a32 | 1576 | filemap_flush(inode->i_mapping); |
dec8ef90 FM |
1577 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
1578 | &BTRFS_I(inode)->runtime_flags)) | |
1579 | filemap_flush(inode->i_mapping); | |
1580 | } | |
1e701a32 | 1581 | |
1a419d85 | 1582 | if (range->compress_type == BTRFS_COMPRESS_LZO) { |
0b246afa | 1583 | btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); |
5c1aab1d NT |
1584 | } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) { |
1585 | btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); | |
1a419d85 LZ |
1586 | } |
1587 | ||
60ccf82f | 1588 | ret = defrag_count; |
940100a4 | 1589 | |
4cb5300b | 1590 | out_ra: |
1e2ef46d | 1591 | if (do_compress) { |
5955102c | 1592 | inode_lock(inode); |
eec63c65 | 1593 | BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; |
5955102c | 1594 | inode_unlock(inode); |
633085c7 | 1595 | } |
4cb5300b CM |
1596 | if (!file) |
1597 | kfree(ra); | |
1598 | kfree(pages); | |
940100a4 | 1599 | return ret; |
f46b5a66 CH |
1600 | } |
1601 | ||
198605a8 | 1602 | static noinline int btrfs_ioctl_resize(struct file *file, |
76dda93c | 1603 | void __user *arg) |
f46b5a66 | 1604 | { |
0b246afa JM |
1605 | struct inode *inode = file_inode(file); |
1606 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
f46b5a66 CH |
1607 | u64 new_size; |
1608 | u64 old_size; | |
1609 | u64 devid = 1; | |
0b246afa | 1610 | struct btrfs_root *root = BTRFS_I(inode)->root; |
f46b5a66 CH |
1611 | struct btrfs_ioctl_vol_args *vol_args; |
1612 | struct btrfs_trans_handle *trans; | |
1613 | struct btrfs_device *device = NULL; | |
1614 | char *sizestr; | |
9a40f122 | 1615 | char *retptr; |
f46b5a66 CH |
1616 | char *devstr = NULL; |
1617 | int ret = 0; | |
f46b5a66 CH |
1618 | int mod = 0; |
1619 | ||
e441d54d CM |
1620 | if (!capable(CAP_SYS_ADMIN)) |
1621 | return -EPERM; | |
1622 | ||
198605a8 MX |
1623 | ret = mnt_want_write_file(file); |
1624 | if (ret) | |
1625 | return ret; | |
1626 | ||
171938e5 | 1627 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
97547676 | 1628 | mnt_drop_write_file(file); |
e57138b3 | 1629 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
c9e9f97b ID |
1630 | } |
1631 | ||
dae7b665 | 1632 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
1633 | if (IS_ERR(vol_args)) { |
1634 | ret = PTR_ERR(vol_args); | |
1635 | goto out; | |
1636 | } | |
5516e595 MF |
1637 | |
1638 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
f46b5a66 | 1639 | |
f46b5a66 CH |
1640 | sizestr = vol_args->name; |
1641 | devstr = strchr(sizestr, ':'); | |
1642 | if (devstr) { | |
f46b5a66 CH |
1643 | sizestr = devstr + 1; |
1644 | *devstr = '\0'; | |
1645 | devstr = vol_args->name; | |
58dfae63 Z |
1646 | ret = kstrtoull(devstr, 10, &devid); |
1647 | if (ret) | |
1648 | goto out_free; | |
dfd79829 MX |
1649 | if (!devid) { |
1650 | ret = -EINVAL; | |
1651 | goto out_free; | |
1652 | } | |
0b246afa | 1653 | btrfs_info(fs_info, "resizing devid %llu", devid); |
f46b5a66 | 1654 | } |
dba60f3f | 1655 | |
09ba3bc9 | 1656 | device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); |
f46b5a66 | 1657 | if (!device) { |
0b246afa JM |
1658 | btrfs_info(fs_info, "resizer unable to find device %llu", |
1659 | devid); | |
dfd79829 | 1660 | ret = -ENODEV; |
c9e9f97b | 1661 | goto out_free; |
f46b5a66 | 1662 | } |
dba60f3f | 1663 | |
ebbede42 | 1664 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
0b246afa | 1665 | btrfs_info(fs_info, |
efe120a0 | 1666 | "resizer unable to apply on readonly device %llu", |
c1c9ff7c | 1667 | devid); |
dfd79829 | 1668 | ret = -EPERM; |
4e42ae1b LB |
1669 | goto out_free; |
1670 | } | |
1671 | ||
f46b5a66 CH |
1672 | if (!strcmp(sizestr, "max")) |
1673 | new_size = device->bdev->bd_inode->i_size; | |
1674 | else { | |
1675 | if (sizestr[0] == '-') { | |
1676 | mod = -1; | |
1677 | sizestr++; | |
1678 | } else if (sizestr[0] == '+') { | |
1679 | mod = 1; | |
1680 | sizestr++; | |
1681 | } | |
9a40f122 GH |
1682 | new_size = memparse(sizestr, &retptr); |
1683 | if (*retptr != '\0' || new_size == 0) { | |
f46b5a66 | 1684 | ret = -EINVAL; |
c9e9f97b | 1685 | goto out_free; |
f46b5a66 CH |
1686 | } |
1687 | } | |
1688 | ||
401e29c1 | 1689 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
dfd79829 | 1690 | ret = -EPERM; |
63a212ab SB |
1691 | goto out_free; |
1692 | } | |
1693 | ||
7cc8e58d | 1694 | old_size = btrfs_device_get_total_bytes(device); |
f46b5a66 CH |
1695 | |
1696 | if (mod < 0) { | |
1697 | if (new_size > old_size) { | |
1698 | ret = -EINVAL; | |
c9e9f97b | 1699 | goto out_free; |
f46b5a66 CH |
1700 | } |
1701 | new_size = old_size - new_size; | |
1702 | } else if (mod > 0) { | |
eb8052e0 | 1703 | if (new_size > ULLONG_MAX - old_size) { |
902c68a4 | 1704 | ret = -ERANGE; |
eb8052e0 WF |
1705 | goto out_free; |
1706 | } | |
f46b5a66 CH |
1707 | new_size = old_size + new_size; |
1708 | } | |
1709 | ||
ee22184b | 1710 | if (new_size < SZ_256M) { |
f46b5a66 | 1711 | ret = -EINVAL; |
c9e9f97b | 1712 | goto out_free; |
f46b5a66 CH |
1713 | } |
1714 | if (new_size > device->bdev->bd_inode->i_size) { | |
1715 | ret = -EFBIG; | |
c9e9f97b | 1716 | goto out_free; |
f46b5a66 CH |
1717 | } |
1718 | ||
47f08b96 | 1719 | new_size = round_down(new_size, fs_info->sectorsize); |
f46b5a66 | 1720 | |
f46b5a66 | 1721 | if (new_size > old_size) { |
a22285a6 | 1722 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
1723 | if (IS_ERR(trans)) { |
1724 | ret = PTR_ERR(trans); | |
c9e9f97b | 1725 | goto out_free; |
98d5dc13 | 1726 | } |
f46b5a66 | 1727 | ret = btrfs_grow_device(trans, device, new_size); |
3a45bb20 | 1728 | btrfs_commit_transaction(trans); |
ece7d20e | 1729 | } else if (new_size < old_size) { |
f46b5a66 | 1730 | ret = btrfs_shrink_device(device, new_size); |
0253f40e | 1731 | } /* equal, nothing need to do */ |
f46b5a66 | 1732 | |
faf8f7b9 MPS |
1733 | if (ret == 0 && new_size != old_size) |
1734 | btrfs_info_in_rcu(fs_info, | |
1735 | "resize device %s (devid %llu) from %llu to %llu", | |
1736 | rcu_str_deref(device->name), device->devid, | |
1737 | old_size, new_size); | |
c9e9f97b | 1738 | out_free: |
f46b5a66 | 1739 | kfree(vol_args); |
c9e9f97b | 1740 | out: |
171938e5 | 1741 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
18f39c41 | 1742 | mnt_drop_write_file(file); |
f46b5a66 CH |
1743 | return ret; |
1744 | } | |
1745 | ||
5d54c67e | 1746 | static noinline int __btrfs_ioctl_snap_create(struct file *file, |
52f75f4f | 1747 | const char *name, unsigned long fd, int subvol, |
5d54c67e | 1748 | bool readonly, |
8696c533 | 1749 | struct btrfs_qgroup_inherit *inherit) |
f46b5a66 | 1750 | { |
f46b5a66 | 1751 | int namelen; |
3de4586c | 1752 | int ret = 0; |
f46b5a66 | 1753 | |
325c50e3 JM |
1754 | if (!S_ISDIR(file_inode(file)->i_mode)) |
1755 | return -ENOTDIR; | |
1756 | ||
a874a63e LB |
1757 | ret = mnt_want_write_file(file); |
1758 | if (ret) | |
1759 | goto out; | |
1760 | ||
72fd032e SW |
1761 | namelen = strlen(name); |
1762 | if (strchr(name, '/')) { | |
f46b5a66 | 1763 | ret = -EINVAL; |
a874a63e | 1764 | goto out_drop_write; |
f46b5a66 CH |
1765 | } |
1766 | ||
16780cab CM |
1767 | if (name[0] == '.' && |
1768 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { | |
1769 | ret = -EEXIST; | |
a874a63e | 1770 | goto out_drop_write; |
16780cab CM |
1771 | } |
1772 | ||
3de4586c | 1773 | if (subvol) { |
72fd032e | 1774 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
5d54c67e | 1775 | NULL, readonly, inherit); |
cb8e7090 | 1776 | } else { |
2903ff01 | 1777 | struct fd src = fdget(fd); |
3de4586c | 1778 | struct inode *src_inode; |
2903ff01 | 1779 | if (!src.file) { |
3de4586c | 1780 | ret = -EINVAL; |
a874a63e | 1781 | goto out_drop_write; |
3de4586c CM |
1782 | } |
1783 | ||
496ad9aa AV |
1784 | src_inode = file_inode(src.file); |
1785 | if (src_inode->i_sb != file_inode(file)->i_sb) { | |
c79b4713 | 1786 | btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, |
efe120a0 | 1787 | "Snapshot src from another FS"); |
23ad5b17 | 1788 | ret = -EXDEV; |
d0242061 DS |
1789 | } else if (!inode_owner_or_capable(src_inode)) { |
1790 | /* | |
1791 | * Subvolume creation is not restricted, but snapshots | |
1792 | * are limited to own subvolumes only | |
1793 | */ | |
1794 | ret = -EPERM; | |
ecd18815 | 1795 | } else { |
c11fbb6e | 1796 | ret = btrfs_mksnapshot(&file->f_path, name, namelen, |
ecd18815 | 1797 | BTRFS_I(src_inode)->root, |
5d54c67e | 1798 | readonly, inherit); |
3de4586c | 1799 | } |
2903ff01 | 1800 | fdput(src); |
cb8e7090 | 1801 | } |
a874a63e LB |
1802 | out_drop_write: |
1803 | mnt_drop_write_file(file); | |
f46b5a66 | 1804 | out: |
72fd032e SW |
1805 | return ret; |
1806 | } | |
1807 | ||
1808 | static noinline int btrfs_ioctl_snap_create(struct file *file, | |
fa0d2b9b | 1809 | void __user *arg, int subvol) |
72fd032e | 1810 | { |
fa0d2b9b | 1811 | struct btrfs_ioctl_vol_args *vol_args; |
72fd032e SW |
1812 | int ret; |
1813 | ||
325c50e3 JM |
1814 | if (!S_ISDIR(file_inode(file)->i_mode)) |
1815 | return -ENOTDIR; | |
1816 | ||
fa0d2b9b LZ |
1817 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1818 | if (IS_ERR(vol_args)) | |
1819 | return PTR_ERR(vol_args); | |
1820 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
72fd032e | 1821 | |
5d54c67e NB |
1822 | ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd, |
1823 | subvol, false, NULL); | |
fdfb1e4f | 1824 | |
fa0d2b9b LZ |
1825 | kfree(vol_args); |
1826 | return ret; | |
1827 | } | |
fdfb1e4f | 1828 | |
fa0d2b9b LZ |
1829 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
1830 | void __user *arg, int subvol) | |
1831 | { | |
1832 | struct btrfs_ioctl_vol_args_v2 *vol_args; | |
1833 | int ret; | |
b83cc969 | 1834 | bool readonly = false; |
6f72c7e2 | 1835 | struct btrfs_qgroup_inherit *inherit = NULL; |
75eaa0e2 | 1836 | |
325c50e3 JM |
1837 | if (!S_ISDIR(file_inode(file)->i_mode)) |
1838 | return -ENOTDIR; | |
1839 | ||
fa0d2b9b LZ |
1840 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1841 | if (IS_ERR(vol_args)) | |
1842 | return PTR_ERR(vol_args); | |
1843 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; | |
75eaa0e2 | 1844 | |
673990db | 1845 | if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) { |
b83cc969 | 1846 | ret = -EOPNOTSUPP; |
c47ca32d | 1847 | goto free_args; |
72fd032e | 1848 | } |
fa0d2b9b | 1849 | |
b83cc969 LZ |
1850 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
1851 | readonly = true; | |
6f72c7e2 | 1852 | if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { |
09cbfeaf | 1853 | if (vol_args->size > PAGE_SIZE) { |
6f72c7e2 | 1854 | ret = -EINVAL; |
c47ca32d | 1855 | goto free_args; |
6f72c7e2 AJ |
1856 | } |
1857 | inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); | |
1858 | if (IS_ERR(inherit)) { | |
1859 | ret = PTR_ERR(inherit); | |
c47ca32d | 1860 | goto free_args; |
6f72c7e2 AJ |
1861 | } |
1862 | } | |
fa0d2b9b | 1863 | |
5d54c67e NB |
1864 | ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd, |
1865 | subvol, readonly, inherit); | |
c47ca32d DC |
1866 | if (ret) |
1867 | goto free_inherit; | |
c47ca32d | 1868 | free_inherit: |
6f72c7e2 | 1869 | kfree(inherit); |
c47ca32d DC |
1870 | free_args: |
1871 | kfree(vol_args); | |
f46b5a66 CH |
1872 | return ret; |
1873 | } | |
1874 | ||
0caa102d LZ |
1875 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
1876 | void __user *arg) | |
1877 | { | |
496ad9aa | 1878 | struct inode *inode = file_inode(file); |
0b246afa | 1879 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0caa102d LZ |
1880 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1881 | int ret = 0; | |
1882 | u64 flags = 0; | |
1883 | ||
4a0cc7ca | 1884 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) |
0caa102d LZ |
1885 | return -EINVAL; |
1886 | ||
0b246afa | 1887 | down_read(&fs_info->subvol_sem); |
0caa102d LZ |
1888 | if (btrfs_root_readonly(root)) |
1889 | flags |= BTRFS_SUBVOL_RDONLY; | |
0b246afa | 1890 | up_read(&fs_info->subvol_sem); |
0caa102d LZ |
1891 | |
1892 | if (copy_to_user(arg, &flags, sizeof(flags))) | |
1893 | ret = -EFAULT; | |
1894 | ||
1895 | return ret; | |
1896 | } | |
1897 | ||
1898 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, | |
1899 | void __user *arg) | |
1900 | { | |
496ad9aa | 1901 | struct inode *inode = file_inode(file); |
0b246afa | 1902 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0caa102d LZ |
1903 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1904 | struct btrfs_trans_handle *trans; | |
1905 | u64 root_flags; | |
1906 | u64 flags; | |
1907 | int ret = 0; | |
1908 | ||
bd60ea0f DS |
1909 | if (!inode_owner_or_capable(inode)) |
1910 | return -EPERM; | |
1911 | ||
b9ca0664 LB |
1912 | ret = mnt_want_write_file(file); |
1913 | if (ret) | |
1914 | goto out; | |
0caa102d | 1915 | |
4a0cc7ca | 1916 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
b9ca0664 LB |
1917 | ret = -EINVAL; |
1918 | goto out_drop_write; | |
1919 | } | |
0caa102d | 1920 | |
b9ca0664 LB |
1921 | if (copy_from_user(&flags, arg, sizeof(flags))) { |
1922 | ret = -EFAULT; | |
1923 | goto out_drop_write; | |
1924 | } | |
0caa102d | 1925 | |
b9ca0664 LB |
1926 | if (flags & ~BTRFS_SUBVOL_RDONLY) { |
1927 | ret = -EOPNOTSUPP; | |
1928 | goto out_drop_write; | |
1929 | } | |
0caa102d | 1930 | |
0b246afa | 1931 | down_write(&fs_info->subvol_sem); |
0caa102d LZ |
1932 | |
1933 | /* nothing to do */ | |
1934 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) | |
b9ca0664 | 1935 | goto out_drop_sem; |
0caa102d LZ |
1936 | |
1937 | root_flags = btrfs_root_flags(&root->root_item); | |
2c686537 | 1938 | if (flags & BTRFS_SUBVOL_RDONLY) { |
0caa102d LZ |
1939 | btrfs_set_root_flags(&root->root_item, |
1940 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); | |
2c686537 DS |
1941 | } else { |
1942 | /* | |
1943 | * Block RO -> RW transition if this subvolume is involved in | |
1944 | * send | |
1945 | */ | |
1946 | spin_lock(&root->root_item_lock); | |
1947 | if (root->send_in_progress == 0) { | |
1948 | btrfs_set_root_flags(&root->root_item, | |
0caa102d | 1949 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); |
2c686537 DS |
1950 | spin_unlock(&root->root_item_lock); |
1951 | } else { | |
1952 | spin_unlock(&root->root_item_lock); | |
0b246afa JM |
1953 | btrfs_warn(fs_info, |
1954 | "Attempt to set subvolume %llu read-write during send", | |
1955 | root->root_key.objectid); | |
2c686537 DS |
1956 | ret = -EPERM; |
1957 | goto out_drop_sem; | |
1958 | } | |
1959 | } | |
0caa102d LZ |
1960 | |
1961 | trans = btrfs_start_transaction(root, 1); | |
1962 | if (IS_ERR(trans)) { | |
1963 | ret = PTR_ERR(trans); | |
1964 | goto out_reset; | |
1965 | } | |
1966 | ||
0b246afa | 1967 | ret = btrfs_update_root(trans, fs_info->tree_root, |
0caa102d | 1968 | &root->root_key, &root->root_item); |
9417ebc8 NB |
1969 | if (ret < 0) { |
1970 | btrfs_end_transaction(trans); | |
1971 | goto out_reset; | |
1972 | } | |
1973 | ||
1974 | ret = btrfs_commit_transaction(trans); | |
0caa102d | 1975 | |
0caa102d LZ |
1976 | out_reset: |
1977 | if (ret) | |
1978 | btrfs_set_root_flags(&root->root_item, root_flags); | |
b9ca0664 | 1979 | out_drop_sem: |
0b246afa | 1980 | up_write(&fs_info->subvol_sem); |
b9ca0664 LB |
1981 | out_drop_write: |
1982 | mnt_drop_write_file(file); | |
1983 | out: | |
0caa102d LZ |
1984 | return ret; |
1985 | } | |
1986 | ||
ac8e9819 CM |
1987 | static noinline int key_in_sk(struct btrfs_key *key, |
1988 | struct btrfs_ioctl_search_key *sk) | |
1989 | { | |
abc6e134 CM |
1990 | struct btrfs_key test; |
1991 | int ret; | |
1992 | ||
1993 | test.objectid = sk->min_objectid; | |
1994 | test.type = sk->min_type; | |
1995 | test.offset = sk->min_offset; | |
1996 | ||
1997 | ret = btrfs_comp_cpu_keys(key, &test); | |
1998 | if (ret < 0) | |
ac8e9819 | 1999 | return 0; |
abc6e134 CM |
2000 | |
2001 | test.objectid = sk->max_objectid; | |
2002 | test.type = sk->max_type; | |
2003 | test.offset = sk->max_offset; | |
2004 | ||
2005 | ret = btrfs_comp_cpu_keys(key, &test); | |
2006 | if (ret > 0) | |
ac8e9819 CM |
2007 | return 0; |
2008 | return 1; | |
2009 | } | |
2010 | ||
df397565 | 2011 | static noinline int copy_to_sk(struct btrfs_path *path, |
ac8e9819 CM |
2012 | struct btrfs_key *key, |
2013 | struct btrfs_ioctl_search_key *sk, | |
9b6e817d | 2014 | size_t *buf_size, |
ba346b35 | 2015 | char __user *ubuf, |
ac8e9819 CM |
2016 | unsigned long *sk_offset, |
2017 | int *num_found) | |
2018 | { | |
2019 | u64 found_transid; | |
2020 | struct extent_buffer *leaf; | |
2021 | struct btrfs_ioctl_search_header sh; | |
dd81d459 | 2022 | struct btrfs_key test; |
ac8e9819 CM |
2023 | unsigned long item_off; |
2024 | unsigned long item_len; | |
2025 | int nritems; | |
2026 | int i; | |
2027 | int slot; | |
ac8e9819 CM |
2028 | int ret = 0; |
2029 | ||
2030 | leaf = path->nodes[0]; | |
2031 | slot = path->slots[0]; | |
2032 | nritems = btrfs_header_nritems(leaf); | |
2033 | ||
2034 | if (btrfs_header_generation(leaf) > sk->max_transid) { | |
2035 | i = nritems; | |
2036 | goto advance_key; | |
2037 | } | |
2038 | found_transid = btrfs_header_generation(leaf); | |
2039 | ||
2040 | for (i = slot; i < nritems; i++) { | |
2041 | item_off = btrfs_item_ptr_offset(leaf, i); | |
2042 | item_len = btrfs_item_size_nr(leaf, i); | |
2043 | ||
03b71c6c GP |
2044 | btrfs_item_key_to_cpu(leaf, key, i); |
2045 | if (!key_in_sk(key, sk)) | |
2046 | continue; | |
2047 | ||
9b6e817d | 2048 | if (sizeof(sh) + item_len > *buf_size) { |
8f5f6178 GH |
2049 | if (*num_found) { |
2050 | ret = 1; | |
2051 | goto out; | |
2052 | } | |
2053 | ||
2054 | /* | |
2055 | * return one empty item back for v1, which does not | |
2056 | * handle -EOVERFLOW | |
2057 | */ | |
2058 | ||
9b6e817d | 2059 | *buf_size = sizeof(sh) + item_len; |
ac8e9819 | 2060 | item_len = 0; |
8f5f6178 GH |
2061 | ret = -EOVERFLOW; |
2062 | } | |
ac8e9819 | 2063 | |
9b6e817d | 2064 | if (sizeof(sh) + item_len + *sk_offset > *buf_size) { |
ac8e9819 | 2065 | ret = 1; |
25c9bc2e | 2066 | goto out; |
ac8e9819 CM |
2067 | } |
2068 | ||
ac8e9819 CM |
2069 | sh.objectid = key->objectid; |
2070 | sh.offset = key->offset; | |
2071 | sh.type = key->type; | |
2072 | sh.len = item_len; | |
2073 | sh.transid = found_transid; | |
2074 | ||
2075 | /* copy search result header */ | |
ba346b35 GH |
2076 | if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { |
2077 | ret = -EFAULT; | |
2078 | goto out; | |
2079 | } | |
2080 | ||
ac8e9819 CM |
2081 | *sk_offset += sizeof(sh); |
2082 | ||
2083 | if (item_len) { | |
ba346b35 | 2084 | char __user *up = ubuf + *sk_offset; |
ac8e9819 | 2085 | /* copy the item */ |
ba346b35 GH |
2086 | if (read_extent_buffer_to_user(leaf, up, |
2087 | item_off, item_len)) { | |
2088 | ret = -EFAULT; | |
2089 | goto out; | |
2090 | } | |
2091 | ||
ac8e9819 | 2092 | *sk_offset += item_len; |
ac8e9819 | 2093 | } |
e2156867 | 2094 | (*num_found)++; |
ac8e9819 | 2095 | |
8f5f6178 GH |
2096 | if (ret) /* -EOVERFLOW from above */ |
2097 | goto out; | |
2098 | ||
25c9bc2e GH |
2099 | if (*num_found >= sk->nr_items) { |
2100 | ret = 1; | |
2101 | goto out; | |
2102 | } | |
ac8e9819 CM |
2103 | } |
2104 | advance_key: | |
abc6e134 | 2105 | ret = 0; |
dd81d459 NA |
2106 | test.objectid = sk->max_objectid; |
2107 | test.type = sk->max_type; | |
2108 | test.offset = sk->max_offset; | |
2109 | if (btrfs_comp_cpu_keys(key, &test) >= 0) | |
2110 | ret = 1; | |
2111 | else if (key->offset < (u64)-1) | |
ac8e9819 | 2112 | key->offset++; |
dd81d459 | 2113 | else if (key->type < (u8)-1) { |
abc6e134 | 2114 | key->offset = 0; |
ac8e9819 | 2115 | key->type++; |
dd81d459 | 2116 | } else if (key->objectid < (u64)-1) { |
abc6e134 CM |
2117 | key->offset = 0; |
2118 | key->type = 0; | |
ac8e9819 | 2119 | key->objectid++; |
abc6e134 CM |
2120 | } else |
2121 | ret = 1; | |
25c9bc2e | 2122 | out: |
ba346b35 GH |
2123 | /* |
2124 | * 0: all items from this leaf copied, continue with next | |
2125 | * 1: * more items can be copied, but unused buffer is too small | |
2126 | * * all items were found | |
2127 | * Either way, it will stops the loop which iterates to the next | |
2128 | * leaf | |
2129 | * -EOVERFLOW: item was to large for buffer | |
2130 | * -EFAULT: could not copy extent buffer back to userspace | |
2131 | */ | |
ac8e9819 CM |
2132 | return ret; |
2133 | } | |
2134 | ||
2135 | static noinline int search_ioctl(struct inode *inode, | |
12544442 | 2136 | struct btrfs_ioctl_search_key *sk, |
9b6e817d | 2137 | size_t *buf_size, |
ba346b35 | 2138 | char __user *ubuf) |
ac8e9819 | 2139 | { |
0b246afa | 2140 | struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); |
ac8e9819 CM |
2141 | struct btrfs_root *root; |
2142 | struct btrfs_key key; | |
ac8e9819 | 2143 | struct btrfs_path *path; |
ac8e9819 CM |
2144 | int ret; |
2145 | int num_found = 0; | |
2146 | unsigned long sk_offset = 0; | |
2147 | ||
9b6e817d GH |
2148 | if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { |
2149 | *buf_size = sizeof(struct btrfs_ioctl_search_header); | |
12544442 | 2150 | return -EOVERFLOW; |
9b6e817d | 2151 | } |
12544442 | 2152 | |
ac8e9819 CM |
2153 | path = btrfs_alloc_path(); |
2154 | if (!path) | |
2155 | return -ENOMEM; | |
2156 | ||
2157 | if (sk->tree_id == 0) { | |
2158 | /* search the root of the inode that was passed */ | |
00246528 | 2159 | root = btrfs_grab_root(BTRFS_I(inode)->root); |
ac8e9819 | 2160 | } else { |
56e9357a | 2161 | root = btrfs_get_fs_root(info, sk->tree_id, true); |
ac8e9819 | 2162 | if (IS_ERR(root)) { |
ac8e9819 | 2163 | btrfs_free_path(path); |
ad1e3d56 | 2164 | return PTR_ERR(root); |
ac8e9819 CM |
2165 | } |
2166 | } | |
2167 | ||
2168 | key.objectid = sk->min_objectid; | |
2169 | key.type = sk->min_type; | |
2170 | key.offset = sk->min_offset; | |
2171 | ||
67871254 | 2172 | while (1) { |
6174d3cb | 2173 | ret = btrfs_search_forward(root, &key, path, sk->min_transid); |
ac8e9819 CM |
2174 | if (ret != 0) { |
2175 | if (ret > 0) | |
2176 | ret = 0; | |
2177 | goto err; | |
2178 | } | |
df397565 | 2179 | ret = copy_to_sk(path, &key, sk, buf_size, ubuf, |
ac8e9819 | 2180 | &sk_offset, &num_found); |
b3b4aa74 | 2181 | btrfs_release_path(path); |
25c9bc2e | 2182 | if (ret) |
ac8e9819 CM |
2183 | break; |
2184 | ||
2185 | } | |
8f5f6178 GH |
2186 | if (ret > 0) |
2187 | ret = 0; | |
ac8e9819 CM |
2188 | err: |
2189 | sk->nr_items = num_found; | |
00246528 | 2190 | btrfs_put_root(root); |
ac8e9819 CM |
2191 | btrfs_free_path(path); |
2192 | return ret; | |
2193 | } | |
2194 | ||
2195 | static noinline int btrfs_ioctl_tree_search(struct file *file, | |
2196 | void __user *argp) | |
2197 | { | |
ba346b35 GH |
2198 | struct btrfs_ioctl_search_args __user *uargs; |
2199 | struct btrfs_ioctl_search_key sk; | |
9b6e817d GH |
2200 | struct inode *inode; |
2201 | int ret; | |
2202 | size_t buf_size; | |
ac8e9819 CM |
2203 | |
2204 | if (!capable(CAP_SYS_ADMIN)) | |
2205 | return -EPERM; | |
2206 | ||
ba346b35 GH |
2207 | uargs = (struct btrfs_ioctl_search_args __user *)argp; |
2208 | ||
2209 | if (copy_from_user(&sk, &uargs->key, sizeof(sk))) | |
2210 | return -EFAULT; | |
ac8e9819 | 2211 | |
ba346b35 | 2212 | buf_size = sizeof(uargs->buf); |
ac8e9819 | 2213 | |
496ad9aa | 2214 | inode = file_inode(file); |
ba346b35 | 2215 | ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); |
8f5f6178 GH |
2216 | |
2217 | /* | |
2218 | * In the origin implementation an overflow is handled by returning a | |
2219 | * search header with a len of zero, so reset ret. | |
2220 | */ | |
2221 | if (ret == -EOVERFLOW) | |
2222 | ret = 0; | |
2223 | ||
ba346b35 | 2224 | if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) |
ac8e9819 | 2225 | ret = -EFAULT; |
ac8e9819 CM |
2226 | return ret; |
2227 | } | |
2228 | ||
cc68a8a5 GH |
2229 | static noinline int btrfs_ioctl_tree_search_v2(struct file *file, |
2230 | void __user *argp) | |
2231 | { | |
2232 | struct btrfs_ioctl_search_args_v2 __user *uarg; | |
2233 | struct btrfs_ioctl_search_args_v2 args; | |
2234 | struct inode *inode; | |
2235 | int ret; | |
2236 | size_t buf_size; | |
ee22184b | 2237 | const size_t buf_limit = SZ_16M; |
cc68a8a5 GH |
2238 | |
2239 | if (!capable(CAP_SYS_ADMIN)) | |
2240 | return -EPERM; | |
2241 | ||
2242 | /* copy search header and buffer size */ | |
2243 | uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; | |
2244 | if (copy_from_user(&args, uarg, sizeof(args))) | |
2245 | return -EFAULT; | |
2246 | ||
2247 | buf_size = args.buf_size; | |
2248 | ||
cc68a8a5 GH |
2249 | /* limit result size to 16MB */ |
2250 | if (buf_size > buf_limit) | |
2251 | buf_size = buf_limit; | |
2252 | ||
2253 | inode = file_inode(file); | |
2254 | ret = search_ioctl(inode, &args.key, &buf_size, | |
718dc5fa | 2255 | (char __user *)(&uarg->buf[0])); |
cc68a8a5 GH |
2256 | if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) |
2257 | ret = -EFAULT; | |
2258 | else if (ret == -EOVERFLOW && | |
2259 | copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) | |
2260 | ret = -EFAULT; | |
2261 | ||
ac8e9819 CM |
2262 | return ret; |
2263 | } | |
2264 | ||
98d377a0 | 2265 | /* |
ac8e9819 CM |
2266 | * Search INODE_REFs to identify path name of 'dirid' directory |
2267 | * in a 'tree_id' tree. and sets path name to 'name'. | |
2268 | */ | |
98d377a0 TH |
2269 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
2270 | u64 tree_id, u64 dirid, char *name) | |
2271 | { | |
2272 | struct btrfs_root *root; | |
2273 | struct btrfs_key key; | |
ac8e9819 | 2274 | char *ptr; |
98d377a0 TH |
2275 | int ret = -1; |
2276 | int slot; | |
2277 | int len; | |
2278 | int total_len = 0; | |
2279 | struct btrfs_inode_ref *iref; | |
2280 | struct extent_buffer *l; | |
2281 | struct btrfs_path *path; | |
2282 | ||
2283 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { | |
2284 | name[0]='\0'; | |
2285 | return 0; | |
2286 | } | |
2287 | ||
2288 | path = btrfs_alloc_path(); | |
2289 | if (!path) | |
2290 | return -ENOMEM; | |
2291 | ||
c8bcbfbd | 2292 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; |
98d377a0 | 2293 | |
56e9357a | 2294 | root = btrfs_get_fs_root(info, tree_id, true); |
98d377a0 | 2295 | if (IS_ERR(root)) { |
ad1e3d56 | 2296 | ret = PTR_ERR(root); |
88234012 JB |
2297 | root = NULL; |
2298 | goto out; | |
2299 | } | |
98d377a0 TH |
2300 | |
2301 | key.objectid = dirid; | |
2302 | key.type = BTRFS_INODE_REF_KEY; | |
8ad6fcab | 2303 | key.offset = (u64)-1; |
98d377a0 | 2304 | |
67871254 | 2305 | while (1) { |
98d377a0 TH |
2306 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
2307 | if (ret < 0) | |
2308 | goto out; | |
18674c6c FDBM |
2309 | else if (ret > 0) { |
2310 | ret = btrfs_previous_item(root, path, dirid, | |
2311 | BTRFS_INODE_REF_KEY); | |
2312 | if (ret < 0) | |
2313 | goto out; | |
2314 | else if (ret > 0) { | |
2315 | ret = -ENOENT; | |
2316 | goto out; | |
2317 | } | |
2318 | } | |
98d377a0 TH |
2319 | |
2320 | l = path->nodes[0]; | |
2321 | slot = path->slots[0]; | |
2322 | btrfs_item_key_to_cpu(l, &key, slot); | |
2323 | ||
98d377a0 TH |
2324 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); |
2325 | len = btrfs_inode_ref_name_len(l, iref); | |
2326 | ptr -= len + 1; | |
2327 | total_len += len + 1; | |
a696cf35 FDBM |
2328 | if (ptr < name) { |
2329 | ret = -ENAMETOOLONG; | |
98d377a0 | 2330 | goto out; |
a696cf35 | 2331 | } |
98d377a0 TH |
2332 | |
2333 | *(ptr + len) = '/'; | |
67871254 | 2334 | read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); |
98d377a0 TH |
2335 | |
2336 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) | |
2337 | break; | |
2338 | ||
b3b4aa74 | 2339 | btrfs_release_path(path); |
98d377a0 | 2340 | key.objectid = key.offset; |
8ad6fcab | 2341 | key.offset = (u64)-1; |
98d377a0 | 2342 | dirid = key.objectid; |
98d377a0 | 2343 | } |
77906a50 | 2344 | memmove(name, ptr, total_len); |
67871254 | 2345 | name[total_len] = '\0'; |
98d377a0 TH |
2346 | ret = 0; |
2347 | out: | |
00246528 | 2348 | btrfs_put_root(root); |
98d377a0 | 2349 | btrfs_free_path(path); |
ac8e9819 CM |
2350 | return ret; |
2351 | } | |
2352 | ||
23d0b79d TM |
2353 | static int btrfs_search_path_in_tree_user(struct inode *inode, |
2354 | struct btrfs_ioctl_ino_lookup_user_args *args) | |
2355 | { | |
2356 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
2357 | struct super_block *sb = inode->i_sb; | |
2358 | struct btrfs_key upper_limit = BTRFS_I(inode)->location; | |
2359 | u64 treeid = BTRFS_I(inode)->root->root_key.objectid; | |
2360 | u64 dirid = args->dirid; | |
2361 | unsigned long item_off; | |
2362 | unsigned long item_len; | |
2363 | struct btrfs_inode_ref *iref; | |
2364 | struct btrfs_root_ref *rref; | |
b8a49ae1 | 2365 | struct btrfs_root *root = NULL; |
23d0b79d TM |
2366 | struct btrfs_path *path; |
2367 | struct btrfs_key key, key2; | |
2368 | struct extent_buffer *leaf; | |
2369 | struct inode *temp_inode; | |
2370 | char *ptr; | |
2371 | int slot; | |
2372 | int len; | |
2373 | int total_len = 0; | |
2374 | int ret; | |
2375 | ||
2376 | path = btrfs_alloc_path(); | |
2377 | if (!path) | |
2378 | return -ENOMEM; | |
2379 | ||
2380 | /* | |
2381 | * If the bottom subvolume does not exist directly under upper_limit, | |
2382 | * construct the path in from the bottom up. | |
2383 | */ | |
2384 | if (dirid != upper_limit.objectid) { | |
2385 | ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1]; | |
2386 | ||
56e9357a | 2387 | root = btrfs_get_fs_root(fs_info, treeid, true); |
23d0b79d TM |
2388 | if (IS_ERR(root)) { |
2389 | ret = PTR_ERR(root); | |
2390 | goto out; | |
2391 | } | |
2392 | ||
2393 | key.objectid = dirid; | |
2394 | key.type = BTRFS_INODE_REF_KEY; | |
2395 | key.offset = (u64)-1; | |
2396 | while (1) { | |
2397 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2398 | if (ret < 0) { | |
b8a49ae1 | 2399 | goto out_put; |
23d0b79d TM |
2400 | } else if (ret > 0) { |
2401 | ret = btrfs_previous_item(root, path, dirid, | |
2402 | BTRFS_INODE_REF_KEY); | |
2403 | if (ret < 0) { | |
b8a49ae1 | 2404 | goto out_put; |
23d0b79d TM |
2405 | } else if (ret > 0) { |
2406 | ret = -ENOENT; | |
b8a49ae1 | 2407 | goto out_put; |
23d0b79d TM |
2408 | } |
2409 | } | |
2410 | ||
2411 | leaf = path->nodes[0]; | |
2412 | slot = path->slots[0]; | |
2413 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2414 | ||
2415 | iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref); | |
2416 | len = btrfs_inode_ref_name_len(leaf, iref); | |
2417 | ptr -= len + 1; | |
2418 | total_len += len + 1; | |
2419 | if (ptr < args->path) { | |
2420 | ret = -ENAMETOOLONG; | |
b8a49ae1 | 2421 | goto out_put; |
23d0b79d TM |
2422 | } |
2423 | ||
2424 | *(ptr + len) = '/'; | |
2425 | read_extent_buffer(leaf, ptr, | |
2426 | (unsigned long)(iref + 1), len); | |
2427 | ||
2428 | /* Check the read+exec permission of this directory */ | |
2429 | ret = btrfs_previous_item(root, path, dirid, | |
2430 | BTRFS_INODE_ITEM_KEY); | |
2431 | if (ret < 0) { | |
b8a49ae1 | 2432 | goto out_put; |
23d0b79d TM |
2433 | } else if (ret > 0) { |
2434 | ret = -ENOENT; | |
b8a49ae1 | 2435 | goto out_put; |
23d0b79d TM |
2436 | } |
2437 | ||
2438 | leaf = path->nodes[0]; | |
2439 | slot = path->slots[0]; | |
2440 | btrfs_item_key_to_cpu(leaf, &key2, slot); | |
2441 | if (key2.objectid != dirid) { | |
2442 | ret = -ENOENT; | |
b8a49ae1 | 2443 | goto out_put; |
23d0b79d TM |
2444 | } |
2445 | ||
0202e83f | 2446 | temp_inode = btrfs_iget(sb, key2.objectid, root); |
3ca57bd6 MT |
2447 | if (IS_ERR(temp_inode)) { |
2448 | ret = PTR_ERR(temp_inode); | |
b8a49ae1 | 2449 | goto out_put; |
3ca57bd6 | 2450 | } |
23d0b79d TM |
2451 | ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC); |
2452 | iput(temp_inode); | |
2453 | if (ret) { | |
2454 | ret = -EACCES; | |
b8a49ae1 | 2455 | goto out_put; |
23d0b79d TM |
2456 | } |
2457 | ||
2458 | if (key.offset == upper_limit.objectid) | |
2459 | break; | |
2460 | if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) { | |
2461 | ret = -EACCES; | |
b8a49ae1 | 2462 | goto out_put; |
23d0b79d TM |
2463 | } |
2464 | ||
2465 | btrfs_release_path(path); | |
2466 | key.objectid = key.offset; | |
2467 | key.offset = (u64)-1; | |
2468 | dirid = key.objectid; | |
2469 | } | |
2470 | ||
2471 | memmove(args->path, ptr, total_len); | |
2472 | args->path[total_len] = '\0'; | |
00246528 | 2473 | btrfs_put_root(root); |
b8a49ae1 | 2474 | root = NULL; |
23d0b79d TM |
2475 | btrfs_release_path(path); |
2476 | } | |
2477 | ||
2478 | /* Get the bottom subvolume's name from ROOT_REF */ | |
23d0b79d TM |
2479 | key.objectid = treeid; |
2480 | key.type = BTRFS_ROOT_REF_KEY; | |
2481 | key.offset = args->treeid; | |
b8a49ae1 | 2482 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
23d0b79d TM |
2483 | if (ret < 0) { |
2484 | goto out; | |
2485 | } else if (ret > 0) { | |
2486 | ret = -ENOENT; | |
2487 | goto out; | |
2488 | } | |
2489 | ||
2490 | leaf = path->nodes[0]; | |
2491 | slot = path->slots[0]; | |
2492 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2493 | ||
2494 | item_off = btrfs_item_ptr_offset(leaf, slot); | |
2495 | item_len = btrfs_item_size_nr(leaf, slot); | |
2496 | /* Check if dirid in ROOT_REF corresponds to passed dirid */ | |
2497 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); | |
2498 | if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) { | |
2499 | ret = -EINVAL; | |
2500 | goto out; | |
2501 | } | |
2502 | ||
2503 | /* Copy subvolume's name */ | |
2504 | item_off += sizeof(struct btrfs_root_ref); | |
2505 | item_len -= sizeof(struct btrfs_root_ref); | |
2506 | read_extent_buffer(leaf, args->name, item_off, item_len); | |
2507 | args->name[item_len] = 0; | |
2508 | ||
b8a49ae1 | 2509 | out_put: |
00246528 | 2510 | btrfs_put_root(root); |
23d0b79d TM |
2511 | out: |
2512 | btrfs_free_path(path); | |
2513 | return ret; | |
2514 | } | |
2515 | ||
ac8e9819 CM |
2516 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, |
2517 | void __user *argp) | |
2518 | { | |
bece2e82 BVA |
2519 | struct btrfs_ioctl_ino_lookup_args *args; |
2520 | struct inode *inode; | |
01b810b8 | 2521 | int ret = 0; |
ac8e9819 | 2522 | |
2354d08f JL |
2523 | args = memdup_user(argp, sizeof(*args)); |
2524 | if (IS_ERR(args)) | |
2525 | return PTR_ERR(args); | |
c2b96929 | 2526 | |
496ad9aa | 2527 | inode = file_inode(file); |
ac8e9819 | 2528 | |
01b810b8 DS |
2529 | /* |
2530 | * Unprivileged query to obtain the containing subvolume root id. The | |
2531 | * path is reset so it's consistent with btrfs_search_path_in_tree. | |
2532 | */ | |
1b53ac4d CM |
2533 | if (args->treeid == 0) |
2534 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; | |
2535 | ||
01b810b8 DS |
2536 | if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { |
2537 | args->name[0] = 0; | |
2538 | goto out; | |
2539 | } | |
2540 | ||
2541 | if (!capable(CAP_SYS_ADMIN)) { | |
2542 | ret = -EPERM; | |
2543 | goto out; | |
2544 | } | |
2545 | ||
ac8e9819 CM |
2546 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
2547 | args->treeid, args->objectid, | |
2548 | args->name); | |
2549 | ||
01b810b8 | 2550 | out: |
ac8e9819 CM |
2551 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
2552 | ret = -EFAULT; | |
2553 | ||
2554 | kfree(args); | |
98d377a0 TH |
2555 | return ret; |
2556 | } | |
2557 | ||
23d0b79d TM |
2558 | /* |
2559 | * Version of ino_lookup ioctl (unprivileged) | |
2560 | * | |
2561 | * The main differences from ino_lookup ioctl are: | |
2562 | * | |
2563 | * 1. Read + Exec permission will be checked using inode_permission() during | |
2564 | * path construction. -EACCES will be returned in case of failure. | |
2565 | * 2. Path construction will be stopped at the inode number which corresponds | |
2566 | * to the fd with which this ioctl is called. If constructed path does not | |
2567 | * exist under fd's inode, -EACCES will be returned. | |
2568 | * 3. The name of bottom subvolume is also searched and filled. | |
2569 | */ | |
2570 | static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) | |
2571 | { | |
2572 | struct btrfs_ioctl_ino_lookup_user_args *args; | |
2573 | struct inode *inode; | |
2574 | int ret; | |
2575 | ||
2576 | args = memdup_user(argp, sizeof(*args)); | |
2577 | if (IS_ERR(args)) | |
2578 | return PTR_ERR(args); | |
2579 | ||
2580 | inode = file_inode(file); | |
2581 | ||
2582 | if (args->dirid == BTRFS_FIRST_FREE_OBJECTID && | |
2583 | BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) { | |
2584 | /* | |
2585 | * The subvolume does not exist under fd with which this is | |
2586 | * called | |
2587 | */ | |
2588 | kfree(args); | |
2589 | return -EACCES; | |
2590 | } | |
2591 | ||
2592 | ret = btrfs_search_path_in_tree_user(inode, args); | |
2593 | ||
2594 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) | |
2595 | ret = -EFAULT; | |
2596 | ||
2597 | kfree(args); | |
2598 | return ret; | |
2599 | } | |
2600 | ||
b64ec075 TM |
2601 | /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ |
2602 | static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) | |
2603 | { | |
2604 | struct btrfs_ioctl_get_subvol_info_args *subvol_info; | |
2605 | struct btrfs_fs_info *fs_info; | |
2606 | struct btrfs_root *root; | |
2607 | struct btrfs_path *path; | |
2608 | struct btrfs_key key; | |
2609 | struct btrfs_root_item *root_item; | |
2610 | struct btrfs_root_ref *rref; | |
2611 | struct extent_buffer *leaf; | |
2612 | unsigned long item_off; | |
2613 | unsigned long item_len; | |
2614 | struct inode *inode; | |
2615 | int slot; | |
2616 | int ret = 0; | |
2617 | ||
2618 | path = btrfs_alloc_path(); | |
2619 | if (!path) | |
2620 | return -ENOMEM; | |
2621 | ||
2622 | subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL); | |
2623 | if (!subvol_info) { | |
2624 | btrfs_free_path(path); | |
2625 | return -ENOMEM; | |
2626 | } | |
2627 | ||
2628 | inode = file_inode(file); | |
2629 | fs_info = BTRFS_I(inode)->root->fs_info; | |
2630 | ||
2631 | /* Get root_item of inode's subvolume */ | |
2632 | key.objectid = BTRFS_I(inode)->root->root_key.objectid; | |
56e9357a | 2633 | root = btrfs_get_fs_root(fs_info, key.objectid, true); |
b64ec075 TM |
2634 | if (IS_ERR(root)) { |
2635 | ret = PTR_ERR(root); | |
04734e84 JB |
2636 | goto out_free; |
2637 | } | |
b64ec075 TM |
2638 | root_item = &root->root_item; |
2639 | ||
2640 | subvol_info->treeid = key.objectid; | |
2641 | ||
2642 | subvol_info->generation = btrfs_root_generation(root_item); | |
2643 | subvol_info->flags = btrfs_root_flags(root_item); | |
2644 | ||
2645 | memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE); | |
2646 | memcpy(subvol_info->parent_uuid, root_item->parent_uuid, | |
2647 | BTRFS_UUID_SIZE); | |
2648 | memcpy(subvol_info->received_uuid, root_item->received_uuid, | |
2649 | BTRFS_UUID_SIZE); | |
2650 | ||
2651 | subvol_info->ctransid = btrfs_root_ctransid(root_item); | |
2652 | subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime); | |
2653 | subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime); | |
2654 | ||
2655 | subvol_info->otransid = btrfs_root_otransid(root_item); | |
2656 | subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime); | |
2657 | subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime); | |
2658 | ||
2659 | subvol_info->stransid = btrfs_root_stransid(root_item); | |
2660 | subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime); | |
2661 | subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime); | |
2662 | ||
2663 | subvol_info->rtransid = btrfs_root_rtransid(root_item); | |
2664 | subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime); | |
2665 | subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime); | |
2666 | ||
2667 | if (key.objectid != BTRFS_FS_TREE_OBJECTID) { | |
2668 | /* Search root tree for ROOT_BACKREF of this subvolume */ | |
b64ec075 TM |
2669 | key.type = BTRFS_ROOT_BACKREF_KEY; |
2670 | key.offset = 0; | |
04734e84 | 2671 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
b64ec075 TM |
2672 | if (ret < 0) { |
2673 | goto out; | |
2674 | } else if (path->slots[0] >= | |
2675 | btrfs_header_nritems(path->nodes[0])) { | |
04734e84 | 2676 | ret = btrfs_next_leaf(fs_info->tree_root, path); |
b64ec075 TM |
2677 | if (ret < 0) { |
2678 | goto out; | |
2679 | } else if (ret > 0) { | |
2680 | ret = -EUCLEAN; | |
2681 | goto out; | |
2682 | } | |
2683 | } | |
2684 | ||
2685 | leaf = path->nodes[0]; | |
2686 | slot = path->slots[0]; | |
2687 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2688 | if (key.objectid == subvol_info->treeid && | |
2689 | key.type == BTRFS_ROOT_BACKREF_KEY) { | |
2690 | subvol_info->parent_id = key.offset; | |
2691 | ||
2692 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); | |
2693 | subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref); | |
2694 | ||
2695 | item_off = btrfs_item_ptr_offset(leaf, slot) | |
2696 | + sizeof(struct btrfs_root_ref); | |
2697 | item_len = btrfs_item_size_nr(leaf, slot) | |
2698 | - sizeof(struct btrfs_root_ref); | |
2699 | read_extent_buffer(leaf, subvol_info->name, | |
2700 | item_off, item_len); | |
2701 | } else { | |
2702 | ret = -ENOENT; | |
2703 | goto out; | |
2704 | } | |
2705 | } | |
2706 | ||
2707 | if (copy_to_user(argp, subvol_info, sizeof(*subvol_info))) | |
2708 | ret = -EFAULT; | |
2709 | ||
2710 | out: | |
00246528 | 2711 | btrfs_put_root(root); |
04734e84 | 2712 | out_free: |
b64ec075 | 2713 | btrfs_free_path(path); |
b091f7fe | 2714 | kfree(subvol_info); |
b64ec075 TM |
2715 | return ret; |
2716 | } | |
2717 | ||
42e4b520 TM |
2718 | /* |
2719 | * Return ROOT_REF information of the subvolume containing this inode | |
2720 | * except the subvolume name. | |
2721 | */ | |
2722 | static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp) | |
2723 | { | |
2724 | struct btrfs_ioctl_get_subvol_rootref_args *rootrefs; | |
2725 | struct btrfs_root_ref *rref; | |
2726 | struct btrfs_root *root; | |
2727 | struct btrfs_path *path; | |
2728 | struct btrfs_key key; | |
2729 | struct extent_buffer *leaf; | |
2730 | struct inode *inode; | |
2731 | u64 objectid; | |
2732 | int slot; | |
2733 | int ret; | |
2734 | u8 found; | |
2735 | ||
2736 | path = btrfs_alloc_path(); | |
2737 | if (!path) | |
2738 | return -ENOMEM; | |
2739 | ||
2740 | rootrefs = memdup_user(argp, sizeof(*rootrefs)); | |
2741 | if (IS_ERR(rootrefs)) { | |
2742 | btrfs_free_path(path); | |
2743 | return PTR_ERR(rootrefs); | |
2744 | } | |
2745 | ||
2746 | inode = file_inode(file); | |
2747 | root = BTRFS_I(inode)->root->fs_info->tree_root; | |
2748 | objectid = BTRFS_I(inode)->root->root_key.objectid; | |
2749 | ||
2750 | key.objectid = objectid; | |
2751 | key.type = BTRFS_ROOT_REF_KEY; | |
2752 | key.offset = rootrefs->min_treeid; | |
2753 | found = 0; | |
2754 | ||
2755 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2756 | if (ret < 0) { | |
2757 | goto out; | |
2758 | } else if (path->slots[0] >= | |
2759 | btrfs_header_nritems(path->nodes[0])) { | |
2760 | ret = btrfs_next_leaf(root, path); | |
2761 | if (ret < 0) { | |
2762 | goto out; | |
2763 | } else if (ret > 0) { | |
2764 | ret = -EUCLEAN; | |
2765 | goto out; | |
2766 | } | |
2767 | } | |
2768 | while (1) { | |
2769 | leaf = path->nodes[0]; | |
2770 | slot = path->slots[0]; | |
2771 | ||
2772 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2773 | if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) { | |
2774 | ret = 0; | |
2775 | goto out; | |
2776 | } | |
2777 | ||
2778 | if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { | |
2779 | ret = -EOVERFLOW; | |
2780 | goto out; | |
2781 | } | |
2782 | ||
2783 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); | |
2784 | rootrefs->rootref[found].treeid = key.offset; | |
2785 | rootrefs->rootref[found].dirid = | |
2786 | btrfs_root_ref_dirid(leaf, rref); | |
2787 | found++; | |
2788 | ||
2789 | ret = btrfs_next_item(root, path); | |
2790 | if (ret < 0) { | |
2791 | goto out; | |
2792 | } else if (ret > 0) { | |
2793 | ret = -EUCLEAN; | |
2794 | goto out; | |
2795 | } | |
2796 | } | |
2797 | ||
2798 | out: | |
2799 | if (!ret || ret == -EOVERFLOW) { | |
2800 | rootrefs->num_items = found; | |
2801 | /* update min_treeid for next search */ | |
2802 | if (found) | |
2803 | rootrefs->min_treeid = | |
2804 | rootrefs->rootref[found - 1].treeid + 1; | |
2805 | if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) | |
2806 | ret = -EFAULT; | |
2807 | } | |
2808 | ||
2809 | kfree(rootrefs); | |
2810 | btrfs_free_path(path); | |
2811 | ||
2812 | return ret; | |
2813 | } | |
2814 | ||
76dda93c | 2815 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
949964c9 MPS |
2816 | void __user *arg, |
2817 | bool destroy_v2) | |
76dda93c | 2818 | { |
54563d41 | 2819 | struct dentry *parent = file->f_path.dentry; |
0b246afa | 2820 | struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); |
76dda93c | 2821 | struct dentry *dentry; |
2b0143b5 | 2822 | struct inode *dir = d_inode(parent); |
76dda93c YZ |
2823 | struct inode *inode; |
2824 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
2825 | struct btrfs_root *dest = NULL; | |
949964c9 MPS |
2826 | struct btrfs_ioctl_vol_args *vol_args = NULL; |
2827 | struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL; | |
2828 | char *subvol_name, *subvol_name_ptr = NULL; | |
2829 | int subvol_namelen; | |
76dda93c | 2830 | int err = 0; |
949964c9 | 2831 | bool destroy_parent = false; |
76dda93c | 2832 | |
949964c9 MPS |
2833 | if (destroy_v2) { |
2834 | vol_args2 = memdup_user(arg, sizeof(*vol_args2)); | |
2835 | if (IS_ERR(vol_args2)) | |
2836 | return PTR_ERR(vol_args2); | |
325c50e3 | 2837 | |
949964c9 MPS |
2838 | if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) { |
2839 | err = -EOPNOTSUPP; | |
2840 | goto out; | |
2841 | } | |
76dda93c | 2842 | |
949964c9 MPS |
2843 | /* |
2844 | * If SPEC_BY_ID is not set, we are looking for the subvolume by | |
2845 | * name, same as v1 currently does. | |
2846 | */ | |
2847 | if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) { | |
2848 | vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0; | |
2849 | subvol_name = vol_args2->name; | |
2850 | ||
2851 | err = mnt_want_write_file(file); | |
2852 | if (err) | |
2853 | goto out; | |
2854 | } else { | |
2855 | if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) { | |
2856 | err = -EINVAL; | |
2857 | goto out; | |
2858 | } | |
2859 | ||
2860 | err = mnt_want_write_file(file); | |
2861 | if (err) | |
2862 | goto out; | |
2863 | ||
2864 | dentry = btrfs_get_dentry(fs_info->sb, | |
2865 | BTRFS_FIRST_FREE_OBJECTID, | |
2866 | vol_args2->subvolid, 0, 0); | |
2867 | if (IS_ERR(dentry)) { | |
2868 | err = PTR_ERR(dentry); | |
2869 | goto out_drop_write; | |
2870 | } | |
2871 | ||
2872 | /* | |
2873 | * Change the default parent since the subvolume being | |
2874 | * deleted can be outside of the current mount point. | |
2875 | */ | |
2876 | parent = btrfs_get_parent(dentry); | |
2877 | ||
2878 | /* | |
2879 | * At this point dentry->d_name can point to '/' if the | |
2880 | * subvolume we want to destroy is outsite of the | |
2881 | * current mount point, so we need to release the | |
2882 | * current dentry and execute the lookup to return a new | |
2883 | * one with ->d_name pointing to the | |
2884 | * <mount point>/subvol_name. | |
2885 | */ | |
2886 | dput(dentry); | |
2887 | if (IS_ERR(parent)) { | |
2888 | err = PTR_ERR(parent); | |
2889 | goto out_drop_write; | |
2890 | } | |
2891 | dir = d_inode(parent); | |
2892 | ||
2893 | /* | |
2894 | * If v2 was used with SPEC_BY_ID, a new parent was | |
2895 | * allocated since the subvolume can be outside of the | |
2896 | * current mount point. Later on we need to release this | |
2897 | * new parent dentry. | |
2898 | */ | |
2899 | destroy_parent = true; | |
2900 | ||
2901 | subvol_name_ptr = btrfs_get_subvol_name_from_objectid( | |
2902 | fs_info, vol_args2->subvolid); | |
2903 | if (IS_ERR(subvol_name_ptr)) { | |
2904 | err = PTR_ERR(subvol_name_ptr); | |
2905 | goto free_parent; | |
2906 | } | |
2907 | /* subvol_name_ptr is already NULL termined */ | |
2908 | subvol_name = (char *)kbasename(subvol_name_ptr); | |
2909 | } | |
2910 | } else { | |
2911 | vol_args = memdup_user(arg, sizeof(*vol_args)); | |
2912 | if (IS_ERR(vol_args)) | |
2913 | return PTR_ERR(vol_args); | |
2914 | ||
2915 | vol_args->name[BTRFS_PATH_NAME_MAX] = 0; | |
2916 | subvol_name = vol_args->name; | |
2917 | ||
2918 | err = mnt_want_write_file(file); | |
2919 | if (err) | |
2920 | goto out; | |
76dda93c YZ |
2921 | } |
2922 | ||
949964c9 | 2923 | subvol_namelen = strlen(subvol_name); |
76dda93c | 2924 | |
949964c9 MPS |
2925 | if (strchr(subvol_name, '/') || |
2926 | strncmp(subvol_name, "..", subvol_namelen) == 0) { | |
2927 | err = -EINVAL; | |
2928 | goto free_subvol_name; | |
2929 | } | |
2930 | ||
2931 | if (!S_ISDIR(dir->i_mode)) { | |
2932 | err = -ENOTDIR; | |
2933 | goto free_subvol_name; | |
2934 | } | |
521e0546 | 2935 | |
00235411 AV |
2936 | err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
2937 | if (err == -EINTR) | |
949964c9 MPS |
2938 | goto free_subvol_name; |
2939 | dentry = lookup_one_len(subvol_name, parent, subvol_namelen); | |
76dda93c YZ |
2940 | if (IS_ERR(dentry)) { |
2941 | err = PTR_ERR(dentry); | |
2942 | goto out_unlock_dir; | |
2943 | } | |
2944 | ||
2b0143b5 | 2945 | if (d_really_is_negative(dentry)) { |
76dda93c YZ |
2946 | err = -ENOENT; |
2947 | goto out_dput; | |
2948 | } | |
2949 | ||
2b0143b5 | 2950 | inode = d_inode(dentry); |
4260f7c7 | 2951 | dest = BTRFS_I(inode)->root; |
67871254 | 2952 | if (!capable(CAP_SYS_ADMIN)) { |
4260f7c7 SW |
2953 | /* |
2954 | * Regular user. Only allow this with a special mount | |
2955 | * option, when the user has write+exec access to the | |
2956 | * subvol root, and when rmdir(2) would have been | |
2957 | * allowed. | |
2958 | * | |
2959 | * Note that this is _not_ check that the subvol is | |
2960 | * empty or doesn't contain data that we wouldn't | |
2961 | * otherwise be able to delete. | |
2962 | * | |
2963 | * Users who want to delete empty subvols should try | |
2964 | * rmdir(2). | |
2965 | */ | |
2966 | err = -EPERM; | |
0b246afa | 2967 | if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) |
4260f7c7 SW |
2968 | goto out_dput; |
2969 | ||
2970 | /* | |
2971 | * Do not allow deletion if the parent dir is the same | |
2972 | * as the dir to be deleted. That means the ioctl | |
2973 | * must be called on the dentry referencing the root | |
2974 | * of the subvol, not a random directory contained | |
2975 | * within it. | |
2976 | */ | |
2977 | err = -EINVAL; | |
2978 | if (root == dest) | |
2979 | goto out_dput; | |
2980 | ||
2981 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); | |
2982 | if (err) | |
2983 | goto out_dput; | |
4260f7c7 SW |
2984 | } |
2985 | ||
5c39da5b MX |
2986 | /* check if subvolume may be deleted by a user */ |
2987 | err = btrfs_may_delete(dir, dentry, 1); | |
2988 | if (err) | |
2989 | goto out_dput; | |
2990 | ||
4a0cc7ca | 2991 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
76dda93c YZ |
2992 | err = -EINVAL; |
2993 | goto out_dput; | |
2994 | } | |
2995 | ||
5955102c | 2996 | inode_lock(inode); |
f60a2364 | 2997 | err = btrfs_delete_subvolume(dir, dentry); |
5955102c | 2998 | inode_unlock(inode); |
46008d9d AG |
2999 | if (!err) { |
3000 | fsnotify_rmdir(dir, dentry); | |
76dda93c | 3001 | d_delete(dentry); |
46008d9d | 3002 | } |
fa6ac876 | 3003 | |
76dda93c YZ |
3004 | out_dput: |
3005 | dput(dentry); | |
3006 | out_unlock_dir: | |
5955102c | 3007 | inode_unlock(dir); |
949964c9 MPS |
3008 | free_subvol_name: |
3009 | kfree(subvol_name_ptr); | |
3010 | free_parent: | |
3011 | if (destroy_parent) | |
3012 | dput(parent); | |
00235411 | 3013 | out_drop_write: |
2a79f17e | 3014 | mnt_drop_write_file(file); |
76dda93c | 3015 | out: |
949964c9 | 3016 | kfree(vol_args2); |
76dda93c YZ |
3017 | kfree(vol_args); |
3018 | return err; | |
3019 | } | |
3020 | ||
1e701a32 | 3021 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
f46b5a66 | 3022 | { |
496ad9aa | 3023 | struct inode *inode = file_inode(file); |
f46b5a66 | 3024 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1e701a32 | 3025 | struct btrfs_ioctl_defrag_range_args *range; |
c146afad YZ |
3026 | int ret; |
3027 | ||
25122d15 ID |
3028 | ret = mnt_want_write_file(file); |
3029 | if (ret) | |
3030 | return ret; | |
b83cc969 | 3031 | |
25122d15 ID |
3032 | if (btrfs_root_readonly(root)) { |
3033 | ret = -EROFS; | |
3034 | goto out; | |
5ac00add | 3035 | } |
f46b5a66 CH |
3036 | |
3037 | switch (inode->i_mode & S_IFMT) { | |
3038 | case S_IFDIR: | |
e441d54d CM |
3039 | if (!capable(CAP_SYS_ADMIN)) { |
3040 | ret = -EPERM; | |
3041 | goto out; | |
3042 | } | |
de78b51a | 3043 | ret = btrfs_defrag_root(root); |
f46b5a66 CH |
3044 | break; |
3045 | case S_IFREG: | |
616d374e AB |
3046 | /* |
3047 | * Note that this does not check the file descriptor for write | |
3048 | * access. This prevents defragmenting executables that are | |
3049 | * running and allows defrag on files open in read-only mode. | |
3050 | */ | |
3051 | if (!capable(CAP_SYS_ADMIN) && | |
3052 | inode_permission(inode, MAY_WRITE)) { | |
3053 | ret = -EPERM; | |
e441d54d CM |
3054 | goto out; |
3055 | } | |
1e701a32 CM |
3056 | |
3057 | range = kzalloc(sizeof(*range), GFP_KERNEL); | |
3058 | if (!range) { | |
3059 | ret = -ENOMEM; | |
3060 | goto out; | |
3061 | } | |
3062 | ||
3063 | if (argp) { | |
3064 | if (copy_from_user(range, argp, | |
3065 | sizeof(*range))) { | |
3066 | ret = -EFAULT; | |
3067 | kfree(range); | |
683be16e | 3068 | goto out; |
1e701a32 CM |
3069 | } |
3070 | /* compression requires us to start the IO */ | |
3071 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { | |
3072 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; | |
3073 | range->extent_thresh = (u32)-1; | |
3074 | } | |
3075 | } else { | |
3076 | /* the rest are all set to zero by kzalloc */ | |
3077 | range->len = (u64)-1; | |
3078 | } | |
496ad9aa | 3079 | ret = btrfs_defrag_file(file_inode(file), file, |
7c829b72 | 3080 | range, BTRFS_OLDEST_GENERATION, 0); |
4cb5300b CM |
3081 | if (ret > 0) |
3082 | ret = 0; | |
1e701a32 | 3083 | kfree(range); |
f46b5a66 | 3084 | break; |
8929ecfa YZ |
3085 | default: |
3086 | ret = -EINVAL; | |
f46b5a66 | 3087 | } |
e441d54d | 3088 | out: |
25122d15 | 3089 | mnt_drop_write_file(file); |
e441d54d | 3090 | return ret; |
f46b5a66 CH |
3091 | } |
3092 | ||
2ff7e61e | 3093 | static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) |
f46b5a66 CH |
3094 | { |
3095 | struct btrfs_ioctl_vol_args *vol_args; | |
3096 | int ret; | |
3097 | ||
e441d54d CM |
3098 | if (!capable(CAP_SYS_ADMIN)) |
3099 | return -EPERM; | |
3100 | ||
171938e5 | 3101 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
e57138b3 | 3102 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
c9e9f97b | 3103 | |
dae7b665 | 3104 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
3105 | if (IS_ERR(vol_args)) { |
3106 | ret = PTR_ERR(vol_args); | |
3107 | goto out; | |
3108 | } | |
f46b5a66 | 3109 | |
5516e595 | 3110 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
2ff7e61e | 3111 | ret = btrfs_init_new_device(fs_info, vol_args->name); |
f46b5a66 | 3112 | |
43d20761 | 3113 | if (!ret) |
0b246afa | 3114 | btrfs_info(fs_info, "disk added %s", vol_args->name); |
43d20761 | 3115 | |
f46b5a66 | 3116 | kfree(vol_args); |
c9e9f97b | 3117 | out: |
171938e5 | 3118 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
f46b5a66 CH |
3119 | return ret; |
3120 | } | |
3121 | ||
6b526ed7 | 3122 | static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) |
f46b5a66 | 3123 | { |
0b246afa JM |
3124 | struct inode *inode = file_inode(file); |
3125 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
6b526ed7 | 3126 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
f46b5a66 CH |
3127 | int ret; |
3128 | ||
e441d54d CM |
3129 | if (!capable(CAP_SYS_ADMIN)) |
3130 | return -EPERM; | |
3131 | ||
da24927b MX |
3132 | ret = mnt_want_write_file(file); |
3133 | if (ret) | |
3134 | return ret; | |
c146afad | 3135 | |
dae7b665 | 3136 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
3137 | if (IS_ERR(vol_args)) { |
3138 | ret = PTR_ERR(vol_args); | |
c47ca32d | 3139 | goto err_drop; |
c9e9f97b | 3140 | } |
f46b5a66 | 3141 | |
748449cd | 3142 | if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) { |
fd4e994b OS |
3143 | ret = -EOPNOTSUPP; |
3144 | goto out; | |
3145 | } | |
f46b5a66 | 3146 | |
171938e5 | 3147 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
183860f6 AJ |
3148 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
3149 | goto out; | |
3150 | } | |
3151 | ||
735654ea | 3152 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { |
2ff7e61e | 3153 | ret = btrfs_rm_device(fs_info, NULL, vol_args->devid); |
6b526ed7 AJ |
3154 | } else { |
3155 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; | |
2ff7e61e | 3156 | ret = btrfs_rm_device(fs_info, vol_args->name, 0); |
6b526ed7 | 3157 | } |
171938e5 | 3158 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
183860f6 | 3159 | |
6b526ed7 | 3160 | if (!ret) { |
735654ea | 3161 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) |
0b246afa | 3162 | btrfs_info(fs_info, "device deleted: id %llu", |
6b526ed7 AJ |
3163 | vol_args->devid); |
3164 | else | |
0b246afa | 3165 | btrfs_info(fs_info, "device deleted: %s", |
6b526ed7 AJ |
3166 | vol_args->name); |
3167 | } | |
183860f6 AJ |
3168 | out: |
3169 | kfree(vol_args); | |
c47ca32d | 3170 | err_drop: |
4ac20c70 | 3171 | mnt_drop_write_file(file); |
f46b5a66 CH |
3172 | return ret; |
3173 | } | |
3174 | ||
da24927b | 3175 | static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) |
f46b5a66 | 3176 | { |
0b246afa JM |
3177 | struct inode *inode = file_inode(file); |
3178 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
f46b5a66 CH |
3179 | struct btrfs_ioctl_vol_args *vol_args; |
3180 | int ret; | |
3181 | ||
e441d54d CM |
3182 | if (!capable(CAP_SYS_ADMIN)) |
3183 | return -EPERM; | |
3184 | ||
da24927b MX |
3185 | ret = mnt_want_write_file(file); |
3186 | if (ret) | |
3187 | return ret; | |
c146afad | 3188 | |
171938e5 | 3189 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
183860f6 | 3190 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
58d7bbf8 DS |
3191 | goto out_drop_write; |
3192 | } | |
3193 | ||
3194 | vol_args = memdup_user(arg, sizeof(*vol_args)); | |
3195 | if (IS_ERR(vol_args)) { | |
3196 | ret = PTR_ERR(vol_args); | |
183860f6 AJ |
3197 | goto out; |
3198 | } | |
3199 | ||
58d7bbf8 | 3200 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
2ff7e61e | 3201 | ret = btrfs_rm_device(fs_info, vol_args->name, 0); |
183860f6 | 3202 | |
ec95d491 | 3203 | if (!ret) |
0b246afa | 3204 | btrfs_info(fs_info, "disk deleted %s", vol_args->name); |
183860f6 | 3205 | kfree(vol_args); |
58d7bbf8 | 3206 | out: |
171938e5 | 3207 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
58d7bbf8 | 3208 | out_drop_write: |
4ac20c70 | 3209 | mnt_drop_write_file(file); |
58d7bbf8 | 3210 | |
f46b5a66 CH |
3211 | return ret; |
3212 | } | |
3213 | ||
2ff7e61e JM |
3214 | static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, |
3215 | void __user *arg) | |
475f6387 | 3216 | { |
027ed2f0 | 3217 | struct btrfs_ioctl_fs_info_args *fi_args; |
475f6387 | 3218 | struct btrfs_device *device; |
0b246afa | 3219 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
027ed2f0 | 3220 | int ret = 0; |
475f6387 | 3221 | |
027ed2f0 LZ |
3222 | fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); |
3223 | if (!fi_args) | |
3224 | return -ENOMEM; | |
3225 | ||
d03262c7 | 3226 | rcu_read_lock(); |
027ed2f0 | 3227 | fi_args->num_devices = fs_devices->num_devices; |
475f6387 | 3228 | |
d03262c7 | 3229 | list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { |
027ed2f0 LZ |
3230 | if (device->devid > fi_args->max_id) |
3231 | fi_args->max_id = device->devid; | |
475f6387 | 3232 | } |
d03262c7 | 3233 | rcu_read_unlock(); |
475f6387 | 3234 | |
de37aa51 | 3235 | memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid)); |
bea7eafd OS |
3236 | fi_args->nodesize = fs_info->nodesize; |
3237 | fi_args->sectorsize = fs_info->sectorsize; | |
3238 | fi_args->clone_alignment = fs_info->sectorsize; | |
80a773fb | 3239 | |
027ed2f0 LZ |
3240 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) |
3241 | ret = -EFAULT; | |
475f6387 | 3242 | |
027ed2f0 LZ |
3243 | kfree(fi_args); |
3244 | return ret; | |
475f6387 JS |
3245 | } |
3246 | ||
2ff7e61e JM |
3247 | static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, |
3248 | void __user *arg) | |
475f6387 JS |
3249 | { |
3250 | struct btrfs_ioctl_dev_info_args *di_args; | |
3251 | struct btrfs_device *dev; | |
475f6387 JS |
3252 | int ret = 0; |
3253 | char *s_uuid = NULL; | |
475f6387 | 3254 | |
475f6387 JS |
3255 | di_args = memdup_user(arg, sizeof(*di_args)); |
3256 | if (IS_ERR(di_args)) | |
3257 | return PTR_ERR(di_args); | |
3258 | ||
dd5f9615 | 3259 | if (!btrfs_is_empty_uuid(di_args->uuid)) |
475f6387 JS |
3260 | s_uuid = di_args->uuid; |
3261 | ||
c5593ca3 | 3262 | rcu_read_lock(); |
e4319cd9 | 3263 | dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid, |
09ba3bc9 | 3264 | NULL, true); |
475f6387 JS |
3265 | |
3266 | if (!dev) { | |
3267 | ret = -ENODEV; | |
3268 | goto out; | |
3269 | } | |
3270 | ||
3271 | di_args->devid = dev->devid; | |
7cc8e58d MX |
3272 | di_args->bytes_used = btrfs_device_get_bytes_used(dev); |
3273 | di_args->total_bytes = btrfs_device_get_total_bytes(dev); | |
475f6387 | 3274 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); |
a27202fb | 3275 | if (dev->name) { |
672d5990 MT |
3276 | strncpy(di_args->path, rcu_str_deref(dev->name), |
3277 | sizeof(di_args->path) - 1); | |
a27202fb JM |
3278 | di_args->path[sizeof(di_args->path) - 1] = 0; |
3279 | } else { | |
99ba55ad | 3280 | di_args->path[0] = '\0'; |
a27202fb | 3281 | } |
475f6387 JS |
3282 | |
3283 | out: | |
c5593ca3 | 3284 | rcu_read_unlock(); |
475f6387 JS |
3285 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) |
3286 | ret = -EFAULT; | |
3287 | ||
3288 | kfree(di_args); | |
3289 | return ret; | |
3290 | } | |
3291 | ||
6ef5ed0d JB |
3292 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
3293 | { | |
496ad9aa | 3294 | struct inode *inode = file_inode(file); |
0b246afa | 3295 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
6ef5ed0d JB |
3296 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3297 | struct btrfs_root *new_root; | |
3298 | struct btrfs_dir_item *di; | |
3299 | struct btrfs_trans_handle *trans; | |
2a2b5d62 | 3300 | struct btrfs_path *path = NULL; |
6ef5ed0d | 3301 | struct btrfs_disk_key disk_key; |
6ef5ed0d JB |
3302 | u64 objectid = 0; |
3303 | u64 dir_id; | |
3c04ce01 | 3304 | int ret; |
6ef5ed0d JB |
3305 | |
3306 | if (!capable(CAP_SYS_ADMIN)) | |
3307 | return -EPERM; | |
3308 | ||
3c04ce01 MX |
3309 | ret = mnt_want_write_file(file); |
3310 | if (ret) | |
3311 | return ret; | |
3312 | ||
3313 | if (copy_from_user(&objectid, argp, sizeof(objectid))) { | |
3314 | ret = -EFAULT; | |
3315 | goto out; | |
3316 | } | |
6ef5ed0d JB |
3317 | |
3318 | if (!objectid) | |
1cecf579 | 3319 | objectid = BTRFS_FS_TREE_OBJECTID; |
6ef5ed0d | 3320 | |
56e9357a | 3321 | new_root = btrfs_get_fs_root(fs_info, objectid, true); |
3c04ce01 MX |
3322 | if (IS_ERR(new_root)) { |
3323 | ret = PTR_ERR(new_root); | |
3324 | goto out; | |
3325 | } | |
2a2b5d62 JB |
3326 | if (!is_fstree(new_root->root_key.objectid)) { |
3327 | ret = -ENOENT; | |
3328 | goto out_free; | |
3329 | } | |
6ef5ed0d | 3330 | |
6ef5ed0d | 3331 | path = btrfs_alloc_path(); |
3c04ce01 MX |
3332 | if (!path) { |
3333 | ret = -ENOMEM; | |
2a2b5d62 | 3334 | goto out_free; |
3c04ce01 | 3335 | } |
6ef5ed0d JB |
3336 | path->leave_spinning = 1; |
3337 | ||
3338 | trans = btrfs_start_transaction(root, 1); | |
98d5dc13 | 3339 | if (IS_ERR(trans)) { |
3c04ce01 | 3340 | ret = PTR_ERR(trans); |
2a2b5d62 | 3341 | goto out_free; |
6ef5ed0d JB |
3342 | } |
3343 | ||
0b246afa JM |
3344 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
3345 | di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, | |
6ef5ed0d | 3346 | dir_id, "default", 7, 1); |
cf1e99a4 | 3347 | if (IS_ERR_OR_NULL(di)) { |
2a2b5d62 | 3348 | btrfs_release_path(path); |
3a45bb20 | 3349 | btrfs_end_transaction(trans); |
0b246afa | 3350 | btrfs_err(fs_info, |
5d163e0e | 3351 | "Umm, you don't have the default diritem, this isn't going to work"); |
3c04ce01 | 3352 | ret = -ENOENT; |
2a2b5d62 | 3353 | goto out_free; |
6ef5ed0d JB |
3354 | } |
3355 | ||
3356 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); | |
3357 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); | |
3358 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
2a2b5d62 | 3359 | btrfs_release_path(path); |
6ef5ed0d | 3360 | |
0b246afa | 3361 | btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); |
3a45bb20 | 3362 | btrfs_end_transaction(trans); |
2a2b5d62 | 3363 | out_free: |
00246528 | 3364 | btrfs_put_root(new_root); |
2a2b5d62 | 3365 | btrfs_free_path(path); |
3c04ce01 MX |
3366 | out: |
3367 | mnt_drop_write_file(file); | |
3368 | return ret; | |
6ef5ed0d JB |
3369 | } |
3370 | ||
c065f5b1 SY |
3371 | static void get_block_group_info(struct list_head *groups_list, |
3372 | struct btrfs_ioctl_space_info *space) | |
bf5fc093 | 3373 | { |
32da5386 | 3374 | struct btrfs_block_group *block_group; |
bf5fc093 JB |
3375 | |
3376 | space->total_bytes = 0; | |
3377 | space->used_bytes = 0; | |
3378 | space->flags = 0; | |
3379 | list_for_each_entry(block_group, groups_list, list) { | |
3380 | space->flags = block_group->flags; | |
b3470b5d | 3381 | space->total_bytes += block_group->length; |
bf38be65 | 3382 | space->used_bytes += block_group->used; |
bf5fc093 JB |
3383 | } |
3384 | } | |
3385 | ||
2ff7e61e JM |
3386 | static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, |
3387 | void __user *arg) | |
1406e432 JB |
3388 | { |
3389 | struct btrfs_ioctl_space_args space_args; | |
3390 | struct btrfs_ioctl_space_info space; | |
3391 | struct btrfs_ioctl_space_info *dest; | |
7fde62bf | 3392 | struct btrfs_ioctl_space_info *dest_orig; |
13f2696f | 3393 | struct btrfs_ioctl_space_info __user *user_dest; |
1406e432 | 3394 | struct btrfs_space_info *info; |
315d8e98 CIK |
3395 | static const u64 types[] = { |
3396 | BTRFS_BLOCK_GROUP_DATA, | |
3397 | BTRFS_BLOCK_GROUP_SYSTEM, | |
3398 | BTRFS_BLOCK_GROUP_METADATA, | |
3399 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA | |
3400 | }; | |
bf5fc093 | 3401 | int num_types = 4; |
7fde62bf | 3402 | int alloc_size; |
1406e432 | 3403 | int ret = 0; |
51788b1b | 3404 | u64 slot_count = 0; |
bf5fc093 | 3405 | int i, c; |
1406e432 JB |
3406 | |
3407 | if (copy_from_user(&space_args, | |
3408 | (struct btrfs_ioctl_space_args __user *)arg, | |
3409 | sizeof(space_args))) | |
3410 | return -EFAULT; | |
3411 | ||
bf5fc093 JB |
3412 | for (i = 0; i < num_types; i++) { |
3413 | struct btrfs_space_info *tmp; | |
3414 | ||
3415 | info = NULL; | |
3416 | rcu_read_lock(); | |
0b246afa | 3417 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
bf5fc093 JB |
3418 | list) { |
3419 | if (tmp->flags == types[i]) { | |
3420 | info = tmp; | |
3421 | break; | |
3422 | } | |
3423 | } | |
3424 | rcu_read_unlock(); | |
3425 | ||
3426 | if (!info) | |
3427 | continue; | |
3428 | ||
3429 | down_read(&info->groups_sem); | |
3430 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
3431 | if (!list_empty(&info->block_groups[c])) | |
3432 | slot_count++; | |
3433 | } | |
3434 | up_read(&info->groups_sem); | |
3435 | } | |
7fde62bf | 3436 | |
36523e95 DS |
3437 | /* |
3438 | * Global block reserve, exported as a space_info | |
3439 | */ | |
3440 | slot_count++; | |
3441 | ||
7fde62bf CM |
3442 | /* space_slots == 0 means they are asking for a count */ |
3443 | if (space_args.space_slots == 0) { | |
3444 | space_args.total_spaces = slot_count; | |
3445 | goto out; | |
3446 | } | |
bf5fc093 | 3447 | |
51788b1b | 3448 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
bf5fc093 | 3449 | |
7fde62bf | 3450 | alloc_size = sizeof(*dest) * slot_count; |
bf5fc093 | 3451 | |
7fde62bf CM |
3452 | /* we generally have at most 6 or so space infos, one for each raid |
3453 | * level. So, a whole page should be more than enough for everyone | |
3454 | */ | |
09cbfeaf | 3455 | if (alloc_size > PAGE_SIZE) |
7fde62bf CM |
3456 | return -ENOMEM; |
3457 | ||
1406e432 | 3458 | space_args.total_spaces = 0; |
8d2db785 | 3459 | dest = kmalloc(alloc_size, GFP_KERNEL); |
7fde62bf CM |
3460 | if (!dest) |
3461 | return -ENOMEM; | |
3462 | dest_orig = dest; | |
1406e432 | 3463 | |
7fde62bf | 3464 | /* now we have a buffer to copy into */ |
bf5fc093 JB |
3465 | for (i = 0; i < num_types; i++) { |
3466 | struct btrfs_space_info *tmp; | |
3467 | ||
51788b1b DR |
3468 | if (!slot_count) |
3469 | break; | |
3470 | ||
bf5fc093 JB |
3471 | info = NULL; |
3472 | rcu_read_lock(); | |
0b246afa | 3473 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
bf5fc093 JB |
3474 | list) { |
3475 | if (tmp->flags == types[i]) { | |
3476 | info = tmp; | |
3477 | break; | |
3478 | } | |
3479 | } | |
3480 | rcu_read_unlock(); | |
7fde62bf | 3481 | |
bf5fc093 JB |
3482 | if (!info) |
3483 | continue; | |
3484 | down_read(&info->groups_sem); | |
3485 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
3486 | if (!list_empty(&info->block_groups[c])) { | |
c065f5b1 SY |
3487 | get_block_group_info(&info->block_groups[c], |
3488 | &space); | |
bf5fc093 JB |
3489 | memcpy(dest, &space, sizeof(space)); |
3490 | dest++; | |
3491 | space_args.total_spaces++; | |
51788b1b | 3492 | slot_count--; |
bf5fc093 | 3493 | } |
51788b1b DR |
3494 | if (!slot_count) |
3495 | break; | |
bf5fc093 JB |
3496 | } |
3497 | up_read(&info->groups_sem); | |
1406e432 | 3498 | } |
1406e432 | 3499 | |
36523e95 DS |
3500 | /* |
3501 | * Add global block reserve | |
3502 | */ | |
3503 | if (slot_count) { | |
0b246afa | 3504 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
36523e95 DS |
3505 | |
3506 | spin_lock(&block_rsv->lock); | |
3507 | space.total_bytes = block_rsv->size; | |
3508 | space.used_bytes = block_rsv->size - block_rsv->reserved; | |
3509 | spin_unlock(&block_rsv->lock); | |
3510 | space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; | |
3511 | memcpy(dest, &space, sizeof(space)); | |
3512 | space_args.total_spaces++; | |
3513 | } | |
3514 | ||
2eec6c81 | 3515 | user_dest = (struct btrfs_ioctl_space_info __user *) |
7fde62bf CM |
3516 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
3517 | ||
3518 | if (copy_to_user(user_dest, dest_orig, alloc_size)) | |
3519 | ret = -EFAULT; | |
3520 | ||
3521 | kfree(dest_orig); | |
3522 | out: | |
3523 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) | |
1406e432 JB |
3524 | ret = -EFAULT; |
3525 | ||
3526 | return ret; | |
3527 | } | |
3528 | ||
9a8c28be MX |
3529 | static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, |
3530 | void __user *argp) | |
46204592 | 3531 | { |
46204592 SW |
3532 | struct btrfs_trans_handle *trans; |
3533 | u64 transid; | |
db5b493a | 3534 | int ret; |
46204592 | 3535 | |
d4edf39b | 3536 | trans = btrfs_attach_transaction_barrier(root); |
ff7c1d33 MX |
3537 | if (IS_ERR(trans)) { |
3538 | if (PTR_ERR(trans) != -ENOENT) | |
3539 | return PTR_ERR(trans); | |
3540 | ||
3541 | /* No running transaction, don't bother */ | |
3542 | transid = root->fs_info->last_trans_committed; | |
3543 | goto out; | |
3544 | } | |
46204592 | 3545 | transid = trans->transid; |
3a45bb20 | 3546 | ret = btrfs_commit_transaction_async(trans, 0); |
8b2b2d3c | 3547 | if (ret) { |
3a45bb20 | 3548 | btrfs_end_transaction(trans); |
db5b493a | 3549 | return ret; |
8b2b2d3c | 3550 | } |
ff7c1d33 | 3551 | out: |
46204592 SW |
3552 | if (argp) |
3553 | if (copy_to_user(argp, &transid, sizeof(transid))) | |
3554 | return -EFAULT; | |
3555 | return 0; | |
3556 | } | |
3557 | ||
2ff7e61e | 3558 | static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, |
9a8c28be | 3559 | void __user *argp) |
46204592 | 3560 | { |
46204592 SW |
3561 | u64 transid; |
3562 | ||
3563 | if (argp) { | |
3564 | if (copy_from_user(&transid, argp, sizeof(transid))) | |
3565 | return -EFAULT; | |
3566 | } else { | |
3567 | transid = 0; /* current trans */ | |
3568 | } | |
2ff7e61e | 3569 | return btrfs_wait_for_commit(fs_info, transid); |
46204592 SW |
3570 | } |
3571 | ||
b8e95489 | 3572 | static long btrfs_ioctl_scrub(struct file *file, void __user *arg) |
475f6387 | 3573 | { |
0b246afa | 3574 | struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); |
475f6387 | 3575 | struct btrfs_ioctl_scrub_args *sa; |
b8e95489 | 3576 | int ret; |
475f6387 JS |
3577 | |
3578 | if (!capable(CAP_SYS_ADMIN)) | |
3579 | return -EPERM; | |
3580 | ||
3581 | sa = memdup_user(arg, sizeof(*sa)); | |
3582 | if (IS_ERR(sa)) | |
3583 | return PTR_ERR(sa); | |
3584 | ||
b8e95489 MX |
3585 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) { |
3586 | ret = mnt_want_write_file(file); | |
3587 | if (ret) | |
3588 | goto out; | |
3589 | } | |
3590 | ||
0b246afa | 3591 | ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, |
63a212ab SB |
3592 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, |
3593 | 0); | |
475f6387 | 3594 | |
5afe6ce7 FM |
3595 | /* |
3596 | * Copy scrub args to user space even if btrfs_scrub_dev() returned an | |
3597 | * error. This is important as it allows user space to know how much | |
3598 | * progress scrub has done. For example, if scrub is canceled we get | |
3599 | * -ECANCELED from btrfs_scrub_dev() and return that error back to user | |
3600 | * space. Later user space can inspect the progress from the structure | |
3601 | * btrfs_ioctl_scrub_args and resume scrub from where it left off | |
3602 | * previously (btrfs-progs does this). | |
3603 | * If we fail to copy the btrfs_ioctl_scrub_args structure to user space | |
3604 | * then return -EFAULT to signal the structure was not copied or it may | |
3605 | * be corrupt and unreliable due to a partial copy. | |
3606 | */ | |
3607 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
475f6387 JS |
3608 | ret = -EFAULT; |
3609 | ||
b8e95489 MX |
3610 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) |
3611 | mnt_drop_write_file(file); | |
3612 | out: | |
475f6387 JS |
3613 | kfree(sa); |
3614 | return ret; | |
3615 | } | |
3616 | ||
2ff7e61e | 3617 | static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) |
475f6387 JS |
3618 | { |
3619 | if (!capable(CAP_SYS_ADMIN)) | |
3620 | return -EPERM; | |
3621 | ||
2ff7e61e | 3622 | return btrfs_scrub_cancel(fs_info); |
475f6387 JS |
3623 | } |
3624 | ||
2ff7e61e | 3625 | static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, |
475f6387 JS |
3626 | void __user *arg) |
3627 | { | |
3628 | struct btrfs_ioctl_scrub_args *sa; | |
3629 | int ret; | |
3630 | ||
3631 | if (!capable(CAP_SYS_ADMIN)) | |
3632 | return -EPERM; | |
3633 | ||
3634 | sa = memdup_user(arg, sizeof(*sa)); | |
3635 | if (IS_ERR(sa)) | |
3636 | return PTR_ERR(sa); | |
3637 | ||
2ff7e61e | 3638 | ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); |
475f6387 | 3639 | |
4fa99b00 | 3640 | if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) |
475f6387 JS |
3641 | ret = -EFAULT; |
3642 | ||
3643 | kfree(sa); | |
3644 | return ret; | |
3645 | } | |
3646 | ||
2ff7e61e | 3647 | static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, |
b27f7c0c | 3648 | void __user *arg) |
c11d2c23 SB |
3649 | { |
3650 | struct btrfs_ioctl_get_dev_stats *sa; | |
3651 | int ret; | |
3652 | ||
c11d2c23 SB |
3653 | sa = memdup_user(arg, sizeof(*sa)); |
3654 | if (IS_ERR(sa)) | |
3655 | return PTR_ERR(sa); | |
3656 | ||
b27f7c0c DS |
3657 | if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { |
3658 | kfree(sa); | |
3659 | return -EPERM; | |
3660 | } | |
3661 | ||
2ff7e61e | 3662 | ret = btrfs_get_dev_stats(fs_info, sa); |
c11d2c23 | 3663 | |
eee99577 | 3664 | if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) |
c11d2c23 SB |
3665 | ret = -EFAULT; |
3666 | ||
3667 | kfree(sa); | |
3668 | return ret; | |
3669 | } | |
3670 | ||
2ff7e61e JM |
3671 | static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, |
3672 | void __user *arg) | |
3f6bcfbd SB |
3673 | { |
3674 | struct btrfs_ioctl_dev_replace_args *p; | |
3675 | int ret; | |
3676 | ||
3677 | if (!capable(CAP_SYS_ADMIN)) | |
3678 | return -EPERM; | |
3679 | ||
3680 | p = memdup_user(arg, sizeof(*p)); | |
3681 | if (IS_ERR(p)) | |
3682 | return PTR_ERR(p); | |
3683 | ||
3684 | switch (p->cmd) { | |
3685 | case BTRFS_IOCTL_DEV_REPLACE_CMD_START: | |
bc98a42c | 3686 | if (sb_rdonly(fs_info->sb)) { |
adfa97cb ID |
3687 | ret = -EROFS; |
3688 | goto out; | |
3689 | } | |
171938e5 | 3690 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
e57138b3 | 3691 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
3f6bcfbd | 3692 | } else { |
2ff7e61e | 3693 | ret = btrfs_dev_replace_by_ioctl(fs_info, p); |
171938e5 | 3694 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
3f6bcfbd SB |
3695 | } |
3696 | break; | |
3697 | case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: | |
0b246afa | 3698 | btrfs_dev_replace_status(fs_info, p); |
3f6bcfbd SB |
3699 | ret = 0; |
3700 | break; | |
3701 | case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: | |
17d202b9 | 3702 | p->result = btrfs_dev_replace_cancel(fs_info); |
97282031 | 3703 | ret = 0; |
3f6bcfbd SB |
3704 | break; |
3705 | default: | |
3706 | ret = -EINVAL; | |
3707 | break; | |
3708 | } | |
3709 | ||
d3a53286 | 3710 | if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p))) |
3f6bcfbd | 3711 | ret = -EFAULT; |
adfa97cb | 3712 | out: |
3f6bcfbd SB |
3713 | kfree(p); |
3714 | return ret; | |
3715 | } | |
3716 | ||
d7728c96 JS |
3717 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
3718 | { | |
3719 | int ret = 0; | |
3720 | int i; | |
740c3d22 | 3721 | u64 rel_ptr; |
d7728c96 | 3722 | int size; |
806468f8 | 3723 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
d7728c96 JS |
3724 | struct inode_fs_paths *ipath = NULL; |
3725 | struct btrfs_path *path; | |
3726 | ||
82b22ac8 | 3727 | if (!capable(CAP_DAC_READ_SEARCH)) |
d7728c96 JS |
3728 | return -EPERM; |
3729 | ||
3730 | path = btrfs_alloc_path(); | |
3731 | if (!path) { | |
3732 | ret = -ENOMEM; | |
3733 | goto out; | |
3734 | } | |
3735 | ||
3736 | ipa = memdup_user(arg, sizeof(*ipa)); | |
3737 | if (IS_ERR(ipa)) { | |
3738 | ret = PTR_ERR(ipa); | |
3739 | ipa = NULL; | |
3740 | goto out; | |
3741 | } | |
3742 | ||
3743 | size = min_t(u32, ipa->size, 4096); | |
3744 | ipath = init_ipath(size, root, path); | |
3745 | if (IS_ERR(ipath)) { | |
3746 | ret = PTR_ERR(ipath); | |
3747 | ipath = NULL; | |
3748 | goto out; | |
3749 | } | |
3750 | ||
3751 | ret = paths_from_inode(ipa->inum, ipath); | |
3752 | if (ret < 0) | |
3753 | goto out; | |
3754 | ||
3755 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { | |
745c4d8e JM |
3756 | rel_ptr = ipath->fspath->val[i] - |
3757 | (u64)(unsigned long)ipath->fspath->val; | |
740c3d22 | 3758 | ipath->fspath->val[i] = rel_ptr; |
d7728c96 JS |
3759 | } |
3760 | ||
718dc5fa OS |
3761 | ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, |
3762 | ipath->fspath, size); | |
d7728c96 JS |
3763 | if (ret) { |
3764 | ret = -EFAULT; | |
3765 | goto out; | |
3766 | } | |
3767 | ||
3768 | out: | |
3769 | btrfs_free_path(path); | |
3770 | free_ipath(ipath); | |
3771 | kfree(ipa); | |
3772 | ||
3773 | return ret; | |
3774 | } | |
3775 | ||
3776 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) | |
3777 | { | |
3778 | struct btrfs_data_container *inodes = ctx; | |
3779 | const size_t c = 3 * sizeof(u64); | |
3780 | ||
3781 | if (inodes->bytes_left >= c) { | |
3782 | inodes->bytes_left -= c; | |
3783 | inodes->val[inodes->elem_cnt] = inum; | |
3784 | inodes->val[inodes->elem_cnt + 1] = offset; | |
3785 | inodes->val[inodes->elem_cnt + 2] = root; | |
3786 | inodes->elem_cnt += 3; | |
3787 | } else { | |
3788 | inodes->bytes_missing += c - inodes->bytes_left; | |
3789 | inodes->bytes_left = 0; | |
3790 | inodes->elem_missed += 3; | |
3791 | } | |
3792 | ||
3793 | return 0; | |
3794 | } | |
3795 | ||
2ff7e61e | 3796 | static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, |
d24a67b2 | 3797 | void __user *arg, int version) |
d7728c96 JS |
3798 | { |
3799 | int ret = 0; | |
3800 | int size; | |
d7728c96 JS |
3801 | struct btrfs_ioctl_logical_ino_args *loi; |
3802 | struct btrfs_data_container *inodes = NULL; | |
3803 | struct btrfs_path *path = NULL; | |
d24a67b2 | 3804 | bool ignore_offset; |
d7728c96 JS |
3805 | |
3806 | if (!capable(CAP_SYS_ADMIN)) | |
3807 | return -EPERM; | |
3808 | ||
3809 | loi = memdup_user(arg, sizeof(*loi)); | |
7b9ea627 SV |
3810 | if (IS_ERR(loi)) |
3811 | return PTR_ERR(loi); | |
d7728c96 | 3812 | |
d24a67b2 ZB |
3813 | if (version == 1) { |
3814 | ignore_offset = false; | |
b115e3bc | 3815 | size = min_t(u32, loi->size, SZ_64K); |
d24a67b2 ZB |
3816 | } else { |
3817 | /* All reserved bits must be 0 for now */ | |
3818 | if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { | |
3819 | ret = -EINVAL; | |
3820 | goto out_loi; | |
3821 | } | |
3822 | /* Only accept flags we have defined so far */ | |
3823 | if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { | |
3824 | ret = -EINVAL; | |
3825 | goto out_loi; | |
3826 | } | |
3827 | ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; | |
b115e3bc | 3828 | size = min_t(u32, loi->size, SZ_16M); |
d24a67b2 ZB |
3829 | } |
3830 | ||
d7728c96 JS |
3831 | path = btrfs_alloc_path(); |
3832 | if (!path) { | |
3833 | ret = -ENOMEM; | |
3834 | goto out; | |
3835 | } | |
3836 | ||
d7728c96 JS |
3837 | inodes = init_data_container(size); |
3838 | if (IS_ERR(inodes)) { | |
3839 | ret = PTR_ERR(inodes); | |
3840 | inodes = NULL; | |
3841 | goto out; | |
3842 | } | |
3843 | ||
2ff7e61e | 3844 | ret = iterate_inodes_from_logical(loi->logical, fs_info, path, |
d24a67b2 | 3845 | build_ino_list, inodes, ignore_offset); |
df031f07 | 3846 | if (ret == -EINVAL) |
d7728c96 JS |
3847 | ret = -ENOENT; |
3848 | if (ret < 0) | |
3849 | goto out; | |
3850 | ||
718dc5fa OS |
3851 | ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, |
3852 | size); | |
d7728c96 JS |
3853 | if (ret) |
3854 | ret = -EFAULT; | |
3855 | ||
3856 | out: | |
3857 | btrfs_free_path(path); | |
f54de068 | 3858 | kvfree(inodes); |
d24a67b2 | 3859 | out_loi: |
d7728c96 JS |
3860 | kfree(loi); |
3861 | ||
3862 | return ret; | |
3863 | } | |
3864 | ||
008ef096 | 3865 | void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, |
c9e9f97b ID |
3866 | struct btrfs_ioctl_balance_args *bargs) |
3867 | { | |
3868 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
3869 | ||
3870 | bargs->flags = bctl->flags; | |
3871 | ||
3009a62f | 3872 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) |
837d5b6e ID |
3873 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; |
3874 | if (atomic_read(&fs_info->balance_pause_req)) | |
3875 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; | |
a7e99c69 ID |
3876 | if (atomic_read(&fs_info->balance_cancel_req)) |
3877 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; | |
837d5b6e | 3878 | |
c9e9f97b ID |
3879 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
3880 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); | |
3881 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); | |
19a39dce | 3882 | |
008ef096 DS |
3883 | spin_lock(&fs_info->balance_lock); |
3884 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); | |
3885 | spin_unlock(&fs_info->balance_lock); | |
c9e9f97b ID |
3886 | } |
3887 | ||
9ba1f6e4 | 3888 | static long btrfs_ioctl_balance(struct file *file, void __user *arg) |
c9e9f97b | 3889 | { |
496ad9aa | 3890 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
c9e9f97b ID |
3891 | struct btrfs_fs_info *fs_info = root->fs_info; |
3892 | struct btrfs_ioctl_balance_args *bargs; | |
3893 | struct btrfs_balance_control *bctl; | |
ed0fb78f | 3894 | bool need_unlock; /* for mut. excl. ops lock */ |
c9e9f97b ID |
3895 | int ret; |
3896 | ||
3897 | if (!capable(CAP_SYS_ADMIN)) | |
3898 | return -EPERM; | |
3899 | ||
e54bfa31 | 3900 | ret = mnt_want_write_file(file); |
9ba1f6e4 LB |
3901 | if (ret) |
3902 | return ret; | |
3903 | ||
ed0fb78f | 3904 | again: |
171938e5 | 3905 | if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
ed0fb78f ID |
3906 | mutex_lock(&fs_info->balance_mutex); |
3907 | need_unlock = true; | |
3908 | goto locked; | |
3909 | } | |
3910 | ||
3911 | /* | |
01327610 | 3912 | * mut. excl. ops lock is locked. Three possibilities: |
ed0fb78f ID |
3913 | * (1) some other op is running |
3914 | * (2) balance is running | |
3915 | * (3) balance is paused -- special case (think resume) | |
3916 | */ | |
c9e9f97b | 3917 | mutex_lock(&fs_info->balance_mutex); |
ed0fb78f ID |
3918 | if (fs_info->balance_ctl) { |
3919 | /* this is either (2) or (3) */ | |
3009a62f | 3920 | if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
ed0fb78f | 3921 | mutex_unlock(&fs_info->balance_mutex); |
dccdb07b DS |
3922 | /* |
3923 | * Lock released to allow other waiters to continue, | |
3924 | * we'll reexamine the status again. | |
3925 | */ | |
ed0fb78f ID |
3926 | mutex_lock(&fs_info->balance_mutex); |
3927 | ||
3928 | if (fs_info->balance_ctl && | |
3009a62f | 3929 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
ed0fb78f ID |
3930 | /* this is (3) */ |
3931 | need_unlock = false; | |
3932 | goto locked; | |
3933 | } | |
3934 | ||
3935 | mutex_unlock(&fs_info->balance_mutex); | |
ed0fb78f ID |
3936 | goto again; |
3937 | } else { | |
3938 | /* this is (2) */ | |
3939 | mutex_unlock(&fs_info->balance_mutex); | |
3940 | ret = -EINPROGRESS; | |
3941 | goto out; | |
3942 | } | |
3943 | } else { | |
3944 | /* this is (1) */ | |
3945 | mutex_unlock(&fs_info->balance_mutex); | |
e57138b3 | 3946 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
ed0fb78f ID |
3947 | goto out; |
3948 | } | |
3949 | ||
3950 | locked: | |
171938e5 | 3951 | BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); |
c9e9f97b ID |
3952 | |
3953 | if (arg) { | |
3954 | bargs = memdup_user(arg, sizeof(*bargs)); | |
3955 | if (IS_ERR(bargs)) { | |
3956 | ret = PTR_ERR(bargs); | |
ed0fb78f | 3957 | goto out_unlock; |
c9e9f97b | 3958 | } |
de322263 ID |
3959 | |
3960 | if (bargs->flags & BTRFS_BALANCE_RESUME) { | |
3961 | if (!fs_info->balance_ctl) { | |
3962 | ret = -ENOTCONN; | |
3963 | goto out_bargs; | |
3964 | } | |
3965 | ||
3966 | bctl = fs_info->balance_ctl; | |
3967 | spin_lock(&fs_info->balance_lock); | |
3968 | bctl->flags |= BTRFS_BALANCE_RESUME; | |
3969 | spin_unlock(&fs_info->balance_lock); | |
3970 | ||
3971 | goto do_balance; | |
3972 | } | |
c9e9f97b ID |
3973 | } else { |
3974 | bargs = NULL; | |
3975 | } | |
3976 | ||
ed0fb78f | 3977 | if (fs_info->balance_ctl) { |
837d5b6e ID |
3978 | ret = -EINPROGRESS; |
3979 | goto out_bargs; | |
3980 | } | |
3981 | ||
8d2db785 | 3982 | bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); |
c9e9f97b ID |
3983 | if (!bctl) { |
3984 | ret = -ENOMEM; | |
3985 | goto out_bargs; | |
3986 | } | |
3987 | ||
c9e9f97b ID |
3988 | if (arg) { |
3989 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); | |
3990 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); | |
3991 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); | |
3992 | ||
3993 | bctl->flags = bargs->flags; | |
f43ffb60 ID |
3994 | } else { |
3995 | /* balance everything - no filters */ | |
3996 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; | |
c9e9f97b ID |
3997 | } |
3998 | ||
8eb93459 DS |
3999 | if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { |
4000 | ret = -EINVAL; | |
0f89abf5 | 4001 | goto out_bctl; |
8eb93459 DS |
4002 | } |
4003 | ||
de322263 | 4004 | do_balance: |
c9e9f97b | 4005 | /* |
149196a2 DS |
4006 | * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to |
4007 | * btrfs_balance. bctl is freed in reset_balance_state, or, if | |
4008 | * restriper was paused all the way until unmount, in free_fs_info. | |
4009 | * The flag should be cleared after reset_balance_state. | |
c9e9f97b | 4010 | */ |
ed0fb78f ID |
4011 | need_unlock = false; |
4012 | ||
6fcf6e2b | 4013 | ret = btrfs_balance(fs_info, bctl, bargs); |
0f89abf5 | 4014 | bctl = NULL; |
ed0fb78f | 4015 | |
d00c2d9c | 4016 | if ((ret == 0 || ret == -ECANCELED) && arg) { |
c9e9f97b ID |
4017 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
4018 | ret = -EFAULT; | |
4019 | } | |
4020 | ||
0f89abf5 CE |
4021 | out_bctl: |
4022 | kfree(bctl); | |
c9e9f97b ID |
4023 | out_bargs: |
4024 | kfree(bargs); | |
ed0fb78f | 4025 | out_unlock: |
c9e9f97b | 4026 | mutex_unlock(&fs_info->balance_mutex); |
ed0fb78f | 4027 | if (need_unlock) |
171938e5 | 4028 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
ed0fb78f | 4029 | out: |
e54bfa31 | 4030 | mnt_drop_write_file(file); |
c9e9f97b ID |
4031 | return ret; |
4032 | } | |
4033 | ||
2ff7e61e | 4034 | static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) |
837d5b6e ID |
4035 | { |
4036 | if (!capable(CAP_SYS_ADMIN)) | |
4037 | return -EPERM; | |
4038 | ||
4039 | switch (cmd) { | |
4040 | case BTRFS_BALANCE_CTL_PAUSE: | |
0b246afa | 4041 | return btrfs_pause_balance(fs_info); |
a7e99c69 | 4042 | case BTRFS_BALANCE_CTL_CANCEL: |
0b246afa | 4043 | return btrfs_cancel_balance(fs_info); |
837d5b6e ID |
4044 | } |
4045 | ||
4046 | return -EINVAL; | |
4047 | } | |
4048 | ||
2ff7e61e | 4049 | static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, |
19a39dce ID |
4050 | void __user *arg) |
4051 | { | |
19a39dce ID |
4052 | struct btrfs_ioctl_balance_args *bargs; |
4053 | int ret = 0; | |
4054 | ||
4055 | if (!capable(CAP_SYS_ADMIN)) | |
4056 | return -EPERM; | |
4057 | ||
4058 | mutex_lock(&fs_info->balance_mutex); | |
4059 | if (!fs_info->balance_ctl) { | |
4060 | ret = -ENOTCONN; | |
4061 | goto out; | |
4062 | } | |
4063 | ||
8d2db785 | 4064 | bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); |
19a39dce ID |
4065 | if (!bargs) { |
4066 | ret = -ENOMEM; | |
4067 | goto out; | |
4068 | } | |
4069 | ||
008ef096 | 4070 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
19a39dce ID |
4071 | |
4072 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | |
4073 | ret = -EFAULT; | |
4074 | ||
4075 | kfree(bargs); | |
4076 | out: | |
4077 | mutex_unlock(&fs_info->balance_mutex); | |
4078 | return ret; | |
4079 | } | |
4080 | ||
905b0dda | 4081 | static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) |
5d13a37b | 4082 | { |
0b246afa JM |
4083 | struct inode *inode = file_inode(file); |
4084 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5d13a37b | 4085 | struct btrfs_ioctl_quota_ctl_args *sa; |
5d13a37b | 4086 | int ret; |
5d13a37b AJ |
4087 | |
4088 | if (!capable(CAP_SYS_ADMIN)) | |
4089 | return -EPERM; | |
4090 | ||
905b0dda MX |
4091 | ret = mnt_want_write_file(file); |
4092 | if (ret) | |
4093 | return ret; | |
5d13a37b AJ |
4094 | |
4095 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4096 | if (IS_ERR(sa)) { |
4097 | ret = PTR_ERR(sa); | |
4098 | goto drop_write; | |
4099 | } | |
5d13a37b | 4100 | |
0b246afa | 4101 | down_write(&fs_info->subvol_sem); |
5d13a37b AJ |
4102 | |
4103 | switch (sa->cmd) { | |
4104 | case BTRFS_QUOTA_CTL_ENABLE: | |
340f1aa2 | 4105 | ret = btrfs_quota_enable(fs_info); |
5d13a37b AJ |
4106 | break; |
4107 | case BTRFS_QUOTA_CTL_DISABLE: | |
340f1aa2 | 4108 | ret = btrfs_quota_disable(fs_info); |
5d13a37b | 4109 | break; |
5d13a37b AJ |
4110 | default: |
4111 | ret = -EINVAL; | |
4112 | break; | |
4113 | } | |
4114 | ||
5d13a37b | 4115 | kfree(sa); |
0b246afa | 4116 | up_write(&fs_info->subvol_sem); |
905b0dda MX |
4117 | drop_write: |
4118 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4119 | return ret; |
4120 | } | |
4121 | ||
905b0dda | 4122 | static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) |
5d13a37b | 4123 | { |
0b246afa JM |
4124 | struct inode *inode = file_inode(file); |
4125 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4126 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5d13a37b AJ |
4127 | struct btrfs_ioctl_qgroup_assign_args *sa; |
4128 | struct btrfs_trans_handle *trans; | |
4129 | int ret; | |
4130 | int err; | |
4131 | ||
4132 | if (!capable(CAP_SYS_ADMIN)) | |
4133 | return -EPERM; | |
4134 | ||
905b0dda MX |
4135 | ret = mnt_want_write_file(file); |
4136 | if (ret) | |
4137 | return ret; | |
5d13a37b AJ |
4138 | |
4139 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4140 | if (IS_ERR(sa)) { |
4141 | ret = PTR_ERR(sa); | |
4142 | goto drop_write; | |
4143 | } | |
5d13a37b AJ |
4144 | |
4145 | trans = btrfs_join_transaction(root); | |
4146 | if (IS_ERR(trans)) { | |
4147 | ret = PTR_ERR(trans); | |
4148 | goto out; | |
4149 | } | |
4150 | ||
5d13a37b | 4151 | if (sa->assign) { |
9f8a6ce6 | 4152 | ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst); |
5d13a37b | 4153 | } else { |
39616c27 | 4154 | ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst); |
5d13a37b AJ |
4155 | } |
4156 | ||
e082f563 | 4157 | /* update qgroup status and info */ |
280f8bd2 | 4158 | err = btrfs_run_qgroups(trans); |
e082f563 | 4159 | if (err < 0) |
0b246afa JM |
4160 | btrfs_handle_fs_error(fs_info, err, |
4161 | "failed to update qgroup status and info"); | |
3a45bb20 | 4162 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
4163 | if (err && !ret) |
4164 | ret = err; | |
4165 | ||
4166 | out: | |
4167 | kfree(sa); | |
905b0dda MX |
4168 | drop_write: |
4169 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4170 | return ret; |
4171 | } | |
4172 | ||
905b0dda | 4173 | static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) |
5d13a37b | 4174 | { |
0b246afa | 4175 | struct inode *inode = file_inode(file); |
0b246afa | 4176 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5d13a37b AJ |
4177 | struct btrfs_ioctl_qgroup_create_args *sa; |
4178 | struct btrfs_trans_handle *trans; | |
4179 | int ret; | |
4180 | int err; | |
4181 | ||
4182 | if (!capable(CAP_SYS_ADMIN)) | |
4183 | return -EPERM; | |
4184 | ||
905b0dda MX |
4185 | ret = mnt_want_write_file(file); |
4186 | if (ret) | |
4187 | return ret; | |
5d13a37b AJ |
4188 | |
4189 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4190 | if (IS_ERR(sa)) { |
4191 | ret = PTR_ERR(sa); | |
4192 | goto drop_write; | |
4193 | } | |
5d13a37b | 4194 | |
d86e56cf MX |
4195 | if (!sa->qgroupid) { |
4196 | ret = -EINVAL; | |
4197 | goto out; | |
4198 | } | |
4199 | ||
5d13a37b AJ |
4200 | trans = btrfs_join_transaction(root); |
4201 | if (IS_ERR(trans)) { | |
4202 | ret = PTR_ERR(trans); | |
4203 | goto out; | |
4204 | } | |
4205 | ||
5d13a37b | 4206 | if (sa->create) { |
49a05ecd | 4207 | ret = btrfs_create_qgroup(trans, sa->qgroupid); |
5d13a37b | 4208 | } else { |
3efbee1d | 4209 | ret = btrfs_remove_qgroup(trans, sa->qgroupid); |
5d13a37b AJ |
4210 | } |
4211 | ||
3a45bb20 | 4212 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
4213 | if (err && !ret) |
4214 | ret = err; | |
4215 | ||
4216 | out: | |
4217 | kfree(sa); | |
905b0dda MX |
4218 | drop_write: |
4219 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4220 | return ret; |
4221 | } | |
4222 | ||
905b0dda | 4223 | static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) |
5d13a37b | 4224 | { |
0b246afa | 4225 | struct inode *inode = file_inode(file); |
0b246afa | 4226 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5d13a37b AJ |
4227 | struct btrfs_ioctl_qgroup_limit_args *sa; |
4228 | struct btrfs_trans_handle *trans; | |
4229 | int ret; | |
4230 | int err; | |
4231 | u64 qgroupid; | |
4232 | ||
4233 | if (!capable(CAP_SYS_ADMIN)) | |
4234 | return -EPERM; | |
4235 | ||
905b0dda MX |
4236 | ret = mnt_want_write_file(file); |
4237 | if (ret) | |
4238 | return ret; | |
5d13a37b AJ |
4239 | |
4240 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4241 | if (IS_ERR(sa)) { |
4242 | ret = PTR_ERR(sa); | |
4243 | goto drop_write; | |
4244 | } | |
5d13a37b AJ |
4245 | |
4246 | trans = btrfs_join_transaction(root); | |
4247 | if (IS_ERR(trans)) { | |
4248 | ret = PTR_ERR(trans); | |
4249 | goto out; | |
4250 | } | |
4251 | ||
4252 | qgroupid = sa->qgroupid; | |
4253 | if (!qgroupid) { | |
4254 | /* take the current subvol as qgroup */ | |
4255 | qgroupid = root->root_key.objectid; | |
4256 | } | |
4257 | ||
f0042d5e | 4258 | ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim); |
5d13a37b | 4259 | |
3a45bb20 | 4260 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
4261 | if (err && !ret) |
4262 | ret = err; | |
4263 | ||
4264 | out: | |
4265 | kfree(sa); | |
905b0dda MX |
4266 | drop_write: |
4267 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4268 | return ret; |
4269 | } | |
4270 | ||
2f232036 JS |
4271 | static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) |
4272 | { | |
0b246afa JM |
4273 | struct inode *inode = file_inode(file); |
4274 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2f232036 JS |
4275 | struct btrfs_ioctl_quota_rescan_args *qsa; |
4276 | int ret; | |
4277 | ||
4278 | if (!capable(CAP_SYS_ADMIN)) | |
4279 | return -EPERM; | |
4280 | ||
4281 | ret = mnt_want_write_file(file); | |
4282 | if (ret) | |
4283 | return ret; | |
4284 | ||
4285 | qsa = memdup_user(arg, sizeof(*qsa)); | |
4286 | if (IS_ERR(qsa)) { | |
4287 | ret = PTR_ERR(qsa); | |
4288 | goto drop_write; | |
4289 | } | |
4290 | ||
4291 | if (qsa->flags) { | |
4292 | ret = -EINVAL; | |
4293 | goto out; | |
4294 | } | |
4295 | ||
0b246afa | 4296 | ret = btrfs_qgroup_rescan(fs_info); |
2f232036 JS |
4297 | |
4298 | out: | |
4299 | kfree(qsa); | |
4300 | drop_write: | |
4301 | mnt_drop_write_file(file); | |
4302 | return ret; | |
4303 | } | |
4304 | ||
b929c1d8 MPS |
4305 | static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info, |
4306 | void __user *arg) | |
2f232036 | 4307 | { |
2f232036 JS |
4308 | struct btrfs_ioctl_quota_rescan_args *qsa; |
4309 | int ret = 0; | |
4310 | ||
4311 | if (!capable(CAP_SYS_ADMIN)) | |
4312 | return -EPERM; | |
4313 | ||
8d2db785 | 4314 | qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); |
2f232036 JS |
4315 | if (!qsa) |
4316 | return -ENOMEM; | |
4317 | ||
0b246afa | 4318 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
2f232036 | 4319 | qsa->flags = 1; |
0b246afa | 4320 | qsa->progress = fs_info->qgroup_rescan_progress.objectid; |
2f232036 JS |
4321 | } |
4322 | ||
4323 | if (copy_to_user(arg, qsa, sizeof(*qsa))) | |
4324 | ret = -EFAULT; | |
4325 | ||
4326 | kfree(qsa); | |
4327 | return ret; | |
4328 | } | |
4329 | ||
b929c1d8 MPS |
4330 | static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info, |
4331 | void __user *arg) | |
57254b6e | 4332 | { |
57254b6e JS |
4333 | if (!capable(CAP_SYS_ADMIN)) |
4334 | return -EPERM; | |
4335 | ||
0b246afa | 4336 | return btrfs_qgroup_wait_for_completion(fs_info, true); |
57254b6e JS |
4337 | } |
4338 | ||
abccd00f HM |
4339 | static long _btrfs_ioctl_set_received_subvol(struct file *file, |
4340 | struct btrfs_ioctl_received_subvol_args *sa) | |
8ea05e3a | 4341 | { |
496ad9aa | 4342 | struct inode *inode = file_inode(file); |
0b246afa | 4343 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8ea05e3a AB |
4344 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4345 | struct btrfs_root_item *root_item = &root->root_item; | |
4346 | struct btrfs_trans_handle *trans; | |
95582b00 | 4347 | struct timespec64 ct = current_time(inode); |
8ea05e3a | 4348 | int ret = 0; |
dd5f9615 | 4349 | int received_uuid_changed; |
8ea05e3a | 4350 | |
bd60ea0f DS |
4351 | if (!inode_owner_or_capable(inode)) |
4352 | return -EPERM; | |
4353 | ||
8ea05e3a AB |
4354 | ret = mnt_want_write_file(file); |
4355 | if (ret < 0) | |
4356 | return ret; | |
4357 | ||
0b246afa | 4358 | down_write(&fs_info->subvol_sem); |
8ea05e3a | 4359 | |
4a0cc7ca | 4360 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
8ea05e3a AB |
4361 | ret = -EINVAL; |
4362 | goto out; | |
4363 | } | |
4364 | ||
4365 | if (btrfs_root_readonly(root)) { | |
4366 | ret = -EROFS; | |
4367 | goto out; | |
4368 | } | |
4369 | ||
dd5f9615 SB |
4370 | /* |
4371 | * 1 - root item | |
4372 | * 2 - uuid items (received uuid + subvol uuid) | |
4373 | */ | |
4374 | trans = btrfs_start_transaction(root, 3); | |
8ea05e3a AB |
4375 | if (IS_ERR(trans)) { |
4376 | ret = PTR_ERR(trans); | |
4377 | trans = NULL; | |
4378 | goto out; | |
4379 | } | |
4380 | ||
4381 | sa->rtransid = trans->transid; | |
4382 | sa->rtime.sec = ct.tv_sec; | |
4383 | sa->rtime.nsec = ct.tv_nsec; | |
4384 | ||
dd5f9615 SB |
4385 | received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, |
4386 | BTRFS_UUID_SIZE); | |
4387 | if (received_uuid_changed && | |
d87ff758 | 4388 | !btrfs_is_empty_uuid(root_item->received_uuid)) { |
d1957791 | 4389 | ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid, |
d87ff758 NB |
4390 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
4391 | root->root_key.objectid); | |
4392 | if (ret && ret != -ENOENT) { | |
4393 | btrfs_abort_transaction(trans, ret); | |
4394 | btrfs_end_transaction(trans); | |
4395 | goto out; | |
4396 | } | |
4397 | } | |
8ea05e3a AB |
4398 | memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); |
4399 | btrfs_set_root_stransid(root_item, sa->stransid); | |
4400 | btrfs_set_root_rtransid(root_item, sa->rtransid); | |
3cae210f QW |
4401 | btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); |
4402 | btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); | |
4403 | btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); | |
4404 | btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); | |
8ea05e3a | 4405 | |
0b246afa | 4406 | ret = btrfs_update_root(trans, fs_info->tree_root, |
8ea05e3a AB |
4407 | &root->root_key, &root->root_item); |
4408 | if (ret < 0) { | |
3a45bb20 | 4409 | btrfs_end_transaction(trans); |
8ea05e3a | 4410 | goto out; |
dd5f9615 SB |
4411 | } |
4412 | if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { | |
cdb345a8 | 4413 | ret = btrfs_uuid_tree_add(trans, sa->uuid, |
dd5f9615 SB |
4414 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
4415 | root->root_key.objectid); | |
4416 | if (ret < 0 && ret != -EEXIST) { | |
66642832 | 4417 | btrfs_abort_transaction(trans, ret); |
efd38150 | 4418 | btrfs_end_transaction(trans); |
8ea05e3a | 4419 | goto out; |
dd5f9615 SB |
4420 | } |
4421 | } | |
3a45bb20 | 4422 | ret = btrfs_commit_transaction(trans); |
abccd00f | 4423 | out: |
0b246afa | 4424 | up_write(&fs_info->subvol_sem); |
abccd00f HM |
4425 | mnt_drop_write_file(file); |
4426 | return ret; | |
4427 | } | |
4428 | ||
4429 | #ifdef CONFIG_64BIT | |
4430 | static long btrfs_ioctl_set_received_subvol_32(struct file *file, | |
4431 | void __user *arg) | |
4432 | { | |
4433 | struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; | |
4434 | struct btrfs_ioctl_received_subvol_args *args64 = NULL; | |
4435 | int ret = 0; | |
4436 | ||
4437 | args32 = memdup_user(arg, sizeof(*args32)); | |
7b9ea627 SV |
4438 | if (IS_ERR(args32)) |
4439 | return PTR_ERR(args32); | |
abccd00f | 4440 | |
8d2db785 | 4441 | args64 = kmalloc(sizeof(*args64), GFP_KERNEL); |
84dbeb87 DC |
4442 | if (!args64) { |
4443 | ret = -ENOMEM; | |
abccd00f HM |
4444 | goto out; |
4445 | } | |
4446 | ||
4447 | memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); | |
4448 | args64->stransid = args32->stransid; | |
4449 | args64->rtransid = args32->rtransid; | |
4450 | args64->stime.sec = args32->stime.sec; | |
4451 | args64->stime.nsec = args32->stime.nsec; | |
4452 | args64->rtime.sec = args32->rtime.sec; | |
4453 | args64->rtime.nsec = args32->rtime.nsec; | |
4454 | args64->flags = args32->flags; | |
4455 | ||
4456 | ret = _btrfs_ioctl_set_received_subvol(file, args64); | |
4457 | if (ret) | |
4458 | goto out; | |
4459 | ||
4460 | memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); | |
4461 | args32->stransid = args64->stransid; | |
4462 | args32->rtransid = args64->rtransid; | |
4463 | args32->stime.sec = args64->stime.sec; | |
4464 | args32->stime.nsec = args64->stime.nsec; | |
4465 | args32->rtime.sec = args64->rtime.sec; | |
4466 | args32->rtime.nsec = args64->rtime.nsec; | |
4467 | args32->flags = args64->flags; | |
4468 | ||
4469 | ret = copy_to_user(arg, args32, sizeof(*args32)); | |
4470 | if (ret) | |
4471 | ret = -EFAULT; | |
4472 | ||
4473 | out: | |
4474 | kfree(args32); | |
4475 | kfree(args64); | |
4476 | return ret; | |
4477 | } | |
4478 | #endif | |
4479 | ||
4480 | static long btrfs_ioctl_set_received_subvol(struct file *file, | |
4481 | void __user *arg) | |
4482 | { | |
4483 | struct btrfs_ioctl_received_subvol_args *sa = NULL; | |
4484 | int ret = 0; | |
4485 | ||
4486 | sa = memdup_user(arg, sizeof(*sa)); | |
7b9ea627 SV |
4487 | if (IS_ERR(sa)) |
4488 | return PTR_ERR(sa); | |
abccd00f HM |
4489 | |
4490 | ret = _btrfs_ioctl_set_received_subvol(file, sa); | |
4491 | ||
4492 | if (ret) | |
4493 | goto out; | |
4494 | ||
8ea05e3a AB |
4495 | ret = copy_to_user(arg, sa, sizeof(*sa)); |
4496 | if (ret) | |
4497 | ret = -EFAULT; | |
4498 | ||
4499 | out: | |
4500 | kfree(sa); | |
8ea05e3a AB |
4501 | return ret; |
4502 | } | |
4503 | ||
b929c1d8 MPS |
4504 | static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info, |
4505 | void __user *arg) | |
867ab667 | 4506 | { |
a1b83ac5 | 4507 | size_t len; |
867ab667 | 4508 | int ret; |
a1b83ac5 AJ |
4509 | char label[BTRFS_LABEL_SIZE]; |
4510 | ||
0b246afa JM |
4511 | spin_lock(&fs_info->super_lock); |
4512 | memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); | |
4513 | spin_unlock(&fs_info->super_lock); | |
a1b83ac5 AJ |
4514 | |
4515 | len = strnlen(label, BTRFS_LABEL_SIZE); | |
867ab667 | 4516 | |
4517 | if (len == BTRFS_LABEL_SIZE) { | |
0b246afa JM |
4518 | btrfs_warn(fs_info, |
4519 | "label is too long, return the first %zu bytes", | |
4520 | --len); | |
867ab667 | 4521 | } |
4522 | ||
867ab667 | 4523 | ret = copy_to_user(arg, label, len); |
867ab667 | 4524 | |
4525 | return ret ? -EFAULT : 0; | |
4526 | } | |
4527 | ||
a8bfd4ab | 4528 | static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) |
4529 | { | |
0b246afa JM |
4530 | struct inode *inode = file_inode(file); |
4531 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4532 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4533 | struct btrfs_super_block *super_block = fs_info->super_copy; | |
a8bfd4ab | 4534 | struct btrfs_trans_handle *trans; |
4535 | char label[BTRFS_LABEL_SIZE]; | |
4536 | int ret; | |
4537 | ||
4538 | if (!capable(CAP_SYS_ADMIN)) | |
4539 | return -EPERM; | |
4540 | ||
4541 | if (copy_from_user(label, arg, sizeof(label))) | |
4542 | return -EFAULT; | |
4543 | ||
4544 | if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { | |
0b246afa | 4545 | btrfs_err(fs_info, |
5d163e0e JM |
4546 | "unable to set label with more than %d bytes", |
4547 | BTRFS_LABEL_SIZE - 1); | |
a8bfd4ab | 4548 | return -EINVAL; |
4549 | } | |
4550 | ||
4551 | ret = mnt_want_write_file(file); | |
4552 | if (ret) | |
4553 | return ret; | |
4554 | ||
a8bfd4ab | 4555 | trans = btrfs_start_transaction(root, 0); |
4556 | if (IS_ERR(trans)) { | |
4557 | ret = PTR_ERR(trans); | |
4558 | goto out_unlock; | |
4559 | } | |
4560 | ||
0b246afa | 4561 | spin_lock(&fs_info->super_lock); |
a8bfd4ab | 4562 | strcpy(super_block->label, label); |
0b246afa | 4563 | spin_unlock(&fs_info->super_lock); |
3a45bb20 | 4564 | ret = btrfs_commit_transaction(trans); |
a8bfd4ab | 4565 | |
4566 | out_unlock: | |
a8bfd4ab | 4567 | mnt_drop_write_file(file); |
4568 | return ret; | |
4569 | } | |
4570 | ||
2eaa055f JM |
4571 | #define INIT_FEATURE_FLAGS(suffix) \ |
4572 | { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ | |
4573 | .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ | |
4574 | .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } | |
4575 | ||
d5131b65 | 4576 | int btrfs_ioctl_get_supported_features(void __user *arg) |
2eaa055f | 4577 | { |
4d4ab6d6 | 4578 | static const struct btrfs_ioctl_feature_flags features[3] = { |
2eaa055f JM |
4579 | INIT_FEATURE_FLAGS(SUPP), |
4580 | INIT_FEATURE_FLAGS(SAFE_SET), | |
4581 | INIT_FEATURE_FLAGS(SAFE_CLEAR) | |
4582 | }; | |
4583 | ||
4584 | if (copy_to_user(arg, &features, sizeof(features))) | |
4585 | return -EFAULT; | |
4586 | ||
4587 | return 0; | |
4588 | } | |
4589 | ||
b929c1d8 MPS |
4590 | static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info, |
4591 | void __user *arg) | |
2eaa055f | 4592 | { |
0b246afa | 4593 | struct btrfs_super_block *super_block = fs_info->super_copy; |
2eaa055f JM |
4594 | struct btrfs_ioctl_feature_flags features; |
4595 | ||
4596 | features.compat_flags = btrfs_super_compat_flags(super_block); | |
4597 | features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); | |
4598 | features.incompat_flags = btrfs_super_incompat_flags(super_block); | |
4599 | ||
4600 | if (copy_to_user(arg, &features, sizeof(features))) | |
4601 | return -EFAULT; | |
4602 | ||
4603 | return 0; | |
4604 | } | |
4605 | ||
2ff7e61e | 4606 | static int check_feature_bits(struct btrfs_fs_info *fs_info, |
3b02a68a | 4607 | enum btrfs_feature_set set, |
2eaa055f JM |
4608 | u64 change_mask, u64 flags, u64 supported_flags, |
4609 | u64 safe_set, u64 safe_clear) | |
4610 | { | |
f10152bc | 4611 | const char *type = btrfs_feature_set_name(set); |
3b02a68a | 4612 | char *names; |
2eaa055f JM |
4613 | u64 disallowed, unsupported; |
4614 | u64 set_mask = flags & change_mask; | |
4615 | u64 clear_mask = ~flags & change_mask; | |
4616 | ||
4617 | unsupported = set_mask & ~supported_flags; | |
4618 | if (unsupported) { | |
3b02a68a JM |
4619 | names = btrfs_printable_features(set, unsupported); |
4620 | if (names) { | |
0b246afa JM |
4621 | btrfs_warn(fs_info, |
4622 | "this kernel does not support the %s feature bit%s", | |
4623 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
4624 | kfree(names); |
4625 | } else | |
0b246afa JM |
4626 | btrfs_warn(fs_info, |
4627 | "this kernel does not support %s bits 0x%llx", | |
4628 | type, unsupported); | |
2eaa055f JM |
4629 | return -EOPNOTSUPP; |
4630 | } | |
4631 | ||
4632 | disallowed = set_mask & ~safe_set; | |
4633 | if (disallowed) { | |
3b02a68a JM |
4634 | names = btrfs_printable_features(set, disallowed); |
4635 | if (names) { | |
0b246afa JM |
4636 | btrfs_warn(fs_info, |
4637 | "can't set the %s feature bit%s while mounted", | |
4638 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
4639 | kfree(names); |
4640 | } else | |
0b246afa JM |
4641 | btrfs_warn(fs_info, |
4642 | "can't set %s bits 0x%llx while mounted", | |
4643 | type, disallowed); | |
2eaa055f JM |
4644 | return -EPERM; |
4645 | } | |
4646 | ||
4647 | disallowed = clear_mask & ~safe_clear; | |
4648 | if (disallowed) { | |
3b02a68a JM |
4649 | names = btrfs_printable_features(set, disallowed); |
4650 | if (names) { | |
0b246afa JM |
4651 | btrfs_warn(fs_info, |
4652 | "can't clear the %s feature bit%s while mounted", | |
4653 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
4654 | kfree(names); |
4655 | } else | |
0b246afa JM |
4656 | btrfs_warn(fs_info, |
4657 | "can't clear %s bits 0x%llx while mounted", | |
4658 | type, disallowed); | |
2eaa055f JM |
4659 | return -EPERM; |
4660 | } | |
4661 | ||
4662 | return 0; | |
4663 | } | |
4664 | ||
2ff7e61e JM |
4665 | #define check_feature(fs_info, change_mask, flags, mask_base) \ |
4666 | check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ | |
2eaa055f JM |
4667 | BTRFS_FEATURE_ ## mask_base ## _SUPP, \ |
4668 | BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ | |
4669 | BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) | |
4670 | ||
4671 | static int btrfs_ioctl_set_features(struct file *file, void __user *arg) | |
4672 | { | |
0b246afa JM |
4673 | struct inode *inode = file_inode(file); |
4674 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4675 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4676 | struct btrfs_super_block *super_block = fs_info->super_copy; | |
2eaa055f JM |
4677 | struct btrfs_ioctl_feature_flags flags[2]; |
4678 | struct btrfs_trans_handle *trans; | |
4679 | u64 newflags; | |
4680 | int ret; | |
4681 | ||
4682 | if (!capable(CAP_SYS_ADMIN)) | |
4683 | return -EPERM; | |
4684 | ||
4685 | if (copy_from_user(flags, arg, sizeof(flags))) | |
4686 | return -EFAULT; | |
4687 | ||
4688 | /* Nothing to do */ | |
4689 | if (!flags[0].compat_flags && !flags[0].compat_ro_flags && | |
4690 | !flags[0].incompat_flags) | |
4691 | return 0; | |
4692 | ||
2ff7e61e | 4693 | ret = check_feature(fs_info, flags[0].compat_flags, |
2eaa055f JM |
4694 | flags[1].compat_flags, COMPAT); |
4695 | if (ret) | |
4696 | return ret; | |
4697 | ||
2ff7e61e | 4698 | ret = check_feature(fs_info, flags[0].compat_ro_flags, |
2eaa055f JM |
4699 | flags[1].compat_ro_flags, COMPAT_RO); |
4700 | if (ret) | |
4701 | return ret; | |
4702 | ||
2ff7e61e | 4703 | ret = check_feature(fs_info, flags[0].incompat_flags, |
2eaa055f JM |
4704 | flags[1].incompat_flags, INCOMPAT); |
4705 | if (ret) | |
4706 | return ret; | |
4707 | ||
7ab19625 DS |
4708 | ret = mnt_want_write_file(file); |
4709 | if (ret) | |
4710 | return ret; | |
4711 | ||
8051aa1a | 4712 | trans = btrfs_start_transaction(root, 0); |
7ab19625 DS |
4713 | if (IS_ERR(trans)) { |
4714 | ret = PTR_ERR(trans); | |
4715 | goto out_drop_write; | |
4716 | } | |
2eaa055f | 4717 | |
0b246afa | 4718 | spin_lock(&fs_info->super_lock); |
2eaa055f JM |
4719 | newflags = btrfs_super_compat_flags(super_block); |
4720 | newflags |= flags[0].compat_flags & flags[1].compat_flags; | |
4721 | newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); | |
4722 | btrfs_set_super_compat_flags(super_block, newflags); | |
4723 | ||
4724 | newflags = btrfs_super_compat_ro_flags(super_block); | |
4725 | newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; | |
4726 | newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); | |
4727 | btrfs_set_super_compat_ro_flags(super_block, newflags); | |
4728 | ||
4729 | newflags = btrfs_super_incompat_flags(super_block); | |
4730 | newflags |= flags[0].incompat_flags & flags[1].incompat_flags; | |
4731 | newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); | |
4732 | btrfs_set_super_incompat_flags(super_block, newflags); | |
0b246afa | 4733 | spin_unlock(&fs_info->super_lock); |
2eaa055f | 4734 | |
3a45bb20 | 4735 | ret = btrfs_commit_transaction(trans); |
7ab19625 DS |
4736 | out_drop_write: |
4737 | mnt_drop_write_file(file); | |
4738 | ||
4739 | return ret; | |
2eaa055f JM |
4740 | } |
4741 | ||
2351f431 JB |
4742 | static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) |
4743 | { | |
4744 | struct btrfs_ioctl_send_args *arg; | |
4745 | int ret; | |
4746 | ||
4747 | if (compat) { | |
4748 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) | |
4749 | struct btrfs_ioctl_send_args_32 args32; | |
4750 | ||
4751 | ret = copy_from_user(&args32, argp, sizeof(args32)); | |
4752 | if (ret) | |
4753 | return -EFAULT; | |
4754 | arg = kzalloc(sizeof(*arg), GFP_KERNEL); | |
4755 | if (!arg) | |
4756 | return -ENOMEM; | |
4757 | arg->send_fd = args32.send_fd; | |
4758 | arg->clone_sources_count = args32.clone_sources_count; | |
4759 | arg->clone_sources = compat_ptr(args32.clone_sources); | |
4760 | arg->parent_root = args32.parent_root; | |
4761 | arg->flags = args32.flags; | |
4762 | memcpy(arg->reserved, args32.reserved, | |
4763 | sizeof(args32.reserved)); | |
4764 | #else | |
4765 | return -ENOTTY; | |
4766 | #endif | |
4767 | } else { | |
4768 | arg = memdup_user(argp, sizeof(*arg)); | |
4769 | if (IS_ERR(arg)) | |
4770 | return PTR_ERR(arg); | |
4771 | } | |
4772 | ret = btrfs_ioctl_send(file, arg); | |
4773 | kfree(arg); | |
4774 | return ret; | |
4775 | } | |
4776 | ||
f46b5a66 CH |
4777 | long btrfs_ioctl(struct file *file, unsigned int |
4778 | cmd, unsigned long arg) | |
4779 | { | |
0b246afa JM |
4780 | struct inode *inode = file_inode(file); |
4781 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4782 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4bcabaa3 | 4783 | void __user *argp = (void __user *)arg; |
f46b5a66 CH |
4784 | |
4785 | switch (cmd) { | |
6cbff00f CH |
4786 | case FS_IOC_GETFLAGS: |
4787 | return btrfs_ioctl_getflags(file, argp); | |
4788 | case FS_IOC_SETFLAGS: | |
4789 | return btrfs_ioctl_setflags(file, argp); | |
4790 | case FS_IOC_GETVERSION: | |
4791 | return btrfs_ioctl_getversion(file, argp); | |
40cf931f | 4792 | case FS_IOC_GETFSLABEL: |
b929c1d8 | 4793 | return btrfs_ioctl_get_fslabel(fs_info, argp); |
40cf931f ES |
4794 | case FS_IOC_SETFSLABEL: |
4795 | return btrfs_ioctl_set_fslabel(file, argp); | |
f7039b1d | 4796 | case FITRIM: |
b929c1d8 | 4797 | return btrfs_ioctl_fitrim(fs_info, argp); |
f46b5a66 | 4798 | case BTRFS_IOC_SNAP_CREATE: |
fa0d2b9b | 4799 | return btrfs_ioctl_snap_create(file, argp, 0); |
fdfb1e4f | 4800 | case BTRFS_IOC_SNAP_CREATE_V2: |
fa0d2b9b | 4801 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
3de4586c | 4802 | case BTRFS_IOC_SUBVOL_CREATE: |
fa0d2b9b | 4803 | return btrfs_ioctl_snap_create(file, argp, 1); |
6f72c7e2 AJ |
4804 | case BTRFS_IOC_SUBVOL_CREATE_V2: |
4805 | return btrfs_ioctl_snap_create_v2(file, argp, 1); | |
76dda93c | 4806 | case BTRFS_IOC_SNAP_DESTROY: |
949964c9 MPS |
4807 | return btrfs_ioctl_snap_destroy(file, argp, false); |
4808 | case BTRFS_IOC_SNAP_DESTROY_V2: | |
4809 | return btrfs_ioctl_snap_destroy(file, argp, true); | |
0caa102d LZ |
4810 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
4811 | return btrfs_ioctl_subvol_getflags(file, argp); | |
4812 | case BTRFS_IOC_SUBVOL_SETFLAGS: | |
4813 | return btrfs_ioctl_subvol_setflags(file, argp); | |
6ef5ed0d JB |
4814 | case BTRFS_IOC_DEFAULT_SUBVOL: |
4815 | return btrfs_ioctl_default_subvol(file, argp); | |
f46b5a66 | 4816 | case BTRFS_IOC_DEFRAG: |
1e701a32 CM |
4817 | return btrfs_ioctl_defrag(file, NULL); |
4818 | case BTRFS_IOC_DEFRAG_RANGE: | |
4819 | return btrfs_ioctl_defrag(file, argp); | |
f46b5a66 | 4820 | case BTRFS_IOC_RESIZE: |
198605a8 | 4821 | return btrfs_ioctl_resize(file, argp); |
f46b5a66 | 4822 | case BTRFS_IOC_ADD_DEV: |
2ff7e61e | 4823 | return btrfs_ioctl_add_dev(fs_info, argp); |
f46b5a66 | 4824 | case BTRFS_IOC_RM_DEV: |
da24927b | 4825 | return btrfs_ioctl_rm_dev(file, argp); |
6b526ed7 AJ |
4826 | case BTRFS_IOC_RM_DEV_V2: |
4827 | return btrfs_ioctl_rm_dev_v2(file, argp); | |
475f6387 | 4828 | case BTRFS_IOC_FS_INFO: |
2ff7e61e | 4829 | return btrfs_ioctl_fs_info(fs_info, argp); |
475f6387 | 4830 | case BTRFS_IOC_DEV_INFO: |
2ff7e61e | 4831 | return btrfs_ioctl_dev_info(fs_info, argp); |
f46b5a66 | 4832 | case BTRFS_IOC_BALANCE: |
9ba1f6e4 | 4833 | return btrfs_ioctl_balance(file, NULL); |
ac8e9819 CM |
4834 | case BTRFS_IOC_TREE_SEARCH: |
4835 | return btrfs_ioctl_tree_search(file, argp); | |
cc68a8a5 GH |
4836 | case BTRFS_IOC_TREE_SEARCH_V2: |
4837 | return btrfs_ioctl_tree_search_v2(file, argp); | |
ac8e9819 CM |
4838 | case BTRFS_IOC_INO_LOOKUP: |
4839 | return btrfs_ioctl_ino_lookup(file, argp); | |
d7728c96 JS |
4840 | case BTRFS_IOC_INO_PATHS: |
4841 | return btrfs_ioctl_ino_to_path(root, argp); | |
4842 | case BTRFS_IOC_LOGICAL_INO: | |
d24a67b2 ZB |
4843 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); |
4844 | case BTRFS_IOC_LOGICAL_INO_V2: | |
4845 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); | |
1406e432 | 4846 | case BTRFS_IOC_SPACE_INFO: |
2ff7e61e | 4847 | return btrfs_ioctl_space_info(fs_info, argp); |
9b199859 FDBM |
4848 | case BTRFS_IOC_SYNC: { |
4849 | int ret; | |
4850 | ||
82b3e53b | 4851 | ret = btrfs_start_delalloc_roots(fs_info, -1); |
9b199859 FDBM |
4852 | if (ret) |
4853 | return ret; | |
0b246afa | 4854 | ret = btrfs_sync_fs(inode->i_sb, 1); |
2fad4e83 DS |
4855 | /* |
4856 | * The transaction thread may want to do more work, | |
01327610 | 4857 | * namely it pokes the cleaner kthread that will start |
2fad4e83 DS |
4858 | * processing uncleaned subvols. |
4859 | */ | |
0b246afa | 4860 | wake_up_process(fs_info->transaction_kthread); |
9b199859 FDBM |
4861 | return ret; |
4862 | } | |
46204592 | 4863 | case BTRFS_IOC_START_SYNC: |
9a8c28be | 4864 | return btrfs_ioctl_start_sync(root, argp); |
46204592 | 4865 | case BTRFS_IOC_WAIT_SYNC: |
2ff7e61e | 4866 | return btrfs_ioctl_wait_sync(fs_info, argp); |
475f6387 | 4867 | case BTRFS_IOC_SCRUB: |
b8e95489 | 4868 | return btrfs_ioctl_scrub(file, argp); |
475f6387 | 4869 | case BTRFS_IOC_SCRUB_CANCEL: |
2ff7e61e | 4870 | return btrfs_ioctl_scrub_cancel(fs_info); |
475f6387 | 4871 | case BTRFS_IOC_SCRUB_PROGRESS: |
2ff7e61e | 4872 | return btrfs_ioctl_scrub_progress(fs_info, argp); |
c9e9f97b | 4873 | case BTRFS_IOC_BALANCE_V2: |
9ba1f6e4 | 4874 | return btrfs_ioctl_balance(file, argp); |
837d5b6e | 4875 | case BTRFS_IOC_BALANCE_CTL: |
2ff7e61e | 4876 | return btrfs_ioctl_balance_ctl(fs_info, arg); |
19a39dce | 4877 | case BTRFS_IOC_BALANCE_PROGRESS: |
2ff7e61e | 4878 | return btrfs_ioctl_balance_progress(fs_info, argp); |
8ea05e3a AB |
4879 | case BTRFS_IOC_SET_RECEIVED_SUBVOL: |
4880 | return btrfs_ioctl_set_received_subvol(file, argp); | |
abccd00f HM |
4881 | #ifdef CONFIG_64BIT |
4882 | case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: | |
4883 | return btrfs_ioctl_set_received_subvol_32(file, argp); | |
4884 | #endif | |
31db9f7c | 4885 | case BTRFS_IOC_SEND: |
2351f431 JB |
4886 | return _btrfs_ioctl_send(file, argp, false); |
4887 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) | |
4888 | case BTRFS_IOC_SEND_32: | |
4889 | return _btrfs_ioctl_send(file, argp, true); | |
4890 | #endif | |
c11d2c23 | 4891 | case BTRFS_IOC_GET_DEV_STATS: |
2ff7e61e | 4892 | return btrfs_ioctl_get_dev_stats(fs_info, argp); |
5d13a37b | 4893 | case BTRFS_IOC_QUOTA_CTL: |
905b0dda | 4894 | return btrfs_ioctl_quota_ctl(file, argp); |
5d13a37b | 4895 | case BTRFS_IOC_QGROUP_ASSIGN: |
905b0dda | 4896 | return btrfs_ioctl_qgroup_assign(file, argp); |
5d13a37b | 4897 | case BTRFS_IOC_QGROUP_CREATE: |
905b0dda | 4898 | return btrfs_ioctl_qgroup_create(file, argp); |
5d13a37b | 4899 | case BTRFS_IOC_QGROUP_LIMIT: |
905b0dda | 4900 | return btrfs_ioctl_qgroup_limit(file, argp); |
2f232036 JS |
4901 | case BTRFS_IOC_QUOTA_RESCAN: |
4902 | return btrfs_ioctl_quota_rescan(file, argp); | |
4903 | case BTRFS_IOC_QUOTA_RESCAN_STATUS: | |
b929c1d8 | 4904 | return btrfs_ioctl_quota_rescan_status(fs_info, argp); |
57254b6e | 4905 | case BTRFS_IOC_QUOTA_RESCAN_WAIT: |
b929c1d8 | 4906 | return btrfs_ioctl_quota_rescan_wait(fs_info, argp); |
3f6bcfbd | 4907 | case BTRFS_IOC_DEV_REPLACE: |
2ff7e61e | 4908 | return btrfs_ioctl_dev_replace(fs_info, argp); |
2eaa055f | 4909 | case BTRFS_IOC_GET_SUPPORTED_FEATURES: |
d5131b65 | 4910 | return btrfs_ioctl_get_supported_features(argp); |
2eaa055f | 4911 | case BTRFS_IOC_GET_FEATURES: |
b929c1d8 | 4912 | return btrfs_ioctl_get_features(fs_info, argp); |
2eaa055f JM |
4913 | case BTRFS_IOC_SET_FEATURES: |
4914 | return btrfs_ioctl_set_features(file, argp); | |
e4202ac9 DS |
4915 | case FS_IOC_FSGETXATTR: |
4916 | return btrfs_ioctl_fsgetxattr(file, argp); | |
025f2121 DS |
4917 | case FS_IOC_FSSETXATTR: |
4918 | return btrfs_ioctl_fssetxattr(file, argp); | |
b64ec075 TM |
4919 | case BTRFS_IOC_GET_SUBVOL_INFO: |
4920 | return btrfs_ioctl_get_subvol_info(file, argp); | |
42e4b520 TM |
4921 | case BTRFS_IOC_GET_SUBVOL_ROOTREF: |
4922 | return btrfs_ioctl_get_subvol_rootref(file, argp); | |
23d0b79d TM |
4923 | case BTRFS_IOC_INO_LOOKUP_USER: |
4924 | return btrfs_ioctl_ino_lookup_user(file, argp); | |
f46b5a66 CH |
4925 | } |
4926 | ||
4927 | return -ENOTTY; | |
4928 | } | |
4c63c245 LD |
4929 | |
4930 | #ifdef CONFIG_COMPAT | |
4931 | long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
4932 | { | |
2a362249 JM |
4933 | /* |
4934 | * These all access 32-bit values anyway so no further | |
4935 | * handling is necessary. | |
4936 | */ | |
4c63c245 LD |
4937 | switch (cmd) { |
4938 | case FS_IOC32_GETFLAGS: | |
4939 | cmd = FS_IOC_GETFLAGS; | |
4940 | break; | |
4941 | case FS_IOC32_SETFLAGS: | |
4942 | cmd = FS_IOC_SETFLAGS; | |
4943 | break; | |
4944 | case FS_IOC32_GETVERSION: | |
4945 | cmd = FS_IOC_GETVERSION; | |
4946 | break; | |
4c63c245 LD |
4947 | } |
4948 | ||
4949 | return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); | |
4950 | } | |
4951 | #endif |