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