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