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