David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/bio.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8 | #include <linux/file.h> |
| 9 | #include <linux/fs.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 10 | #include <linux/fsnotify.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/highmem.h> |
| 13 | #include <linux/time.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 14 | #include <linux/string.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 15 | #include <linux/backing-dev.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 16 | #include <linux/mount.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 17 | #include <linux/namei.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 18 | #include <linux/writeback.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 19 | #include <linux/compat.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 20 | #include <linux/security.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 21 | #include <linux/xattr.h> |
David Sterba | f54de06 | 2017-05-31 19:32:09 +0200 | [diff] [blame] | 22 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 24 | #include <linux/blkdev.h> |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 25 | #include <linux/uuid.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 26 | #include <linux/btrfs.h> |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Jeff Layton | ae5e165 | 2018-01-29 06:41:30 -0500 | [diff] [blame] | 28 | #include <linux/iversion.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 29 | #include "ctree.h" |
| 30 | #include "disk-io.h" |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 31 | #include "export.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 32 | #include "transaction.h" |
| 33 | #include "btrfs_inode.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 34 | #include "print-tree.h" |
| 35 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 36 | #include "locking.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 37 | #include "inode-map.h" |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 38 | #include "backref.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 39 | #include "rcu-string.h" |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 40 | #include "send.h" |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 41 | #include "dev-replace.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 42 | #include "props.h" |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 43 | #include "sysfs.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 44 | #include "qgroup.h" |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 45 | #include "tree-log.h" |
Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 46 | #include "compression.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 47 | #include "space-info.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 48 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 49 | #include "block-group.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 50 | |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 51 | #ifdef CONFIG_64BIT |
| 52 | /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI |
| 53 | * structures are incorrect, as the timespec structure from userspace |
| 54 | * is 4 bytes too small. We define these alternatives here to teach |
| 55 | * the kernel about the 32-bit struct packing. |
| 56 | */ |
| 57 | struct btrfs_ioctl_timespec_32 { |
| 58 | __u64 sec; |
| 59 | __u32 nsec; |
| 60 | } __attribute__ ((__packed__)); |
| 61 | |
| 62 | struct btrfs_ioctl_received_subvol_args_32 { |
| 63 | char uuid[BTRFS_UUID_SIZE]; /* in */ |
| 64 | __u64 stransid; /* in */ |
| 65 | __u64 rtransid; /* out */ |
| 66 | struct btrfs_ioctl_timespec_32 stime; /* in */ |
| 67 | struct btrfs_ioctl_timespec_32 rtime; /* out */ |
| 68 | __u64 flags; /* in */ |
| 69 | __u64 reserved[16]; /* in */ |
| 70 | } __attribute__ ((__packed__)); |
| 71 | |
| 72 | #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ |
| 73 | struct btrfs_ioctl_received_subvol_args_32) |
| 74 | #endif |
| 75 | |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 76 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) |
| 77 | struct btrfs_ioctl_send_args_32 { |
| 78 | __s64 send_fd; /* in */ |
| 79 | __u64 clone_sources_count; /* in */ |
| 80 | compat_uptr_t clone_sources; /* in */ |
| 81 | __u64 parent_root; /* in */ |
| 82 | __u64 flags; /* in */ |
| 83 | __u64 reserved[4]; /* in */ |
| 84 | } __attribute__ ((__packed__)); |
| 85 | |
| 86 | #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \ |
| 87 | struct btrfs_ioctl_send_args_32) |
| 88 | #endif |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 89 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 90 | /* Mask out flags that are inappropriate for the given type of inode. */ |
David Sterba | 1905a0f | 2018-03-26 18:52:15 +0200 | [diff] [blame] | 91 | static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode, |
| 92 | unsigned int flags) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 93 | { |
David Sterba | 1905a0f | 2018-03-26 18:52:15 +0200 | [diff] [blame] | 94 | if (S_ISDIR(inode->i_mode)) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 95 | return flags; |
David Sterba | 1905a0f | 2018-03-26 18:52:15 +0200 | [diff] [blame] | 96 | else if (S_ISREG(inode->i_mode)) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 97 | return flags & ~FS_DIRSYNC_FL; |
| 98 | else |
| 99 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); |
| 100 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 101 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 102 | /* |
David Sterba | a157d4f | 2018-03-26 19:12:25 +0200 | [diff] [blame] | 103 | * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS |
| 104 | * ioctl. |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 105 | */ |
David Sterba | a157d4f | 2018-03-26 19:12:25 +0200 | [diff] [blame] | 106 | static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 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; |
Li Zefan | d0092bd | 2011-04-15 03:03:06 +0000 | [diff] [blame] | 122 | if (flags & BTRFS_INODE_NODATACOW) |
| 123 | iflags |= FS_NOCOW_FL; |
| 124 | |
Satoru Takeuchi | 13f48dc | 2016-03-15 09:09:59 +0900 | [diff] [blame] | 125 | if (flags & BTRFS_INODE_NOCOMPRESS) |
Li Zefan | d0092bd | 2011-04-15 03:03:06 +0000 | [diff] [blame] | 126 | iflags |= FS_NOCOMP_FL; |
Satoru Takeuchi | 13f48dc | 2016-03-15 09:09:59 +0900 | [diff] [blame] | 127 | else if (flags & BTRFS_INODE_COMPRESS) |
| 128 | iflags |= FS_COMPR_FL; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 129 | |
| 130 | return iflags; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Update inode->i_flags based on the btrfs internal flags. |
| 135 | */ |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 136 | void btrfs_sync_inode_flags_to_i_flags(struct inode *inode) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 137 | { |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 138 | struct btrfs_inode *binode = BTRFS_I(inode); |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 139 | unsigned int new_fl = 0; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 140 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 141 | if (binode->flags & BTRFS_INODE_SYNC) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 142 | new_fl |= S_SYNC; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 143 | if (binode->flags & BTRFS_INODE_IMMUTABLE) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 144 | new_fl |= S_IMMUTABLE; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 145 | if (binode->flags & BTRFS_INODE_APPEND) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 146 | new_fl |= S_APPEND; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 147 | if (binode->flags & BTRFS_INODE_NOATIME) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 148 | new_fl |= S_NOATIME; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 149 | if (binode->flags & BTRFS_INODE_DIRSYNC) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 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); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 157 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) |
| 158 | { |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 159 | struct btrfs_inode *binode = BTRFS_I(file_inode(file)); |
| 160 | unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 161 | |
| 162 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 163 | return -EFAULT; |
| 164 | return 0; |
| 165 | } |
| 166 | |
David Sterba | 5ba76ab | 2018-03-26 18:52:15 +0200 | [diff] [blame] | 167 | /* Check if @flags are a supported and valid set of FS_*_FL flags */ |
| 168 | static int check_fsflags(unsigned int flags) |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 169 | { |
| 170 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
| 171 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
| 172 | FS_SYNC_FL | FS_DIRSYNC_FL | \ |
Li Zefan | e1e8fb6 | 2011-04-15 03:02:49 +0000 | [diff] [blame] | 173 | FS_NOCOMP_FL | FS_COMPR_FL | |
| 174 | FS_NOCOW_FL)) |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 175 | return -EOPNOTSUPP; |
| 176 | |
| 177 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) |
| 178 | return -EINVAL; |
| 179 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 183 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) |
| 184 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 185 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 186 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 187 | struct btrfs_inode *binode = BTRFS_I(inode); |
| 188 | struct btrfs_root *root = binode->root; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 189 | struct btrfs_trans_handle *trans; |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 190 | unsigned int fsflags, old_fsflags; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 191 | int ret; |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 192 | const char *comp = NULL; |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 193 | u32 binode_flags = binode->flags; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 194 | |
David Sterba | bd60ea0 | 2014-01-16 15:50:22 +0100 | [diff] [blame] | 195 | if (!inode_owner_or_capable(inode)) |
| 196 | return -EPERM; |
| 197 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 198 | if (btrfs_root_readonly(root)) |
| 199 | return -EROFS; |
| 200 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 201 | if (copy_from_user(&fsflags, arg, sizeof(fsflags))) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 202 | return -EFAULT; |
| 203 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 204 | ret = check_fsflags(fsflags); |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 205 | if (ret) |
| 206 | return ret; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 207 | |
Jan Kara | e784868 | 2012-06-12 16:20:32 +0200 | [diff] [blame] | 208 | ret = mnt_want_write_file(file); |
| 209 | if (ret) |
| 210 | return ret; |
| 211 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 212 | inode_lock(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 213 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 214 | fsflags = btrfs_mask_fsflags_for_type(inode, fsflags); |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 215 | old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags); |
| 216 | ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags); |
| 217 | if (ret) |
| 218 | goto out_unlock; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 219 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 220 | if (fsflags & FS_SYNC_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 221 | binode_flags |= BTRFS_INODE_SYNC; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 222 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 223 | binode_flags &= ~BTRFS_INODE_SYNC; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 224 | if (fsflags & FS_IMMUTABLE_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 225 | binode_flags |= BTRFS_INODE_IMMUTABLE; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 226 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 227 | binode_flags &= ~BTRFS_INODE_IMMUTABLE; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 228 | if (fsflags & FS_APPEND_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 229 | binode_flags |= BTRFS_INODE_APPEND; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 230 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 231 | binode_flags &= ~BTRFS_INODE_APPEND; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 232 | if (fsflags & FS_NODUMP_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 233 | binode_flags |= BTRFS_INODE_NODUMP; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 234 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 235 | binode_flags &= ~BTRFS_INODE_NODUMP; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 236 | if (fsflags & FS_NOATIME_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 237 | binode_flags |= BTRFS_INODE_NOATIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 238 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 239 | binode_flags &= ~BTRFS_INODE_NOATIME; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 240 | if (fsflags & FS_DIRSYNC_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 241 | binode_flags |= BTRFS_INODE_DIRSYNC; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 242 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 243 | binode_flags &= ~BTRFS_INODE_DIRSYNC; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 244 | if (fsflags & FS_NOCOW_FL) { |
Anand Jain | 44e5194 | 2019-04-20 19:48:57 +0800 | [diff] [blame] | 245 | if (S_ISREG(inode->i_mode)) { |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 246 | /* |
| 247 | * It's safe to turn csums off here, no extents exist. |
| 248 | * Otherwise we want the flag to reflect the real COW |
| 249 | * status of the file and will not set it. |
| 250 | */ |
| 251 | if (inode->i_size == 0) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 252 | binode_flags |= BTRFS_INODE_NODATACOW | |
| 253 | BTRFS_INODE_NODATASUM; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 254 | } else { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 255 | binode_flags |= BTRFS_INODE_NODATACOW; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 256 | } |
| 257 | } else { |
| 258 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 259 | * Revert back under same assumptions as above |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 260 | */ |
Anand Jain | 44e5194 | 2019-04-20 19:48:57 +0800 | [diff] [blame] | 261 | if (S_ISREG(inode->i_mode)) { |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 262 | if (inode->i_size == 0) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 263 | binode_flags &= ~(BTRFS_INODE_NODATACOW | |
| 264 | BTRFS_INODE_NODATASUM); |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 265 | } else { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 266 | binode_flags &= ~BTRFS_INODE_NODATACOW; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 267 | } |
| 268 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 269 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 270 | /* |
| 271 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS |
| 272 | * flag may be changed automatically if compression code won't make |
| 273 | * things smaller. |
| 274 | */ |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 275 | if (fsflags & FS_NOCOMP_FL) { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 276 | binode_flags &= ~BTRFS_INODE_COMPRESS; |
| 277 | binode_flags |= BTRFS_INODE_NOCOMPRESS; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 278 | } else if (fsflags & FS_COMPR_FL) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 279 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 280 | if (IS_SWAPFILE(inode)) { |
| 281 | ret = -ETXTBSY; |
| 282 | goto out_unlock; |
| 283 | } |
| 284 | |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 285 | binode_flags |= BTRFS_INODE_COMPRESS; |
| 286 | binode_flags &= ~BTRFS_INODE_NOCOMPRESS; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 287 | |
David Sterba | 9337050 | 2017-10-31 17:32:41 +0100 | [diff] [blame] | 288 | comp = btrfs_compress_type2str(fs_info->compress_type); |
| 289 | if (!comp || comp[0] == 0) |
| 290 | comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); |
Li Zefan | ebcb904 | 2011-04-15 03:03:17 +0000 | [diff] [blame] | 291 | } else { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 292 | binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 293 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 294 | |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 295 | /* |
| 296 | * 1 for inode item |
| 297 | * 2 for properties |
| 298 | */ |
| 299 | trans = btrfs_start_transaction(root, 3); |
Li Zefan | f062abf0 | 2011-12-29 13:36:45 +0800 | [diff] [blame] | 300 | if (IS_ERR(trans)) { |
| 301 | ret = PTR_ERR(trans); |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 302 | goto out_unlock; |
Li Zefan | f062abf0 | 2011-12-29 13:36:45 +0800 | [diff] [blame] | 303 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 304 | |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 305 | if (comp) { |
| 306 | ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp, |
| 307 | strlen(comp), 0); |
| 308 | if (ret) { |
| 309 | btrfs_abort_transaction(trans, ret); |
| 310 | goto out_end_trans; |
| 311 | } |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 312 | } else { |
| 313 | ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL, |
| 314 | 0, 0); |
| 315 | if (ret && ret != -ENODATA) { |
| 316 | btrfs_abort_transaction(trans, ret); |
| 317 | goto out_end_trans; |
| 318 | } |
| 319 | } |
| 320 | |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 321 | binode->flags = binode_flags; |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 322 | btrfs_sync_inode_flags_to_i_flags(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 323 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 324 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 325 | ret = btrfs_update_inode(trans, root, inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 326 | |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 327 | out_end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 328 | btrfs_end_transaction(trans); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 329 | out_unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 330 | inode_unlock(inode); |
Jan Kara | e784868 | 2012-06-12 16:20:32 +0200 | [diff] [blame] | 331 | mnt_drop_write_file(file); |
liubo | 2d4e6f6ad | 2011-02-24 09:38:16 +0000 | [diff] [blame] | 332 | return ret; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 333 | } |
| 334 | |
David Sterba | 19f93b3 | 2018-03-26 19:42:05 +0200 | [diff] [blame] | 335 | /* |
| 336 | * Translate btrfs internal inode flags to xflags as expected by the |
| 337 | * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are |
| 338 | * silently dropped. |
| 339 | */ |
| 340 | static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags) |
| 341 | { |
| 342 | unsigned int xflags = 0; |
| 343 | |
| 344 | if (flags & BTRFS_INODE_APPEND) |
| 345 | xflags |= FS_XFLAG_APPEND; |
| 346 | if (flags & BTRFS_INODE_IMMUTABLE) |
| 347 | xflags |= FS_XFLAG_IMMUTABLE; |
| 348 | if (flags & BTRFS_INODE_NOATIME) |
| 349 | xflags |= FS_XFLAG_NOATIME; |
| 350 | if (flags & BTRFS_INODE_NODUMP) |
| 351 | xflags |= FS_XFLAG_NODUMP; |
| 352 | if (flags & BTRFS_INODE_SYNC) |
| 353 | xflags |= FS_XFLAG_SYNC; |
| 354 | |
| 355 | return xflags; |
| 356 | } |
| 357 | |
| 358 | /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */ |
| 359 | static int check_xflags(unsigned int flags) |
| 360 | { |
| 361 | if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME | |
| 362 | FS_XFLAG_NODUMP | FS_XFLAG_SYNC)) |
| 363 | return -EOPNOTSUPP; |
| 364 | return 0; |
| 365 | } |
| 366 | |
David Sterba | e4202ac | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 367 | /* |
| 368 | * Set the xflags from the internal inode flags. The remaining items of fsxattr |
| 369 | * are zeroed. |
| 370 | */ |
| 371 | static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg) |
| 372 | { |
| 373 | struct btrfs_inode *binode = BTRFS_I(file_inode(file)); |
| 374 | struct fsxattr fa; |
| 375 | |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 376 | simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags)); |
David Sterba | e4202ac | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 377 | if (copy_to_user(arg, &fa, sizeof(fa))) |
| 378 | return -EFAULT; |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 383 | static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg) |
| 384 | { |
| 385 | struct inode *inode = file_inode(file); |
| 386 | struct btrfs_inode *binode = BTRFS_I(inode); |
| 387 | struct btrfs_root *root = binode->root; |
| 388 | struct btrfs_trans_handle *trans; |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 389 | struct fsxattr fa, old_fa; |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 390 | unsigned old_flags; |
| 391 | unsigned old_i_flags; |
| 392 | int ret = 0; |
| 393 | |
| 394 | if (!inode_owner_or_capable(inode)) |
| 395 | return -EPERM; |
| 396 | |
| 397 | if (btrfs_root_readonly(root)) |
| 398 | return -EROFS; |
| 399 | |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 400 | if (copy_from_user(&fa, arg, sizeof(fa))) |
| 401 | return -EFAULT; |
| 402 | |
| 403 | ret = check_xflags(fa.fsx_xflags); |
| 404 | if (ret) |
| 405 | return ret; |
| 406 | |
| 407 | if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0) |
| 408 | return -EOPNOTSUPP; |
| 409 | |
| 410 | ret = mnt_want_write_file(file); |
| 411 | if (ret) |
| 412 | return ret; |
| 413 | |
| 414 | inode_lock(inode); |
| 415 | |
| 416 | old_flags = binode->flags; |
| 417 | old_i_flags = inode->i_flags; |
| 418 | |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 419 | simple_fill_fsxattr(&old_fa, |
| 420 | btrfs_inode_flags_to_xflags(binode->flags)); |
| 421 | ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa); |
| 422 | if (ret) |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 423 | goto out_unlock; |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 424 | |
| 425 | if (fa.fsx_xflags & FS_XFLAG_SYNC) |
| 426 | binode->flags |= BTRFS_INODE_SYNC; |
| 427 | else |
| 428 | binode->flags &= ~BTRFS_INODE_SYNC; |
| 429 | if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE) |
| 430 | binode->flags |= BTRFS_INODE_IMMUTABLE; |
| 431 | else |
| 432 | binode->flags &= ~BTRFS_INODE_IMMUTABLE; |
| 433 | if (fa.fsx_xflags & FS_XFLAG_APPEND) |
| 434 | binode->flags |= BTRFS_INODE_APPEND; |
| 435 | else |
| 436 | binode->flags &= ~BTRFS_INODE_APPEND; |
| 437 | if (fa.fsx_xflags & FS_XFLAG_NODUMP) |
| 438 | binode->flags |= BTRFS_INODE_NODUMP; |
| 439 | else |
| 440 | binode->flags &= ~BTRFS_INODE_NODUMP; |
| 441 | if (fa.fsx_xflags & FS_XFLAG_NOATIME) |
| 442 | binode->flags |= BTRFS_INODE_NOATIME; |
| 443 | else |
| 444 | binode->flags &= ~BTRFS_INODE_NOATIME; |
| 445 | |
| 446 | /* 1 item for the inode */ |
| 447 | trans = btrfs_start_transaction(root, 1); |
| 448 | if (IS_ERR(trans)) { |
| 449 | ret = PTR_ERR(trans); |
| 450 | goto out_unlock; |
| 451 | } |
| 452 | |
| 453 | btrfs_sync_inode_flags_to_i_flags(inode); |
| 454 | inode_inc_iversion(inode); |
| 455 | inode->i_ctime = current_time(inode); |
| 456 | ret = btrfs_update_inode(trans, root, inode); |
| 457 | |
| 458 | btrfs_end_transaction(trans); |
| 459 | |
| 460 | out_unlock: |
| 461 | if (ret) { |
| 462 | binode->flags = old_flags; |
| 463 | inode->i_flags = old_i_flags; |
| 464 | } |
| 465 | |
| 466 | inode_unlock(inode); |
| 467 | mnt_drop_write_file(file); |
| 468 | |
| 469 | return ret; |
| 470 | } |
| 471 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 472 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) |
| 473 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 474 | struct inode *inode = file_inode(file); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 475 | |
| 476 | return put_user(inode->i_generation, arg); |
| 477 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 478 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 479 | static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info, |
| 480 | void __user *arg) |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 481 | { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 482 | struct btrfs_device *device; |
| 483 | struct request_queue *q; |
| 484 | struct fstrim_range range; |
| 485 | u64 minlen = ULLONG_MAX; |
| 486 | u64 num_devices = 0; |
| 487 | int ret; |
| 488 | |
| 489 | if (!capable(CAP_SYS_ADMIN)) |
| 490 | return -EPERM; |
| 491 | |
Filipe Manana | f35f06c | 2019-03-26 10:49:56 +0000 | [diff] [blame] | 492 | /* |
| 493 | * If the fs is mounted with nologreplay, which requires it to be |
| 494 | * mounted in RO mode as well, we can not allow discard on free space |
| 495 | * inside block groups, because log trees refer to extents that are not |
| 496 | * pinned in a block group's free space cache (pinning the extents is |
| 497 | * precisely the first phase of replaying a log tree). |
| 498 | */ |
| 499 | if (btrfs_test_opt(fs_info, NOLOGREPLAY)) |
| 500 | return -EROFS; |
| 501 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 502 | rcu_read_lock(); |
| 503 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, |
| 504 | dev_list) { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 505 | if (!device->bdev) |
| 506 | continue; |
| 507 | q = bdev_get_queue(device->bdev); |
| 508 | if (blk_queue_discard(q)) { |
| 509 | num_devices++; |
Seraphime Kirkovski | 50d0446 | 2016-12-15 14:38:28 +0100 | [diff] [blame] | 510 | minlen = min_t(u64, q->limits.discard_granularity, |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 511 | minlen); |
| 512 | } |
| 513 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 514 | rcu_read_unlock(); |
Lukas Czerner | f4c697e | 2011-09-05 16:34:54 +0200 | [diff] [blame] | 515 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 516 | if (!num_devices) |
| 517 | return -EOPNOTSUPP; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 518 | if (copy_from_user(&range, arg, sizeof(range))) |
| 519 | return -EFAULT; |
Qu Wenruo | 6ba9fc8 | 2018-09-07 14:16:24 +0800 | [diff] [blame] | 520 | |
| 521 | /* |
| 522 | * NOTE: Don't truncate the range using super->total_bytes. Bytenr of |
| 523 | * block group is in the logical address space, which can be any |
| 524 | * sectorsize aligned bytenr in the range [0, U64_MAX]. |
| 525 | */ |
| 526 | if (range.len < fs_info->sb->s_blocksize) |
Lukas Czerner | f4c697e | 2011-09-05 16:34:54 +0200 | [diff] [blame] | 527 | return -EINVAL; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 528 | |
| 529 | range.minlen = max(range.minlen, minlen); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 530 | ret = btrfs_trim_fs(fs_info, &range); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 531 | if (ret < 0) |
| 532 | return ret; |
| 533 | |
| 534 | if (copy_to_user(arg, &range, sizeof(range))) |
| 535 | return -EFAULT; |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
David Sterba | e1f60a6 | 2019-10-01 19:57:39 +0200 | [diff] [blame] | 540 | int __pure btrfs_is_empty_uuid(u8 *uuid) |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 541 | { |
Chris Mason | 46e0f66 | 2013-11-15 12:14:55 +0100 | [diff] [blame] | 542 | int i; |
| 543 | |
| 544 | for (i = 0; i < BTRFS_UUID_SIZE; i++) { |
| 545 | if (uuid[i]) |
| 546 | return 0; |
| 547 | } |
| 548 | return 1; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 549 | } |
| 550 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 551 | static noinline int create_subvol(struct inode *dir, |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 552 | struct dentry *dentry, |
David Sterba | 52f75f4 | 2017-02-14 18:33:53 +0100 | [diff] [blame] | 553 | const char *name, int namelen, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 554 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 555 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 556 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 557 | struct btrfs_trans_handle *trans; |
| 558 | struct btrfs_key key; |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 559 | struct btrfs_root_item *root_item; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 560 | struct btrfs_inode_item *inode_item; |
| 561 | struct extent_buffer *leaf; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 562 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 563 | struct btrfs_root *new_root; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 564 | struct btrfs_block_rsv block_rsv; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 565 | struct timespec64 cur_time = current_time(dir); |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 566 | struct inode *inode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 567 | int ret; |
| 568 | int err; |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame^] | 569 | dev_t anon_dev = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 570 | u64 objectid; |
| 571 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 572 | u64 index = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 573 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 574 | root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); |
| 575 | if (!root_item) |
| 576 | return -ENOMEM; |
| 577 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 578 | ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid); |
Al Viro | 2fbe8c8 | 2011-07-16 21:38:06 -0400 | [diff] [blame] | 579 | if (ret) |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 580 | goto fail_free; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 581 | |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame^] | 582 | ret = get_anon_bdev(&anon_dev); |
| 583 | if (ret < 0) |
| 584 | goto fail_free; |
| 585 | |
Qu Wenruo | e09fe2d | 2015-02-27 16:24:23 +0800 | [diff] [blame] | 586 | /* |
| 587 | * Don't create subvolume whose level is not zero. Or qgroup will be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 588 | * screwed up since it assumes subvolume qgroup's level to be 0. |
Qu Wenruo | e09fe2d | 2015-02-27 16:24:23 +0800 | [diff] [blame] | 589 | */ |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 590 | if (btrfs_qgroup_level(objectid)) { |
| 591 | ret = -ENOSPC; |
| 592 | goto fail_free; |
| 593 | } |
Qu Wenruo | e09fe2d | 2015-02-27 16:24:23 +0800 | [diff] [blame] | 594 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 595 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 596 | /* |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 597 | * The same as the snapshot creation, please see the comment |
| 598 | * of create_snapshot(). |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 599 | */ |
Gu JinXiang | c4c129d | 2018-05-30 11:00:38 +0800 | [diff] [blame] | 600 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 601 | if (ret) |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 602 | goto fail_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 603 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 604 | trans = btrfs_start_transaction(root, 0); |
| 605 | if (IS_ERR(trans)) { |
| 606 | ret = PTR_ERR(trans); |
David Sterba | 7775c81 | 2017-02-10 19:18:18 +0100 | [diff] [blame] | 607 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 608 | goto fail_free; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 609 | } |
| 610 | trans->block_rsv = &block_rsv; |
| 611 | trans->bytes_reserved = block_rsv.size; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 612 | |
Lu Fengqi | a9377422 | 2018-07-18 14:45:41 +0800 | [diff] [blame] | 613 | ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit); |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 614 | if (ret) |
| 615 | goto fail; |
| 616 | |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 617 | leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); |
Josef Bacik | 8e8a1e3 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 618 | if (IS_ERR(leaf)) { |
| 619 | ret = PTR_ERR(leaf); |
| 620 | goto fail; |
| 621 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 622 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 623 | btrfs_mark_buffer_dirty(leaf); |
| 624 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 625 | inode_item = &root_item->inode; |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 626 | btrfs_set_stack_inode_generation(inode_item, 1); |
| 627 | btrfs_set_stack_inode_size(inode_item, 3); |
| 628 | btrfs_set_stack_inode_nlink(inode_item, 1); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 629 | btrfs_set_stack_inode_nbytes(inode_item, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 630 | fs_info->nodesize); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 631 | btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 632 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 633 | btrfs_set_root_flags(root_item, 0); |
| 634 | btrfs_set_root_limit(root_item, 0); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 635 | btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); |
Li Zefan | 08fe4db | 2011-03-28 02:01:25 +0000 | [diff] [blame] | 636 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 637 | btrfs_set_root_bytenr(root_item, leaf->start); |
| 638 | btrfs_set_root_generation(root_item, trans->transid); |
| 639 | btrfs_set_root_level(root_item, 0); |
| 640 | btrfs_set_root_refs(root_item, 1); |
| 641 | btrfs_set_root_used(root_item, leaf->len); |
| 642 | btrfs_set_root_last_snapshot(root_item, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 643 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 644 | btrfs_set_root_generation_v2(root_item, |
| 645 | btrfs_root_generation(root_item)); |
Andy Shevchenko | 807fc79 | 2020-02-24 17:37:51 +0200 | [diff] [blame] | 646 | generate_random_guid(root_item->uuid); |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 647 | btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); |
| 648 | btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); |
| 649 | root_item->ctime = root_item->otime; |
| 650 | btrfs_set_root_ctransid(root_item, trans->transid); |
| 651 | btrfs_set_root_otransid(root_item, trans->transid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 652 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 653 | btrfs_tree_unlock(leaf); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 654 | free_extent_buffer(leaf); |
| 655 | leaf = NULL; |
| 656 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 657 | btrfs_set_root_dirid(root_item, new_dirid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 658 | |
| 659 | key.objectid = objectid; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 660 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 661 | key.type = BTRFS_ROOT_ITEM_KEY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 662 | ret = btrfs_insert_root(trans, fs_info->tree_root, &key, |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 663 | root_item); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 664 | if (ret) |
| 665 | goto fail; |
| 666 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 667 | key.offset = (u64)-1; |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame^] | 668 | new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 669 | if (IS_ERR(new_root)) { |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame^] | 670 | free_anon_bdev(anon_dev); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 671 | ret = PTR_ERR(new_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 672 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 673 | goto fail; |
| 674 | } |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame^] | 675 | /* Freeing will be done in btrfs_put_root() of new_root */ |
| 676 | anon_dev = 0; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 677 | |
| 678 | btrfs_record_root_in_trans(trans, new_root); |
| 679 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 680 | ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 681 | btrfs_put_root(new_root); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 682 | if (ret) { |
| 683 | /* We potentially lose an unused inode item here */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 684 | btrfs_abort_transaction(trans, ret); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 685 | goto fail; |
| 686 | } |
| 687 | |
Chandan Rajendra | f32e48e | 2016-01-07 18:56:59 +0530 | [diff] [blame] | 688 | mutex_lock(&new_root->objectid_mutex); |
| 689 | new_root->highest_objectid = new_dirid; |
| 690 | mutex_unlock(&new_root->objectid_mutex); |
| 691 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 692 | /* |
| 693 | * insert the directory item |
| 694 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 695 | ret = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 696 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 697 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 698 | goto fail; |
| 699 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 700 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 701 | ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 702 | BTRFS_FT_DIR, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 703 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 704 | btrfs_abort_transaction(trans, ret); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 705 | goto fail; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 706 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 707 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 708 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); |
Yan Zheng | 52c2617 | 2009-01-05 15:43:43 -0500 | [diff] [blame] | 709 | ret = btrfs_update_inode(trans, root, dir); |
Josef Bacik | c7e54b5 | 2019-12-06 09:37:15 -0500 | [diff] [blame] | 710 | if (ret) { |
| 711 | btrfs_abort_transaction(trans, ret); |
| 712 | goto fail; |
| 713 | } |
Yan Zheng | 52c2617 | 2009-01-05 15:43:43 -0500 | [diff] [blame] | 714 | |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 715 | ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 716 | btrfs_ino(BTRFS_I(dir)), index, name, namelen); |
Josef Bacik | c7e54b5 | 2019-12-06 09:37:15 -0500 | [diff] [blame] | 717 | if (ret) { |
| 718 | btrfs_abort_transaction(trans, ret); |
| 719 | goto fail; |
| 720 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 721 | |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 722 | ret = btrfs_uuid_tree_add(trans, root_item->uuid, |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 723 | BTRFS_UUID_KEY_SUBVOL, objectid); |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 724 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 725 | btrfs_abort_transaction(trans, ret); |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 726 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 727 | fail: |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 728 | kfree(root_item); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 729 | trans->block_rsv = NULL; |
| 730 | trans->bytes_reserved = 0; |
David Sterba | 7775c81 | 2017-02-10 19:18:18 +0100 | [diff] [blame] | 731 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); |
Liu Bo | de6e820 | 2014-01-09 14:57:06 +0800 | [diff] [blame] | 732 | |
Nikolay Borisov | 9babda9 | 2020-03-13 17:23:20 +0200 | [diff] [blame] | 733 | err = btrfs_commit_transaction(trans); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 734 | if (err && !ret) |
| 735 | ret = err; |
Chris Mason | 1a65e24 | 2013-02-06 12:06:02 -0500 | [diff] [blame] | 736 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 737 | if (!ret) { |
| 738 | inode = btrfs_lookup_dentry(dir, dentry); |
Liu Bo | de6e820 | 2014-01-09 14:57:06 +0800 | [diff] [blame] | 739 | if (IS_ERR(inode)) |
| 740 | return PTR_ERR(inode); |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 741 | d_instantiate(dentry, inode); |
| 742 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 743 | return ret; |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 744 | |
| 745 | fail_free: |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame^] | 746 | if (anon_dev) |
| 747 | free_anon_bdev(anon_dev); |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 748 | kfree(root_item); |
| 749 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 750 | } |
| 751 | |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 752 | static int create_snapshot(struct btrfs_root *root, struct inode *dir, |
Nikolay Borisov | 9babda9 | 2020-03-13 17:23:20 +0200 | [diff] [blame] | 753 | struct dentry *dentry, bool readonly, |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 754 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 755 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 756 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 757 | struct inode *inode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 758 | struct btrfs_pending_snapshot *pending_snapshot; |
| 759 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 760 | int ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 761 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 762 | if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 763 | return -EINVAL; |
| 764 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 765 | if (atomic_read(&root->nr_swapfiles)) { |
| 766 | btrfs_warn(fs_info, |
| 767 | "cannot snapshot subvolume with active swapfile"); |
| 768 | return -ETXTBSY; |
| 769 | } |
| 770 | |
David Sterba | 23269bf | 2017-02-13 11:03:44 +0100 | [diff] [blame] | 771 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); |
David Sterba | a1ee736 | 2015-11-10 18:53:56 +0100 | [diff] [blame] | 772 | if (!pending_snapshot) |
| 773 | return -ENOMEM; |
| 774 | |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame^] | 775 | ret = get_anon_bdev(&pending_snapshot->anon_dev); |
| 776 | if (ret < 0) |
| 777 | goto free_pending; |
David Sterba | b0c0ea6 | 2015-11-10 18:54:00 +0100 | [diff] [blame] | 778 | pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), |
David Sterba | 23269bf | 2017-02-13 11:03:44 +0100 | [diff] [blame] | 779 | GFP_KERNEL); |
David Sterba | 8546b57 | 2015-11-10 18:54:03 +0100 | [diff] [blame] | 780 | pending_snapshot->path = btrfs_alloc_path(); |
| 781 | if (!pending_snapshot->root_item || !pending_snapshot->path) { |
David Sterba | b0c0ea6 | 2015-11-10 18:54:00 +0100 | [diff] [blame] | 782 | ret = -ENOMEM; |
| 783 | goto free_pending; |
| 784 | } |
| 785 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 786 | btrfs_init_block_rsv(&pending_snapshot->block_rsv, |
| 787 | BTRFS_BLOCK_RSV_TEMP); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 788 | /* |
| 789 | * 1 - parent dir inode |
| 790 | * 2 - dir entries |
| 791 | * 1 - root item |
| 792 | * 2 - root ref/backref |
| 793 | * 1 - root of snapshot |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 794 | * 1 - UUID item |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 795 | */ |
| 796 | ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 797 | &pending_snapshot->block_rsv, 8, |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 798 | false); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 799 | if (ret) |
Robbie Ko | c11fbb6 | 2020-05-14 17:19:18 +0800 | [diff] [blame] | 800 | goto free_pending; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 801 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 802 | pending_snapshot->dentry = dentry; |
| 803 | pending_snapshot->root = root; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 804 | pending_snapshot->readonly = readonly; |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 805 | pending_snapshot->dir = dir; |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 806 | pending_snapshot->inherit = inherit; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 807 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 808 | trans = btrfs_start_transaction(root, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 809 | if (IS_ERR(trans)) { |
| 810 | ret = PTR_ERR(trans); |
| 811 | goto fail; |
| 812 | } |
| 813 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 814 | spin_lock(&fs_info->trans_lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 815 | list_add(&pending_snapshot->list, |
| 816 | &trans->transaction->pending_snapshots); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 817 | spin_unlock(&fs_info->trans_lock); |
Nikolay Borisov | 9babda9 | 2020-03-13 17:23:20 +0200 | [diff] [blame] | 818 | |
| 819 | ret = btrfs_commit_transaction(trans); |
Miao Xie | aec8030 | 2013-03-04 09:44:29 +0000 | [diff] [blame] | 820 | if (ret) |
Josef Bacik | c37b2b6 | 2012-10-22 15:51:44 -0400 | [diff] [blame] | 821 | goto fail; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 822 | |
| 823 | ret = pending_snapshot->error; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 824 | if (ret) |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 825 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 826 | |
Chris Mason | d379730 | 2014-10-15 13:50:56 -0700 | [diff] [blame] | 827 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
| 828 | if (ret) |
| 829 | goto fail; |
| 830 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 831 | inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 832 | if (IS_ERR(inode)) { |
| 833 | ret = PTR_ERR(inode); |
| 834 | goto fail; |
| 835 | } |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 836 | |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 837 | d_instantiate(dentry, inode); |
| 838 | ret = 0; |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame^] | 839 | pending_snapshot->anon_dev = 0; |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 840 | fail: |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame^] | 841 | /* Prevent double freeing of anon_dev */ |
| 842 | if (ret && pending_snapshot->snap) |
| 843 | pending_snapshot->snap->anon_dev = 0; |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 844 | btrfs_put_root(pending_snapshot->snap); |
David Sterba | 7775c81 | 2017-02-10 19:18:18 +0100 | [diff] [blame] | 845 | btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv); |
David Sterba | b0c0ea6 | 2015-11-10 18:54:00 +0100 | [diff] [blame] | 846 | free_pending: |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame^] | 847 | if (pending_snapshot->anon_dev) |
| 848 | free_anon_bdev(pending_snapshot->anon_dev); |
David Sterba | b0c0ea6 | 2015-11-10 18:54:00 +0100 | [diff] [blame] | 849 | kfree(pending_snapshot->root_item); |
David Sterba | 8546b57 | 2015-11-10 18:54:03 +0100 | [diff] [blame] | 850 | btrfs_free_path(pending_snapshot->path); |
David Sterba | a1ee736 | 2015-11-10 18:53:56 +0100 | [diff] [blame] | 851 | kfree(pending_snapshot); |
| 852 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 853 | return ret; |
| 854 | } |
| 855 | |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 856 | /* copy of may_delete in fs/namei.c() |
| 857 | * Check whether we can remove a link victim from directory dir, check |
| 858 | * whether the type of victim is right. |
| 859 | * 1. We can't do it if dir is read-only (done in permission()) |
| 860 | * 2. We should have write and exec permissions on dir |
| 861 | * 3. We can't remove anything from append-only dir |
| 862 | * 4. We can't do anything with immutable dir (done in permission()) |
| 863 | * 5. If the sticky bit on dir is set we should either |
| 864 | * a. be owner of dir, or |
| 865 | * b. be owner of victim, or |
| 866 | * c. have CAP_FOWNER capability |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 867 | * 6. If the victim is append-only or immutable we can't do anything with |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 868 | * links pointing to it. |
| 869 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 870 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 871 | * 9. We can't remove a root or mountpoint. |
| 872 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by |
| 873 | * nfs_async_unlink(). |
| 874 | */ |
| 875 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 876 | static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 877 | { |
| 878 | int error; |
| 879 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 880 | if (d_really_is_negative(victim)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 881 | return -ENOENT; |
| 882 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 883 | BUG_ON(d_inode(victim->d_parent) != dir); |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 884 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 885 | |
| 886 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 887 | if (error) |
| 888 | return error; |
| 889 | if (IS_APPEND(dir)) |
| 890 | return -EPERM; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 891 | if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) || |
| 892 | IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim))) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 893 | return -EPERM; |
| 894 | if (isdir) { |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 895 | if (!d_is_dir(victim)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 896 | return -ENOTDIR; |
| 897 | if (IS_ROOT(victim)) |
| 898 | return -EBUSY; |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 899 | } else if (d_is_dir(victim)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 900 | return -EISDIR; |
| 901 | if (IS_DEADDIR(dir)) |
| 902 | return -ENOENT; |
| 903 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 904 | return -EBUSY; |
| 905 | return 0; |
| 906 | } |
| 907 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 908 | /* copy of may_create in fs/namei.c() */ |
| 909 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) |
| 910 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 911 | if (d_really_is_positive(child)) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 912 | return -EEXIST; |
| 913 | if (IS_DEADDIR(dir)) |
| 914 | return -ENOENT; |
| 915 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 916 | } |
| 917 | |
| 918 | /* |
| 919 | * Create a new subvolume below @parent. This is largely modeled after |
| 920 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup |
| 921 | * inside this filesystem so it's quite a bit simpler. |
| 922 | */ |
Al Viro | 9287209 | 2016-11-20 19:34:31 -0500 | [diff] [blame] | 923 | static noinline int btrfs_mksubvol(const struct path *parent, |
David Sterba | 52f75f4 | 2017-02-14 18:33:53 +0100 | [diff] [blame] | 924 | const char *name, int namelen, |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 925 | struct btrfs_root *snap_src, |
Nikolay Borisov | 9babda9 | 2020-03-13 17:23:20 +0200 | [diff] [blame] | 926 | bool readonly, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 927 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 928 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 929 | struct inode *dir = d_inode(parent->dentry); |
| 930 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 931 | struct dentry *dentry; |
| 932 | int error; |
| 933 | |
Al Viro | 0023541 | 2016-05-26 00:05:12 -0400 | [diff] [blame] | 934 | error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
| 935 | if (error == -EINTR) |
| 936 | return error; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 937 | |
| 938 | dentry = lookup_one_len(name, parent->dentry, namelen); |
| 939 | error = PTR_ERR(dentry); |
| 940 | if (IS_ERR(dentry)) |
| 941 | goto out_unlock; |
| 942 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 943 | error = btrfs_may_create(dir, dentry); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 944 | if (error) |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 945 | goto out_dput; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 946 | |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 947 | /* |
| 948 | * even if this name doesn't exist, we may get hash collisions. |
| 949 | * check for them now when we can safely fail |
| 950 | */ |
| 951 | error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, |
| 952 | dir->i_ino, name, |
| 953 | namelen); |
| 954 | if (error) |
| 955 | goto out_dput; |
| 956 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 957 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 958 | |
| 959 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) |
| 960 | goto out_up_read; |
| 961 | |
Nikolay Borisov | 9babda9 | 2020-03-13 17:23:20 +0200 | [diff] [blame] | 962 | if (snap_src) |
| 963 | error = create_snapshot(snap_src, dir, dentry, readonly, inherit); |
| 964 | else |
| 965 | error = create_subvol(dir, dentry, name, namelen, inherit); |
| 966 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 967 | if (!error) |
| 968 | fsnotify_mkdir(dir, dentry); |
| 969 | out_up_read: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 970 | up_read(&fs_info->subvol_sem); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 971 | out_dput: |
| 972 | dput(dentry); |
| 973 | out_unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 974 | inode_unlock(dir); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 975 | return error; |
| 976 | } |
| 977 | |
Robbie Ko | c11fbb6 | 2020-05-14 17:19:18 +0800 | [diff] [blame] | 978 | static noinline int btrfs_mksnapshot(const struct path *parent, |
| 979 | const char *name, int namelen, |
| 980 | struct btrfs_root *root, |
| 981 | bool readonly, |
| 982 | struct btrfs_qgroup_inherit *inherit) |
| 983 | { |
| 984 | int ret; |
| 985 | bool snapshot_force_cow = false; |
| 986 | |
| 987 | /* |
| 988 | * Force new buffered writes to reserve space even when NOCOW is |
| 989 | * possible. This is to avoid later writeback (running dealloc) to |
| 990 | * fallback to COW mode and unexpectedly fail with ENOSPC. |
| 991 | */ |
| 992 | btrfs_drew_read_lock(&root->snapshot_lock); |
| 993 | |
| 994 | ret = btrfs_start_delalloc_snapshot(root); |
| 995 | if (ret) |
| 996 | goto out; |
| 997 | |
| 998 | /* |
| 999 | * All previous writes have started writeback in NOCOW mode, so now |
| 1000 | * we force future writes to fallback to COW mode during snapshot |
| 1001 | * creation. |
| 1002 | */ |
| 1003 | atomic_inc(&root->snapshot_force_cow); |
| 1004 | snapshot_force_cow = true; |
| 1005 | |
| 1006 | btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); |
| 1007 | |
| 1008 | ret = btrfs_mksubvol(parent, name, namelen, |
| 1009 | root, readonly, inherit); |
| 1010 | out: |
| 1011 | if (snapshot_force_cow) |
| 1012 | atomic_dec(&root->snapshot_force_cow); |
| 1013 | btrfs_drew_read_unlock(&root->snapshot_lock); |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1017 | /* |
| 1018 | * When we're defragging a range, we don't want to kick it off again |
| 1019 | * if it is really just waiting for delalloc to send it down. |
| 1020 | * If we find a nice big extent or delalloc range for the bytes in the |
| 1021 | * file you want to defrag, we return 0 to let you know to skip this |
| 1022 | * part of the file |
| 1023 | */ |
David Sterba | aab110a | 2014-07-29 17:32:10 +0200 | [diff] [blame] | 1024 | static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1025 | { |
| 1026 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 1027 | struct extent_map *em = NULL; |
| 1028 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 1029 | u64 end; |
| 1030 | |
| 1031 | read_lock(&em_tree->lock); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1032 | em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1033 | read_unlock(&em_tree->lock); |
| 1034 | |
| 1035 | if (em) { |
| 1036 | end = extent_map_end(em); |
| 1037 | free_extent_map(em); |
| 1038 | if (end - offset > thresh) |
| 1039 | return 0; |
| 1040 | } |
| 1041 | /* if we already have a nice delalloc here, just stop */ |
| 1042 | thresh /= 2; |
| 1043 | end = count_range_bits(io_tree, &offset, offset + thresh, |
| 1044 | thresh, EXTENT_DELALLOC, 1); |
| 1045 | if (end >= thresh) |
| 1046 | return 0; |
| 1047 | return 1; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * helper function to walk through a file and find extents |
| 1052 | * newer than a specific transid, and smaller than thresh. |
| 1053 | * |
| 1054 | * This is used by the defragging code to find new and small |
| 1055 | * extents |
| 1056 | */ |
| 1057 | static int find_new_extents(struct btrfs_root *root, |
| 1058 | struct inode *inode, u64 newer_than, |
David Sterba | aab110a | 2014-07-29 17:32:10 +0200 | [diff] [blame] | 1059 | u64 *off, u32 thresh) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1060 | { |
| 1061 | struct btrfs_path *path; |
| 1062 | struct btrfs_key min_key; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1063 | struct extent_buffer *leaf; |
| 1064 | struct btrfs_file_extent_item *extent; |
| 1065 | int type; |
| 1066 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1067 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1068 | |
| 1069 | path = btrfs_alloc_path(); |
| 1070 | if (!path) |
| 1071 | return -ENOMEM; |
| 1072 | |
David Sterba | a4689d2 | 2011-05-31 17:08:14 +0000 | [diff] [blame] | 1073 | min_key.objectid = ino; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1074 | min_key.type = BTRFS_EXTENT_DATA_KEY; |
| 1075 | min_key.offset = *off; |
| 1076 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 1077 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 1078 | ret = btrfs_search_forward(root, &min_key, path, newer_than); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1079 | if (ret != 0) |
| 1080 | goto none; |
Filipe Manana | f094c9bd | 2014-03-12 01:28:24 +0000 | [diff] [blame] | 1081 | process_slot: |
David Sterba | a4689d2 | 2011-05-31 17:08:14 +0000 | [diff] [blame] | 1082 | if (min_key.objectid != ino) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1083 | goto none; |
| 1084 | if (min_key.type != BTRFS_EXTENT_DATA_KEY) |
| 1085 | goto none; |
| 1086 | |
| 1087 | leaf = path->nodes[0]; |
| 1088 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1089 | struct btrfs_file_extent_item); |
| 1090 | |
| 1091 | type = btrfs_file_extent_type(leaf, extent); |
| 1092 | if (type == BTRFS_FILE_EXTENT_REG && |
| 1093 | btrfs_file_extent_num_bytes(leaf, extent) < thresh && |
| 1094 | check_defrag_in_cache(inode, min_key.offset, thresh)) { |
| 1095 | *off = min_key.offset; |
| 1096 | btrfs_free_path(path); |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
Filipe Manana | f094c9bd | 2014-03-12 01:28:24 +0000 | [diff] [blame] | 1100 | path->slots[0]++; |
| 1101 | if (path->slots[0] < btrfs_header_nritems(leaf)) { |
| 1102 | btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); |
| 1103 | goto process_slot; |
| 1104 | } |
| 1105 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1106 | if (min_key.offset == (u64)-1) |
| 1107 | goto none; |
| 1108 | |
| 1109 | min_key.offset++; |
| 1110 | btrfs_release_path(path); |
| 1111 | } |
| 1112 | none: |
| 1113 | btrfs_free_path(path); |
| 1114 | return -ENOENT; |
| 1115 | } |
| 1116 | |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1117 | static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1118 | { |
| 1119 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1120 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1121 | struct extent_map *em; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1122 | u64 len = PAGE_SIZE; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1123 | |
| 1124 | /* |
| 1125 | * hopefully we have this extent in the tree already, try without |
| 1126 | * the full extent lock |
| 1127 | */ |
| 1128 | read_lock(&em_tree->lock); |
| 1129 | em = lookup_extent_mapping(em_tree, start, len); |
| 1130 | read_unlock(&em_tree->lock); |
| 1131 | |
| 1132 | if (!em) { |
Filipe Manana | 308d980 | 2014-03-11 13:56:15 +0000 | [diff] [blame] | 1133 | struct extent_state *cached = NULL; |
| 1134 | u64 end = start + len - 1; |
| 1135 | |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1136 | /* get the big lock and read metadata off disk */ |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1137 | lock_extent_bits(io_tree, start, end, &cached); |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 1138 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1139 | unlock_extent_cached(io_tree, start, end, &cached); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1140 | |
Dan Carpenter | 6cf8bfb | 2010-03-20 11:22:10 +0000 | [diff] [blame] | 1141 | if (IS_ERR(em)) |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1142 | return NULL; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1143 | } |
| 1144 | |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1145 | return em; |
| 1146 | } |
| 1147 | |
| 1148 | static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) |
| 1149 | { |
| 1150 | struct extent_map *next; |
| 1151 | bool ret = true; |
| 1152 | |
| 1153 | /* this is the last extent */ |
| 1154 | if (em->start + em->len >= i_size_read(inode)) |
| 1155 | return false; |
| 1156 | |
| 1157 | next = defrag_lookup_extent(inode, em->start + em->len); |
Chris Mason | e9512d7 | 2014-08-26 13:55:54 -0700 | [diff] [blame] | 1158 | if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) |
| 1159 | ret = false; |
| 1160 | else if ((em->block_start + em->block_len == next->block_start) && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1161 | (em->block_len > SZ_128K && next->block_len > SZ_128K)) |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1162 | ret = false; |
| 1163 | |
| 1164 | free_extent_map(next); |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
David Sterba | aab110a | 2014-07-29 17:32:10 +0200 | [diff] [blame] | 1168 | static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, |
Andrew Mahone | a43a211 | 2012-06-19 21:08:32 -0400 | [diff] [blame] | 1169 | u64 *last_len, u64 *skip, u64 *defrag_end, |
| 1170 | int compress) |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1171 | { |
| 1172 | struct extent_map *em; |
| 1173 | int ret = 1; |
| 1174 | bool next_mergeable = true; |
Liu Bo | 4a3560c | 2015-08-07 16:48:41 +0800 | [diff] [blame] | 1175 | bool prev_mergeable = true; |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1176 | |
| 1177 | /* |
| 1178 | * make sure that once we start defragging an extent, we keep on |
| 1179 | * defragging it |
| 1180 | */ |
| 1181 | if (start < *defrag_end) |
| 1182 | return 1; |
| 1183 | |
| 1184 | *skip = 0; |
| 1185 | |
| 1186 | em = defrag_lookup_extent(inode, start); |
| 1187 | if (!em) |
| 1188 | return 0; |
| 1189 | |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1190 | /* this will cover holes, and inline extents */ |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1191 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1192 | ret = 0; |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1193 | goto out; |
| 1194 | } |
| 1195 | |
Liu Bo | 4a3560c | 2015-08-07 16:48:41 +0800 | [diff] [blame] | 1196 | if (!*defrag_end) |
| 1197 | prev_mergeable = false; |
| 1198 | |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1199 | next_mergeable = defrag_check_next_extent(inode, em); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1200 | /* |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1201 | * we hit a real extent, if it is big or the next extent is not a |
| 1202 | * real extent, don't bother defragging it |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1203 | */ |
Andrew Mahone | a43a211 | 2012-06-19 21:08:32 -0400 | [diff] [blame] | 1204 | if (!compress && (*last_len == 0 || *last_len >= thresh) && |
Liu Bo | 4a3560c | 2015-08-07 16:48:41 +0800 | [diff] [blame] | 1205 | (em->len >= thresh || (!next_mergeable && !prev_mergeable))) |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1206 | ret = 0; |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1207 | out: |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1208 | /* |
| 1209 | * last_len ends up being a counter of how many bytes we've defragged. |
| 1210 | * every time we choose not to defrag an extent, we reset *last_len |
| 1211 | * so that the next tiny extent will force a defrag. |
| 1212 | * |
| 1213 | * The end result of this is that tiny extents before a single big |
| 1214 | * extent will force at least part of that big extent to be defragged. |
| 1215 | */ |
| 1216 | if (ret) { |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1217 | *defrag_end = extent_map_end(em); |
| 1218 | } else { |
| 1219 | *last_len = 0; |
| 1220 | *skip = extent_map_end(em); |
| 1221 | *defrag_end = 0; |
| 1222 | } |
| 1223 | |
| 1224 | free_extent_map(em); |
| 1225 | return ret; |
| 1226 | } |
| 1227 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1228 | /* |
| 1229 | * it doesn't do much good to defrag one or two pages |
| 1230 | * at a time. This pulls in a nice chunk of pages |
| 1231 | * to COW and defrag. |
| 1232 | * |
| 1233 | * It also makes sure the delalloc code has enough |
| 1234 | * dirty data to avoid making new small extents as part |
| 1235 | * of the defrag |
| 1236 | * |
| 1237 | * It's a good idea to start RA on this range |
| 1238 | * before calling this. |
| 1239 | */ |
| 1240 | static int cluster_pages_for_defrag(struct inode *inode, |
| 1241 | struct page **pages, |
| 1242 | unsigned long start_index, |
Justin Maggard | c41570c | 2014-01-21 11:18:29 -0800 | [diff] [blame] | 1243 | unsigned long num_pages) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1244 | { |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1245 | unsigned long file_end; |
| 1246 | u64 isize = i_size_read(inode); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1247 | u64 page_start; |
| 1248 | u64 page_end; |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1249 | u64 page_cnt; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1250 | int ret; |
| 1251 | int i; |
| 1252 | int i_done; |
| 1253 | struct btrfs_ordered_extent *ordered; |
| 1254 | struct extent_state *cached_state = NULL; |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1255 | struct extent_io_tree *tree; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 1256 | struct extent_changeset *data_reserved = NULL; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 1257 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1258 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1259 | file_end = (isize - 1) >> PAGE_SHIFT; |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1260 | if (!isize || start_index > file_end) |
| 1261 | return 0; |
| 1262 | |
| 1263 | page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1264 | |
Nikolay Borisov | e5b7231e | 2020-06-03 08:55:42 +0300 | [diff] [blame] | 1265 | ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1266 | start_index << PAGE_SHIFT, |
| 1267 | page_cnt << PAGE_SHIFT); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1268 | if (ret) |
| 1269 | return ret; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1270 | i_done = 0; |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1271 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1272 | |
| 1273 | /* step one, lock all the pages */ |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1274 | for (i = 0; i < page_cnt; i++) { |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1275 | struct page *page; |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1276 | again: |
Josef Bacik | a94733d | 2011-07-11 10:47:06 -0400 | [diff] [blame] | 1277 | page = find_or_create_page(inode->i_mapping, |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1278 | start_index + i, mask); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1279 | if (!page) |
| 1280 | break; |
| 1281 | |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1282 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1283 | page_end = page_start + PAGE_SIZE - 1; |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1284 | while (1) { |
Filipe Manana | 308d980 | 2014-03-11 13:56:15 +0000 | [diff] [blame] | 1285 | lock_extent_bits(tree, page_start, page_end, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1286 | &cached_state); |
Nikolay Borisov | c350437 | 2020-06-03 08:55:03 +0300 | [diff] [blame] | 1287 | ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1288 | page_start); |
Filipe Manana | 308d980 | 2014-03-11 13:56:15 +0000 | [diff] [blame] | 1289 | unlock_extent_cached(tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1290 | &cached_state); |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1291 | if (!ordered) |
| 1292 | break; |
| 1293 | |
| 1294 | unlock_page(page); |
| 1295 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1296 | btrfs_put_ordered_extent(ordered); |
| 1297 | lock_page(page); |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1298 | /* |
| 1299 | * we unlocked the page above, so we need check if |
| 1300 | * it was released or not. |
| 1301 | */ |
| 1302 | if (page->mapping != inode->i_mapping) { |
| 1303 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1304 | put_page(page); |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1305 | goto again; |
| 1306 | } |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1307 | } |
| 1308 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1309 | if (!PageUptodate(page)) { |
| 1310 | btrfs_readpage(NULL, page); |
| 1311 | lock_page(page); |
| 1312 | if (!PageUptodate(page)) { |
| 1313 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1314 | put_page(page); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1315 | ret = -EIO; |
| 1316 | break; |
| 1317 | } |
| 1318 | } |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1319 | |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1320 | if (page->mapping != inode->i_mapping) { |
| 1321 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1322 | put_page(page); |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1323 | goto again; |
| 1324 | } |
| 1325 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1326 | pages[i] = page; |
| 1327 | i_done++; |
| 1328 | } |
| 1329 | if (!i_done || ret) |
| 1330 | goto out; |
| 1331 | |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1332 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1333 | goto out; |
| 1334 | |
| 1335 | /* |
| 1336 | * so now we have a nice long stream of locked |
| 1337 | * and up to date pages, lets wait on them |
| 1338 | */ |
| 1339 | for (i = 0; i < i_done; i++) |
| 1340 | wait_on_page_writeback(pages[i]); |
| 1341 | |
| 1342 | page_start = page_offset(pages[0]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1343 | page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1344 | |
| 1345 | lock_extent_bits(&BTRFS_I(inode)->io_tree, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1346 | page_start, page_end - 1, &cached_state); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1347 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 1348 | page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 1349 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1350 | |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1351 | if (i_done != page_cnt) { |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1352 | spin_lock(&BTRFS_I(inode)->lock); |
Su Yue | 28c4a3e | 2018-09-05 11:07:33 +0800 | [diff] [blame] | 1353 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1354 | spin_unlock(&BTRFS_I(inode)->lock); |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 1355 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1356 | start_index << PAGE_SHIFT, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 1357 | (page_cnt - i_done) << PAGE_SHIFT, true); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 1361 | set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, |
David Sterba | 018ed4f | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1362 | &cached_state); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1363 | |
| 1364 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1365 | page_start, page_end - 1, &cached_state); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1366 | |
| 1367 | for (i = 0; i < i_done; i++) { |
| 1368 | clear_page_dirty_for_io(pages[i]); |
| 1369 | ClearPageChecked(pages[i]); |
| 1370 | set_page_extent_mapped(pages[i]); |
| 1371 | set_page_dirty(pages[i]); |
| 1372 | unlock_page(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1373 | put_page(pages[i]); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1374 | } |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 1375 | btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 1376 | extent_changeset_free(data_reserved); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1377 | return i_done; |
| 1378 | out: |
| 1379 | for (i = 0; i < i_done; i++) { |
| 1380 | unlock_page(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1381 | put_page(pages[i]); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1382 | } |
Nikolay Borisov | 86d5292 | 2020-06-03 08:55:40 +0300 | [diff] [blame] | 1383 | btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1384 | start_index << PAGE_SHIFT, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 1385 | page_cnt << PAGE_SHIFT, true); |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 1386 | btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 1387 | extent_changeset_free(data_reserved); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1388 | return ret; |
| 1389 | |
| 1390 | } |
| 1391 | |
| 1392 | int btrfs_defrag_file(struct inode *inode, struct file *file, |
| 1393 | struct btrfs_ioctl_defrag_range_args *range, |
| 1394 | u64 newer_than, unsigned long max_to_defrag) |
| 1395 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1396 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1397 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1398 | struct file_ra_state *ra = NULL; |
| 1399 | unsigned long last_index; |
Li Zefan | 151a31b | 2011-09-02 15:56:39 +0800 | [diff] [blame] | 1400 | u64 isize = i_size_read(inode); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1401 | u64 last_len = 0; |
| 1402 | u64 skip = 0; |
| 1403 | u64 defrag_end = 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1404 | u64 newer_off = range->start; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1405 | unsigned long i; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1406 | unsigned long ra_index = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1407 | int ret; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1408 | int defrag_count = 0; |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1409 | int compress_type = BTRFS_COMPRESS_ZLIB; |
David Sterba | aab110a | 2014-07-29 17:32:10 +0200 | [diff] [blame] | 1410 | u32 extent_thresh = range->extent_thresh; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1411 | unsigned long max_cluster = SZ_256K >> PAGE_SHIFT; |
Justin Maggard | c41570c | 2014-01-21 11:18:29 -0800 | [diff] [blame] | 1412 | unsigned long cluster = max_cluster; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1413 | u64 new_align = ~((u64)SZ_128K - 1); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1414 | struct page **pages = NULL; |
David Sterba | 1e2ef46 | 2017-07-17 20:01:59 +0200 | [diff] [blame] | 1415 | bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1416 | |
Liu Bo | 0abd5b1 | 2013-04-16 09:20:28 +0000 | [diff] [blame] | 1417 | if (isize == 0) |
| 1418 | return 0; |
| 1419 | |
| 1420 | if (range->start >= isize) |
| 1421 | return -EINVAL; |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1422 | |
David Sterba | 1e2ef46 | 2017-07-17 20:01:59 +0200 | [diff] [blame] | 1423 | if (do_compress) { |
Chengguang Xu | ce96b7f | 2019-10-10 15:59:57 +0800 | [diff] [blame] | 1424 | if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES) |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1425 | return -EINVAL; |
| 1426 | if (range->compress_type) |
| 1427 | compress_type = range->compress_type; |
| 1428 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1429 | |
Liu Bo | 0abd5b1 | 2013-04-16 09:20:28 +0000 | [diff] [blame] | 1430 | if (extent_thresh == 0) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1431 | extent_thresh = SZ_256K; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1432 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1433 | /* |
David Sterba | 0a52d10 | 2017-06-22 03:22:58 +0200 | [diff] [blame] | 1434 | * If we were not given a file, allocate a readahead context. As |
| 1435 | * readahead is just an optimization, defrag will work without it so |
| 1436 | * we don't error out. |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1437 | */ |
| 1438 | if (!file) { |
David Sterba | 63e727e | 2017-06-22 03:13:02 +0200 | [diff] [blame] | 1439 | ra = kzalloc(sizeof(*ra), GFP_KERNEL); |
David Sterba | 0a52d10 | 2017-06-22 03:22:58 +0200 | [diff] [blame] | 1440 | if (ra) |
| 1441 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1442 | } else { |
| 1443 | ra = &file->f_ra; |
| 1444 | } |
| 1445 | |
David Sterba | 63e727e | 2017-06-22 03:13:02 +0200 | [diff] [blame] | 1446 | pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1447 | if (!pages) { |
| 1448 | ret = -ENOMEM; |
| 1449 | goto out_ra; |
| 1450 | } |
| 1451 | |
| 1452 | /* find the last page to defrag */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1453 | if (range->start + range->len > range->start) { |
Li Zefan | 151a31b | 2011-09-02 15:56:39 +0800 | [diff] [blame] | 1454 | last_index = min_t(u64, isize - 1, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1455 | range->start + range->len - 1) >> PAGE_SHIFT; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1456 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1457 | last_index = (isize - 1) >> PAGE_SHIFT; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1458 | } |
| 1459 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1460 | if (newer_than) { |
| 1461 | ret = find_new_extents(root, inode, newer_than, |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1462 | &newer_off, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1463 | if (!ret) { |
| 1464 | range->start = newer_off; |
| 1465 | /* |
| 1466 | * we always align our defrag to help keep |
| 1467 | * the extents in the file evenly spaced |
| 1468 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1469 | i = (newer_off & new_align) >> PAGE_SHIFT; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1470 | } else |
| 1471 | goto out_ra; |
| 1472 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1473 | i = range->start >> PAGE_SHIFT; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1474 | } |
| 1475 | if (!max_to_defrag) |
chandan | 070034b | 2015-06-09 10:35:11 +0530 | [diff] [blame] | 1476 | max_to_defrag = last_index - i + 1; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1477 | |
Li Zefan | 2a0f7f5 | 2011-10-10 15:43:34 -0400 | [diff] [blame] | 1478 | /* |
| 1479 | * make writeback starts from i, so the defrag range can be |
| 1480 | * written sequentially. |
| 1481 | */ |
| 1482 | if (i < inode->i_mapping->writeback_index) |
| 1483 | inode->i_mapping->writeback_index = i; |
| 1484 | |
Chris Mason | f7f43cc | 2011-10-11 11:41:40 -0400 | [diff] [blame] | 1485 | while (i <= last_index && defrag_count < max_to_defrag && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1486 | (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) { |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1487 | /* |
| 1488 | * make sure we stop running if someone unmounts |
| 1489 | * the FS |
| 1490 | */ |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1491 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1492 | break; |
| 1493 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1494 | if (btrfs_defrag_cancelled(fs_info)) { |
| 1495 | btrfs_debug(fs_info, "defrag_file cancelled"); |
David Sterba | 210549e | 2013-02-09 23:38:06 +0000 | [diff] [blame] | 1496 | ret = -EAGAIN; |
| 1497 | break; |
| 1498 | } |
| 1499 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1500 | if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1501 | extent_thresh, &last_len, &skip, |
David Sterba | 1e2ef46 | 2017-07-17 20:01:59 +0200 | [diff] [blame] | 1502 | &defrag_end, do_compress)){ |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1503 | unsigned long next; |
| 1504 | /* |
| 1505 | * the should_defrag function tells us how much to skip |
| 1506 | * bump our counter by the suggested amount |
| 1507 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1508 | next = DIV_ROUND_UP(skip, PAGE_SIZE); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1509 | i = max(i + 1, next); |
| 1510 | continue; |
| 1511 | } |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1512 | |
| 1513 | if (!newer_than) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1514 | cluster = (PAGE_ALIGN(defrag_end) >> |
| 1515 | PAGE_SHIFT) - i; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1516 | cluster = min(cluster, max_cluster); |
| 1517 | } else { |
| 1518 | cluster = max_cluster; |
| 1519 | } |
| 1520 | |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1521 | if (i + cluster > ra_index) { |
| 1522 | ra_index = max(i, ra_index); |
David Sterba | 0a52d10 | 2017-06-22 03:22:58 +0200 | [diff] [blame] | 1523 | if (ra) |
David Sterba | d3c0bab | 2017-06-22 03:35:28 +0200 | [diff] [blame] | 1524 | page_cache_sync_readahead(inode->i_mapping, ra, |
| 1525 | file, ra_index, cluster); |
chandan | e4826a5 | 2015-06-09 17:38:32 +0530 | [diff] [blame] | 1526 | ra_index += cluster; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1527 | } |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1528 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1529 | inode_lock(inode); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 1530 | if (IS_SWAPFILE(inode)) { |
| 1531 | ret = -ETXTBSY; |
| 1532 | } else { |
| 1533 | if (do_compress) |
| 1534 | BTRFS_I(inode)->defrag_compress = compress_type; |
| 1535 | ret = cluster_pages_for_defrag(inode, pages, i, cluster); |
| 1536 | } |
Liu Bo | ecb8bea | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1537 | if (ret < 0) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1538 | inode_unlock(inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1539 | goto out_ra; |
Liu Bo | ecb8bea | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1540 | } |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1541 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1542 | defrag_count += ret; |
Namjae Jeon | d0e1d66 | 2012-12-11 16:00:21 -0800 | [diff] [blame] | 1543 | balance_dirty_pages_ratelimited(inode->i_mapping); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1544 | inode_unlock(inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1545 | |
| 1546 | if (newer_than) { |
| 1547 | if (newer_off == (u64)-1) |
| 1548 | break; |
| 1549 | |
Liu Bo | e1f041e | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1550 | if (ret > 0) |
| 1551 | i += ret; |
| 1552 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1553 | newer_off = max(newer_off + 1, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1554 | (u64)i << PAGE_SHIFT); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1555 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1556 | ret = find_new_extents(root, inode, newer_than, |
| 1557 | &newer_off, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1558 | if (!ret) { |
| 1559 | range->start = newer_off; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1560 | i = (newer_off & new_align) >> PAGE_SHIFT; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1561 | } else { |
| 1562 | break; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1563 | } |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1564 | } else { |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1565 | if (ret > 0) { |
Li Zefan | cbcc832 | 2011-09-02 15:56:25 +0800 | [diff] [blame] | 1566 | i += ret; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1567 | last_len += ret << PAGE_SHIFT; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1568 | } else { |
Li Zefan | cbcc832 | 2011-09-02 15:56:25 +0800 | [diff] [blame] | 1569 | i++; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1570 | last_len = 0; |
| 1571 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1572 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1573 | } |
| 1574 | |
Filipe Manana | dec8ef9 | 2014-03-01 10:55:54 +0000 | [diff] [blame] | 1575 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1576 | filemap_flush(inode->i_mapping); |
Filipe Manana | dec8ef9 | 2014-03-01 10:55:54 +0000 | [diff] [blame] | 1577 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1578 | &BTRFS_I(inode)->runtime_flags)) |
| 1579 | filemap_flush(inode->i_mapping); |
| 1580 | } |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1581 | |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1582 | if (range->compress_type == BTRFS_COMPRESS_LZO) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1583 | btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 1584 | } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) { |
| 1585 | btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1586 | } |
| 1587 | |
Diego Calleja | 60ccf82 | 2011-09-01 16:33:57 +0200 | [diff] [blame] | 1588 | ret = defrag_count; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1589 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1590 | out_ra: |
David Sterba | 1e2ef46 | 2017-07-17 20:01:59 +0200 | [diff] [blame] | 1591 | if (do_compress) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1592 | inode_lock(inode); |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 1593 | BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1594 | inode_unlock(inode); |
Filipe David Borba Manana | 633085c | 2013-08-16 15:23:33 +0100 | [diff] [blame] | 1595 | } |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1596 | if (!file) |
| 1597 | kfree(ra); |
| 1598 | kfree(pages); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1599 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1600 | } |
| 1601 | |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 1602 | static noinline int btrfs_ioctl_resize(struct file *file, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1603 | void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1604 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1605 | struct inode *inode = file_inode(file); |
| 1606 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1607 | u64 new_size; |
| 1608 | u64 old_size; |
| 1609 | u64 devid = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1610 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1611 | struct btrfs_ioctl_vol_args *vol_args; |
| 1612 | struct btrfs_trans_handle *trans; |
| 1613 | struct btrfs_device *device = NULL; |
| 1614 | char *sizestr; |
Gui Hecheng | 9a40f12 | 2014-03-31 18:03:25 +0800 | [diff] [blame] | 1615 | char *retptr; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1616 | char *devstr = NULL; |
| 1617 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1618 | int mod = 0; |
| 1619 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1620 | if (!capable(CAP_SYS_ADMIN)) |
| 1621 | return -EPERM; |
| 1622 | |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 1623 | ret = mnt_want_write_file(file); |
| 1624 | if (ret) |
| 1625 | return ret; |
| 1626 | |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 1627 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
Miao Xie | 9754767 | 2012-12-21 10:38:50 +0000 | [diff] [blame] | 1628 | mnt_drop_write_file(file); |
Anand Jain | e57138b | 2013-08-21 11:44:48 +0800 | [diff] [blame] | 1629 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1630 | } |
| 1631 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 1632 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1633 | if (IS_ERR(vol_args)) { |
| 1634 | ret = PTR_ERR(vol_args); |
| 1635 | goto out; |
| 1636 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 1637 | |
| 1638 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1639 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1640 | sizestr = vol_args->name; |
| 1641 | devstr = strchr(sizestr, ':'); |
| 1642 | if (devstr) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1643 | sizestr = devstr + 1; |
| 1644 | *devstr = '\0'; |
| 1645 | devstr = vol_args->name; |
ZhangZhen | 58dfae6 | 2014-05-13 16:36:08 +0800 | [diff] [blame] | 1646 | ret = kstrtoull(devstr, 10, &devid); |
| 1647 | if (ret) |
| 1648 | goto out_free; |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1649 | if (!devid) { |
| 1650 | ret = -EINVAL; |
| 1651 | goto out_free; |
| 1652 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1653 | btrfs_info(fs_info, "resizing devid %llu", devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1654 | } |
Miao Xie | dba60f3 | 2012-12-21 09:19:51 +0000 | [diff] [blame] | 1655 | |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 1656 | device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1657 | if (!device) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1658 | btrfs_info(fs_info, "resizer unable to find device %llu", |
| 1659 | devid); |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1660 | ret = -ENODEV; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1661 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1662 | } |
Miao Xie | dba60f3 | 2012-12-21 09:19:51 +0000 | [diff] [blame] | 1663 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1664 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1665 | btrfs_info(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1666 | "resizer unable to apply on readonly device %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1667 | devid); |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1668 | ret = -EPERM; |
Liu Bo | 4e42ae1 | 2012-06-14 02:23:19 -0600 | [diff] [blame] | 1669 | goto out_free; |
| 1670 | } |
| 1671 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1672 | if (!strcmp(sizestr, "max")) |
| 1673 | new_size = device->bdev->bd_inode->i_size; |
| 1674 | else { |
| 1675 | if (sizestr[0] == '-') { |
| 1676 | mod = -1; |
| 1677 | sizestr++; |
| 1678 | } else if (sizestr[0] == '+') { |
| 1679 | mod = 1; |
| 1680 | sizestr++; |
| 1681 | } |
Gui Hecheng | 9a40f12 | 2014-03-31 18:03:25 +0800 | [diff] [blame] | 1682 | new_size = memparse(sizestr, &retptr); |
| 1683 | if (*retptr != '\0' || new_size == 0) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1684 | ret = -EINVAL; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1685 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1686 | } |
| 1687 | } |
| 1688 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1689 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1690 | ret = -EPERM; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1691 | goto out_free; |
| 1692 | } |
| 1693 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1694 | old_size = btrfs_device_get_total_bytes(device); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1695 | |
| 1696 | if (mod < 0) { |
| 1697 | if (new_size > old_size) { |
| 1698 | ret = -EINVAL; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1699 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1700 | } |
| 1701 | new_size = old_size - new_size; |
| 1702 | } else if (mod > 0) { |
Wenliang Fan | eb8052e | 2013-12-20 15:28:56 +0800 | [diff] [blame] | 1703 | if (new_size > ULLONG_MAX - old_size) { |
Gui Hecheng | 902c68a | 2014-05-29 09:19:58 +0800 | [diff] [blame] | 1704 | ret = -ERANGE; |
Wenliang Fan | eb8052e | 2013-12-20 15:28:56 +0800 | [diff] [blame] | 1705 | goto out_free; |
| 1706 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1707 | new_size = old_size + new_size; |
| 1708 | } |
| 1709 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1710 | if (new_size < SZ_256M) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1711 | ret = -EINVAL; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1712 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1713 | } |
| 1714 | if (new_size > device->bdev->bd_inode->i_size) { |
| 1715 | ret = -EFBIG; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1716 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1717 | } |
| 1718 | |
Nikolay Borisov | 47f08b9 | 2017-07-18 15:39:08 +0300 | [diff] [blame] | 1719 | new_size = round_down(new_size, fs_info->sectorsize); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1720 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1721 | if (new_size > old_size) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1722 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1723 | if (IS_ERR(trans)) { |
| 1724 | ret = PTR_ERR(trans); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1725 | goto out_free; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1726 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1727 | ret = btrfs_grow_device(trans, device, new_size); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 1728 | btrfs_commit_transaction(trans); |
Mike Fleetwood | ece7d20 | 2011-11-18 18:55:01 +0000 | [diff] [blame] | 1729 | } else if (new_size < old_size) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1730 | ret = btrfs_shrink_device(device, new_size); |
jeff.liu | 0253f40 | 2012-10-27 12:06:39 +0000 | [diff] [blame] | 1731 | } /* equal, nothing need to do */ |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1732 | |
Marcos Paulo de Souza | faf8f7b | 2020-02-11 10:55:26 -0300 | [diff] [blame] | 1733 | if (ret == 0 && new_size != old_size) |
| 1734 | btrfs_info_in_rcu(fs_info, |
| 1735 | "resize device %s (devid %llu) from %llu to %llu", |
| 1736 | rcu_str_deref(device->name), device->devid, |
| 1737 | old_size, new_size); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1738 | out_free: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1739 | kfree(vol_args); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1740 | out: |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 1741 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 18f39c4 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 1742 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1743 | return ret; |
| 1744 | } |
| 1745 | |
Nikolay Borisov | 5d54c67 | 2020-03-13 17:23:19 +0200 | [diff] [blame] | 1746 | static noinline int __btrfs_ioctl_snap_create(struct file *file, |
David Sterba | 52f75f4 | 2017-02-14 18:33:53 +0100 | [diff] [blame] | 1747 | const char *name, unsigned long fd, int subvol, |
Nikolay Borisov | 5d54c67 | 2020-03-13 17:23:19 +0200 | [diff] [blame] | 1748 | bool readonly, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 1749 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1750 | { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1751 | int namelen; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1752 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1753 | |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 1754 | if (!S_ISDIR(file_inode(file)->i_mode)) |
| 1755 | return -ENOTDIR; |
| 1756 | |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1757 | ret = mnt_want_write_file(file); |
| 1758 | if (ret) |
| 1759 | goto out; |
| 1760 | |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1761 | namelen = strlen(name); |
| 1762 | if (strchr(name, '/')) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1763 | ret = -EINVAL; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1764 | goto out_drop_write; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1765 | } |
| 1766 | |
Chris Mason | 16780ca | 2012-02-20 22:14:55 -0500 | [diff] [blame] | 1767 | if (name[0] == '.' && |
| 1768 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { |
| 1769 | ret = -EEXIST; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1770 | goto out_drop_write; |
Chris Mason | 16780ca | 2012-02-20 22:14:55 -0500 | [diff] [blame] | 1771 | } |
| 1772 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1773 | if (subvol) { |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1774 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
Nikolay Borisov | 5d54c67 | 2020-03-13 17:23:19 +0200 | [diff] [blame] | 1775 | NULL, readonly, inherit); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1776 | } else { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1777 | struct fd src = fdget(fd); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1778 | struct inode *src_inode; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1779 | if (!src.file) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1780 | ret = -EINVAL; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1781 | goto out_drop_write; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1782 | } |
| 1783 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1784 | src_inode = file_inode(src.file); |
| 1785 | if (src_inode->i_sb != file_inode(file)->i_sb) { |
Josef Bacik | c79b471 | 2016-03-25 10:02:41 -0400 | [diff] [blame] | 1786 | btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1787 | "Snapshot src from another FS"); |
Kusanagi Kouichi | 23ad5b1 | 2014-01-30 16:32:02 +0900 | [diff] [blame] | 1788 | ret = -EXDEV; |
David Sterba | d024206 | 2014-01-15 18:15:52 +0100 | [diff] [blame] | 1789 | } else if (!inode_owner_or_capable(src_inode)) { |
| 1790 | /* |
| 1791 | * Subvolume creation is not restricted, but snapshots |
| 1792 | * are limited to own subvolumes only |
| 1793 | */ |
| 1794 | ret = -EPERM; |
Al Viro | ecd1881 | 2012-08-26 21:20:24 -0400 | [diff] [blame] | 1795 | } else { |
Robbie Ko | c11fbb6 | 2020-05-14 17:19:18 +0800 | [diff] [blame] | 1796 | ret = btrfs_mksnapshot(&file->f_path, name, namelen, |
Al Viro | ecd1881 | 2012-08-26 21:20:24 -0400 | [diff] [blame] | 1797 | BTRFS_I(src_inode)->root, |
Nikolay Borisov | 5d54c67 | 2020-03-13 17:23:19 +0200 | [diff] [blame] | 1798 | readonly, inherit); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1799 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1800 | fdput(src); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1801 | } |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1802 | out_drop_write: |
| 1803 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1804 | out: |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1805 | return ret; |
| 1806 | } |
| 1807 | |
| 1808 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1809 | void __user *arg, int subvol) |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1810 | { |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1811 | struct btrfs_ioctl_vol_args *vol_args; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1812 | int ret; |
| 1813 | |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 1814 | if (!S_ISDIR(file_inode(file)->i_mode)) |
| 1815 | return -ENOTDIR; |
| 1816 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1817 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1818 | if (IS_ERR(vol_args)) |
| 1819 | return PTR_ERR(vol_args); |
| 1820 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1821 | |
Nikolay Borisov | 5d54c67 | 2020-03-13 17:23:19 +0200 | [diff] [blame] | 1822 | ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd, |
| 1823 | subvol, false, NULL); |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1824 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1825 | kfree(vol_args); |
| 1826 | return ret; |
| 1827 | } |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1828 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1829 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
| 1830 | void __user *arg, int subvol) |
| 1831 | { |
| 1832 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
| 1833 | int ret; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1834 | bool readonly = false; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1835 | struct btrfs_qgroup_inherit *inherit = NULL; |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1836 | |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 1837 | if (!S_ISDIR(file_inode(file)->i_mode)) |
| 1838 | return -ENOTDIR; |
| 1839 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1840 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1841 | if (IS_ERR(vol_args)) |
| 1842 | return PTR_ERR(vol_args); |
| 1843 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; |
Sage Weil | 75eaa0e | 2010-12-10 00:36:28 +0000 | [diff] [blame] | 1844 | |
David Sterba | 673990d | 2020-02-21 13:24:37 +0100 | [diff] [blame] | 1845 | if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1846 | ret = -EOPNOTSUPP; |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1847 | goto free_args; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1848 | } |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1849 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1850 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
| 1851 | readonly = true; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1852 | if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1853 | if (vol_args->size > PAGE_SIZE) { |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1854 | ret = -EINVAL; |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1855 | goto free_args; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1856 | } |
| 1857 | inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); |
| 1858 | if (IS_ERR(inherit)) { |
| 1859 | ret = PTR_ERR(inherit); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1860 | goto free_args; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1861 | } |
| 1862 | } |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1863 | |
Nikolay Borisov | 5d54c67 | 2020-03-13 17:23:19 +0200 | [diff] [blame] | 1864 | ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd, |
| 1865 | subvol, readonly, inherit); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1866 | if (ret) |
| 1867 | goto free_inherit; |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1868 | free_inherit: |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1869 | kfree(inherit); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1870 | free_args: |
| 1871 | kfree(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1872 | return ret; |
| 1873 | } |
| 1874 | |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1875 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
| 1876 | void __user *arg) |
| 1877 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1878 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1879 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1880 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1881 | int ret = 0; |
| 1882 | u64 flags = 0; |
| 1883 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1884 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1885 | return -EINVAL; |
| 1886 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1887 | down_read(&fs_info->subvol_sem); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1888 | if (btrfs_root_readonly(root)) |
| 1889 | flags |= BTRFS_SUBVOL_RDONLY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1890 | up_read(&fs_info->subvol_sem); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1891 | |
| 1892 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 1893 | ret = -EFAULT; |
| 1894 | |
| 1895 | return ret; |
| 1896 | } |
| 1897 | |
| 1898 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, |
| 1899 | void __user *arg) |
| 1900 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1901 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1902 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1903 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1904 | struct btrfs_trans_handle *trans; |
| 1905 | u64 root_flags; |
| 1906 | u64 flags; |
| 1907 | int ret = 0; |
| 1908 | |
David Sterba | bd60ea0 | 2014-01-16 15:50:22 +0100 | [diff] [blame] | 1909 | if (!inode_owner_or_capable(inode)) |
| 1910 | return -EPERM; |
| 1911 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1912 | ret = mnt_want_write_file(file); |
| 1913 | if (ret) |
| 1914 | goto out; |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1915 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1916 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1917 | ret = -EINVAL; |
| 1918 | goto out_drop_write; |
| 1919 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1920 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1921 | if (copy_from_user(&flags, arg, sizeof(flags))) { |
| 1922 | ret = -EFAULT; |
| 1923 | goto out_drop_write; |
| 1924 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1925 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1926 | if (flags & ~BTRFS_SUBVOL_RDONLY) { |
| 1927 | ret = -EOPNOTSUPP; |
| 1928 | goto out_drop_write; |
| 1929 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1930 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1931 | down_write(&fs_info->subvol_sem); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1932 | |
| 1933 | /* nothing to do */ |
| 1934 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1935 | goto out_drop_sem; |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1936 | |
| 1937 | root_flags = btrfs_root_flags(&root->root_item); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 1938 | if (flags & BTRFS_SUBVOL_RDONLY) { |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1939 | btrfs_set_root_flags(&root->root_item, |
| 1940 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 1941 | } else { |
| 1942 | /* |
| 1943 | * Block RO -> RW transition if this subvolume is involved in |
| 1944 | * send |
| 1945 | */ |
| 1946 | spin_lock(&root->root_item_lock); |
| 1947 | if (root->send_in_progress == 0) { |
| 1948 | btrfs_set_root_flags(&root->root_item, |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1949 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 1950 | spin_unlock(&root->root_item_lock); |
| 1951 | } else { |
| 1952 | spin_unlock(&root->root_item_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1953 | btrfs_warn(fs_info, |
| 1954 | "Attempt to set subvolume %llu read-write during send", |
| 1955 | root->root_key.objectid); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 1956 | ret = -EPERM; |
| 1957 | goto out_drop_sem; |
| 1958 | } |
| 1959 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1960 | |
| 1961 | trans = btrfs_start_transaction(root, 1); |
| 1962 | if (IS_ERR(trans)) { |
| 1963 | ret = PTR_ERR(trans); |
| 1964 | goto out_reset; |
| 1965 | } |
| 1966 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1967 | ret = btrfs_update_root(trans, fs_info->tree_root, |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1968 | &root->root_key, &root->root_item); |
Nikolay Borisov | 9417ebc | 2017-09-28 10:53:17 +0300 | [diff] [blame] | 1969 | if (ret < 0) { |
| 1970 | btrfs_end_transaction(trans); |
| 1971 | goto out_reset; |
| 1972 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1973 | |
Nikolay Borisov | 9417ebc | 2017-09-28 10:53:17 +0300 | [diff] [blame] | 1974 | ret = btrfs_commit_transaction(trans); |
| 1975 | |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1976 | out_reset: |
| 1977 | if (ret) |
| 1978 | btrfs_set_root_flags(&root->root_item, root_flags); |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1979 | out_drop_sem: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1980 | up_write(&fs_info->subvol_sem); |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1981 | out_drop_write: |
| 1982 | mnt_drop_write_file(file); |
| 1983 | out: |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1984 | return ret; |
| 1985 | } |
| 1986 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1987 | static noinline int key_in_sk(struct btrfs_key *key, |
| 1988 | struct btrfs_ioctl_search_key *sk) |
| 1989 | { |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 1990 | struct btrfs_key test; |
| 1991 | int ret; |
| 1992 | |
| 1993 | test.objectid = sk->min_objectid; |
| 1994 | test.type = sk->min_type; |
| 1995 | test.offset = sk->min_offset; |
| 1996 | |
| 1997 | ret = btrfs_comp_cpu_keys(key, &test); |
| 1998 | if (ret < 0) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1999 | return 0; |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2000 | |
| 2001 | test.objectid = sk->max_objectid; |
| 2002 | test.type = sk->max_type; |
| 2003 | test.offset = sk->max_offset; |
| 2004 | |
| 2005 | ret = btrfs_comp_cpu_keys(key, &test); |
| 2006 | if (ret > 0) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2007 | return 0; |
| 2008 | return 1; |
| 2009 | } |
| 2010 | |
Jeff Mahoney | df39756 | 2016-06-21 20:18:21 -0400 | [diff] [blame] | 2011 | static noinline int copy_to_sk(struct btrfs_path *path, |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2012 | struct btrfs_key *key, |
| 2013 | struct btrfs_ioctl_search_key *sk, |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2014 | size_t *buf_size, |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2015 | char __user *ubuf, |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2016 | unsigned long *sk_offset, |
| 2017 | int *num_found) |
| 2018 | { |
| 2019 | u64 found_transid; |
| 2020 | struct extent_buffer *leaf; |
| 2021 | struct btrfs_ioctl_search_header sh; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2022 | struct btrfs_key test; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2023 | unsigned long item_off; |
| 2024 | unsigned long item_len; |
| 2025 | int nritems; |
| 2026 | int i; |
| 2027 | int slot; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2028 | int ret = 0; |
| 2029 | |
| 2030 | leaf = path->nodes[0]; |
| 2031 | slot = path->slots[0]; |
| 2032 | nritems = btrfs_header_nritems(leaf); |
| 2033 | |
| 2034 | if (btrfs_header_generation(leaf) > sk->max_transid) { |
| 2035 | i = nritems; |
| 2036 | goto advance_key; |
| 2037 | } |
| 2038 | found_transid = btrfs_header_generation(leaf); |
| 2039 | |
| 2040 | for (i = slot; i < nritems; i++) { |
| 2041 | item_off = btrfs_item_ptr_offset(leaf, i); |
| 2042 | item_len = btrfs_item_size_nr(leaf, i); |
| 2043 | |
Gabriel de Perthuis | 03b71c6 | 2013-05-06 17:40:18 +0000 | [diff] [blame] | 2044 | btrfs_item_key_to_cpu(leaf, key, i); |
| 2045 | if (!key_in_sk(key, sk)) |
| 2046 | continue; |
| 2047 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2048 | if (sizeof(sh) + item_len > *buf_size) { |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2049 | if (*num_found) { |
| 2050 | ret = 1; |
| 2051 | goto out; |
| 2052 | } |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2053 | |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2054 | /* |
| 2055 | * return one empty item back for v1, which does not |
| 2056 | * handle -EOVERFLOW |
| 2057 | */ |
| 2058 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2059 | *buf_size = sizeof(sh) + item_len; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2060 | item_len = 0; |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2061 | ret = -EOVERFLOW; |
| 2062 | } |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2063 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2064 | if (sizeof(sh) + item_len + *sk_offset > *buf_size) { |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2065 | ret = 1; |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2066 | goto out; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2067 | } |
| 2068 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2069 | sh.objectid = key->objectid; |
| 2070 | sh.offset = key->offset; |
| 2071 | sh.type = key->type; |
| 2072 | sh.len = item_len; |
| 2073 | sh.transid = found_transid; |
| 2074 | |
| 2075 | /* copy search result header */ |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2076 | if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { |
| 2077 | ret = -EFAULT; |
| 2078 | goto out; |
| 2079 | } |
| 2080 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2081 | *sk_offset += sizeof(sh); |
| 2082 | |
| 2083 | if (item_len) { |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2084 | char __user *up = ubuf + *sk_offset; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2085 | /* copy the item */ |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2086 | if (read_extent_buffer_to_user(leaf, up, |
| 2087 | item_off, item_len)) { |
| 2088 | ret = -EFAULT; |
| 2089 | goto out; |
| 2090 | } |
| 2091 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2092 | *sk_offset += item_len; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2093 | } |
Hugo Mills | e215686 | 2011-05-14 17:43:41 +0000 | [diff] [blame] | 2094 | (*num_found)++; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2095 | |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2096 | if (ret) /* -EOVERFLOW from above */ |
| 2097 | goto out; |
| 2098 | |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2099 | if (*num_found >= sk->nr_items) { |
| 2100 | ret = 1; |
| 2101 | goto out; |
| 2102 | } |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2103 | } |
| 2104 | advance_key: |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2105 | ret = 0; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2106 | test.objectid = sk->max_objectid; |
| 2107 | test.type = sk->max_type; |
| 2108 | test.offset = sk->max_offset; |
| 2109 | if (btrfs_comp_cpu_keys(key, &test) >= 0) |
| 2110 | ret = 1; |
| 2111 | else if (key->offset < (u64)-1) |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2112 | key->offset++; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2113 | else if (key->type < (u8)-1) { |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2114 | key->offset = 0; |
| 2115 | key->type++; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2116 | } else if (key->objectid < (u64)-1) { |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2117 | key->offset = 0; |
| 2118 | key->type = 0; |
| 2119 | key->objectid++; |
| 2120 | } else |
| 2121 | ret = 1; |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2122 | out: |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2123 | /* |
| 2124 | * 0: all items from this leaf copied, continue with next |
| 2125 | * 1: * more items can be copied, but unused buffer is too small |
| 2126 | * * all items were found |
| 2127 | * Either way, it will stops the loop which iterates to the next |
| 2128 | * leaf |
| 2129 | * -EOVERFLOW: item was to large for buffer |
| 2130 | * -EFAULT: could not copy extent buffer back to userspace |
| 2131 | */ |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2132 | return ret; |
| 2133 | } |
| 2134 | |
| 2135 | static noinline int search_ioctl(struct inode *inode, |
Gerhard Heift | 1254444 | 2014-01-30 16:23:58 +0100 | [diff] [blame] | 2136 | struct btrfs_ioctl_search_key *sk, |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2137 | size_t *buf_size, |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2138 | char __user *ubuf) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2139 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2140 | struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2141 | struct btrfs_root *root; |
| 2142 | struct btrfs_key key; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2143 | struct btrfs_path *path; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2144 | int ret; |
| 2145 | int num_found = 0; |
| 2146 | unsigned long sk_offset = 0; |
| 2147 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2148 | if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { |
| 2149 | *buf_size = sizeof(struct btrfs_ioctl_search_header); |
Gerhard Heift | 1254444 | 2014-01-30 16:23:58 +0100 | [diff] [blame] | 2150 | return -EOVERFLOW; |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2151 | } |
Gerhard Heift | 1254444 | 2014-01-30 16:23:58 +0100 | [diff] [blame] | 2152 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2153 | path = btrfs_alloc_path(); |
| 2154 | if (!path) |
| 2155 | return -ENOMEM; |
| 2156 | |
| 2157 | if (sk->tree_id == 0) { |
| 2158 | /* search the root of the inode that was passed */ |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2159 | root = btrfs_grab_root(BTRFS_I(inode)->root); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2160 | } else { |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 2161 | root = btrfs_get_fs_root(info, sk->tree_id, true); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2162 | if (IS_ERR(root)) { |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2163 | btrfs_free_path(path); |
Misono Tomohiro | ad1e3d5 | 2018-05-21 13:57:27 +0900 | [diff] [blame] | 2164 | return PTR_ERR(root); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | key.objectid = sk->min_objectid; |
| 2169 | key.type = sk->min_type; |
| 2170 | key.offset = sk->min_offset; |
| 2171 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2172 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 2173 | ret = btrfs_search_forward(root, &key, path, sk->min_transid); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2174 | if (ret != 0) { |
| 2175 | if (ret > 0) |
| 2176 | ret = 0; |
| 2177 | goto err; |
| 2178 | } |
Jeff Mahoney | df39756 | 2016-06-21 20:18:21 -0400 | [diff] [blame] | 2179 | ret = copy_to_sk(path, &key, sk, buf_size, ubuf, |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2180 | &sk_offset, &num_found); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2181 | btrfs_release_path(path); |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2182 | if (ret) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2183 | break; |
| 2184 | |
| 2185 | } |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2186 | if (ret > 0) |
| 2187 | ret = 0; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2188 | err: |
| 2189 | sk->nr_items = num_found; |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2190 | btrfs_put_root(root); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2191 | btrfs_free_path(path); |
| 2192 | return ret; |
| 2193 | } |
| 2194 | |
| 2195 | static noinline int btrfs_ioctl_tree_search(struct file *file, |
| 2196 | void __user *argp) |
| 2197 | { |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2198 | struct btrfs_ioctl_search_args __user *uargs; |
| 2199 | struct btrfs_ioctl_search_key sk; |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2200 | struct inode *inode; |
| 2201 | int ret; |
| 2202 | size_t buf_size; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2203 | |
| 2204 | if (!capable(CAP_SYS_ADMIN)) |
| 2205 | return -EPERM; |
| 2206 | |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2207 | uargs = (struct btrfs_ioctl_search_args __user *)argp; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2208 | |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2209 | if (copy_from_user(&sk, &uargs->key, sizeof(sk))) |
| 2210 | return -EFAULT; |
| 2211 | |
| 2212 | buf_size = sizeof(uargs->buf); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2213 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2214 | inode = file_inode(file); |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2215 | ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2216 | |
| 2217 | /* |
| 2218 | * In the origin implementation an overflow is handled by returning a |
| 2219 | * search header with a len of zero, so reset ret. |
| 2220 | */ |
| 2221 | if (ret == -EOVERFLOW) |
| 2222 | ret = 0; |
| 2223 | |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2224 | if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2225 | ret = -EFAULT; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2226 | return ret; |
| 2227 | } |
| 2228 | |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2229 | static noinline int btrfs_ioctl_tree_search_v2(struct file *file, |
| 2230 | void __user *argp) |
| 2231 | { |
| 2232 | struct btrfs_ioctl_search_args_v2 __user *uarg; |
| 2233 | struct btrfs_ioctl_search_args_v2 args; |
| 2234 | struct inode *inode; |
| 2235 | int ret; |
| 2236 | size_t buf_size; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 2237 | const size_t buf_limit = SZ_16M; |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2238 | |
| 2239 | if (!capable(CAP_SYS_ADMIN)) |
| 2240 | return -EPERM; |
| 2241 | |
| 2242 | /* copy search header and buffer size */ |
| 2243 | uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; |
| 2244 | if (copy_from_user(&args, uarg, sizeof(args))) |
| 2245 | return -EFAULT; |
| 2246 | |
| 2247 | buf_size = args.buf_size; |
| 2248 | |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2249 | /* limit result size to 16MB */ |
| 2250 | if (buf_size > buf_limit) |
| 2251 | buf_size = buf_limit; |
| 2252 | |
| 2253 | inode = file_inode(file); |
| 2254 | ret = search_ioctl(inode, &args.key, &buf_size, |
Omar Sandoval | 718dc5f | 2017-08-22 23:46:05 -0700 | [diff] [blame] | 2255 | (char __user *)(&uarg->buf[0])); |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2256 | if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) |
| 2257 | ret = -EFAULT; |
| 2258 | else if (ret == -EOVERFLOW && |
| 2259 | copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) |
| 2260 | ret = -EFAULT; |
| 2261 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2262 | return ret; |
| 2263 | } |
| 2264 | |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2265 | /* |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2266 | * Search INODE_REFs to identify path name of 'dirid' directory |
| 2267 | * in a 'tree_id' tree. and sets path name to 'name'. |
| 2268 | */ |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2269 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
| 2270 | u64 tree_id, u64 dirid, char *name) |
| 2271 | { |
| 2272 | struct btrfs_root *root; |
| 2273 | struct btrfs_key key; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2274 | char *ptr; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2275 | int ret = -1; |
| 2276 | int slot; |
| 2277 | int len; |
| 2278 | int total_len = 0; |
| 2279 | struct btrfs_inode_ref *iref; |
| 2280 | struct extent_buffer *l; |
| 2281 | struct btrfs_path *path; |
| 2282 | |
| 2283 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { |
| 2284 | name[0]='\0'; |
| 2285 | return 0; |
| 2286 | } |
| 2287 | |
| 2288 | path = btrfs_alloc_path(); |
| 2289 | if (!path) |
| 2290 | return -ENOMEM; |
| 2291 | |
Nikolay Borisov | c8bcbfbd | 2017-12-01 11:19:42 +0200 | [diff] [blame] | 2292 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2293 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 2294 | root = btrfs_get_fs_root(info, tree_id, true); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2295 | if (IS_ERR(root)) { |
Misono Tomohiro | ad1e3d5 | 2018-05-21 13:57:27 +0900 | [diff] [blame] | 2296 | ret = PTR_ERR(root); |
Josef Bacik | 8823401 | 2020-01-24 09:32:34 -0500 | [diff] [blame] | 2297 | root = NULL; |
| 2298 | goto out; |
| 2299 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2300 | |
| 2301 | key.objectid = dirid; |
| 2302 | key.type = BTRFS_INODE_REF_KEY; |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 2303 | key.offset = (u64)-1; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2304 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2305 | while (1) { |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2306 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2307 | if (ret < 0) |
| 2308 | goto out; |
Filipe David Borba Manana | 18674c6 | 2013-08-14 03:00:21 +0100 | [diff] [blame] | 2309 | else if (ret > 0) { |
| 2310 | ret = btrfs_previous_item(root, path, dirid, |
| 2311 | BTRFS_INODE_REF_KEY); |
| 2312 | if (ret < 0) |
| 2313 | goto out; |
| 2314 | else if (ret > 0) { |
| 2315 | ret = -ENOENT; |
| 2316 | goto out; |
| 2317 | } |
| 2318 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2319 | |
| 2320 | l = path->nodes[0]; |
| 2321 | slot = path->slots[0]; |
| 2322 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2323 | |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2324 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); |
| 2325 | len = btrfs_inode_ref_name_len(l, iref); |
| 2326 | ptr -= len + 1; |
| 2327 | total_len += len + 1; |
Filipe David Borba Manana | a696cf3 | 2013-08-14 03:00:20 +0100 | [diff] [blame] | 2328 | if (ptr < name) { |
| 2329 | ret = -ENAMETOOLONG; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2330 | goto out; |
Filipe David Borba Manana | a696cf3 | 2013-08-14 03:00:20 +0100 | [diff] [blame] | 2331 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2332 | |
| 2333 | *(ptr + len) = '/'; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2334 | read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2335 | |
| 2336 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) |
| 2337 | break; |
| 2338 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2339 | btrfs_release_path(path); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2340 | key.objectid = key.offset; |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 2341 | key.offset = (u64)-1; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2342 | dirid = key.objectid; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2343 | } |
Li Zefan | 77906a50 | 2011-07-14 03:16:00 +0000 | [diff] [blame] | 2344 | memmove(name, ptr, total_len); |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2345 | name[total_len] = '\0'; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2346 | ret = 0; |
| 2347 | out: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2348 | btrfs_put_root(root); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2349 | btrfs_free_path(path); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2350 | return ret; |
| 2351 | } |
| 2352 | |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2353 | static int btrfs_search_path_in_tree_user(struct inode *inode, |
| 2354 | struct btrfs_ioctl_ino_lookup_user_args *args) |
| 2355 | { |
| 2356 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2357 | struct super_block *sb = inode->i_sb; |
| 2358 | struct btrfs_key upper_limit = BTRFS_I(inode)->location; |
| 2359 | u64 treeid = BTRFS_I(inode)->root->root_key.objectid; |
| 2360 | u64 dirid = args->dirid; |
| 2361 | unsigned long item_off; |
| 2362 | unsigned long item_len; |
| 2363 | struct btrfs_inode_ref *iref; |
| 2364 | struct btrfs_root_ref *rref; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2365 | struct btrfs_root *root = NULL; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2366 | struct btrfs_path *path; |
| 2367 | struct btrfs_key key, key2; |
| 2368 | struct extent_buffer *leaf; |
| 2369 | struct inode *temp_inode; |
| 2370 | char *ptr; |
| 2371 | int slot; |
| 2372 | int len; |
| 2373 | int total_len = 0; |
| 2374 | int ret; |
| 2375 | |
| 2376 | path = btrfs_alloc_path(); |
| 2377 | if (!path) |
| 2378 | return -ENOMEM; |
| 2379 | |
| 2380 | /* |
| 2381 | * If the bottom subvolume does not exist directly under upper_limit, |
| 2382 | * construct the path in from the bottom up. |
| 2383 | */ |
| 2384 | if (dirid != upper_limit.objectid) { |
| 2385 | ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1]; |
| 2386 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 2387 | root = btrfs_get_fs_root(fs_info, treeid, true); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2388 | if (IS_ERR(root)) { |
| 2389 | ret = PTR_ERR(root); |
| 2390 | goto out; |
| 2391 | } |
| 2392 | |
| 2393 | key.objectid = dirid; |
| 2394 | key.type = BTRFS_INODE_REF_KEY; |
| 2395 | key.offset = (u64)-1; |
| 2396 | while (1) { |
| 2397 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2398 | if (ret < 0) { |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2399 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2400 | } else if (ret > 0) { |
| 2401 | ret = btrfs_previous_item(root, path, dirid, |
| 2402 | BTRFS_INODE_REF_KEY); |
| 2403 | if (ret < 0) { |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2404 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2405 | } else if (ret > 0) { |
| 2406 | ret = -ENOENT; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2407 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | leaf = path->nodes[0]; |
| 2412 | slot = path->slots[0]; |
| 2413 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2414 | |
| 2415 | iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref); |
| 2416 | len = btrfs_inode_ref_name_len(leaf, iref); |
| 2417 | ptr -= len + 1; |
| 2418 | total_len += len + 1; |
| 2419 | if (ptr < args->path) { |
| 2420 | ret = -ENAMETOOLONG; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2421 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | *(ptr + len) = '/'; |
| 2425 | read_extent_buffer(leaf, ptr, |
| 2426 | (unsigned long)(iref + 1), len); |
| 2427 | |
| 2428 | /* Check the read+exec permission of this directory */ |
| 2429 | ret = btrfs_previous_item(root, path, dirid, |
| 2430 | BTRFS_INODE_ITEM_KEY); |
| 2431 | if (ret < 0) { |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2432 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2433 | } else if (ret > 0) { |
| 2434 | ret = -ENOENT; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2435 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | leaf = path->nodes[0]; |
| 2439 | slot = path->slots[0]; |
| 2440 | btrfs_item_key_to_cpu(leaf, &key2, slot); |
| 2441 | if (key2.objectid != dirid) { |
| 2442 | ret = -ENOENT; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2443 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2444 | } |
| 2445 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 2446 | temp_inode = btrfs_iget(sb, key2.objectid, root); |
Misono Tomohiro | 3ca57bd | 2018-06-04 16:41:07 +0900 | [diff] [blame] | 2447 | if (IS_ERR(temp_inode)) { |
| 2448 | ret = PTR_ERR(temp_inode); |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2449 | goto out_put; |
Misono Tomohiro | 3ca57bd | 2018-06-04 16:41:07 +0900 | [diff] [blame] | 2450 | } |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2451 | ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC); |
| 2452 | iput(temp_inode); |
| 2453 | if (ret) { |
| 2454 | ret = -EACCES; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2455 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | if (key.offset == upper_limit.objectid) |
| 2459 | break; |
| 2460 | if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) { |
| 2461 | ret = -EACCES; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2462 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | btrfs_release_path(path); |
| 2466 | key.objectid = key.offset; |
| 2467 | key.offset = (u64)-1; |
| 2468 | dirid = key.objectid; |
| 2469 | } |
| 2470 | |
| 2471 | memmove(args->path, ptr, total_len); |
| 2472 | args->path[total_len] = '\0'; |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2473 | btrfs_put_root(root); |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2474 | root = NULL; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2475 | btrfs_release_path(path); |
| 2476 | } |
| 2477 | |
| 2478 | /* Get the bottom subvolume's name from ROOT_REF */ |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2479 | key.objectid = treeid; |
| 2480 | key.type = BTRFS_ROOT_REF_KEY; |
| 2481 | key.offset = args->treeid; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2482 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2483 | if (ret < 0) { |
| 2484 | goto out; |
| 2485 | } else if (ret > 0) { |
| 2486 | ret = -ENOENT; |
| 2487 | goto out; |
| 2488 | } |
| 2489 | |
| 2490 | leaf = path->nodes[0]; |
| 2491 | slot = path->slots[0]; |
| 2492 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2493 | |
| 2494 | item_off = btrfs_item_ptr_offset(leaf, slot); |
| 2495 | item_len = btrfs_item_size_nr(leaf, slot); |
| 2496 | /* Check if dirid in ROOT_REF corresponds to passed dirid */ |
| 2497 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); |
| 2498 | if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) { |
| 2499 | ret = -EINVAL; |
| 2500 | goto out; |
| 2501 | } |
| 2502 | |
| 2503 | /* Copy subvolume's name */ |
| 2504 | item_off += sizeof(struct btrfs_root_ref); |
| 2505 | item_len -= sizeof(struct btrfs_root_ref); |
| 2506 | read_extent_buffer(leaf, args->name, item_off, item_len); |
| 2507 | args->name[item_len] = 0; |
| 2508 | |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2509 | out_put: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2510 | btrfs_put_root(root); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2511 | out: |
| 2512 | btrfs_free_path(path); |
| 2513 | return ret; |
| 2514 | } |
| 2515 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2516 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, |
| 2517 | void __user *argp) |
| 2518 | { |
Bart Van Assche | bece2e8 | 2018-06-20 10:03:31 -0700 | [diff] [blame] | 2519 | struct btrfs_ioctl_ino_lookup_args *args; |
| 2520 | struct inode *inode; |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2521 | int ret = 0; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2522 | |
Julia Lawall | 2354d08f | 2010-10-29 15:14:18 -0400 | [diff] [blame] | 2523 | args = memdup_user(argp, sizeof(*args)); |
| 2524 | if (IS_ERR(args)) |
| 2525 | return PTR_ERR(args); |
Dan Carpenter | c2b9692 | 2010-03-20 11:24:15 +0000 | [diff] [blame] | 2526 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2527 | inode = file_inode(file); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2528 | |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2529 | /* |
| 2530 | * Unprivileged query to obtain the containing subvolume root id. The |
| 2531 | * path is reset so it's consistent with btrfs_search_path_in_tree. |
| 2532 | */ |
Chris Mason | 1b53ac4 | 2010-03-18 12:17:05 -0400 | [diff] [blame] | 2533 | if (args->treeid == 0) |
| 2534 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; |
| 2535 | |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2536 | if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { |
| 2537 | args->name[0] = 0; |
| 2538 | goto out; |
| 2539 | } |
| 2540 | |
| 2541 | if (!capable(CAP_SYS_ADMIN)) { |
| 2542 | ret = -EPERM; |
| 2543 | goto out; |
| 2544 | } |
| 2545 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2546 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
| 2547 | args->treeid, args->objectid, |
| 2548 | args->name); |
| 2549 | |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2550 | out: |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2551 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 2552 | ret = -EFAULT; |
| 2553 | |
| 2554 | kfree(args); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2555 | return ret; |
| 2556 | } |
| 2557 | |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2558 | /* |
| 2559 | * Version of ino_lookup ioctl (unprivileged) |
| 2560 | * |
| 2561 | * The main differences from ino_lookup ioctl are: |
| 2562 | * |
| 2563 | * 1. Read + Exec permission will be checked using inode_permission() during |
| 2564 | * path construction. -EACCES will be returned in case of failure. |
| 2565 | * 2. Path construction will be stopped at the inode number which corresponds |
| 2566 | * to the fd with which this ioctl is called. If constructed path does not |
| 2567 | * exist under fd's inode, -EACCES will be returned. |
| 2568 | * 3. The name of bottom subvolume is also searched and filled. |
| 2569 | */ |
| 2570 | static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) |
| 2571 | { |
| 2572 | struct btrfs_ioctl_ino_lookup_user_args *args; |
| 2573 | struct inode *inode; |
| 2574 | int ret; |
| 2575 | |
| 2576 | args = memdup_user(argp, sizeof(*args)); |
| 2577 | if (IS_ERR(args)) |
| 2578 | return PTR_ERR(args); |
| 2579 | |
| 2580 | inode = file_inode(file); |
| 2581 | |
| 2582 | if (args->dirid == BTRFS_FIRST_FREE_OBJECTID && |
| 2583 | BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) { |
| 2584 | /* |
| 2585 | * The subvolume does not exist under fd with which this is |
| 2586 | * called |
| 2587 | */ |
| 2588 | kfree(args); |
| 2589 | return -EACCES; |
| 2590 | } |
| 2591 | |
| 2592 | ret = btrfs_search_path_in_tree_user(inode, args); |
| 2593 | |
| 2594 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 2595 | ret = -EFAULT; |
| 2596 | |
| 2597 | kfree(args); |
| 2598 | return ret; |
| 2599 | } |
| 2600 | |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2601 | /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ |
| 2602 | static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) |
| 2603 | { |
| 2604 | struct btrfs_ioctl_get_subvol_info_args *subvol_info; |
| 2605 | struct btrfs_fs_info *fs_info; |
| 2606 | struct btrfs_root *root; |
| 2607 | struct btrfs_path *path; |
| 2608 | struct btrfs_key key; |
| 2609 | struct btrfs_root_item *root_item; |
| 2610 | struct btrfs_root_ref *rref; |
| 2611 | struct extent_buffer *leaf; |
| 2612 | unsigned long item_off; |
| 2613 | unsigned long item_len; |
| 2614 | struct inode *inode; |
| 2615 | int slot; |
| 2616 | int ret = 0; |
| 2617 | |
| 2618 | path = btrfs_alloc_path(); |
| 2619 | if (!path) |
| 2620 | return -ENOMEM; |
| 2621 | |
| 2622 | subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL); |
| 2623 | if (!subvol_info) { |
| 2624 | btrfs_free_path(path); |
| 2625 | return -ENOMEM; |
| 2626 | } |
| 2627 | |
| 2628 | inode = file_inode(file); |
| 2629 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2630 | |
| 2631 | /* Get root_item of inode's subvolume */ |
| 2632 | key.objectid = BTRFS_I(inode)->root->root_key.objectid; |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 2633 | root = btrfs_get_fs_root(fs_info, key.objectid, true); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2634 | if (IS_ERR(root)) { |
| 2635 | ret = PTR_ERR(root); |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2636 | goto out_free; |
| 2637 | } |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2638 | root_item = &root->root_item; |
| 2639 | |
| 2640 | subvol_info->treeid = key.objectid; |
| 2641 | |
| 2642 | subvol_info->generation = btrfs_root_generation(root_item); |
| 2643 | subvol_info->flags = btrfs_root_flags(root_item); |
| 2644 | |
| 2645 | memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE); |
| 2646 | memcpy(subvol_info->parent_uuid, root_item->parent_uuid, |
| 2647 | BTRFS_UUID_SIZE); |
| 2648 | memcpy(subvol_info->received_uuid, root_item->received_uuid, |
| 2649 | BTRFS_UUID_SIZE); |
| 2650 | |
| 2651 | subvol_info->ctransid = btrfs_root_ctransid(root_item); |
| 2652 | subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime); |
| 2653 | subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime); |
| 2654 | |
| 2655 | subvol_info->otransid = btrfs_root_otransid(root_item); |
| 2656 | subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime); |
| 2657 | subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime); |
| 2658 | |
| 2659 | subvol_info->stransid = btrfs_root_stransid(root_item); |
| 2660 | subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime); |
| 2661 | subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime); |
| 2662 | |
| 2663 | subvol_info->rtransid = btrfs_root_rtransid(root_item); |
| 2664 | subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime); |
| 2665 | subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime); |
| 2666 | |
| 2667 | if (key.objectid != BTRFS_FS_TREE_OBJECTID) { |
| 2668 | /* Search root tree for ROOT_BACKREF of this subvolume */ |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2669 | key.type = BTRFS_ROOT_BACKREF_KEY; |
| 2670 | key.offset = 0; |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2671 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2672 | if (ret < 0) { |
| 2673 | goto out; |
| 2674 | } else if (path->slots[0] >= |
| 2675 | btrfs_header_nritems(path->nodes[0])) { |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2676 | ret = btrfs_next_leaf(fs_info->tree_root, path); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2677 | if (ret < 0) { |
| 2678 | goto out; |
| 2679 | } else if (ret > 0) { |
| 2680 | ret = -EUCLEAN; |
| 2681 | goto out; |
| 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | leaf = path->nodes[0]; |
| 2686 | slot = path->slots[0]; |
| 2687 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2688 | if (key.objectid == subvol_info->treeid && |
| 2689 | key.type == BTRFS_ROOT_BACKREF_KEY) { |
| 2690 | subvol_info->parent_id = key.offset; |
| 2691 | |
| 2692 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); |
| 2693 | subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref); |
| 2694 | |
| 2695 | item_off = btrfs_item_ptr_offset(leaf, slot) |
| 2696 | + sizeof(struct btrfs_root_ref); |
| 2697 | item_len = btrfs_item_size_nr(leaf, slot) |
| 2698 | - sizeof(struct btrfs_root_ref); |
| 2699 | read_extent_buffer(leaf, subvol_info->name, |
| 2700 | item_off, item_len); |
| 2701 | } else { |
| 2702 | ret = -ENOENT; |
| 2703 | goto out; |
| 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | if (copy_to_user(argp, subvol_info, sizeof(*subvol_info))) |
| 2708 | ret = -EFAULT; |
| 2709 | |
| 2710 | out: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2711 | btrfs_put_root(root); |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2712 | out_free: |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2713 | btrfs_free_path(path); |
Waiman Long | b091f7f | 2020-06-16 11:31:59 -0400 | [diff] [blame] | 2714 | kfree(subvol_info); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2715 | return ret; |
| 2716 | } |
| 2717 | |
Tomohiro Misono | 42e4b52 | 2018-05-21 10:09:43 +0900 | [diff] [blame] | 2718 | /* |
| 2719 | * Return ROOT_REF information of the subvolume containing this inode |
| 2720 | * except the subvolume name. |
| 2721 | */ |
| 2722 | static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp) |
| 2723 | { |
| 2724 | struct btrfs_ioctl_get_subvol_rootref_args *rootrefs; |
| 2725 | struct btrfs_root_ref *rref; |
| 2726 | struct btrfs_root *root; |
| 2727 | struct btrfs_path *path; |
| 2728 | struct btrfs_key key; |
| 2729 | struct extent_buffer *leaf; |
| 2730 | struct inode *inode; |
| 2731 | u64 objectid; |
| 2732 | int slot; |
| 2733 | int ret; |
| 2734 | u8 found; |
| 2735 | |
| 2736 | path = btrfs_alloc_path(); |
| 2737 | if (!path) |
| 2738 | return -ENOMEM; |
| 2739 | |
| 2740 | rootrefs = memdup_user(argp, sizeof(*rootrefs)); |
| 2741 | if (IS_ERR(rootrefs)) { |
| 2742 | btrfs_free_path(path); |
| 2743 | return PTR_ERR(rootrefs); |
| 2744 | } |
| 2745 | |
| 2746 | inode = file_inode(file); |
| 2747 | root = BTRFS_I(inode)->root->fs_info->tree_root; |
| 2748 | objectid = BTRFS_I(inode)->root->root_key.objectid; |
| 2749 | |
| 2750 | key.objectid = objectid; |
| 2751 | key.type = BTRFS_ROOT_REF_KEY; |
| 2752 | key.offset = rootrefs->min_treeid; |
| 2753 | found = 0; |
| 2754 | |
| 2755 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2756 | if (ret < 0) { |
| 2757 | goto out; |
| 2758 | } else if (path->slots[0] >= |
| 2759 | btrfs_header_nritems(path->nodes[0])) { |
| 2760 | ret = btrfs_next_leaf(root, path); |
| 2761 | if (ret < 0) { |
| 2762 | goto out; |
| 2763 | } else if (ret > 0) { |
| 2764 | ret = -EUCLEAN; |
| 2765 | goto out; |
| 2766 | } |
| 2767 | } |
| 2768 | while (1) { |
| 2769 | leaf = path->nodes[0]; |
| 2770 | slot = path->slots[0]; |
| 2771 | |
| 2772 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2773 | if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) { |
| 2774 | ret = 0; |
| 2775 | goto out; |
| 2776 | } |
| 2777 | |
| 2778 | if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { |
| 2779 | ret = -EOVERFLOW; |
| 2780 | goto out; |
| 2781 | } |
| 2782 | |
| 2783 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); |
| 2784 | rootrefs->rootref[found].treeid = key.offset; |
| 2785 | rootrefs->rootref[found].dirid = |
| 2786 | btrfs_root_ref_dirid(leaf, rref); |
| 2787 | found++; |
| 2788 | |
| 2789 | ret = btrfs_next_item(root, path); |
| 2790 | if (ret < 0) { |
| 2791 | goto out; |
| 2792 | } else if (ret > 0) { |
| 2793 | ret = -EUCLEAN; |
| 2794 | goto out; |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | out: |
| 2799 | if (!ret || ret == -EOVERFLOW) { |
| 2800 | rootrefs->num_items = found; |
| 2801 | /* update min_treeid for next search */ |
| 2802 | if (found) |
| 2803 | rootrefs->min_treeid = |
| 2804 | rootrefs->rootref[found - 1].treeid + 1; |
| 2805 | if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) |
| 2806 | ret = -EFAULT; |
| 2807 | } |
| 2808 | |
| 2809 | kfree(rootrefs); |
| 2810 | btrfs_free_path(path); |
| 2811 | |
| 2812 | return ret; |
| 2813 | } |
| 2814 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2815 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2816 | void __user *arg, |
| 2817 | bool destroy_v2) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2818 | { |
Al Viro | 54563d4 | 2013-09-01 15:57:51 -0400 | [diff] [blame] | 2819 | struct dentry *parent = file->f_path.dentry; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2820 | struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2821 | struct dentry *dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2822 | struct inode *dir = d_inode(parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2823 | struct inode *inode; |
| 2824 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2825 | struct btrfs_root *dest = NULL; |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2826 | struct btrfs_ioctl_vol_args *vol_args = NULL; |
| 2827 | struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL; |
| 2828 | char *subvol_name, *subvol_name_ptr = NULL; |
| 2829 | int subvol_namelen; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2830 | int err = 0; |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2831 | bool destroy_parent = false; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2832 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2833 | if (destroy_v2) { |
| 2834 | vol_args2 = memdup_user(arg, sizeof(*vol_args2)); |
| 2835 | if (IS_ERR(vol_args2)) |
| 2836 | return PTR_ERR(vol_args2); |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 2837 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2838 | if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) { |
| 2839 | err = -EOPNOTSUPP; |
| 2840 | goto out; |
| 2841 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2842 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2843 | /* |
| 2844 | * If SPEC_BY_ID is not set, we are looking for the subvolume by |
| 2845 | * name, same as v1 currently does. |
| 2846 | */ |
| 2847 | if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) { |
| 2848 | vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0; |
| 2849 | subvol_name = vol_args2->name; |
| 2850 | |
| 2851 | err = mnt_want_write_file(file); |
| 2852 | if (err) |
| 2853 | goto out; |
| 2854 | } else { |
| 2855 | if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) { |
| 2856 | err = -EINVAL; |
| 2857 | goto out; |
| 2858 | } |
| 2859 | |
| 2860 | err = mnt_want_write_file(file); |
| 2861 | if (err) |
| 2862 | goto out; |
| 2863 | |
| 2864 | dentry = btrfs_get_dentry(fs_info->sb, |
| 2865 | BTRFS_FIRST_FREE_OBJECTID, |
| 2866 | vol_args2->subvolid, 0, 0); |
| 2867 | if (IS_ERR(dentry)) { |
| 2868 | err = PTR_ERR(dentry); |
| 2869 | goto out_drop_write; |
| 2870 | } |
| 2871 | |
| 2872 | /* |
| 2873 | * Change the default parent since the subvolume being |
| 2874 | * deleted can be outside of the current mount point. |
| 2875 | */ |
| 2876 | parent = btrfs_get_parent(dentry); |
| 2877 | |
| 2878 | /* |
| 2879 | * At this point dentry->d_name can point to '/' if the |
| 2880 | * subvolume we want to destroy is outsite of the |
| 2881 | * current mount point, so we need to release the |
| 2882 | * current dentry and execute the lookup to return a new |
| 2883 | * one with ->d_name pointing to the |
| 2884 | * <mount point>/subvol_name. |
| 2885 | */ |
| 2886 | dput(dentry); |
| 2887 | if (IS_ERR(parent)) { |
| 2888 | err = PTR_ERR(parent); |
| 2889 | goto out_drop_write; |
| 2890 | } |
| 2891 | dir = d_inode(parent); |
| 2892 | |
| 2893 | /* |
| 2894 | * If v2 was used with SPEC_BY_ID, a new parent was |
| 2895 | * allocated since the subvolume can be outside of the |
| 2896 | * current mount point. Later on we need to release this |
| 2897 | * new parent dentry. |
| 2898 | */ |
| 2899 | destroy_parent = true; |
| 2900 | |
| 2901 | subvol_name_ptr = btrfs_get_subvol_name_from_objectid( |
| 2902 | fs_info, vol_args2->subvolid); |
| 2903 | if (IS_ERR(subvol_name_ptr)) { |
| 2904 | err = PTR_ERR(subvol_name_ptr); |
| 2905 | goto free_parent; |
| 2906 | } |
| 2907 | /* subvol_name_ptr is already NULL termined */ |
| 2908 | subvol_name = (char *)kbasename(subvol_name_ptr); |
| 2909 | } |
| 2910 | } else { |
| 2911 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 2912 | if (IS_ERR(vol_args)) |
| 2913 | return PTR_ERR(vol_args); |
| 2914 | |
| 2915 | vol_args->name[BTRFS_PATH_NAME_MAX] = 0; |
| 2916 | subvol_name = vol_args->name; |
| 2917 | |
| 2918 | err = mnt_want_write_file(file); |
| 2919 | if (err) |
| 2920 | goto out; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2921 | } |
| 2922 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2923 | subvol_namelen = strlen(subvol_name); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2924 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2925 | if (strchr(subvol_name, '/') || |
| 2926 | strncmp(subvol_name, "..", subvol_namelen) == 0) { |
| 2927 | err = -EINVAL; |
| 2928 | goto free_subvol_name; |
| 2929 | } |
| 2930 | |
| 2931 | if (!S_ISDIR(dir->i_mode)) { |
| 2932 | err = -ENOTDIR; |
| 2933 | goto free_subvol_name; |
| 2934 | } |
David Sterba | 521e054 | 2014-04-15 16:41:44 +0200 | [diff] [blame] | 2935 | |
Al Viro | 0023541 | 2016-05-26 00:05:12 -0400 | [diff] [blame] | 2936 | err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
| 2937 | if (err == -EINTR) |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2938 | goto free_subvol_name; |
| 2939 | dentry = lookup_one_len(subvol_name, parent, subvol_namelen); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2940 | if (IS_ERR(dentry)) { |
| 2941 | err = PTR_ERR(dentry); |
| 2942 | goto out_unlock_dir; |
| 2943 | } |
| 2944 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2945 | if (d_really_is_negative(dentry)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2946 | err = -ENOENT; |
| 2947 | goto out_dput; |
| 2948 | } |
| 2949 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2950 | inode = d_inode(dentry); |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 2951 | dest = BTRFS_I(inode)->root; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2952 | if (!capable(CAP_SYS_ADMIN)) { |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 2953 | /* |
| 2954 | * Regular user. Only allow this with a special mount |
| 2955 | * option, when the user has write+exec access to the |
| 2956 | * subvol root, and when rmdir(2) would have been |
| 2957 | * allowed. |
| 2958 | * |
| 2959 | * Note that this is _not_ check that the subvol is |
| 2960 | * empty or doesn't contain data that we wouldn't |
| 2961 | * otherwise be able to delete. |
| 2962 | * |
| 2963 | * Users who want to delete empty subvols should try |
| 2964 | * rmdir(2). |
| 2965 | */ |
| 2966 | err = -EPERM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2967 | if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 2968 | goto out_dput; |
| 2969 | |
| 2970 | /* |
| 2971 | * Do not allow deletion if the parent dir is the same |
| 2972 | * as the dir to be deleted. That means the ioctl |
| 2973 | * must be called on the dentry referencing the root |
| 2974 | * of the subvol, not a random directory contained |
| 2975 | * within it. |
| 2976 | */ |
| 2977 | err = -EINVAL; |
| 2978 | if (root == dest) |
| 2979 | goto out_dput; |
| 2980 | |
| 2981 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); |
| 2982 | if (err) |
| 2983 | goto out_dput; |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 2984 | } |
| 2985 | |
Miao Xie | 5c39da5 | 2012-10-22 11:39:53 +0000 | [diff] [blame] | 2986 | /* check if subvolume may be deleted by a user */ |
| 2987 | err = btrfs_may_delete(dir, dentry, 1); |
| 2988 | if (err) |
| 2989 | goto out_dput; |
| 2990 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 2991 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2992 | err = -EINVAL; |
| 2993 | goto out_dput; |
| 2994 | } |
| 2995 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2996 | inode_lock(inode); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 2997 | err = btrfs_delete_subvolume(dir, dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2998 | inode_unlock(inode); |
Amir Goldstein | 46008d9 | 2019-05-26 17:34:03 +0300 | [diff] [blame] | 2999 | if (!err) { |
| 3000 | fsnotify_rmdir(dir, dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3001 | d_delete(dentry); |
Amir Goldstein | 46008d9 | 2019-05-26 17:34:03 +0300 | [diff] [blame] | 3002 | } |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 3003 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3004 | out_dput: |
| 3005 | dput(dentry); |
| 3006 | out_unlock_dir: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3007 | inode_unlock(dir); |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 3008 | free_subvol_name: |
| 3009 | kfree(subvol_name_ptr); |
| 3010 | free_parent: |
| 3011 | if (destroy_parent) |
| 3012 | dput(parent); |
Al Viro | 0023541 | 2016-05-26 00:05:12 -0400 | [diff] [blame] | 3013 | out_drop_write: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 3014 | mnt_drop_write_file(file); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3015 | out: |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 3016 | kfree(vol_args2); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3017 | kfree(vol_args); |
| 3018 | return err; |
| 3019 | } |
| 3020 | |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3021 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3022 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3023 | struct inode *inode = file_inode(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3024 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3025 | struct btrfs_ioctl_defrag_range_args *range; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3026 | int ret; |
| 3027 | |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3028 | ret = mnt_want_write_file(file); |
| 3029 | if (ret) |
| 3030 | return ret; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3031 | |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3032 | if (btrfs_root_readonly(root)) { |
| 3033 | ret = -EROFS; |
| 3034 | goto out; |
Stefan Behrens | 5ac00ad | 2012-11-05 17:54:08 +0100 | [diff] [blame] | 3035 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3036 | |
| 3037 | switch (inode->i_mode & S_IFMT) { |
| 3038 | case S_IFDIR: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3039 | if (!capable(CAP_SYS_ADMIN)) { |
| 3040 | ret = -EPERM; |
| 3041 | goto out; |
| 3042 | } |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 3043 | ret = btrfs_defrag_root(root); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3044 | break; |
| 3045 | case S_IFREG: |
Adam Borowski | 616d374 | 2018-07-18 00:08:59 +0200 | [diff] [blame] | 3046 | /* |
| 3047 | * Note that this does not check the file descriptor for write |
| 3048 | * access. This prevents defragmenting executables that are |
| 3049 | * running and allows defrag on files open in read-only mode. |
| 3050 | */ |
| 3051 | if (!capable(CAP_SYS_ADMIN) && |
| 3052 | inode_permission(inode, MAY_WRITE)) { |
| 3053 | ret = -EPERM; |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3054 | goto out; |
| 3055 | } |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3056 | |
| 3057 | range = kzalloc(sizeof(*range), GFP_KERNEL); |
| 3058 | if (!range) { |
| 3059 | ret = -ENOMEM; |
| 3060 | goto out; |
| 3061 | } |
| 3062 | |
| 3063 | if (argp) { |
| 3064 | if (copy_from_user(range, argp, |
| 3065 | sizeof(*range))) { |
| 3066 | ret = -EFAULT; |
| 3067 | kfree(range); |
Dan Carpenter | 683be16 | 2010-03-20 11:24:48 +0000 | [diff] [blame] | 3068 | goto out; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3069 | } |
| 3070 | /* compression requires us to start the IO */ |
| 3071 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { |
| 3072 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; |
| 3073 | range->extent_thresh = (u32)-1; |
| 3074 | } |
| 3075 | } else { |
| 3076 | /* the rest are all set to zero by kzalloc */ |
| 3077 | range->len = (u64)-1; |
| 3078 | } |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3079 | ret = btrfs_defrag_file(file_inode(file), file, |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 3080 | range, BTRFS_OLDEST_GENERATION, 0); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 3081 | if (ret > 0) |
| 3082 | ret = 0; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3083 | kfree(range); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3084 | break; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3085 | default: |
| 3086 | ret = -EINVAL; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3087 | } |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3088 | out: |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3089 | mnt_drop_write_file(file); |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3090 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3091 | } |
| 3092 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3093 | static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3094 | { |
| 3095 | struct btrfs_ioctl_vol_args *vol_args; |
| 3096 | int ret; |
| 3097 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3098 | if (!capable(CAP_SYS_ADMIN)) |
| 3099 | return -EPERM; |
| 3100 | |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3101 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
Anand Jain | e57138b | 2013-08-21 11:44:48 +0800 | [diff] [blame] | 3102 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3103 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 3104 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3105 | if (IS_ERR(vol_args)) { |
| 3106 | ret = PTR_ERR(vol_args); |
| 3107 | goto out; |
| 3108 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3109 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 3110 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3111 | ret = btrfs_init_new_device(fs_info, vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3112 | |
Anand Jain | 43d2076 | 2014-07-01 00:58:56 +0800 | [diff] [blame] | 3113 | if (!ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3114 | btrfs_info(fs_info, "disk added %s", vol_args->name); |
Anand Jain | 43d2076 | 2014-07-01 00:58:56 +0800 | [diff] [blame] | 3115 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3116 | kfree(vol_args); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3117 | out: |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3118 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3119 | return ret; |
| 3120 | } |
| 3121 | |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3122 | static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3123 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3124 | struct inode *inode = file_inode(file); |
| 3125 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3126 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3127 | int ret; |
| 3128 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3129 | if (!capable(CAP_SYS_ADMIN)) |
| 3130 | return -EPERM; |
| 3131 | |
Miao Xie | da24927 | 2012-11-26 08:44:50 +0000 | [diff] [blame] | 3132 | ret = mnt_want_write_file(file); |
| 3133 | if (ret) |
| 3134 | return ret; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3135 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 3136 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3137 | if (IS_ERR(vol_args)) { |
| 3138 | ret = PTR_ERR(vol_args); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 3139 | goto err_drop; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3140 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3141 | |
David Sterba | 748449cdb | 2020-02-21 13:30:14 +0100 | [diff] [blame] | 3142 | if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) { |
Omar Sandoval | fd4e994 | 2018-05-22 15:44:01 -0700 | [diff] [blame] | 3143 | ret = -EOPNOTSUPP; |
| 3144 | goto out; |
| 3145 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3146 | |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3147 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 3148 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
| 3149 | goto out; |
| 3150 | } |
| 3151 | |
David Sterba | 735654e | 2016-02-15 18:15:21 +0100 | [diff] [blame] | 3152 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3153 | ret = btrfs_rm_device(fs_info, NULL, vol_args->devid); |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3154 | } else { |
| 3155 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3156 | ret = btrfs_rm_device(fs_info, vol_args->name, 0); |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3157 | } |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3158 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 3159 | |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3160 | if (!ret) { |
David Sterba | 735654e | 2016-02-15 18:15:21 +0100 | [diff] [blame] | 3161 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3162 | btrfs_info(fs_info, "device deleted: id %llu", |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3163 | vol_args->devid); |
| 3164 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3165 | btrfs_info(fs_info, "device deleted: %s", |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3166 | vol_args->name); |
| 3167 | } |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 3168 | out: |
| 3169 | kfree(vol_args); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 3170 | err_drop: |
Ilya Dryomov | 4ac20c7 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3171 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3172 | return ret; |
| 3173 | } |
| 3174 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3175 | static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) |
| 3176 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3177 | struct inode *inode = file_inode(file); |
| 3178 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3179 | struct btrfs_ioctl_vol_args *vol_args; |
| 3180 | int ret; |
| 3181 | |
| 3182 | if (!capable(CAP_SYS_ADMIN)) |
| 3183 | return -EPERM; |
| 3184 | |
| 3185 | ret = mnt_want_write_file(file); |
| 3186 | if (ret) |
| 3187 | return ret; |
| 3188 | |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3189 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3190 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
David Sterba | 58d7bbf | 2016-05-04 14:10:47 +0200 | [diff] [blame] | 3191 | goto out_drop_write; |
| 3192 | } |
| 3193 | |
| 3194 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 3195 | if (IS_ERR(vol_args)) { |
| 3196 | ret = PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3197 | goto out; |
| 3198 | } |
| 3199 | |
David Sterba | 58d7bbf | 2016-05-04 14:10:47 +0200 | [diff] [blame] | 3200 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3201 | ret = btrfs_rm_device(fs_info, vol_args->name, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3202 | |
| 3203 | if (!ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3204 | btrfs_info(fs_info, "disk deleted %s", vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3205 | kfree(vol_args); |
David Sterba | 58d7bbf | 2016-05-04 14:10:47 +0200 | [diff] [blame] | 3206 | out: |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3207 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
David Sterba | 58d7bbf | 2016-05-04 14:10:47 +0200 | [diff] [blame] | 3208 | out_drop_write: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3209 | mnt_drop_write_file(file); |
David Sterba | 58d7bbf | 2016-05-04 14:10:47 +0200 | [diff] [blame] | 3210 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3211 | return ret; |
| 3212 | } |
| 3213 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3214 | static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, |
| 3215 | void __user *arg) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3216 | { |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3217 | struct btrfs_ioctl_fs_info_args *fi_args; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3218 | struct btrfs_device *device; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3219 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3220 | int ret = 0; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3221 | |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3222 | fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); |
| 3223 | if (!fi_args) |
| 3224 | return -ENOMEM; |
| 3225 | |
David Sterba | d03262c | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3226 | rcu_read_lock(); |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3227 | fi_args->num_devices = fs_devices->num_devices; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3228 | |
David Sterba | d03262c | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3229 | list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3230 | if (device->devid > fi_args->max_id) |
| 3231 | fi_args->max_id = device->devid; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3232 | } |
David Sterba | d03262c | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3233 | rcu_read_unlock(); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3234 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 3235 | memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid)); |
Omar Sandoval | bea7eaf | 2017-08-22 23:46:00 -0700 | [diff] [blame] | 3236 | fi_args->nodesize = fs_info->nodesize; |
| 3237 | fi_args->sectorsize = fs_info->sectorsize; |
| 3238 | fi_args->clone_alignment = fs_info->sectorsize; |
David Sterba | 80a773f | 2014-05-07 18:17:06 +0200 | [diff] [blame] | 3239 | |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3240 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) |
| 3241 | ret = -EFAULT; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3242 | |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3243 | kfree(fi_args); |
| 3244 | return ret; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3245 | } |
| 3246 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3247 | static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, |
| 3248 | void __user *arg) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3249 | { |
| 3250 | struct btrfs_ioctl_dev_info_args *di_args; |
| 3251 | struct btrfs_device *dev; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3252 | int ret = 0; |
| 3253 | char *s_uuid = NULL; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3254 | |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3255 | di_args = memdup_user(arg, sizeof(*di_args)); |
| 3256 | if (IS_ERR(di_args)) |
| 3257 | return PTR_ERR(di_args); |
| 3258 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 3259 | if (!btrfs_is_empty_uuid(di_args->uuid)) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3260 | s_uuid = di_args->uuid; |
| 3261 | |
David Sterba | c5593ca | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3262 | rcu_read_lock(); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 3263 | dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 3264 | NULL, true); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3265 | |
| 3266 | if (!dev) { |
| 3267 | ret = -ENODEV; |
| 3268 | goto out; |
| 3269 | } |
| 3270 | |
| 3271 | di_args->devid = dev->devid; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3272 | di_args->bytes_used = btrfs_device_get_bytes_used(dev); |
| 3273 | di_args->total_bytes = btrfs_device_get_total_bytes(dev); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3274 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 3275 | if (dev->name) { |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 3276 | strncpy(di_args->path, rcu_str_deref(dev->name), |
| 3277 | sizeof(di_args->path) - 1); |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 3278 | di_args->path[sizeof(di_args->path) - 1] = 0; |
| 3279 | } else { |
Stefan Behrens | 99ba55a | 2012-03-19 16:17:22 +0100 | [diff] [blame] | 3280 | di_args->path[0] = '\0'; |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 3281 | } |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3282 | |
| 3283 | out: |
David Sterba | c5593ca | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3284 | rcu_read_unlock(); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3285 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) |
| 3286 | ret = -EFAULT; |
| 3287 | |
| 3288 | kfree(di_args); |
| 3289 | return ret; |
| 3290 | } |
| 3291 | |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3292 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
| 3293 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3294 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3295 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3296 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3297 | struct btrfs_root *new_root; |
| 3298 | struct btrfs_dir_item *di; |
| 3299 | struct btrfs_trans_handle *trans; |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3300 | struct btrfs_path *path = NULL; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3301 | struct btrfs_disk_key disk_key; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3302 | u64 objectid = 0; |
| 3303 | u64 dir_id; |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3304 | int ret; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3305 | |
| 3306 | if (!capable(CAP_SYS_ADMIN)) |
| 3307 | return -EPERM; |
| 3308 | |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3309 | ret = mnt_want_write_file(file); |
| 3310 | if (ret) |
| 3311 | return ret; |
| 3312 | |
| 3313 | if (copy_from_user(&objectid, argp, sizeof(objectid))) { |
| 3314 | ret = -EFAULT; |
| 3315 | goto out; |
| 3316 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3317 | |
| 3318 | if (!objectid) |
chandan | 1cecf57 | 2013-09-13 19:34:10 +0530 | [diff] [blame] | 3319 | objectid = BTRFS_FS_TREE_OBJECTID; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3320 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 3321 | new_root = btrfs_get_fs_root(fs_info, objectid, true); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3322 | if (IS_ERR(new_root)) { |
| 3323 | ret = PTR_ERR(new_root); |
| 3324 | goto out; |
| 3325 | } |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3326 | if (!is_fstree(new_root->root_key.objectid)) { |
| 3327 | ret = -ENOENT; |
| 3328 | goto out_free; |
| 3329 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3330 | |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3331 | path = btrfs_alloc_path(); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3332 | if (!path) { |
| 3333 | ret = -ENOMEM; |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3334 | goto out_free; |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3335 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3336 | path->leave_spinning = 1; |
| 3337 | |
| 3338 | trans = btrfs_start_transaction(root, 1); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3339 | if (IS_ERR(trans)) { |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3340 | ret = PTR_ERR(trans); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3341 | goto out_free; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3344 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 3345 | di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3346 | dir_id, "default", 7, 1); |
Dan Carpenter | cf1e99a | 2010-05-29 09:47:24 +0000 | [diff] [blame] | 3347 | if (IS_ERR_OR_NULL(di)) { |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3348 | btrfs_release_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3349 | btrfs_end_transaction(trans); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3350 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3351 | "Umm, you don't have the default diritem, this isn't going to work"); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3352 | ret = -ENOENT; |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3353 | goto out_free; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); |
| 3357 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); |
| 3358 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3359 | btrfs_release_path(path); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3360 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3361 | btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3362 | btrfs_end_transaction(trans); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3363 | out_free: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 3364 | btrfs_put_root(new_root); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3365 | btrfs_free_path(path); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3366 | out: |
| 3367 | mnt_drop_write_file(file); |
| 3368 | return ret; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
Su Yue | c065f5b1 | 2018-04-02 17:24:11 +0800 | [diff] [blame] | 3371 | static void get_block_group_info(struct list_head *groups_list, |
| 3372 | struct btrfs_ioctl_space_info *space) |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3373 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3374 | struct btrfs_block_group *block_group; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3375 | |
| 3376 | space->total_bytes = 0; |
| 3377 | space->used_bytes = 0; |
| 3378 | space->flags = 0; |
| 3379 | list_for_each_entry(block_group, groups_list, list) { |
| 3380 | space->flags = block_group->flags; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3381 | space->total_bytes += block_group->length; |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 3382 | space->used_bytes += block_group->used; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3383 | } |
| 3384 | } |
| 3385 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3386 | static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, |
| 3387 | void __user *arg) |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3388 | { |
| 3389 | struct btrfs_ioctl_space_args space_args; |
| 3390 | struct btrfs_ioctl_space_info space; |
| 3391 | struct btrfs_ioctl_space_info *dest; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3392 | struct btrfs_ioctl_space_info *dest_orig; |
Daniel J Blueman | 13f2696 | 2011-04-11 15:56:31 +0000 | [diff] [blame] | 3393 | struct btrfs_ioctl_space_info __user *user_dest; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3394 | struct btrfs_space_info *info; |
Colin Ian King | 315d8e9 | 2017-09-19 16:01:23 +0100 | [diff] [blame] | 3395 | static const u64 types[] = { |
| 3396 | BTRFS_BLOCK_GROUP_DATA, |
| 3397 | BTRFS_BLOCK_GROUP_SYSTEM, |
| 3398 | BTRFS_BLOCK_GROUP_METADATA, |
| 3399 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA |
| 3400 | }; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3401 | int num_types = 4; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3402 | int alloc_size; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3403 | int ret = 0; |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3404 | u64 slot_count = 0; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3405 | int i, c; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3406 | |
| 3407 | if (copy_from_user(&space_args, |
| 3408 | (struct btrfs_ioctl_space_args __user *)arg, |
| 3409 | sizeof(space_args))) |
| 3410 | return -EFAULT; |
| 3411 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3412 | for (i = 0; i < num_types; i++) { |
| 3413 | struct btrfs_space_info *tmp; |
| 3414 | |
| 3415 | info = NULL; |
| 3416 | rcu_read_lock(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3417 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3418 | list) { |
| 3419 | if (tmp->flags == types[i]) { |
| 3420 | info = tmp; |
| 3421 | break; |
| 3422 | } |
| 3423 | } |
| 3424 | rcu_read_unlock(); |
| 3425 | |
| 3426 | if (!info) |
| 3427 | continue; |
| 3428 | |
| 3429 | down_read(&info->groups_sem); |
| 3430 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 3431 | if (!list_empty(&info->block_groups[c])) |
| 3432 | slot_count++; |
| 3433 | } |
| 3434 | up_read(&info->groups_sem); |
| 3435 | } |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3436 | |
David Sterba | 36523e95 | 2014-02-07 14:34:12 +0100 | [diff] [blame] | 3437 | /* |
| 3438 | * Global block reserve, exported as a space_info |
| 3439 | */ |
| 3440 | slot_count++; |
| 3441 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3442 | /* space_slots == 0 means they are asking for a count */ |
| 3443 | if (space_args.space_slots == 0) { |
| 3444 | space_args.total_spaces = slot_count; |
| 3445 | goto out; |
| 3446 | } |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3447 | |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3448 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3449 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3450 | alloc_size = sizeof(*dest) * slot_count; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3451 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3452 | /* we generally have at most 6 or so space infos, one for each raid |
| 3453 | * level. So, a whole page should be more than enough for everyone |
| 3454 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3455 | if (alloc_size > PAGE_SIZE) |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3456 | return -ENOMEM; |
| 3457 | |
| 3458 | space_args.total_spaces = 0; |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 3459 | dest = kmalloc(alloc_size, GFP_KERNEL); |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3460 | if (!dest) |
| 3461 | return -ENOMEM; |
| 3462 | dest_orig = dest; |
| 3463 | |
| 3464 | /* now we have a buffer to copy into */ |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3465 | for (i = 0; i < num_types; i++) { |
| 3466 | struct btrfs_space_info *tmp; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3467 | |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3468 | if (!slot_count) |
| 3469 | break; |
| 3470 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3471 | info = NULL; |
| 3472 | rcu_read_lock(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3473 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3474 | list) { |
| 3475 | if (tmp->flags == types[i]) { |
| 3476 | info = tmp; |
| 3477 | break; |
| 3478 | } |
| 3479 | } |
| 3480 | rcu_read_unlock(); |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3481 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3482 | if (!info) |
| 3483 | continue; |
| 3484 | down_read(&info->groups_sem); |
| 3485 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 3486 | if (!list_empty(&info->block_groups[c])) { |
Su Yue | c065f5b1 | 2018-04-02 17:24:11 +0800 | [diff] [blame] | 3487 | get_block_group_info(&info->block_groups[c], |
| 3488 | &space); |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3489 | memcpy(dest, &space, sizeof(space)); |
| 3490 | dest++; |
| 3491 | space_args.total_spaces++; |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3492 | slot_count--; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3493 | } |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3494 | if (!slot_count) |
| 3495 | break; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3496 | } |
| 3497 | up_read(&info->groups_sem); |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3498 | } |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3499 | |
David Sterba | 36523e95 | 2014-02-07 14:34:12 +0100 | [diff] [blame] | 3500 | /* |
| 3501 | * Add global block reserve |
| 3502 | */ |
| 3503 | if (slot_count) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3504 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
David Sterba | 36523e95 | 2014-02-07 14:34:12 +0100 | [diff] [blame] | 3505 | |
| 3506 | spin_lock(&block_rsv->lock); |
| 3507 | space.total_bytes = block_rsv->size; |
| 3508 | space.used_bytes = block_rsv->size - block_rsv->reserved; |
| 3509 | spin_unlock(&block_rsv->lock); |
| 3510 | space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; |
| 3511 | memcpy(dest, &space, sizeof(space)); |
| 3512 | space_args.total_spaces++; |
| 3513 | } |
| 3514 | |
Daniel J Blueman | 2eec6c8 | 2012-04-26 00:37:14 +0800 | [diff] [blame] | 3515 | user_dest = (struct btrfs_ioctl_space_info __user *) |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3516 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
| 3517 | |
| 3518 | if (copy_to_user(user_dest, dest_orig, alloc_size)) |
| 3519 | ret = -EFAULT; |
| 3520 | |
| 3521 | kfree(dest_orig); |
| 3522 | out: |
| 3523 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3524 | ret = -EFAULT; |
| 3525 | |
| 3526 | return ret; |
| 3527 | } |
| 3528 | |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 3529 | static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, |
| 3530 | void __user *argp) |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3531 | { |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3532 | struct btrfs_trans_handle *trans; |
| 3533 | u64 transid; |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 3534 | int ret; |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3535 | |
Miao Xie | d4edf39 | 2013-02-20 09:17:06 +0000 | [diff] [blame] | 3536 | trans = btrfs_attach_transaction_barrier(root); |
Miao Xie | ff7c1d3 | 2012-11-26 08:41:29 +0000 | [diff] [blame] | 3537 | if (IS_ERR(trans)) { |
| 3538 | if (PTR_ERR(trans) != -ENOENT) |
| 3539 | return PTR_ERR(trans); |
| 3540 | |
| 3541 | /* No running transaction, don't bother */ |
| 3542 | transid = root->fs_info->last_trans_committed; |
| 3543 | goto out; |
| 3544 | } |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3545 | transid = trans->transid; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3546 | ret = btrfs_commit_transaction_async(trans, 0); |
Tsutomu Itoh | 8b2b2d3 | 2011-04-04 01:52:13 +0000 | [diff] [blame] | 3547 | if (ret) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3548 | btrfs_end_transaction(trans); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 3549 | return ret; |
Tsutomu Itoh | 8b2b2d3 | 2011-04-04 01:52:13 +0000 | [diff] [blame] | 3550 | } |
Miao Xie | ff7c1d3 | 2012-11-26 08:41:29 +0000 | [diff] [blame] | 3551 | out: |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3552 | if (argp) |
| 3553 | if (copy_to_user(argp, &transid, sizeof(transid))) |
| 3554 | return -EFAULT; |
| 3555 | return 0; |
| 3556 | } |
| 3557 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3558 | static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 3559 | void __user *argp) |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3560 | { |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3561 | u64 transid; |
| 3562 | |
| 3563 | if (argp) { |
| 3564 | if (copy_from_user(&transid, argp, sizeof(transid))) |
| 3565 | return -EFAULT; |
| 3566 | } else { |
| 3567 | transid = 0; /* current trans */ |
| 3568 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3569 | return btrfs_wait_for_commit(fs_info, transid); |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3570 | } |
| 3571 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3572 | static long btrfs_ioctl_scrub(struct file *file, void __user *arg) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3573 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3574 | struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3575 | struct btrfs_ioctl_scrub_args *sa; |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3576 | int ret; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3577 | |
| 3578 | if (!capable(CAP_SYS_ADMIN)) |
| 3579 | return -EPERM; |
| 3580 | |
| 3581 | sa = memdup_user(arg, sizeof(*sa)); |
| 3582 | if (IS_ERR(sa)) |
| 3583 | return PTR_ERR(sa); |
| 3584 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3585 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) { |
| 3586 | ret = mnt_want_write_file(file); |
| 3587 | if (ret) |
| 3588 | goto out; |
| 3589 | } |
| 3590 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3591 | ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3592 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, |
| 3593 | 0); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3594 | |
Filipe Manana | 5afe6ce | 2020-01-16 11:29:20 +0000 | [diff] [blame] | 3595 | /* |
| 3596 | * Copy scrub args to user space even if btrfs_scrub_dev() returned an |
| 3597 | * error. This is important as it allows user space to know how much |
| 3598 | * progress scrub has done. For example, if scrub is canceled we get |
| 3599 | * -ECANCELED from btrfs_scrub_dev() and return that error back to user |
| 3600 | * space. Later user space can inspect the progress from the structure |
| 3601 | * btrfs_ioctl_scrub_args and resume scrub from where it left off |
| 3602 | * previously (btrfs-progs does this). |
| 3603 | * If we fail to copy the btrfs_ioctl_scrub_args structure to user space |
| 3604 | * then return -EFAULT to signal the structure was not copied or it may |
| 3605 | * be corrupt and unreliable due to a partial copy. |
| 3606 | */ |
| 3607 | if (copy_to_user(arg, sa, sizeof(*sa))) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3608 | ret = -EFAULT; |
| 3609 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3610 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) |
| 3611 | mnt_drop_write_file(file); |
| 3612 | out: |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3613 | kfree(sa); |
| 3614 | return ret; |
| 3615 | } |
| 3616 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3617 | static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3618 | { |
| 3619 | if (!capable(CAP_SYS_ADMIN)) |
| 3620 | return -EPERM; |
| 3621 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3622 | return btrfs_scrub_cancel(fs_info); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3623 | } |
| 3624 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3625 | static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3626 | void __user *arg) |
| 3627 | { |
| 3628 | struct btrfs_ioctl_scrub_args *sa; |
| 3629 | int ret; |
| 3630 | |
| 3631 | if (!capable(CAP_SYS_ADMIN)) |
| 3632 | return -EPERM; |
| 3633 | |
| 3634 | sa = memdup_user(arg, sizeof(*sa)); |
| 3635 | if (IS_ERR(sa)) |
| 3636 | return PTR_ERR(sa); |
| 3637 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3638 | ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3639 | |
Filipe Manana | 4fa99b0 | 2018-12-14 19:45:13 +0000 | [diff] [blame] | 3640 | if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3641 | ret = -EFAULT; |
| 3642 | |
| 3643 | kfree(sa); |
| 3644 | return ret; |
| 3645 | } |
| 3646 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3647 | static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 3648 | void __user *arg) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3649 | { |
| 3650 | struct btrfs_ioctl_get_dev_stats *sa; |
| 3651 | int ret; |
| 3652 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3653 | sa = memdup_user(arg, sizeof(*sa)); |
| 3654 | if (IS_ERR(sa)) |
| 3655 | return PTR_ERR(sa); |
| 3656 | |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 3657 | if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { |
| 3658 | kfree(sa); |
| 3659 | return -EPERM; |
| 3660 | } |
| 3661 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3662 | ret = btrfs_get_dev_stats(fs_info, sa); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3663 | |
Filipe Manana | eee9957 | 2018-12-14 19:45:22 +0000 | [diff] [blame] | 3664 | if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3665 | ret = -EFAULT; |
| 3666 | |
| 3667 | kfree(sa); |
| 3668 | return ret; |
| 3669 | } |
| 3670 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3671 | static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, |
| 3672 | void __user *arg) |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3673 | { |
| 3674 | struct btrfs_ioctl_dev_replace_args *p; |
| 3675 | int ret; |
| 3676 | |
| 3677 | if (!capable(CAP_SYS_ADMIN)) |
| 3678 | return -EPERM; |
| 3679 | |
| 3680 | p = memdup_user(arg, sizeof(*p)); |
| 3681 | if (IS_ERR(p)) |
| 3682 | return PTR_ERR(p); |
| 3683 | |
| 3684 | switch (p->cmd) { |
| 3685 | case BTRFS_IOCTL_DEV_REPLACE_CMD_START: |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 3686 | if (sb_rdonly(fs_info->sb)) { |
Ilya Dryomov | adfa97c | 2013-10-10 20:39:28 +0300 | [diff] [blame] | 3687 | ret = -EROFS; |
| 3688 | goto out; |
| 3689 | } |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3690 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
Anand Jain | e57138b | 2013-08-21 11:44:48 +0800 | [diff] [blame] | 3691 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3692 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3693 | ret = btrfs_dev_replace_by_ioctl(fs_info, p); |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3694 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3695 | } |
| 3696 | break; |
| 3697 | case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3698 | btrfs_dev_replace_status(fs_info, p); |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3699 | ret = 0; |
| 3700 | break; |
| 3701 | case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: |
Anand Jain | 17d202b | 2018-02-12 23:33:30 +0800 | [diff] [blame] | 3702 | p->result = btrfs_dev_replace_cancel(fs_info); |
Anand Jain | 9728203 | 2018-02-12 23:33:29 +0800 | [diff] [blame] | 3703 | ret = 0; |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3704 | break; |
| 3705 | default: |
| 3706 | ret = -EINVAL; |
| 3707 | break; |
| 3708 | } |
| 3709 | |
Filipe Manana | d3a5328 | 2019-01-08 11:42:09 +0000 | [diff] [blame] | 3710 | if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p))) |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3711 | ret = -EFAULT; |
Ilya Dryomov | adfa97c | 2013-10-10 20:39:28 +0300 | [diff] [blame] | 3712 | out: |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3713 | kfree(p); |
| 3714 | return ret; |
| 3715 | } |
| 3716 | |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3717 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
| 3718 | { |
| 3719 | int ret = 0; |
| 3720 | int i; |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 3721 | u64 rel_ptr; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3722 | int size; |
Chris Mason | 806468f | 2011-11-06 03:07:10 -0500 | [diff] [blame] | 3723 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3724 | struct inode_fs_paths *ipath = NULL; |
| 3725 | struct btrfs_path *path; |
| 3726 | |
Kusanagi Kouichi | 82b22ac | 2013-01-28 11:33:31 +0000 | [diff] [blame] | 3727 | if (!capable(CAP_DAC_READ_SEARCH)) |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3728 | return -EPERM; |
| 3729 | |
| 3730 | path = btrfs_alloc_path(); |
| 3731 | if (!path) { |
| 3732 | ret = -ENOMEM; |
| 3733 | goto out; |
| 3734 | } |
| 3735 | |
| 3736 | ipa = memdup_user(arg, sizeof(*ipa)); |
| 3737 | if (IS_ERR(ipa)) { |
| 3738 | ret = PTR_ERR(ipa); |
| 3739 | ipa = NULL; |
| 3740 | goto out; |
| 3741 | } |
| 3742 | |
| 3743 | size = min_t(u32, ipa->size, 4096); |
| 3744 | ipath = init_ipath(size, root, path); |
| 3745 | if (IS_ERR(ipath)) { |
| 3746 | ret = PTR_ERR(ipath); |
| 3747 | ipath = NULL; |
| 3748 | goto out; |
| 3749 | } |
| 3750 | |
| 3751 | ret = paths_from_inode(ipa->inum, ipath); |
| 3752 | if (ret < 0) |
| 3753 | goto out; |
| 3754 | |
| 3755 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { |
Jeff Mahoney | 745c4d8 | 2011-11-20 07:31:57 -0500 | [diff] [blame] | 3756 | rel_ptr = ipath->fspath->val[i] - |
| 3757 | (u64)(unsigned long)ipath->fspath->val; |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 3758 | ipath->fspath->val[i] = rel_ptr; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3759 | } |
| 3760 | |
Omar Sandoval | 718dc5f | 2017-08-22 23:46:05 -0700 | [diff] [blame] | 3761 | ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, |
| 3762 | ipath->fspath, size); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3763 | if (ret) { |
| 3764 | ret = -EFAULT; |
| 3765 | goto out; |
| 3766 | } |
| 3767 | |
| 3768 | out: |
| 3769 | btrfs_free_path(path); |
| 3770 | free_ipath(ipath); |
| 3771 | kfree(ipa); |
| 3772 | |
| 3773 | return ret; |
| 3774 | } |
| 3775 | |
| 3776 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) |
| 3777 | { |
| 3778 | struct btrfs_data_container *inodes = ctx; |
| 3779 | const size_t c = 3 * sizeof(u64); |
| 3780 | |
| 3781 | if (inodes->bytes_left >= c) { |
| 3782 | inodes->bytes_left -= c; |
| 3783 | inodes->val[inodes->elem_cnt] = inum; |
| 3784 | inodes->val[inodes->elem_cnt + 1] = offset; |
| 3785 | inodes->val[inodes->elem_cnt + 2] = root; |
| 3786 | inodes->elem_cnt += 3; |
| 3787 | } else { |
| 3788 | inodes->bytes_missing += c - inodes->bytes_left; |
| 3789 | inodes->bytes_left = 0; |
| 3790 | inodes->elem_missed += 3; |
| 3791 | } |
| 3792 | |
| 3793 | return 0; |
| 3794 | } |
| 3795 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3796 | static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3797 | void __user *arg, int version) |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3798 | { |
| 3799 | int ret = 0; |
| 3800 | int size; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3801 | struct btrfs_ioctl_logical_ino_args *loi; |
| 3802 | struct btrfs_data_container *inodes = NULL; |
| 3803 | struct btrfs_path *path = NULL; |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3804 | bool ignore_offset; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3805 | |
| 3806 | if (!capable(CAP_SYS_ADMIN)) |
| 3807 | return -EPERM; |
| 3808 | |
| 3809 | loi = memdup_user(arg, sizeof(*loi)); |
Shailendra Verma | 7b9ea62 | 2016-11-10 15:17:41 +0530 | [diff] [blame] | 3810 | if (IS_ERR(loi)) |
| 3811 | return PTR_ERR(loi); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3812 | |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3813 | if (version == 1) { |
| 3814 | ignore_offset = false; |
Zygo Blaxell | b115e3b | 2017-09-22 13:58:47 -0400 | [diff] [blame] | 3815 | size = min_t(u32, loi->size, SZ_64K); |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3816 | } else { |
| 3817 | /* All reserved bits must be 0 for now */ |
| 3818 | if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { |
| 3819 | ret = -EINVAL; |
| 3820 | goto out_loi; |
| 3821 | } |
| 3822 | /* Only accept flags we have defined so far */ |
| 3823 | if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { |
| 3824 | ret = -EINVAL; |
| 3825 | goto out_loi; |
| 3826 | } |
| 3827 | ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; |
Zygo Blaxell | b115e3b | 2017-09-22 13:58:47 -0400 | [diff] [blame] | 3828 | size = min_t(u32, loi->size, SZ_16M); |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3829 | } |
| 3830 | |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3831 | path = btrfs_alloc_path(); |
| 3832 | if (!path) { |
| 3833 | ret = -ENOMEM; |
| 3834 | goto out; |
| 3835 | } |
| 3836 | |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3837 | inodes = init_data_container(size); |
| 3838 | if (IS_ERR(inodes)) { |
| 3839 | ret = PTR_ERR(inodes); |
| 3840 | inodes = NULL; |
| 3841 | goto out; |
| 3842 | } |
| 3843 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3844 | ret = iterate_inodes_from_logical(loi->logical, fs_info, path, |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3845 | build_ino_list, inodes, ignore_offset); |
Liu Bo | df031f0 | 2012-09-07 20:01:29 -0600 | [diff] [blame] | 3846 | if (ret == -EINVAL) |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3847 | ret = -ENOENT; |
| 3848 | if (ret < 0) |
| 3849 | goto out; |
| 3850 | |
Omar Sandoval | 718dc5f | 2017-08-22 23:46:05 -0700 | [diff] [blame] | 3851 | ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, |
| 3852 | size); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3853 | if (ret) |
| 3854 | ret = -EFAULT; |
| 3855 | |
| 3856 | out: |
| 3857 | btrfs_free_path(path); |
David Sterba | f54de06 | 2017-05-31 19:32:09 +0200 | [diff] [blame] | 3858 | kvfree(inodes); |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3859 | out_loi: |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3860 | kfree(loi); |
| 3861 | |
| 3862 | return ret; |
| 3863 | } |
| 3864 | |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 3865 | void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3866 | struct btrfs_ioctl_balance_args *bargs) |
| 3867 | { |
| 3868 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3869 | |
| 3870 | bargs->flags = bctl->flags; |
| 3871 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3872 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3873 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; |
| 3874 | if (atomic_read(&fs_info->balance_pause_req)) |
| 3875 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3876 | if (atomic_read(&fs_info->balance_cancel_req)) |
| 3877 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3878 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3879 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
| 3880 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); |
| 3881 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3882 | |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 3883 | spin_lock(&fs_info->balance_lock); |
| 3884 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); |
| 3885 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3886 | } |
| 3887 | |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 3888 | static long btrfs_ioctl_balance(struct file *file, void __user *arg) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3889 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3890 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3891 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3892 | struct btrfs_ioctl_balance_args *bargs; |
| 3893 | struct btrfs_balance_control *bctl; |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3894 | bool need_unlock; /* for mut. excl. ops lock */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3895 | int ret; |
| 3896 | |
| 3897 | if (!capable(CAP_SYS_ADMIN)) |
| 3898 | return -EPERM; |
| 3899 | |
Liu Bo | e54bfa31 | 2012-06-29 03:58:48 -0600 | [diff] [blame] | 3900 | ret = mnt_want_write_file(file); |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 3901 | if (ret) |
| 3902 | return ret; |
| 3903 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3904 | again: |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3905 | if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3906 | mutex_lock(&fs_info->balance_mutex); |
| 3907 | need_unlock = true; |
| 3908 | goto locked; |
| 3909 | } |
| 3910 | |
| 3911 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3912 | * mut. excl. ops lock is locked. Three possibilities: |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3913 | * (1) some other op is running |
| 3914 | * (2) balance is running |
| 3915 | * (3) balance is paused -- special case (think resume) |
| 3916 | */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3917 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3918 | if (fs_info->balance_ctl) { |
| 3919 | /* this is either (2) or (3) */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3920 | if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3921 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 3922 | /* |
| 3923 | * Lock released to allow other waiters to continue, |
| 3924 | * we'll reexamine the status again. |
| 3925 | */ |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3926 | mutex_lock(&fs_info->balance_mutex); |
| 3927 | |
| 3928 | if (fs_info->balance_ctl && |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3929 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3930 | /* this is (3) */ |
| 3931 | need_unlock = false; |
| 3932 | goto locked; |
| 3933 | } |
| 3934 | |
| 3935 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3936 | goto again; |
| 3937 | } else { |
| 3938 | /* this is (2) */ |
| 3939 | mutex_unlock(&fs_info->balance_mutex); |
| 3940 | ret = -EINPROGRESS; |
| 3941 | goto out; |
| 3942 | } |
| 3943 | } else { |
| 3944 | /* this is (1) */ |
| 3945 | mutex_unlock(&fs_info->balance_mutex); |
Anand Jain | e57138b | 2013-08-21 11:44:48 +0800 | [diff] [blame] | 3946 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3947 | goto out; |
| 3948 | } |
| 3949 | |
| 3950 | locked: |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3951 | BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3952 | |
| 3953 | if (arg) { |
| 3954 | bargs = memdup_user(arg, sizeof(*bargs)); |
| 3955 | if (IS_ERR(bargs)) { |
| 3956 | ret = PTR_ERR(bargs); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3957 | goto out_unlock; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3958 | } |
Ilya Dryomov | de32226 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3959 | |
| 3960 | if (bargs->flags & BTRFS_BALANCE_RESUME) { |
| 3961 | if (!fs_info->balance_ctl) { |
| 3962 | ret = -ENOTCONN; |
| 3963 | goto out_bargs; |
| 3964 | } |
| 3965 | |
| 3966 | bctl = fs_info->balance_ctl; |
| 3967 | spin_lock(&fs_info->balance_lock); |
| 3968 | bctl->flags |= BTRFS_BALANCE_RESUME; |
| 3969 | spin_unlock(&fs_info->balance_lock); |
| 3970 | |
| 3971 | goto do_balance; |
| 3972 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3973 | } else { |
| 3974 | bargs = NULL; |
| 3975 | } |
| 3976 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3977 | if (fs_info->balance_ctl) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3978 | ret = -EINPROGRESS; |
| 3979 | goto out_bargs; |
| 3980 | } |
| 3981 | |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 3982 | bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3983 | if (!bctl) { |
| 3984 | ret = -ENOMEM; |
| 3985 | goto out_bargs; |
| 3986 | } |
| 3987 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3988 | if (arg) { |
| 3989 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); |
| 3990 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); |
| 3991 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); |
| 3992 | |
| 3993 | bctl->flags = bargs->flags; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3994 | } else { |
| 3995 | /* balance everything - no filters */ |
| 3996 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3997 | } |
| 3998 | |
David Sterba | 8eb9345 | 2015-10-12 16:55:54 +0200 | [diff] [blame] | 3999 | if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { |
| 4000 | ret = -EINVAL; |
Christian Engelmayer | 0f89abf | 2015-10-21 00:50:06 +0200 | [diff] [blame] | 4001 | goto out_bctl; |
David Sterba | 8eb9345 | 2015-10-12 16:55:54 +0200 | [diff] [blame] | 4002 | } |
| 4003 | |
Ilya Dryomov | de32226 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4004 | do_balance: |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4005 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4006 | * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to |
| 4007 | * btrfs_balance. bctl is freed in reset_balance_state, or, if |
| 4008 | * restriper was paused all the way until unmount, in free_fs_info. |
| 4009 | * The flag should be cleared after reset_balance_state. |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4010 | */ |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4011 | need_unlock = false; |
| 4012 | |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4013 | ret = btrfs_balance(fs_info, bctl, bargs); |
Christian Engelmayer | 0f89abf | 2015-10-21 00:50:06 +0200 | [diff] [blame] | 4014 | bctl = NULL; |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4015 | |
Filipe Manana | d00c2d9 | 2019-01-08 11:42:01 +0000 | [diff] [blame] | 4016 | if ((ret == 0 || ret == -ECANCELED) && arg) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4017 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
| 4018 | ret = -EFAULT; |
| 4019 | } |
| 4020 | |
Christian Engelmayer | 0f89abf | 2015-10-21 00:50:06 +0200 | [diff] [blame] | 4021 | out_bctl: |
| 4022 | kfree(bctl); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4023 | out_bargs: |
| 4024 | kfree(bargs); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4025 | out_unlock: |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4026 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4027 | if (need_unlock) |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 4028 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4029 | out: |
Liu Bo | e54bfa31 | 2012-06-29 03:58:48 -0600 | [diff] [blame] | 4030 | mnt_drop_write_file(file); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4031 | return ret; |
| 4032 | } |
| 4033 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4034 | static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4035 | { |
| 4036 | if (!capable(CAP_SYS_ADMIN)) |
| 4037 | return -EPERM; |
| 4038 | |
| 4039 | switch (cmd) { |
| 4040 | case BTRFS_BALANCE_CTL_PAUSE: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4041 | return btrfs_pause_balance(fs_info); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4042 | case BTRFS_BALANCE_CTL_CANCEL: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4043 | return btrfs_cancel_balance(fs_info); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4044 | } |
| 4045 | |
| 4046 | return -EINVAL; |
| 4047 | } |
| 4048 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4049 | static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4050 | void __user *arg) |
| 4051 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4052 | struct btrfs_ioctl_balance_args *bargs; |
| 4053 | int ret = 0; |
| 4054 | |
| 4055 | if (!capable(CAP_SYS_ADMIN)) |
| 4056 | return -EPERM; |
| 4057 | |
| 4058 | mutex_lock(&fs_info->balance_mutex); |
| 4059 | if (!fs_info->balance_ctl) { |
| 4060 | ret = -ENOTCONN; |
| 4061 | goto out; |
| 4062 | } |
| 4063 | |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 4064 | bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4065 | if (!bargs) { |
| 4066 | ret = -ENOMEM; |
| 4067 | goto out; |
| 4068 | } |
| 4069 | |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4070 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4071 | |
| 4072 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
| 4073 | ret = -EFAULT; |
| 4074 | |
| 4075 | kfree(bargs); |
| 4076 | out: |
| 4077 | mutex_unlock(&fs_info->balance_mutex); |
| 4078 | return ret; |
| 4079 | } |
| 4080 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4081 | static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4082 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4083 | struct inode *inode = file_inode(file); |
| 4084 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4085 | struct btrfs_ioctl_quota_ctl_args *sa; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4086 | int ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4087 | |
| 4088 | if (!capable(CAP_SYS_ADMIN)) |
| 4089 | return -EPERM; |
| 4090 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4091 | ret = mnt_want_write_file(file); |
| 4092 | if (ret) |
| 4093 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4094 | |
| 4095 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4096 | if (IS_ERR(sa)) { |
| 4097 | ret = PTR_ERR(sa); |
| 4098 | goto drop_write; |
| 4099 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4100 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4101 | down_write(&fs_info->subvol_sem); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4102 | |
| 4103 | switch (sa->cmd) { |
| 4104 | case BTRFS_QUOTA_CTL_ENABLE: |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 4105 | ret = btrfs_quota_enable(fs_info); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4106 | break; |
| 4107 | case BTRFS_QUOTA_CTL_DISABLE: |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 4108 | ret = btrfs_quota_disable(fs_info); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4109 | break; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4110 | default: |
| 4111 | ret = -EINVAL; |
| 4112 | break; |
| 4113 | } |
| 4114 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4115 | kfree(sa); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4116 | up_write(&fs_info->subvol_sem); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4117 | drop_write: |
| 4118 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4119 | return ret; |
| 4120 | } |
| 4121 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4122 | static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4123 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4124 | struct inode *inode = file_inode(file); |
| 4125 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 4126 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4127 | struct btrfs_ioctl_qgroup_assign_args *sa; |
| 4128 | struct btrfs_trans_handle *trans; |
| 4129 | int ret; |
| 4130 | int err; |
| 4131 | |
| 4132 | if (!capable(CAP_SYS_ADMIN)) |
| 4133 | return -EPERM; |
| 4134 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4135 | ret = mnt_want_write_file(file); |
| 4136 | if (ret) |
| 4137 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4138 | |
| 4139 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4140 | if (IS_ERR(sa)) { |
| 4141 | ret = PTR_ERR(sa); |
| 4142 | goto drop_write; |
| 4143 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4144 | |
| 4145 | trans = btrfs_join_transaction(root); |
| 4146 | if (IS_ERR(trans)) { |
| 4147 | ret = PTR_ERR(trans); |
| 4148 | goto out; |
| 4149 | } |
| 4150 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4151 | if (sa->assign) { |
Lu Fengqi | 9f8a6ce | 2018-07-18 14:45:30 +0800 | [diff] [blame] | 4152 | ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4153 | } else { |
Lu Fengqi | 39616c2 | 2018-07-18 14:45:32 +0800 | [diff] [blame] | 4154 | ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4155 | } |
| 4156 | |
Qu Wenruo | e082f56 | 2015-02-27 16:24:28 +0800 | [diff] [blame] | 4157 | /* update qgroup status and info */ |
Lu Fengqi | 280f8bd | 2018-07-18 14:45:40 +0800 | [diff] [blame] | 4158 | err = btrfs_run_qgroups(trans); |
Qu Wenruo | e082f56 | 2015-02-27 16:24:28 +0800 | [diff] [blame] | 4159 | if (err < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4160 | btrfs_handle_fs_error(fs_info, err, |
| 4161 | "failed to update qgroup status and info"); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4162 | err = btrfs_end_transaction(trans); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4163 | if (err && !ret) |
| 4164 | ret = err; |
| 4165 | |
| 4166 | out: |
| 4167 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4168 | drop_write: |
| 4169 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4170 | return ret; |
| 4171 | } |
| 4172 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4173 | static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4174 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4175 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4176 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4177 | struct btrfs_ioctl_qgroup_create_args *sa; |
| 4178 | struct btrfs_trans_handle *trans; |
| 4179 | int ret; |
| 4180 | int err; |
| 4181 | |
| 4182 | if (!capable(CAP_SYS_ADMIN)) |
| 4183 | return -EPERM; |
| 4184 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4185 | ret = mnt_want_write_file(file); |
| 4186 | if (ret) |
| 4187 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4188 | |
| 4189 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4190 | if (IS_ERR(sa)) { |
| 4191 | ret = PTR_ERR(sa); |
| 4192 | goto drop_write; |
| 4193 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4194 | |
Miao Xie | d86e56c | 2012-11-15 11:35:41 +0000 | [diff] [blame] | 4195 | if (!sa->qgroupid) { |
| 4196 | ret = -EINVAL; |
| 4197 | goto out; |
| 4198 | } |
| 4199 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4200 | trans = btrfs_join_transaction(root); |
| 4201 | if (IS_ERR(trans)) { |
| 4202 | ret = PTR_ERR(trans); |
| 4203 | goto out; |
| 4204 | } |
| 4205 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4206 | if (sa->create) { |
Lu Fengqi | 49a05ec | 2018-07-18 14:45:33 +0800 | [diff] [blame] | 4207 | ret = btrfs_create_qgroup(trans, sa->qgroupid); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4208 | } else { |
Lu Fengqi | 3efbee1 | 2018-07-18 14:45:34 +0800 | [diff] [blame] | 4209 | ret = btrfs_remove_qgroup(trans, sa->qgroupid); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4210 | } |
| 4211 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4212 | err = btrfs_end_transaction(trans); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4213 | if (err && !ret) |
| 4214 | ret = err; |
| 4215 | |
| 4216 | out: |
| 4217 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4218 | drop_write: |
| 4219 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4220 | return ret; |
| 4221 | } |
| 4222 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4223 | static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4224 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4225 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4226 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4227 | struct btrfs_ioctl_qgroup_limit_args *sa; |
| 4228 | struct btrfs_trans_handle *trans; |
| 4229 | int ret; |
| 4230 | int err; |
| 4231 | u64 qgroupid; |
| 4232 | |
| 4233 | if (!capable(CAP_SYS_ADMIN)) |
| 4234 | return -EPERM; |
| 4235 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4236 | ret = mnt_want_write_file(file); |
| 4237 | if (ret) |
| 4238 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4239 | |
| 4240 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4241 | if (IS_ERR(sa)) { |
| 4242 | ret = PTR_ERR(sa); |
| 4243 | goto drop_write; |
| 4244 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4245 | |
| 4246 | trans = btrfs_join_transaction(root); |
| 4247 | if (IS_ERR(trans)) { |
| 4248 | ret = PTR_ERR(trans); |
| 4249 | goto out; |
| 4250 | } |
| 4251 | |
| 4252 | qgroupid = sa->qgroupid; |
| 4253 | if (!qgroupid) { |
| 4254 | /* take the current subvol as qgroup */ |
| 4255 | qgroupid = root->root_key.objectid; |
| 4256 | } |
| 4257 | |
Lu Fengqi | f0042d5 | 2018-07-18 14:45:35 +0800 | [diff] [blame] | 4258 | ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4259 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4260 | err = btrfs_end_transaction(trans); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4261 | if (err && !ret) |
| 4262 | ret = err; |
| 4263 | |
| 4264 | out: |
| 4265 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4266 | drop_write: |
| 4267 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4268 | return ret; |
| 4269 | } |
| 4270 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4271 | static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) |
| 4272 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4273 | struct inode *inode = file_inode(file); |
| 4274 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4275 | struct btrfs_ioctl_quota_rescan_args *qsa; |
| 4276 | int ret; |
| 4277 | |
| 4278 | if (!capable(CAP_SYS_ADMIN)) |
| 4279 | return -EPERM; |
| 4280 | |
| 4281 | ret = mnt_want_write_file(file); |
| 4282 | if (ret) |
| 4283 | return ret; |
| 4284 | |
| 4285 | qsa = memdup_user(arg, sizeof(*qsa)); |
| 4286 | if (IS_ERR(qsa)) { |
| 4287 | ret = PTR_ERR(qsa); |
| 4288 | goto drop_write; |
| 4289 | } |
| 4290 | |
| 4291 | if (qsa->flags) { |
| 4292 | ret = -EINVAL; |
| 4293 | goto out; |
| 4294 | } |
| 4295 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4296 | ret = btrfs_qgroup_rescan(fs_info); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4297 | |
| 4298 | out: |
| 4299 | kfree(qsa); |
| 4300 | drop_write: |
| 4301 | mnt_drop_write_file(file); |
| 4302 | return ret; |
| 4303 | } |
| 4304 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4305 | static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info, |
| 4306 | void __user *arg) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4307 | { |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4308 | struct btrfs_ioctl_quota_rescan_args *qsa; |
| 4309 | int ret = 0; |
| 4310 | |
| 4311 | if (!capable(CAP_SYS_ADMIN)) |
| 4312 | return -EPERM; |
| 4313 | |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 4314 | qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4315 | if (!qsa) |
| 4316 | return -ENOMEM; |
| 4317 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4318 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4319 | qsa->flags = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4320 | qsa->progress = fs_info->qgroup_rescan_progress.objectid; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | if (copy_to_user(arg, qsa, sizeof(*qsa))) |
| 4324 | ret = -EFAULT; |
| 4325 | |
| 4326 | kfree(qsa); |
| 4327 | return ret; |
| 4328 | } |
| 4329 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4330 | static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info, |
| 4331 | void __user *arg) |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 4332 | { |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 4333 | if (!capable(CAP_SYS_ADMIN)) |
| 4334 | return -EPERM; |
| 4335 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4336 | return btrfs_qgroup_wait_for_completion(fs_info, true); |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4339 | static long _btrfs_ioctl_set_received_subvol(struct file *file, |
| 4340 | struct btrfs_ioctl_received_subvol_args *sa) |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4341 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4342 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4343 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4344 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4345 | struct btrfs_root_item *root_item = &root->root_item; |
| 4346 | struct btrfs_trans_handle *trans; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 4347 | struct timespec64 ct = current_time(inode); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4348 | int ret = 0; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4349 | int received_uuid_changed; |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4350 | |
David Sterba | bd60ea0 | 2014-01-16 15:50:22 +0100 | [diff] [blame] | 4351 | if (!inode_owner_or_capable(inode)) |
| 4352 | return -EPERM; |
| 4353 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4354 | ret = mnt_want_write_file(file); |
| 4355 | if (ret < 0) |
| 4356 | return ret; |
| 4357 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4358 | down_write(&fs_info->subvol_sem); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4359 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4360 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4361 | ret = -EINVAL; |
| 4362 | goto out; |
| 4363 | } |
| 4364 | |
| 4365 | if (btrfs_root_readonly(root)) { |
| 4366 | ret = -EROFS; |
| 4367 | goto out; |
| 4368 | } |
| 4369 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4370 | /* |
| 4371 | * 1 - root item |
| 4372 | * 2 - uuid items (received uuid + subvol uuid) |
| 4373 | */ |
| 4374 | trans = btrfs_start_transaction(root, 3); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4375 | if (IS_ERR(trans)) { |
| 4376 | ret = PTR_ERR(trans); |
| 4377 | trans = NULL; |
| 4378 | goto out; |
| 4379 | } |
| 4380 | |
| 4381 | sa->rtransid = trans->transid; |
| 4382 | sa->rtime.sec = ct.tv_sec; |
| 4383 | sa->rtime.nsec = ct.tv_nsec; |
| 4384 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4385 | received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, |
| 4386 | BTRFS_UUID_SIZE); |
| 4387 | if (received_uuid_changed && |
Nikolay Borisov | d87ff75 | 2018-03-12 14:48:09 +0200 | [diff] [blame] | 4388 | !btrfs_is_empty_uuid(root_item->received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4389 | ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid, |
Nikolay Borisov | d87ff75 | 2018-03-12 14:48:09 +0200 | [diff] [blame] | 4390 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4391 | root->root_key.objectid); |
| 4392 | if (ret && ret != -ENOENT) { |
| 4393 | btrfs_abort_transaction(trans, ret); |
| 4394 | btrfs_end_transaction(trans); |
| 4395 | goto out; |
| 4396 | } |
| 4397 | } |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4398 | memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); |
| 4399 | btrfs_set_root_stransid(root_item, sa->stransid); |
| 4400 | btrfs_set_root_rtransid(root_item, sa->rtransid); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 4401 | btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); |
| 4402 | btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); |
| 4403 | btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); |
| 4404 | btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4405 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4406 | ret = btrfs_update_root(trans, fs_info->tree_root, |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4407 | &root->root_key, &root->root_item); |
| 4408 | if (ret < 0) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4409 | btrfs_end_transaction(trans); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4410 | goto out; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4411 | } |
| 4412 | if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4413 | ret = btrfs_uuid_tree_add(trans, sa->uuid, |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4414 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4415 | root->root_key.objectid); |
| 4416 | if (ret < 0 && ret != -EEXIST) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4417 | btrfs_abort_transaction(trans, ret); |
Nikolay Borisov | efd3815 | 2017-09-28 11:45:26 +0300 | [diff] [blame] | 4418 | btrfs_end_transaction(trans); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4419 | goto out; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4420 | } |
| 4421 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4422 | ret = btrfs_commit_transaction(trans); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4423 | out: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4424 | up_write(&fs_info->subvol_sem); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4425 | mnt_drop_write_file(file); |
| 4426 | return ret; |
| 4427 | } |
| 4428 | |
| 4429 | #ifdef CONFIG_64BIT |
| 4430 | static long btrfs_ioctl_set_received_subvol_32(struct file *file, |
| 4431 | void __user *arg) |
| 4432 | { |
| 4433 | struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; |
| 4434 | struct btrfs_ioctl_received_subvol_args *args64 = NULL; |
| 4435 | int ret = 0; |
| 4436 | |
| 4437 | args32 = memdup_user(arg, sizeof(*args32)); |
Shailendra Verma | 7b9ea62 | 2016-11-10 15:17:41 +0530 | [diff] [blame] | 4438 | if (IS_ERR(args32)) |
| 4439 | return PTR_ERR(args32); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4440 | |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 4441 | args64 = kmalloc(sizeof(*args64), GFP_KERNEL); |
Dan Carpenter | 84dbeb8 | 2014-03-28 11:06:00 +0300 | [diff] [blame] | 4442 | if (!args64) { |
| 4443 | ret = -ENOMEM; |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4444 | goto out; |
| 4445 | } |
| 4446 | |
| 4447 | memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); |
| 4448 | args64->stransid = args32->stransid; |
| 4449 | args64->rtransid = args32->rtransid; |
| 4450 | args64->stime.sec = args32->stime.sec; |
| 4451 | args64->stime.nsec = args32->stime.nsec; |
| 4452 | args64->rtime.sec = args32->rtime.sec; |
| 4453 | args64->rtime.nsec = args32->rtime.nsec; |
| 4454 | args64->flags = args32->flags; |
| 4455 | |
| 4456 | ret = _btrfs_ioctl_set_received_subvol(file, args64); |
| 4457 | if (ret) |
| 4458 | goto out; |
| 4459 | |
| 4460 | memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); |
| 4461 | args32->stransid = args64->stransid; |
| 4462 | args32->rtransid = args64->rtransid; |
| 4463 | args32->stime.sec = args64->stime.sec; |
| 4464 | args32->stime.nsec = args64->stime.nsec; |
| 4465 | args32->rtime.sec = args64->rtime.sec; |
| 4466 | args32->rtime.nsec = args64->rtime.nsec; |
| 4467 | args32->flags = args64->flags; |
| 4468 | |
| 4469 | ret = copy_to_user(arg, args32, sizeof(*args32)); |
| 4470 | if (ret) |
| 4471 | ret = -EFAULT; |
| 4472 | |
| 4473 | out: |
| 4474 | kfree(args32); |
| 4475 | kfree(args64); |
| 4476 | return ret; |
| 4477 | } |
| 4478 | #endif |
| 4479 | |
| 4480 | static long btrfs_ioctl_set_received_subvol(struct file *file, |
| 4481 | void __user *arg) |
| 4482 | { |
| 4483 | struct btrfs_ioctl_received_subvol_args *sa = NULL; |
| 4484 | int ret = 0; |
| 4485 | |
| 4486 | sa = memdup_user(arg, sizeof(*sa)); |
Shailendra Verma | 7b9ea62 | 2016-11-10 15:17:41 +0530 | [diff] [blame] | 4487 | if (IS_ERR(sa)) |
| 4488 | return PTR_ERR(sa); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4489 | |
| 4490 | ret = _btrfs_ioctl_set_received_subvol(file, sa); |
| 4491 | |
| 4492 | if (ret) |
| 4493 | goto out; |
| 4494 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4495 | ret = copy_to_user(arg, sa, sizeof(*sa)); |
| 4496 | if (ret) |
| 4497 | ret = -EFAULT; |
| 4498 | |
| 4499 | out: |
| 4500 | kfree(sa); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4501 | return ret; |
| 4502 | } |
| 4503 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4504 | static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info, |
| 4505 | void __user *arg) |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4506 | { |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 4507 | size_t len; |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4508 | int ret; |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 4509 | char label[BTRFS_LABEL_SIZE]; |
| 4510 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4511 | spin_lock(&fs_info->super_lock); |
| 4512 | memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); |
| 4513 | spin_unlock(&fs_info->super_lock); |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 4514 | |
| 4515 | len = strnlen(label, BTRFS_LABEL_SIZE); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4516 | |
| 4517 | if (len == BTRFS_LABEL_SIZE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4518 | btrfs_warn(fs_info, |
| 4519 | "label is too long, return the first %zu bytes", |
| 4520 | --len); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4523 | ret = copy_to_user(arg, label, len); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4524 | |
| 4525 | return ret ? -EFAULT : 0; |
| 4526 | } |
| 4527 | |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4528 | static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) |
| 4529 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4530 | struct inode *inode = file_inode(file); |
| 4531 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 4532 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4533 | struct btrfs_super_block *super_block = fs_info->super_copy; |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4534 | struct btrfs_trans_handle *trans; |
| 4535 | char label[BTRFS_LABEL_SIZE]; |
| 4536 | int ret; |
| 4537 | |
| 4538 | if (!capable(CAP_SYS_ADMIN)) |
| 4539 | return -EPERM; |
| 4540 | |
| 4541 | if (copy_from_user(label, arg, sizeof(label))) |
| 4542 | return -EFAULT; |
| 4543 | |
| 4544 | if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4545 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4546 | "unable to set label with more than %d bytes", |
| 4547 | BTRFS_LABEL_SIZE - 1); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4548 | return -EINVAL; |
| 4549 | } |
| 4550 | |
| 4551 | ret = mnt_want_write_file(file); |
| 4552 | if (ret) |
| 4553 | return ret; |
| 4554 | |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4555 | trans = btrfs_start_transaction(root, 0); |
| 4556 | if (IS_ERR(trans)) { |
| 4557 | ret = PTR_ERR(trans); |
| 4558 | goto out_unlock; |
| 4559 | } |
| 4560 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4561 | spin_lock(&fs_info->super_lock); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4562 | strcpy(super_block->label, label); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4563 | spin_unlock(&fs_info->super_lock); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4564 | ret = btrfs_commit_transaction(trans); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4565 | |
| 4566 | out_unlock: |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4567 | mnt_drop_write_file(file); |
| 4568 | return ret; |
| 4569 | } |
| 4570 | |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4571 | #define INIT_FEATURE_FLAGS(suffix) \ |
| 4572 | { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ |
| 4573 | .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ |
| 4574 | .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } |
| 4575 | |
David Sterba | d5131b6 | 2016-02-17 15:26:27 +0100 | [diff] [blame] | 4576 | int btrfs_ioctl_get_supported_features(void __user *arg) |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4577 | { |
David Sterba | 4d4ab6d | 2015-11-19 11:42:31 +0100 | [diff] [blame] | 4578 | static const struct btrfs_ioctl_feature_flags features[3] = { |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4579 | INIT_FEATURE_FLAGS(SUPP), |
| 4580 | INIT_FEATURE_FLAGS(SAFE_SET), |
| 4581 | INIT_FEATURE_FLAGS(SAFE_CLEAR) |
| 4582 | }; |
| 4583 | |
| 4584 | if (copy_to_user(arg, &features, sizeof(features))) |
| 4585 | return -EFAULT; |
| 4586 | |
| 4587 | return 0; |
| 4588 | } |
| 4589 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4590 | static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info, |
| 4591 | void __user *arg) |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4592 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4593 | struct btrfs_super_block *super_block = fs_info->super_copy; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4594 | struct btrfs_ioctl_feature_flags features; |
| 4595 | |
| 4596 | features.compat_flags = btrfs_super_compat_flags(super_block); |
| 4597 | features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); |
| 4598 | features.incompat_flags = btrfs_super_incompat_flags(super_block); |
| 4599 | |
| 4600 | if (copy_to_user(arg, &features, sizeof(features))) |
| 4601 | return -EFAULT; |
| 4602 | |
| 4603 | return 0; |
| 4604 | } |
| 4605 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4606 | static int check_feature_bits(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4607 | enum btrfs_feature_set set, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4608 | u64 change_mask, u64 flags, u64 supported_flags, |
| 4609 | u64 safe_set, u64 safe_clear) |
| 4610 | { |
David Sterba | f10152b | 2019-08-01 19:07:55 +0200 | [diff] [blame] | 4611 | const char *type = btrfs_feature_set_name(set); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4612 | char *names; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4613 | u64 disallowed, unsupported; |
| 4614 | u64 set_mask = flags & change_mask; |
| 4615 | u64 clear_mask = ~flags & change_mask; |
| 4616 | |
| 4617 | unsupported = set_mask & ~supported_flags; |
| 4618 | if (unsupported) { |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4619 | names = btrfs_printable_features(set, unsupported); |
| 4620 | if (names) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4621 | btrfs_warn(fs_info, |
| 4622 | "this kernel does not support the %s feature bit%s", |
| 4623 | names, strchr(names, ',') ? "s" : ""); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4624 | kfree(names); |
| 4625 | } else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4626 | btrfs_warn(fs_info, |
| 4627 | "this kernel does not support %s bits 0x%llx", |
| 4628 | type, unsupported); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4629 | return -EOPNOTSUPP; |
| 4630 | } |
| 4631 | |
| 4632 | disallowed = set_mask & ~safe_set; |
| 4633 | if (disallowed) { |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4634 | names = btrfs_printable_features(set, disallowed); |
| 4635 | if (names) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4636 | btrfs_warn(fs_info, |
| 4637 | "can't set the %s feature bit%s while mounted", |
| 4638 | names, strchr(names, ',') ? "s" : ""); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4639 | kfree(names); |
| 4640 | } else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4641 | btrfs_warn(fs_info, |
| 4642 | "can't set %s bits 0x%llx while mounted", |
| 4643 | type, disallowed); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4644 | return -EPERM; |
| 4645 | } |
| 4646 | |
| 4647 | disallowed = clear_mask & ~safe_clear; |
| 4648 | if (disallowed) { |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4649 | names = btrfs_printable_features(set, disallowed); |
| 4650 | if (names) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4651 | btrfs_warn(fs_info, |
| 4652 | "can't clear the %s feature bit%s while mounted", |
| 4653 | names, strchr(names, ',') ? "s" : ""); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4654 | kfree(names); |
| 4655 | } else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4656 | btrfs_warn(fs_info, |
| 4657 | "can't clear %s bits 0x%llx while mounted", |
| 4658 | type, disallowed); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4659 | return -EPERM; |
| 4660 | } |
| 4661 | |
| 4662 | return 0; |
| 4663 | } |
| 4664 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4665 | #define check_feature(fs_info, change_mask, flags, mask_base) \ |
| 4666 | check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4667 | BTRFS_FEATURE_ ## mask_base ## _SUPP, \ |
| 4668 | BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ |
| 4669 | BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) |
| 4670 | |
| 4671 | static int btrfs_ioctl_set_features(struct file *file, void __user *arg) |
| 4672 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4673 | struct inode *inode = file_inode(file); |
| 4674 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 4675 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4676 | struct btrfs_super_block *super_block = fs_info->super_copy; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4677 | struct btrfs_ioctl_feature_flags flags[2]; |
| 4678 | struct btrfs_trans_handle *trans; |
| 4679 | u64 newflags; |
| 4680 | int ret; |
| 4681 | |
| 4682 | if (!capable(CAP_SYS_ADMIN)) |
| 4683 | return -EPERM; |
| 4684 | |
| 4685 | if (copy_from_user(flags, arg, sizeof(flags))) |
| 4686 | return -EFAULT; |
| 4687 | |
| 4688 | /* Nothing to do */ |
| 4689 | if (!flags[0].compat_flags && !flags[0].compat_ro_flags && |
| 4690 | !flags[0].incompat_flags) |
| 4691 | return 0; |
| 4692 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4693 | ret = check_feature(fs_info, flags[0].compat_flags, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4694 | flags[1].compat_flags, COMPAT); |
| 4695 | if (ret) |
| 4696 | return ret; |
| 4697 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4698 | ret = check_feature(fs_info, flags[0].compat_ro_flags, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4699 | flags[1].compat_ro_flags, COMPAT_RO); |
| 4700 | if (ret) |
| 4701 | return ret; |
| 4702 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4703 | ret = check_feature(fs_info, flags[0].incompat_flags, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4704 | flags[1].incompat_flags, INCOMPAT); |
| 4705 | if (ret) |
| 4706 | return ret; |
| 4707 | |
David Sterba | 7ab1962 | 2016-05-04 11:32:00 +0200 | [diff] [blame] | 4708 | ret = mnt_want_write_file(file); |
| 4709 | if (ret) |
| 4710 | return ret; |
| 4711 | |
David Sterba | 8051aa1 | 2014-02-07 14:34:04 +0100 | [diff] [blame] | 4712 | trans = btrfs_start_transaction(root, 0); |
David Sterba | 7ab1962 | 2016-05-04 11:32:00 +0200 | [diff] [blame] | 4713 | if (IS_ERR(trans)) { |
| 4714 | ret = PTR_ERR(trans); |
| 4715 | goto out_drop_write; |
| 4716 | } |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4717 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4718 | spin_lock(&fs_info->super_lock); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4719 | newflags = btrfs_super_compat_flags(super_block); |
| 4720 | newflags |= flags[0].compat_flags & flags[1].compat_flags; |
| 4721 | newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); |
| 4722 | btrfs_set_super_compat_flags(super_block, newflags); |
| 4723 | |
| 4724 | newflags = btrfs_super_compat_ro_flags(super_block); |
| 4725 | newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; |
| 4726 | newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); |
| 4727 | btrfs_set_super_compat_ro_flags(super_block, newflags); |
| 4728 | |
| 4729 | newflags = btrfs_super_incompat_flags(super_block); |
| 4730 | newflags |= flags[0].incompat_flags & flags[1].incompat_flags; |
| 4731 | newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); |
| 4732 | btrfs_set_super_incompat_flags(super_block, newflags); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4733 | spin_unlock(&fs_info->super_lock); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4734 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4735 | ret = btrfs_commit_transaction(trans); |
David Sterba | 7ab1962 | 2016-05-04 11:32:00 +0200 | [diff] [blame] | 4736 | out_drop_write: |
| 4737 | mnt_drop_write_file(file); |
| 4738 | |
| 4739 | return ret; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4740 | } |
| 4741 | |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 4742 | static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) |
| 4743 | { |
| 4744 | struct btrfs_ioctl_send_args *arg; |
| 4745 | int ret; |
| 4746 | |
| 4747 | if (compat) { |
| 4748 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) |
| 4749 | struct btrfs_ioctl_send_args_32 args32; |
| 4750 | |
| 4751 | ret = copy_from_user(&args32, argp, sizeof(args32)); |
| 4752 | if (ret) |
| 4753 | return -EFAULT; |
| 4754 | arg = kzalloc(sizeof(*arg), GFP_KERNEL); |
| 4755 | if (!arg) |
| 4756 | return -ENOMEM; |
| 4757 | arg->send_fd = args32.send_fd; |
| 4758 | arg->clone_sources_count = args32.clone_sources_count; |
| 4759 | arg->clone_sources = compat_ptr(args32.clone_sources); |
| 4760 | arg->parent_root = args32.parent_root; |
| 4761 | arg->flags = args32.flags; |
| 4762 | memcpy(arg->reserved, args32.reserved, |
| 4763 | sizeof(args32.reserved)); |
| 4764 | #else |
| 4765 | return -ENOTTY; |
| 4766 | #endif |
| 4767 | } else { |
| 4768 | arg = memdup_user(argp, sizeof(*arg)); |
| 4769 | if (IS_ERR(arg)) |
| 4770 | return PTR_ERR(arg); |
| 4771 | } |
| 4772 | ret = btrfs_ioctl_send(file, arg); |
| 4773 | kfree(arg); |
| 4774 | return ret; |
| 4775 | } |
| 4776 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4777 | long btrfs_ioctl(struct file *file, unsigned int |
| 4778 | cmd, unsigned long arg) |
| 4779 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4780 | struct inode *inode = file_inode(file); |
| 4781 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 4782 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 4783 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4784 | |
| 4785 | switch (cmd) { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4786 | case FS_IOC_GETFLAGS: |
| 4787 | return btrfs_ioctl_getflags(file, argp); |
| 4788 | case FS_IOC_SETFLAGS: |
| 4789 | return btrfs_ioctl_setflags(file, argp); |
| 4790 | case FS_IOC_GETVERSION: |
| 4791 | return btrfs_ioctl_getversion(file, argp); |
Eric Sandeen | 40cf931 | 2019-07-17 12:39:20 -0500 | [diff] [blame] | 4792 | case FS_IOC_GETFSLABEL: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4793 | return btrfs_ioctl_get_fslabel(fs_info, argp); |
Eric Sandeen | 40cf931 | 2019-07-17 12:39:20 -0500 | [diff] [blame] | 4794 | case FS_IOC_SETFSLABEL: |
| 4795 | return btrfs_ioctl_set_fslabel(file, argp); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 4796 | case FITRIM: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4797 | return btrfs_ioctl_fitrim(fs_info, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4798 | case BTRFS_IOC_SNAP_CREATE: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 4799 | return btrfs_ioctl_snap_create(file, argp, 0); |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 4800 | case BTRFS_IOC_SNAP_CREATE_V2: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 4801 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4802 | case BTRFS_IOC_SUBVOL_CREATE: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 4803 | return btrfs_ioctl_snap_create(file, argp, 1); |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 4804 | case BTRFS_IOC_SUBVOL_CREATE_V2: |
| 4805 | return btrfs_ioctl_snap_create_v2(file, argp, 1); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4806 | case BTRFS_IOC_SNAP_DESTROY: |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 4807 | return btrfs_ioctl_snap_destroy(file, argp, false); |
| 4808 | case BTRFS_IOC_SNAP_DESTROY_V2: |
| 4809 | return btrfs_ioctl_snap_destroy(file, argp, true); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 4810 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
| 4811 | return btrfs_ioctl_subvol_getflags(file, argp); |
| 4812 | case BTRFS_IOC_SUBVOL_SETFLAGS: |
| 4813 | return btrfs_ioctl_subvol_setflags(file, argp); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4814 | case BTRFS_IOC_DEFAULT_SUBVOL: |
| 4815 | return btrfs_ioctl_default_subvol(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4816 | case BTRFS_IOC_DEFRAG: |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 4817 | return btrfs_ioctl_defrag(file, NULL); |
| 4818 | case BTRFS_IOC_DEFRAG_RANGE: |
| 4819 | return btrfs_ioctl_defrag(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4820 | case BTRFS_IOC_RESIZE: |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 4821 | return btrfs_ioctl_resize(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4822 | case BTRFS_IOC_ADD_DEV: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4823 | return btrfs_ioctl_add_dev(fs_info, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4824 | case BTRFS_IOC_RM_DEV: |
Miao Xie | da24927 | 2012-11-26 08:44:50 +0000 | [diff] [blame] | 4825 | return btrfs_ioctl_rm_dev(file, argp); |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 4826 | case BTRFS_IOC_RM_DEV_V2: |
| 4827 | return btrfs_ioctl_rm_dev_v2(file, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4828 | case BTRFS_IOC_FS_INFO: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4829 | return btrfs_ioctl_fs_info(fs_info, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4830 | case BTRFS_IOC_DEV_INFO: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4831 | return btrfs_ioctl_dev_info(fs_info, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4832 | case BTRFS_IOC_BALANCE: |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 4833 | return btrfs_ioctl_balance(file, NULL); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 4834 | case BTRFS_IOC_TREE_SEARCH: |
| 4835 | return btrfs_ioctl_tree_search(file, argp); |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 4836 | case BTRFS_IOC_TREE_SEARCH_V2: |
| 4837 | return btrfs_ioctl_tree_search_v2(file, argp); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 4838 | case BTRFS_IOC_INO_LOOKUP: |
| 4839 | return btrfs_ioctl_ino_lookup(file, argp); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4840 | case BTRFS_IOC_INO_PATHS: |
| 4841 | return btrfs_ioctl_ino_to_path(root, argp); |
| 4842 | case BTRFS_IOC_LOGICAL_INO: |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 4843 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); |
| 4844 | case BTRFS_IOC_LOGICAL_INO_V2: |
| 4845 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 4846 | case BTRFS_IOC_SPACE_INFO: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4847 | return btrfs_ioctl_space_info(fs_info, argp); |
Filipe David Borba Manana | 9b19985 | 2013-09-23 11:35:11 +0100 | [diff] [blame] | 4848 | case BTRFS_IOC_SYNC: { |
| 4849 | int ret; |
| 4850 | |
Nikolay Borisov | 82b3e53 | 2018-04-23 10:54:13 +0300 | [diff] [blame] | 4851 | ret = btrfs_start_delalloc_roots(fs_info, -1); |
Filipe David Borba Manana | 9b19985 | 2013-09-23 11:35:11 +0100 | [diff] [blame] | 4852 | if (ret) |
| 4853 | return ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4854 | ret = btrfs_sync_fs(inode->i_sb, 1); |
David Sterba | 2fad4e8 | 2014-07-23 14:39:35 +0200 | [diff] [blame] | 4855 | /* |
| 4856 | * The transaction thread may want to do more work, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4857 | * namely it pokes the cleaner kthread that will start |
David Sterba | 2fad4e8 | 2014-07-23 14:39:35 +0200 | [diff] [blame] | 4858 | * processing uncleaned subvols. |
| 4859 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4860 | wake_up_process(fs_info->transaction_kthread); |
Filipe David Borba Manana | 9b19985 | 2013-09-23 11:35:11 +0100 | [diff] [blame] | 4861 | return ret; |
| 4862 | } |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4863 | case BTRFS_IOC_START_SYNC: |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 4864 | return btrfs_ioctl_start_sync(root, argp); |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4865 | case BTRFS_IOC_WAIT_SYNC: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4866 | return btrfs_ioctl_wait_sync(fs_info, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4867 | case BTRFS_IOC_SCRUB: |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 4868 | return btrfs_ioctl_scrub(file, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4869 | case BTRFS_IOC_SCRUB_CANCEL: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4870 | return btrfs_ioctl_scrub_cancel(fs_info); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4871 | case BTRFS_IOC_SCRUB_PROGRESS: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4872 | return btrfs_ioctl_scrub_progress(fs_info, argp); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4873 | case BTRFS_IOC_BALANCE_V2: |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 4874 | return btrfs_ioctl_balance(file, argp); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4875 | case BTRFS_IOC_BALANCE_CTL: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4876 | return btrfs_ioctl_balance_ctl(fs_info, arg); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4877 | case BTRFS_IOC_BALANCE_PROGRESS: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4878 | return btrfs_ioctl_balance_progress(fs_info, argp); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4879 | case BTRFS_IOC_SET_RECEIVED_SUBVOL: |
| 4880 | return btrfs_ioctl_set_received_subvol(file, argp); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4881 | #ifdef CONFIG_64BIT |
| 4882 | case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: |
| 4883 | return btrfs_ioctl_set_received_subvol_32(file, argp); |
| 4884 | #endif |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4885 | case BTRFS_IOC_SEND: |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 4886 | return _btrfs_ioctl_send(file, argp, false); |
| 4887 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) |
| 4888 | case BTRFS_IOC_SEND_32: |
| 4889 | return _btrfs_ioctl_send(file, argp, true); |
| 4890 | #endif |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 4891 | case BTRFS_IOC_GET_DEV_STATS: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4892 | return btrfs_ioctl_get_dev_stats(fs_info, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4893 | case BTRFS_IOC_QUOTA_CTL: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4894 | return btrfs_ioctl_quota_ctl(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4895 | case BTRFS_IOC_QGROUP_ASSIGN: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4896 | return btrfs_ioctl_qgroup_assign(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4897 | case BTRFS_IOC_QGROUP_CREATE: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4898 | return btrfs_ioctl_qgroup_create(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4899 | case BTRFS_IOC_QGROUP_LIMIT: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4900 | return btrfs_ioctl_qgroup_limit(file, argp); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4901 | case BTRFS_IOC_QUOTA_RESCAN: |
| 4902 | return btrfs_ioctl_quota_rescan(file, argp); |
| 4903 | case BTRFS_IOC_QUOTA_RESCAN_STATUS: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4904 | return btrfs_ioctl_quota_rescan_status(fs_info, argp); |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 4905 | case BTRFS_IOC_QUOTA_RESCAN_WAIT: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4906 | return btrfs_ioctl_quota_rescan_wait(fs_info, argp); |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 4907 | case BTRFS_IOC_DEV_REPLACE: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4908 | return btrfs_ioctl_dev_replace(fs_info, argp); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4909 | case BTRFS_IOC_GET_SUPPORTED_FEATURES: |
David Sterba | d5131b6 | 2016-02-17 15:26:27 +0100 | [diff] [blame] | 4910 | return btrfs_ioctl_get_supported_features(argp); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4911 | case BTRFS_IOC_GET_FEATURES: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4912 | return btrfs_ioctl_get_features(fs_info, argp); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4913 | case BTRFS_IOC_SET_FEATURES: |
| 4914 | return btrfs_ioctl_set_features(file, argp); |
David Sterba | e4202ac | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 4915 | case FS_IOC_FSGETXATTR: |
| 4916 | return btrfs_ioctl_fsgetxattr(file, argp); |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 4917 | case FS_IOC_FSSETXATTR: |
| 4918 | return btrfs_ioctl_fssetxattr(file, argp); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 4919 | case BTRFS_IOC_GET_SUBVOL_INFO: |
| 4920 | return btrfs_ioctl_get_subvol_info(file, argp); |
Tomohiro Misono | 42e4b52 | 2018-05-21 10:09:43 +0900 | [diff] [blame] | 4921 | case BTRFS_IOC_GET_SUBVOL_ROOTREF: |
| 4922 | return btrfs_ioctl_get_subvol_rootref(file, argp); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 4923 | case BTRFS_IOC_INO_LOOKUP_USER: |
| 4924 | return btrfs_ioctl_ino_lookup_user(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4925 | } |
| 4926 | |
| 4927 | return -ENOTTY; |
| 4928 | } |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 4929 | |
| 4930 | #ifdef CONFIG_COMPAT |
| 4931 | long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 4932 | { |
Jeff Mahoney | 2a36224 | 2017-02-06 19:39:09 -0500 | [diff] [blame] | 4933 | /* |
| 4934 | * These all access 32-bit values anyway so no further |
| 4935 | * handling is necessary. |
| 4936 | */ |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 4937 | switch (cmd) { |
| 4938 | case FS_IOC32_GETFLAGS: |
| 4939 | cmd = FS_IOC_GETFLAGS; |
| 4940 | break; |
| 4941 | case FS_IOC32_SETFLAGS: |
| 4942 | cmd = FS_IOC_SETFLAGS; |
| 4943 | break; |
| 4944 | case FS_IOC32_GETVERSION: |
| 4945 | cmd = FS_IOC_GETVERSION; |
| 4946 | break; |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
| 4949 | return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
| 4950 | } |
| 4951 | #endif |