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