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