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