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