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