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