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