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