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