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