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