]>
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 MF |
2916 | } |
2917 | kfree(cmp->src_pages); | |
2918 | kfree(cmp->dst_pages); | |
2919 | } | |
2920 | ||
2921 | static int btrfs_cmp_data_prepare(struct inode *src, u64 loff, | |
2922 | struct inode *dst, u64 dst_loff, | |
2923 | u64 len, struct cmp_pages *cmp) | |
2924 | { | |
2925 | int ret; | |
09cbfeaf | 2926 | int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT; |
f4414602 MF |
2927 | struct page **src_pgarr, **dst_pgarr; |
2928 | ||
2929 | /* | |
2930 | * We must gather up all the pages before we initiate our | |
2931 | * extent locking. We use an array for the page pointers. Size | |
2932 | * of the array is bounded by len, which is in turn bounded by | |
2933 | * BTRFS_MAX_DEDUPE_LEN. | |
2934 | */ | |
66722f7c DS |
2935 | src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); |
2936 | dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); | |
f4414602 MF |
2937 | if (!src_pgarr || !dst_pgarr) { |
2938 | kfree(src_pgarr); | |
2939 | kfree(dst_pgarr); | |
2940 | return -ENOMEM; | |
416161db | 2941 | } |
f4414602 MF |
2942 | cmp->num_pages = num_pages; |
2943 | cmp->src_pages = src_pgarr; | |
2944 | cmp->dst_pages = dst_pgarr; | |
2945 | ||
b1517622 FM |
2946 | /* |
2947 | * If deduping ranges in the same inode, locking rules make it mandatory | |
2948 | * to always lock pages in ascending order to avoid deadlocks with | |
2949 | * concurrent tasks (such as starting writeback/delalloc). | |
2950 | */ | |
2951 | if (src == dst && dst_loff < loff) { | |
2952 | swap(src_pgarr, dst_pgarr); | |
2953 | swap(loff, dst_loff); | |
2954 | } | |
2955 | ||
2956 | ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff); | |
f4414602 MF |
2957 | if (ret) |
2958 | goto out; | |
2959 | ||
b1517622 | 2960 | ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff); |
f4414602 MF |
2961 | |
2962 | out: | |
2963 | if (ret) | |
2964 | btrfs_cmp_data_free(cmp); | |
78ad4ce0 | 2965 | return ret; |
416161db MF |
2966 | } |
2967 | ||
1a287cfe | 2968 | static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp) |
416161db MF |
2969 | { |
2970 | int ret = 0; | |
f4414602 | 2971 | int i; |
416161db | 2972 | struct page *src_page, *dst_page; |
09cbfeaf | 2973 | unsigned int cmp_len = PAGE_SIZE; |
416161db MF |
2974 | void *addr, *dst_addr; |
2975 | ||
f4414602 | 2976 | i = 0; |
416161db | 2977 | while (len) { |
09cbfeaf | 2978 | if (len < PAGE_SIZE) |
416161db MF |
2979 | cmp_len = len; |
2980 | ||
f4414602 MF |
2981 | BUG_ON(i >= cmp->num_pages); |
2982 | ||
2983 | src_page = cmp->src_pages[i]; | |
2984 | dst_page = cmp->dst_pages[i]; | |
e0bd70c6 FM |
2985 | ASSERT(PageLocked(src_page)); |
2986 | ASSERT(PageLocked(dst_page)); | |
f4414602 | 2987 | |
416161db MF |
2988 | addr = kmap_atomic(src_page); |
2989 | dst_addr = kmap_atomic(dst_page); | |
2990 | ||
2991 | flush_dcache_page(src_page); | |
2992 | flush_dcache_page(dst_page); | |
2993 | ||
2994 | if (memcmp(addr, dst_addr, cmp_len)) | |
2b3909f8 | 2995 | ret = -EBADE; |
416161db MF |
2996 | |
2997 | kunmap_atomic(addr); | |
2998 | kunmap_atomic(dst_addr); | |
416161db MF |
2999 | |
3000 | if (ret) | |
3001 | break; | |
3002 | ||
416161db | 3003 | len -= cmp_len; |
f4414602 | 3004 | i++; |
416161db MF |
3005 | } |
3006 | ||
3007 | return ret; | |
3008 | } | |
3009 | ||
e1d227a4 MF |
3010 | static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen, |
3011 | u64 olen) | |
416161db | 3012 | { |
e1d227a4 | 3013 | u64 len = *plen; |
416161db MF |
3014 | u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; |
3015 | ||
e1d227a4 | 3016 | if (off + olen > inode->i_size || off + olen < off) |
416161db | 3017 | return -EINVAL; |
e1d227a4 MF |
3018 | |
3019 | /* if we extend to eof, continue to block boundary */ | |
3020 | if (off + len == inode->i_size) | |
3021 | *plen = len = ALIGN(inode->i_size, bs) - off; | |
3022 | ||
416161db MF |
3023 | /* Check that we are block aligned - btrfs_clone() requires this */ |
3024 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) | |
3025 | return -EINVAL; | |
3026 | ||
3027 | return 0; | |
3028 | } | |
3029 | ||
e1d227a4 | 3030 | static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, |
416161db MF |
3031 | struct inode *dst, u64 dst_loff) |
3032 | { | |
3033 | int ret; | |
e1d227a4 | 3034 | u64 len = olen; |
f4414602 | 3035 | struct cmp_pages cmp; |
fc4badd9 | 3036 | bool same_inode = (src == dst); |
0efa9f48 MF |
3037 | u64 same_lock_start = 0; |
3038 | u64 same_lock_len = 0; | |
416161db | 3039 | |
113e8283 FM |
3040 | if (len == 0) |
3041 | return 0; | |
3042 | ||
fc4badd9 | 3043 | if (same_inode) |
5955102c | 3044 | inode_lock(src); |
fc4badd9 OS |
3045 | else |
3046 | btrfs_double_inode_lock(src, dst); | |
416161db | 3047 | |
fc4badd9 OS |
3048 | ret = extent_same_check_offsets(src, loff, &len, olen); |
3049 | if (ret) | |
3050 | goto out_unlock; | |
416161db | 3051 | |
fc4badd9 OS |
3052 | ret = extent_same_check_offsets(dst, dst_loff, &len, olen); |
3053 | if (ret) | |
3054 | goto out_unlock; | |
3055 | ||
3056 | if (same_inode) { | |
0efa9f48 MF |
3057 | /* |
3058 | * Single inode case wants the same checks, except we | |
3059 | * don't want our length pushed out past i_size as | |
3060 | * comparing that data range makes no sense. | |
3061 | * | |
3062 | * extent_same_check_offsets() will do this for an | |
3063 | * unaligned length at i_size, so catch it here and | |
3064 | * reject the request. | |
3065 | * | |
3066 | * This effectively means we require aligned extents | |
3067 | * for the single-inode case, whereas the other cases | |
3068 | * allow an unaligned length so long as it ends at | |
3069 | * i_size. | |
3070 | */ | |
3071 | if (len != olen) { | |
3072 | ret = -EINVAL; | |
3073 | goto out_unlock; | |
3074 | } | |
3075 | ||
3076 | /* Check for overlapping ranges */ | |
3077 | if (dst_loff + len > loff && dst_loff < loff + len) { | |
3078 | ret = -EINVAL; | |
3079 | goto out_unlock; | |
3080 | } | |
3081 | ||
3082 | same_lock_start = min_t(u64, loff, dst_loff); | |
3083 | same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start; | |
0efa9f48 | 3084 | } |
416161db MF |
3085 | |
3086 | /* don't make the dst file partly checksummed */ | |
3087 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != | |
3088 | (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { | |
3089 | ret = -EINVAL; | |
3090 | goto out_unlock; | |
3091 | } | |
3092 | ||
e0bd70c6 | 3093 | again: |
f4414602 MF |
3094 | ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp); |
3095 | if (ret) | |
3096 | goto out_unlock; | |
3097 | ||
0efa9f48 | 3098 | if (same_inode) |
e0bd70c6 FM |
3099 | ret = lock_extent_range(src, same_lock_start, same_lock_len, |
3100 | false); | |
0efa9f48 | 3101 | else |
e0bd70c6 FM |
3102 | ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len, |
3103 | false); | |
3104 | /* | |
3105 | * If one of the inodes has dirty pages in the respective range or | |
3106 | * ordered extents, we need to flush dellaloc and wait for all ordered | |
3107 | * extents in the range. We must unlock the pages and the ranges in the | |
3108 | * io trees to avoid deadlocks when flushing delalloc (requires locking | |
3109 | * pages) and when waiting for ordered extents to complete (they require | |
3110 | * range locking). | |
3111 | */ | |
3112 | if (ret == -EAGAIN) { | |
3113 | /* | |
3114 | * Ranges in the io trees already unlocked. Now unlock all | |
3115 | * pages before waiting for all IO to complete. | |
3116 | */ | |
3117 | btrfs_cmp_data_free(&cmp); | |
3118 | if (same_inode) { | |
3119 | btrfs_wait_ordered_range(src, same_lock_start, | |
3120 | same_lock_len); | |
3121 | } else { | |
3122 | btrfs_wait_ordered_range(src, loff, len); | |
3123 | btrfs_wait_ordered_range(dst, dst_loff, len); | |
3124 | } | |
3125 | goto again; | |
3126 | } | |
3127 | ASSERT(ret == 0); | |
3128 | if (WARN_ON(ret)) { | |
3129 | /* ranges in the io trees already unlocked */ | |
3130 | btrfs_cmp_data_free(&cmp); | |
3131 | return ret; | |
3132 | } | |
f4414602 | 3133 | |
207910dd | 3134 | /* pass original length for comparison so we stay within i_size */ |
1a287cfe | 3135 | ret = btrfs_cmp_data(olen, &cmp); |
416161db | 3136 | if (ret == 0) |
1c919a5e | 3137 | ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1); |
416161db | 3138 | |
0efa9f48 MF |
3139 | if (same_inode) |
3140 | unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start, | |
3141 | same_lock_start + same_lock_len - 1); | |
3142 | else | |
3143 | btrfs_double_extent_unlock(src, loff, dst, dst_loff, len); | |
f4414602 MF |
3144 | |
3145 | btrfs_cmp_data_free(&cmp); | |
416161db | 3146 | out_unlock: |
0efa9f48 | 3147 | if (same_inode) |
5955102c | 3148 | inode_unlock(src); |
0efa9f48 MF |
3149 | else |
3150 | btrfs_double_inode_unlock(src, dst); | |
416161db MF |
3151 | |
3152 | return ret; | |
3153 | } | |
3154 | ||
ee22184b | 3155 | #define BTRFS_MAX_DEDUPE_LEN SZ_16M |
416161db | 3156 | |
2b3909f8 DW |
3157 | ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen, |
3158 | struct file *dst_file, u64 dst_loff) | |
416161db | 3159 | { |
2b3909f8 DW |
3160 | struct inode *src = file_inode(src_file); |
3161 | struct inode *dst = file_inode(dst_file); | |
416161db | 3162 | u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; |
2b3909f8 | 3163 | ssize_t res; |
416161db | 3164 | |
2b3909f8 DW |
3165 | if (olen > BTRFS_MAX_DEDUPE_LEN) |
3166 | olen = BTRFS_MAX_DEDUPE_LEN; | |
416161db | 3167 | |
09cbfeaf | 3168 | if (WARN_ON_ONCE(bs < PAGE_SIZE)) { |
416161db MF |
3169 | /* |
3170 | * Btrfs does not support blocksize < page_size. As a | |
3171 | * result, btrfs_cmp_data() won't correctly handle | |
3172 | * this situation without an update. | |
3173 | */ | |
2b3909f8 | 3174 | return -EINVAL; |
416161db MF |
3175 | } |
3176 | ||
2b3909f8 DW |
3177 | res = btrfs_extent_same(src, loff, olen, dst, dst_loff); |
3178 | if (res) | |
3179 | return res; | |
3180 | return olen; | |
416161db MF |
3181 | } |
3182 | ||
f82a9901 FM |
3183 | static int clone_finish_inode_update(struct btrfs_trans_handle *trans, |
3184 | struct inode *inode, | |
3185 | u64 endoff, | |
3186 | const u64 destoff, | |
1c919a5e MF |
3187 | const u64 olen, |
3188 | int no_time_update) | |
f82a9901 FM |
3189 | { |
3190 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3191 | int ret; | |
3192 | ||
3193 | inode_inc_iversion(inode); | |
1c919a5e | 3194 | if (!no_time_update) |
c2050a45 | 3195 | inode->i_mtime = inode->i_ctime = current_time(inode); |
f82a9901 FM |
3196 | /* |
3197 | * We round up to the block size at eof when determining which | |
3198 | * extents to clone above, but shouldn't round up the file size. | |
3199 | */ | |
3200 | if (endoff > destoff + olen) | |
3201 | endoff = destoff + olen; | |
3202 | if (endoff > inode->i_size) | |
6ef06d27 | 3203 | btrfs_i_size_write(BTRFS_I(inode), endoff); |
f82a9901 FM |
3204 | |
3205 | ret = btrfs_update_inode(trans, root, inode); | |
3206 | if (ret) { | |
66642832 | 3207 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 3208 | btrfs_end_transaction(trans); |
f82a9901 FM |
3209 | goto out; |
3210 | } | |
3a45bb20 | 3211 | ret = btrfs_end_transaction(trans); |
f82a9901 FM |
3212 | out: |
3213 | return ret; | |
3214 | } | |
3215 | ||
a2f392e4 | 3216 | static void clone_update_extent_map(struct btrfs_inode *inode, |
7ffbb598 FM |
3217 | const struct btrfs_trans_handle *trans, |
3218 | const struct btrfs_path *path, | |
7ffbb598 FM |
3219 | const u64 hole_offset, |
3220 | const u64 hole_len) | |
3221 | { | |
a2f392e4 | 3222 | struct extent_map_tree *em_tree = &inode->extent_tree; |
7ffbb598 FM |
3223 | struct extent_map *em; |
3224 | int ret; | |
3225 | ||
3226 | em = alloc_extent_map(); | |
3227 | if (!em) { | |
a2f392e4 | 3228 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
7ffbb598 FM |
3229 | return; |
3230 | } | |
3231 | ||
14f59796 FM |
3232 | if (path) { |
3233 | struct btrfs_file_extent_item *fi; | |
3234 | ||
3235 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3236 | struct btrfs_file_extent_item); | |
a2f392e4 | 3237 | btrfs_extent_item_to_extent_map(inode, path, fi, false, em); |
7ffbb598 FM |
3238 | em->generation = -1; |
3239 | if (btrfs_file_extent_type(path->nodes[0], fi) == | |
3240 | BTRFS_FILE_EXTENT_INLINE) | |
3241 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
a2f392e4 | 3242 | &inode->runtime_flags); |
7ffbb598 FM |
3243 | } else { |
3244 | em->start = hole_offset; | |
3245 | em->len = hole_len; | |
3246 | em->ram_bytes = em->len; | |
3247 | em->orig_start = hole_offset; | |
3248 | em->block_start = EXTENT_MAP_HOLE; | |
3249 | em->block_len = 0; | |
3250 | em->orig_block_len = 0; | |
3251 | em->compress_type = BTRFS_COMPRESS_NONE; | |
3252 | em->generation = trans->transid; | |
3253 | } | |
3254 | ||
3255 | while (1) { | |
3256 | write_lock(&em_tree->lock); | |
3257 | ret = add_extent_mapping(em_tree, em, 1); | |
3258 | write_unlock(&em_tree->lock); | |
3259 | if (ret != -EEXIST) { | |
3260 | free_extent_map(em); | |
3261 | break; | |
3262 | } | |
a2f392e4 | 3263 | btrfs_drop_extent_cache(inode, em->start, |
7ffbb598 FM |
3264 | em->start + em->len - 1, 0); |
3265 | } | |
3266 | ||
ee39b432 | 3267 | if (ret) |
a2f392e4 | 3268 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
7ffbb598 FM |
3269 | } |
3270 | ||
8039d87d FM |
3271 | /* |
3272 | * Make sure we do not end up inserting an inline extent into a file that has | |
3273 | * already other (non-inline) extents. If a file has an inline extent it can | |
3274 | * not have any other extents and the (single) inline extent must start at the | |
3275 | * file offset 0. Failing to respect these rules will lead to file corruption, | |
3276 | * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc | |
3277 | * | |
3278 | * We can have extents that have been already written to disk or we can have | |
3279 | * dirty ranges still in delalloc, in which case the extent maps and items are | |
3280 | * created only when we run delalloc, and the delalloc ranges might fall outside | |
3281 | * the range we are currently locking in the inode's io tree. So we check the | |
3282 | * inode's i_size because of that (i_size updates are done while holding the | |
3283 | * i_mutex, which we are holding here). | |
3284 | * We also check to see if the inode has a size not greater than "datal" but has | |
3285 | * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are | |
3286 | * protected against such concurrent fallocate calls by the i_mutex). | |
3287 | * | |
3288 | * If the file has no extents but a size greater than datal, do not allow the | |
3289 | * copy because we would need turn the inline extent into a non-inline one (even | |
3290 | * with NO_HOLES enabled). If we find our destination inode only has one inline | |
3291 | * extent, just overwrite it with the source inline extent if its size is less | |
3292 | * than the source extent's size, or we could copy the source inline extent's | |
3293 | * data into the destination inode's inline extent if the later is greater then | |
3294 | * the former. | |
3295 | */ | |
4a0ab9d7 | 3296 | static int clone_copy_inline_extent(struct inode *dst, |
8039d87d FM |
3297 | struct btrfs_trans_handle *trans, |
3298 | struct btrfs_path *path, | |
3299 | struct btrfs_key *new_key, | |
3300 | const u64 drop_start, | |
3301 | const u64 datal, | |
3302 | const u64 skip, | |
3303 | const u64 size, | |
3304 | char *inline_data) | |
3305 | { | |
0b246afa | 3306 | struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb); |
8039d87d FM |
3307 | struct btrfs_root *root = BTRFS_I(dst)->root; |
3308 | const u64 aligned_end = ALIGN(new_key->offset + datal, | |
0b246afa | 3309 | fs_info->sectorsize); |
8039d87d FM |
3310 | int ret; |
3311 | struct btrfs_key key; | |
3312 | ||
3313 | if (new_key->offset > 0) | |
3314 | return -EOPNOTSUPP; | |
3315 | ||
4a0cc7ca | 3316 | key.objectid = btrfs_ino(BTRFS_I(dst)); |
8039d87d FM |
3317 | key.type = BTRFS_EXTENT_DATA_KEY; |
3318 | key.offset = 0; | |
3319 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
3320 | if (ret < 0) { | |
3321 | return ret; | |
3322 | } else if (ret > 0) { | |
3323 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { | |
3324 | ret = btrfs_next_leaf(root, path); | |
3325 | if (ret < 0) | |
3326 | return ret; | |
3327 | else if (ret > 0) | |
3328 | goto copy_inline_extent; | |
3329 | } | |
3330 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
4a0cc7ca | 3331 | if (key.objectid == btrfs_ino(BTRFS_I(dst)) && |
8039d87d FM |
3332 | key.type == BTRFS_EXTENT_DATA_KEY) { |
3333 | ASSERT(key.offset > 0); | |
3334 | return -EOPNOTSUPP; | |
3335 | } | |
3336 | } else if (i_size_read(dst) <= datal) { | |
3337 | struct btrfs_file_extent_item *ei; | |
3338 | u64 ext_len; | |
3339 | ||
3340 | /* | |
3341 | * If the file size is <= datal, make sure there are no other | |
3342 | * extents following (can happen do to an fallocate call with | |
3343 | * the flag FALLOC_FL_KEEP_SIZE). | |
3344 | */ | |
3345 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3346 | struct btrfs_file_extent_item); | |
3347 | /* | |
3348 | * If it's an inline extent, it can not have other extents | |
3349 | * following it. | |
3350 | */ | |
3351 | if (btrfs_file_extent_type(path->nodes[0], ei) == | |
3352 | BTRFS_FILE_EXTENT_INLINE) | |
3353 | goto copy_inline_extent; | |
3354 | ||
3355 | ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei); | |
3356 | if (ext_len > aligned_end) | |
3357 | return -EOPNOTSUPP; | |
3358 | ||
3359 | ret = btrfs_next_item(root, path); | |
3360 | if (ret < 0) { | |
3361 | return ret; | |
3362 | } else if (ret == 0) { | |
3363 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
3364 | path->slots[0]); | |
4a0cc7ca | 3365 | if (key.objectid == btrfs_ino(BTRFS_I(dst)) && |
8039d87d FM |
3366 | key.type == BTRFS_EXTENT_DATA_KEY) |
3367 | return -EOPNOTSUPP; | |
3368 | } | |
3369 | } | |
3370 | ||
3371 | copy_inline_extent: | |
3372 | /* | |
3373 | * We have no extent items, or we have an extent at offset 0 which may | |
3374 | * or may not be inlined. All these cases are dealt the same way. | |
3375 | */ | |
3376 | if (i_size_read(dst) > datal) { | |
3377 | /* | |
3378 | * If the destination inode has an inline extent... | |
3379 | * This would require copying the data from the source inline | |
3380 | * extent into the beginning of the destination's inline extent. | |
3381 | * But this is really complex, both extents can be compressed | |
3382 | * or just one of them, which would require decompressing and | |
3383 | * re-compressing data (which could increase the new compressed | |
3384 | * size, not allowing the compressed data to fit anymore in an | |
3385 | * inline extent). | |
3386 | * So just don't support this case for now (it should be rare, | |
3387 | * we are not really saving space when cloning inline extents). | |
3388 | */ | |
3389 | return -EOPNOTSUPP; | |
3390 | } | |
3391 | ||
3392 | btrfs_release_path(path); | |
3393 | ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1); | |
3394 | if (ret) | |
3395 | return ret; | |
3396 | ret = btrfs_insert_empty_item(trans, root, path, new_key, size); | |
3397 | if (ret) | |
3398 | return ret; | |
3399 | ||
3400 | if (skip) { | |
3401 | const u32 start = btrfs_file_extent_calc_inline_size(0); | |
3402 | ||
3403 | memmove(inline_data + start, inline_data + start + skip, datal); | |
3404 | } | |
3405 | ||
3406 | write_extent_buffer(path->nodes[0], inline_data, | |
3407 | btrfs_item_ptr_offset(path->nodes[0], | |
3408 | path->slots[0]), | |
3409 | size); | |
3410 | inode_add_bytes(dst, datal); | |
3411 | ||
3412 | return 0; | |
3413 | } | |
3414 | ||
32b7c687 MF |
3415 | /** |
3416 | * btrfs_clone() - clone a range from inode file to another | |
3417 | * | |
3418 | * @src: Inode to clone from | |
3419 | * @inode: Inode to clone to | |
3420 | * @off: Offset within source to start clone from | |
3421 | * @olen: Original length, passed by user, of range to clone | |
1c919a5e | 3422 | * @olen_aligned: Block-aligned value of olen |
32b7c687 | 3423 | * @destoff: Offset within @inode to start clone |
1c919a5e | 3424 | * @no_time_update: Whether to update mtime/ctime on the target inode |
32b7c687 MF |
3425 | */ |
3426 | static int btrfs_clone(struct inode *src, struct inode *inode, | |
f82a9901 | 3427 | const u64 off, const u64 olen, const u64 olen_aligned, |
1c919a5e | 3428 | const u64 destoff, int no_time_update) |
f46b5a66 | 3429 | { |
0b246afa | 3430 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
f46b5a66 | 3431 | struct btrfs_root *root = BTRFS_I(inode)->root; |
32b7c687 | 3432 | struct btrfs_path *path = NULL; |
f46b5a66 | 3433 | struct extent_buffer *leaf; |
32b7c687 MF |
3434 | struct btrfs_trans_handle *trans; |
3435 | char *buf = NULL; | |
ae01a0ab | 3436 | struct btrfs_key key; |
f46b5a66 CH |
3437 | u32 nritems; |
3438 | int slot; | |
ae01a0ab | 3439 | int ret; |
f82a9901 | 3440 | const u64 len = olen_aligned; |
f82a9901 | 3441 | u64 last_dest_end = destoff; |
ae01a0ab YZ |
3442 | |
3443 | ret = -ENOMEM; | |
752ade68 MH |
3444 | buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); |
3445 | if (!buf) | |
3446 | return ret; | |
ae01a0ab YZ |
3447 | |
3448 | path = btrfs_alloc_path(); | |
3449 | if (!path) { | |
15351955 | 3450 | kvfree(buf); |
32b7c687 | 3451 | return ret; |
d525e8ab LZ |
3452 | } |
3453 | ||
e4058b54 | 3454 | path->reada = READA_FORWARD; |
c5c9cd4d | 3455 | /* clone data */ |
4a0cc7ca | 3456 | key.objectid = btrfs_ino(BTRFS_I(src)); |
ae01a0ab | 3457 | key.type = BTRFS_EXTENT_DATA_KEY; |
2c463823 | 3458 | key.offset = off; |
f46b5a66 CH |
3459 | |
3460 | while (1) { | |
de249e66 | 3461 | u64 next_key_min_offset = key.offset + 1; |
df858e76 | 3462 | |
f46b5a66 CH |
3463 | /* |
3464 | * note the key will change type as we walk through the | |
3465 | * tree. | |
3466 | */ | |
e4355f34 | 3467 | path->leave_spinning = 1; |
362a20c5 DS |
3468 | ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, |
3469 | 0, 0); | |
f46b5a66 CH |
3470 | if (ret < 0) |
3471 | goto out; | |
2c463823 FM |
3472 | /* |
3473 | * First search, if no extent item that starts at offset off was | |
3474 | * found but the previous item is an extent item, it's possible | |
3475 | * it might overlap our target range, therefore process it. | |
3476 | */ | |
3477 | if (key.offset == off && ret > 0 && path->slots[0] > 0) { | |
3478 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
3479 | path->slots[0] - 1); | |
3480 | if (key.type == BTRFS_EXTENT_DATA_KEY) | |
3481 | path->slots[0]--; | |
3482 | } | |
f46b5a66 | 3483 | |
ae01a0ab | 3484 | nritems = btrfs_header_nritems(path->nodes[0]); |
e4355f34 | 3485 | process_slot: |
ae01a0ab | 3486 | if (path->slots[0] >= nritems) { |
362a20c5 | 3487 | ret = btrfs_next_leaf(BTRFS_I(src)->root, path); |
f46b5a66 CH |
3488 | if (ret < 0) |
3489 | goto out; | |
3490 | if (ret > 0) | |
3491 | break; | |
ae01a0ab | 3492 | nritems = btrfs_header_nritems(path->nodes[0]); |
f46b5a66 CH |
3493 | } |
3494 | leaf = path->nodes[0]; | |
3495 | slot = path->slots[0]; | |
f46b5a66 | 3496 | |
ae01a0ab | 3497 | btrfs_item_key_to_cpu(leaf, &key, slot); |
962a298f | 3498 | if (key.type > BTRFS_EXTENT_DATA_KEY || |
4a0cc7ca | 3499 | key.objectid != btrfs_ino(BTRFS_I(src))) |
f46b5a66 CH |
3500 | break; |
3501 | ||
962a298f | 3502 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
c5c9cd4d SW |
3503 | struct btrfs_file_extent_item *extent; |
3504 | int type; | |
31840ae1 ZY |
3505 | u32 size; |
3506 | struct btrfs_key new_key; | |
c5c9cd4d SW |
3507 | u64 disko = 0, diskl = 0; |
3508 | u64 datao = 0, datal = 0; | |
3509 | u8 comp; | |
f82a9901 | 3510 | u64 drop_start; |
31840ae1 | 3511 | |
c5c9cd4d SW |
3512 | extent = btrfs_item_ptr(leaf, slot, |
3513 | struct btrfs_file_extent_item); | |
3514 | comp = btrfs_file_extent_compression(leaf, extent); | |
3515 | type = btrfs_file_extent_type(leaf, extent); | |
c8a894d7 CM |
3516 | if (type == BTRFS_FILE_EXTENT_REG || |
3517 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d397712b CM |
3518 | disko = btrfs_file_extent_disk_bytenr(leaf, |
3519 | extent); | |
3520 | diskl = btrfs_file_extent_disk_num_bytes(leaf, | |
3521 | extent); | |
c5c9cd4d | 3522 | datao = btrfs_file_extent_offset(leaf, extent); |
d397712b CM |
3523 | datal = btrfs_file_extent_num_bytes(leaf, |
3524 | extent); | |
c5c9cd4d SW |
3525 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
3526 | /* take upper bound, may be compressed */ | |
3527 | datal = btrfs_file_extent_ram_bytes(leaf, | |
3528 | extent); | |
3529 | } | |
31840ae1 | 3530 | |
2c463823 FM |
3531 | /* |
3532 | * The first search might have left us at an extent | |
3533 | * item that ends before our target range's start, can | |
3534 | * happen if we have holes and NO_HOLES feature enabled. | |
3535 | */ | |
3536 | if (key.offset + datal <= off) { | |
e4355f34 FDBM |
3537 | path->slots[0]++; |
3538 | goto process_slot; | |
2c463823 FM |
3539 | } else if (key.offset >= off + len) { |
3540 | break; | |
e4355f34 | 3541 | } |
df858e76 | 3542 | next_key_min_offset = key.offset + datal; |
e4355f34 FDBM |
3543 | size = btrfs_item_size_nr(leaf, slot); |
3544 | read_extent_buffer(leaf, buf, | |
3545 | btrfs_item_ptr_offset(leaf, slot), | |
3546 | size); | |
3547 | ||
3548 | btrfs_release_path(path); | |
3549 | path->leave_spinning = 0; | |
c5c9cd4d | 3550 | |
31840ae1 | 3551 | memcpy(&new_key, &key, sizeof(new_key)); |
4a0cc7ca | 3552 | new_key.objectid = btrfs_ino(BTRFS_I(inode)); |
4d728ec7 LZ |
3553 | if (off <= key.offset) |
3554 | new_key.offset = key.offset + destoff - off; | |
3555 | else | |
3556 | new_key.offset = destoff; | |
31840ae1 | 3557 | |
f82a9901 FM |
3558 | /* |
3559 | * Deal with a hole that doesn't have an extent item | |
3560 | * that represents it (NO_HOLES feature enabled). | |
3561 | * This hole is either in the middle of the cloning | |
3562 | * range or at the beginning (fully overlaps it or | |
3563 | * partially overlaps it). | |
3564 | */ | |
3565 | if (new_key.offset != last_dest_end) | |
3566 | drop_start = last_dest_end; | |
3567 | else | |
3568 | drop_start = new_key.offset; | |
3569 | ||
b6f3409b SW |
3570 | /* |
3571 | * 1 - adjusting old extent (we may have to split it) | |
3572 | * 1 - add new extent | |
3573 | * 1 - inode update | |
3574 | */ | |
3575 | trans = btrfs_start_transaction(root, 3); | |
a22285a6 YZ |
3576 | if (IS_ERR(trans)) { |
3577 | ret = PTR_ERR(trans); | |
3578 | goto out; | |
3579 | } | |
3580 | ||
c8a894d7 CM |
3581 | if (type == BTRFS_FILE_EXTENT_REG || |
3582 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d72c0842 LZ |
3583 | /* |
3584 | * a | --- range to clone ---| b | |
3585 | * | ------------- extent ------------- | | |
3586 | */ | |
3587 | ||
93915584 | 3588 | /* subtract range b */ |
d72c0842 LZ |
3589 | if (key.offset + datal > off + len) |
3590 | datal = off + len - key.offset; | |
3591 | ||
93915584 | 3592 | /* subtract range a */ |
a22285a6 YZ |
3593 | if (off > key.offset) { |
3594 | datao += off - key.offset; | |
3595 | datal -= off - key.offset; | |
3596 | } | |
3597 | ||
5dc562c5 | 3598 | ret = btrfs_drop_extents(trans, root, inode, |
f82a9901 | 3599 | drop_start, |
a22285a6 | 3600 | new_key.offset + datal, |
2671485d | 3601 | 1); |
79787eaa | 3602 | if (ret) { |
3f9e3df8 | 3603 | if (ret != -EOPNOTSUPP) |
00fdf13a | 3604 | btrfs_abort_transaction(trans, |
66642832 | 3605 | ret); |
3a45bb20 | 3606 | btrfs_end_transaction(trans); |
79787eaa JM |
3607 | goto out; |
3608 | } | |
a22285a6 | 3609 | |
c5c9cd4d SW |
3610 | ret = btrfs_insert_empty_item(trans, root, path, |
3611 | &new_key, size); | |
79787eaa | 3612 | if (ret) { |
66642832 | 3613 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 3614 | btrfs_end_transaction(trans); |
79787eaa JM |
3615 | goto out; |
3616 | } | |
c5c9cd4d SW |
3617 | |
3618 | leaf = path->nodes[0]; | |
3619 | slot = path->slots[0]; | |
3620 | write_extent_buffer(leaf, buf, | |
31840ae1 ZY |
3621 | btrfs_item_ptr_offset(leaf, slot), |
3622 | size); | |
ae01a0ab | 3623 | |
c5c9cd4d | 3624 | extent = btrfs_item_ptr(leaf, slot, |
f46b5a66 | 3625 | struct btrfs_file_extent_item); |
c5c9cd4d | 3626 | |
c5c9cd4d SW |
3627 | /* disko == 0 means it's a hole */ |
3628 | if (!disko) | |
3629 | datao = 0; | |
c5c9cd4d SW |
3630 | |
3631 | btrfs_set_file_extent_offset(leaf, extent, | |
3632 | datao); | |
3633 | btrfs_set_file_extent_num_bytes(leaf, extent, | |
3634 | datal); | |
fcebe456 | 3635 | |
c5c9cd4d SW |
3636 | if (disko) { |
3637 | inode_add_bytes(inode, datal); | |
2ff7e61e | 3638 | ret = btrfs_inc_extent_ref(trans, |
84f7d8e6 | 3639 | root, |
5d4f98a2 YZ |
3640 | disko, diskl, 0, |
3641 | root->root_key.objectid, | |
4a0cc7ca | 3642 | btrfs_ino(BTRFS_I(inode)), |
b06c4bf5 | 3643 | new_key.offset - datao); |
79787eaa JM |
3644 | if (ret) { |
3645 | btrfs_abort_transaction(trans, | |
79787eaa | 3646 | ret); |
3a45bb20 | 3647 | btrfs_end_transaction(trans); |
79787eaa JM |
3648 | goto out; |
3649 | ||
3650 | } | |
f46b5a66 | 3651 | } |
c5c9cd4d SW |
3652 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
3653 | u64 skip = 0; | |
3654 | u64 trim = 0; | |
ed958762 | 3655 | |
c5c9cd4d SW |
3656 | if (off > key.offset) { |
3657 | skip = off - key.offset; | |
3658 | new_key.offset += skip; | |
3659 | } | |
d397712b | 3660 | |
aa42ffd9 LB |
3661 | if (key.offset + datal > off + len) |
3662 | trim = key.offset + datal - (off + len); | |
d397712b | 3663 | |
c5c9cd4d | 3664 | if (comp && (skip || trim)) { |
c5c9cd4d | 3665 | ret = -EINVAL; |
3a45bb20 | 3666 | btrfs_end_transaction(trans); |
c5c9cd4d SW |
3667 | goto out; |
3668 | } | |
3669 | size -= skip + trim; | |
3670 | datal -= skip + trim; | |
a22285a6 | 3671 | |
4a0ab9d7 | 3672 | ret = clone_copy_inline_extent(inode, |
8039d87d FM |
3673 | trans, path, |
3674 | &new_key, | |
3675 | drop_start, | |
3676 | datal, | |
3677 | skip, size, buf); | |
79787eaa | 3678 | if (ret) { |
3f9e3df8 | 3679 | if (ret != -EOPNOTSUPP) |
3a29bc09 | 3680 | btrfs_abort_transaction(trans, |
8039d87d | 3681 | ret); |
3a45bb20 | 3682 | btrfs_end_transaction(trans); |
79787eaa JM |
3683 | goto out; |
3684 | } | |
c5c9cd4d SW |
3685 | leaf = path->nodes[0]; |
3686 | slot = path->slots[0]; | |
f46b5a66 | 3687 | } |
c5c9cd4d | 3688 | |
7ffbb598 FM |
3689 | /* If we have an implicit hole (NO_HOLES feature). */ |
3690 | if (drop_start < new_key.offset) | |
a2f392e4 | 3691 | clone_update_extent_map(BTRFS_I(inode), trans, |
14f59796 | 3692 | NULL, drop_start, |
7ffbb598 FM |
3693 | new_key.offset - drop_start); |
3694 | ||
a2f392e4 NB |
3695 | clone_update_extent_map(BTRFS_I(inode), trans, |
3696 | path, 0, 0); | |
7ffbb598 | 3697 | |
c5c9cd4d | 3698 | btrfs_mark_buffer_dirty(leaf); |
b3b4aa74 | 3699 | btrfs_release_path(path); |
c5c9cd4d | 3700 | |
62e2390e | 3701 | last_dest_end = ALIGN(new_key.offset + datal, |
0b246afa | 3702 | fs_info->sectorsize); |
f82a9901 FM |
3703 | ret = clone_finish_inode_update(trans, inode, |
3704 | last_dest_end, | |
1c919a5e MF |
3705 | destoff, olen, |
3706 | no_time_update); | |
f82a9901 | 3707 | if (ret) |
79787eaa | 3708 | goto out; |
2c463823 FM |
3709 | if (new_key.offset + datal >= destoff + len) |
3710 | break; | |
a22285a6 | 3711 | } |
b3b4aa74 | 3712 | btrfs_release_path(path); |
df858e76 | 3713 | key.offset = next_key_min_offset; |
69ae5e44 WX |
3714 | |
3715 | if (fatal_signal_pending(current)) { | |
3716 | ret = -EINTR; | |
3717 | goto out; | |
3718 | } | |
f46b5a66 | 3719 | } |
f46b5a66 | 3720 | ret = 0; |
32b7c687 | 3721 | |
f82a9901 FM |
3722 | if (last_dest_end < destoff + len) { |
3723 | /* | |
3724 | * We have an implicit hole (NO_HOLES feature is enabled) that | |
3725 | * fully or partially overlaps our cloning range at its end. | |
3726 | */ | |
3727 | btrfs_release_path(path); | |
3728 | ||
3729 | /* | |
3730 | * 1 - remove extent(s) | |
3731 | * 1 - inode update | |
3732 | */ | |
3733 | trans = btrfs_start_transaction(root, 2); | |
3734 | if (IS_ERR(trans)) { | |
3735 | ret = PTR_ERR(trans); | |
3736 | goto out; | |
3737 | } | |
3738 | ret = btrfs_drop_extents(trans, root, inode, | |
3739 | last_dest_end, destoff + len, 1); | |
3740 | if (ret) { | |
3741 | if (ret != -EOPNOTSUPP) | |
66642832 | 3742 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 3743 | btrfs_end_transaction(trans); |
f82a9901 FM |
3744 | goto out; |
3745 | } | |
a2f392e4 NB |
3746 | clone_update_extent_map(BTRFS_I(inode), trans, NULL, |
3747 | last_dest_end, | |
3748 | destoff + len - last_dest_end); | |
f82a9901 | 3749 | ret = clone_finish_inode_update(trans, inode, destoff + len, |
1c919a5e | 3750 | destoff, olen, no_time_update); |
f82a9901 FM |
3751 | } |
3752 | ||
f46b5a66 | 3753 | out: |
32b7c687 | 3754 | btrfs_free_path(path); |
15351955 | 3755 | kvfree(buf); |
32b7c687 MF |
3756 | return ret; |
3757 | } | |
3758 | ||
3db11b2e ZB |
3759 | static noinline int btrfs_clone_files(struct file *file, struct file *file_src, |
3760 | u64 off, u64 olen, u64 destoff) | |
32b7c687 | 3761 | { |
54563d41 | 3762 | struct inode *inode = file_inode(file); |
3db11b2e | 3763 | struct inode *src = file_inode(file_src); |
0b246afa | 3764 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
32b7c687 | 3765 | struct btrfs_root *root = BTRFS_I(inode)->root; |
32b7c687 MF |
3766 | int ret; |
3767 | u64 len = olen; | |
0b246afa | 3768 | u64 bs = fs_info->sb->s_blocksize; |
3db11b2e | 3769 | int same_inode = src == inode; |
32b7c687 MF |
3770 | |
3771 | /* | |
3772 | * TODO: | |
3773 | * - split compressed inline extents. annoying: we need to | |
3774 | * decompress into destination's address_space (the file offset | |
3775 | * may change, so source mapping won't do), then recompress (or | |
3776 | * otherwise reinsert) a subrange. | |
00fdf13a LB |
3777 | * |
3778 | * - split destination inode's inline extents. The inline extents can | |
3779 | * be either compressed or non-compressed. | |
32b7c687 MF |
3780 | */ |
3781 | ||
32b7c687 MF |
3782 | if (btrfs_root_readonly(root)) |
3783 | return -EROFS; | |
3784 | ||
3db11b2e ZB |
3785 | if (file_src->f_path.mnt != file->f_path.mnt || |
3786 | src->i_sb != inode->i_sb) | |
3787 | return -EXDEV; | |
32b7c687 MF |
3788 | |
3789 | /* don't make the dst file partly checksummed */ | |
3790 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != | |
3791 | (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) | |
3db11b2e | 3792 | return -EINVAL; |
32b7c687 | 3793 | |
32b7c687 | 3794 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
3db11b2e | 3795 | return -EISDIR; |
32b7c687 MF |
3796 | |
3797 | if (!same_inode) { | |
293a8489 | 3798 | btrfs_double_inode_lock(src, inode); |
32b7c687 | 3799 | } else { |
5955102c | 3800 | inode_lock(src); |
32b7c687 MF |
3801 | } |
3802 | ||
3803 | /* determine range to clone */ | |
3804 | ret = -EINVAL; | |
3805 | if (off + len > src->i_size || off + len < off) | |
3806 | goto out_unlock; | |
3807 | if (len == 0) | |
3808 | olen = len = src->i_size - off; | |
3809 | /* if we extend to eof, continue to block boundary */ | |
3810 | if (off + len == src->i_size) | |
3811 | len = ALIGN(src->i_size, bs) - off; | |
3812 | ||
ccccf3d6 FM |
3813 | if (len == 0) { |
3814 | ret = 0; | |
3815 | goto out_unlock; | |
3816 | } | |
3817 | ||
32b7c687 MF |
3818 | /* verify the end result is block aligned */ |
3819 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || | |
3820 | !IS_ALIGNED(destoff, bs)) | |
3821 | goto out_unlock; | |
3822 | ||
3823 | /* verify if ranges are overlapped within the same file */ | |
3824 | if (same_inode) { | |
3825 | if (destoff + len > off && destoff < off + len) | |
3826 | goto out_unlock; | |
3827 | } | |
3828 | ||
3829 | if (destoff > inode->i_size) { | |
3830 | ret = btrfs_cont_expand(inode, inode->i_size, destoff); | |
3831 | if (ret) | |
3832 | goto out_unlock; | |
3833 | } | |
3834 | ||
c125b8bf FM |
3835 | /* |
3836 | * Lock the target range too. Right after we replace the file extent | |
3837 | * items in the fs tree (which now point to the cloned data), we might | |
3838 | * have a worker replace them with extent items relative to a write | |
3839 | * operation that was issued before this clone operation (i.e. confront | |
3840 | * with inode.c:btrfs_finish_ordered_io). | |
3841 | */ | |
3842 | if (same_inode) { | |
3843 | u64 lock_start = min_t(u64, off, destoff); | |
3844 | u64 lock_len = max_t(u64, off, destoff) + len - lock_start; | |
32b7c687 | 3845 | |
e0bd70c6 | 3846 | ret = lock_extent_range(src, lock_start, lock_len, true); |
c125b8bf | 3847 | } else { |
e0bd70c6 FM |
3848 | ret = btrfs_double_extent_lock(src, off, inode, destoff, len, |
3849 | true); | |
3850 | } | |
3851 | ASSERT(ret == 0); | |
3852 | if (WARN_ON(ret)) { | |
3853 | /* ranges in the io trees already unlocked */ | |
3854 | goto out_unlock; | |
c125b8bf | 3855 | } |
32b7c687 | 3856 | |
1c919a5e | 3857 | ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); |
32b7c687 | 3858 | |
c125b8bf FM |
3859 | if (same_inode) { |
3860 | u64 lock_start = min_t(u64, off, destoff); | |
3861 | u64 lock_end = max_t(u64, off, destoff) + len - 1; | |
3862 | ||
3863 | unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end); | |
3864 | } else { | |
293a8489 | 3865 | btrfs_double_extent_unlock(src, off, inode, destoff, len); |
c125b8bf FM |
3866 | } |
3867 | /* | |
3868 | * Truncate page cache pages so that future reads will see the cloned | |
3869 | * data immediately and not the previous data. | |
3870 | */ | |
65bfa658 | 3871 | truncate_inode_pages_range(&inode->i_data, |
09cbfeaf KS |
3872 | round_down(destoff, PAGE_SIZE), |
3873 | round_up(destoff + len, PAGE_SIZE) - 1); | |
f46b5a66 | 3874 | out_unlock: |
293a8489 MF |
3875 | if (!same_inode) |
3876 | btrfs_double_inode_unlock(src, inode); | |
3877 | else | |
5955102c | 3878 | inode_unlock(src); |
3db11b2e ZB |
3879 | return ret; |
3880 | } | |
3881 | ||
04b38d60 CH |
3882 | int btrfs_clone_file_range(struct file *src_file, loff_t off, |
3883 | struct file *dst_file, loff_t destoff, u64 len) | |
3db11b2e | 3884 | { |
04b38d60 | 3885 | return btrfs_clone_files(dst_file, src_file, off, len, destoff); |
c5c9cd4d SW |
3886 | } |
3887 | ||
6ef5ed0d JB |
3888 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
3889 | { | |
496ad9aa | 3890 | struct inode *inode = file_inode(file); |
0b246afa | 3891 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
6ef5ed0d JB |
3892 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3893 | struct btrfs_root *new_root; | |
3894 | struct btrfs_dir_item *di; | |
3895 | struct btrfs_trans_handle *trans; | |
3896 | struct btrfs_path *path; | |
3897 | struct btrfs_key location; | |
3898 | struct btrfs_disk_key disk_key; | |
6ef5ed0d JB |
3899 | u64 objectid = 0; |
3900 | u64 dir_id; | |
3c04ce01 | 3901 | int ret; |
6ef5ed0d JB |
3902 | |
3903 | if (!capable(CAP_SYS_ADMIN)) | |
3904 | return -EPERM; | |
3905 | ||
3c04ce01 MX |
3906 | ret = mnt_want_write_file(file); |
3907 | if (ret) | |
3908 | return ret; | |
3909 | ||
3910 | if (copy_from_user(&objectid, argp, sizeof(objectid))) { | |
3911 | ret = -EFAULT; | |
3912 | goto out; | |
3913 | } | |
6ef5ed0d JB |
3914 | |
3915 | if (!objectid) | |
1cecf579 | 3916 | objectid = BTRFS_FS_TREE_OBJECTID; |
6ef5ed0d JB |
3917 | |
3918 | location.objectid = objectid; | |
3919 | location.type = BTRFS_ROOT_ITEM_KEY; | |
3920 | location.offset = (u64)-1; | |
3921 | ||
0b246afa | 3922 | new_root = btrfs_read_fs_root_no_name(fs_info, &location); |
3c04ce01 MX |
3923 | if (IS_ERR(new_root)) { |
3924 | ret = PTR_ERR(new_root); | |
3925 | goto out; | |
3926 | } | |
6d6d2829 | 3927 | if (!is_fstree(new_root->objectid)) { |
3928 | ret = -ENOENT; | |
3929 | goto out; | |
3930 | } | |
6ef5ed0d | 3931 | |
6ef5ed0d | 3932 | path = btrfs_alloc_path(); |
3c04ce01 MX |
3933 | if (!path) { |
3934 | ret = -ENOMEM; | |
3935 | goto out; | |
3936 | } | |
6ef5ed0d JB |
3937 | path->leave_spinning = 1; |
3938 | ||
3939 | trans = btrfs_start_transaction(root, 1); | |
98d5dc13 | 3940 | if (IS_ERR(trans)) { |
6ef5ed0d | 3941 | btrfs_free_path(path); |
3c04ce01 MX |
3942 | ret = PTR_ERR(trans); |
3943 | goto out; | |
6ef5ed0d JB |
3944 | } |
3945 | ||
0b246afa JM |
3946 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
3947 | di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, | |
6ef5ed0d | 3948 | dir_id, "default", 7, 1); |
cf1e99a4 | 3949 | if (IS_ERR_OR_NULL(di)) { |
6ef5ed0d | 3950 | btrfs_free_path(path); |
3a45bb20 | 3951 | btrfs_end_transaction(trans); |
0b246afa | 3952 | btrfs_err(fs_info, |
5d163e0e | 3953 | "Umm, you don't have the default diritem, this isn't going to work"); |
3c04ce01 MX |
3954 | ret = -ENOENT; |
3955 | goto out; | |
6ef5ed0d JB |
3956 | } |
3957 | ||
3958 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); | |
3959 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); | |
3960 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
3961 | btrfs_free_path(path); | |
3962 | ||
0b246afa | 3963 | btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); |
3a45bb20 | 3964 | btrfs_end_transaction(trans); |
3c04ce01 MX |
3965 | out: |
3966 | mnt_drop_write_file(file); | |
3967 | return ret; | |
6ef5ed0d JB |
3968 | } |
3969 | ||
c065f5b1 SY |
3970 | static void get_block_group_info(struct list_head *groups_list, |
3971 | struct btrfs_ioctl_space_info *space) | |
bf5fc093 JB |
3972 | { |
3973 | struct btrfs_block_group_cache *block_group; | |
3974 | ||
3975 | space->total_bytes = 0; | |
3976 | space->used_bytes = 0; | |
3977 | space->flags = 0; | |
3978 | list_for_each_entry(block_group, groups_list, list) { | |
3979 | space->flags = block_group->flags; | |
3980 | space->total_bytes += block_group->key.offset; | |
3981 | space->used_bytes += | |
3982 | btrfs_block_group_used(&block_group->item); | |
3983 | } | |
3984 | } | |
3985 | ||
2ff7e61e JM |
3986 | static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, |
3987 | void __user *arg) | |
1406e432 JB |
3988 | { |
3989 | struct btrfs_ioctl_space_args space_args; | |
3990 | struct btrfs_ioctl_space_info space; | |
3991 | struct btrfs_ioctl_space_info *dest; | |
7fde62bf | 3992 | struct btrfs_ioctl_space_info *dest_orig; |
13f2696f | 3993 | struct btrfs_ioctl_space_info __user *user_dest; |
1406e432 | 3994 | struct btrfs_space_info *info; |
315d8e98 CIK |
3995 | static const u64 types[] = { |
3996 | BTRFS_BLOCK_GROUP_DATA, | |
3997 | BTRFS_BLOCK_GROUP_SYSTEM, | |
3998 | BTRFS_BLOCK_GROUP_METADATA, | |
3999 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA | |
4000 | }; | |
bf5fc093 | 4001 | int num_types = 4; |
7fde62bf | 4002 | int alloc_size; |
1406e432 | 4003 | int ret = 0; |
51788b1b | 4004 | u64 slot_count = 0; |
bf5fc093 | 4005 | int i, c; |
1406e432 JB |
4006 | |
4007 | if (copy_from_user(&space_args, | |
4008 | (struct btrfs_ioctl_space_args __user *)arg, | |
4009 | sizeof(space_args))) | |
4010 | return -EFAULT; | |
4011 | ||
bf5fc093 JB |
4012 | for (i = 0; i < num_types; i++) { |
4013 | struct btrfs_space_info *tmp; | |
4014 | ||
4015 | info = NULL; | |
4016 | rcu_read_lock(); | |
0b246afa | 4017 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
bf5fc093 JB |
4018 | list) { |
4019 | if (tmp->flags == types[i]) { | |
4020 | info = tmp; | |
4021 | break; | |
4022 | } | |
4023 | } | |
4024 | rcu_read_unlock(); | |
4025 | ||
4026 | if (!info) | |
4027 | continue; | |
4028 | ||
4029 | down_read(&info->groups_sem); | |
4030 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
4031 | if (!list_empty(&info->block_groups[c])) | |
4032 | slot_count++; | |
4033 | } | |
4034 | up_read(&info->groups_sem); | |
4035 | } | |
7fde62bf | 4036 | |
36523e95 DS |
4037 | /* |
4038 | * Global block reserve, exported as a space_info | |
4039 | */ | |
4040 | slot_count++; | |
4041 | ||
7fde62bf CM |
4042 | /* space_slots == 0 means they are asking for a count */ |
4043 | if (space_args.space_slots == 0) { | |
4044 | space_args.total_spaces = slot_count; | |
4045 | goto out; | |
4046 | } | |
bf5fc093 | 4047 | |
51788b1b | 4048 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
bf5fc093 | 4049 | |
7fde62bf | 4050 | alloc_size = sizeof(*dest) * slot_count; |
bf5fc093 | 4051 | |
7fde62bf CM |
4052 | /* we generally have at most 6 or so space infos, one for each raid |
4053 | * level. So, a whole page should be more than enough for everyone | |
4054 | */ | |
09cbfeaf | 4055 | if (alloc_size > PAGE_SIZE) |
7fde62bf CM |
4056 | return -ENOMEM; |
4057 | ||
1406e432 | 4058 | space_args.total_spaces = 0; |
8d2db785 | 4059 | dest = kmalloc(alloc_size, GFP_KERNEL); |
7fde62bf CM |
4060 | if (!dest) |
4061 | return -ENOMEM; | |
4062 | dest_orig = dest; | |
1406e432 | 4063 | |
7fde62bf | 4064 | /* now we have a buffer to copy into */ |
bf5fc093 JB |
4065 | for (i = 0; i < num_types; i++) { |
4066 | struct btrfs_space_info *tmp; | |
4067 | ||
51788b1b DR |
4068 | if (!slot_count) |
4069 | break; | |
4070 | ||
bf5fc093 JB |
4071 | info = NULL; |
4072 | rcu_read_lock(); | |
0b246afa | 4073 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
bf5fc093 JB |
4074 | list) { |
4075 | if (tmp->flags == types[i]) { | |
4076 | info = tmp; | |
4077 | break; | |
4078 | } | |
4079 | } | |
4080 | rcu_read_unlock(); | |
7fde62bf | 4081 | |
bf5fc093 JB |
4082 | if (!info) |
4083 | continue; | |
4084 | down_read(&info->groups_sem); | |
4085 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
4086 | if (!list_empty(&info->block_groups[c])) { | |
c065f5b1 SY |
4087 | get_block_group_info(&info->block_groups[c], |
4088 | &space); | |
bf5fc093 JB |
4089 | memcpy(dest, &space, sizeof(space)); |
4090 | dest++; | |
4091 | space_args.total_spaces++; | |
51788b1b | 4092 | slot_count--; |
bf5fc093 | 4093 | } |
51788b1b DR |
4094 | if (!slot_count) |
4095 | break; | |
bf5fc093 JB |
4096 | } |
4097 | up_read(&info->groups_sem); | |
1406e432 | 4098 | } |
1406e432 | 4099 | |
36523e95 DS |
4100 | /* |
4101 | * Add global block reserve | |
4102 | */ | |
4103 | if (slot_count) { | |
0b246afa | 4104 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
36523e95 DS |
4105 | |
4106 | spin_lock(&block_rsv->lock); | |
4107 | space.total_bytes = block_rsv->size; | |
4108 | space.used_bytes = block_rsv->size - block_rsv->reserved; | |
4109 | spin_unlock(&block_rsv->lock); | |
4110 | space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; | |
4111 | memcpy(dest, &space, sizeof(space)); | |
4112 | space_args.total_spaces++; | |
4113 | } | |
4114 | ||
2eec6c81 | 4115 | user_dest = (struct btrfs_ioctl_space_info __user *) |
7fde62bf CM |
4116 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
4117 | ||
4118 | if (copy_to_user(user_dest, dest_orig, alloc_size)) | |
4119 | ret = -EFAULT; | |
4120 | ||
4121 | kfree(dest_orig); | |
4122 | out: | |
4123 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) | |
1406e432 JB |
4124 | ret = -EFAULT; |
4125 | ||
4126 | return ret; | |
4127 | } | |
4128 | ||
9a8c28be MX |
4129 | static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, |
4130 | void __user *argp) | |
46204592 | 4131 | { |
46204592 SW |
4132 | struct btrfs_trans_handle *trans; |
4133 | u64 transid; | |
db5b493a | 4134 | int ret; |
46204592 | 4135 | |
d4edf39b | 4136 | trans = btrfs_attach_transaction_barrier(root); |
ff7c1d33 MX |
4137 | if (IS_ERR(trans)) { |
4138 | if (PTR_ERR(trans) != -ENOENT) | |
4139 | return PTR_ERR(trans); | |
4140 | ||
4141 | /* No running transaction, don't bother */ | |
4142 | transid = root->fs_info->last_trans_committed; | |
4143 | goto out; | |
4144 | } | |
46204592 | 4145 | transid = trans->transid; |
3a45bb20 | 4146 | ret = btrfs_commit_transaction_async(trans, 0); |
8b2b2d3c | 4147 | if (ret) { |
3a45bb20 | 4148 | btrfs_end_transaction(trans); |
db5b493a | 4149 | return ret; |
8b2b2d3c | 4150 | } |
ff7c1d33 | 4151 | out: |
46204592 SW |
4152 | if (argp) |
4153 | if (copy_to_user(argp, &transid, sizeof(transid))) | |
4154 | return -EFAULT; | |
4155 | return 0; | |
4156 | } | |
4157 | ||
2ff7e61e | 4158 | static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, |
9a8c28be | 4159 | void __user *argp) |
46204592 | 4160 | { |
46204592 SW |
4161 | u64 transid; |
4162 | ||
4163 | if (argp) { | |
4164 | if (copy_from_user(&transid, argp, sizeof(transid))) | |
4165 | return -EFAULT; | |
4166 | } else { | |
4167 | transid = 0; /* current trans */ | |
4168 | } | |
2ff7e61e | 4169 | return btrfs_wait_for_commit(fs_info, transid); |
46204592 SW |
4170 | } |
4171 | ||
b8e95489 | 4172 | static long btrfs_ioctl_scrub(struct file *file, void __user *arg) |
475f6387 | 4173 | { |
0b246afa | 4174 | struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); |
475f6387 | 4175 | struct btrfs_ioctl_scrub_args *sa; |
b8e95489 | 4176 | int ret; |
475f6387 JS |
4177 | |
4178 | if (!capable(CAP_SYS_ADMIN)) | |
4179 | return -EPERM; | |
4180 | ||
4181 | sa = memdup_user(arg, sizeof(*sa)); | |
4182 | if (IS_ERR(sa)) | |
4183 | return PTR_ERR(sa); | |
4184 | ||
b8e95489 MX |
4185 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) { |
4186 | ret = mnt_want_write_file(file); | |
4187 | if (ret) | |
4188 | goto out; | |
4189 | } | |
4190 | ||
0b246afa | 4191 | ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, |
63a212ab SB |
4192 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, |
4193 | 0); | |
475f6387 JS |
4194 | |
4195 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
4196 | ret = -EFAULT; | |
4197 | ||
b8e95489 MX |
4198 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) |
4199 | mnt_drop_write_file(file); | |
4200 | out: | |
475f6387 JS |
4201 | kfree(sa); |
4202 | return ret; | |
4203 | } | |
4204 | ||
2ff7e61e | 4205 | static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) |
475f6387 JS |
4206 | { |
4207 | if (!capable(CAP_SYS_ADMIN)) | |
4208 | return -EPERM; | |
4209 | ||
2ff7e61e | 4210 | return btrfs_scrub_cancel(fs_info); |
475f6387 JS |
4211 | } |
4212 | ||
2ff7e61e | 4213 | static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, |
475f6387 JS |
4214 | void __user *arg) |
4215 | { | |
4216 | struct btrfs_ioctl_scrub_args *sa; | |
4217 | int ret; | |
4218 | ||
4219 | if (!capable(CAP_SYS_ADMIN)) | |
4220 | return -EPERM; | |
4221 | ||
4222 | sa = memdup_user(arg, sizeof(*sa)); | |
4223 | if (IS_ERR(sa)) | |
4224 | return PTR_ERR(sa); | |
4225 | ||
2ff7e61e | 4226 | ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); |
475f6387 JS |
4227 | |
4228 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
4229 | ret = -EFAULT; | |
4230 | ||
4231 | kfree(sa); | |
4232 | return ret; | |
4233 | } | |
4234 | ||
2ff7e61e | 4235 | static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, |
b27f7c0c | 4236 | void __user *arg) |
c11d2c23 SB |
4237 | { |
4238 | struct btrfs_ioctl_get_dev_stats *sa; | |
4239 | int ret; | |
4240 | ||
c11d2c23 SB |
4241 | sa = memdup_user(arg, sizeof(*sa)); |
4242 | if (IS_ERR(sa)) | |
4243 | return PTR_ERR(sa); | |
4244 | ||
b27f7c0c DS |
4245 | if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { |
4246 | kfree(sa); | |
4247 | return -EPERM; | |
4248 | } | |
4249 | ||
2ff7e61e | 4250 | ret = btrfs_get_dev_stats(fs_info, sa); |
c11d2c23 SB |
4251 | |
4252 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
4253 | ret = -EFAULT; | |
4254 | ||
4255 | kfree(sa); | |
4256 | return ret; | |
4257 | } | |
4258 | ||
2ff7e61e JM |
4259 | static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, |
4260 | void __user *arg) | |
3f6bcfbd SB |
4261 | { |
4262 | struct btrfs_ioctl_dev_replace_args *p; | |
4263 | int ret; | |
4264 | ||
4265 | if (!capable(CAP_SYS_ADMIN)) | |
4266 | return -EPERM; | |
4267 | ||
4268 | p = memdup_user(arg, sizeof(*p)); | |
4269 | if (IS_ERR(p)) | |
4270 | return PTR_ERR(p); | |
4271 | ||
4272 | switch (p->cmd) { | |
4273 | case BTRFS_IOCTL_DEV_REPLACE_CMD_START: | |
bc98a42c | 4274 | if (sb_rdonly(fs_info->sb)) { |
adfa97cb ID |
4275 | ret = -EROFS; |
4276 | goto out; | |
4277 | } | |
171938e5 | 4278 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
e57138b3 | 4279 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
3f6bcfbd | 4280 | } else { |
2ff7e61e | 4281 | ret = btrfs_dev_replace_by_ioctl(fs_info, p); |
171938e5 | 4282 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
3f6bcfbd SB |
4283 | } |
4284 | break; | |
4285 | case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: | |
0b246afa | 4286 | btrfs_dev_replace_status(fs_info, p); |
3f6bcfbd SB |
4287 | ret = 0; |
4288 | break; | |
4289 | case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: | |
17d202b9 | 4290 | p->result = btrfs_dev_replace_cancel(fs_info); |
97282031 | 4291 | ret = 0; |
3f6bcfbd SB |
4292 | break; |
4293 | default: | |
4294 | ret = -EINVAL; | |
4295 | break; | |
4296 | } | |
4297 | ||
4298 | if (copy_to_user(arg, p, sizeof(*p))) | |
4299 | ret = -EFAULT; | |
adfa97cb | 4300 | out: |
3f6bcfbd SB |
4301 | kfree(p); |
4302 | return ret; | |
4303 | } | |
4304 | ||
d7728c96 JS |
4305 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
4306 | { | |
4307 | int ret = 0; | |
4308 | int i; | |
740c3d22 | 4309 | u64 rel_ptr; |
d7728c96 | 4310 | int size; |
806468f8 | 4311 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
d7728c96 JS |
4312 | struct inode_fs_paths *ipath = NULL; |
4313 | struct btrfs_path *path; | |
4314 | ||
82b22ac8 | 4315 | if (!capable(CAP_DAC_READ_SEARCH)) |
d7728c96 JS |
4316 | return -EPERM; |
4317 | ||
4318 | path = btrfs_alloc_path(); | |
4319 | if (!path) { | |
4320 | ret = -ENOMEM; | |
4321 | goto out; | |
4322 | } | |
4323 | ||
4324 | ipa = memdup_user(arg, sizeof(*ipa)); | |
4325 | if (IS_ERR(ipa)) { | |
4326 | ret = PTR_ERR(ipa); | |
4327 | ipa = NULL; | |
4328 | goto out; | |
4329 | } | |
4330 | ||
4331 | size = min_t(u32, ipa->size, 4096); | |
4332 | ipath = init_ipath(size, root, path); | |
4333 | if (IS_ERR(ipath)) { | |
4334 | ret = PTR_ERR(ipath); | |
4335 | ipath = NULL; | |
4336 | goto out; | |
4337 | } | |
4338 | ||
4339 | ret = paths_from_inode(ipa->inum, ipath); | |
4340 | if (ret < 0) | |
4341 | goto out; | |
4342 | ||
4343 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { | |
745c4d8e JM |
4344 | rel_ptr = ipath->fspath->val[i] - |
4345 | (u64)(unsigned long)ipath->fspath->val; | |
740c3d22 | 4346 | ipath->fspath->val[i] = rel_ptr; |
d7728c96 JS |
4347 | } |
4348 | ||
718dc5fa OS |
4349 | ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, |
4350 | ipath->fspath, size); | |
d7728c96 JS |
4351 | if (ret) { |
4352 | ret = -EFAULT; | |
4353 | goto out; | |
4354 | } | |
4355 | ||
4356 | out: | |
4357 | btrfs_free_path(path); | |
4358 | free_ipath(ipath); | |
4359 | kfree(ipa); | |
4360 | ||
4361 | return ret; | |
4362 | } | |
4363 | ||
4364 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) | |
4365 | { | |
4366 | struct btrfs_data_container *inodes = ctx; | |
4367 | const size_t c = 3 * sizeof(u64); | |
4368 | ||
4369 | if (inodes->bytes_left >= c) { | |
4370 | inodes->bytes_left -= c; | |
4371 | inodes->val[inodes->elem_cnt] = inum; | |
4372 | inodes->val[inodes->elem_cnt + 1] = offset; | |
4373 | inodes->val[inodes->elem_cnt + 2] = root; | |
4374 | inodes->elem_cnt += 3; | |
4375 | } else { | |
4376 | inodes->bytes_missing += c - inodes->bytes_left; | |
4377 | inodes->bytes_left = 0; | |
4378 | inodes->elem_missed += 3; | |
4379 | } | |
4380 | ||
4381 | return 0; | |
4382 | } | |
4383 | ||
2ff7e61e | 4384 | static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, |
d24a67b2 | 4385 | void __user *arg, int version) |
d7728c96 JS |
4386 | { |
4387 | int ret = 0; | |
4388 | int size; | |
d7728c96 JS |
4389 | struct btrfs_ioctl_logical_ino_args *loi; |
4390 | struct btrfs_data_container *inodes = NULL; | |
4391 | struct btrfs_path *path = NULL; | |
d24a67b2 | 4392 | bool ignore_offset; |
d7728c96 JS |
4393 | |
4394 | if (!capable(CAP_SYS_ADMIN)) | |
4395 | return -EPERM; | |
4396 | ||
4397 | loi = memdup_user(arg, sizeof(*loi)); | |
7b9ea627 SV |
4398 | if (IS_ERR(loi)) |
4399 | return PTR_ERR(loi); | |
d7728c96 | 4400 | |
d24a67b2 ZB |
4401 | if (version == 1) { |
4402 | ignore_offset = false; | |
b115e3bc | 4403 | size = min_t(u32, loi->size, SZ_64K); |
d24a67b2 ZB |
4404 | } else { |
4405 | /* All reserved bits must be 0 for now */ | |
4406 | if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { | |
4407 | ret = -EINVAL; | |
4408 | goto out_loi; | |
4409 | } | |
4410 | /* Only accept flags we have defined so far */ | |
4411 | if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { | |
4412 | ret = -EINVAL; | |
4413 | goto out_loi; | |
4414 | } | |
4415 | ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; | |
b115e3bc | 4416 | size = min_t(u32, loi->size, SZ_16M); |
d24a67b2 ZB |
4417 | } |
4418 | ||
d7728c96 JS |
4419 | path = btrfs_alloc_path(); |
4420 | if (!path) { | |
4421 | ret = -ENOMEM; | |
4422 | goto out; | |
4423 | } | |
4424 | ||
d7728c96 JS |
4425 | inodes = init_data_container(size); |
4426 | if (IS_ERR(inodes)) { | |
4427 | ret = PTR_ERR(inodes); | |
4428 | inodes = NULL; | |
4429 | goto out; | |
4430 | } | |
4431 | ||
2ff7e61e | 4432 | ret = iterate_inodes_from_logical(loi->logical, fs_info, path, |
d24a67b2 | 4433 | build_ino_list, inodes, ignore_offset); |
df031f07 | 4434 | if (ret == -EINVAL) |
d7728c96 JS |
4435 | ret = -ENOENT; |
4436 | if (ret < 0) | |
4437 | goto out; | |
4438 | ||
718dc5fa OS |
4439 | ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, |
4440 | size); | |
d7728c96 JS |
4441 | if (ret) |
4442 | ret = -EFAULT; | |
4443 | ||
4444 | out: | |
4445 | btrfs_free_path(path); | |
f54de068 | 4446 | kvfree(inodes); |
d24a67b2 | 4447 | out_loi: |
d7728c96 JS |
4448 | kfree(loi); |
4449 | ||
4450 | return ret; | |
4451 | } | |
4452 | ||
008ef096 | 4453 | void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, |
c9e9f97b ID |
4454 | struct btrfs_ioctl_balance_args *bargs) |
4455 | { | |
4456 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
4457 | ||
4458 | bargs->flags = bctl->flags; | |
4459 | ||
3009a62f | 4460 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) |
837d5b6e ID |
4461 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; |
4462 | if (atomic_read(&fs_info->balance_pause_req)) | |
4463 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; | |
a7e99c69 ID |
4464 | if (atomic_read(&fs_info->balance_cancel_req)) |
4465 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; | |
837d5b6e | 4466 | |
c9e9f97b ID |
4467 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
4468 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); | |
4469 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); | |
19a39dce | 4470 | |
008ef096 DS |
4471 | spin_lock(&fs_info->balance_lock); |
4472 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); | |
4473 | spin_unlock(&fs_info->balance_lock); | |
c9e9f97b ID |
4474 | } |
4475 | ||
9ba1f6e4 | 4476 | static long btrfs_ioctl_balance(struct file *file, void __user *arg) |
c9e9f97b | 4477 | { |
496ad9aa | 4478 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
c9e9f97b ID |
4479 | struct btrfs_fs_info *fs_info = root->fs_info; |
4480 | struct btrfs_ioctl_balance_args *bargs; | |
4481 | struct btrfs_balance_control *bctl; | |
ed0fb78f | 4482 | bool need_unlock; /* for mut. excl. ops lock */ |
c9e9f97b ID |
4483 | int ret; |
4484 | ||
4485 | if (!capable(CAP_SYS_ADMIN)) | |
4486 | return -EPERM; | |
4487 | ||
e54bfa31 | 4488 | ret = mnt_want_write_file(file); |
9ba1f6e4 LB |
4489 | if (ret) |
4490 | return ret; | |
4491 | ||
ed0fb78f | 4492 | again: |
171938e5 | 4493 | if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
ed0fb78f ID |
4494 | mutex_lock(&fs_info->balance_mutex); |
4495 | need_unlock = true; | |
4496 | goto locked; | |
4497 | } | |
4498 | ||
4499 | /* | |
01327610 | 4500 | * mut. excl. ops lock is locked. Three possibilities: |
ed0fb78f ID |
4501 | * (1) some other op is running |
4502 | * (2) balance is running | |
4503 | * (3) balance is paused -- special case (think resume) | |
4504 | */ | |
c9e9f97b | 4505 | mutex_lock(&fs_info->balance_mutex); |
ed0fb78f ID |
4506 | if (fs_info->balance_ctl) { |
4507 | /* this is either (2) or (3) */ | |
3009a62f | 4508 | if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
ed0fb78f | 4509 | mutex_unlock(&fs_info->balance_mutex); |
dccdb07b DS |
4510 | /* |
4511 | * Lock released to allow other waiters to continue, | |
4512 | * we'll reexamine the status again. | |
4513 | */ | |
ed0fb78f ID |
4514 | mutex_lock(&fs_info->balance_mutex); |
4515 | ||
4516 | if (fs_info->balance_ctl && | |
3009a62f | 4517 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
ed0fb78f ID |
4518 | /* this is (3) */ |
4519 | need_unlock = false; | |
4520 | goto locked; | |
4521 | } | |
4522 | ||
4523 | mutex_unlock(&fs_info->balance_mutex); | |
ed0fb78f ID |
4524 | goto again; |
4525 | } else { | |
4526 | /* this is (2) */ | |
4527 | mutex_unlock(&fs_info->balance_mutex); | |
4528 | ret = -EINPROGRESS; | |
4529 | goto out; | |
4530 | } | |
4531 | } else { | |
4532 | /* this is (1) */ | |
4533 | mutex_unlock(&fs_info->balance_mutex); | |
e57138b3 | 4534 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
ed0fb78f ID |
4535 | goto out; |
4536 | } | |
4537 | ||
4538 | locked: | |
171938e5 | 4539 | BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); |
c9e9f97b ID |
4540 | |
4541 | if (arg) { | |
4542 | bargs = memdup_user(arg, sizeof(*bargs)); | |
4543 | if (IS_ERR(bargs)) { | |
4544 | ret = PTR_ERR(bargs); | |
ed0fb78f | 4545 | goto out_unlock; |
c9e9f97b | 4546 | } |
de322263 ID |
4547 | |
4548 | if (bargs->flags & BTRFS_BALANCE_RESUME) { | |
4549 | if (!fs_info->balance_ctl) { | |
4550 | ret = -ENOTCONN; | |
4551 | goto out_bargs; | |
4552 | } | |
4553 | ||
4554 | bctl = fs_info->balance_ctl; | |
4555 | spin_lock(&fs_info->balance_lock); | |
4556 | bctl->flags |= BTRFS_BALANCE_RESUME; | |
4557 | spin_unlock(&fs_info->balance_lock); | |
4558 | ||
4559 | goto do_balance; | |
4560 | } | |
c9e9f97b ID |
4561 | } else { |
4562 | bargs = NULL; | |
4563 | } | |
4564 | ||
ed0fb78f | 4565 | if (fs_info->balance_ctl) { |
837d5b6e ID |
4566 | ret = -EINPROGRESS; |
4567 | goto out_bargs; | |
4568 | } | |
4569 | ||
8d2db785 | 4570 | bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); |
c9e9f97b ID |
4571 | if (!bctl) { |
4572 | ret = -ENOMEM; | |
4573 | goto out_bargs; | |
4574 | } | |
4575 | ||
c9e9f97b ID |
4576 | if (arg) { |
4577 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); | |
4578 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); | |
4579 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); | |
4580 | ||
4581 | bctl->flags = bargs->flags; | |
f43ffb60 ID |
4582 | } else { |
4583 | /* balance everything - no filters */ | |
4584 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; | |
c9e9f97b ID |
4585 | } |
4586 | ||
8eb93459 DS |
4587 | if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { |
4588 | ret = -EINVAL; | |
0f89abf5 | 4589 | goto out_bctl; |
8eb93459 DS |
4590 | } |
4591 | ||
de322263 | 4592 | do_balance: |
c9e9f97b | 4593 | /* |
149196a2 DS |
4594 | * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to |
4595 | * btrfs_balance. bctl is freed in reset_balance_state, or, if | |
4596 | * restriper was paused all the way until unmount, in free_fs_info. | |
4597 | * The flag should be cleared after reset_balance_state. | |
c9e9f97b | 4598 | */ |
ed0fb78f ID |
4599 | need_unlock = false; |
4600 | ||
6fcf6e2b | 4601 | ret = btrfs_balance(fs_info, bctl, bargs); |
0f89abf5 | 4602 | bctl = NULL; |
ed0fb78f | 4603 | |
c9e9f97b ID |
4604 | if (arg) { |
4605 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | |
4606 | ret = -EFAULT; | |
4607 | } | |
4608 | ||
0f89abf5 CE |
4609 | out_bctl: |
4610 | kfree(bctl); | |
c9e9f97b ID |
4611 | out_bargs: |
4612 | kfree(bargs); | |
ed0fb78f | 4613 | out_unlock: |
c9e9f97b | 4614 | mutex_unlock(&fs_info->balance_mutex); |
ed0fb78f | 4615 | if (need_unlock) |
171938e5 | 4616 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
ed0fb78f | 4617 | out: |
e54bfa31 | 4618 | mnt_drop_write_file(file); |
c9e9f97b ID |
4619 | return ret; |
4620 | } | |
4621 | ||
2ff7e61e | 4622 | static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) |
837d5b6e ID |
4623 | { |
4624 | if (!capable(CAP_SYS_ADMIN)) | |
4625 | return -EPERM; | |
4626 | ||
4627 | switch (cmd) { | |
4628 | case BTRFS_BALANCE_CTL_PAUSE: | |
0b246afa | 4629 | return btrfs_pause_balance(fs_info); |
a7e99c69 | 4630 | case BTRFS_BALANCE_CTL_CANCEL: |
0b246afa | 4631 | return btrfs_cancel_balance(fs_info); |
837d5b6e ID |
4632 | } |
4633 | ||
4634 | return -EINVAL; | |
4635 | } | |
4636 | ||
2ff7e61e | 4637 | static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, |
19a39dce ID |
4638 | void __user *arg) |
4639 | { | |
19a39dce ID |
4640 | struct btrfs_ioctl_balance_args *bargs; |
4641 | int ret = 0; | |
4642 | ||
4643 | if (!capable(CAP_SYS_ADMIN)) | |
4644 | return -EPERM; | |
4645 | ||
4646 | mutex_lock(&fs_info->balance_mutex); | |
4647 | if (!fs_info->balance_ctl) { | |
4648 | ret = -ENOTCONN; | |
4649 | goto out; | |
4650 | } | |
4651 | ||
8d2db785 | 4652 | bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); |
19a39dce ID |
4653 | if (!bargs) { |
4654 | ret = -ENOMEM; | |
4655 | goto out; | |
4656 | } | |
4657 | ||
008ef096 | 4658 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
19a39dce ID |
4659 | |
4660 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | |
4661 | ret = -EFAULT; | |
4662 | ||
4663 | kfree(bargs); | |
4664 | out: | |
4665 | mutex_unlock(&fs_info->balance_mutex); | |
4666 | return ret; | |
4667 | } | |
4668 | ||
905b0dda | 4669 | static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) |
5d13a37b | 4670 | { |
0b246afa JM |
4671 | struct inode *inode = file_inode(file); |
4672 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5d13a37b AJ |
4673 | struct btrfs_ioctl_quota_ctl_args *sa; |
4674 | struct btrfs_trans_handle *trans = NULL; | |
4675 | int ret; | |
4676 | int err; | |
4677 | ||
4678 | if (!capable(CAP_SYS_ADMIN)) | |
4679 | return -EPERM; | |
4680 | ||
905b0dda MX |
4681 | ret = mnt_want_write_file(file); |
4682 | if (ret) | |
4683 | return ret; | |
5d13a37b AJ |
4684 | |
4685 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4686 | if (IS_ERR(sa)) { |
4687 | ret = PTR_ERR(sa); | |
4688 | goto drop_write; | |
4689 | } | |
5d13a37b | 4690 | |
0b246afa JM |
4691 | down_write(&fs_info->subvol_sem); |
4692 | trans = btrfs_start_transaction(fs_info->tree_root, 2); | |
2f232036 JS |
4693 | if (IS_ERR(trans)) { |
4694 | ret = PTR_ERR(trans); | |
4695 | goto out; | |
5d13a37b AJ |
4696 | } |
4697 | ||
4698 | switch (sa->cmd) { | |
4699 | case BTRFS_QUOTA_CTL_ENABLE: | |
0b246afa | 4700 | ret = btrfs_quota_enable(trans, fs_info); |
5d13a37b AJ |
4701 | break; |
4702 | case BTRFS_QUOTA_CTL_DISABLE: | |
0b246afa | 4703 | ret = btrfs_quota_disable(trans, fs_info); |
5d13a37b | 4704 | break; |
5d13a37b AJ |
4705 | default: |
4706 | ret = -EINVAL; | |
4707 | break; | |
4708 | } | |
4709 | ||
3a45bb20 | 4710 | err = btrfs_commit_transaction(trans); |
2f232036 JS |
4711 | if (err && !ret) |
4712 | ret = err; | |
5d13a37b AJ |
4713 | out: |
4714 | kfree(sa); | |
0b246afa | 4715 | up_write(&fs_info->subvol_sem); |
905b0dda MX |
4716 | drop_write: |
4717 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4718 | return ret; |
4719 | } | |
4720 | ||
905b0dda | 4721 | static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) |
5d13a37b | 4722 | { |
0b246afa JM |
4723 | struct inode *inode = file_inode(file); |
4724 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4725 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5d13a37b AJ |
4726 | struct btrfs_ioctl_qgroup_assign_args *sa; |
4727 | struct btrfs_trans_handle *trans; | |
4728 | int ret; | |
4729 | int err; | |
4730 | ||
4731 | if (!capable(CAP_SYS_ADMIN)) | |
4732 | return -EPERM; | |
4733 | ||
905b0dda MX |
4734 | ret = mnt_want_write_file(file); |
4735 | if (ret) | |
4736 | return ret; | |
5d13a37b AJ |
4737 | |
4738 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4739 | if (IS_ERR(sa)) { |
4740 | ret = PTR_ERR(sa); | |
4741 | goto drop_write; | |
4742 | } | |
5d13a37b AJ |
4743 | |
4744 | trans = btrfs_join_transaction(root); | |
4745 | if (IS_ERR(trans)) { | |
4746 | ret = PTR_ERR(trans); | |
4747 | goto out; | |
4748 | } | |
4749 | ||
5d13a37b | 4750 | if (sa->assign) { |
0b246afa | 4751 | ret = btrfs_add_qgroup_relation(trans, fs_info, |
5d13a37b AJ |
4752 | sa->src, sa->dst); |
4753 | } else { | |
0b246afa | 4754 | ret = btrfs_del_qgroup_relation(trans, fs_info, |
5d13a37b AJ |
4755 | sa->src, sa->dst); |
4756 | } | |
4757 | ||
e082f563 | 4758 | /* update qgroup status and info */ |
0b246afa | 4759 | err = btrfs_run_qgroups(trans, fs_info); |
e082f563 | 4760 | if (err < 0) |
0b246afa JM |
4761 | btrfs_handle_fs_error(fs_info, err, |
4762 | "failed to update qgroup status and info"); | |
3a45bb20 | 4763 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
4764 | if (err && !ret) |
4765 | ret = err; | |
4766 | ||
4767 | out: | |
4768 | kfree(sa); | |
905b0dda MX |
4769 | drop_write: |
4770 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4771 | return ret; |
4772 | } | |
4773 | ||
905b0dda | 4774 | static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) |
5d13a37b | 4775 | { |
0b246afa JM |
4776 | struct inode *inode = file_inode(file); |
4777 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4778 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5d13a37b AJ |
4779 | struct btrfs_ioctl_qgroup_create_args *sa; |
4780 | struct btrfs_trans_handle *trans; | |
4781 | int ret; | |
4782 | int err; | |
4783 | ||
4784 | if (!capable(CAP_SYS_ADMIN)) | |
4785 | return -EPERM; | |
4786 | ||
905b0dda MX |
4787 | ret = mnt_want_write_file(file); |
4788 | if (ret) | |
4789 | return ret; | |
5d13a37b AJ |
4790 | |
4791 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4792 | if (IS_ERR(sa)) { |
4793 | ret = PTR_ERR(sa); | |
4794 | goto drop_write; | |
4795 | } | |
5d13a37b | 4796 | |
d86e56cf MX |
4797 | if (!sa->qgroupid) { |
4798 | ret = -EINVAL; | |
4799 | goto out; | |
4800 | } | |
4801 | ||
5d13a37b AJ |
4802 | trans = btrfs_join_transaction(root); |
4803 | if (IS_ERR(trans)) { | |
4804 | ret = PTR_ERR(trans); | |
4805 | goto out; | |
4806 | } | |
4807 | ||
5d13a37b | 4808 | if (sa->create) { |
0b246afa | 4809 | ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid); |
5d13a37b | 4810 | } else { |
0b246afa | 4811 | ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid); |
5d13a37b AJ |
4812 | } |
4813 | ||
3a45bb20 | 4814 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
4815 | if (err && !ret) |
4816 | ret = err; | |
4817 | ||
4818 | out: | |
4819 | kfree(sa); | |
905b0dda MX |
4820 | drop_write: |
4821 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4822 | return ret; |
4823 | } | |
4824 | ||
905b0dda | 4825 | static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) |
5d13a37b | 4826 | { |
0b246afa JM |
4827 | struct inode *inode = file_inode(file); |
4828 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
4829 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5d13a37b AJ |
4830 | struct btrfs_ioctl_qgroup_limit_args *sa; |
4831 | struct btrfs_trans_handle *trans; | |
4832 | int ret; | |
4833 | int err; | |
4834 | u64 qgroupid; | |
4835 | ||
4836 | if (!capable(CAP_SYS_ADMIN)) | |
4837 | return -EPERM; | |
4838 | ||
905b0dda MX |
4839 | ret = mnt_want_write_file(file); |
4840 | if (ret) | |
4841 | return ret; | |
5d13a37b AJ |
4842 | |
4843 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4844 | if (IS_ERR(sa)) { |
4845 | ret = PTR_ERR(sa); | |
4846 | goto drop_write; | |
4847 | } | |
5d13a37b AJ |
4848 | |
4849 | trans = btrfs_join_transaction(root); | |
4850 | if (IS_ERR(trans)) { | |
4851 | ret = PTR_ERR(trans); | |
4852 | goto out; | |
4853 | } | |
4854 | ||
4855 | qgroupid = sa->qgroupid; | |
4856 | if (!qgroupid) { | |
4857 | /* take the current subvol as qgroup */ | |
4858 | qgroupid = root->root_key.objectid; | |
4859 | } | |
4860 | ||
0b246afa | 4861 | ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim); |
5d13a37b | 4862 | |
3a45bb20 | 4863 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
4864 | if (err && !ret) |
4865 | ret = err; | |
4866 | ||
4867 | out: | |
4868 | kfree(sa); | |
905b0dda MX |
4869 | drop_write: |
4870 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4871 | return ret; |
4872 | } | |
4873 | ||
2f232036 JS |
4874 | static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) |
4875 | { | |
0b246afa JM |
4876 | struct inode *inode = file_inode(file); |
4877 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2f232036 JS |
4878 | struct btrfs_ioctl_quota_rescan_args *qsa; |
4879 | int ret; | |
4880 | ||
4881 | if (!capable(CAP_SYS_ADMIN)) | |
4882 | return -EPERM; | |
4883 | ||
4884 | ret = mnt_want_write_file(file); | |
4885 | if (ret) | |
4886 | return ret; | |
4887 | ||
4888 | qsa = memdup_user(arg, sizeof(*qsa)); | |
4889 | if (IS_ERR(qsa)) { | |
4890 | ret = PTR_ERR(qsa); | |
4891 | goto drop_write; | |
4892 | } | |
4893 | ||
4894 | if (qsa->flags) { | |
4895 | ret = -EINVAL; | |
4896 | goto out; | |
4897 | } | |
4898 | ||
0b246afa | 4899 | ret = btrfs_qgroup_rescan(fs_info); |
2f232036 JS |
4900 | |
4901 | out: | |
4902 | kfree(qsa); | |
4903 | drop_write: | |
4904 | mnt_drop_write_file(file); | |
4905 | return ret; | |
4906 | } | |
4907 | ||
4908 | static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) | |
4909 | { | |
0b246afa JM |
4910 | struct inode *inode = file_inode(file); |
4911 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2f232036 JS |
4912 | struct btrfs_ioctl_quota_rescan_args *qsa; |
4913 | int ret = 0; | |
4914 | ||
4915 | if (!capable(CAP_SYS_ADMIN)) | |
4916 | return -EPERM; | |
4917 | ||
8d2db785 | 4918 | qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); |
2f232036 JS |
4919 | if (!qsa) |
4920 | return -ENOMEM; | |
4921 | ||
0b246afa | 4922 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
2f232036 | 4923 | qsa->flags = 1; |
0b246afa | 4924 | qsa->progress = fs_info->qgroup_rescan_progress.objectid; |
2f232036 JS |
4925 | } |
4926 | ||
4927 | if (copy_to_user(arg, qsa, sizeof(*qsa))) | |
4928 | ret = -EFAULT; | |
4929 | ||
4930 | kfree(qsa); | |
4931 | return ret; | |
4932 | } | |
4933 | ||
57254b6e JS |
4934 | static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) |
4935 | { | |
0b246afa JM |
4936 | struct inode *inode = file_inode(file); |
4937 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
57254b6e JS |
4938 | |
4939 | if (!capable(CAP_SYS_ADMIN)) | |
4940 | return -EPERM; | |
4941 | ||
0b246afa | 4942 | return btrfs_qgroup_wait_for_completion(fs_info, true); |
57254b6e JS |
4943 | } |
4944 | ||
abccd00f HM |
4945 | static long _btrfs_ioctl_set_received_subvol(struct file *file, |
4946 | struct btrfs_ioctl_received_subvol_args *sa) | |
8ea05e3a | 4947 | { |
496ad9aa | 4948 | struct inode *inode = file_inode(file); |
0b246afa | 4949 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8ea05e3a AB |
4950 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4951 | struct btrfs_root_item *root_item = &root->root_item; | |
4952 | struct btrfs_trans_handle *trans; | |
c2050a45 | 4953 | struct timespec ct = current_time(inode); |
8ea05e3a | 4954 | int ret = 0; |
dd5f9615 | 4955 | int received_uuid_changed; |
8ea05e3a | 4956 | |
bd60ea0f DS |
4957 | if (!inode_owner_or_capable(inode)) |
4958 | return -EPERM; | |
4959 | ||
8ea05e3a AB |
4960 | ret = mnt_want_write_file(file); |
4961 | if (ret < 0) | |
4962 | return ret; | |
4963 | ||
0b246afa | 4964 | down_write(&fs_info->subvol_sem); |
8ea05e3a | 4965 | |
4a0cc7ca | 4966 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
8ea05e3a AB |
4967 | ret = -EINVAL; |
4968 | goto out; | |
4969 | } | |
4970 | ||
4971 | if (btrfs_root_readonly(root)) { | |
4972 | ret = -EROFS; | |
4973 | goto out; | |
4974 | } | |
4975 | ||
dd5f9615 SB |
4976 | /* |
4977 | * 1 - root item | |
4978 | * 2 - uuid items (received uuid + subvol uuid) | |
4979 | */ | |
4980 | trans = btrfs_start_transaction(root, 3); | |
8ea05e3a AB |
4981 | if (IS_ERR(trans)) { |
4982 | ret = PTR_ERR(trans); | |
4983 | trans = NULL; | |
4984 | goto out; | |
4985 | } | |
4986 | ||
4987 | sa->rtransid = trans->transid; | |
4988 | sa->rtime.sec = ct.tv_sec; | |
4989 | sa->rtime.nsec = ct.tv_nsec; | |
4990 | ||
dd5f9615 SB |
4991 | received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, |
4992 | BTRFS_UUID_SIZE); | |
4993 | if (received_uuid_changed && | |
d87ff758 NB |
4994 | !btrfs_is_empty_uuid(root_item->received_uuid)) { |
4995 | ret = btrfs_uuid_tree_rem(trans, fs_info, | |
4996 | root_item->received_uuid, | |
4997 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4998 | root->root_key.objectid); | |
4999 | if (ret && ret != -ENOENT) { | |
5000 | btrfs_abort_transaction(trans, ret); | |
5001 | btrfs_end_transaction(trans); | |
5002 | goto out; | |
5003 | } | |
5004 | } | |
8ea05e3a AB |
5005 | memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); |
5006 | btrfs_set_root_stransid(root_item, sa->stransid); | |
5007 | btrfs_set_root_rtransid(root_item, sa->rtransid); | |
3cae210f QW |
5008 | btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); |
5009 | btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); | |
5010 | btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); | |
5011 | btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); | |
8ea05e3a | 5012 | |
0b246afa | 5013 | ret = btrfs_update_root(trans, fs_info->tree_root, |
8ea05e3a AB |
5014 | &root->root_key, &root->root_item); |
5015 | if (ret < 0) { | |
3a45bb20 | 5016 | btrfs_end_transaction(trans); |
8ea05e3a | 5017 | goto out; |
dd5f9615 SB |
5018 | } |
5019 | if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { | |
0b246afa | 5020 | ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid, |
dd5f9615 SB |
5021 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
5022 | root->root_key.objectid); | |
5023 | if (ret < 0 && ret != -EEXIST) { | |
66642832 | 5024 | btrfs_abort_transaction(trans, ret); |
efd38150 | 5025 | btrfs_end_transaction(trans); |
8ea05e3a | 5026 | goto out; |
dd5f9615 SB |
5027 | } |
5028 | } | |
3a45bb20 | 5029 | ret = btrfs_commit_transaction(trans); |
abccd00f | 5030 | out: |
0b246afa | 5031 | up_write(&fs_info->subvol_sem); |
abccd00f HM |
5032 | mnt_drop_write_file(file); |
5033 | return ret; | |
5034 | } | |
5035 | ||
5036 | #ifdef CONFIG_64BIT | |
5037 | static long btrfs_ioctl_set_received_subvol_32(struct file *file, | |
5038 | void __user *arg) | |
5039 | { | |
5040 | struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; | |
5041 | struct btrfs_ioctl_received_subvol_args *args64 = NULL; | |
5042 | int ret = 0; | |
5043 | ||
5044 | args32 = memdup_user(arg, sizeof(*args32)); | |
7b9ea627 SV |
5045 | if (IS_ERR(args32)) |
5046 | return PTR_ERR(args32); | |
abccd00f | 5047 | |
8d2db785 | 5048 | args64 = kmalloc(sizeof(*args64), GFP_KERNEL); |
84dbeb87 DC |
5049 | if (!args64) { |
5050 | ret = -ENOMEM; | |
abccd00f HM |
5051 | goto out; |
5052 | } | |
5053 | ||
5054 | memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); | |
5055 | args64->stransid = args32->stransid; | |
5056 | args64->rtransid = args32->rtransid; | |
5057 | args64->stime.sec = args32->stime.sec; | |
5058 | args64->stime.nsec = args32->stime.nsec; | |
5059 | args64->rtime.sec = args32->rtime.sec; | |
5060 | args64->rtime.nsec = args32->rtime.nsec; | |
5061 | args64->flags = args32->flags; | |
5062 | ||
5063 | ret = _btrfs_ioctl_set_received_subvol(file, args64); | |
5064 | if (ret) | |
5065 | goto out; | |
5066 | ||
5067 | memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); | |
5068 | args32->stransid = args64->stransid; | |
5069 | args32->rtransid = args64->rtransid; | |
5070 | args32->stime.sec = args64->stime.sec; | |
5071 | args32->stime.nsec = args64->stime.nsec; | |
5072 | args32->rtime.sec = args64->rtime.sec; | |
5073 | args32->rtime.nsec = args64->rtime.nsec; | |
5074 | args32->flags = args64->flags; | |
5075 | ||
5076 | ret = copy_to_user(arg, args32, sizeof(*args32)); | |
5077 | if (ret) | |
5078 | ret = -EFAULT; | |
5079 | ||
5080 | out: | |
5081 | kfree(args32); | |
5082 | kfree(args64); | |
5083 | return ret; | |
5084 | } | |
5085 | #endif | |
5086 | ||
5087 | static long btrfs_ioctl_set_received_subvol(struct file *file, | |
5088 | void __user *arg) | |
5089 | { | |
5090 | struct btrfs_ioctl_received_subvol_args *sa = NULL; | |
5091 | int ret = 0; | |
5092 | ||
5093 | sa = memdup_user(arg, sizeof(*sa)); | |
7b9ea627 SV |
5094 | if (IS_ERR(sa)) |
5095 | return PTR_ERR(sa); | |
abccd00f HM |
5096 | |
5097 | ret = _btrfs_ioctl_set_received_subvol(file, sa); | |
5098 | ||
5099 | if (ret) | |
5100 | goto out; | |
5101 | ||
8ea05e3a AB |
5102 | ret = copy_to_user(arg, sa, sizeof(*sa)); |
5103 | if (ret) | |
5104 | ret = -EFAULT; | |
5105 | ||
5106 | out: | |
5107 | kfree(sa); | |
8ea05e3a AB |
5108 | return ret; |
5109 | } | |
5110 | ||
867ab667 | 5111 | static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) |
5112 | { | |
0b246afa JM |
5113 | struct inode *inode = file_inode(file); |
5114 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
a1b83ac5 | 5115 | size_t len; |
867ab667 | 5116 | int ret; |
a1b83ac5 AJ |
5117 | char label[BTRFS_LABEL_SIZE]; |
5118 | ||
0b246afa JM |
5119 | spin_lock(&fs_info->super_lock); |
5120 | memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); | |
5121 | spin_unlock(&fs_info->super_lock); | |
a1b83ac5 AJ |
5122 | |
5123 | len = strnlen(label, BTRFS_LABEL_SIZE); | |
867ab667 | 5124 | |
5125 | if (len == BTRFS_LABEL_SIZE) { | |
0b246afa JM |
5126 | btrfs_warn(fs_info, |
5127 | "label is too long, return the first %zu bytes", | |
5128 | --len); | |
867ab667 | 5129 | } |
5130 | ||
867ab667 | 5131 | ret = copy_to_user(arg, label, len); |
867ab667 | 5132 | |
5133 | return ret ? -EFAULT : 0; | |
5134 | } | |
5135 | ||
a8bfd4ab | 5136 | static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) |
5137 | { | |
0b246afa JM |
5138 | struct inode *inode = file_inode(file); |
5139 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5140 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5141 | struct btrfs_super_block *super_block = fs_info->super_copy; | |
a8bfd4ab | 5142 | struct btrfs_trans_handle *trans; |
5143 | char label[BTRFS_LABEL_SIZE]; | |
5144 | int ret; | |
5145 | ||
5146 | if (!capable(CAP_SYS_ADMIN)) | |
5147 | return -EPERM; | |
5148 | ||
5149 | if (copy_from_user(label, arg, sizeof(label))) | |
5150 | return -EFAULT; | |
5151 | ||
5152 | if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { | |
0b246afa | 5153 | btrfs_err(fs_info, |
5d163e0e JM |
5154 | "unable to set label with more than %d bytes", |
5155 | BTRFS_LABEL_SIZE - 1); | |
a8bfd4ab | 5156 | return -EINVAL; |
5157 | } | |
5158 | ||
5159 | ret = mnt_want_write_file(file); | |
5160 | if (ret) | |
5161 | return ret; | |
5162 | ||
a8bfd4ab | 5163 | trans = btrfs_start_transaction(root, 0); |
5164 | if (IS_ERR(trans)) { | |
5165 | ret = PTR_ERR(trans); | |
5166 | goto out_unlock; | |
5167 | } | |
5168 | ||
0b246afa | 5169 | spin_lock(&fs_info->super_lock); |
a8bfd4ab | 5170 | strcpy(super_block->label, label); |
0b246afa | 5171 | spin_unlock(&fs_info->super_lock); |
3a45bb20 | 5172 | ret = btrfs_commit_transaction(trans); |
a8bfd4ab | 5173 | |
5174 | out_unlock: | |
a8bfd4ab | 5175 | mnt_drop_write_file(file); |
5176 | return ret; | |
5177 | } | |
5178 | ||
2eaa055f JM |
5179 | #define INIT_FEATURE_FLAGS(suffix) \ |
5180 | { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ | |
5181 | .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ | |
5182 | .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } | |
5183 | ||
d5131b65 | 5184 | int btrfs_ioctl_get_supported_features(void __user *arg) |
2eaa055f | 5185 | { |
4d4ab6d6 | 5186 | static const struct btrfs_ioctl_feature_flags features[3] = { |
2eaa055f JM |
5187 | INIT_FEATURE_FLAGS(SUPP), |
5188 | INIT_FEATURE_FLAGS(SAFE_SET), | |
5189 | INIT_FEATURE_FLAGS(SAFE_CLEAR) | |
5190 | }; | |
5191 | ||
5192 | if (copy_to_user(arg, &features, sizeof(features))) | |
5193 | return -EFAULT; | |
5194 | ||
5195 | return 0; | |
5196 | } | |
5197 | ||
5198 | static int btrfs_ioctl_get_features(struct file *file, void __user *arg) | |
5199 | { | |
0b246afa JM |
5200 | struct inode *inode = file_inode(file); |
5201 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5202 | struct btrfs_super_block *super_block = fs_info->super_copy; | |
2eaa055f JM |
5203 | struct btrfs_ioctl_feature_flags features; |
5204 | ||
5205 | features.compat_flags = btrfs_super_compat_flags(super_block); | |
5206 | features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); | |
5207 | features.incompat_flags = btrfs_super_incompat_flags(super_block); | |
5208 | ||
5209 | if (copy_to_user(arg, &features, sizeof(features))) | |
5210 | return -EFAULT; | |
5211 | ||
5212 | return 0; | |
5213 | } | |
5214 | ||
2ff7e61e | 5215 | static int check_feature_bits(struct btrfs_fs_info *fs_info, |
3b02a68a | 5216 | enum btrfs_feature_set set, |
2eaa055f JM |
5217 | u64 change_mask, u64 flags, u64 supported_flags, |
5218 | u64 safe_set, u64 safe_clear) | |
5219 | { | |
3b02a68a JM |
5220 | const char *type = btrfs_feature_set_names[set]; |
5221 | char *names; | |
2eaa055f JM |
5222 | u64 disallowed, unsupported; |
5223 | u64 set_mask = flags & change_mask; | |
5224 | u64 clear_mask = ~flags & change_mask; | |
5225 | ||
5226 | unsupported = set_mask & ~supported_flags; | |
5227 | if (unsupported) { | |
3b02a68a JM |
5228 | names = btrfs_printable_features(set, unsupported); |
5229 | if (names) { | |
0b246afa JM |
5230 | btrfs_warn(fs_info, |
5231 | "this kernel does not support the %s feature bit%s", | |
5232 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
5233 | kfree(names); |
5234 | } else | |
0b246afa JM |
5235 | btrfs_warn(fs_info, |
5236 | "this kernel does not support %s bits 0x%llx", | |
5237 | type, unsupported); | |
2eaa055f JM |
5238 | return -EOPNOTSUPP; |
5239 | } | |
5240 | ||
5241 | disallowed = set_mask & ~safe_set; | |
5242 | if (disallowed) { | |
3b02a68a JM |
5243 | names = btrfs_printable_features(set, disallowed); |
5244 | if (names) { | |
0b246afa JM |
5245 | btrfs_warn(fs_info, |
5246 | "can't set the %s feature bit%s while mounted", | |
5247 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
5248 | kfree(names); |
5249 | } else | |
0b246afa JM |
5250 | btrfs_warn(fs_info, |
5251 | "can't set %s bits 0x%llx while mounted", | |
5252 | type, disallowed); | |
2eaa055f JM |
5253 | return -EPERM; |
5254 | } | |
5255 | ||
5256 | disallowed = clear_mask & ~safe_clear; | |
5257 | if (disallowed) { | |
3b02a68a JM |
5258 | names = btrfs_printable_features(set, disallowed); |
5259 | if (names) { | |
0b246afa JM |
5260 | btrfs_warn(fs_info, |
5261 | "can't clear the %s feature bit%s while mounted", | |
5262 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
5263 | kfree(names); |
5264 | } else | |
0b246afa JM |
5265 | btrfs_warn(fs_info, |
5266 | "can't clear %s bits 0x%llx while mounted", | |
5267 | type, disallowed); | |
2eaa055f JM |
5268 | return -EPERM; |
5269 | } | |
5270 | ||
5271 | return 0; | |
5272 | } | |
5273 | ||
2ff7e61e JM |
5274 | #define check_feature(fs_info, change_mask, flags, mask_base) \ |
5275 | check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ | |
2eaa055f JM |
5276 | BTRFS_FEATURE_ ## mask_base ## _SUPP, \ |
5277 | BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ | |
5278 | BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) | |
5279 | ||
5280 | static int btrfs_ioctl_set_features(struct file *file, void __user *arg) | |
5281 | { | |
0b246afa JM |
5282 | struct inode *inode = file_inode(file); |
5283 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5284 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5285 | struct btrfs_super_block *super_block = fs_info->super_copy; | |
2eaa055f JM |
5286 | struct btrfs_ioctl_feature_flags flags[2]; |
5287 | struct btrfs_trans_handle *trans; | |
5288 | u64 newflags; | |
5289 | int ret; | |
5290 | ||
5291 | if (!capable(CAP_SYS_ADMIN)) | |
5292 | return -EPERM; | |
5293 | ||
5294 | if (copy_from_user(flags, arg, sizeof(flags))) | |
5295 | return -EFAULT; | |
5296 | ||
5297 | /* Nothing to do */ | |
5298 | if (!flags[0].compat_flags && !flags[0].compat_ro_flags && | |
5299 | !flags[0].incompat_flags) | |
5300 | return 0; | |
5301 | ||
2ff7e61e | 5302 | ret = check_feature(fs_info, flags[0].compat_flags, |
2eaa055f JM |
5303 | flags[1].compat_flags, COMPAT); |
5304 | if (ret) | |
5305 | return ret; | |
5306 | ||
2ff7e61e | 5307 | ret = check_feature(fs_info, flags[0].compat_ro_flags, |
2eaa055f JM |
5308 | flags[1].compat_ro_flags, COMPAT_RO); |
5309 | if (ret) | |
5310 | return ret; | |
5311 | ||
2ff7e61e | 5312 | ret = check_feature(fs_info, flags[0].incompat_flags, |
2eaa055f JM |
5313 | flags[1].incompat_flags, INCOMPAT); |
5314 | if (ret) | |
5315 | return ret; | |
5316 | ||
7ab19625 DS |
5317 | ret = mnt_want_write_file(file); |
5318 | if (ret) | |
5319 | return ret; | |
5320 | ||
8051aa1a | 5321 | trans = btrfs_start_transaction(root, 0); |
7ab19625 DS |
5322 | if (IS_ERR(trans)) { |
5323 | ret = PTR_ERR(trans); | |
5324 | goto out_drop_write; | |
5325 | } | |
2eaa055f | 5326 | |
0b246afa | 5327 | spin_lock(&fs_info->super_lock); |
2eaa055f JM |
5328 | newflags = btrfs_super_compat_flags(super_block); |
5329 | newflags |= flags[0].compat_flags & flags[1].compat_flags; | |
5330 | newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); | |
5331 | btrfs_set_super_compat_flags(super_block, newflags); | |
5332 | ||
5333 | newflags = btrfs_super_compat_ro_flags(super_block); | |
5334 | newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; | |
5335 | newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); | |
5336 | btrfs_set_super_compat_ro_flags(super_block, newflags); | |
5337 | ||
5338 | newflags = btrfs_super_incompat_flags(super_block); | |
5339 | newflags |= flags[0].incompat_flags & flags[1].incompat_flags; | |
5340 | newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); | |
5341 | btrfs_set_super_incompat_flags(super_block, newflags); | |
0b246afa | 5342 | spin_unlock(&fs_info->super_lock); |
2eaa055f | 5343 | |
3a45bb20 | 5344 | ret = btrfs_commit_transaction(trans); |
7ab19625 DS |
5345 | out_drop_write: |
5346 | mnt_drop_write_file(file); | |
5347 | ||
5348 | return ret; | |
2eaa055f JM |
5349 | } |
5350 | ||
2351f431 JB |
5351 | static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) |
5352 | { | |
5353 | struct btrfs_ioctl_send_args *arg; | |
5354 | int ret; | |
5355 | ||
5356 | if (compat) { | |
5357 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) | |
5358 | struct btrfs_ioctl_send_args_32 args32; | |
5359 | ||
5360 | ret = copy_from_user(&args32, argp, sizeof(args32)); | |
5361 | if (ret) | |
5362 | return -EFAULT; | |
5363 | arg = kzalloc(sizeof(*arg), GFP_KERNEL); | |
5364 | if (!arg) | |
5365 | return -ENOMEM; | |
5366 | arg->send_fd = args32.send_fd; | |
5367 | arg->clone_sources_count = args32.clone_sources_count; | |
5368 | arg->clone_sources = compat_ptr(args32.clone_sources); | |
5369 | arg->parent_root = args32.parent_root; | |
5370 | arg->flags = args32.flags; | |
5371 | memcpy(arg->reserved, args32.reserved, | |
5372 | sizeof(args32.reserved)); | |
5373 | #else | |
5374 | return -ENOTTY; | |
5375 | #endif | |
5376 | } else { | |
5377 | arg = memdup_user(argp, sizeof(*arg)); | |
5378 | if (IS_ERR(arg)) | |
5379 | return PTR_ERR(arg); | |
5380 | } | |
5381 | ret = btrfs_ioctl_send(file, arg); | |
5382 | kfree(arg); | |
5383 | return ret; | |
5384 | } | |
5385 | ||
f46b5a66 CH |
5386 | long btrfs_ioctl(struct file *file, unsigned int |
5387 | cmd, unsigned long arg) | |
5388 | { | |
0b246afa JM |
5389 | struct inode *inode = file_inode(file); |
5390 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5391 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4bcabaa3 | 5392 | void __user *argp = (void __user *)arg; |
f46b5a66 CH |
5393 | |
5394 | switch (cmd) { | |
6cbff00f CH |
5395 | case FS_IOC_GETFLAGS: |
5396 | return btrfs_ioctl_getflags(file, argp); | |
5397 | case FS_IOC_SETFLAGS: | |
5398 | return btrfs_ioctl_setflags(file, argp); | |
5399 | case FS_IOC_GETVERSION: | |
5400 | return btrfs_ioctl_getversion(file, argp); | |
f7039b1d LD |
5401 | case FITRIM: |
5402 | return btrfs_ioctl_fitrim(file, argp); | |
f46b5a66 | 5403 | case BTRFS_IOC_SNAP_CREATE: |
fa0d2b9b | 5404 | return btrfs_ioctl_snap_create(file, argp, 0); |
fdfb1e4f | 5405 | case BTRFS_IOC_SNAP_CREATE_V2: |
fa0d2b9b | 5406 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
3de4586c | 5407 | case BTRFS_IOC_SUBVOL_CREATE: |
fa0d2b9b | 5408 | return btrfs_ioctl_snap_create(file, argp, 1); |
6f72c7e2 AJ |
5409 | case BTRFS_IOC_SUBVOL_CREATE_V2: |
5410 | return btrfs_ioctl_snap_create_v2(file, argp, 1); | |
76dda93c YZ |
5411 | case BTRFS_IOC_SNAP_DESTROY: |
5412 | return btrfs_ioctl_snap_destroy(file, argp); | |
0caa102d LZ |
5413 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
5414 | return btrfs_ioctl_subvol_getflags(file, argp); | |
5415 | case BTRFS_IOC_SUBVOL_SETFLAGS: | |
5416 | return btrfs_ioctl_subvol_setflags(file, argp); | |
6ef5ed0d JB |
5417 | case BTRFS_IOC_DEFAULT_SUBVOL: |
5418 | return btrfs_ioctl_default_subvol(file, argp); | |
f46b5a66 | 5419 | case BTRFS_IOC_DEFRAG: |
1e701a32 CM |
5420 | return btrfs_ioctl_defrag(file, NULL); |
5421 | case BTRFS_IOC_DEFRAG_RANGE: | |
5422 | return btrfs_ioctl_defrag(file, argp); | |
f46b5a66 | 5423 | case BTRFS_IOC_RESIZE: |
198605a8 | 5424 | return btrfs_ioctl_resize(file, argp); |
f46b5a66 | 5425 | case BTRFS_IOC_ADD_DEV: |
2ff7e61e | 5426 | return btrfs_ioctl_add_dev(fs_info, argp); |
f46b5a66 | 5427 | case BTRFS_IOC_RM_DEV: |
da24927b | 5428 | return btrfs_ioctl_rm_dev(file, argp); |
6b526ed7 AJ |
5429 | case BTRFS_IOC_RM_DEV_V2: |
5430 | return btrfs_ioctl_rm_dev_v2(file, argp); | |
475f6387 | 5431 | case BTRFS_IOC_FS_INFO: |
2ff7e61e | 5432 | return btrfs_ioctl_fs_info(fs_info, argp); |
475f6387 | 5433 | case BTRFS_IOC_DEV_INFO: |
2ff7e61e | 5434 | return btrfs_ioctl_dev_info(fs_info, argp); |
f46b5a66 | 5435 | case BTRFS_IOC_BALANCE: |
9ba1f6e4 | 5436 | return btrfs_ioctl_balance(file, NULL); |
ac8e9819 CM |
5437 | case BTRFS_IOC_TREE_SEARCH: |
5438 | return btrfs_ioctl_tree_search(file, argp); | |
cc68a8a5 GH |
5439 | case BTRFS_IOC_TREE_SEARCH_V2: |
5440 | return btrfs_ioctl_tree_search_v2(file, argp); | |
ac8e9819 CM |
5441 | case BTRFS_IOC_INO_LOOKUP: |
5442 | return btrfs_ioctl_ino_lookup(file, argp); | |
d7728c96 JS |
5443 | case BTRFS_IOC_INO_PATHS: |
5444 | return btrfs_ioctl_ino_to_path(root, argp); | |
5445 | case BTRFS_IOC_LOGICAL_INO: | |
d24a67b2 ZB |
5446 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); |
5447 | case BTRFS_IOC_LOGICAL_INO_V2: | |
5448 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); | |
1406e432 | 5449 | case BTRFS_IOC_SPACE_INFO: |
2ff7e61e | 5450 | return btrfs_ioctl_space_info(fs_info, argp); |
9b199859 FDBM |
5451 | case BTRFS_IOC_SYNC: { |
5452 | int ret; | |
5453 | ||
82b3e53b | 5454 | ret = btrfs_start_delalloc_roots(fs_info, -1); |
9b199859 FDBM |
5455 | if (ret) |
5456 | return ret; | |
0b246afa | 5457 | ret = btrfs_sync_fs(inode->i_sb, 1); |
2fad4e83 DS |
5458 | /* |
5459 | * The transaction thread may want to do more work, | |
01327610 | 5460 | * namely it pokes the cleaner kthread that will start |
2fad4e83 DS |
5461 | * processing uncleaned subvols. |
5462 | */ | |
0b246afa | 5463 | wake_up_process(fs_info->transaction_kthread); |
9b199859 FDBM |
5464 | return ret; |
5465 | } | |
46204592 | 5466 | case BTRFS_IOC_START_SYNC: |
9a8c28be | 5467 | return btrfs_ioctl_start_sync(root, argp); |
46204592 | 5468 | case BTRFS_IOC_WAIT_SYNC: |
2ff7e61e | 5469 | return btrfs_ioctl_wait_sync(fs_info, argp); |
475f6387 | 5470 | case BTRFS_IOC_SCRUB: |
b8e95489 | 5471 | return btrfs_ioctl_scrub(file, argp); |
475f6387 | 5472 | case BTRFS_IOC_SCRUB_CANCEL: |
2ff7e61e | 5473 | return btrfs_ioctl_scrub_cancel(fs_info); |
475f6387 | 5474 | case BTRFS_IOC_SCRUB_PROGRESS: |
2ff7e61e | 5475 | return btrfs_ioctl_scrub_progress(fs_info, argp); |
c9e9f97b | 5476 | case BTRFS_IOC_BALANCE_V2: |
9ba1f6e4 | 5477 | return btrfs_ioctl_balance(file, argp); |
837d5b6e | 5478 | case BTRFS_IOC_BALANCE_CTL: |
2ff7e61e | 5479 | return btrfs_ioctl_balance_ctl(fs_info, arg); |
19a39dce | 5480 | case BTRFS_IOC_BALANCE_PROGRESS: |
2ff7e61e | 5481 | return btrfs_ioctl_balance_progress(fs_info, argp); |
8ea05e3a AB |
5482 | case BTRFS_IOC_SET_RECEIVED_SUBVOL: |
5483 | return btrfs_ioctl_set_received_subvol(file, argp); | |
abccd00f HM |
5484 | #ifdef CONFIG_64BIT |
5485 | case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: | |
5486 | return btrfs_ioctl_set_received_subvol_32(file, argp); | |
5487 | #endif | |
31db9f7c | 5488 | case BTRFS_IOC_SEND: |
2351f431 JB |
5489 | return _btrfs_ioctl_send(file, argp, false); |
5490 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) | |
5491 | case BTRFS_IOC_SEND_32: | |
5492 | return _btrfs_ioctl_send(file, argp, true); | |
5493 | #endif | |
c11d2c23 | 5494 | case BTRFS_IOC_GET_DEV_STATS: |
2ff7e61e | 5495 | return btrfs_ioctl_get_dev_stats(fs_info, argp); |
5d13a37b | 5496 | case BTRFS_IOC_QUOTA_CTL: |
905b0dda | 5497 | return btrfs_ioctl_quota_ctl(file, argp); |
5d13a37b | 5498 | case BTRFS_IOC_QGROUP_ASSIGN: |
905b0dda | 5499 | return btrfs_ioctl_qgroup_assign(file, argp); |
5d13a37b | 5500 | case BTRFS_IOC_QGROUP_CREATE: |
905b0dda | 5501 | return btrfs_ioctl_qgroup_create(file, argp); |
5d13a37b | 5502 | case BTRFS_IOC_QGROUP_LIMIT: |
905b0dda | 5503 | return btrfs_ioctl_qgroup_limit(file, argp); |
2f232036 JS |
5504 | case BTRFS_IOC_QUOTA_RESCAN: |
5505 | return btrfs_ioctl_quota_rescan(file, argp); | |
5506 | case BTRFS_IOC_QUOTA_RESCAN_STATUS: | |
5507 | return btrfs_ioctl_quota_rescan_status(file, argp); | |
57254b6e JS |
5508 | case BTRFS_IOC_QUOTA_RESCAN_WAIT: |
5509 | return btrfs_ioctl_quota_rescan_wait(file, argp); | |
3f6bcfbd | 5510 | case BTRFS_IOC_DEV_REPLACE: |
2ff7e61e | 5511 | return btrfs_ioctl_dev_replace(fs_info, argp); |
867ab667 | 5512 | case BTRFS_IOC_GET_FSLABEL: |
5513 | return btrfs_ioctl_get_fslabel(file, argp); | |
a8bfd4ab | 5514 | case BTRFS_IOC_SET_FSLABEL: |
5515 | return btrfs_ioctl_set_fslabel(file, argp); | |
2eaa055f | 5516 | case BTRFS_IOC_GET_SUPPORTED_FEATURES: |
d5131b65 | 5517 | return btrfs_ioctl_get_supported_features(argp); |
2eaa055f JM |
5518 | case BTRFS_IOC_GET_FEATURES: |
5519 | return btrfs_ioctl_get_features(file, argp); | |
5520 | case BTRFS_IOC_SET_FEATURES: | |
5521 | return btrfs_ioctl_set_features(file, argp); | |
e4202ac9 DS |
5522 | case FS_IOC_FSGETXATTR: |
5523 | return btrfs_ioctl_fsgetxattr(file, argp); | |
025f2121 DS |
5524 | case FS_IOC_FSSETXATTR: |
5525 | return btrfs_ioctl_fssetxattr(file, argp); | |
f46b5a66 CH |
5526 | } |
5527 | ||
5528 | return -ENOTTY; | |
5529 | } | |
4c63c245 LD |
5530 | |
5531 | #ifdef CONFIG_COMPAT | |
5532 | long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
5533 | { | |
2a362249 JM |
5534 | /* |
5535 | * These all access 32-bit values anyway so no further | |
5536 | * handling is necessary. | |
5537 | */ | |
4c63c245 LD |
5538 | switch (cmd) { |
5539 | case FS_IOC32_GETFLAGS: | |
5540 | cmd = FS_IOC_GETFLAGS; | |
5541 | break; | |
5542 | case FS_IOC32_SETFLAGS: | |
5543 | cmd = FS_IOC_SETFLAGS; | |
5544 | break; | |
5545 | case FS_IOC32_GETVERSION: | |
5546 | cmd = FS_IOC_GETVERSION; | |
5547 | break; | |
4c63c245 LD |
5548 | } |
5549 | ||
5550 | return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); | |
5551 | } | |
5552 | #endif |