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