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