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