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