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