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 | |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 90 | static int btrfs_clone(struct inode *src, struct inode *inode, |
Mark Fasheh | 1c919a5 | 2015-06-30 14:42:08 -0700 | [diff] [blame] | 91 | u64 off, u64 olen, u64 olen_aligned, u64 destoff, |
| 92 | int no_time_update); |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 93 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 94 | /* Mask out flags that are inappropriate for the given type of inode. */ |
David Sterba | 1905a0f | 2018-03-26 18:52:15 +0200 | [diff] [blame] | 95 | static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode, |
| 96 | unsigned int flags) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 97 | { |
David Sterba | 1905a0f | 2018-03-26 18:52:15 +0200 | [diff] [blame] | 98 | if (S_ISDIR(inode->i_mode)) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 99 | return flags; |
David Sterba | 1905a0f | 2018-03-26 18:52:15 +0200 | [diff] [blame] | 100 | else if (S_ISREG(inode->i_mode)) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 101 | return flags & ~FS_DIRSYNC_FL; |
| 102 | else |
| 103 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); |
| 104 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 105 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 106 | /* |
David Sterba | a157d4f | 2018-03-26 19:12:25 +0200 | [diff] [blame] | 107 | * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS |
| 108 | * ioctl. |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 109 | */ |
David Sterba | a157d4f | 2018-03-26 19:12:25 +0200 | [diff] [blame] | 110 | static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 111 | { |
| 112 | unsigned int iflags = 0; |
| 113 | |
| 114 | if (flags & BTRFS_INODE_SYNC) |
| 115 | iflags |= FS_SYNC_FL; |
| 116 | if (flags & BTRFS_INODE_IMMUTABLE) |
| 117 | iflags |= FS_IMMUTABLE_FL; |
| 118 | if (flags & BTRFS_INODE_APPEND) |
| 119 | iflags |= FS_APPEND_FL; |
| 120 | if (flags & BTRFS_INODE_NODUMP) |
| 121 | iflags |= FS_NODUMP_FL; |
| 122 | if (flags & BTRFS_INODE_NOATIME) |
| 123 | iflags |= FS_NOATIME_FL; |
| 124 | if (flags & BTRFS_INODE_DIRSYNC) |
| 125 | iflags |= FS_DIRSYNC_FL; |
Li Zefan | d0092bd | 2011-04-15 03:03:06 +0000 | [diff] [blame] | 126 | if (flags & BTRFS_INODE_NODATACOW) |
| 127 | iflags |= FS_NOCOW_FL; |
| 128 | |
Satoru Takeuchi | 13f48dc | 2016-03-15 09:09:59 +0900 | [diff] [blame] | 129 | if (flags & BTRFS_INODE_NOCOMPRESS) |
Li Zefan | d0092bd | 2011-04-15 03:03:06 +0000 | [diff] [blame] | 130 | iflags |= FS_NOCOMP_FL; |
Satoru Takeuchi | 13f48dc | 2016-03-15 09:09:59 +0900 | [diff] [blame] | 131 | else if (flags & BTRFS_INODE_COMPRESS) |
| 132 | iflags |= FS_COMPR_FL; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 133 | |
| 134 | return iflags; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Update inode->i_flags based on the btrfs internal flags. |
| 139 | */ |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 140 | void btrfs_sync_inode_flags_to_i_flags(struct inode *inode) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 141 | { |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 142 | struct btrfs_inode *binode = BTRFS_I(inode); |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 143 | unsigned int new_fl = 0; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 144 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 145 | if (binode->flags & BTRFS_INODE_SYNC) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 146 | new_fl |= S_SYNC; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 147 | if (binode->flags & BTRFS_INODE_IMMUTABLE) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 148 | new_fl |= S_IMMUTABLE; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 149 | if (binode->flags & BTRFS_INODE_APPEND) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 150 | new_fl |= S_APPEND; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 151 | if (binode->flags & BTRFS_INODE_NOATIME) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 152 | new_fl |= S_NOATIME; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 153 | if (binode->flags & BTRFS_INODE_DIRSYNC) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 154 | new_fl |= S_DIRSYNC; |
| 155 | |
| 156 | set_mask_bits(&inode->i_flags, |
| 157 | S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC, |
| 158 | new_fl); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 161 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) |
| 162 | { |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 163 | struct btrfs_inode *binode = BTRFS_I(file_inode(file)); |
| 164 | unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 165 | |
| 166 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 167 | return -EFAULT; |
| 168 | return 0; |
| 169 | } |
| 170 | |
David Sterba | 5ba76ab | 2018-03-26 18:52:15 +0200 | [diff] [blame] | 171 | /* Check if @flags are a supported and valid set of FS_*_FL flags */ |
| 172 | static int check_fsflags(unsigned int flags) |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 173 | { |
| 174 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
| 175 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
| 176 | FS_SYNC_FL | FS_DIRSYNC_FL | \ |
Li Zefan | e1e8fb6 | 2011-04-15 03:02:49 +0000 | [diff] [blame] | 177 | FS_NOCOMP_FL | FS_COMPR_FL | |
| 178 | FS_NOCOW_FL)) |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 179 | return -EOPNOTSUPP; |
| 180 | |
| 181 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) |
| 182 | return -EINVAL; |
| 183 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 187 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) |
| 188 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 189 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 190 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 191 | struct btrfs_inode *binode = BTRFS_I(inode); |
| 192 | struct btrfs_root *root = binode->root; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 193 | struct btrfs_trans_handle *trans; |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 194 | unsigned int fsflags, old_fsflags; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 195 | int ret; |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 196 | const char *comp = NULL; |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 197 | u32 binode_flags = binode->flags; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 198 | |
David Sterba | bd60ea0 | 2014-01-16 15:50:22 +0100 | [diff] [blame] | 199 | if (!inode_owner_or_capable(inode)) |
| 200 | return -EPERM; |
| 201 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 202 | if (btrfs_root_readonly(root)) |
| 203 | return -EROFS; |
| 204 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 205 | if (copy_from_user(&fsflags, arg, sizeof(fsflags))) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 206 | return -EFAULT; |
| 207 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 208 | ret = check_fsflags(fsflags); |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 209 | if (ret) |
| 210 | return ret; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 211 | |
Jan Kara | e784868 | 2012-06-12 16:20:32 +0200 | [diff] [blame] | 212 | ret = mnt_want_write_file(file); |
| 213 | if (ret) |
| 214 | return ret; |
| 215 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 216 | inode_lock(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 217 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 218 | fsflags = btrfs_mask_fsflags_for_type(inode, fsflags); |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 219 | old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags); |
| 220 | ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags); |
| 221 | if (ret) |
| 222 | goto out_unlock; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 223 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 224 | if (fsflags & FS_SYNC_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 225 | binode_flags |= BTRFS_INODE_SYNC; |
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_SYNC; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 228 | if (fsflags & FS_IMMUTABLE_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 229 | binode_flags |= BTRFS_INODE_IMMUTABLE; |
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_IMMUTABLE; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 232 | if (fsflags & FS_APPEND_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 233 | binode_flags |= BTRFS_INODE_APPEND; |
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_APPEND; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 236 | if (fsflags & FS_NODUMP_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 237 | binode_flags |= BTRFS_INODE_NODUMP; |
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_NODUMP; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 240 | if (fsflags & FS_NOATIME_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 241 | binode_flags |= BTRFS_INODE_NOATIME; |
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_NOATIME; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 244 | if (fsflags & FS_DIRSYNC_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 245 | binode_flags |= BTRFS_INODE_DIRSYNC; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 246 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 247 | binode_flags &= ~BTRFS_INODE_DIRSYNC; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 248 | if (fsflags & FS_NOCOW_FL) { |
Anand Jain | 44e5194 | 2019-04-20 19:48:57 +0800 | [diff] [blame] | 249 | if (S_ISREG(inode->i_mode)) { |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 250 | /* |
| 251 | * It's safe to turn csums off here, no extents exist. |
| 252 | * Otherwise we want the flag to reflect the real COW |
| 253 | * status of the file and will not set it. |
| 254 | */ |
| 255 | if (inode->i_size == 0) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 256 | binode_flags |= BTRFS_INODE_NODATACOW | |
| 257 | BTRFS_INODE_NODATASUM; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 258 | } else { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 259 | binode_flags |= BTRFS_INODE_NODATACOW; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 260 | } |
| 261 | } else { |
| 262 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 263 | * Revert back under same assumptions as above |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 264 | */ |
Anand Jain | 44e5194 | 2019-04-20 19:48:57 +0800 | [diff] [blame] | 265 | if (S_ISREG(inode->i_mode)) { |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 266 | if (inode->i_size == 0) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 267 | binode_flags &= ~(BTRFS_INODE_NODATACOW | |
| 268 | BTRFS_INODE_NODATASUM); |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 269 | } else { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 270 | binode_flags &= ~BTRFS_INODE_NODATACOW; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 271 | } |
| 272 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 273 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 274 | /* |
| 275 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS |
| 276 | * flag may be changed automatically if compression code won't make |
| 277 | * things smaller. |
| 278 | */ |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 279 | if (fsflags & FS_NOCOMP_FL) { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 280 | binode_flags &= ~BTRFS_INODE_COMPRESS; |
| 281 | binode_flags |= BTRFS_INODE_NOCOMPRESS; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 282 | } else if (fsflags & FS_COMPR_FL) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 283 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 284 | if (IS_SWAPFILE(inode)) { |
| 285 | ret = -ETXTBSY; |
| 286 | goto out_unlock; |
| 287 | } |
| 288 | |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 289 | binode_flags |= BTRFS_INODE_COMPRESS; |
| 290 | binode_flags &= ~BTRFS_INODE_NOCOMPRESS; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 291 | |
David Sterba | 9337050 | 2017-10-31 17:32:41 +0100 | [diff] [blame] | 292 | comp = btrfs_compress_type2str(fs_info->compress_type); |
| 293 | if (!comp || comp[0] == 0) |
| 294 | comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); |
Li Zefan | ebcb904 | 2011-04-15 03:03:17 +0000 | [diff] [blame] | 295 | } else { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 296 | binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 297 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 298 | |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 299 | /* |
| 300 | * 1 for inode item |
| 301 | * 2 for properties |
| 302 | */ |
| 303 | trans = btrfs_start_transaction(root, 3); |
Li Zefan | f062abf0 | 2011-12-29 13:36:45 +0800 | [diff] [blame] | 304 | if (IS_ERR(trans)) { |
| 305 | ret = PTR_ERR(trans); |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 306 | goto out_unlock; |
Li Zefan | f062abf0 | 2011-12-29 13:36:45 +0800 | [diff] [blame] | 307 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 308 | |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 309 | if (comp) { |
| 310 | ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp, |
| 311 | strlen(comp), 0); |
| 312 | if (ret) { |
| 313 | btrfs_abort_transaction(trans, ret); |
| 314 | goto out_end_trans; |
| 315 | } |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 316 | } else { |
| 317 | ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL, |
| 318 | 0, 0); |
| 319 | if (ret && ret != -ENODATA) { |
| 320 | btrfs_abort_transaction(trans, ret); |
| 321 | goto out_end_trans; |
| 322 | } |
| 323 | } |
| 324 | |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 325 | binode->flags = binode_flags; |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 326 | btrfs_sync_inode_flags_to_i_flags(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 327 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 328 | inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 329 | ret = btrfs_update_inode(trans, root, inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 330 | |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 331 | out_end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 332 | btrfs_end_transaction(trans); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 333 | out_unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 334 | inode_unlock(inode); |
Jan Kara | e784868 | 2012-06-12 16:20:32 +0200 | [diff] [blame] | 335 | mnt_drop_write_file(file); |
liubo | 2d4e6f6ad | 2011-02-24 09:38:16 +0000 | [diff] [blame] | 336 | return ret; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 337 | } |
| 338 | |
David Sterba | 19f93b3 | 2018-03-26 19:42:05 +0200 | [diff] [blame] | 339 | /* |
| 340 | * Translate btrfs internal inode flags to xflags as expected by the |
| 341 | * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are |
| 342 | * silently dropped. |
| 343 | */ |
| 344 | static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags) |
| 345 | { |
| 346 | unsigned int xflags = 0; |
| 347 | |
| 348 | if (flags & BTRFS_INODE_APPEND) |
| 349 | xflags |= FS_XFLAG_APPEND; |
| 350 | if (flags & BTRFS_INODE_IMMUTABLE) |
| 351 | xflags |= FS_XFLAG_IMMUTABLE; |
| 352 | if (flags & BTRFS_INODE_NOATIME) |
| 353 | xflags |= FS_XFLAG_NOATIME; |
| 354 | if (flags & BTRFS_INODE_NODUMP) |
| 355 | xflags |= FS_XFLAG_NODUMP; |
| 356 | if (flags & BTRFS_INODE_SYNC) |
| 357 | xflags |= FS_XFLAG_SYNC; |
| 358 | |
| 359 | return xflags; |
| 360 | } |
| 361 | |
| 362 | /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */ |
| 363 | static int check_xflags(unsigned int flags) |
| 364 | { |
| 365 | if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME | |
| 366 | FS_XFLAG_NODUMP | FS_XFLAG_SYNC)) |
| 367 | return -EOPNOTSUPP; |
| 368 | return 0; |
| 369 | } |
| 370 | |
David Sterba | e4202ac | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 371 | /* |
| 372 | * Set the xflags from the internal inode flags. The remaining items of fsxattr |
| 373 | * are zeroed. |
| 374 | */ |
| 375 | static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg) |
| 376 | { |
| 377 | struct btrfs_inode *binode = BTRFS_I(file_inode(file)); |
| 378 | struct fsxattr fa; |
| 379 | |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 380 | simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags)); |
David Sterba | e4202ac | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 381 | if (copy_to_user(arg, &fa, sizeof(fa))) |
| 382 | return -EFAULT; |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 387 | static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg) |
| 388 | { |
| 389 | struct inode *inode = file_inode(file); |
| 390 | struct btrfs_inode *binode = BTRFS_I(inode); |
| 391 | struct btrfs_root *root = binode->root; |
| 392 | struct btrfs_trans_handle *trans; |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 393 | struct fsxattr fa, old_fa; |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 394 | unsigned old_flags; |
| 395 | unsigned old_i_flags; |
| 396 | int ret = 0; |
| 397 | |
| 398 | if (!inode_owner_or_capable(inode)) |
| 399 | return -EPERM; |
| 400 | |
| 401 | if (btrfs_root_readonly(root)) |
| 402 | return -EROFS; |
| 403 | |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 404 | if (copy_from_user(&fa, arg, sizeof(fa))) |
| 405 | return -EFAULT; |
| 406 | |
| 407 | ret = check_xflags(fa.fsx_xflags); |
| 408 | if (ret) |
| 409 | return ret; |
| 410 | |
| 411 | if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0) |
| 412 | return -EOPNOTSUPP; |
| 413 | |
| 414 | ret = mnt_want_write_file(file); |
| 415 | if (ret) |
| 416 | return ret; |
| 417 | |
| 418 | inode_lock(inode); |
| 419 | |
| 420 | old_flags = binode->flags; |
| 421 | old_i_flags = inode->i_flags; |
| 422 | |
Darrick J. Wong | 7b0e492 | 2019-07-01 08:25:35 -0700 | [diff] [blame] | 423 | simple_fill_fsxattr(&old_fa, |
| 424 | btrfs_inode_flags_to_xflags(binode->flags)); |
| 425 | ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa); |
| 426 | if (ret) |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 427 | goto out_unlock; |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 428 | |
| 429 | if (fa.fsx_xflags & FS_XFLAG_SYNC) |
| 430 | binode->flags |= BTRFS_INODE_SYNC; |
| 431 | else |
| 432 | binode->flags &= ~BTRFS_INODE_SYNC; |
| 433 | if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE) |
| 434 | binode->flags |= BTRFS_INODE_IMMUTABLE; |
| 435 | else |
| 436 | binode->flags &= ~BTRFS_INODE_IMMUTABLE; |
| 437 | if (fa.fsx_xflags & FS_XFLAG_APPEND) |
| 438 | binode->flags |= BTRFS_INODE_APPEND; |
| 439 | else |
| 440 | binode->flags &= ~BTRFS_INODE_APPEND; |
| 441 | if (fa.fsx_xflags & FS_XFLAG_NODUMP) |
| 442 | binode->flags |= BTRFS_INODE_NODUMP; |
| 443 | else |
| 444 | binode->flags &= ~BTRFS_INODE_NODUMP; |
| 445 | if (fa.fsx_xflags & FS_XFLAG_NOATIME) |
| 446 | binode->flags |= BTRFS_INODE_NOATIME; |
| 447 | else |
| 448 | binode->flags &= ~BTRFS_INODE_NOATIME; |
| 449 | |
| 450 | /* 1 item for the inode */ |
| 451 | trans = btrfs_start_transaction(root, 1); |
| 452 | if (IS_ERR(trans)) { |
| 453 | ret = PTR_ERR(trans); |
| 454 | goto out_unlock; |
| 455 | } |
| 456 | |
| 457 | btrfs_sync_inode_flags_to_i_flags(inode); |
| 458 | inode_inc_iversion(inode); |
| 459 | inode->i_ctime = current_time(inode); |
| 460 | ret = btrfs_update_inode(trans, root, inode); |
| 461 | |
| 462 | btrfs_end_transaction(trans); |
| 463 | |
| 464 | out_unlock: |
| 465 | if (ret) { |
| 466 | binode->flags = old_flags; |
| 467 | inode->i_flags = old_i_flags; |
| 468 | } |
| 469 | |
| 470 | inode_unlock(inode); |
| 471 | mnt_drop_write_file(file); |
| 472 | |
| 473 | return ret; |
| 474 | } |
| 475 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 476 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) |
| 477 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 478 | struct inode *inode = file_inode(file); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 479 | |
| 480 | return put_user(inode->i_generation, arg); |
| 481 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 482 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 483 | static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info, |
| 484 | void __user *arg) |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 485 | { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 486 | struct btrfs_device *device; |
| 487 | struct request_queue *q; |
| 488 | struct fstrim_range range; |
| 489 | u64 minlen = ULLONG_MAX; |
| 490 | u64 num_devices = 0; |
| 491 | int ret; |
| 492 | |
| 493 | if (!capable(CAP_SYS_ADMIN)) |
| 494 | return -EPERM; |
| 495 | |
Filipe Manana | f35f06c | 2019-03-26 10:49:56 +0000 | [diff] [blame] | 496 | /* |
| 497 | * If the fs is mounted with nologreplay, which requires it to be |
| 498 | * mounted in RO mode as well, we can not allow discard on free space |
| 499 | * inside block groups, because log trees refer to extents that are not |
| 500 | * pinned in a block group's free space cache (pinning the extents is |
| 501 | * precisely the first phase of replaying a log tree). |
| 502 | */ |
| 503 | if (btrfs_test_opt(fs_info, NOLOGREPLAY)) |
| 504 | return -EROFS; |
| 505 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 506 | rcu_read_lock(); |
| 507 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, |
| 508 | dev_list) { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 509 | if (!device->bdev) |
| 510 | continue; |
| 511 | q = bdev_get_queue(device->bdev); |
| 512 | if (blk_queue_discard(q)) { |
| 513 | num_devices++; |
Seraphime Kirkovski | 50d0446 | 2016-12-15 14:38:28 +0100 | [diff] [blame] | 514 | minlen = min_t(u64, q->limits.discard_granularity, |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 515 | minlen); |
| 516 | } |
| 517 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 518 | rcu_read_unlock(); |
Lukas Czerner | f4c697e | 2011-09-05 16:34:54 +0200 | [diff] [blame] | 519 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 520 | if (!num_devices) |
| 521 | return -EOPNOTSUPP; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 522 | if (copy_from_user(&range, arg, sizeof(range))) |
| 523 | return -EFAULT; |
Qu Wenruo | 6ba9fc8 | 2018-09-07 14:16:24 +0800 | [diff] [blame] | 524 | |
| 525 | /* |
| 526 | * NOTE: Don't truncate the range using super->total_bytes. Bytenr of |
| 527 | * block group is in the logical address space, which can be any |
| 528 | * sectorsize aligned bytenr in the range [0, U64_MAX]. |
| 529 | */ |
| 530 | if (range.len < fs_info->sb->s_blocksize) |
Lukas Czerner | f4c697e | 2011-09-05 16:34:54 +0200 | [diff] [blame] | 531 | return -EINVAL; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 532 | |
| 533 | range.minlen = max(range.minlen, minlen); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 534 | ret = btrfs_trim_fs(fs_info, &range); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 535 | if (ret < 0) |
| 536 | return ret; |
| 537 | |
| 538 | if (copy_to_user(arg, &range, sizeof(range))) |
| 539 | return -EFAULT; |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
David Sterba | e1f60a6 | 2019-10-01 19:57:39 +0200 | [diff] [blame] | 544 | int __pure btrfs_is_empty_uuid(u8 *uuid) |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 545 | { |
Chris Mason | 46e0f66 | 2013-11-15 12:14:55 +0100 | [diff] [blame] | 546 | int i; |
| 547 | |
| 548 | for (i = 0; i < BTRFS_UUID_SIZE; i++) { |
| 549 | if (uuid[i]) |
| 550 | return 0; |
| 551 | } |
| 552 | return 1; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 555 | static noinline int create_subvol(struct inode *dir, |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 556 | struct dentry *dentry, |
David Sterba | 52f75f4 | 2017-02-14 18:33:53 +0100 | [diff] [blame] | 557 | const char *name, int namelen, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 558 | u64 *async_transid, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 559 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 560 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 561 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 562 | struct btrfs_trans_handle *trans; |
| 563 | struct btrfs_key key; |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 564 | struct btrfs_root_item *root_item; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 565 | struct btrfs_inode_item *inode_item; |
| 566 | struct extent_buffer *leaf; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 567 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 568 | struct btrfs_root *new_root; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 569 | struct btrfs_block_rsv block_rsv; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 570 | struct timespec64 cur_time = current_time(dir); |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 571 | struct inode *inode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 572 | int ret; |
| 573 | int err; |
| 574 | u64 objectid; |
| 575 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 576 | u64 index = 0; |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 577 | uuid_le new_uuid; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 578 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 579 | root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); |
| 580 | if (!root_item) |
| 581 | return -ENOMEM; |
| 582 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 583 | ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid); |
Al Viro | 2fbe8c8 | 2011-07-16 21:38:06 -0400 | [diff] [blame] | 584 | if (ret) |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 585 | goto fail_free; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 586 | |
Qu Wenruo | e09fe2d | 2015-02-27 16:24:23 +0800 | [diff] [blame] | 587 | /* |
| 588 | * 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] | 589 | * screwed up since it assumes subvolume qgroup's level to be 0. |
Qu Wenruo | e09fe2d | 2015-02-27 16:24:23 +0800 | [diff] [blame] | 590 | */ |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 591 | if (btrfs_qgroup_level(objectid)) { |
| 592 | ret = -ENOSPC; |
| 593 | goto fail_free; |
| 594 | } |
Qu Wenruo | e09fe2d | 2015-02-27 16:24:23 +0800 | [diff] [blame] | 595 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 596 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 597 | /* |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 598 | * The same as the snapshot creation, please see the comment |
| 599 | * of create_snapshot(). |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 600 | */ |
Gu JinXiang | c4c129d | 2018-05-30 11:00:38 +0800 | [diff] [blame] | 601 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 602 | if (ret) |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 603 | goto fail_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 604 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 605 | trans = btrfs_start_transaction(root, 0); |
| 606 | if (IS_ERR(trans)) { |
| 607 | ret = PTR_ERR(trans); |
David Sterba | 7775c81 | 2017-02-10 19:18:18 +0100 | [diff] [blame] | 608 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 609 | goto fail_free; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 610 | } |
| 611 | trans->block_rsv = &block_rsv; |
| 612 | trans->bytes_reserved = block_rsv.size; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 613 | |
Lu Fengqi | a9377422 | 2018-07-18 14:45:41 +0800 | [diff] [blame] | 614 | ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit); |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 615 | if (ret) |
| 616 | goto fail; |
| 617 | |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 618 | 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] | 619 | if (IS_ERR(leaf)) { |
| 620 | ret = PTR_ERR(leaf); |
| 621 | goto fail; |
| 622 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 623 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 624 | btrfs_mark_buffer_dirty(leaf); |
| 625 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 626 | inode_item = &root_item->inode; |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 627 | btrfs_set_stack_inode_generation(inode_item, 1); |
| 628 | btrfs_set_stack_inode_size(inode_item, 3); |
| 629 | btrfs_set_stack_inode_nlink(inode_item, 1); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 630 | btrfs_set_stack_inode_nbytes(inode_item, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 631 | fs_info->nodesize); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 632 | btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 633 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 634 | btrfs_set_root_flags(root_item, 0); |
| 635 | btrfs_set_root_limit(root_item, 0); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 636 | btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); |
Li Zefan | 08fe4db | 2011-03-28 02:01:25 +0000 | [diff] [blame] | 637 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 638 | btrfs_set_root_bytenr(root_item, leaf->start); |
| 639 | btrfs_set_root_generation(root_item, trans->transid); |
| 640 | btrfs_set_root_level(root_item, 0); |
| 641 | btrfs_set_root_refs(root_item, 1); |
| 642 | btrfs_set_root_used(root_item, leaf->len); |
| 643 | btrfs_set_root_last_snapshot(root_item, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 644 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 645 | btrfs_set_root_generation_v2(root_item, |
| 646 | btrfs_root_generation(root_item)); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 647 | uuid_le_gen(&new_uuid); |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 648 | memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); |
| 649 | btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); |
| 650 | btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); |
| 651 | root_item->ctime = root_item->otime; |
| 652 | btrfs_set_root_ctransid(root_item, trans->transid); |
| 653 | btrfs_set_root_otransid(root_item, trans->transid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 654 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 655 | btrfs_tree_unlock(leaf); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 656 | free_extent_buffer(leaf); |
| 657 | leaf = NULL; |
| 658 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 659 | btrfs_set_root_dirid(root_item, new_dirid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 660 | |
| 661 | key.objectid = objectid; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 662 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 663 | key.type = BTRFS_ROOT_ITEM_KEY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 664 | ret = btrfs_insert_root(trans, fs_info->tree_root, &key, |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 665 | root_item); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 666 | if (ret) |
| 667 | goto fail; |
| 668 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 669 | key.offset = (u64)-1; |
Josef Bacik | 3619c94 | 2020-01-24 09:32:24 -0500 | [diff] [blame] | 670 | new_root = btrfs_get_fs_root(fs_info, &key, true); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 671 | if (IS_ERR(new_root)) { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 672 | ret = PTR_ERR(new_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 673 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 674 | goto fail; |
| 675 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 676 | |
| 677 | btrfs_record_root_in_trans(trans, new_root); |
| 678 | |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 679 | ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 680 | btrfs_put_root(new_root); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 681 | if (ret) { |
| 682 | /* We potentially lose an unused inode item here */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 683 | btrfs_abort_transaction(trans, ret); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 684 | goto fail; |
| 685 | } |
| 686 | |
Chandan Rajendra | f32e48e | 2016-01-07 18:56:59 +0530 | [diff] [blame] | 687 | mutex_lock(&new_root->objectid_mutex); |
| 688 | new_root->highest_objectid = new_dirid; |
| 689 | mutex_unlock(&new_root->objectid_mutex); |
| 690 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 691 | /* |
| 692 | * insert the directory item |
| 693 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 694 | ret = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 695 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 696 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 697 | goto fail; |
| 698 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 699 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 700 | ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 701 | BTRFS_FT_DIR, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 702 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 703 | btrfs_abort_transaction(trans, ret); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 704 | goto fail; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 705 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 706 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 707 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); |
Yan Zheng | 52c2617 | 2009-01-05 15:43:43 -0500 | [diff] [blame] | 708 | ret = btrfs_update_inode(trans, root, dir); |
Josef Bacik | c7e54b5 | 2019-12-06 09:37:15 -0500 | [diff] [blame] | 709 | if (ret) { |
| 710 | btrfs_abort_transaction(trans, ret); |
| 711 | goto fail; |
| 712 | } |
Yan Zheng | 52c2617 | 2009-01-05 15:43:43 -0500 | [diff] [blame] | 713 | |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 714 | ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 715 | btrfs_ino(BTRFS_I(dir)), index, name, namelen); |
Josef Bacik | c7e54b5 | 2019-12-06 09:37:15 -0500 | [diff] [blame] | 716 | if (ret) { |
| 717 | btrfs_abort_transaction(trans, ret); |
| 718 | goto fail; |
| 719 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 720 | |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 721 | ret = btrfs_uuid_tree_add(trans, root_item->uuid, |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 722 | BTRFS_UUID_KEY_SUBVOL, objectid); |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 723 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 724 | btrfs_abort_transaction(trans, ret); |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 725 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 726 | fail: |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 727 | kfree(root_item); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 728 | trans->block_rsv = NULL; |
| 729 | trans->bytes_reserved = 0; |
David Sterba | 7775c81 | 2017-02-10 19:18:18 +0100 | [diff] [blame] | 730 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); |
Liu Bo | de6e820 | 2014-01-09 14:57:06 +0800 | [diff] [blame] | 731 | |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 732 | if (async_transid) { |
| 733 | *async_transid = trans->transid; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 734 | err = btrfs_commit_transaction_async(trans, 1); |
Miao Xie | 00d71c9 | 2013-03-04 09:45:06 +0000 | [diff] [blame] | 735 | if (err) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 736 | err = btrfs_commit_transaction(trans); |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 737 | } else { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 738 | err = btrfs_commit_transaction(trans); |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 739 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 740 | if (err && !ret) |
| 741 | ret = err; |
Chris Mason | 1a65e24 | 2013-02-06 12:06:02 -0500 | [diff] [blame] | 742 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 743 | if (!ret) { |
| 744 | inode = btrfs_lookup_dentry(dir, dentry); |
Liu Bo | de6e820 | 2014-01-09 14:57:06 +0800 | [diff] [blame] | 745 | if (IS_ERR(inode)) |
| 746 | return PTR_ERR(inode); |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 747 | d_instantiate(dentry, inode); |
| 748 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 749 | return ret; |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 750 | |
| 751 | fail_free: |
| 752 | kfree(root_item); |
| 753 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 754 | } |
| 755 | |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 756 | static int create_snapshot(struct btrfs_root *root, struct inode *dir, |
David Sterba | 61d7e4c | 2017-02-10 19:54:06 +0100 | [diff] [blame] | 757 | struct dentry *dentry, |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 758 | u64 *async_transid, bool readonly, |
| 759 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 760 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 761 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 762 | struct inode *inode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 763 | struct btrfs_pending_snapshot *pending_snapshot; |
| 764 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 765 | int ret; |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 766 | bool snapshot_force_cow = false; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 767 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 768 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 769 | return -EINVAL; |
| 770 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 771 | if (atomic_read(&root->nr_swapfiles)) { |
| 772 | btrfs_warn(fs_info, |
| 773 | "cannot snapshot subvolume with active swapfile"); |
| 774 | return -ETXTBSY; |
| 775 | } |
| 776 | |
David Sterba | 23269bf | 2017-02-13 11:03:44 +0100 | [diff] [blame] | 777 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); |
David Sterba | a1ee736 | 2015-11-10 18:53:56 +0100 | [diff] [blame] | 778 | if (!pending_snapshot) |
| 779 | return -ENOMEM; |
| 780 | |
David Sterba | b0c0ea6 | 2015-11-10 18:54:00 +0100 | [diff] [blame] | 781 | pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), |
David Sterba | 23269bf | 2017-02-13 11:03:44 +0100 | [diff] [blame] | 782 | GFP_KERNEL); |
David Sterba | 8546b57 | 2015-11-10 18:54:03 +0100 | [diff] [blame] | 783 | pending_snapshot->path = btrfs_alloc_path(); |
| 784 | if (!pending_snapshot->root_item || !pending_snapshot->path) { |
David Sterba | b0c0ea6 | 2015-11-10 18:54:00 +0100 | [diff] [blame] | 785 | ret = -ENOMEM; |
| 786 | goto free_pending; |
| 787 | } |
| 788 | |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 789 | /* |
| 790 | * Force new buffered writes to reserve space even when NOCOW is |
| 791 | * possible. This is to avoid later writeback (running dealloc) to |
| 792 | * fallback to COW mode and unexpectedly fail with ENOSPC. |
| 793 | */ |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 794 | atomic_inc(&root->will_be_snapshotted); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 795 | smp_mb__after_atomic(); |
Liu Bo | 45bac0f | 2017-09-01 16:14:29 -0600 | [diff] [blame] | 796 | /* wait for no snapshot writes */ |
| 797 | wait_event(root->subv_writers->wait, |
| 798 | percpu_counter_sum(&root->subv_writers->counter) == 0); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 799 | |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 800 | ret = btrfs_start_delalloc_snapshot(root); |
Miao Xie | 6a03843 | 2013-05-15 07:48:24 +0000 | [diff] [blame] | 801 | if (ret) |
David Sterba | a1ee736 | 2015-11-10 18:53:56 +0100 | [diff] [blame] | 802 | goto dec_and_free; |
Miao Xie | 6a03843 | 2013-05-15 07:48:24 +0000 | [diff] [blame] | 803 | |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 804 | /* |
| 805 | * All previous writes have started writeback in NOCOW mode, so now |
| 806 | * we force future writes to fallback to COW mode during snapshot |
| 807 | * creation. |
| 808 | */ |
| 809 | atomic_inc(&root->snapshot_force_cow); |
| 810 | snapshot_force_cow = true; |
| 811 | |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 812 | btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); |
Miao Xie | 6a03843 | 2013-05-15 07:48:24 +0000 | [diff] [blame] | 813 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 814 | btrfs_init_block_rsv(&pending_snapshot->block_rsv, |
| 815 | BTRFS_BLOCK_RSV_TEMP); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 816 | /* |
| 817 | * 1 - parent dir inode |
| 818 | * 2 - dir entries |
| 819 | * 1 - root item |
| 820 | * 2 - root ref/backref |
| 821 | * 1 - root of snapshot |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 822 | * 1 - UUID item |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 823 | */ |
| 824 | ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 825 | &pending_snapshot->block_rsv, 8, |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 826 | false); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 827 | if (ret) |
David Sterba | a1ee736 | 2015-11-10 18:53:56 +0100 | [diff] [blame] | 828 | goto dec_and_free; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 829 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 830 | pending_snapshot->dentry = dentry; |
| 831 | pending_snapshot->root = root; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 832 | pending_snapshot->readonly = readonly; |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 833 | pending_snapshot->dir = dir; |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 834 | pending_snapshot->inherit = inherit; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 835 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 836 | trans = btrfs_start_transaction(root, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 837 | if (IS_ERR(trans)) { |
| 838 | ret = PTR_ERR(trans); |
| 839 | goto fail; |
| 840 | } |
| 841 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 842 | spin_lock(&fs_info->trans_lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 843 | list_add(&pending_snapshot->list, |
| 844 | &trans->transaction->pending_snapshots); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 845 | spin_unlock(&fs_info->trans_lock); |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 846 | if (async_transid) { |
| 847 | *async_transid = trans->transid; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 848 | ret = btrfs_commit_transaction_async(trans, 1); |
Miao Xie | 00d71c9 | 2013-03-04 09:45:06 +0000 | [diff] [blame] | 849 | if (ret) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 850 | ret = btrfs_commit_transaction(trans); |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 851 | } else { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 852 | ret = btrfs_commit_transaction(trans); |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 853 | } |
Miao Xie | aec8030 | 2013-03-04 09:44:29 +0000 | [diff] [blame] | 854 | if (ret) |
Josef Bacik | c37b2b6 | 2012-10-22 15:51:44 -0400 | [diff] [blame] | 855 | goto fail; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 856 | |
| 857 | ret = pending_snapshot->error; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 858 | if (ret) |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 859 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 860 | |
Chris Mason | d379730 | 2014-10-15 13:50:56 -0700 | [diff] [blame] | 861 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
| 862 | if (ret) |
| 863 | goto fail; |
| 864 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 865 | inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 866 | if (IS_ERR(inode)) { |
| 867 | ret = PTR_ERR(inode); |
| 868 | goto fail; |
| 869 | } |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 870 | |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 871 | d_instantiate(dentry, inode); |
| 872 | ret = 0; |
| 873 | fail: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 874 | btrfs_put_root(pending_snapshot->snap); |
David Sterba | 7775c81 | 2017-02-10 19:18:18 +0100 | [diff] [blame] | 875 | btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv); |
David Sterba | a1ee736 | 2015-11-10 18:53:56 +0100 | [diff] [blame] | 876 | dec_and_free: |
Robbie Ko | 8ecebf4d | 2018-08-06 10:30:30 +0800 | [diff] [blame] | 877 | if (snapshot_force_cow) |
| 878 | atomic_dec(&root->snapshot_force_cow); |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 879 | if (atomic_dec_and_test(&root->will_be_snapshotted)) |
Peter Zijlstra | 4625956 | 2018-03-15 11:43:08 +0100 | [diff] [blame] | 880 | wake_up_var(&root->will_be_snapshotted); |
David Sterba | b0c0ea6 | 2015-11-10 18:54:00 +0100 | [diff] [blame] | 881 | free_pending: |
| 882 | kfree(pending_snapshot->root_item); |
David Sterba | 8546b57 | 2015-11-10 18:54:03 +0100 | [diff] [blame] | 883 | btrfs_free_path(pending_snapshot->path); |
David Sterba | a1ee736 | 2015-11-10 18:53:56 +0100 | [diff] [blame] | 884 | kfree(pending_snapshot); |
| 885 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 886 | return ret; |
| 887 | } |
| 888 | |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 889 | /* copy of may_delete in fs/namei.c() |
| 890 | * Check whether we can remove a link victim from directory dir, check |
| 891 | * whether the type of victim is right. |
| 892 | * 1. We can't do it if dir is read-only (done in permission()) |
| 893 | * 2. We should have write and exec permissions on dir |
| 894 | * 3. We can't remove anything from append-only dir |
| 895 | * 4. We can't do anything with immutable dir (done in permission()) |
| 896 | * 5. If the sticky bit on dir is set we should either |
| 897 | * a. be owner of dir, or |
| 898 | * b. be owner of victim, or |
| 899 | * c. have CAP_FOWNER capability |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 900 | * 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] | 901 | * links pointing to it. |
| 902 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 903 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 904 | * 9. We can't remove a root or mountpoint. |
| 905 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by |
| 906 | * nfs_async_unlink(). |
| 907 | */ |
| 908 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 909 | 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] | 910 | { |
| 911 | int error; |
| 912 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 913 | if (d_really_is_negative(victim)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 914 | return -ENOENT; |
| 915 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 916 | BUG_ON(d_inode(victim->d_parent) != dir); |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 917 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 918 | |
| 919 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 920 | if (error) |
| 921 | return error; |
| 922 | if (IS_APPEND(dir)) |
| 923 | return -EPERM; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 924 | if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) || |
| 925 | IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim))) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 926 | return -EPERM; |
| 927 | if (isdir) { |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 928 | if (!d_is_dir(victim)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 929 | return -ENOTDIR; |
| 930 | if (IS_ROOT(victim)) |
| 931 | return -EBUSY; |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 932 | } else if (d_is_dir(victim)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 933 | return -EISDIR; |
| 934 | if (IS_DEADDIR(dir)) |
| 935 | return -ENOENT; |
| 936 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 937 | return -EBUSY; |
| 938 | return 0; |
| 939 | } |
| 940 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 941 | /* copy of may_create in fs/namei.c() */ |
| 942 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) |
| 943 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 944 | if (d_really_is_positive(child)) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 945 | return -EEXIST; |
| 946 | if (IS_DEADDIR(dir)) |
| 947 | return -ENOENT; |
| 948 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * Create a new subvolume below @parent. This is largely modeled after |
| 953 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup |
| 954 | * inside this filesystem so it's quite a bit simpler. |
| 955 | */ |
Al Viro | 9287209 | 2016-11-20 19:34:31 -0500 | [diff] [blame] | 956 | static noinline int btrfs_mksubvol(const struct path *parent, |
David Sterba | 52f75f4 | 2017-02-14 18:33:53 +0100 | [diff] [blame] | 957 | const char *name, int namelen, |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 958 | struct btrfs_root *snap_src, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 959 | u64 *async_transid, bool readonly, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 960 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 961 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 962 | struct inode *dir = d_inode(parent->dentry); |
| 963 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 964 | struct dentry *dentry; |
| 965 | int error; |
| 966 | |
Al Viro | 0023541 | 2016-05-26 00:05:12 -0400 | [diff] [blame] | 967 | error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
| 968 | if (error == -EINTR) |
| 969 | return error; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 970 | |
| 971 | dentry = lookup_one_len(name, parent->dentry, namelen); |
| 972 | error = PTR_ERR(dentry); |
| 973 | if (IS_ERR(dentry)) |
| 974 | goto out_unlock; |
| 975 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 976 | error = btrfs_may_create(dir, dentry); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 977 | if (error) |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 978 | goto out_dput; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 979 | |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 980 | /* |
| 981 | * even if this name doesn't exist, we may get hash collisions. |
| 982 | * check for them now when we can safely fail |
| 983 | */ |
| 984 | error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, |
| 985 | dir->i_ino, name, |
| 986 | namelen); |
| 987 | if (error) |
| 988 | goto out_dput; |
| 989 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 990 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 991 | |
| 992 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) |
| 993 | goto out_up_read; |
| 994 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 995 | if (snap_src) { |
David Sterba | 61d7e4c | 2017-02-10 19:54:06 +0100 | [diff] [blame] | 996 | error = create_snapshot(snap_src, dir, dentry, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 997 | async_transid, readonly, inherit); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 998 | } else { |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 999 | error = create_subvol(dir, dentry, name, namelen, |
| 1000 | async_transid, inherit); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1001 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1002 | if (!error) |
| 1003 | fsnotify_mkdir(dir, dentry); |
| 1004 | out_up_read: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1005 | up_read(&fs_info->subvol_sem); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1006 | out_dput: |
| 1007 | dput(dentry); |
| 1008 | out_unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1009 | inode_unlock(dir); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1010 | return error; |
| 1011 | } |
| 1012 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1013 | /* |
| 1014 | * When we're defragging a range, we don't want to kick it off again |
| 1015 | * if it is really just waiting for delalloc to send it down. |
| 1016 | * If we find a nice big extent or delalloc range for the bytes in the |
| 1017 | * file you want to defrag, we return 0 to let you know to skip this |
| 1018 | * part of the file |
| 1019 | */ |
David Sterba | aab110a | 2014-07-29 17:32:10 +0200 | [diff] [blame] | 1020 | 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] | 1021 | { |
| 1022 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 1023 | struct extent_map *em = NULL; |
| 1024 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 1025 | u64 end; |
| 1026 | |
| 1027 | read_lock(&em_tree->lock); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1028 | em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1029 | read_unlock(&em_tree->lock); |
| 1030 | |
| 1031 | if (em) { |
| 1032 | end = extent_map_end(em); |
| 1033 | free_extent_map(em); |
| 1034 | if (end - offset > thresh) |
| 1035 | return 0; |
| 1036 | } |
| 1037 | /* if we already have a nice delalloc here, just stop */ |
| 1038 | thresh /= 2; |
| 1039 | end = count_range_bits(io_tree, &offset, offset + thresh, |
| 1040 | thresh, EXTENT_DELALLOC, 1); |
| 1041 | if (end >= thresh) |
| 1042 | return 0; |
| 1043 | return 1; |
| 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * helper function to walk through a file and find extents |
| 1048 | * newer than a specific transid, and smaller than thresh. |
| 1049 | * |
| 1050 | * This is used by the defragging code to find new and small |
| 1051 | * extents |
| 1052 | */ |
| 1053 | static int find_new_extents(struct btrfs_root *root, |
| 1054 | struct inode *inode, u64 newer_than, |
David Sterba | aab110a | 2014-07-29 17:32:10 +0200 | [diff] [blame] | 1055 | u64 *off, u32 thresh) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1056 | { |
| 1057 | struct btrfs_path *path; |
| 1058 | struct btrfs_key min_key; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1059 | struct extent_buffer *leaf; |
| 1060 | struct btrfs_file_extent_item *extent; |
| 1061 | int type; |
| 1062 | int ret; |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1063 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1064 | |
| 1065 | path = btrfs_alloc_path(); |
| 1066 | if (!path) |
| 1067 | return -ENOMEM; |
| 1068 | |
David Sterba | a4689d2 | 2011-05-31 17:08:14 +0000 | [diff] [blame] | 1069 | min_key.objectid = ino; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1070 | min_key.type = BTRFS_EXTENT_DATA_KEY; |
| 1071 | min_key.offset = *off; |
| 1072 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 1073 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 1074 | ret = btrfs_search_forward(root, &min_key, path, newer_than); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1075 | if (ret != 0) |
| 1076 | goto none; |
Filipe Manana | f094c9bd | 2014-03-12 01:28:24 +0000 | [diff] [blame] | 1077 | process_slot: |
David Sterba | a4689d2 | 2011-05-31 17:08:14 +0000 | [diff] [blame] | 1078 | if (min_key.objectid != ino) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1079 | goto none; |
| 1080 | if (min_key.type != BTRFS_EXTENT_DATA_KEY) |
| 1081 | goto none; |
| 1082 | |
| 1083 | leaf = path->nodes[0]; |
| 1084 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1085 | struct btrfs_file_extent_item); |
| 1086 | |
| 1087 | type = btrfs_file_extent_type(leaf, extent); |
| 1088 | if (type == BTRFS_FILE_EXTENT_REG && |
| 1089 | btrfs_file_extent_num_bytes(leaf, extent) < thresh && |
| 1090 | check_defrag_in_cache(inode, min_key.offset, thresh)) { |
| 1091 | *off = min_key.offset; |
| 1092 | btrfs_free_path(path); |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
Filipe Manana | f094c9bd | 2014-03-12 01:28:24 +0000 | [diff] [blame] | 1096 | path->slots[0]++; |
| 1097 | if (path->slots[0] < btrfs_header_nritems(leaf)) { |
| 1098 | btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); |
| 1099 | goto process_slot; |
| 1100 | } |
| 1101 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1102 | if (min_key.offset == (u64)-1) |
| 1103 | goto none; |
| 1104 | |
| 1105 | min_key.offset++; |
| 1106 | btrfs_release_path(path); |
| 1107 | } |
| 1108 | none: |
| 1109 | btrfs_free_path(path); |
| 1110 | return -ENOENT; |
| 1111 | } |
| 1112 | |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1113 | static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1114 | { |
| 1115 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1116 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1117 | struct extent_map *em; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1118 | u64 len = PAGE_SIZE; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1119 | |
| 1120 | /* |
| 1121 | * hopefully we have this extent in the tree already, try without |
| 1122 | * the full extent lock |
| 1123 | */ |
| 1124 | read_lock(&em_tree->lock); |
| 1125 | em = lookup_extent_mapping(em_tree, start, len); |
| 1126 | read_unlock(&em_tree->lock); |
| 1127 | |
| 1128 | if (!em) { |
Filipe Manana | 308d980 | 2014-03-11 13:56:15 +0000 | [diff] [blame] | 1129 | struct extent_state *cached = NULL; |
| 1130 | u64 end = start + len - 1; |
| 1131 | |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1132 | /* get the big lock and read metadata off disk */ |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1133 | lock_extent_bits(io_tree, start, end, &cached); |
Omar Sandoval | 39b07b5 | 2019-12-02 17:34:23 -0800 | [diff] [blame] | 1134 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1135 | unlock_extent_cached(io_tree, start, end, &cached); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1136 | |
Dan Carpenter | 6cf8bfb | 2010-03-20 11:22:10 +0000 | [diff] [blame] | 1137 | if (IS_ERR(em)) |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1138 | return NULL; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1139 | } |
| 1140 | |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1141 | return em; |
| 1142 | } |
| 1143 | |
| 1144 | static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) |
| 1145 | { |
| 1146 | struct extent_map *next; |
| 1147 | bool ret = true; |
| 1148 | |
| 1149 | /* this is the last extent */ |
| 1150 | if (em->start + em->len >= i_size_read(inode)) |
| 1151 | return false; |
| 1152 | |
| 1153 | next = defrag_lookup_extent(inode, em->start + em->len); |
Chris Mason | e9512d7 | 2014-08-26 13:55:54 -0700 | [diff] [blame] | 1154 | if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) |
| 1155 | ret = false; |
| 1156 | else if ((em->block_start + em->block_len == next->block_start) && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1157 | (em->block_len > SZ_128K && next->block_len > SZ_128K)) |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1158 | ret = false; |
| 1159 | |
| 1160 | free_extent_map(next); |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
David Sterba | aab110a | 2014-07-29 17:32:10 +0200 | [diff] [blame] | 1164 | static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, |
Andrew Mahone | a43a211 | 2012-06-19 21:08:32 -0400 | [diff] [blame] | 1165 | u64 *last_len, u64 *skip, u64 *defrag_end, |
| 1166 | int compress) |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1167 | { |
| 1168 | struct extent_map *em; |
| 1169 | int ret = 1; |
| 1170 | bool next_mergeable = true; |
Liu Bo | 4a3560c | 2015-08-07 16:48:41 +0800 | [diff] [blame] | 1171 | bool prev_mergeable = true; |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1172 | |
| 1173 | /* |
| 1174 | * make sure that once we start defragging an extent, we keep on |
| 1175 | * defragging it |
| 1176 | */ |
| 1177 | if (start < *defrag_end) |
| 1178 | return 1; |
| 1179 | |
| 1180 | *skip = 0; |
| 1181 | |
| 1182 | em = defrag_lookup_extent(inode, start); |
| 1183 | if (!em) |
| 1184 | return 0; |
| 1185 | |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1186 | /* this will cover holes, and inline extents */ |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1187 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1188 | ret = 0; |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1189 | goto out; |
| 1190 | } |
| 1191 | |
Liu Bo | 4a3560c | 2015-08-07 16:48:41 +0800 | [diff] [blame] | 1192 | if (!*defrag_end) |
| 1193 | prev_mergeable = false; |
| 1194 | |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1195 | next_mergeable = defrag_check_next_extent(inode, em); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1196 | /* |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1197 | * we hit a real extent, if it is big or the next extent is not a |
| 1198 | * real extent, don't bother defragging it |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1199 | */ |
Andrew Mahone | a43a211 | 2012-06-19 21:08:32 -0400 | [diff] [blame] | 1200 | if (!compress && (*last_len == 0 || *last_len >= thresh) && |
Liu Bo | 4a3560c | 2015-08-07 16:48:41 +0800 | [diff] [blame] | 1201 | (em->len >= thresh || (!next_mergeable && !prev_mergeable))) |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1202 | ret = 0; |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1203 | out: |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1204 | /* |
| 1205 | * last_len ends up being a counter of how many bytes we've defragged. |
| 1206 | * every time we choose not to defrag an extent, we reset *last_len |
| 1207 | * so that the next tiny extent will force a defrag. |
| 1208 | * |
| 1209 | * The end result of this is that tiny extents before a single big |
| 1210 | * extent will force at least part of that big extent to be defragged. |
| 1211 | */ |
| 1212 | if (ret) { |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1213 | *defrag_end = extent_map_end(em); |
| 1214 | } else { |
| 1215 | *last_len = 0; |
| 1216 | *skip = extent_map_end(em); |
| 1217 | *defrag_end = 0; |
| 1218 | } |
| 1219 | |
| 1220 | free_extent_map(em); |
| 1221 | return ret; |
| 1222 | } |
| 1223 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1224 | /* |
| 1225 | * it doesn't do much good to defrag one or two pages |
| 1226 | * at a time. This pulls in a nice chunk of pages |
| 1227 | * to COW and defrag. |
| 1228 | * |
| 1229 | * It also makes sure the delalloc code has enough |
| 1230 | * dirty data to avoid making new small extents as part |
| 1231 | * of the defrag |
| 1232 | * |
| 1233 | * It's a good idea to start RA on this range |
| 1234 | * before calling this. |
| 1235 | */ |
| 1236 | static int cluster_pages_for_defrag(struct inode *inode, |
| 1237 | struct page **pages, |
| 1238 | unsigned long start_index, |
Justin Maggard | c41570c | 2014-01-21 11:18:29 -0800 | [diff] [blame] | 1239 | unsigned long num_pages) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1240 | { |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1241 | unsigned long file_end; |
| 1242 | u64 isize = i_size_read(inode); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1243 | u64 page_start; |
| 1244 | u64 page_end; |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1245 | u64 page_cnt; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1246 | int ret; |
| 1247 | int i; |
| 1248 | int i_done; |
| 1249 | struct btrfs_ordered_extent *ordered; |
| 1250 | struct extent_state *cached_state = NULL; |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1251 | struct extent_io_tree *tree; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 1252 | struct extent_changeset *data_reserved = NULL; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 1253 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1254 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1255 | file_end = (isize - 1) >> PAGE_SHIFT; |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1256 | if (!isize || start_index > file_end) |
| 1257 | return 0; |
| 1258 | |
| 1259 | 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] | 1260 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 1261 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1262 | start_index << PAGE_SHIFT, |
| 1263 | page_cnt << PAGE_SHIFT); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1264 | if (ret) |
| 1265 | return ret; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1266 | i_done = 0; |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1267 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1268 | |
| 1269 | /* step one, lock all the pages */ |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1270 | for (i = 0; i < page_cnt; i++) { |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1271 | struct page *page; |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1272 | again: |
Josef Bacik | a94733d | 2011-07-11 10:47:06 -0400 | [diff] [blame] | 1273 | page = find_or_create_page(inode->i_mapping, |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1274 | start_index + i, mask); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1275 | if (!page) |
| 1276 | break; |
| 1277 | |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1278 | page_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1279 | page_end = page_start + PAGE_SIZE - 1; |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1280 | while (1) { |
Filipe Manana | 308d980 | 2014-03-11 13:56:15 +0000 | [diff] [blame] | 1281 | lock_extent_bits(tree, page_start, page_end, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1282 | &cached_state); |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1283 | ordered = btrfs_lookup_ordered_extent(inode, |
| 1284 | page_start); |
Filipe Manana | 308d980 | 2014-03-11 13:56:15 +0000 | [diff] [blame] | 1285 | unlock_extent_cached(tree, page_start, page_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1286 | &cached_state); |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1287 | if (!ordered) |
| 1288 | break; |
| 1289 | |
| 1290 | unlock_page(page); |
| 1291 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1292 | btrfs_put_ordered_extent(ordered); |
| 1293 | lock_page(page); |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1294 | /* |
| 1295 | * we unlocked the page above, so we need check if |
| 1296 | * it was released or not. |
| 1297 | */ |
| 1298 | if (page->mapping != inode->i_mapping) { |
| 1299 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1300 | put_page(page); |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1301 | goto again; |
| 1302 | } |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1303 | } |
| 1304 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1305 | if (!PageUptodate(page)) { |
| 1306 | btrfs_readpage(NULL, page); |
| 1307 | lock_page(page); |
| 1308 | if (!PageUptodate(page)) { |
| 1309 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1310 | put_page(page); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1311 | ret = -EIO; |
| 1312 | break; |
| 1313 | } |
| 1314 | } |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1315 | |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1316 | if (page->mapping != inode->i_mapping) { |
| 1317 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1318 | put_page(page); |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1319 | goto again; |
| 1320 | } |
| 1321 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1322 | pages[i] = page; |
| 1323 | i_done++; |
| 1324 | } |
| 1325 | if (!i_done || ret) |
| 1326 | goto out; |
| 1327 | |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1328 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1329 | goto out; |
| 1330 | |
| 1331 | /* |
| 1332 | * so now we have a nice long stream of locked |
| 1333 | * and up to date pages, lets wait on them |
| 1334 | */ |
| 1335 | for (i = 0; i < i_done; i++) |
| 1336 | wait_on_page_writeback(pages[i]); |
| 1337 | |
| 1338 | page_start = page_offset(pages[0]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1339 | page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1340 | |
| 1341 | lock_extent_bits(&BTRFS_I(inode)->io_tree, |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1342 | page_start, page_end - 1, &cached_state); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1343 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 1344 | page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 1345 | EXTENT_DEFRAG, 0, 0, &cached_state); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1346 | |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1347 | if (i_done != page_cnt) { |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1348 | spin_lock(&BTRFS_I(inode)->lock); |
Su Yue | 28c4a3e | 2018-09-05 11:07:33 +0800 | [diff] [blame] | 1349 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1350 | spin_unlock(&BTRFS_I(inode)->lock); |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 1351 | btrfs_delalloc_release_space(inode, data_reserved, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1352 | start_index << PAGE_SHIFT, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 1353 | (page_cnt - i_done) << PAGE_SHIFT, true); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 1357 | 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] | 1358 | &cached_state); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1359 | |
| 1360 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1361 | page_start, page_end - 1, &cached_state); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1362 | |
| 1363 | for (i = 0; i < i_done; i++) { |
| 1364 | clear_page_dirty_for_io(pages[i]); |
| 1365 | ClearPageChecked(pages[i]); |
| 1366 | set_page_extent_mapped(pages[i]); |
| 1367 | set_page_dirty(pages[i]); |
| 1368 | unlock_page(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1369 | put_page(pages[i]); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1370 | } |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 1371 | btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 1372 | extent_changeset_free(data_reserved); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1373 | return i_done; |
| 1374 | out: |
| 1375 | for (i = 0; i < i_done; i++) { |
| 1376 | unlock_page(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1377 | put_page(pages[i]); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1378 | } |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 1379 | btrfs_delalloc_release_space(inode, data_reserved, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1380 | start_index << PAGE_SHIFT, |
Qu Wenruo | 43b1859 | 2017-12-12 15:34:32 +0800 | [diff] [blame] | 1381 | page_cnt << PAGE_SHIFT, true); |
Qu Wenruo | 8702ba9 | 2019-10-14 14:34:51 +0800 | [diff] [blame] | 1382 | btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 1383 | extent_changeset_free(data_reserved); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1384 | return ret; |
| 1385 | |
| 1386 | } |
| 1387 | |
| 1388 | int btrfs_defrag_file(struct inode *inode, struct file *file, |
| 1389 | struct btrfs_ioctl_defrag_range_args *range, |
| 1390 | u64 newer_than, unsigned long max_to_defrag) |
| 1391 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1392 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1393 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1394 | struct file_ra_state *ra = NULL; |
| 1395 | unsigned long last_index; |
Li Zefan | 151a31b | 2011-09-02 15:56:39 +0800 | [diff] [blame] | 1396 | u64 isize = i_size_read(inode); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1397 | u64 last_len = 0; |
| 1398 | u64 skip = 0; |
| 1399 | u64 defrag_end = 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1400 | u64 newer_off = range->start; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1401 | unsigned long i; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1402 | unsigned long ra_index = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1403 | int ret; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1404 | int defrag_count = 0; |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1405 | int compress_type = BTRFS_COMPRESS_ZLIB; |
David Sterba | aab110a | 2014-07-29 17:32:10 +0200 | [diff] [blame] | 1406 | u32 extent_thresh = range->extent_thresh; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1407 | unsigned long max_cluster = SZ_256K >> PAGE_SHIFT; |
Justin Maggard | c41570c | 2014-01-21 11:18:29 -0800 | [diff] [blame] | 1408 | unsigned long cluster = max_cluster; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1409 | u64 new_align = ~((u64)SZ_128K - 1); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1410 | struct page **pages = NULL; |
David Sterba | 1e2ef46 | 2017-07-17 20:01:59 +0200 | [diff] [blame] | 1411 | bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1412 | |
Liu Bo | 0abd5b1 | 2013-04-16 09:20:28 +0000 | [diff] [blame] | 1413 | if (isize == 0) |
| 1414 | return 0; |
| 1415 | |
| 1416 | if (range->start >= isize) |
| 1417 | return -EINVAL; |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1418 | |
David Sterba | 1e2ef46 | 2017-07-17 20:01:59 +0200 | [diff] [blame] | 1419 | if (do_compress) { |
Chengguang Xu | ce96b7f | 2019-10-10 15:59:57 +0800 | [diff] [blame] | 1420 | if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES) |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1421 | return -EINVAL; |
| 1422 | if (range->compress_type) |
| 1423 | compress_type = range->compress_type; |
| 1424 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1425 | |
Liu Bo | 0abd5b1 | 2013-04-16 09:20:28 +0000 | [diff] [blame] | 1426 | if (extent_thresh == 0) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1427 | extent_thresh = SZ_256K; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1428 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1429 | /* |
David Sterba | 0a52d10 | 2017-06-22 03:22:58 +0200 | [diff] [blame] | 1430 | * If we were not given a file, allocate a readahead context. As |
| 1431 | * readahead is just an optimization, defrag will work without it so |
| 1432 | * we don't error out. |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1433 | */ |
| 1434 | if (!file) { |
David Sterba | 63e727e | 2017-06-22 03:13:02 +0200 | [diff] [blame] | 1435 | ra = kzalloc(sizeof(*ra), GFP_KERNEL); |
David Sterba | 0a52d10 | 2017-06-22 03:22:58 +0200 | [diff] [blame] | 1436 | if (ra) |
| 1437 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1438 | } else { |
| 1439 | ra = &file->f_ra; |
| 1440 | } |
| 1441 | |
David Sterba | 63e727e | 2017-06-22 03:13:02 +0200 | [diff] [blame] | 1442 | pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1443 | if (!pages) { |
| 1444 | ret = -ENOMEM; |
| 1445 | goto out_ra; |
| 1446 | } |
| 1447 | |
| 1448 | /* find the last page to defrag */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1449 | if (range->start + range->len > range->start) { |
Li Zefan | 151a31b | 2011-09-02 15:56:39 +0800 | [diff] [blame] | 1450 | last_index = min_t(u64, isize - 1, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1451 | range->start + range->len - 1) >> PAGE_SHIFT; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1452 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1453 | last_index = (isize - 1) >> PAGE_SHIFT; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1454 | } |
| 1455 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1456 | if (newer_than) { |
| 1457 | ret = find_new_extents(root, inode, newer_than, |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1458 | &newer_off, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1459 | if (!ret) { |
| 1460 | range->start = newer_off; |
| 1461 | /* |
| 1462 | * we always align our defrag to help keep |
| 1463 | * the extents in the file evenly spaced |
| 1464 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1465 | i = (newer_off & new_align) >> PAGE_SHIFT; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1466 | } else |
| 1467 | goto out_ra; |
| 1468 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1469 | i = range->start >> PAGE_SHIFT; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1470 | } |
| 1471 | if (!max_to_defrag) |
chandan | 070034b | 2015-06-09 10:35:11 +0530 | [diff] [blame] | 1472 | max_to_defrag = last_index - i + 1; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1473 | |
Li Zefan | 2a0f7f5 | 2011-10-10 15:43:34 -0400 | [diff] [blame] | 1474 | /* |
| 1475 | * make writeback starts from i, so the defrag range can be |
| 1476 | * written sequentially. |
| 1477 | */ |
| 1478 | if (i < inode->i_mapping->writeback_index) |
| 1479 | inode->i_mapping->writeback_index = i; |
| 1480 | |
Chris Mason | f7f43cc | 2011-10-11 11:41:40 -0400 | [diff] [blame] | 1481 | while (i <= last_index && defrag_count < max_to_defrag && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1482 | (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) { |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1483 | /* |
| 1484 | * make sure we stop running if someone unmounts |
| 1485 | * the FS |
| 1486 | */ |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1487 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1488 | break; |
| 1489 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1490 | if (btrfs_defrag_cancelled(fs_info)) { |
| 1491 | btrfs_debug(fs_info, "defrag_file cancelled"); |
David Sterba | 210549e | 2013-02-09 23:38:06 +0000 | [diff] [blame] | 1492 | ret = -EAGAIN; |
| 1493 | break; |
| 1494 | } |
| 1495 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1496 | if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1497 | extent_thresh, &last_len, &skip, |
David Sterba | 1e2ef46 | 2017-07-17 20:01:59 +0200 | [diff] [blame] | 1498 | &defrag_end, do_compress)){ |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1499 | unsigned long next; |
| 1500 | /* |
| 1501 | * the should_defrag function tells us how much to skip |
| 1502 | * bump our counter by the suggested amount |
| 1503 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1504 | next = DIV_ROUND_UP(skip, PAGE_SIZE); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1505 | i = max(i + 1, next); |
| 1506 | continue; |
| 1507 | } |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1508 | |
| 1509 | if (!newer_than) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1510 | cluster = (PAGE_ALIGN(defrag_end) >> |
| 1511 | PAGE_SHIFT) - i; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1512 | cluster = min(cluster, max_cluster); |
| 1513 | } else { |
| 1514 | cluster = max_cluster; |
| 1515 | } |
| 1516 | |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1517 | if (i + cluster > ra_index) { |
| 1518 | ra_index = max(i, ra_index); |
David Sterba | 0a52d10 | 2017-06-22 03:22:58 +0200 | [diff] [blame] | 1519 | if (ra) |
David Sterba | d3c0bab | 2017-06-22 03:35:28 +0200 | [diff] [blame] | 1520 | page_cache_sync_readahead(inode->i_mapping, ra, |
| 1521 | file, ra_index, cluster); |
chandan | e4826a5 | 2015-06-09 17:38:32 +0530 | [diff] [blame] | 1522 | ra_index += cluster; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1523 | } |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1524 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1525 | inode_lock(inode); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 1526 | if (IS_SWAPFILE(inode)) { |
| 1527 | ret = -ETXTBSY; |
| 1528 | } else { |
| 1529 | if (do_compress) |
| 1530 | BTRFS_I(inode)->defrag_compress = compress_type; |
| 1531 | ret = cluster_pages_for_defrag(inode, pages, i, cluster); |
| 1532 | } |
Liu Bo | ecb8bea | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1533 | if (ret < 0) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1534 | inode_unlock(inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1535 | goto out_ra; |
Liu Bo | ecb8bea | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1536 | } |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1537 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1538 | defrag_count += ret; |
Namjae Jeon | d0e1d66 | 2012-12-11 16:00:21 -0800 | [diff] [blame] | 1539 | balance_dirty_pages_ratelimited(inode->i_mapping); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1540 | inode_unlock(inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1541 | |
| 1542 | if (newer_than) { |
| 1543 | if (newer_off == (u64)-1) |
| 1544 | break; |
| 1545 | |
Liu Bo | e1f041e | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1546 | if (ret > 0) |
| 1547 | i += ret; |
| 1548 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1549 | newer_off = max(newer_off + 1, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1550 | (u64)i << PAGE_SHIFT); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1551 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1552 | ret = find_new_extents(root, inode, newer_than, |
| 1553 | &newer_off, SZ_64K); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1554 | if (!ret) { |
| 1555 | range->start = newer_off; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1556 | i = (newer_off & new_align) >> PAGE_SHIFT; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1557 | } else { |
| 1558 | break; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1559 | } |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1560 | } else { |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1561 | if (ret > 0) { |
Li Zefan | cbcc832 | 2011-09-02 15:56:25 +0800 | [diff] [blame] | 1562 | i += ret; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1563 | last_len += ret << PAGE_SHIFT; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1564 | } else { |
Li Zefan | cbcc832 | 2011-09-02 15:56:25 +0800 | [diff] [blame] | 1565 | i++; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1566 | last_len = 0; |
| 1567 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1568 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1569 | } |
| 1570 | |
Filipe Manana | dec8ef9 | 2014-03-01 10:55:54 +0000 | [diff] [blame] | 1571 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1572 | filemap_flush(inode->i_mapping); |
Filipe Manana | dec8ef9 | 2014-03-01 10:55:54 +0000 | [diff] [blame] | 1573 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1574 | &BTRFS_I(inode)->runtime_flags)) |
| 1575 | filemap_flush(inode->i_mapping); |
| 1576 | } |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1577 | |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1578 | if (range->compress_type == BTRFS_COMPRESS_LZO) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1579 | btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 1580 | } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) { |
| 1581 | btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1582 | } |
| 1583 | |
Diego Calleja | 60ccf82 | 2011-09-01 16:33:57 +0200 | [diff] [blame] | 1584 | ret = defrag_count; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1585 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1586 | out_ra: |
David Sterba | 1e2ef46 | 2017-07-17 20:01:59 +0200 | [diff] [blame] | 1587 | if (do_compress) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1588 | inode_lock(inode); |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 1589 | BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1590 | inode_unlock(inode); |
Filipe David Borba Manana | 633085c | 2013-08-16 15:23:33 +0100 | [diff] [blame] | 1591 | } |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1592 | if (!file) |
| 1593 | kfree(ra); |
| 1594 | kfree(pages); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1595 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1596 | } |
| 1597 | |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 1598 | static noinline int btrfs_ioctl_resize(struct file *file, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1599 | void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1600 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1601 | struct inode *inode = file_inode(file); |
| 1602 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1603 | u64 new_size; |
| 1604 | u64 old_size; |
| 1605 | u64 devid = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1606 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1607 | struct btrfs_ioctl_vol_args *vol_args; |
| 1608 | struct btrfs_trans_handle *trans; |
| 1609 | struct btrfs_device *device = NULL; |
| 1610 | char *sizestr; |
Gui Hecheng | 9a40f12 | 2014-03-31 18:03:25 +0800 | [diff] [blame] | 1611 | char *retptr; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1612 | char *devstr = NULL; |
| 1613 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1614 | int mod = 0; |
| 1615 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1616 | if (!capable(CAP_SYS_ADMIN)) |
| 1617 | return -EPERM; |
| 1618 | |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 1619 | ret = mnt_want_write_file(file); |
| 1620 | if (ret) |
| 1621 | return ret; |
| 1622 | |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 1623 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
Miao Xie | 9754767 | 2012-12-21 10:38:50 +0000 | [diff] [blame] | 1624 | mnt_drop_write_file(file); |
Anand Jain | e57138b | 2013-08-21 11:44:48 +0800 | [diff] [blame] | 1625 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1626 | } |
| 1627 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 1628 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1629 | if (IS_ERR(vol_args)) { |
| 1630 | ret = PTR_ERR(vol_args); |
| 1631 | goto out; |
| 1632 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 1633 | |
| 1634 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1635 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1636 | sizestr = vol_args->name; |
| 1637 | devstr = strchr(sizestr, ':'); |
| 1638 | if (devstr) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1639 | sizestr = devstr + 1; |
| 1640 | *devstr = '\0'; |
| 1641 | devstr = vol_args->name; |
ZhangZhen | 58dfae6 | 2014-05-13 16:36:08 +0800 | [diff] [blame] | 1642 | ret = kstrtoull(devstr, 10, &devid); |
| 1643 | if (ret) |
| 1644 | goto out_free; |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1645 | if (!devid) { |
| 1646 | ret = -EINVAL; |
| 1647 | goto out_free; |
| 1648 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1649 | btrfs_info(fs_info, "resizing devid %llu", devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1650 | } |
Miao Xie | dba60f3 | 2012-12-21 09:19:51 +0000 | [diff] [blame] | 1651 | |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 1652 | device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1653 | if (!device) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1654 | btrfs_info(fs_info, "resizer unable to find device %llu", |
| 1655 | devid); |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1656 | ret = -ENODEV; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1657 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1658 | } |
Miao Xie | dba60f3 | 2012-12-21 09:19:51 +0000 | [diff] [blame] | 1659 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1660 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1661 | btrfs_info(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1662 | "resizer unable to apply on readonly device %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1663 | devid); |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1664 | ret = -EPERM; |
Liu Bo | 4e42ae1 | 2012-06-14 02:23:19 -0600 | [diff] [blame] | 1665 | goto out_free; |
| 1666 | } |
| 1667 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1668 | if (!strcmp(sizestr, "max")) |
| 1669 | new_size = device->bdev->bd_inode->i_size; |
| 1670 | else { |
| 1671 | if (sizestr[0] == '-') { |
| 1672 | mod = -1; |
| 1673 | sizestr++; |
| 1674 | } else if (sizestr[0] == '+') { |
| 1675 | mod = 1; |
| 1676 | sizestr++; |
| 1677 | } |
Gui Hecheng | 9a40f12 | 2014-03-31 18:03:25 +0800 | [diff] [blame] | 1678 | new_size = memparse(sizestr, &retptr); |
| 1679 | if (*retptr != '\0' || new_size == 0) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1680 | ret = -EINVAL; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1681 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1682 | } |
| 1683 | } |
| 1684 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1685 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1686 | ret = -EPERM; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1687 | goto out_free; |
| 1688 | } |
| 1689 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1690 | old_size = btrfs_device_get_total_bytes(device); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1691 | |
| 1692 | if (mod < 0) { |
| 1693 | if (new_size > old_size) { |
| 1694 | ret = -EINVAL; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1695 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1696 | } |
| 1697 | new_size = old_size - new_size; |
| 1698 | } else if (mod > 0) { |
Wenliang Fan | eb8052e | 2013-12-20 15:28:56 +0800 | [diff] [blame] | 1699 | if (new_size > ULLONG_MAX - old_size) { |
Gui Hecheng | 902c68a | 2014-05-29 09:19:58 +0800 | [diff] [blame] | 1700 | ret = -ERANGE; |
Wenliang Fan | eb8052e | 2013-12-20 15:28:56 +0800 | [diff] [blame] | 1701 | goto out_free; |
| 1702 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1703 | new_size = old_size + new_size; |
| 1704 | } |
| 1705 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1706 | if (new_size < SZ_256M) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1707 | ret = -EINVAL; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1708 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1709 | } |
| 1710 | if (new_size > device->bdev->bd_inode->i_size) { |
| 1711 | ret = -EFBIG; |
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 | |
Nikolay Borisov | 47f08b9 | 2017-07-18 15:39:08 +0300 | [diff] [blame] | 1715 | new_size = round_down(new_size, fs_info->sectorsize); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1716 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1717 | btrfs_info_in_rcu(fs_info, "new size for %s is %llu", |
| 1718 | rcu_str_deref(device->name), new_size); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1719 | |
| 1720 | if (new_size > old_size) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1721 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1722 | if (IS_ERR(trans)) { |
| 1723 | ret = PTR_ERR(trans); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1724 | goto out_free; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1725 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1726 | ret = btrfs_grow_device(trans, device, new_size); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 1727 | btrfs_commit_transaction(trans); |
Mike Fleetwood | ece7d20 | 2011-11-18 18:55:01 +0000 | [diff] [blame] | 1728 | } else if (new_size < old_size) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1729 | ret = btrfs_shrink_device(device, new_size); |
jeff.liu | 0253f40 | 2012-10-27 12:06:39 +0000 | [diff] [blame] | 1730 | } /* equal, nothing need to do */ |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1731 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1732 | out_free: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1733 | kfree(vol_args); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1734 | out: |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 1735 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 18f39c4 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 1736 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1737 | return ret; |
| 1738 | } |
| 1739 | |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1740 | static noinline int btrfs_ioctl_snap_create_transid(struct file *file, |
David Sterba | 52f75f4 | 2017-02-14 18:33:53 +0100 | [diff] [blame] | 1741 | const char *name, unsigned long fd, int subvol, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1742 | u64 *transid, bool readonly, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 1743 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1744 | { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1745 | int namelen; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1746 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1747 | |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 1748 | if (!S_ISDIR(file_inode(file)->i_mode)) |
| 1749 | return -ENOTDIR; |
| 1750 | |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1751 | ret = mnt_want_write_file(file); |
| 1752 | if (ret) |
| 1753 | goto out; |
| 1754 | |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1755 | namelen = strlen(name); |
| 1756 | if (strchr(name, '/')) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1757 | ret = -EINVAL; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1758 | goto out_drop_write; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1759 | } |
| 1760 | |
Chris Mason | 16780ca | 2012-02-20 22:14:55 -0500 | [diff] [blame] | 1761 | if (name[0] == '.' && |
| 1762 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { |
| 1763 | ret = -EEXIST; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1764 | goto out_drop_write; |
Chris Mason | 16780ca | 2012-02-20 22:14:55 -0500 | [diff] [blame] | 1765 | } |
| 1766 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1767 | if (subvol) { |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1768 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1769 | NULL, transid, readonly, inherit); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1770 | } else { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1771 | struct fd src = fdget(fd); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1772 | struct inode *src_inode; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1773 | if (!src.file) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1774 | ret = -EINVAL; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1775 | goto out_drop_write; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1776 | } |
| 1777 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1778 | src_inode = file_inode(src.file); |
| 1779 | if (src_inode->i_sb != file_inode(file)->i_sb) { |
Josef Bacik | c79b471 | 2016-03-25 10:02:41 -0400 | [diff] [blame] | 1780 | btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1781 | "Snapshot src from another FS"); |
Kusanagi Kouichi | 23ad5b1 | 2014-01-30 16:32:02 +0900 | [diff] [blame] | 1782 | ret = -EXDEV; |
David Sterba | d024206 | 2014-01-15 18:15:52 +0100 | [diff] [blame] | 1783 | } else if (!inode_owner_or_capable(src_inode)) { |
| 1784 | /* |
| 1785 | * Subvolume creation is not restricted, but snapshots |
| 1786 | * are limited to own subvolumes only |
| 1787 | */ |
| 1788 | ret = -EPERM; |
Al Viro | ecd1881 | 2012-08-26 21:20:24 -0400 | [diff] [blame] | 1789 | } else { |
| 1790 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
| 1791 | BTRFS_I(src_inode)->root, |
| 1792 | transid, readonly, inherit); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1793 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1794 | fdput(src); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1795 | } |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1796 | out_drop_write: |
| 1797 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1798 | out: |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1799 | return ret; |
| 1800 | } |
| 1801 | |
| 1802 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1803 | void __user *arg, int subvol) |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1804 | { |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1805 | struct btrfs_ioctl_vol_args *vol_args; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1806 | int ret; |
| 1807 | |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 1808 | if (!S_ISDIR(file_inode(file)->i_mode)) |
| 1809 | return -ENOTDIR; |
| 1810 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1811 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1812 | if (IS_ERR(vol_args)) |
| 1813 | return PTR_ERR(vol_args); |
| 1814 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1815 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1816 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1817 | vol_args->fd, subvol, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1818 | NULL, false, NULL); |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1819 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1820 | kfree(vol_args); |
| 1821 | return ret; |
| 1822 | } |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1823 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1824 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
| 1825 | void __user *arg, int subvol) |
| 1826 | { |
| 1827 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
| 1828 | int ret; |
| 1829 | u64 transid = 0; |
| 1830 | u64 *ptr = NULL; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1831 | bool readonly = false; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1832 | struct btrfs_qgroup_inherit *inherit = NULL; |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1833 | |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 1834 | if (!S_ISDIR(file_inode(file)->i_mode)) |
| 1835 | return -ENOTDIR; |
| 1836 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1837 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1838 | if (IS_ERR(vol_args)) |
| 1839 | return PTR_ERR(vol_args); |
| 1840 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; |
Sage Weil | 75eaa0e | 2010-12-10 00:36:28 +0000 | [diff] [blame] | 1841 | |
David Sterba | 673990d | 2020-02-21 13:24:37 +0100 | [diff] [blame] | 1842 | if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1843 | ret = -EOPNOTSUPP; |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1844 | goto free_args; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1845 | } |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1846 | |
Nikolay Borisov | ebc8735 | 2019-08-26 17:34:24 +0300 | [diff] [blame] | 1847 | if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) { |
| 1848 | struct inode *inode = file_inode(file); |
| 1849 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 1850 | |
| 1851 | btrfs_warn(fs_info, |
| 1852 | "SNAP_CREATE_V2 ioctl with CREATE_ASYNC is deprecated and will be removed in kernel 5.7"); |
| 1853 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1854 | ptr = &transid; |
Nikolay Borisov | ebc8735 | 2019-08-26 17:34:24 +0300 | [diff] [blame] | 1855 | } |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1856 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
| 1857 | readonly = true; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1858 | if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1859 | if (vol_args->size > PAGE_SIZE) { |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1860 | ret = -EINVAL; |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1861 | goto free_args; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1862 | } |
| 1863 | inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); |
| 1864 | if (IS_ERR(inherit)) { |
| 1865 | ret = PTR_ERR(inherit); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1866 | goto free_args; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1867 | } |
| 1868 | } |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1869 | |
| 1870 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1871 | vol_args->fd, subvol, ptr, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 1872 | readonly, inherit); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1873 | if (ret) |
| 1874 | goto free_inherit; |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1875 | |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1876 | if (ptr && copy_to_user(arg + |
| 1877 | offsetof(struct btrfs_ioctl_vol_args_v2, |
| 1878 | transid), |
| 1879 | ptr, sizeof(*ptr))) |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1880 | ret = -EFAULT; |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1881 | |
| 1882 | free_inherit: |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1883 | kfree(inherit); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1884 | free_args: |
| 1885 | kfree(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1886 | return ret; |
| 1887 | } |
| 1888 | |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1889 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
| 1890 | void __user *arg) |
| 1891 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1892 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1893 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1894 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1895 | int ret = 0; |
| 1896 | u64 flags = 0; |
| 1897 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1898 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1899 | return -EINVAL; |
| 1900 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1901 | down_read(&fs_info->subvol_sem); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1902 | if (btrfs_root_readonly(root)) |
| 1903 | flags |= BTRFS_SUBVOL_RDONLY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1904 | up_read(&fs_info->subvol_sem); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1905 | |
| 1906 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 1907 | ret = -EFAULT; |
| 1908 | |
| 1909 | return ret; |
| 1910 | } |
| 1911 | |
| 1912 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, |
| 1913 | void __user *arg) |
| 1914 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1915 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1916 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1917 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1918 | struct btrfs_trans_handle *trans; |
| 1919 | u64 root_flags; |
| 1920 | u64 flags; |
| 1921 | int ret = 0; |
| 1922 | |
David Sterba | bd60ea0 | 2014-01-16 15:50:22 +0100 | [diff] [blame] | 1923 | if (!inode_owner_or_capable(inode)) |
| 1924 | return -EPERM; |
| 1925 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1926 | ret = mnt_want_write_file(file); |
| 1927 | if (ret) |
| 1928 | goto out; |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1929 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1930 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1931 | ret = -EINVAL; |
| 1932 | goto out_drop_write; |
| 1933 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1934 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1935 | if (copy_from_user(&flags, arg, sizeof(flags))) { |
| 1936 | ret = -EFAULT; |
| 1937 | goto out_drop_write; |
| 1938 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1939 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1940 | if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { |
| 1941 | ret = -EINVAL; |
| 1942 | goto out_drop_write; |
| 1943 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1944 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1945 | if (flags & ~BTRFS_SUBVOL_RDONLY) { |
| 1946 | ret = -EOPNOTSUPP; |
| 1947 | goto out_drop_write; |
| 1948 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1949 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1950 | down_write(&fs_info->subvol_sem); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1951 | |
| 1952 | /* nothing to do */ |
| 1953 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1954 | goto out_drop_sem; |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1955 | |
| 1956 | root_flags = btrfs_root_flags(&root->root_item); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 1957 | if (flags & BTRFS_SUBVOL_RDONLY) { |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1958 | btrfs_set_root_flags(&root->root_item, |
| 1959 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 1960 | } else { |
| 1961 | /* |
| 1962 | * Block RO -> RW transition if this subvolume is involved in |
| 1963 | * send |
| 1964 | */ |
| 1965 | spin_lock(&root->root_item_lock); |
| 1966 | if (root->send_in_progress == 0) { |
| 1967 | btrfs_set_root_flags(&root->root_item, |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1968 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 1969 | spin_unlock(&root->root_item_lock); |
| 1970 | } else { |
| 1971 | spin_unlock(&root->root_item_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1972 | btrfs_warn(fs_info, |
| 1973 | "Attempt to set subvolume %llu read-write during send", |
| 1974 | root->root_key.objectid); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 1975 | ret = -EPERM; |
| 1976 | goto out_drop_sem; |
| 1977 | } |
| 1978 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1979 | |
| 1980 | trans = btrfs_start_transaction(root, 1); |
| 1981 | if (IS_ERR(trans)) { |
| 1982 | ret = PTR_ERR(trans); |
| 1983 | goto out_reset; |
| 1984 | } |
| 1985 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1986 | ret = btrfs_update_root(trans, fs_info->tree_root, |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1987 | &root->root_key, &root->root_item); |
Nikolay Borisov | 9417ebc | 2017-09-28 10:53:17 +0300 | [diff] [blame] | 1988 | if (ret < 0) { |
| 1989 | btrfs_end_transaction(trans); |
| 1990 | goto out_reset; |
| 1991 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1992 | |
Nikolay Borisov | 9417ebc | 2017-09-28 10:53:17 +0300 | [diff] [blame] | 1993 | ret = btrfs_commit_transaction(trans); |
| 1994 | |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1995 | out_reset: |
| 1996 | if (ret) |
| 1997 | btrfs_set_root_flags(&root->root_item, root_flags); |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1998 | out_drop_sem: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1999 | up_write(&fs_info->subvol_sem); |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 2000 | out_drop_write: |
| 2001 | mnt_drop_write_file(file); |
| 2002 | out: |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2003 | return ret; |
| 2004 | } |
| 2005 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2006 | static noinline int key_in_sk(struct btrfs_key *key, |
| 2007 | struct btrfs_ioctl_search_key *sk) |
| 2008 | { |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2009 | struct btrfs_key test; |
| 2010 | int ret; |
| 2011 | |
| 2012 | test.objectid = sk->min_objectid; |
| 2013 | test.type = sk->min_type; |
| 2014 | test.offset = sk->min_offset; |
| 2015 | |
| 2016 | ret = btrfs_comp_cpu_keys(key, &test); |
| 2017 | if (ret < 0) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2018 | return 0; |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2019 | |
| 2020 | test.objectid = sk->max_objectid; |
| 2021 | test.type = sk->max_type; |
| 2022 | test.offset = sk->max_offset; |
| 2023 | |
| 2024 | ret = btrfs_comp_cpu_keys(key, &test); |
| 2025 | if (ret > 0) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2026 | return 0; |
| 2027 | return 1; |
| 2028 | } |
| 2029 | |
Jeff Mahoney | df39756 | 2016-06-21 20:18:21 -0400 | [diff] [blame] | 2030 | static noinline int copy_to_sk(struct btrfs_path *path, |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2031 | struct btrfs_key *key, |
| 2032 | struct btrfs_ioctl_search_key *sk, |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2033 | size_t *buf_size, |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2034 | char __user *ubuf, |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2035 | unsigned long *sk_offset, |
| 2036 | int *num_found) |
| 2037 | { |
| 2038 | u64 found_transid; |
| 2039 | struct extent_buffer *leaf; |
| 2040 | struct btrfs_ioctl_search_header sh; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2041 | struct btrfs_key test; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2042 | unsigned long item_off; |
| 2043 | unsigned long item_len; |
| 2044 | int nritems; |
| 2045 | int i; |
| 2046 | int slot; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2047 | int ret = 0; |
| 2048 | |
| 2049 | leaf = path->nodes[0]; |
| 2050 | slot = path->slots[0]; |
| 2051 | nritems = btrfs_header_nritems(leaf); |
| 2052 | |
| 2053 | if (btrfs_header_generation(leaf) > sk->max_transid) { |
| 2054 | i = nritems; |
| 2055 | goto advance_key; |
| 2056 | } |
| 2057 | found_transid = btrfs_header_generation(leaf); |
| 2058 | |
| 2059 | for (i = slot; i < nritems; i++) { |
| 2060 | item_off = btrfs_item_ptr_offset(leaf, i); |
| 2061 | item_len = btrfs_item_size_nr(leaf, i); |
| 2062 | |
Gabriel de Perthuis | 03b71c6 | 2013-05-06 17:40:18 +0000 | [diff] [blame] | 2063 | btrfs_item_key_to_cpu(leaf, key, i); |
| 2064 | if (!key_in_sk(key, sk)) |
| 2065 | continue; |
| 2066 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2067 | if (sizeof(sh) + item_len > *buf_size) { |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2068 | if (*num_found) { |
| 2069 | ret = 1; |
| 2070 | goto out; |
| 2071 | } |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2072 | |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2073 | /* |
| 2074 | * return one empty item back for v1, which does not |
| 2075 | * handle -EOVERFLOW |
| 2076 | */ |
| 2077 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2078 | *buf_size = sizeof(sh) + item_len; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2079 | item_len = 0; |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2080 | ret = -EOVERFLOW; |
| 2081 | } |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2082 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2083 | if (sizeof(sh) + item_len + *sk_offset > *buf_size) { |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2084 | ret = 1; |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2085 | goto out; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2086 | } |
| 2087 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2088 | sh.objectid = key->objectid; |
| 2089 | sh.offset = key->offset; |
| 2090 | sh.type = key->type; |
| 2091 | sh.len = item_len; |
| 2092 | sh.transid = found_transid; |
| 2093 | |
| 2094 | /* copy search result header */ |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2095 | if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { |
| 2096 | ret = -EFAULT; |
| 2097 | goto out; |
| 2098 | } |
| 2099 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2100 | *sk_offset += sizeof(sh); |
| 2101 | |
| 2102 | if (item_len) { |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2103 | char __user *up = ubuf + *sk_offset; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2104 | /* copy the item */ |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2105 | if (read_extent_buffer_to_user(leaf, up, |
| 2106 | item_off, item_len)) { |
| 2107 | ret = -EFAULT; |
| 2108 | goto out; |
| 2109 | } |
| 2110 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2111 | *sk_offset += item_len; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2112 | } |
Hugo Mills | e215686 | 2011-05-14 17:43:41 +0000 | [diff] [blame] | 2113 | (*num_found)++; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2114 | |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2115 | if (ret) /* -EOVERFLOW from above */ |
| 2116 | goto out; |
| 2117 | |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2118 | if (*num_found >= sk->nr_items) { |
| 2119 | ret = 1; |
| 2120 | goto out; |
| 2121 | } |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2122 | } |
| 2123 | advance_key: |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2124 | ret = 0; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2125 | test.objectid = sk->max_objectid; |
| 2126 | test.type = sk->max_type; |
| 2127 | test.offset = sk->max_offset; |
| 2128 | if (btrfs_comp_cpu_keys(key, &test) >= 0) |
| 2129 | ret = 1; |
| 2130 | else if (key->offset < (u64)-1) |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2131 | key->offset++; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2132 | else if (key->type < (u8)-1) { |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2133 | key->offset = 0; |
| 2134 | key->type++; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2135 | } else if (key->objectid < (u64)-1) { |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2136 | key->offset = 0; |
| 2137 | key->type = 0; |
| 2138 | key->objectid++; |
| 2139 | } else |
| 2140 | ret = 1; |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2141 | out: |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2142 | /* |
| 2143 | * 0: all items from this leaf copied, continue with next |
| 2144 | * 1: * more items can be copied, but unused buffer is too small |
| 2145 | * * all items were found |
| 2146 | * Either way, it will stops the loop which iterates to the next |
| 2147 | * leaf |
| 2148 | * -EOVERFLOW: item was to large for buffer |
| 2149 | * -EFAULT: could not copy extent buffer back to userspace |
| 2150 | */ |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2151 | return ret; |
| 2152 | } |
| 2153 | |
| 2154 | static noinline int search_ioctl(struct inode *inode, |
Gerhard Heift | 1254444 | 2014-01-30 16:23:58 +0100 | [diff] [blame] | 2155 | struct btrfs_ioctl_search_key *sk, |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2156 | size_t *buf_size, |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2157 | char __user *ubuf) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2158 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2159 | struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2160 | struct btrfs_root *root; |
| 2161 | struct btrfs_key key; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2162 | struct btrfs_path *path; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2163 | int ret; |
| 2164 | int num_found = 0; |
| 2165 | unsigned long sk_offset = 0; |
| 2166 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2167 | if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { |
| 2168 | *buf_size = sizeof(struct btrfs_ioctl_search_header); |
Gerhard Heift | 1254444 | 2014-01-30 16:23:58 +0100 | [diff] [blame] | 2169 | return -EOVERFLOW; |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2170 | } |
Gerhard Heift | 1254444 | 2014-01-30 16:23:58 +0100 | [diff] [blame] | 2171 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2172 | path = btrfs_alloc_path(); |
| 2173 | if (!path) |
| 2174 | return -ENOMEM; |
| 2175 | |
| 2176 | if (sk->tree_id == 0) { |
| 2177 | /* search the root of the inode that was passed */ |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2178 | root = btrfs_grab_root(BTRFS_I(inode)->root); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2179 | } else { |
| 2180 | key.objectid = sk->tree_id; |
| 2181 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2182 | key.offset = (u64)-1; |
Josef Bacik | 3619c94 | 2020-01-24 09:32:24 -0500 | [diff] [blame] | 2183 | root = btrfs_get_fs_root(info, &key, true); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2184 | if (IS_ERR(root)) { |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2185 | btrfs_free_path(path); |
Misono Tomohiro | ad1e3d5 | 2018-05-21 13:57:27 +0900 | [diff] [blame] | 2186 | return PTR_ERR(root); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | key.objectid = sk->min_objectid; |
| 2191 | key.type = sk->min_type; |
| 2192 | key.offset = sk->min_offset; |
| 2193 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2194 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 2195 | ret = btrfs_search_forward(root, &key, path, sk->min_transid); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2196 | if (ret != 0) { |
| 2197 | if (ret > 0) |
| 2198 | ret = 0; |
| 2199 | goto err; |
| 2200 | } |
Jeff Mahoney | df39756 | 2016-06-21 20:18:21 -0400 | [diff] [blame] | 2201 | ret = copy_to_sk(path, &key, sk, buf_size, ubuf, |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2202 | &sk_offset, &num_found); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2203 | btrfs_release_path(path); |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2204 | if (ret) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2205 | break; |
| 2206 | |
| 2207 | } |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2208 | if (ret > 0) |
| 2209 | ret = 0; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2210 | err: |
| 2211 | sk->nr_items = num_found; |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2212 | btrfs_put_root(root); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2213 | btrfs_free_path(path); |
| 2214 | return ret; |
| 2215 | } |
| 2216 | |
| 2217 | static noinline int btrfs_ioctl_tree_search(struct file *file, |
| 2218 | void __user *argp) |
| 2219 | { |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2220 | struct btrfs_ioctl_search_args __user *uargs; |
| 2221 | struct btrfs_ioctl_search_key sk; |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2222 | struct inode *inode; |
| 2223 | int ret; |
| 2224 | size_t buf_size; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2225 | |
| 2226 | if (!capable(CAP_SYS_ADMIN)) |
| 2227 | return -EPERM; |
| 2228 | |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2229 | uargs = (struct btrfs_ioctl_search_args __user *)argp; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2230 | |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2231 | if (copy_from_user(&sk, &uargs->key, sizeof(sk))) |
| 2232 | return -EFAULT; |
| 2233 | |
| 2234 | buf_size = sizeof(uargs->buf); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2235 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2236 | inode = file_inode(file); |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2237 | ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2238 | |
| 2239 | /* |
| 2240 | * In the origin implementation an overflow is handled by returning a |
| 2241 | * search header with a len of zero, so reset ret. |
| 2242 | */ |
| 2243 | if (ret == -EOVERFLOW) |
| 2244 | ret = 0; |
| 2245 | |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2246 | if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2247 | ret = -EFAULT; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2248 | return ret; |
| 2249 | } |
| 2250 | |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2251 | static noinline int btrfs_ioctl_tree_search_v2(struct file *file, |
| 2252 | void __user *argp) |
| 2253 | { |
| 2254 | struct btrfs_ioctl_search_args_v2 __user *uarg; |
| 2255 | struct btrfs_ioctl_search_args_v2 args; |
| 2256 | struct inode *inode; |
| 2257 | int ret; |
| 2258 | size_t buf_size; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 2259 | const size_t buf_limit = SZ_16M; |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2260 | |
| 2261 | if (!capable(CAP_SYS_ADMIN)) |
| 2262 | return -EPERM; |
| 2263 | |
| 2264 | /* copy search header and buffer size */ |
| 2265 | uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; |
| 2266 | if (copy_from_user(&args, uarg, sizeof(args))) |
| 2267 | return -EFAULT; |
| 2268 | |
| 2269 | buf_size = args.buf_size; |
| 2270 | |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2271 | /* limit result size to 16MB */ |
| 2272 | if (buf_size > buf_limit) |
| 2273 | buf_size = buf_limit; |
| 2274 | |
| 2275 | inode = file_inode(file); |
| 2276 | ret = search_ioctl(inode, &args.key, &buf_size, |
Omar Sandoval | 718dc5f | 2017-08-22 23:46:05 -0700 | [diff] [blame] | 2277 | (char __user *)(&uarg->buf[0])); |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2278 | if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) |
| 2279 | ret = -EFAULT; |
| 2280 | else if (ret == -EOVERFLOW && |
| 2281 | copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) |
| 2282 | ret = -EFAULT; |
| 2283 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2284 | return ret; |
| 2285 | } |
| 2286 | |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2287 | /* |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2288 | * Search INODE_REFs to identify path name of 'dirid' directory |
| 2289 | * in a 'tree_id' tree. and sets path name to 'name'. |
| 2290 | */ |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2291 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
| 2292 | u64 tree_id, u64 dirid, char *name) |
| 2293 | { |
| 2294 | struct btrfs_root *root; |
| 2295 | struct btrfs_key key; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2296 | char *ptr; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2297 | int ret = -1; |
| 2298 | int slot; |
| 2299 | int len; |
| 2300 | int total_len = 0; |
| 2301 | struct btrfs_inode_ref *iref; |
| 2302 | struct extent_buffer *l; |
| 2303 | struct btrfs_path *path; |
| 2304 | |
| 2305 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { |
| 2306 | name[0]='\0'; |
| 2307 | return 0; |
| 2308 | } |
| 2309 | |
| 2310 | path = btrfs_alloc_path(); |
| 2311 | if (!path) |
| 2312 | return -ENOMEM; |
| 2313 | |
Nikolay Borisov | c8bcbfbd | 2017-12-01 11:19:42 +0200 | [diff] [blame] | 2314 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2315 | |
| 2316 | key.objectid = tree_id; |
| 2317 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2318 | key.offset = (u64)-1; |
Josef Bacik | 3619c94 | 2020-01-24 09:32:24 -0500 | [diff] [blame] | 2319 | root = btrfs_get_fs_root(info, &key, true); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2320 | if (IS_ERR(root)) { |
Misono Tomohiro | ad1e3d5 | 2018-05-21 13:57:27 +0900 | [diff] [blame] | 2321 | ret = PTR_ERR(root); |
Josef Bacik | 8823401 | 2020-01-24 09:32:34 -0500 | [diff] [blame] | 2322 | root = NULL; |
| 2323 | goto out; |
| 2324 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2325 | |
| 2326 | key.objectid = dirid; |
| 2327 | key.type = BTRFS_INODE_REF_KEY; |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 2328 | key.offset = (u64)-1; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2329 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2330 | while (1) { |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2331 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2332 | if (ret < 0) |
| 2333 | goto out; |
Filipe David Borba Manana | 18674c6 | 2013-08-14 03:00:21 +0100 | [diff] [blame] | 2334 | else if (ret > 0) { |
| 2335 | ret = btrfs_previous_item(root, path, dirid, |
| 2336 | BTRFS_INODE_REF_KEY); |
| 2337 | if (ret < 0) |
| 2338 | goto out; |
| 2339 | else if (ret > 0) { |
| 2340 | ret = -ENOENT; |
| 2341 | goto out; |
| 2342 | } |
| 2343 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2344 | |
| 2345 | l = path->nodes[0]; |
| 2346 | slot = path->slots[0]; |
| 2347 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2348 | |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2349 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); |
| 2350 | len = btrfs_inode_ref_name_len(l, iref); |
| 2351 | ptr -= len + 1; |
| 2352 | total_len += len + 1; |
Filipe David Borba Manana | a696cf3 | 2013-08-14 03:00:20 +0100 | [diff] [blame] | 2353 | if (ptr < name) { |
| 2354 | ret = -ENAMETOOLONG; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2355 | goto out; |
Filipe David Borba Manana | a696cf3 | 2013-08-14 03:00:20 +0100 | [diff] [blame] | 2356 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2357 | |
| 2358 | *(ptr + len) = '/'; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2359 | read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2360 | |
| 2361 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) |
| 2362 | break; |
| 2363 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2364 | btrfs_release_path(path); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2365 | key.objectid = key.offset; |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 2366 | key.offset = (u64)-1; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2367 | dirid = key.objectid; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2368 | } |
Li Zefan | 77906a50 | 2011-07-14 03:16:00 +0000 | [diff] [blame] | 2369 | memmove(name, ptr, total_len); |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2370 | name[total_len] = '\0'; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2371 | ret = 0; |
| 2372 | out: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2373 | btrfs_put_root(root); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2374 | btrfs_free_path(path); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2375 | return ret; |
| 2376 | } |
| 2377 | |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2378 | static int btrfs_search_path_in_tree_user(struct inode *inode, |
| 2379 | struct btrfs_ioctl_ino_lookup_user_args *args) |
| 2380 | { |
| 2381 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2382 | struct super_block *sb = inode->i_sb; |
| 2383 | struct btrfs_key upper_limit = BTRFS_I(inode)->location; |
| 2384 | u64 treeid = BTRFS_I(inode)->root->root_key.objectid; |
| 2385 | u64 dirid = args->dirid; |
| 2386 | unsigned long item_off; |
| 2387 | unsigned long item_len; |
| 2388 | struct btrfs_inode_ref *iref; |
| 2389 | struct btrfs_root_ref *rref; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2390 | struct btrfs_root *root = NULL; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2391 | struct btrfs_path *path; |
| 2392 | struct btrfs_key key, key2; |
| 2393 | struct extent_buffer *leaf; |
| 2394 | struct inode *temp_inode; |
| 2395 | char *ptr; |
| 2396 | int slot; |
| 2397 | int len; |
| 2398 | int total_len = 0; |
| 2399 | int ret; |
| 2400 | |
| 2401 | path = btrfs_alloc_path(); |
| 2402 | if (!path) |
| 2403 | return -ENOMEM; |
| 2404 | |
| 2405 | /* |
| 2406 | * If the bottom subvolume does not exist directly under upper_limit, |
| 2407 | * construct the path in from the bottom up. |
| 2408 | */ |
| 2409 | if (dirid != upper_limit.objectid) { |
| 2410 | ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1]; |
| 2411 | |
| 2412 | key.objectid = treeid; |
| 2413 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2414 | key.offset = (u64)-1; |
Josef Bacik | 3619c94 | 2020-01-24 09:32:24 -0500 | [diff] [blame] | 2415 | root = btrfs_get_fs_root(fs_info, &key, true); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2416 | if (IS_ERR(root)) { |
| 2417 | ret = PTR_ERR(root); |
| 2418 | goto out; |
| 2419 | } |
| 2420 | |
| 2421 | key.objectid = dirid; |
| 2422 | key.type = BTRFS_INODE_REF_KEY; |
| 2423 | key.offset = (u64)-1; |
| 2424 | while (1) { |
| 2425 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2426 | if (ret < 0) { |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2427 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2428 | } else if (ret > 0) { |
| 2429 | ret = btrfs_previous_item(root, path, dirid, |
| 2430 | BTRFS_INODE_REF_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 | |
| 2439 | leaf = path->nodes[0]; |
| 2440 | slot = path->slots[0]; |
| 2441 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2442 | |
| 2443 | iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref); |
| 2444 | len = btrfs_inode_ref_name_len(leaf, iref); |
| 2445 | ptr -= len + 1; |
| 2446 | total_len += len + 1; |
| 2447 | if (ptr < args->path) { |
| 2448 | ret = -ENAMETOOLONG; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2449 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | *(ptr + len) = '/'; |
| 2453 | read_extent_buffer(leaf, ptr, |
| 2454 | (unsigned long)(iref + 1), len); |
| 2455 | |
| 2456 | /* Check the read+exec permission of this directory */ |
| 2457 | ret = btrfs_previous_item(root, path, dirid, |
| 2458 | BTRFS_INODE_ITEM_KEY); |
| 2459 | if (ret < 0) { |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2460 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2461 | } else if (ret > 0) { |
| 2462 | ret = -ENOENT; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2463 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | leaf = path->nodes[0]; |
| 2467 | slot = path->slots[0]; |
| 2468 | btrfs_item_key_to_cpu(leaf, &key2, slot); |
| 2469 | if (key2.objectid != dirid) { |
| 2470 | ret = -ENOENT; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2471 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2472 | } |
| 2473 | |
David Sterba | 4c66e0d | 2019-10-03 19:09:35 +0200 | [diff] [blame] | 2474 | temp_inode = btrfs_iget(sb, &key2, root); |
Misono Tomohiro | 3ca57bd | 2018-06-04 16:41:07 +0900 | [diff] [blame] | 2475 | if (IS_ERR(temp_inode)) { |
| 2476 | ret = PTR_ERR(temp_inode); |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2477 | goto out_put; |
Misono Tomohiro | 3ca57bd | 2018-06-04 16:41:07 +0900 | [diff] [blame] | 2478 | } |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2479 | ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC); |
| 2480 | iput(temp_inode); |
| 2481 | if (ret) { |
| 2482 | ret = -EACCES; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2483 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | if (key.offset == upper_limit.objectid) |
| 2487 | break; |
| 2488 | if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) { |
| 2489 | ret = -EACCES; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2490 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | btrfs_release_path(path); |
| 2494 | key.objectid = key.offset; |
| 2495 | key.offset = (u64)-1; |
| 2496 | dirid = key.objectid; |
| 2497 | } |
| 2498 | |
| 2499 | memmove(args->path, ptr, total_len); |
| 2500 | args->path[total_len] = '\0'; |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2501 | btrfs_put_root(root); |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2502 | root = NULL; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2503 | btrfs_release_path(path); |
| 2504 | } |
| 2505 | |
| 2506 | /* Get the bottom subvolume's name from ROOT_REF */ |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2507 | key.objectid = treeid; |
| 2508 | key.type = BTRFS_ROOT_REF_KEY; |
| 2509 | key.offset = args->treeid; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2510 | 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] | 2511 | if (ret < 0) { |
| 2512 | goto out; |
| 2513 | } else if (ret > 0) { |
| 2514 | ret = -ENOENT; |
| 2515 | goto out; |
| 2516 | } |
| 2517 | |
| 2518 | leaf = path->nodes[0]; |
| 2519 | slot = path->slots[0]; |
| 2520 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2521 | |
| 2522 | item_off = btrfs_item_ptr_offset(leaf, slot); |
| 2523 | item_len = btrfs_item_size_nr(leaf, slot); |
| 2524 | /* Check if dirid in ROOT_REF corresponds to passed dirid */ |
| 2525 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); |
| 2526 | if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) { |
| 2527 | ret = -EINVAL; |
| 2528 | goto out; |
| 2529 | } |
| 2530 | |
| 2531 | /* Copy subvolume's name */ |
| 2532 | item_off += sizeof(struct btrfs_root_ref); |
| 2533 | item_len -= sizeof(struct btrfs_root_ref); |
| 2534 | read_extent_buffer(leaf, args->name, item_off, item_len); |
| 2535 | args->name[item_len] = 0; |
| 2536 | |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2537 | out_put: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2538 | btrfs_put_root(root); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2539 | out: |
| 2540 | btrfs_free_path(path); |
| 2541 | return ret; |
| 2542 | } |
| 2543 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2544 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, |
| 2545 | void __user *argp) |
| 2546 | { |
Bart Van Assche | bece2e8 | 2018-06-20 10:03:31 -0700 | [diff] [blame] | 2547 | struct btrfs_ioctl_ino_lookup_args *args; |
| 2548 | struct inode *inode; |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2549 | int ret = 0; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2550 | |
Julia Lawall | 2354d08f | 2010-10-29 15:14:18 -0400 | [diff] [blame] | 2551 | args = memdup_user(argp, sizeof(*args)); |
| 2552 | if (IS_ERR(args)) |
| 2553 | return PTR_ERR(args); |
Dan Carpenter | c2b9692 | 2010-03-20 11:24:15 +0000 | [diff] [blame] | 2554 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2555 | inode = file_inode(file); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2556 | |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2557 | /* |
| 2558 | * Unprivileged query to obtain the containing subvolume root id. The |
| 2559 | * path is reset so it's consistent with btrfs_search_path_in_tree. |
| 2560 | */ |
Chris Mason | 1b53ac4 | 2010-03-18 12:17:05 -0400 | [diff] [blame] | 2561 | if (args->treeid == 0) |
| 2562 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; |
| 2563 | |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2564 | if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { |
| 2565 | args->name[0] = 0; |
| 2566 | goto out; |
| 2567 | } |
| 2568 | |
| 2569 | if (!capable(CAP_SYS_ADMIN)) { |
| 2570 | ret = -EPERM; |
| 2571 | goto out; |
| 2572 | } |
| 2573 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2574 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
| 2575 | args->treeid, args->objectid, |
| 2576 | args->name); |
| 2577 | |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2578 | out: |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2579 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 2580 | ret = -EFAULT; |
| 2581 | |
| 2582 | kfree(args); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2583 | return ret; |
| 2584 | } |
| 2585 | |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2586 | /* |
| 2587 | * Version of ino_lookup ioctl (unprivileged) |
| 2588 | * |
| 2589 | * The main differences from ino_lookup ioctl are: |
| 2590 | * |
| 2591 | * 1. Read + Exec permission will be checked using inode_permission() during |
| 2592 | * path construction. -EACCES will be returned in case of failure. |
| 2593 | * 2. Path construction will be stopped at the inode number which corresponds |
| 2594 | * to the fd with which this ioctl is called. If constructed path does not |
| 2595 | * exist under fd's inode, -EACCES will be returned. |
| 2596 | * 3. The name of bottom subvolume is also searched and filled. |
| 2597 | */ |
| 2598 | static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) |
| 2599 | { |
| 2600 | struct btrfs_ioctl_ino_lookup_user_args *args; |
| 2601 | struct inode *inode; |
| 2602 | int ret; |
| 2603 | |
| 2604 | args = memdup_user(argp, sizeof(*args)); |
| 2605 | if (IS_ERR(args)) |
| 2606 | return PTR_ERR(args); |
| 2607 | |
| 2608 | inode = file_inode(file); |
| 2609 | |
| 2610 | if (args->dirid == BTRFS_FIRST_FREE_OBJECTID && |
| 2611 | BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) { |
| 2612 | /* |
| 2613 | * The subvolume does not exist under fd with which this is |
| 2614 | * called |
| 2615 | */ |
| 2616 | kfree(args); |
| 2617 | return -EACCES; |
| 2618 | } |
| 2619 | |
| 2620 | ret = btrfs_search_path_in_tree_user(inode, args); |
| 2621 | |
| 2622 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 2623 | ret = -EFAULT; |
| 2624 | |
| 2625 | kfree(args); |
| 2626 | return ret; |
| 2627 | } |
| 2628 | |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2629 | /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ |
| 2630 | static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) |
| 2631 | { |
| 2632 | struct btrfs_ioctl_get_subvol_info_args *subvol_info; |
| 2633 | struct btrfs_fs_info *fs_info; |
| 2634 | struct btrfs_root *root; |
| 2635 | struct btrfs_path *path; |
| 2636 | struct btrfs_key key; |
| 2637 | struct btrfs_root_item *root_item; |
| 2638 | struct btrfs_root_ref *rref; |
| 2639 | struct extent_buffer *leaf; |
| 2640 | unsigned long item_off; |
| 2641 | unsigned long item_len; |
| 2642 | struct inode *inode; |
| 2643 | int slot; |
| 2644 | int ret = 0; |
| 2645 | |
| 2646 | path = btrfs_alloc_path(); |
| 2647 | if (!path) |
| 2648 | return -ENOMEM; |
| 2649 | |
| 2650 | subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL); |
| 2651 | if (!subvol_info) { |
| 2652 | btrfs_free_path(path); |
| 2653 | return -ENOMEM; |
| 2654 | } |
| 2655 | |
| 2656 | inode = file_inode(file); |
| 2657 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2658 | |
| 2659 | /* Get root_item of inode's subvolume */ |
| 2660 | key.objectid = BTRFS_I(inode)->root->root_key.objectid; |
| 2661 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2662 | key.offset = (u64)-1; |
Josef Bacik | 3619c94 | 2020-01-24 09:32:24 -0500 | [diff] [blame] | 2663 | root = btrfs_get_fs_root(fs_info, &key, true); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2664 | if (IS_ERR(root)) { |
| 2665 | ret = PTR_ERR(root); |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2666 | goto out_free; |
| 2667 | } |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2668 | root_item = &root->root_item; |
| 2669 | |
| 2670 | subvol_info->treeid = key.objectid; |
| 2671 | |
| 2672 | subvol_info->generation = btrfs_root_generation(root_item); |
| 2673 | subvol_info->flags = btrfs_root_flags(root_item); |
| 2674 | |
| 2675 | memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE); |
| 2676 | memcpy(subvol_info->parent_uuid, root_item->parent_uuid, |
| 2677 | BTRFS_UUID_SIZE); |
| 2678 | memcpy(subvol_info->received_uuid, root_item->received_uuid, |
| 2679 | BTRFS_UUID_SIZE); |
| 2680 | |
| 2681 | subvol_info->ctransid = btrfs_root_ctransid(root_item); |
| 2682 | subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime); |
| 2683 | subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime); |
| 2684 | |
| 2685 | subvol_info->otransid = btrfs_root_otransid(root_item); |
| 2686 | subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime); |
| 2687 | subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime); |
| 2688 | |
| 2689 | subvol_info->stransid = btrfs_root_stransid(root_item); |
| 2690 | subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime); |
| 2691 | subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime); |
| 2692 | |
| 2693 | subvol_info->rtransid = btrfs_root_rtransid(root_item); |
| 2694 | subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime); |
| 2695 | subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime); |
| 2696 | |
| 2697 | if (key.objectid != BTRFS_FS_TREE_OBJECTID) { |
| 2698 | /* Search root tree for ROOT_BACKREF of this subvolume */ |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2699 | key.type = BTRFS_ROOT_BACKREF_KEY; |
| 2700 | key.offset = 0; |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2701 | 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] | 2702 | if (ret < 0) { |
| 2703 | goto out; |
| 2704 | } else if (path->slots[0] >= |
| 2705 | btrfs_header_nritems(path->nodes[0])) { |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2706 | ret = btrfs_next_leaf(fs_info->tree_root, path); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2707 | if (ret < 0) { |
| 2708 | goto out; |
| 2709 | } else if (ret > 0) { |
| 2710 | ret = -EUCLEAN; |
| 2711 | goto out; |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | leaf = path->nodes[0]; |
| 2716 | slot = path->slots[0]; |
| 2717 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2718 | if (key.objectid == subvol_info->treeid && |
| 2719 | key.type == BTRFS_ROOT_BACKREF_KEY) { |
| 2720 | subvol_info->parent_id = key.offset; |
| 2721 | |
| 2722 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); |
| 2723 | subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref); |
| 2724 | |
| 2725 | item_off = btrfs_item_ptr_offset(leaf, slot) |
| 2726 | + sizeof(struct btrfs_root_ref); |
| 2727 | item_len = btrfs_item_size_nr(leaf, slot) |
| 2728 | - sizeof(struct btrfs_root_ref); |
| 2729 | read_extent_buffer(leaf, subvol_info->name, |
| 2730 | item_off, item_len); |
| 2731 | } else { |
| 2732 | ret = -ENOENT; |
| 2733 | goto out; |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | if (copy_to_user(argp, subvol_info, sizeof(*subvol_info))) |
| 2738 | ret = -EFAULT; |
| 2739 | |
| 2740 | out: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2741 | btrfs_put_root(root); |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2742 | out_free: |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2743 | btrfs_free_path(path); |
| 2744 | kzfree(subvol_info); |
| 2745 | return ret; |
| 2746 | } |
| 2747 | |
Tomohiro Misono | 42e4b52 | 2018-05-21 10:09:43 +0900 | [diff] [blame] | 2748 | /* |
| 2749 | * Return ROOT_REF information of the subvolume containing this inode |
| 2750 | * except the subvolume name. |
| 2751 | */ |
| 2752 | static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp) |
| 2753 | { |
| 2754 | struct btrfs_ioctl_get_subvol_rootref_args *rootrefs; |
| 2755 | struct btrfs_root_ref *rref; |
| 2756 | struct btrfs_root *root; |
| 2757 | struct btrfs_path *path; |
| 2758 | struct btrfs_key key; |
| 2759 | struct extent_buffer *leaf; |
| 2760 | struct inode *inode; |
| 2761 | u64 objectid; |
| 2762 | int slot; |
| 2763 | int ret; |
| 2764 | u8 found; |
| 2765 | |
| 2766 | path = btrfs_alloc_path(); |
| 2767 | if (!path) |
| 2768 | return -ENOMEM; |
| 2769 | |
| 2770 | rootrefs = memdup_user(argp, sizeof(*rootrefs)); |
| 2771 | if (IS_ERR(rootrefs)) { |
| 2772 | btrfs_free_path(path); |
| 2773 | return PTR_ERR(rootrefs); |
| 2774 | } |
| 2775 | |
| 2776 | inode = file_inode(file); |
| 2777 | root = BTRFS_I(inode)->root->fs_info->tree_root; |
| 2778 | objectid = BTRFS_I(inode)->root->root_key.objectid; |
| 2779 | |
| 2780 | key.objectid = objectid; |
| 2781 | key.type = BTRFS_ROOT_REF_KEY; |
| 2782 | key.offset = rootrefs->min_treeid; |
| 2783 | found = 0; |
| 2784 | |
| 2785 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2786 | if (ret < 0) { |
| 2787 | goto out; |
| 2788 | } else if (path->slots[0] >= |
| 2789 | btrfs_header_nritems(path->nodes[0])) { |
| 2790 | ret = btrfs_next_leaf(root, path); |
| 2791 | if (ret < 0) { |
| 2792 | goto out; |
| 2793 | } else if (ret > 0) { |
| 2794 | ret = -EUCLEAN; |
| 2795 | goto out; |
| 2796 | } |
| 2797 | } |
| 2798 | while (1) { |
| 2799 | leaf = path->nodes[0]; |
| 2800 | slot = path->slots[0]; |
| 2801 | |
| 2802 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2803 | if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) { |
| 2804 | ret = 0; |
| 2805 | goto out; |
| 2806 | } |
| 2807 | |
| 2808 | if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { |
| 2809 | ret = -EOVERFLOW; |
| 2810 | goto out; |
| 2811 | } |
| 2812 | |
| 2813 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); |
| 2814 | rootrefs->rootref[found].treeid = key.offset; |
| 2815 | rootrefs->rootref[found].dirid = |
| 2816 | btrfs_root_ref_dirid(leaf, rref); |
| 2817 | found++; |
| 2818 | |
| 2819 | ret = btrfs_next_item(root, path); |
| 2820 | if (ret < 0) { |
| 2821 | goto out; |
| 2822 | } else if (ret > 0) { |
| 2823 | ret = -EUCLEAN; |
| 2824 | goto out; |
| 2825 | } |
| 2826 | } |
| 2827 | |
| 2828 | out: |
| 2829 | if (!ret || ret == -EOVERFLOW) { |
| 2830 | rootrefs->num_items = found; |
| 2831 | /* update min_treeid for next search */ |
| 2832 | if (found) |
| 2833 | rootrefs->min_treeid = |
| 2834 | rootrefs->rootref[found - 1].treeid + 1; |
| 2835 | if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) |
| 2836 | ret = -EFAULT; |
| 2837 | } |
| 2838 | |
| 2839 | kfree(rootrefs); |
| 2840 | btrfs_free_path(path); |
| 2841 | |
| 2842 | return ret; |
| 2843 | } |
| 2844 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2845 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 2846 | void __user *arg, |
| 2847 | bool destroy_v2) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2848 | { |
Al Viro | 54563d4 | 2013-09-01 15:57:51 -0400 | [diff] [blame] | 2849 | struct dentry *parent = file->f_path.dentry; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2850 | struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2851 | struct dentry *dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2852 | struct inode *dir = d_inode(parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2853 | struct inode *inode; |
| 2854 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2855 | struct btrfs_root *dest = NULL; |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 2856 | struct btrfs_ioctl_vol_args *vol_args = NULL; |
| 2857 | struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL; |
| 2858 | char *subvol_name, *subvol_name_ptr = NULL; |
| 2859 | int subvol_namelen; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2860 | int err = 0; |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 2861 | bool destroy_parent = false; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2862 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 2863 | if (destroy_v2) { |
| 2864 | vol_args2 = memdup_user(arg, sizeof(*vol_args2)); |
| 2865 | if (IS_ERR(vol_args2)) |
| 2866 | return PTR_ERR(vol_args2); |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 2867 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 2868 | if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) { |
| 2869 | err = -EOPNOTSUPP; |
| 2870 | goto out; |
| 2871 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2872 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 2873 | /* |
| 2874 | * If SPEC_BY_ID is not set, we are looking for the subvolume by |
| 2875 | * name, same as v1 currently does. |
| 2876 | */ |
| 2877 | if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) { |
| 2878 | vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0; |
| 2879 | subvol_name = vol_args2->name; |
| 2880 | |
| 2881 | err = mnt_want_write_file(file); |
| 2882 | if (err) |
| 2883 | goto out; |
| 2884 | } else { |
| 2885 | if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) { |
| 2886 | err = -EINVAL; |
| 2887 | goto out; |
| 2888 | } |
| 2889 | |
| 2890 | err = mnt_want_write_file(file); |
| 2891 | if (err) |
| 2892 | goto out; |
| 2893 | |
| 2894 | dentry = btrfs_get_dentry(fs_info->sb, |
| 2895 | BTRFS_FIRST_FREE_OBJECTID, |
| 2896 | vol_args2->subvolid, 0, 0); |
| 2897 | if (IS_ERR(dentry)) { |
| 2898 | err = PTR_ERR(dentry); |
| 2899 | goto out_drop_write; |
| 2900 | } |
| 2901 | |
| 2902 | /* |
| 2903 | * Change the default parent since the subvolume being |
| 2904 | * deleted can be outside of the current mount point. |
| 2905 | */ |
| 2906 | parent = btrfs_get_parent(dentry); |
| 2907 | |
| 2908 | /* |
| 2909 | * At this point dentry->d_name can point to '/' if the |
| 2910 | * subvolume we want to destroy is outsite of the |
| 2911 | * current mount point, so we need to release the |
| 2912 | * current dentry and execute the lookup to return a new |
| 2913 | * one with ->d_name pointing to the |
| 2914 | * <mount point>/subvol_name. |
| 2915 | */ |
| 2916 | dput(dentry); |
| 2917 | if (IS_ERR(parent)) { |
| 2918 | err = PTR_ERR(parent); |
| 2919 | goto out_drop_write; |
| 2920 | } |
| 2921 | dir = d_inode(parent); |
| 2922 | |
| 2923 | /* |
| 2924 | * If v2 was used with SPEC_BY_ID, a new parent was |
| 2925 | * allocated since the subvolume can be outside of the |
| 2926 | * current mount point. Later on we need to release this |
| 2927 | * new parent dentry. |
| 2928 | */ |
| 2929 | destroy_parent = true; |
| 2930 | |
| 2931 | subvol_name_ptr = btrfs_get_subvol_name_from_objectid( |
| 2932 | fs_info, vol_args2->subvolid); |
| 2933 | if (IS_ERR(subvol_name_ptr)) { |
| 2934 | err = PTR_ERR(subvol_name_ptr); |
| 2935 | goto free_parent; |
| 2936 | } |
| 2937 | /* subvol_name_ptr is already NULL termined */ |
| 2938 | subvol_name = (char *)kbasename(subvol_name_ptr); |
| 2939 | } |
| 2940 | } else { |
| 2941 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 2942 | if (IS_ERR(vol_args)) |
| 2943 | return PTR_ERR(vol_args); |
| 2944 | |
| 2945 | vol_args->name[BTRFS_PATH_NAME_MAX] = 0; |
| 2946 | subvol_name = vol_args->name; |
| 2947 | |
| 2948 | err = mnt_want_write_file(file); |
| 2949 | if (err) |
| 2950 | goto out; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2951 | } |
| 2952 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 2953 | subvol_namelen = strlen(subvol_name); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2954 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 2955 | if (strchr(subvol_name, '/') || |
| 2956 | strncmp(subvol_name, "..", subvol_namelen) == 0) { |
| 2957 | err = -EINVAL; |
| 2958 | goto free_subvol_name; |
| 2959 | } |
| 2960 | |
| 2961 | if (!S_ISDIR(dir->i_mode)) { |
| 2962 | err = -ENOTDIR; |
| 2963 | goto free_subvol_name; |
| 2964 | } |
David Sterba | 521e054 | 2014-04-15 16:41:44 +0200 | [diff] [blame] | 2965 | |
Al Viro | 0023541 | 2016-05-26 00:05:12 -0400 | [diff] [blame] | 2966 | err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
| 2967 | if (err == -EINTR) |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 2968 | goto free_subvol_name; |
| 2969 | dentry = lookup_one_len(subvol_name, parent, subvol_namelen); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2970 | if (IS_ERR(dentry)) { |
| 2971 | err = PTR_ERR(dentry); |
| 2972 | goto out_unlock_dir; |
| 2973 | } |
| 2974 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2975 | if (d_really_is_negative(dentry)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2976 | err = -ENOENT; |
| 2977 | goto out_dput; |
| 2978 | } |
| 2979 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2980 | inode = d_inode(dentry); |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 2981 | dest = BTRFS_I(inode)->root; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2982 | if (!capable(CAP_SYS_ADMIN)) { |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 2983 | /* |
| 2984 | * Regular user. Only allow this with a special mount |
| 2985 | * option, when the user has write+exec access to the |
| 2986 | * subvol root, and when rmdir(2) would have been |
| 2987 | * allowed. |
| 2988 | * |
| 2989 | * Note that this is _not_ check that the subvol is |
| 2990 | * empty or doesn't contain data that we wouldn't |
| 2991 | * otherwise be able to delete. |
| 2992 | * |
| 2993 | * Users who want to delete empty subvols should try |
| 2994 | * rmdir(2). |
| 2995 | */ |
| 2996 | err = -EPERM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2997 | if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 2998 | goto out_dput; |
| 2999 | |
| 3000 | /* |
| 3001 | * Do not allow deletion if the parent dir is the same |
| 3002 | * as the dir to be deleted. That means the ioctl |
| 3003 | * must be called on the dentry referencing the root |
| 3004 | * of the subvol, not a random directory contained |
| 3005 | * within it. |
| 3006 | */ |
| 3007 | err = -EINVAL; |
| 3008 | if (root == dest) |
| 3009 | goto out_dput; |
| 3010 | |
| 3011 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); |
| 3012 | if (err) |
| 3013 | goto out_dput; |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 3014 | } |
| 3015 | |
Miao Xie | 5c39da5 | 2012-10-22 11:39:53 +0000 | [diff] [blame] | 3016 | /* check if subvolume may be deleted by a user */ |
| 3017 | err = btrfs_may_delete(dir, dentry, 1); |
| 3018 | if (err) |
| 3019 | goto out_dput; |
| 3020 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3021 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3022 | err = -EINVAL; |
| 3023 | goto out_dput; |
| 3024 | } |
| 3025 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3026 | inode_lock(inode); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 3027 | err = btrfs_delete_subvolume(dir, dentry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3028 | inode_unlock(inode); |
Amir Goldstein | 46008d9 | 2019-05-26 17:34:03 +0300 | [diff] [blame] | 3029 | if (!err) { |
| 3030 | fsnotify_rmdir(dir, dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3031 | d_delete(dentry); |
Amir Goldstein | 46008d9 | 2019-05-26 17:34:03 +0300 | [diff] [blame] | 3032 | } |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 3033 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3034 | out_dput: |
| 3035 | dput(dentry); |
| 3036 | out_unlock_dir: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 3037 | inode_unlock(dir); |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 3038 | free_subvol_name: |
| 3039 | kfree(subvol_name_ptr); |
| 3040 | free_parent: |
| 3041 | if (destroy_parent) |
| 3042 | dput(parent); |
Al Viro | 0023541 | 2016-05-26 00:05:12 -0400 | [diff] [blame] | 3043 | out_drop_write: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 3044 | mnt_drop_write_file(file); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3045 | out: |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 3046 | kfree(vol_args2); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3047 | kfree(vol_args); |
| 3048 | return err; |
| 3049 | } |
| 3050 | |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3051 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3052 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3053 | struct inode *inode = file_inode(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3054 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3055 | struct btrfs_ioctl_defrag_range_args *range; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3056 | int ret; |
| 3057 | |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3058 | ret = mnt_want_write_file(file); |
| 3059 | if (ret) |
| 3060 | return ret; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3061 | |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3062 | if (btrfs_root_readonly(root)) { |
| 3063 | ret = -EROFS; |
| 3064 | goto out; |
Stefan Behrens | 5ac00ad | 2012-11-05 17:54:08 +0100 | [diff] [blame] | 3065 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3066 | |
| 3067 | switch (inode->i_mode & S_IFMT) { |
| 3068 | case S_IFDIR: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3069 | if (!capable(CAP_SYS_ADMIN)) { |
| 3070 | ret = -EPERM; |
| 3071 | goto out; |
| 3072 | } |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 3073 | ret = btrfs_defrag_root(root); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3074 | break; |
| 3075 | case S_IFREG: |
Adam Borowski | 616d374 | 2018-07-18 00:08:59 +0200 | [diff] [blame] | 3076 | /* |
| 3077 | * Note that this does not check the file descriptor for write |
| 3078 | * access. This prevents defragmenting executables that are |
| 3079 | * running and allows defrag on files open in read-only mode. |
| 3080 | */ |
| 3081 | if (!capable(CAP_SYS_ADMIN) && |
| 3082 | inode_permission(inode, MAY_WRITE)) { |
| 3083 | ret = -EPERM; |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3084 | goto out; |
| 3085 | } |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3086 | |
| 3087 | range = kzalloc(sizeof(*range), GFP_KERNEL); |
| 3088 | if (!range) { |
| 3089 | ret = -ENOMEM; |
| 3090 | goto out; |
| 3091 | } |
| 3092 | |
| 3093 | if (argp) { |
| 3094 | if (copy_from_user(range, argp, |
| 3095 | sizeof(*range))) { |
| 3096 | ret = -EFAULT; |
| 3097 | kfree(range); |
Dan Carpenter | 683be16 | 2010-03-20 11:24:48 +0000 | [diff] [blame] | 3098 | goto out; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3099 | } |
| 3100 | /* compression requires us to start the IO */ |
| 3101 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { |
| 3102 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; |
| 3103 | range->extent_thresh = (u32)-1; |
| 3104 | } |
| 3105 | } else { |
| 3106 | /* the rest are all set to zero by kzalloc */ |
| 3107 | range->len = (u64)-1; |
| 3108 | } |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3109 | ret = btrfs_defrag_file(file_inode(file), file, |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 3110 | range, BTRFS_OLDEST_GENERATION, 0); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 3111 | if (ret > 0) |
| 3112 | ret = 0; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3113 | kfree(range); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3114 | break; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3115 | default: |
| 3116 | ret = -EINVAL; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3117 | } |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3118 | out: |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3119 | mnt_drop_write_file(file); |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3120 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3121 | } |
| 3122 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3123 | 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] | 3124 | { |
| 3125 | struct btrfs_ioctl_vol_args *vol_args; |
| 3126 | int ret; |
| 3127 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3128 | if (!capable(CAP_SYS_ADMIN)) |
| 3129 | return -EPERM; |
| 3130 | |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3131 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
Anand Jain | e57138b | 2013-08-21 11:44:48 +0800 | [diff] [blame] | 3132 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3133 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 3134 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3135 | if (IS_ERR(vol_args)) { |
| 3136 | ret = PTR_ERR(vol_args); |
| 3137 | goto out; |
| 3138 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3139 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 3140 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3141 | ret = btrfs_init_new_device(fs_info, vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3142 | |
Anand Jain | 43d2076 | 2014-07-01 00:58:56 +0800 | [diff] [blame] | 3143 | if (!ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3144 | btrfs_info(fs_info, "disk added %s", vol_args->name); |
Anand Jain | 43d2076 | 2014-07-01 00:58:56 +0800 | [diff] [blame] | 3145 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3146 | kfree(vol_args); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3147 | out: |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3148 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3149 | return ret; |
| 3150 | } |
| 3151 | |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3152 | 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] | 3153 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3154 | struct inode *inode = file_inode(file); |
| 3155 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3156 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3157 | int ret; |
| 3158 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3159 | if (!capable(CAP_SYS_ADMIN)) |
| 3160 | return -EPERM; |
| 3161 | |
Miao Xie | da24927 | 2012-11-26 08:44:50 +0000 | [diff] [blame] | 3162 | ret = mnt_want_write_file(file); |
| 3163 | if (ret) |
| 3164 | return ret; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3165 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 3166 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3167 | if (IS_ERR(vol_args)) { |
| 3168 | ret = PTR_ERR(vol_args); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 3169 | goto err_drop; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3170 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3171 | |
David Sterba | 748449cdb | 2020-02-21 13:30:14 +0100 | [diff] [blame] | 3172 | if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) { |
Omar Sandoval | fd4e994 | 2018-05-22 15:44:01 -0700 | [diff] [blame] | 3173 | ret = -EOPNOTSUPP; |
| 3174 | goto out; |
| 3175 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3176 | |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3177 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 3178 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
| 3179 | goto out; |
| 3180 | } |
| 3181 | |
David Sterba | 735654e | 2016-02-15 18:15:21 +0100 | [diff] [blame] | 3182 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3183 | ret = btrfs_rm_device(fs_info, NULL, vol_args->devid); |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3184 | } else { |
| 3185 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3186 | ret = btrfs_rm_device(fs_info, vol_args->name, 0); |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3187 | } |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3188 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 3189 | |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3190 | if (!ret) { |
David Sterba | 735654e | 2016-02-15 18:15:21 +0100 | [diff] [blame] | 3191 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3192 | btrfs_info(fs_info, "device deleted: id %llu", |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3193 | vol_args->devid); |
| 3194 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3195 | btrfs_info(fs_info, "device deleted: %s", |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3196 | vol_args->name); |
| 3197 | } |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 3198 | out: |
| 3199 | kfree(vol_args); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 3200 | err_drop: |
Ilya Dryomov | 4ac20c7 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3201 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3202 | return ret; |
| 3203 | } |
| 3204 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3205 | static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) |
| 3206 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3207 | struct inode *inode = file_inode(file); |
| 3208 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3209 | struct btrfs_ioctl_vol_args *vol_args; |
| 3210 | int ret; |
| 3211 | |
| 3212 | if (!capable(CAP_SYS_ADMIN)) |
| 3213 | return -EPERM; |
| 3214 | |
| 3215 | ret = mnt_want_write_file(file); |
| 3216 | if (ret) |
| 3217 | return ret; |
| 3218 | |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3219 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3220 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
David Sterba | 58d7bbf | 2016-05-04 14:10:47 +0200 | [diff] [blame] | 3221 | goto out_drop_write; |
| 3222 | } |
| 3223 | |
| 3224 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 3225 | if (IS_ERR(vol_args)) { |
| 3226 | ret = PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3227 | goto out; |
| 3228 | } |
| 3229 | |
David Sterba | 58d7bbf | 2016-05-04 14:10:47 +0200 | [diff] [blame] | 3230 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3231 | ret = btrfs_rm_device(fs_info, vol_args->name, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3232 | |
| 3233 | if (!ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3234 | btrfs_info(fs_info, "disk deleted %s", vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3235 | kfree(vol_args); |
David Sterba | 58d7bbf | 2016-05-04 14:10:47 +0200 | [diff] [blame] | 3236 | out: |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 3237 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
David Sterba | 58d7bbf | 2016-05-04 14:10:47 +0200 | [diff] [blame] | 3238 | out_drop_write: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3239 | mnt_drop_write_file(file); |
David Sterba | 58d7bbf | 2016-05-04 14:10:47 +0200 | [diff] [blame] | 3240 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3241 | return ret; |
| 3242 | } |
| 3243 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3244 | static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, |
| 3245 | void __user *arg) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3246 | { |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3247 | struct btrfs_ioctl_fs_info_args *fi_args; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3248 | struct btrfs_device *device; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3249 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3250 | int ret = 0; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3251 | |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3252 | fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); |
| 3253 | if (!fi_args) |
| 3254 | return -ENOMEM; |
| 3255 | |
David Sterba | d03262c | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3256 | rcu_read_lock(); |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3257 | fi_args->num_devices = fs_devices->num_devices; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3258 | |
David Sterba | d03262c | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3259 | list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3260 | if (device->devid > fi_args->max_id) |
| 3261 | fi_args->max_id = device->devid; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3262 | } |
David Sterba | d03262c | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3263 | rcu_read_unlock(); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3264 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 3265 | memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid)); |
Omar Sandoval | bea7eaf | 2017-08-22 23:46:00 -0700 | [diff] [blame] | 3266 | fi_args->nodesize = fs_info->nodesize; |
| 3267 | fi_args->sectorsize = fs_info->sectorsize; |
| 3268 | fi_args->clone_alignment = fs_info->sectorsize; |
David Sterba | 80a773f | 2014-05-07 18:17:06 +0200 | [diff] [blame] | 3269 | |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3270 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) |
| 3271 | ret = -EFAULT; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3272 | |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3273 | kfree(fi_args); |
| 3274 | return ret; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3275 | } |
| 3276 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3277 | static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, |
| 3278 | void __user *arg) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3279 | { |
| 3280 | struct btrfs_ioctl_dev_info_args *di_args; |
| 3281 | struct btrfs_device *dev; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3282 | int ret = 0; |
| 3283 | char *s_uuid = NULL; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3284 | |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3285 | di_args = memdup_user(arg, sizeof(*di_args)); |
| 3286 | if (IS_ERR(di_args)) |
| 3287 | return PTR_ERR(di_args); |
| 3288 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 3289 | if (!btrfs_is_empty_uuid(di_args->uuid)) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3290 | s_uuid = di_args->uuid; |
| 3291 | |
David Sterba | c5593ca | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3292 | rcu_read_lock(); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 3293 | 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] | 3294 | NULL, true); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3295 | |
| 3296 | if (!dev) { |
| 3297 | ret = -ENODEV; |
| 3298 | goto out; |
| 3299 | } |
| 3300 | |
| 3301 | di_args->devid = dev->devid; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3302 | di_args->bytes_used = btrfs_device_get_bytes_used(dev); |
| 3303 | di_args->total_bytes = btrfs_device_get_total_bytes(dev); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3304 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 3305 | if (dev->name) { |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 3306 | strncpy(di_args->path, rcu_str_deref(dev->name), |
| 3307 | sizeof(di_args->path) - 1); |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 3308 | di_args->path[sizeof(di_args->path) - 1] = 0; |
| 3309 | } else { |
Stefan Behrens | 99ba55a | 2012-03-19 16:17:22 +0100 | [diff] [blame] | 3310 | di_args->path[0] = '\0'; |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 3311 | } |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3312 | |
| 3313 | out: |
David Sterba | c5593ca | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3314 | rcu_read_unlock(); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3315 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) |
| 3316 | ret = -EFAULT; |
| 3317 | |
| 3318 | kfree(di_args); |
| 3319 | return ret; |
| 3320 | } |
| 3321 | |
Filipe Manana | d8b5524 | 2019-01-08 11:43:07 +0000 | [diff] [blame] | 3322 | static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, |
| 3323 | struct inode *inode2, u64 loff2, u64 len) |
| 3324 | { |
| 3325 | unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); |
| 3326 | unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); |
| 3327 | } |
| 3328 | |
| 3329 | static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1, |
| 3330 | struct inode *inode2, u64 loff2, u64 len) |
| 3331 | { |
| 3332 | if (inode1 < inode2) { |
| 3333 | swap(inode1, inode2); |
| 3334 | swap(loff1, loff2); |
| 3335 | } else if (inode1 == inode2 && loff2 < loff1) { |
| 3336 | swap(loff1, loff2); |
| 3337 | } |
| 3338 | lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); |
| 3339 | lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); |
| 3340 | } |
| 3341 | |
Filipe Manana | 57a50e2 | 2018-12-12 18:05:59 +0000 | [diff] [blame] | 3342 | static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len, |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 3343 | struct inode *dst, u64 dst_loff) |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 3344 | { |
Filipe Manana | 831d2fa | 2019-12-16 18:26:56 +0000 | [diff] [blame] | 3345 | const u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 3346 | int ret; |
Mark Fasheh | f441460 | 2015-06-30 14:42:05 -0700 | [diff] [blame] | 3347 | |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 3348 | /* |
Filipe Manana | d8b5524 | 2019-01-08 11:43:07 +0000 | [diff] [blame] | 3349 | * Lock destination range to serialize with concurrent readpages() and |
| 3350 | * source range to serialize with relocation. |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 3351 | */ |
Filipe Manana | d8b5524 | 2019-01-08 11:43:07 +0000 | [diff] [blame] | 3352 | btrfs_double_extent_lock(src, loff, dst, dst_loff, len); |
Filipe Manana | 831d2fa | 2019-12-16 18:26:56 +0000 | [diff] [blame] | 3353 | ret = btrfs_clone(src, dst, loff, len, ALIGN(len, bs), dst_loff, 1); |
Filipe Manana | d8b5524 | 2019-01-08 11:43:07 +0000 | [diff] [blame] | 3354 | btrfs_double_extent_unlock(src, loff, dst, dst_loff, len); |
Timofey Titovets | 3973909 | 2018-05-02 08:15:36 +0300 | [diff] [blame] | 3355 | |
| 3356 | return ret; |
| 3357 | } |
| 3358 | |
Timofey Titovets | b672876 | 2018-05-02 08:15:37 +0300 | [diff] [blame] | 3359 | #define BTRFS_MAX_DEDUPE_LEN SZ_16M |
| 3360 | |
Timofey Titovets | 3973909 | 2018-05-02 08:15:36 +0300 | [diff] [blame] | 3361 | static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, |
| 3362 | struct inode *dst, u64 dst_loff) |
| 3363 | { |
| 3364 | int ret; |
Timofey Titovets | b672876 | 2018-05-02 08:15:37 +0300 | [diff] [blame] | 3365 | u64 i, tail_len, chunk_count; |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 3366 | struct btrfs_root *root_dst = BTRFS_I(dst)->root; |
| 3367 | |
| 3368 | spin_lock(&root_dst->root_item_lock); |
| 3369 | if (root_dst->send_in_progress) { |
| 3370 | btrfs_warn_rl(root_dst->fs_info, |
| 3371 | "cannot deduplicate to root %llu while send operations are using it (%d in progress)", |
| 3372 | root_dst->root_key.objectid, |
| 3373 | root_dst->send_in_progress); |
| 3374 | spin_unlock(&root_dst->root_item_lock); |
| 3375 | return -EAGAIN; |
| 3376 | } |
| 3377 | root_dst->dedupe_in_progress++; |
| 3378 | spin_unlock(&root_dst->root_item_lock); |
Timofey Titovets | 3973909 | 2018-05-02 08:15:36 +0300 | [diff] [blame] | 3379 | |
Timofey Titovets | b672876 | 2018-05-02 08:15:37 +0300 | [diff] [blame] | 3380 | tail_len = olen % BTRFS_MAX_DEDUPE_LEN; |
| 3381 | chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN); |
Timofey Titovets | 67b07bd | 2018-05-02 08:15:38 +0300 | [diff] [blame] | 3382 | |
Timofey Titovets | b672876 | 2018-05-02 08:15:37 +0300 | [diff] [blame] | 3383 | for (i = 0; i < chunk_count; i++) { |
| 3384 | ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN, |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 3385 | dst, dst_loff); |
Timofey Titovets | b672876 | 2018-05-02 08:15:37 +0300 | [diff] [blame] | 3386 | if (ret) |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 3387 | goto out; |
Timofey Titovets | b672876 | 2018-05-02 08:15:37 +0300 | [diff] [blame] | 3388 | |
| 3389 | loff += BTRFS_MAX_DEDUPE_LEN; |
| 3390 | dst_loff += BTRFS_MAX_DEDUPE_LEN; |
| 3391 | } |
| 3392 | |
| 3393 | if (tail_len > 0) |
Timofey Titovets | 67b07bd | 2018-05-02 08:15:38 +0300 | [diff] [blame] | 3394 | ret = btrfs_extent_same_range(src, loff, tail_len, dst, |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 3395 | dst_loff); |
Filipe Manana | 62d54f3 | 2019-04-22 16:43:42 +0100 | [diff] [blame] | 3396 | out: |
| 3397 | spin_lock(&root_dst->root_item_lock); |
| 3398 | root_dst->dedupe_in_progress--; |
| 3399 | spin_unlock(&root_dst->root_item_lock); |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 3400 | |
| 3401 | return ret; |
| 3402 | } |
| 3403 | |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3404 | static int clone_finish_inode_update(struct btrfs_trans_handle *trans, |
| 3405 | struct inode *inode, |
| 3406 | u64 endoff, |
| 3407 | const u64 destoff, |
Mark Fasheh | 1c919a5 | 2015-06-30 14:42:08 -0700 | [diff] [blame] | 3408 | const u64 olen, |
| 3409 | int no_time_update) |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3410 | { |
| 3411 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3412 | int ret; |
| 3413 | |
| 3414 | inode_inc_iversion(inode); |
Mark Fasheh | 1c919a5 | 2015-06-30 14:42:08 -0700 | [diff] [blame] | 3415 | if (!no_time_update) |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 3416 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3417 | /* |
| 3418 | * We round up to the block size at eof when determining which |
| 3419 | * extents to clone above, but shouldn't round up the file size. |
| 3420 | */ |
| 3421 | if (endoff > destoff + olen) |
| 3422 | endoff = destoff + olen; |
Josef Bacik | 790a1d4 | 2020-01-17 09:02:19 -0500 | [diff] [blame] | 3423 | if (endoff > inode->i_size) { |
| 3424 | i_size_write(inode, endoff); |
Josef Bacik | d923afe | 2020-01-17 09:02:23 -0500 | [diff] [blame] | 3425 | btrfs_inode_safe_disk_i_size_write(inode, 0); |
Josef Bacik | 790a1d4 | 2020-01-17 09:02:19 -0500 | [diff] [blame] | 3426 | } |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3427 | |
| 3428 | ret = btrfs_update_inode(trans, root, inode); |
| 3429 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3430 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3431 | btrfs_end_transaction(trans); |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3432 | goto out; |
| 3433 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3434 | ret = btrfs_end_transaction(trans); |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3435 | out: |
| 3436 | return ret; |
| 3437 | } |
| 3438 | |
Filipe Manana | 8039d87 | 2015-10-13 15:15:00 +0100 | [diff] [blame] | 3439 | /* |
| 3440 | * Make sure we do not end up inserting an inline extent into a file that has |
| 3441 | * already other (non-inline) extents. If a file has an inline extent it can |
| 3442 | * not have any other extents and the (single) inline extent must start at the |
| 3443 | * file offset 0. Failing to respect these rules will lead to file corruption, |
| 3444 | * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc |
| 3445 | * |
| 3446 | * We can have extents that have been already written to disk or we can have |
| 3447 | * dirty ranges still in delalloc, in which case the extent maps and items are |
| 3448 | * created only when we run delalloc, and the delalloc ranges might fall outside |
| 3449 | * the range we are currently locking in the inode's io tree. So we check the |
| 3450 | * inode's i_size because of that (i_size updates are done while holding the |
| 3451 | * i_mutex, which we are holding here). |
| 3452 | * We also check to see if the inode has a size not greater than "datal" but has |
| 3453 | * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are |
| 3454 | * protected against such concurrent fallocate calls by the i_mutex). |
| 3455 | * |
| 3456 | * If the file has no extents but a size greater than datal, do not allow the |
| 3457 | * copy because we would need turn the inline extent into a non-inline one (even |
| 3458 | * with NO_HOLES enabled). If we find our destination inode only has one inline |
| 3459 | * extent, just overwrite it with the source inline extent if its size is less |
| 3460 | * than the source extent's size, or we could copy the source inline extent's |
| 3461 | * data into the destination inode's inline extent if the later is greater then |
| 3462 | * the former. |
| 3463 | */ |
David Sterba | 4a0ab9d | 2017-02-10 20:18:49 +0100 | [diff] [blame] | 3464 | static int clone_copy_inline_extent(struct inode *dst, |
Filipe Manana | 8039d87 | 2015-10-13 15:15:00 +0100 | [diff] [blame] | 3465 | struct btrfs_trans_handle *trans, |
| 3466 | struct btrfs_path *path, |
| 3467 | struct btrfs_key *new_key, |
| 3468 | const u64 drop_start, |
| 3469 | const u64 datal, |
| 3470 | const u64 skip, |
| 3471 | const u64 size, |
| 3472 | char *inline_data) |
| 3473 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3474 | struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb); |
Filipe Manana | 8039d87 | 2015-10-13 15:15:00 +0100 | [diff] [blame] | 3475 | struct btrfs_root *root = BTRFS_I(dst)->root; |
| 3476 | const u64 aligned_end = ALIGN(new_key->offset + datal, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3477 | fs_info->sectorsize); |
Filipe Manana | 8039d87 | 2015-10-13 15:15:00 +0100 | [diff] [blame] | 3478 | int ret; |
| 3479 | struct btrfs_key key; |
| 3480 | |
| 3481 | if (new_key->offset > 0) |
| 3482 | return -EOPNOTSUPP; |
| 3483 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3484 | key.objectid = btrfs_ino(BTRFS_I(dst)); |
Filipe Manana | 8039d87 | 2015-10-13 15:15:00 +0100 | [diff] [blame] | 3485 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 3486 | key.offset = 0; |
| 3487 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3488 | if (ret < 0) { |
| 3489 | return ret; |
| 3490 | } else if (ret > 0) { |
| 3491 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 3492 | ret = btrfs_next_leaf(root, path); |
| 3493 | if (ret < 0) |
| 3494 | return ret; |
| 3495 | else if (ret > 0) |
| 3496 | goto copy_inline_extent; |
| 3497 | } |
| 3498 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3499 | if (key.objectid == btrfs_ino(BTRFS_I(dst)) && |
Filipe Manana | 8039d87 | 2015-10-13 15:15:00 +0100 | [diff] [blame] | 3500 | key.type == BTRFS_EXTENT_DATA_KEY) { |
| 3501 | ASSERT(key.offset > 0); |
| 3502 | return -EOPNOTSUPP; |
| 3503 | } |
| 3504 | } else if (i_size_read(dst) <= datal) { |
| 3505 | struct btrfs_file_extent_item *ei; |
| 3506 | u64 ext_len; |
| 3507 | |
| 3508 | /* |
| 3509 | * If the file size is <= datal, make sure there are no other |
| 3510 | * extents following (can happen do to an fallocate call with |
| 3511 | * the flag FALLOC_FL_KEEP_SIZE). |
| 3512 | */ |
| 3513 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3514 | struct btrfs_file_extent_item); |
| 3515 | /* |
| 3516 | * If it's an inline extent, it can not have other extents |
| 3517 | * following it. |
| 3518 | */ |
| 3519 | if (btrfs_file_extent_type(path->nodes[0], ei) == |
| 3520 | BTRFS_FILE_EXTENT_INLINE) |
| 3521 | goto copy_inline_extent; |
| 3522 | |
| 3523 | ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei); |
| 3524 | if (ext_len > aligned_end) |
| 3525 | return -EOPNOTSUPP; |
| 3526 | |
| 3527 | ret = btrfs_next_item(root, path); |
| 3528 | if (ret < 0) { |
| 3529 | return ret; |
| 3530 | } else if (ret == 0) { |
| 3531 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 3532 | path->slots[0]); |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3533 | if (key.objectid == btrfs_ino(BTRFS_I(dst)) && |
Filipe Manana | 8039d87 | 2015-10-13 15:15:00 +0100 | [diff] [blame] | 3534 | key.type == BTRFS_EXTENT_DATA_KEY) |
| 3535 | return -EOPNOTSUPP; |
| 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | copy_inline_extent: |
| 3540 | /* |
| 3541 | * We have no extent items, or we have an extent at offset 0 which may |
| 3542 | * or may not be inlined. All these cases are dealt the same way. |
| 3543 | */ |
| 3544 | if (i_size_read(dst) > datal) { |
| 3545 | /* |
| 3546 | * If the destination inode has an inline extent... |
| 3547 | * This would require copying the data from the source inline |
| 3548 | * extent into the beginning of the destination's inline extent. |
| 3549 | * But this is really complex, both extents can be compressed |
| 3550 | * or just one of them, which would require decompressing and |
| 3551 | * re-compressing data (which could increase the new compressed |
| 3552 | * size, not allowing the compressed data to fit anymore in an |
| 3553 | * inline extent). |
| 3554 | * So just don't support this case for now (it should be rare, |
| 3555 | * we are not really saving space when cloning inline extents). |
| 3556 | */ |
| 3557 | return -EOPNOTSUPP; |
| 3558 | } |
| 3559 | |
| 3560 | btrfs_release_path(path); |
| 3561 | ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1); |
| 3562 | if (ret) |
| 3563 | return ret; |
| 3564 | ret = btrfs_insert_empty_item(trans, root, path, new_key, size); |
| 3565 | if (ret) |
| 3566 | return ret; |
| 3567 | |
| 3568 | if (skip) { |
| 3569 | const u32 start = btrfs_file_extent_calc_inline_size(0); |
| 3570 | |
| 3571 | memmove(inline_data + start, inline_data + start + skip, datal); |
| 3572 | } |
| 3573 | |
| 3574 | write_extent_buffer(path->nodes[0], inline_data, |
| 3575 | btrfs_item_ptr_offset(path->nodes[0], |
| 3576 | path->slots[0]), |
| 3577 | size); |
| 3578 | inode_add_bytes(dst, datal); |
Filipe Manana | 690a5db | 2019-07-05 11:09:50 +0100 | [diff] [blame] | 3579 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(dst)->runtime_flags); |
Filipe Manana | 8039d87 | 2015-10-13 15:15:00 +0100 | [diff] [blame] | 3580 | |
| 3581 | return 0; |
| 3582 | } |
| 3583 | |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3584 | /** |
| 3585 | * btrfs_clone() - clone a range from inode file to another |
| 3586 | * |
| 3587 | * @src: Inode to clone from |
| 3588 | * @inode: Inode to clone to |
| 3589 | * @off: Offset within source to start clone from |
| 3590 | * @olen: Original length, passed by user, of range to clone |
Mark Fasheh | 1c919a5 | 2015-06-30 14:42:08 -0700 | [diff] [blame] | 3591 | * @olen_aligned: Block-aligned value of olen |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3592 | * @destoff: Offset within @inode to start clone |
Mark Fasheh | 1c919a5 | 2015-06-30 14:42:08 -0700 | [diff] [blame] | 3593 | * @no_time_update: Whether to update mtime/ctime on the target inode |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3594 | */ |
| 3595 | static int btrfs_clone(struct inode *src, struct inode *inode, |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3596 | const u64 off, const u64 olen, const u64 olen_aligned, |
Mark Fasheh | 1c919a5 | 2015-06-30 14:42:08 -0700 | [diff] [blame] | 3597 | const u64 destoff, int no_time_update) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3598 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3599 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3600 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3601 | struct btrfs_path *path = NULL; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3602 | struct extent_buffer *leaf; |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3603 | struct btrfs_trans_handle *trans; |
| 3604 | char *buf = NULL; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 3605 | struct btrfs_key key; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3606 | u32 nritems; |
| 3607 | int slot; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 3608 | int ret; |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3609 | const u64 len = olen_aligned; |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3610 | u64 last_dest_end = destoff; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 3611 | |
| 3612 | ret = -ENOMEM; |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 3613 | buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); |
| 3614 | if (!buf) |
| 3615 | return ret; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 3616 | |
| 3617 | path = btrfs_alloc_path(); |
| 3618 | if (!path) { |
David Sterba | 1535195 | 2016-04-11 18:40:08 +0200 | [diff] [blame] | 3619 | kvfree(buf); |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3620 | return ret; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 3621 | } |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3622 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3623 | path->reada = READA_FORWARD; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 3624 | /* clone data */ |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3625 | key.objectid = btrfs_ino(BTRFS_I(src)); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 3626 | key.type = BTRFS_EXTENT_DATA_KEY; |
Filipe Manana | 2c46382 | 2014-05-31 02:31:05 +0100 | [diff] [blame] | 3627 | key.offset = off; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3628 | |
| 3629 | while (1) { |
Chris Mason | de249e6 | 2015-04-11 05:09:06 -0700 | [diff] [blame] | 3630 | u64 next_key_min_offset = key.offset + 1; |
Filipe Manana | b64119b | 2019-07-02 15:23:07 +0100 | [diff] [blame] | 3631 | struct btrfs_file_extent_item *extent; |
| 3632 | int type; |
| 3633 | u32 size; |
| 3634 | struct btrfs_key new_key; |
| 3635 | u64 disko = 0, diskl = 0; |
| 3636 | u64 datao = 0, datal = 0; |
| 3637 | u8 comp; |
| 3638 | u64 drop_start; |
Filipe Manana | df858e7 | 2015-03-31 14:56:46 +0100 | [diff] [blame] | 3639 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3640 | /* |
| 3641 | * note the key will change type as we walk through the |
| 3642 | * tree. |
| 3643 | */ |
Filipe David Borba Manana | e4355f3 | 2014-01-13 19:35:01 +0000 | [diff] [blame] | 3644 | path->leave_spinning = 1; |
David Sterba | 362a20c | 2011-08-01 18:11:57 +0200 | [diff] [blame] | 3645 | ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, |
| 3646 | 0, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3647 | if (ret < 0) |
| 3648 | goto out; |
Filipe Manana | 2c46382 | 2014-05-31 02:31:05 +0100 | [diff] [blame] | 3649 | /* |
| 3650 | * First search, if no extent item that starts at offset off was |
| 3651 | * found but the previous item is an extent item, it's possible |
| 3652 | * it might overlap our target range, therefore process it. |
| 3653 | */ |
| 3654 | if (key.offset == off && ret > 0 && path->slots[0] > 0) { |
| 3655 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 3656 | path->slots[0] - 1); |
| 3657 | if (key.type == BTRFS_EXTENT_DATA_KEY) |
| 3658 | path->slots[0]--; |
| 3659 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3660 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 3661 | nritems = btrfs_header_nritems(path->nodes[0]); |
Filipe David Borba Manana | e4355f3 | 2014-01-13 19:35:01 +0000 | [diff] [blame] | 3662 | process_slot: |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 3663 | if (path->slots[0] >= nritems) { |
David Sterba | 362a20c | 2011-08-01 18:11:57 +0200 | [diff] [blame] | 3664 | ret = btrfs_next_leaf(BTRFS_I(src)->root, path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3665 | if (ret < 0) |
| 3666 | goto out; |
| 3667 | if (ret > 0) |
| 3668 | break; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 3669 | nritems = btrfs_header_nritems(path->nodes[0]); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3670 | } |
| 3671 | leaf = path->nodes[0]; |
| 3672 | slot = path->slots[0]; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3673 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 3674 | btrfs_item_key_to_cpu(leaf, &key, slot); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3675 | if (key.type > BTRFS_EXTENT_DATA_KEY || |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3676 | key.objectid != btrfs_ino(BTRFS_I(src))) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3677 | break; |
| 3678 | |
Filipe Manana | b64119b | 2019-07-02 15:23:07 +0100 | [diff] [blame] | 3679 | ASSERT(key.type == BTRFS_EXTENT_DATA_KEY); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3680 | |
Filipe Manana | b64119b | 2019-07-02 15:23:07 +0100 | [diff] [blame] | 3681 | extent = btrfs_item_ptr(leaf, slot, |
| 3682 | struct btrfs_file_extent_item); |
| 3683 | comp = btrfs_file_extent_compression(leaf, extent); |
| 3684 | type = btrfs_file_extent_type(leaf, extent); |
| 3685 | if (type == BTRFS_FILE_EXTENT_REG || |
| 3686 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 3687 | disko = btrfs_file_extent_disk_bytenr(leaf, extent); |
| 3688 | diskl = btrfs_file_extent_disk_num_bytes(leaf, extent); |
| 3689 | datao = btrfs_file_extent_offset(leaf, extent); |
| 3690 | datal = btrfs_file_extent_num_bytes(leaf, extent); |
| 3691 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 3692 | /* Take upper bound, may be compressed */ |
| 3693 | datal = btrfs_file_extent_ram_bytes(leaf, extent); |
| 3694 | } |
| 3695 | |
| 3696 | /* |
| 3697 | * The first search might have left us at an extent item that |
| 3698 | * ends before our target range's start, can happen if we have |
| 3699 | * holes and NO_HOLES feature enabled. |
| 3700 | */ |
| 3701 | if (key.offset + datal <= off) { |
| 3702 | path->slots[0]++; |
| 3703 | goto process_slot; |
| 3704 | } else if (key.offset >= off + len) { |
| 3705 | break; |
| 3706 | } |
| 3707 | next_key_min_offset = key.offset + datal; |
| 3708 | size = btrfs_item_size_nr(leaf, slot); |
| 3709 | read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot), |
| 3710 | size); |
| 3711 | |
| 3712 | btrfs_release_path(path); |
| 3713 | path->leave_spinning = 0; |
| 3714 | |
| 3715 | memcpy(&new_key, &key, sizeof(new_key)); |
| 3716 | new_key.objectid = btrfs_ino(BTRFS_I(inode)); |
| 3717 | if (off <= key.offset) |
| 3718 | new_key.offset = key.offset + destoff - off; |
| 3719 | else |
| 3720 | new_key.offset = destoff; |
| 3721 | |
| 3722 | /* |
| 3723 | * Deal with a hole that doesn't have an extent item that |
| 3724 | * represents it (NO_HOLES feature enabled). |
| 3725 | * This hole is either in the middle of the cloning range or at |
| 3726 | * the beginning (fully overlaps it or partially overlaps it). |
| 3727 | */ |
| 3728 | if (new_key.offset != last_dest_end) |
| 3729 | drop_start = last_dest_end; |
| 3730 | else |
| 3731 | drop_start = new_key.offset; |
| 3732 | |
| 3733 | if (type == BTRFS_FILE_EXTENT_REG || |
| 3734 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 3735 | struct btrfs_clone_extent_info clone_info; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3736 | |
Filipe Manana | 2c46382 | 2014-05-31 02:31:05 +0100 | [diff] [blame] | 3737 | /* |
Filipe Manana | b64119b | 2019-07-02 15:23:07 +0100 | [diff] [blame] | 3738 | * a | --- range to clone ---| b |
| 3739 | * | ------------- extent ------------- | |
Filipe Manana | 2c46382 | 2014-05-31 02:31:05 +0100 | [diff] [blame] | 3740 | */ |
Filipe Manana | b64119b | 2019-07-02 15:23:07 +0100 | [diff] [blame] | 3741 | |
| 3742 | /* Subtract range b */ |
| 3743 | if (key.offset + datal > off + len) |
| 3744 | datal = off + len - key.offset; |
| 3745 | |
| 3746 | /* Subtract range a */ |
| 3747 | if (off > key.offset) { |
| 3748 | datao += off - key.offset; |
| 3749 | datal -= off - key.offset; |
Filipe David Borba Manana | e4355f3 | 2014-01-13 19:35:01 +0000 | [diff] [blame] | 3750 | } |
Filipe David Borba Manana | e4355f3 | 2014-01-13 19:35:01 +0000 | [diff] [blame] | 3751 | |
Filipe Manana | b64119b | 2019-07-02 15:23:07 +0100 | [diff] [blame] | 3752 | clone_info.disk_offset = disko; |
| 3753 | clone_info.disk_len = diskl; |
| 3754 | clone_info.data_offset = datao; |
| 3755 | clone_info.data_len = datal; |
| 3756 | clone_info.file_offset = new_key.offset; |
| 3757 | clone_info.extent_buf = buf; |
| 3758 | clone_info.item_size = size; |
| 3759 | ret = btrfs_punch_hole_range(inode, path, |
Filipe Manana | 690a5db | 2019-07-05 11:09:50 +0100 | [diff] [blame] | 3760 | drop_start, |
| 3761 | new_key.offset + datal - 1, |
| 3762 | &clone_info, &trans); |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3763 | if (ret) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3764 | goto out; |
Filipe Manana | b64119b | 2019-07-02 15:23:07 +0100 | [diff] [blame] | 3765 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 3766 | u64 skip = 0; |
| 3767 | u64 trim = 0; |
| 3768 | |
| 3769 | if (off > key.offset) { |
| 3770 | skip = off - key.offset; |
| 3771 | new_key.offset += skip; |
| 3772 | } |
| 3773 | |
| 3774 | if (key.offset + datal > off + len) |
| 3775 | trim = key.offset + datal - (off + len); |
| 3776 | |
| 3777 | if (comp && (skip || trim)) { |
| 3778 | ret = -EINVAL; |
| 3779 | goto out; |
| 3780 | } |
| 3781 | size -= skip + trim; |
| 3782 | datal -= skip + trim; |
| 3783 | |
| 3784 | /* |
| 3785 | * If our extent is inline, we know we will drop or |
| 3786 | * adjust at most 1 extent item in the destination root. |
| 3787 | * |
| 3788 | * 1 - adjusting old extent (we may have to split it) |
| 3789 | * 1 - add new extent |
| 3790 | * 1 - inode update |
| 3791 | */ |
| 3792 | trans = btrfs_start_transaction(root, 3); |
| 3793 | if (IS_ERR(trans)) { |
| 3794 | ret = PTR_ERR(trans); |
| 3795 | goto out; |
| 3796 | } |
| 3797 | |
| 3798 | ret = clone_copy_inline_extent(inode, trans, path, |
| 3799 | &new_key, drop_start, |
| 3800 | datal, skip, size, buf); |
| 3801 | if (ret) { |
| 3802 | if (ret != -EOPNOTSUPP) |
| 3803 | btrfs_abort_transaction(trans, ret); |
| 3804 | btrfs_end_transaction(trans); |
| 3805 | goto out; |
| 3806 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3807 | } |
Filipe Manana | b64119b | 2019-07-02 15:23:07 +0100 | [diff] [blame] | 3808 | |
| 3809 | btrfs_release_path(path); |
| 3810 | |
| 3811 | last_dest_end = ALIGN(new_key.offset + datal, |
| 3812 | fs_info->sectorsize); |
| 3813 | ret = clone_finish_inode_update(trans, inode, last_dest_end, |
| 3814 | destoff, olen, no_time_update); |
| 3815 | if (ret) |
| 3816 | goto out; |
| 3817 | if (new_key.offset + datal >= destoff + len) |
| 3818 | break; |
| 3819 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3820 | btrfs_release_path(path); |
Filipe Manana | df858e7 | 2015-03-31 14:56:46 +0100 | [diff] [blame] | 3821 | key.offset = next_key_min_offset; |
Wang Xiaoguang | 69ae5e4 | 2016-10-13 09:23:39 +0800 | [diff] [blame] | 3822 | |
| 3823 | if (fatal_signal_pending(current)) { |
| 3824 | ret = -EINTR; |
| 3825 | goto out; |
| 3826 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3827 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3828 | ret = 0; |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3829 | |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3830 | if (last_dest_end < destoff + len) { |
| 3831 | /* |
Filipe Manana | 147271e | 2019-12-05 16:58:41 +0000 | [diff] [blame] | 3832 | * We have an implicit hole that fully or partially overlaps our |
| 3833 | * cloning range at its end. This means that we either have the |
| 3834 | * NO_HOLES feature enabled or the implicit hole happened due to |
| 3835 | * mixing buffered and direct IO writes against this file. |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3836 | */ |
| 3837 | btrfs_release_path(path); |
Filipe Manana | 690a5db | 2019-07-05 11:09:50 +0100 | [diff] [blame] | 3838 | path->leave_spinning = 0; |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3839 | |
Filipe Manana | 690a5db | 2019-07-05 11:09:50 +0100 | [diff] [blame] | 3840 | ret = btrfs_punch_hole_range(inode, path, |
| 3841 | last_dest_end, destoff + len - 1, |
Filipe Manana | 147271e | 2019-12-05 16:58:41 +0000 | [diff] [blame] | 3842 | NULL, &trans); |
Filipe Manana | 690a5db | 2019-07-05 11:09:50 +0100 | [diff] [blame] | 3843 | if (ret) |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3844 | goto out; |
Filipe Manana | 690a5db | 2019-07-05 11:09:50 +0100 | [diff] [blame] | 3845 | |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3846 | ret = clone_finish_inode_update(trans, inode, destoff + len, |
Mark Fasheh | 1c919a5 | 2015-06-30 14:42:08 -0700 | [diff] [blame] | 3847 | destoff, olen, no_time_update); |
Filipe Manana | f82a990 | 2014-06-01 01:50:28 +0100 | [diff] [blame] | 3848 | } |
| 3849 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3850 | out: |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3851 | btrfs_free_path(path); |
David Sterba | 1535195 | 2016-04-11 18:40:08 +0200 | [diff] [blame] | 3852 | kvfree(buf); |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3853 | return ret; |
| 3854 | } |
| 3855 | |
Zach Brown | 3db11b2 | 2015-11-10 16:53:32 -0500 | [diff] [blame] | 3856 | static noinline int btrfs_clone_files(struct file *file, struct file *file_src, |
| 3857 | u64 off, u64 olen, u64 destoff) |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3858 | { |
Al Viro | 54563d4 | 2013-09-01 15:57:51 -0400 | [diff] [blame] | 3859 | struct inode *inode = file_inode(file); |
Zach Brown | 3db11b2 | 2015-11-10 16:53:32 -0500 | [diff] [blame] | 3860 | struct inode *src = file_inode(file_src); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3861 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3862 | int ret; |
| 3863 | u64 len = olen; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3864 | u64 bs = fs_info->sb->s_blocksize; |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3865 | |
| 3866 | /* |
| 3867 | * TODO: |
| 3868 | * - split compressed inline extents. annoying: we need to |
| 3869 | * decompress into destination's address_space (the file offset |
| 3870 | * may change, so source mapping won't do), then recompress (or |
| 3871 | * otherwise reinsert) a subrange. |
Liu Bo | 00fdf13 | 2014-03-10 18:56:07 +0800 | [diff] [blame] | 3872 | * |
| 3873 | * - split destination inode's inline extents. The inline extents can |
| 3874 | * be either compressed or non-compressed. |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3875 | */ |
| 3876 | |
Filipe Manana | ac765f8 | 2018-11-05 11:14:17 +0000 | [diff] [blame] | 3877 | /* |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 3878 | * VFS's generic_remap_file_range_prep() protects us from cloning the |
| 3879 | * eof block into the middle of a file, which would result in corruption |
| 3880 | * if the file size is not blocksize aligned. So we don't need to check |
| 3881 | * for that case here. |
Filipe Manana | ac765f8 | 2018-11-05 11:14:17 +0000 | [diff] [blame] | 3882 | */ |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 3883 | if (off + len == src->i_size) |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3884 | len = ALIGN(src->i_size, bs) - off; |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3885 | |
| 3886 | if (destoff > inode->i_size) { |
Filipe Manana | f7fa110 | 2019-01-08 11:42:54 +0000 | [diff] [blame] | 3887 | const u64 wb_start = ALIGN_DOWN(inode->i_size, bs); |
| 3888 | |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3889 | ret = btrfs_cont_expand(inode, inode->i_size, destoff); |
| 3890 | if (ret) |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 3891 | return ret; |
Filipe Manana | f7fa110 | 2019-01-08 11:42:54 +0000 | [diff] [blame] | 3892 | /* |
| 3893 | * We may have truncated the last block if the inode's size is |
| 3894 | * not sector size aligned, so we need to wait for writeback to |
| 3895 | * complete before proceeding further, otherwise we can race |
| 3896 | * with cloning and attempt to increment a reference to an |
| 3897 | * extent that no longer exists (writeback completed right after |
| 3898 | * we found the previous extent covering eof and before we |
| 3899 | * attempted to increment its reference count). |
| 3900 | */ |
| 3901 | ret = btrfs_wait_ordered_range(inode, wb_start, |
| 3902 | destoff - wb_start); |
| 3903 | if (ret) |
| 3904 | return ret; |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3905 | } |
| 3906 | |
Filipe Manana | c125b8b | 2014-05-23 05:03:34 +0100 | [diff] [blame] | 3907 | /* |
Filipe Manana | d8b5524 | 2019-01-08 11:43:07 +0000 | [diff] [blame] | 3908 | * Lock destination range to serialize with concurrent readpages() and |
| 3909 | * source range to serialize with relocation. |
Filipe Manana | c125b8b | 2014-05-23 05:03:34 +0100 | [diff] [blame] | 3910 | */ |
Filipe Manana | d8b5524 | 2019-01-08 11:43:07 +0000 | [diff] [blame] | 3911 | btrfs_double_extent_lock(src, off, inode, destoff, len); |
Mark Fasheh | 1c919a5 | 2015-06-30 14:42:08 -0700 | [diff] [blame] | 3912 | ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); |
Filipe Manana | d8b5524 | 2019-01-08 11:43:07 +0000 | [diff] [blame] | 3913 | btrfs_double_extent_unlock(src, off, inode, destoff, len); |
Filipe Manana | c125b8b | 2014-05-23 05:03:34 +0100 | [diff] [blame] | 3914 | /* |
| 3915 | * Truncate page cache pages so that future reads will see the cloned |
| 3916 | * data immediately and not the previous data. |
| 3917 | */ |
Chandan Rajendra | 65bfa65 | 2016-01-21 15:56:04 +0530 | [diff] [blame] | 3918 | truncate_inode_pages_range(&inode->i_data, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3919 | round_down(destoff, PAGE_SIZE), |
| 3920 | round_up(destoff + len, PAGE_SIZE) - 1); |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 3921 | |
| 3922 | return ret; |
| 3923 | } |
| 3924 | |
| 3925 | static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in, |
| 3926 | struct file *file_out, loff_t pos_out, |
| 3927 | loff_t *len, unsigned int remap_flags) |
| 3928 | { |
| 3929 | struct inode *inode_in = file_inode(file_in); |
| 3930 | struct inode *inode_out = file_inode(file_out); |
| 3931 | u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize; |
| 3932 | bool same_inode = inode_out == inode_in; |
| 3933 | u64 wb_len; |
| 3934 | int ret; |
| 3935 | |
| 3936 | if (!(remap_flags & REMAP_FILE_DEDUP)) { |
| 3937 | struct btrfs_root *root_out = BTRFS_I(inode_out)->root; |
| 3938 | |
| 3939 | if (btrfs_root_readonly(root_out)) |
| 3940 | return -EROFS; |
| 3941 | |
| 3942 | if (file_in->f_path.mnt != file_out->f_path.mnt || |
| 3943 | inode_in->i_sb != inode_out->i_sb) |
| 3944 | return -EXDEV; |
| 3945 | } |
| 3946 | |
Filipe Manana | 500710d | 2018-12-12 18:05:56 +0000 | [diff] [blame] | 3947 | /* don't make the dst file partly checksummed */ |
| 3948 | if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) != |
| 3949 | (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) { |
Goldwyn Rodrigues | 7984ae5 | 2019-02-25 13:07:44 -0600 | [diff] [blame] | 3950 | return -EINVAL; |
Filipe Manana | 500710d | 2018-12-12 18:05:56 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 3953 | /* |
| 3954 | * Now that the inodes are locked, we need to start writeback ourselves |
| 3955 | * and can not rely on the writeback from the VFS's generic helper |
| 3956 | * generic_remap_file_range_prep() because: |
| 3957 | * |
| 3958 | * 1) For compression we must call filemap_fdatawrite_range() range |
| 3959 | * twice (btrfs_fdatawrite_range() does it for us), and the generic |
| 3960 | * helper only calls it once; |
| 3961 | * |
| 3962 | * 2) filemap_fdatawrite_range(), called by the generic helper only |
| 3963 | * waits for the writeback to complete, i.e. for IO to be done, and |
| 3964 | * not for the ordered extents to complete. We need to wait for them |
| 3965 | * to complete so that new file extent items are in the fs tree. |
| 3966 | */ |
| 3967 | if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP)) |
| 3968 | wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs); |
| 3969 | else |
| 3970 | wb_len = ALIGN(*len, bs); |
| 3971 | |
| 3972 | /* |
| 3973 | * Since we don't lock ranges, wait for ongoing lockless dio writes (as |
| 3974 | * any in progress could create its ordered extents after we wait for |
| 3975 | * existing ordered extents below). |
| 3976 | */ |
| 3977 | inode_dio_wait(inode_in); |
| 3978 | if (!same_inode) |
| 3979 | inode_dio_wait(inode_out); |
| 3980 | |
Qu Wenruo | a94d1d0 | 2019-05-08 18:49:58 +0800 | [diff] [blame] | 3981 | /* |
| 3982 | * Workaround to make sure NOCOW buffered write reach disk as NOCOW. |
| 3983 | * |
| 3984 | * Btrfs' back references do not have a block level granularity, they |
| 3985 | * work at the whole extent level. |
| 3986 | * NOCOW buffered write without data space reserved may not be able |
| 3987 | * to fall back to CoW due to lack of data space, thus could cause |
| 3988 | * data loss. |
| 3989 | * |
| 3990 | * Here we take a shortcut by flushing the whole inode, so that all |
| 3991 | * nocow write should reach disk as nocow before we increase the |
| 3992 | * reference of the extent. We could do better by only flushing NOCOW |
| 3993 | * data, but that needs extra accounting. |
| 3994 | * |
| 3995 | * Also we don't need to check ASYNC_EXTENT, as async extent will be |
| 3996 | * CoWed anyway, not affecting nocow part. |
| 3997 | */ |
| 3998 | ret = filemap_flush(inode_in->i_mapping); |
| 3999 | if (ret < 0) |
| 4000 | return ret; |
| 4001 | |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 4002 | ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs), |
| 4003 | wb_len); |
| 4004 | if (ret < 0) |
Goldwyn Rodrigues | 7984ae5 | 2019-02-25 13:07:44 -0600 | [diff] [blame] | 4005 | return ret; |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 4006 | ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs), |
| 4007 | wb_len); |
| 4008 | if (ret < 0) |
Goldwyn Rodrigues | 7984ae5 | 2019-02-25 13:07:44 -0600 | [diff] [blame] | 4009 | return ret; |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 4010 | |
Goldwyn Rodrigues | 7984ae5 | 2019-02-25 13:07:44 -0600 | [diff] [blame] | 4011 | return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out, |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 4012 | len, remap_flags); |
Zach Brown | 3db11b2 | 2015-11-10 16:53:32 -0500 | [diff] [blame] | 4013 | } |
| 4014 | |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 4015 | loff_t btrfs_remap_file_range(struct file *src_file, loff_t off, |
| 4016 | struct file *dst_file, loff_t destoff, loff_t len, |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 4017 | unsigned int remap_flags) |
Zach Brown | 3db11b2 | 2015-11-10 16:53:32 -0500 | [diff] [blame] | 4018 | { |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 4019 | struct inode *src_inode = file_inode(src_file); |
| 4020 | struct inode *dst_inode = file_inode(dst_file); |
| 4021 | bool same_inode = dst_inode == src_inode; |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 4022 | int ret; |
| 4023 | |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 4024 | if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY)) |
| 4025 | return -EINVAL; |
| 4026 | |
Goldwyn Rodrigues | 7984ae5 | 2019-02-25 13:07:44 -0600 | [diff] [blame] | 4027 | if (same_inode) |
| 4028 | inode_lock(src_inode); |
| 4029 | else |
| 4030 | lock_two_nondirectories(src_inode, dst_inode); |
| 4031 | |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 4032 | ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff, |
| 4033 | &len, remap_flags); |
| 4034 | if (ret < 0 || len == 0) |
Goldwyn Rodrigues | 7984ae5 | 2019-02-25 13:07:44 -0600 | [diff] [blame] | 4035 | goto out_unlock; |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 4036 | |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 4037 | if (remap_flags & REMAP_FILE_DEDUP) |
| 4038 | ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff); |
| 4039 | else |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 4040 | ret = btrfs_clone_files(dst_file, src_file, off, len, destoff); |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 4041 | |
Goldwyn Rodrigues | 7984ae5 | 2019-02-25 13:07:44 -0600 | [diff] [blame] | 4042 | out_unlock: |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 4043 | if (same_inode) |
| 4044 | inode_unlock(src_inode); |
| 4045 | else |
Filipe Manana | 4ea748e | 2019-02-26 12:06:09 +0000 | [diff] [blame] | 4046 | unlock_two_nondirectories(src_inode, dst_inode); |
Filipe Manana | 34a28e3 | 2018-12-07 15:25:38 +0000 | [diff] [blame] | 4047 | |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 4048 | return ret < 0 ? ret : len; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 4049 | } |
| 4050 | |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4051 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
| 4052 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4053 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4054 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4055 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4056 | struct btrfs_root *new_root; |
| 4057 | struct btrfs_dir_item *di; |
| 4058 | struct btrfs_trans_handle *trans; |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 4059 | struct btrfs_path *path = NULL; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4060 | struct btrfs_key location; |
| 4061 | struct btrfs_disk_key disk_key; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4062 | u64 objectid = 0; |
| 4063 | u64 dir_id; |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 4064 | int ret; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4065 | |
| 4066 | if (!capable(CAP_SYS_ADMIN)) |
| 4067 | return -EPERM; |
| 4068 | |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 4069 | ret = mnt_want_write_file(file); |
| 4070 | if (ret) |
| 4071 | return ret; |
| 4072 | |
| 4073 | if (copy_from_user(&objectid, argp, sizeof(objectid))) { |
| 4074 | ret = -EFAULT; |
| 4075 | goto out; |
| 4076 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4077 | |
| 4078 | if (!objectid) |
chandan | 1cecf57 | 2013-09-13 19:34:10 +0530 | [diff] [blame] | 4079 | objectid = BTRFS_FS_TREE_OBJECTID; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4080 | |
| 4081 | location.objectid = objectid; |
| 4082 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 4083 | location.offset = (u64)-1; |
| 4084 | |
Josef Bacik | 3619c94 | 2020-01-24 09:32:24 -0500 | [diff] [blame] | 4085 | new_root = btrfs_get_fs_root(fs_info, &location, true); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 4086 | if (IS_ERR(new_root)) { |
| 4087 | ret = PTR_ERR(new_root); |
| 4088 | goto out; |
| 4089 | } |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 4090 | if (!is_fstree(new_root->root_key.objectid)) { |
| 4091 | ret = -ENOENT; |
| 4092 | goto out_free; |
| 4093 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4094 | |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4095 | path = btrfs_alloc_path(); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 4096 | if (!path) { |
| 4097 | ret = -ENOMEM; |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 4098 | goto out_free; |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 4099 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4100 | path->leave_spinning = 1; |
| 4101 | |
| 4102 | trans = btrfs_start_transaction(root, 1); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4103 | if (IS_ERR(trans)) { |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 4104 | ret = PTR_ERR(trans); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 4105 | goto out_free; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4108 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 4109 | di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4110 | dir_id, "default", 7, 1); |
Dan Carpenter | cf1e99a | 2010-05-29 09:47:24 +0000 | [diff] [blame] | 4111 | if (IS_ERR_OR_NULL(di)) { |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 4112 | btrfs_release_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4113 | btrfs_end_transaction(trans); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4114 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4115 | "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] | 4116 | ret = -ENOENT; |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 4117 | goto out_free; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); |
| 4121 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); |
| 4122 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 4123 | btrfs_release_path(path); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4124 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4125 | btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4126 | btrfs_end_transaction(trans); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 4127 | out_free: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 4128 | btrfs_put_root(new_root); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 4129 | btrfs_free_path(path); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 4130 | out: |
| 4131 | mnt_drop_write_file(file); |
| 4132 | return ret; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
Su Yue | c065f5b1 | 2018-04-02 17:24:11 +0800 | [diff] [blame] | 4135 | static void get_block_group_info(struct list_head *groups_list, |
| 4136 | struct btrfs_ioctl_space_info *space) |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4137 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4138 | struct btrfs_block_group *block_group; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4139 | |
| 4140 | space->total_bytes = 0; |
| 4141 | space->used_bytes = 0; |
| 4142 | space->flags = 0; |
| 4143 | list_for_each_entry(block_group, groups_list, list) { |
| 4144 | space->flags = block_group->flags; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 4145 | space->total_bytes += block_group->length; |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 4146 | space->used_bytes += block_group->used; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4147 | } |
| 4148 | } |
| 4149 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4150 | static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, |
| 4151 | void __user *arg) |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 4152 | { |
| 4153 | struct btrfs_ioctl_space_args space_args; |
| 4154 | struct btrfs_ioctl_space_info space; |
| 4155 | struct btrfs_ioctl_space_info *dest; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 4156 | struct btrfs_ioctl_space_info *dest_orig; |
Daniel J Blueman | 13f2696 | 2011-04-11 15:56:31 +0000 | [diff] [blame] | 4157 | struct btrfs_ioctl_space_info __user *user_dest; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 4158 | struct btrfs_space_info *info; |
Colin Ian King | 315d8e9 | 2017-09-19 16:01:23 +0100 | [diff] [blame] | 4159 | static const u64 types[] = { |
| 4160 | BTRFS_BLOCK_GROUP_DATA, |
| 4161 | BTRFS_BLOCK_GROUP_SYSTEM, |
| 4162 | BTRFS_BLOCK_GROUP_METADATA, |
| 4163 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA |
| 4164 | }; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4165 | int num_types = 4; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 4166 | int alloc_size; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 4167 | int ret = 0; |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 4168 | u64 slot_count = 0; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4169 | int i, c; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 4170 | |
| 4171 | if (copy_from_user(&space_args, |
| 4172 | (struct btrfs_ioctl_space_args __user *)arg, |
| 4173 | sizeof(space_args))) |
| 4174 | return -EFAULT; |
| 4175 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4176 | for (i = 0; i < num_types; i++) { |
| 4177 | struct btrfs_space_info *tmp; |
| 4178 | |
| 4179 | info = NULL; |
| 4180 | rcu_read_lock(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4181 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4182 | list) { |
| 4183 | if (tmp->flags == types[i]) { |
| 4184 | info = tmp; |
| 4185 | break; |
| 4186 | } |
| 4187 | } |
| 4188 | rcu_read_unlock(); |
| 4189 | |
| 4190 | if (!info) |
| 4191 | continue; |
| 4192 | |
| 4193 | down_read(&info->groups_sem); |
| 4194 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 4195 | if (!list_empty(&info->block_groups[c])) |
| 4196 | slot_count++; |
| 4197 | } |
| 4198 | up_read(&info->groups_sem); |
| 4199 | } |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 4200 | |
David Sterba | 36523e95 | 2014-02-07 14:34:12 +0100 | [diff] [blame] | 4201 | /* |
| 4202 | * Global block reserve, exported as a space_info |
| 4203 | */ |
| 4204 | slot_count++; |
| 4205 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 4206 | /* space_slots == 0 means they are asking for a count */ |
| 4207 | if (space_args.space_slots == 0) { |
| 4208 | space_args.total_spaces = slot_count; |
| 4209 | goto out; |
| 4210 | } |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4211 | |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 4212 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4213 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 4214 | alloc_size = sizeof(*dest) * slot_count; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4215 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 4216 | /* we generally have at most 6 or so space infos, one for each raid |
| 4217 | * level. So, a whole page should be more than enough for everyone |
| 4218 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4219 | if (alloc_size > PAGE_SIZE) |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 4220 | return -ENOMEM; |
| 4221 | |
| 4222 | space_args.total_spaces = 0; |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 4223 | dest = kmalloc(alloc_size, GFP_KERNEL); |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 4224 | if (!dest) |
| 4225 | return -ENOMEM; |
| 4226 | dest_orig = dest; |
| 4227 | |
| 4228 | /* now we have a buffer to copy into */ |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4229 | for (i = 0; i < num_types; i++) { |
| 4230 | struct btrfs_space_info *tmp; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 4231 | |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 4232 | if (!slot_count) |
| 4233 | break; |
| 4234 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4235 | info = NULL; |
| 4236 | rcu_read_lock(); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4237 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4238 | list) { |
| 4239 | if (tmp->flags == types[i]) { |
| 4240 | info = tmp; |
| 4241 | break; |
| 4242 | } |
| 4243 | } |
| 4244 | rcu_read_unlock(); |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 4245 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4246 | if (!info) |
| 4247 | continue; |
| 4248 | down_read(&info->groups_sem); |
| 4249 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 4250 | if (!list_empty(&info->block_groups[c])) { |
Su Yue | c065f5b1 | 2018-04-02 17:24:11 +0800 | [diff] [blame] | 4251 | get_block_group_info(&info->block_groups[c], |
| 4252 | &space); |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4253 | memcpy(dest, &space, sizeof(space)); |
| 4254 | dest++; |
| 4255 | space_args.total_spaces++; |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 4256 | slot_count--; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4257 | } |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 4258 | if (!slot_count) |
| 4259 | break; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 4260 | } |
| 4261 | up_read(&info->groups_sem); |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 4262 | } |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 4263 | |
David Sterba | 36523e95 | 2014-02-07 14:34:12 +0100 | [diff] [blame] | 4264 | /* |
| 4265 | * Add global block reserve |
| 4266 | */ |
| 4267 | if (slot_count) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4268 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
David Sterba | 36523e95 | 2014-02-07 14:34:12 +0100 | [diff] [blame] | 4269 | |
| 4270 | spin_lock(&block_rsv->lock); |
| 4271 | space.total_bytes = block_rsv->size; |
| 4272 | space.used_bytes = block_rsv->size - block_rsv->reserved; |
| 4273 | spin_unlock(&block_rsv->lock); |
| 4274 | space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; |
| 4275 | memcpy(dest, &space, sizeof(space)); |
| 4276 | space_args.total_spaces++; |
| 4277 | } |
| 4278 | |
Daniel J Blueman | 2eec6c8 | 2012-04-26 00:37:14 +0800 | [diff] [blame] | 4279 | user_dest = (struct btrfs_ioctl_space_info __user *) |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 4280 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
| 4281 | |
| 4282 | if (copy_to_user(user_dest, dest_orig, alloc_size)) |
| 4283 | ret = -EFAULT; |
| 4284 | |
| 4285 | kfree(dest_orig); |
| 4286 | out: |
| 4287 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 4288 | ret = -EFAULT; |
| 4289 | |
| 4290 | return ret; |
| 4291 | } |
| 4292 | |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 4293 | static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, |
| 4294 | void __user *argp) |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4295 | { |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4296 | struct btrfs_trans_handle *trans; |
| 4297 | u64 transid; |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4298 | int ret; |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4299 | |
Miao Xie | d4edf39 | 2013-02-20 09:17:06 +0000 | [diff] [blame] | 4300 | trans = btrfs_attach_transaction_barrier(root); |
Miao Xie | ff7c1d3 | 2012-11-26 08:41:29 +0000 | [diff] [blame] | 4301 | if (IS_ERR(trans)) { |
| 4302 | if (PTR_ERR(trans) != -ENOENT) |
| 4303 | return PTR_ERR(trans); |
| 4304 | |
| 4305 | /* No running transaction, don't bother */ |
| 4306 | transid = root->fs_info->last_trans_committed; |
| 4307 | goto out; |
| 4308 | } |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4309 | transid = trans->transid; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4310 | ret = btrfs_commit_transaction_async(trans, 0); |
Tsutomu Itoh | 8b2b2d3 | 2011-04-04 01:52:13 +0000 | [diff] [blame] | 4311 | if (ret) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4312 | btrfs_end_transaction(trans); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4313 | return ret; |
Tsutomu Itoh | 8b2b2d3 | 2011-04-04 01:52:13 +0000 | [diff] [blame] | 4314 | } |
Miao Xie | ff7c1d3 | 2012-11-26 08:41:29 +0000 | [diff] [blame] | 4315 | out: |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4316 | if (argp) |
| 4317 | if (copy_to_user(argp, &transid, sizeof(transid))) |
| 4318 | return -EFAULT; |
| 4319 | return 0; |
| 4320 | } |
| 4321 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4322 | 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] | 4323 | void __user *argp) |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4324 | { |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4325 | u64 transid; |
| 4326 | |
| 4327 | if (argp) { |
| 4328 | if (copy_from_user(&transid, argp, sizeof(transid))) |
| 4329 | return -EFAULT; |
| 4330 | } else { |
| 4331 | transid = 0; /* current trans */ |
| 4332 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4333 | return btrfs_wait_for_commit(fs_info, transid); |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4334 | } |
| 4335 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 4336 | static long btrfs_ioctl_scrub(struct file *file, void __user *arg) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4337 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4338 | 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] | 4339 | struct btrfs_ioctl_scrub_args *sa; |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 4340 | int ret; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4341 | |
| 4342 | if (!capable(CAP_SYS_ADMIN)) |
| 4343 | return -EPERM; |
| 4344 | |
| 4345 | sa = memdup_user(arg, sizeof(*sa)); |
| 4346 | if (IS_ERR(sa)) |
| 4347 | return PTR_ERR(sa); |
| 4348 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 4349 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) { |
| 4350 | ret = mnt_want_write_file(file); |
| 4351 | if (ret) |
| 4352 | goto out; |
| 4353 | } |
| 4354 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4355 | ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4356 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, |
| 4357 | 0); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4358 | |
Filipe Manana | 5afe6ce | 2020-01-16 11:29:20 +0000 | [diff] [blame] | 4359 | /* |
| 4360 | * Copy scrub args to user space even if btrfs_scrub_dev() returned an |
| 4361 | * error. This is important as it allows user space to know how much |
| 4362 | * progress scrub has done. For example, if scrub is canceled we get |
| 4363 | * -ECANCELED from btrfs_scrub_dev() and return that error back to user |
| 4364 | * space. Later user space can inspect the progress from the structure |
| 4365 | * btrfs_ioctl_scrub_args and resume scrub from where it left off |
| 4366 | * previously (btrfs-progs does this). |
| 4367 | * If we fail to copy the btrfs_ioctl_scrub_args structure to user space |
| 4368 | * then return -EFAULT to signal the structure was not copied or it may |
| 4369 | * be corrupt and unreliable due to a partial copy. |
| 4370 | */ |
| 4371 | if (copy_to_user(arg, sa, sizeof(*sa))) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4372 | ret = -EFAULT; |
| 4373 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 4374 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) |
| 4375 | mnt_drop_write_file(file); |
| 4376 | out: |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4377 | kfree(sa); |
| 4378 | return ret; |
| 4379 | } |
| 4380 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4381 | static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4382 | { |
| 4383 | if (!capable(CAP_SYS_ADMIN)) |
| 4384 | return -EPERM; |
| 4385 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4386 | return btrfs_scrub_cancel(fs_info); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4387 | } |
| 4388 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4389 | static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4390 | void __user *arg) |
| 4391 | { |
| 4392 | struct btrfs_ioctl_scrub_args *sa; |
| 4393 | int ret; |
| 4394 | |
| 4395 | if (!capable(CAP_SYS_ADMIN)) |
| 4396 | return -EPERM; |
| 4397 | |
| 4398 | sa = memdup_user(arg, sizeof(*sa)); |
| 4399 | if (IS_ERR(sa)) |
| 4400 | return PTR_ERR(sa); |
| 4401 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4402 | ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4403 | |
Filipe Manana | 4fa99b0 | 2018-12-14 19:45:13 +0000 | [diff] [blame] | 4404 | if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4405 | ret = -EFAULT; |
| 4406 | |
| 4407 | kfree(sa); |
| 4408 | return ret; |
| 4409 | } |
| 4410 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4411 | 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] | 4412 | void __user *arg) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 4413 | { |
| 4414 | struct btrfs_ioctl_get_dev_stats *sa; |
| 4415 | int ret; |
| 4416 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 4417 | sa = memdup_user(arg, sizeof(*sa)); |
| 4418 | if (IS_ERR(sa)) |
| 4419 | return PTR_ERR(sa); |
| 4420 | |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 4421 | if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { |
| 4422 | kfree(sa); |
| 4423 | return -EPERM; |
| 4424 | } |
| 4425 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4426 | ret = btrfs_get_dev_stats(fs_info, sa); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 4427 | |
Filipe Manana | eee9957 | 2018-12-14 19:45:22 +0000 | [diff] [blame] | 4428 | if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 4429 | ret = -EFAULT; |
| 4430 | |
| 4431 | kfree(sa); |
| 4432 | return ret; |
| 4433 | } |
| 4434 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4435 | static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, |
| 4436 | void __user *arg) |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 4437 | { |
| 4438 | struct btrfs_ioctl_dev_replace_args *p; |
| 4439 | int ret; |
| 4440 | |
| 4441 | if (!capable(CAP_SYS_ADMIN)) |
| 4442 | return -EPERM; |
| 4443 | |
| 4444 | p = memdup_user(arg, sizeof(*p)); |
| 4445 | if (IS_ERR(p)) |
| 4446 | return PTR_ERR(p); |
| 4447 | |
| 4448 | switch (p->cmd) { |
| 4449 | case BTRFS_IOCTL_DEV_REPLACE_CMD_START: |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 4450 | if (sb_rdonly(fs_info->sb)) { |
Ilya Dryomov | adfa97c | 2013-10-10 20:39:28 +0300 | [diff] [blame] | 4451 | ret = -EROFS; |
| 4452 | goto out; |
| 4453 | } |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 4454 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
Anand Jain | e57138b | 2013-08-21 11:44:48 +0800 | [diff] [blame] | 4455 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 4456 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4457 | ret = btrfs_dev_replace_by_ioctl(fs_info, p); |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 4458 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 4459 | } |
| 4460 | break; |
| 4461 | case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4462 | btrfs_dev_replace_status(fs_info, p); |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 4463 | ret = 0; |
| 4464 | break; |
| 4465 | case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: |
Anand Jain | 17d202b | 2018-02-12 23:33:30 +0800 | [diff] [blame] | 4466 | p->result = btrfs_dev_replace_cancel(fs_info); |
Anand Jain | 9728203 | 2018-02-12 23:33:29 +0800 | [diff] [blame] | 4467 | ret = 0; |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 4468 | break; |
| 4469 | default: |
| 4470 | ret = -EINVAL; |
| 4471 | break; |
| 4472 | } |
| 4473 | |
Filipe Manana | d3a5328 | 2019-01-08 11:42:09 +0000 | [diff] [blame] | 4474 | if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p))) |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 4475 | ret = -EFAULT; |
Ilya Dryomov | adfa97c | 2013-10-10 20:39:28 +0300 | [diff] [blame] | 4476 | out: |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 4477 | kfree(p); |
| 4478 | return ret; |
| 4479 | } |
| 4480 | |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4481 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
| 4482 | { |
| 4483 | int ret = 0; |
| 4484 | int i; |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 4485 | u64 rel_ptr; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4486 | int size; |
Chris Mason | 806468f | 2011-11-06 03:07:10 -0500 | [diff] [blame] | 4487 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4488 | struct inode_fs_paths *ipath = NULL; |
| 4489 | struct btrfs_path *path; |
| 4490 | |
Kusanagi Kouichi | 82b22ac | 2013-01-28 11:33:31 +0000 | [diff] [blame] | 4491 | if (!capable(CAP_DAC_READ_SEARCH)) |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4492 | return -EPERM; |
| 4493 | |
| 4494 | path = btrfs_alloc_path(); |
| 4495 | if (!path) { |
| 4496 | ret = -ENOMEM; |
| 4497 | goto out; |
| 4498 | } |
| 4499 | |
| 4500 | ipa = memdup_user(arg, sizeof(*ipa)); |
| 4501 | if (IS_ERR(ipa)) { |
| 4502 | ret = PTR_ERR(ipa); |
| 4503 | ipa = NULL; |
| 4504 | goto out; |
| 4505 | } |
| 4506 | |
| 4507 | size = min_t(u32, ipa->size, 4096); |
| 4508 | ipath = init_ipath(size, root, path); |
| 4509 | if (IS_ERR(ipath)) { |
| 4510 | ret = PTR_ERR(ipath); |
| 4511 | ipath = NULL; |
| 4512 | goto out; |
| 4513 | } |
| 4514 | |
| 4515 | ret = paths_from_inode(ipa->inum, ipath); |
| 4516 | if (ret < 0) |
| 4517 | goto out; |
| 4518 | |
| 4519 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { |
Jeff Mahoney | 745c4d8 | 2011-11-20 07:31:57 -0500 | [diff] [blame] | 4520 | rel_ptr = ipath->fspath->val[i] - |
| 4521 | (u64)(unsigned long)ipath->fspath->val; |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 4522 | ipath->fspath->val[i] = rel_ptr; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4523 | } |
| 4524 | |
Omar Sandoval | 718dc5f | 2017-08-22 23:46:05 -0700 | [diff] [blame] | 4525 | ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, |
| 4526 | ipath->fspath, size); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4527 | if (ret) { |
| 4528 | ret = -EFAULT; |
| 4529 | goto out; |
| 4530 | } |
| 4531 | |
| 4532 | out: |
| 4533 | btrfs_free_path(path); |
| 4534 | free_ipath(ipath); |
| 4535 | kfree(ipa); |
| 4536 | |
| 4537 | return ret; |
| 4538 | } |
| 4539 | |
| 4540 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) |
| 4541 | { |
| 4542 | struct btrfs_data_container *inodes = ctx; |
| 4543 | const size_t c = 3 * sizeof(u64); |
| 4544 | |
| 4545 | if (inodes->bytes_left >= c) { |
| 4546 | inodes->bytes_left -= c; |
| 4547 | inodes->val[inodes->elem_cnt] = inum; |
| 4548 | inodes->val[inodes->elem_cnt + 1] = offset; |
| 4549 | inodes->val[inodes->elem_cnt + 2] = root; |
| 4550 | inodes->elem_cnt += 3; |
| 4551 | } else { |
| 4552 | inodes->bytes_missing += c - inodes->bytes_left; |
| 4553 | inodes->bytes_left = 0; |
| 4554 | inodes->elem_missed += 3; |
| 4555 | } |
| 4556 | |
| 4557 | return 0; |
| 4558 | } |
| 4559 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4560 | 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] | 4561 | void __user *arg, int version) |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4562 | { |
| 4563 | int ret = 0; |
| 4564 | int size; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4565 | struct btrfs_ioctl_logical_ino_args *loi; |
| 4566 | struct btrfs_data_container *inodes = NULL; |
| 4567 | struct btrfs_path *path = NULL; |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 4568 | bool ignore_offset; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4569 | |
| 4570 | if (!capable(CAP_SYS_ADMIN)) |
| 4571 | return -EPERM; |
| 4572 | |
| 4573 | loi = memdup_user(arg, sizeof(*loi)); |
Shailendra Verma | 7b9ea62 | 2016-11-10 15:17:41 +0530 | [diff] [blame] | 4574 | if (IS_ERR(loi)) |
| 4575 | return PTR_ERR(loi); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4576 | |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 4577 | if (version == 1) { |
| 4578 | ignore_offset = false; |
Zygo Blaxell | b115e3b | 2017-09-22 13:58:47 -0400 | [diff] [blame] | 4579 | size = min_t(u32, loi->size, SZ_64K); |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 4580 | } else { |
| 4581 | /* All reserved bits must be 0 for now */ |
| 4582 | if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { |
| 4583 | ret = -EINVAL; |
| 4584 | goto out_loi; |
| 4585 | } |
| 4586 | /* Only accept flags we have defined so far */ |
| 4587 | if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { |
| 4588 | ret = -EINVAL; |
| 4589 | goto out_loi; |
| 4590 | } |
| 4591 | ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; |
Zygo Blaxell | b115e3b | 2017-09-22 13:58:47 -0400 | [diff] [blame] | 4592 | size = min_t(u32, loi->size, SZ_16M); |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 4593 | } |
| 4594 | |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4595 | path = btrfs_alloc_path(); |
| 4596 | if (!path) { |
| 4597 | ret = -ENOMEM; |
| 4598 | goto out; |
| 4599 | } |
| 4600 | |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4601 | inodes = init_data_container(size); |
| 4602 | if (IS_ERR(inodes)) { |
| 4603 | ret = PTR_ERR(inodes); |
| 4604 | inodes = NULL; |
| 4605 | goto out; |
| 4606 | } |
| 4607 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4608 | ret = iterate_inodes_from_logical(loi->logical, fs_info, path, |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 4609 | build_ino_list, inodes, ignore_offset); |
Liu Bo | df031f0 | 2012-09-07 20:01:29 -0600 | [diff] [blame] | 4610 | if (ret == -EINVAL) |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4611 | ret = -ENOENT; |
| 4612 | if (ret < 0) |
| 4613 | goto out; |
| 4614 | |
Omar Sandoval | 718dc5f | 2017-08-22 23:46:05 -0700 | [diff] [blame] | 4615 | ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, |
| 4616 | size); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4617 | if (ret) |
| 4618 | ret = -EFAULT; |
| 4619 | |
| 4620 | out: |
| 4621 | btrfs_free_path(path); |
David Sterba | f54de06 | 2017-05-31 19:32:09 +0200 | [diff] [blame] | 4622 | kvfree(inodes); |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 4623 | out_loi: |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4624 | kfree(loi); |
| 4625 | |
| 4626 | return ret; |
| 4627 | } |
| 4628 | |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4629 | void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4630 | struct btrfs_ioctl_balance_args *bargs) |
| 4631 | { |
| 4632 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 4633 | |
| 4634 | bargs->flags = bctl->flags; |
| 4635 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4636 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4637 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; |
| 4638 | if (atomic_read(&fs_info->balance_pause_req)) |
| 4639 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4640 | if (atomic_read(&fs_info->balance_cancel_req)) |
| 4641 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4642 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4643 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
| 4644 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); |
| 4645 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4646 | |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4647 | spin_lock(&fs_info->balance_lock); |
| 4648 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); |
| 4649 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4650 | } |
| 4651 | |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 4652 | static long btrfs_ioctl_balance(struct file *file, void __user *arg) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4653 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4654 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4655 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4656 | struct btrfs_ioctl_balance_args *bargs; |
| 4657 | struct btrfs_balance_control *bctl; |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4658 | bool need_unlock; /* for mut. excl. ops lock */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4659 | int ret; |
| 4660 | |
| 4661 | if (!capable(CAP_SYS_ADMIN)) |
| 4662 | return -EPERM; |
| 4663 | |
Liu Bo | e54bfa31 | 2012-06-29 03:58:48 -0600 | [diff] [blame] | 4664 | ret = mnt_want_write_file(file); |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 4665 | if (ret) |
| 4666 | return ret; |
| 4667 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4668 | again: |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 4669 | if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4670 | mutex_lock(&fs_info->balance_mutex); |
| 4671 | need_unlock = true; |
| 4672 | goto locked; |
| 4673 | } |
| 4674 | |
| 4675 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4676 | * mut. excl. ops lock is locked. Three possibilities: |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4677 | * (1) some other op is running |
| 4678 | * (2) balance is running |
| 4679 | * (3) balance is paused -- special case (think resume) |
| 4680 | */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4681 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4682 | if (fs_info->balance_ctl) { |
| 4683 | /* this is either (2) or (3) */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4684 | if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4685 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4686 | /* |
| 4687 | * Lock released to allow other waiters to continue, |
| 4688 | * we'll reexamine the status again. |
| 4689 | */ |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4690 | mutex_lock(&fs_info->balance_mutex); |
| 4691 | |
| 4692 | if (fs_info->balance_ctl && |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4693 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4694 | /* this is (3) */ |
| 4695 | need_unlock = false; |
| 4696 | goto locked; |
| 4697 | } |
| 4698 | |
| 4699 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4700 | goto again; |
| 4701 | } else { |
| 4702 | /* this is (2) */ |
| 4703 | mutex_unlock(&fs_info->balance_mutex); |
| 4704 | ret = -EINPROGRESS; |
| 4705 | goto out; |
| 4706 | } |
| 4707 | } else { |
| 4708 | /* this is (1) */ |
| 4709 | mutex_unlock(&fs_info->balance_mutex); |
Anand Jain | e57138b | 2013-08-21 11:44:48 +0800 | [diff] [blame] | 4710 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4711 | goto out; |
| 4712 | } |
| 4713 | |
| 4714 | locked: |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 4715 | BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4716 | |
| 4717 | if (arg) { |
| 4718 | bargs = memdup_user(arg, sizeof(*bargs)); |
| 4719 | if (IS_ERR(bargs)) { |
| 4720 | ret = PTR_ERR(bargs); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4721 | goto out_unlock; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4722 | } |
Ilya Dryomov | de32226 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4723 | |
| 4724 | if (bargs->flags & BTRFS_BALANCE_RESUME) { |
| 4725 | if (!fs_info->balance_ctl) { |
| 4726 | ret = -ENOTCONN; |
| 4727 | goto out_bargs; |
| 4728 | } |
| 4729 | |
| 4730 | bctl = fs_info->balance_ctl; |
| 4731 | spin_lock(&fs_info->balance_lock); |
| 4732 | bctl->flags |= BTRFS_BALANCE_RESUME; |
| 4733 | spin_unlock(&fs_info->balance_lock); |
| 4734 | |
| 4735 | goto do_balance; |
| 4736 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4737 | } else { |
| 4738 | bargs = NULL; |
| 4739 | } |
| 4740 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4741 | if (fs_info->balance_ctl) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4742 | ret = -EINPROGRESS; |
| 4743 | goto out_bargs; |
| 4744 | } |
| 4745 | |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 4746 | bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4747 | if (!bctl) { |
| 4748 | ret = -ENOMEM; |
| 4749 | goto out_bargs; |
| 4750 | } |
| 4751 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4752 | if (arg) { |
| 4753 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); |
| 4754 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); |
| 4755 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); |
| 4756 | |
| 4757 | bctl->flags = bargs->flags; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4758 | } else { |
| 4759 | /* balance everything - no filters */ |
| 4760 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4761 | } |
| 4762 | |
David Sterba | 8eb9345 | 2015-10-12 16:55:54 +0200 | [diff] [blame] | 4763 | if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { |
| 4764 | ret = -EINVAL; |
Christian Engelmayer | 0f89abf | 2015-10-21 00:50:06 +0200 | [diff] [blame] | 4765 | goto out_bctl; |
David Sterba | 8eb9345 | 2015-10-12 16:55:54 +0200 | [diff] [blame] | 4766 | } |
| 4767 | |
Ilya Dryomov | de32226 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4768 | do_balance: |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4769 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4770 | * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to |
| 4771 | * btrfs_balance. bctl is freed in reset_balance_state, or, if |
| 4772 | * restriper was paused all the way until unmount, in free_fs_info. |
| 4773 | * The flag should be cleared after reset_balance_state. |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4774 | */ |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4775 | need_unlock = false; |
| 4776 | |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4777 | ret = btrfs_balance(fs_info, bctl, bargs); |
Christian Engelmayer | 0f89abf | 2015-10-21 00:50:06 +0200 | [diff] [blame] | 4778 | bctl = NULL; |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4779 | |
Filipe Manana | d00c2d9 | 2019-01-08 11:42:01 +0000 | [diff] [blame] | 4780 | if ((ret == 0 || ret == -ECANCELED) && arg) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4781 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
| 4782 | ret = -EFAULT; |
| 4783 | } |
| 4784 | |
Christian Engelmayer | 0f89abf | 2015-10-21 00:50:06 +0200 | [diff] [blame] | 4785 | out_bctl: |
| 4786 | kfree(bctl); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4787 | out_bargs: |
| 4788 | kfree(bargs); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4789 | out_unlock: |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4790 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4791 | if (need_unlock) |
David Sterba | 171938e | 2017-03-28 14:44:21 +0200 | [diff] [blame] | 4792 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4793 | out: |
Liu Bo | e54bfa31 | 2012-06-29 03:58:48 -0600 | [diff] [blame] | 4794 | mnt_drop_write_file(file); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4795 | return ret; |
| 4796 | } |
| 4797 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4798 | 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] | 4799 | { |
| 4800 | if (!capable(CAP_SYS_ADMIN)) |
| 4801 | return -EPERM; |
| 4802 | |
| 4803 | switch (cmd) { |
| 4804 | case BTRFS_BALANCE_CTL_PAUSE: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4805 | return btrfs_pause_balance(fs_info); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4806 | case BTRFS_BALANCE_CTL_CANCEL: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4807 | return btrfs_cancel_balance(fs_info); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4808 | } |
| 4809 | |
| 4810 | return -EINVAL; |
| 4811 | } |
| 4812 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4813 | static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4814 | void __user *arg) |
| 4815 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4816 | struct btrfs_ioctl_balance_args *bargs; |
| 4817 | int ret = 0; |
| 4818 | |
| 4819 | if (!capable(CAP_SYS_ADMIN)) |
| 4820 | return -EPERM; |
| 4821 | |
| 4822 | mutex_lock(&fs_info->balance_mutex); |
| 4823 | if (!fs_info->balance_ctl) { |
| 4824 | ret = -ENOTCONN; |
| 4825 | goto out; |
| 4826 | } |
| 4827 | |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 4828 | bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4829 | if (!bargs) { |
| 4830 | ret = -ENOMEM; |
| 4831 | goto out; |
| 4832 | } |
| 4833 | |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4834 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4835 | |
| 4836 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
| 4837 | ret = -EFAULT; |
| 4838 | |
| 4839 | kfree(bargs); |
| 4840 | out: |
| 4841 | mutex_unlock(&fs_info->balance_mutex); |
| 4842 | return ret; |
| 4843 | } |
| 4844 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4845 | static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4846 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4847 | struct inode *inode = file_inode(file); |
| 4848 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4849 | struct btrfs_ioctl_quota_ctl_args *sa; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4850 | int ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4851 | |
| 4852 | if (!capable(CAP_SYS_ADMIN)) |
| 4853 | return -EPERM; |
| 4854 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4855 | ret = mnt_want_write_file(file); |
| 4856 | if (ret) |
| 4857 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4858 | |
| 4859 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4860 | if (IS_ERR(sa)) { |
| 4861 | ret = PTR_ERR(sa); |
| 4862 | goto drop_write; |
| 4863 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4864 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4865 | down_write(&fs_info->subvol_sem); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4866 | |
| 4867 | switch (sa->cmd) { |
| 4868 | case BTRFS_QUOTA_CTL_ENABLE: |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 4869 | ret = btrfs_quota_enable(fs_info); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4870 | break; |
| 4871 | case BTRFS_QUOTA_CTL_DISABLE: |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 4872 | ret = btrfs_quota_disable(fs_info); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4873 | break; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4874 | default: |
| 4875 | ret = -EINVAL; |
| 4876 | break; |
| 4877 | } |
| 4878 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4879 | kfree(sa); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4880 | up_write(&fs_info->subvol_sem); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4881 | drop_write: |
| 4882 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4883 | return ret; |
| 4884 | } |
| 4885 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4886 | static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4887 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4888 | struct inode *inode = file_inode(file); |
| 4889 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 4890 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4891 | struct btrfs_ioctl_qgroup_assign_args *sa; |
| 4892 | struct btrfs_trans_handle *trans; |
| 4893 | int ret; |
| 4894 | int err; |
| 4895 | |
| 4896 | if (!capable(CAP_SYS_ADMIN)) |
| 4897 | return -EPERM; |
| 4898 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4899 | ret = mnt_want_write_file(file); |
| 4900 | if (ret) |
| 4901 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4902 | |
| 4903 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4904 | if (IS_ERR(sa)) { |
| 4905 | ret = PTR_ERR(sa); |
| 4906 | goto drop_write; |
| 4907 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4908 | |
| 4909 | trans = btrfs_join_transaction(root); |
| 4910 | if (IS_ERR(trans)) { |
| 4911 | ret = PTR_ERR(trans); |
| 4912 | goto out; |
| 4913 | } |
| 4914 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4915 | if (sa->assign) { |
Lu Fengqi | 9f8a6ce | 2018-07-18 14:45:30 +0800 | [diff] [blame] | 4916 | ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4917 | } else { |
Lu Fengqi | 39616c2 | 2018-07-18 14:45:32 +0800 | [diff] [blame] | 4918 | ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4919 | } |
| 4920 | |
Qu Wenruo | e082f56 | 2015-02-27 16:24:28 +0800 | [diff] [blame] | 4921 | /* update qgroup status and info */ |
Lu Fengqi | 280f8bd | 2018-07-18 14:45:40 +0800 | [diff] [blame] | 4922 | err = btrfs_run_qgroups(trans); |
Qu Wenruo | e082f56 | 2015-02-27 16:24:28 +0800 | [diff] [blame] | 4923 | if (err < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4924 | btrfs_handle_fs_error(fs_info, err, |
| 4925 | "failed to update qgroup status and info"); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4926 | err = btrfs_end_transaction(trans); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4927 | if (err && !ret) |
| 4928 | ret = err; |
| 4929 | |
| 4930 | out: |
| 4931 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4932 | drop_write: |
| 4933 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4934 | return ret; |
| 4935 | } |
| 4936 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4937 | static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4938 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4939 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4940 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4941 | struct btrfs_ioctl_qgroup_create_args *sa; |
| 4942 | struct btrfs_trans_handle *trans; |
| 4943 | int ret; |
| 4944 | int err; |
| 4945 | |
| 4946 | if (!capable(CAP_SYS_ADMIN)) |
| 4947 | return -EPERM; |
| 4948 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4949 | ret = mnt_want_write_file(file); |
| 4950 | if (ret) |
| 4951 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4952 | |
| 4953 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4954 | if (IS_ERR(sa)) { |
| 4955 | ret = PTR_ERR(sa); |
| 4956 | goto drop_write; |
| 4957 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4958 | |
Miao Xie | d86e56c | 2012-11-15 11:35:41 +0000 | [diff] [blame] | 4959 | if (!sa->qgroupid) { |
| 4960 | ret = -EINVAL; |
| 4961 | goto out; |
| 4962 | } |
| 4963 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4964 | trans = btrfs_join_transaction(root); |
| 4965 | if (IS_ERR(trans)) { |
| 4966 | ret = PTR_ERR(trans); |
| 4967 | goto out; |
| 4968 | } |
| 4969 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4970 | if (sa->create) { |
Lu Fengqi | 49a05ec | 2018-07-18 14:45:33 +0800 | [diff] [blame] | 4971 | ret = btrfs_create_qgroup(trans, sa->qgroupid); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4972 | } else { |
Lu Fengqi | 3efbee1 | 2018-07-18 14:45:34 +0800 | [diff] [blame] | 4973 | ret = btrfs_remove_qgroup(trans, sa->qgroupid); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4974 | } |
| 4975 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4976 | err = btrfs_end_transaction(trans); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4977 | if (err && !ret) |
| 4978 | ret = err; |
| 4979 | |
| 4980 | out: |
| 4981 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4982 | drop_write: |
| 4983 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4984 | return ret; |
| 4985 | } |
| 4986 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4987 | static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4988 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4989 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4990 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4991 | struct btrfs_ioctl_qgroup_limit_args *sa; |
| 4992 | struct btrfs_trans_handle *trans; |
| 4993 | int ret; |
| 4994 | int err; |
| 4995 | u64 qgroupid; |
| 4996 | |
| 4997 | if (!capable(CAP_SYS_ADMIN)) |
| 4998 | return -EPERM; |
| 4999 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 5000 | ret = mnt_want_write_file(file); |
| 5001 | if (ret) |
| 5002 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5003 | |
| 5004 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 5005 | if (IS_ERR(sa)) { |
| 5006 | ret = PTR_ERR(sa); |
| 5007 | goto drop_write; |
| 5008 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5009 | |
| 5010 | trans = btrfs_join_transaction(root); |
| 5011 | if (IS_ERR(trans)) { |
| 5012 | ret = PTR_ERR(trans); |
| 5013 | goto out; |
| 5014 | } |
| 5015 | |
| 5016 | qgroupid = sa->qgroupid; |
| 5017 | if (!qgroupid) { |
| 5018 | /* take the current subvol as qgroup */ |
| 5019 | qgroupid = root->root_key.objectid; |
| 5020 | } |
| 5021 | |
Lu Fengqi | f0042d5 | 2018-07-18 14:45:35 +0800 | [diff] [blame] | 5022 | ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5023 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5024 | err = btrfs_end_transaction(trans); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5025 | if (err && !ret) |
| 5026 | ret = err; |
| 5027 | |
| 5028 | out: |
| 5029 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 5030 | drop_write: |
| 5031 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5032 | return ret; |
| 5033 | } |
| 5034 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 5035 | static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) |
| 5036 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5037 | struct inode *inode = file_inode(file); |
| 5038 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 5039 | struct btrfs_ioctl_quota_rescan_args *qsa; |
| 5040 | int ret; |
| 5041 | |
| 5042 | if (!capable(CAP_SYS_ADMIN)) |
| 5043 | return -EPERM; |
| 5044 | |
| 5045 | ret = mnt_want_write_file(file); |
| 5046 | if (ret) |
| 5047 | return ret; |
| 5048 | |
| 5049 | qsa = memdup_user(arg, sizeof(*qsa)); |
| 5050 | if (IS_ERR(qsa)) { |
| 5051 | ret = PTR_ERR(qsa); |
| 5052 | goto drop_write; |
| 5053 | } |
| 5054 | |
| 5055 | if (qsa->flags) { |
| 5056 | ret = -EINVAL; |
| 5057 | goto out; |
| 5058 | } |
| 5059 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5060 | ret = btrfs_qgroup_rescan(fs_info); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 5061 | |
| 5062 | out: |
| 5063 | kfree(qsa); |
| 5064 | drop_write: |
| 5065 | mnt_drop_write_file(file); |
| 5066 | return ret; |
| 5067 | } |
| 5068 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5069 | static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info, |
| 5070 | void __user *arg) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 5071 | { |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 5072 | struct btrfs_ioctl_quota_rescan_args *qsa; |
| 5073 | int ret = 0; |
| 5074 | |
| 5075 | if (!capable(CAP_SYS_ADMIN)) |
| 5076 | return -EPERM; |
| 5077 | |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 5078 | qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 5079 | if (!qsa) |
| 5080 | return -ENOMEM; |
| 5081 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5082 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 5083 | qsa->flags = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5084 | qsa->progress = fs_info->qgroup_rescan_progress.objectid; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | if (copy_to_user(arg, qsa, sizeof(*qsa))) |
| 5088 | ret = -EFAULT; |
| 5089 | |
| 5090 | kfree(qsa); |
| 5091 | return ret; |
| 5092 | } |
| 5093 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5094 | static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info, |
| 5095 | void __user *arg) |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 5096 | { |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 5097 | if (!capable(CAP_SYS_ADMIN)) |
| 5098 | return -EPERM; |
| 5099 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5100 | return btrfs_qgroup_wait_for_completion(fs_info, true); |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 5101 | } |
| 5102 | |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 5103 | static long _btrfs_ioctl_set_received_subvol(struct file *file, |
| 5104 | struct btrfs_ioctl_received_subvol_args *sa) |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5105 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5106 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5107 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5108 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5109 | struct btrfs_root_item *root_item = &root->root_item; |
| 5110 | struct btrfs_trans_handle *trans; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 5111 | struct timespec64 ct = current_time(inode); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5112 | int ret = 0; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 5113 | int received_uuid_changed; |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5114 | |
David Sterba | bd60ea0 | 2014-01-16 15:50:22 +0100 | [diff] [blame] | 5115 | if (!inode_owner_or_capable(inode)) |
| 5116 | return -EPERM; |
| 5117 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5118 | ret = mnt_want_write_file(file); |
| 5119 | if (ret < 0) |
| 5120 | return ret; |
| 5121 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5122 | down_write(&fs_info->subvol_sem); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5123 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 5124 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5125 | ret = -EINVAL; |
| 5126 | goto out; |
| 5127 | } |
| 5128 | |
| 5129 | if (btrfs_root_readonly(root)) { |
| 5130 | ret = -EROFS; |
| 5131 | goto out; |
| 5132 | } |
| 5133 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 5134 | /* |
| 5135 | * 1 - root item |
| 5136 | * 2 - uuid items (received uuid + subvol uuid) |
| 5137 | */ |
| 5138 | trans = btrfs_start_transaction(root, 3); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5139 | if (IS_ERR(trans)) { |
| 5140 | ret = PTR_ERR(trans); |
| 5141 | trans = NULL; |
| 5142 | goto out; |
| 5143 | } |
| 5144 | |
| 5145 | sa->rtransid = trans->transid; |
| 5146 | sa->rtime.sec = ct.tv_sec; |
| 5147 | sa->rtime.nsec = ct.tv_nsec; |
| 5148 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 5149 | received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, |
| 5150 | BTRFS_UUID_SIZE); |
| 5151 | if (received_uuid_changed && |
Nikolay Borisov | d87ff75 | 2018-03-12 14:48:09 +0200 | [diff] [blame] | 5152 | !btrfs_is_empty_uuid(root_item->received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 5153 | ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid, |
Nikolay Borisov | d87ff75 | 2018-03-12 14:48:09 +0200 | [diff] [blame] | 5154 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 5155 | root->root_key.objectid); |
| 5156 | if (ret && ret != -ENOENT) { |
| 5157 | btrfs_abort_transaction(trans, ret); |
| 5158 | btrfs_end_transaction(trans); |
| 5159 | goto out; |
| 5160 | } |
| 5161 | } |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5162 | memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); |
| 5163 | btrfs_set_root_stransid(root_item, sa->stransid); |
| 5164 | btrfs_set_root_rtransid(root_item, sa->rtransid); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 5165 | btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); |
| 5166 | btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); |
| 5167 | btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); |
| 5168 | btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5169 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5170 | ret = btrfs_update_root(trans, fs_info->tree_root, |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5171 | &root->root_key, &root->root_item); |
| 5172 | if (ret < 0) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5173 | btrfs_end_transaction(trans); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5174 | goto out; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 5175 | } |
| 5176 | if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 5177 | ret = btrfs_uuid_tree_add(trans, sa->uuid, |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 5178 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 5179 | root->root_key.objectid); |
| 5180 | if (ret < 0 && ret != -EEXIST) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5181 | btrfs_abort_transaction(trans, ret); |
Nikolay Borisov | efd3815 | 2017-09-28 11:45:26 +0300 | [diff] [blame] | 5182 | btrfs_end_transaction(trans); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5183 | goto out; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 5184 | } |
| 5185 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5186 | ret = btrfs_commit_transaction(trans); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 5187 | out: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5188 | up_write(&fs_info->subvol_sem); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 5189 | mnt_drop_write_file(file); |
| 5190 | return ret; |
| 5191 | } |
| 5192 | |
| 5193 | #ifdef CONFIG_64BIT |
| 5194 | static long btrfs_ioctl_set_received_subvol_32(struct file *file, |
| 5195 | void __user *arg) |
| 5196 | { |
| 5197 | struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; |
| 5198 | struct btrfs_ioctl_received_subvol_args *args64 = NULL; |
| 5199 | int ret = 0; |
| 5200 | |
| 5201 | args32 = memdup_user(arg, sizeof(*args32)); |
Shailendra Verma | 7b9ea62 | 2016-11-10 15:17:41 +0530 | [diff] [blame] | 5202 | if (IS_ERR(args32)) |
| 5203 | return PTR_ERR(args32); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 5204 | |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 5205 | args64 = kmalloc(sizeof(*args64), GFP_KERNEL); |
Dan Carpenter | 84dbeb8 | 2014-03-28 11:06:00 +0300 | [diff] [blame] | 5206 | if (!args64) { |
| 5207 | ret = -ENOMEM; |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 5208 | goto out; |
| 5209 | } |
| 5210 | |
| 5211 | memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); |
| 5212 | args64->stransid = args32->stransid; |
| 5213 | args64->rtransid = args32->rtransid; |
| 5214 | args64->stime.sec = args32->stime.sec; |
| 5215 | args64->stime.nsec = args32->stime.nsec; |
| 5216 | args64->rtime.sec = args32->rtime.sec; |
| 5217 | args64->rtime.nsec = args32->rtime.nsec; |
| 5218 | args64->flags = args32->flags; |
| 5219 | |
| 5220 | ret = _btrfs_ioctl_set_received_subvol(file, args64); |
| 5221 | if (ret) |
| 5222 | goto out; |
| 5223 | |
| 5224 | memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); |
| 5225 | args32->stransid = args64->stransid; |
| 5226 | args32->rtransid = args64->rtransid; |
| 5227 | args32->stime.sec = args64->stime.sec; |
| 5228 | args32->stime.nsec = args64->stime.nsec; |
| 5229 | args32->rtime.sec = args64->rtime.sec; |
| 5230 | args32->rtime.nsec = args64->rtime.nsec; |
| 5231 | args32->flags = args64->flags; |
| 5232 | |
| 5233 | ret = copy_to_user(arg, args32, sizeof(*args32)); |
| 5234 | if (ret) |
| 5235 | ret = -EFAULT; |
| 5236 | |
| 5237 | out: |
| 5238 | kfree(args32); |
| 5239 | kfree(args64); |
| 5240 | return ret; |
| 5241 | } |
| 5242 | #endif |
| 5243 | |
| 5244 | static long btrfs_ioctl_set_received_subvol(struct file *file, |
| 5245 | void __user *arg) |
| 5246 | { |
| 5247 | struct btrfs_ioctl_received_subvol_args *sa = NULL; |
| 5248 | int ret = 0; |
| 5249 | |
| 5250 | sa = memdup_user(arg, sizeof(*sa)); |
Shailendra Verma | 7b9ea62 | 2016-11-10 15:17:41 +0530 | [diff] [blame] | 5251 | if (IS_ERR(sa)) |
| 5252 | return PTR_ERR(sa); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 5253 | |
| 5254 | ret = _btrfs_ioctl_set_received_subvol(file, sa); |
| 5255 | |
| 5256 | if (ret) |
| 5257 | goto out; |
| 5258 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5259 | ret = copy_to_user(arg, sa, sizeof(*sa)); |
| 5260 | if (ret) |
| 5261 | ret = -EFAULT; |
| 5262 | |
| 5263 | out: |
| 5264 | kfree(sa); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5265 | return ret; |
| 5266 | } |
| 5267 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5268 | static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info, |
| 5269 | void __user *arg) |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 5270 | { |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 5271 | size_t len; |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 5272 | int ret; |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 5273 | char label[BTRFS_LABEL_SIZE]; |
| 5274 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5275 | spin_lock(&fs_info->super_lock); |
| 5276 | memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); |
| 5277 | spin_unlock(&fs_info->super_lock); |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 5278 | |
| 5279 | len = strnlen(label, BTRFS_LABEL_SIZE); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 5280 | |
| 5281 | if (len == BTRFS_LABEL_SIZE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5282 | btrfs_warn(fs_info, |
| 5283 | "label is too long, return the first %zu bytes", |
| 5284 | --len); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 5285 | } |
| 5286 | |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 5287 | ret = copy_to_user(arg, label, len); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 5288 | |
| 5289 | return ret ? -EFAULT : 0; |
| 5290 | } |
| 5291 | |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 5292 | static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) |
| 5293 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5294 | struct inode *inode = file_inode(file); |
| 5295 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 5296 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5297 | struct btrfs_super_block *super_block = fs_info->super_copy; |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 5298 | struct btrfs_trans_handle *trans; |
| 5299 | char label[BTRFS_LABEL_SIZE]; |
| 5300 | int ret; |
| 5301 | |
| 5302 | if (!capable(CAP_SYS_ADMIN)) |
| 5303 | return -EPERM; |
| 5304 | |
| 5305 | if (copy_from_user(label, arg, sizeof(label))) |
| 5306 | return -EFAULT; |
| 5307 | |
| 5308 | if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5309 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5310 | "unable to set label with more than %d bytes", |
| 5311 | BTRFS_LABEL_SIZE - 1); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 5312 | return -EINVAL; |
| 5313 | } |
| 5314 | |
| 5315 | ret = mnt_want_write_file(file); |
| 5316 | if (ret) |
| 5317 | return ret; |
| 5318 | |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 5319 | trans = btrfs_start_transaction(root, 0); |
| 5320 | if (IS_ERR(trans)) { |
| 5321 | ret = PTR_ERR(trans); |
| 5322 | goto out_unlock; |
| 5323 | } |
| 5324 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5325 | spin_lock(&fs_info->super_lock); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 5326 | strcpy(super_block->label, label); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5327 | spin_unlock(&fs_info->super_lock); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5328 | ret = btrfs_commit_transaction(trans); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 5329 | |
| 5330 | out_unlock: |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 5331 | mnt_drop_write_file(file); |
| 5332 | return ret; |
| 5333 | } |
| 5334 | |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5335 | #define INIT_FEATURE_FLAGS(suffix) \ |
| 5336 | { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ |
| 5337 | .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ |
| 5338 | .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } |
| 5339 | |
David Sterba | d5131b6 | 2016-02-17 15:26:27 +0100 | [diff] [blame] | 5340 | int btrfs_ioctl_get_supported_features(void __user *arg) |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5341 | { |
David Sterba | 4d4ab6d | 2015-11-19 11:42:31 +0100 | [diff] [blame] | 5342 | static const struct btrfs_ioctl_feature_flags features[3] = { |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5343 | INIT_FEATURE_FLAGS(SUPP), |
| 5344 | INIT_FEATURE_FLAGS(SAFE_SET), |
| 5345 | INIT_FEATURE_FLAGS(SAFE_CLEAR) |
| 5346 | }; |
| 5347 | |
| 5348 | if (copy_to_user(arg, &features, sizeof(features))) |
| 5349 | return -EFAULT; |
| 5350 | |
| 5351 | return 0; |
| 5352 | } |
| 5353 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5354 | static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info, |
| 5355 | void __user *arg) |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5356 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5357 | struct btrfs_super_block *super_block = fs_info->super_copy; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5358 | struct btrfs_ioctl_feature_flags features; |
| 5359 | |
| 5360 | features.compat_flags = btrfs_super_compat_flags(super_block); |
| 5361 | features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); |
| 5362 | features.incompat_flags = btrfs_super_incompat_flags(super_block); |
| 5363 | |
| 5364 | if (copy_to_user(arg, &features, sizeof(features))) |
| 5365 | return -EFAULT; |
| 5366 | |
| 5367 | return 0; |
| 5368 | } |
| 5369 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5370 | static int check_feature_bits(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 5371 | enum btrfs_feature_set set, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5372 | u64 change_mask, u64 flags, u64 supported_flags, |
| 5373 | u64 safe_set, u64 safe_clear) |
| 5374 | { |
David Sterba | f10152b | 2019-08-01 19:07:55 +0200 | [diff] [blame] | 5375 | const char *type = btrfs_feature_set_name(set); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 5376 | char *names; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5377 | u64 disallowed, unsupported; |
| 5378 | u64 set_mask = flags & change_mask; |
| 5379 | u64 clear_mask = ~flags & change_mask; |
| 5380 | |
| 5381 | unsupported = set_mask & ~supported_flags; |
| 5382 | if (unsupported) { |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 5383 | names = btrfs_printable_features(set, unsupported); |
| 5384 | if (names) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5385 | btrfs_warn(fs_info, |
| 5386 | "this kernel does not support the %s feature bit%s", |
| 5387 | names, strchr(names, ',') ? "s" : ""); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 5388 | kfree(names); |
| 5389 | } else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5390 | btrfs_warn(fs_info, |
| 5391 | "this kernel does not support %s bits 0x%llx", |
| 5392 | type, unsupported); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5393 | return -EOPNOTSUPP; |
| 5394 | } |
| 5395 | |
| 5396 | disallowed = set_mask & ~safe_set; |
| 5397 | if (disallowed) { |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 5398 | names = btrfs_printable_features(set, disallowed); |
| 5399 | if (names) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5400 | btrfs_warn(fs_info, |
| 5401 | "can't set the %s feature bit%s while mounted", |
| 5402 | names, strchr(names, ',') ? "s" : ""); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 5403 | kfree(names); |
| 5404 | } else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5405 | btrfs_warn(fs_info, |
| 5406 | "can't set %s bits 0x%llx while mounted", |
| 5407 | type, disallowed); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5408 | return -EPERM; |
| 5409 | } |
| 5410 | |
| 5411 | disallowed = clear_mask & ~safe_clear; |
| 5412 | if (disallowed) { |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 5413 | names = btrfs_printable_features(set, disallowed); |
| 5414 | if (names) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5415 | btrfs_warn(fs_info, |
| 5416 | "can't clear the %s feature bit%s while mounted", |
| 5417 | names, strchr(names, ',') ? "s" : ""); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 5418 | kfree(names); |
| 5419 | } else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5420 | btrfs_warn(fs_info, |
| 5421 | "can't clear %s bits 0x%llx while mounted", |
| 5422 | type, disallowed); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5423 | return -EPERM; |
| 5424 | } |
| 5425 | |
| 5426 | return 0; |
| 5427 | } |
| 5428 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5429 | #define check_feature(fs_info, change_mask, flags, mask_base) \ |
| 5430 | check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5431 | BTRFS_FEATURE_ ## mask_base ## _SUPP, \ |
| 5432 | BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ |
| 5433 | BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) |
| 5434 | |
| 5435 | static int btrfs_ioctl_set_features(struct file *file, void __user *arg) |
| 5436 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5437 | struct inode *inode = file_inode(file); |
| 5438 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 5439 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5440 | struct btrfs_super_block *super_block = fs_info->super_copy; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5441 | struct btrfs_ioctl_feature_flags flags[2]; |
| 5442 | struct btrfs_trans_handle *trans; |
| 5443 | u64 newflags; |
| 5444 | int ret; |
| 5445 | |
| 5446 | if (!capable(CAP_SYS_ADMIN)) |
| 5447 | return -EPERM; |
| 5448 | |
| 5449 | if (copy_from_user(flags, arg, sizeof(flags))) |
| 5450 | return -EFAULT; |
| 5451 | |
| 5452 | /* Nothing to do */ |
| 5453 | if (!flags[0].compat_flags && !flags[0].compat_ro_flags && |
| 5454 | !flags[0].incompat_flags) |
| 5455 | return 0; |
| 5456 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5457 | ret = check_feature(fs_info, flags[0].compat_flags, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5458 | flags[1].compat_flags, COMPAT); |
| 5459 | if (ret) |
| 5460 | return ret; |
| 5461 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5462 | ret = check_feature(fs_info, flags[0].compat_ro_flags, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5463 | flags[1].compat_ro_flags, COMPAT_RO); |
| 5464 | if (ret) |
| 5465 | return ret; |
| 5466 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5467 | ret = check_feature(fs_info, flags[0].incompat_flags, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5468 | flags[1].incompat_flags, INCOMPAT); |
| 5469 | if (ret) |
| 5470 | return ret; |
| 5471 | |
David Sterba | 7ab1962 | 2016-05-04 11:32:00 +0200 | [diff] [blame] | 5472 | ret = mnt_want_write_file(file); |
| 5473 | if (ret) |
| 5474 | return ret; |
| 5475 | |
David Sterba | 8051aa1 | 2014-02-07 14:34:04 +0100 | [diff] [blame] | 5476 | trans = btrfs_start_transaction(root, 0); |
David Sterba | 7ab1962 | 2016-05-04 11:32:00 +0200 | [diff] [blame] | 5477 | if (IS_ERR(trans)) { |
| 5478 | ret = PTR_ERR(trans); |
| 5479 | goto out_drop_write; |
| 5480 | } |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5481 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5482 | spin_lock(&fs_info->super_lock); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5483 | newflags = btrfs_super_compat_flags(super_block); |
| 5484 | newflags |= flags[0].compat_flags & flags[1].compat_flags; |
| 5485 | newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); |
| 5486 | btrfs_set_super_compat_flags(super_block, newflags); |
| 5487 | |
| 5488 | newflags = btrfs_super_compat_ro_flags(super_block); |
| 5489 | newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; |
| 5490 | newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); |
| 5491 | btrfs_set_super_compat_ro_flags(super_block, newflags); |
| 5492 | |
| 5493 | newflags = btrfs_super_incompat_flags(super_block); |
| 5494 | newflags |= flags[0].incompat_flags & flags[1].incompat_flags; |
| 5495 | newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); |
| 5496 | btrfs_set_super_incompat_flags(super_block, newflags); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5497 | spin_unlock(&fs_info->super_lock); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5498 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5499 | ret = btrfs_commit_transaction(trans); |
David Sterba | 7ab1962 | 2016-05-04 11:32:00 +0200 | [diff] [blame] | 5500 | out_drop_write: |
| 5501 | mnt_drop_write_file(file); |
| 5502 | |
| 5503 | return ret; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5504 | } |
| 5505 | |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 5506 | static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) |
| 5507 | { |
| 5508 | struct btrfs_ioctl_send_args *arg; |
| 5509 | int ret; |
| 5510 | |
| 5511 | if (compat) { |
| 5512 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) |
| 5513 | struct btrfs_ioctl_send_args_32 args32; |
| 5514 | |
| 5515 | ret = copy_from_user(&args32, argp, sizeof(args32)); |
| 5516 | if (ret) |
| 5517 | return -EFAULT; |
| 5518 | arg = kzalloc(sizeof(*arg), GFP_KERNEL); |
| 5519 | if (!arg) |
| 5520 | return -ENOMEM; |
| 5521 | arg->send_fd = args32.send_fd; |
| 5522 | arg->clone_sources_count = args32.clone_sources_count; |
| 5523 | arg->clone_sources = compat_ptr(args32.clone_sources); |
| 5524 | arg->parent_root = args32.parent_root; |
| 5525 | arg->flags = args32.flags; |
| 5526 | memcpy(arg->reserved, args32.reserved, |
| 5527 | sizeof(args32.reserved)); |
| 5528 | #else |
| 5529 | return -ENOTTY; |
| 5530 | #endif |
| 5531 | } else { |
| 5532 | arg = memdup_user(argp, sizeof(*arg)); |
| 5533 | if (IS_ERR(arg)) |
| 5534 | return PTR_ERR(arg); |
| 5535 | } |
| 5536 | ret = btrfs_ioctl_send(file, arg); |
| 5537 | kfree(arg); |
| 5538 | return ret; |
| 5539 | } |
| 5540 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5541 | long btrfs_ioctl(struct file *file, unsigned int |
| 5542 | cmd, unsigned long arg) |
| 5543 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5544 | struct inode *inode = file_inode(file); |
| 5545 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 5546 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 5547 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5548 | |
| 5549 | switch (cmd) { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 5550 | case FS_IOC_GETFLAGS: |
| 5551 | return btrfs_ioctl_getflags(file, argp); |
| 5552 | case FS_IOC_SETFLAGS: |
| 5553 | return btrfs_ioctl_setflags(file, argp); |
| 5554 | case FS_IOC_GETVERSION: |
| 5555 | return btrfs_ioctl_getversion(file, argp); |
Eric Sandeen | 40cf931 | 2019-07-17 12:39:20 -0500 | [diff] [blame] | 5556 | case FS_IOC_GETFSLABEL: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5557 | return btrfs_ioctl_get_fslabel(fs_info, argp); |
Eric Sandeen | 40cf931 | 2019-07-17 12:39:20 -0500 | [diff] [blame] | 5558 | case FS_IOC_SETFSLABEL: |
| 5559 | return btrfs_ioctl_set_fslabel(file, argp); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5560 | case FITRIM: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5561 | return btrfs_ioctl_fitrim(fs_info, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5562 | case BTRFS_IOC_SNAP_CREATE: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 5563 | return btrfs_ioctl_snap_create(file, argp, 0); |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 5564 | case BTRFS_IOC_SNAP_CREATE_V2: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 5565 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 5566 | case BTRFS_IOC_SUBVOL_CREATE: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 5567 | return btrfs_ioctl_snap_create(file, argp, 1); |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 5568 | case BTRFS_IOC_SUBVOL_CREATE_V2: |
| 5569 | return btrfs_ioctl_snap_create_v2(file, argp, 1); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5570 | case BTRFS_IOC_SNAP_DESTROY: |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame^] | 5571 | return btrfs_ioctl_snap_destroy(file, argp, false); |
| 5572 | case BTRFS_IOC_SNAP_DESTROY_V2: |
| 5573 | return btrfs_ioctl_snap_destroy(file, argp, true); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 5574 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
| 5575 | return btrfs_ioctl_subvol_getflags(file, argp); |
| 5576 | case BTRFS_IOC_SUBVOL_SETFLAGS: |
| 5577 | return btrfs_ioctl_subvol_setflags(file, argp); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 5578 | case BTRFS_IOC_DEFAULT_SUBVOL: |
| 5579 | return btrfs_ioctl_default_subvol(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5580 | case BTRFS_IOC_DEFRAG: |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 5581 | return btrfs_ioctl_defrag(file, NULL); |
| 5582 | case BTRFS_IOC_DEFRAG_RANGE: |
| 5583 | return btrfs_ioctl_defrag(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5584 | case BTRFS_IOC_RESIZE: |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 5585 | return btrfs_ioctl_resize(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5586 | case BTRFS_IOC_ADD_DEV: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5587 | return btrfs_ioctl_add_dev(fs_info, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5588 | case BTRFS_IOC_RM_DEV: |
Miao Xie | da24927 | 2012-11-26 08:44:50 +0000 | [diff] [blame] | 5589 | return btrfs_ioctl_rm_dev(file, argp); |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 5590 | case BTRFS_IOC_RM_DEV_V2: |
| 5591 | return btrfs_ioctl_rm_dev_v2(file, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 5592 | case BTRFS_IOC_FS_INFO: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5593 | return btrfs_ioctl_fs_info(fs_info, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 5594 | case BTRFS_IOC_DEV_INFO: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5595 | return btrfs_ioctl_dev_info(fs_info, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5596 | case BTRFS_IOC_BALANCE: |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 5597 | return btrfs_ioctl_balance(file, NULL); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 5598 | case BTRFS_IOC_TREE_SEARCH: |
| 5599 | return btrfs_ioctl_tree_search(file, argp); |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 5600 | case BTRFS_IOC_TREE_SEARCH_V2: |
| 5601 | return btrfs_ioctl_tree_search_v2(file, argp); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 5602 | case BTRFS_IOC_INO_LOOKUP: |
| 5603 | return btrfs_ioctl_ino_lookup(file, argp); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 5604 | case BTRFS_IOC_INO_PATHS: |
| 5605 | return btrfs_ioctl_ino_to_path(root, argp); |
| 5606 | case BTRFS_IOC_LOGICAL_INO: |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 5607 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); |
| 5608 | case BTRFS_IOC_LOGICAL_INO_V2: |
| 5609 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 5610 | case BTRFS_IOC_SPACE_INFO: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5611 | return btrfs_ioctl_space_info(fs_info, argp); |
Filipe David Borba Manana | 9b19985 | 2013-09-23 11:35:11 +0100 | [diff] [blame] | 5612 | case BTRFS_IOC_SYNC: { |
| 5613 | int ret; |
| 5614 | |
Nikolay Borisov | 82b3e53 | 2018-04-23 10:54:13 +0300 | [diff] [blame] | 5615 | ret = btrfs_start_delalloc_roots(fs_info, -1); |
Filipe David Borba Manana | 9b19985 | 2013-09-23 11:35:11 +0100 | [diff] [blame] | 5616 | if (ret) |
| 5617 | return ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5618 | ret = btrfs_sync_fs(inode->i_sb, 1); |
David Sterba | 2fad4e8 | 2014-07-23 14:39:35 +0200 | [diff] [blame] | 5619 | /* |
| 5620 | * The transaction thread may want to do more work, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5621 | * namely it pokes the cleaner kthread that will start |
David Sterba | 2fad4e8 | 2014-07-23 14:39:35 +0200 | [diff] [blame] | 5622 | * processing uncleaned subvols. |
| 5623 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5624 | wake_up_process(fs_info->transaction_kthread); |
Filipe David Borba Manana | 9b19985 | 2013-09-23 11:35:11 +0100 | [diff] [blame] | 5625 | return ret; |
| 5626 | } |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 5627 | case BTRFS_IOC_START_SYNC: |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 5628 | return btrfs_ioctl_start_sync(root, argp); |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 5629 | case BTRFS_IOC_WAIT_SYNC: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5630 | return btrfs_ioctl_wait_sync(fs_info, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 5631 | case BTRFS_IOC_SCRUB: |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 5632 | return btrfs_ioctl_scrub(file, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 5633 | case BTRFS_IOC_SCRUB_CANCEL: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5634 | return btrfs_ioctl_scrub_cancel(fs_info); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 5635 | case BTRFS_IOC_SCRUB_PROGRESS: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5636 | return btrfs_ioctl_scrub_progress(fs_info, argp); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 5637 | case BTRFS_IOC_BALANCE_V2: |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 5638 | return btrfs_ioctl_balance(file, argp); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 5639 | case BTRFS_IOC_BALANCE_CTL: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5640 | return btrfs_ioctl_balance_ctl(fs_info, arg); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 5641 | case BTRFS_IOC_BALANCE_PROGRESS: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5642 | return btrfs_ioctl_balance_progress(fs_info, argp); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5643 | case BTRFS_IOC_SET_RECEIVED_SUBVOL: |
| 5644 | return btrfs_ioctl_set_received_subvol(file, argp); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 5645 | #ifdef CONFIG_64BIT |
| 5646 | case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: |
| 5647 | return btrfs_ioctl_set_received_subvol_32(file, argp); |
| 5648 | #endif |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5649 | case BTRFS_IOC_SEND: |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 5650 | return _btrfs_ioctl_send(file, argp, false); |
| 5651 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) |
| 5652 | case BTRFS_IOC_SEND_32: |
| 5653 | return _btrfs_ioctl_send(file, argp, true); |
| 5654 | #endif |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5655 | case BTRFS_IOC_GET_DEV_STATS: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5656 | return btrfs_ioctl_get_dev_stats(fs_info, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5657 | case BTRFS_IOC_QUOTA_CTL: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 5658 | return btrfs_ioctl_quota_ctl(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5659 | case BTRFS_IOC_QGROUP_ASSIGN: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 5660 | return btrfs_ioctl_qgroup_assign(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5661 | case BTRFS_IOC_QGROUP_CREATE: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 5662 | return btrfs_ioctl_qgroup_create(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5663 | case BTRFS_IOC_QGROUP_LIMIT: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 5664 | return btrfs_ioctl_qgroup_limit(file, argp); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 5665 | case BTRFS_IOC_QUOTA_RESCAN: |
| 5666 | return btrfs_ioctl_quota_rescan(file, argp); |
| 5667 | case BTRFS_IOC_QUOTA_RESCAN_STATUS: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5668 | return btrfs_ioctl_quota_rescan_status(fs_info, argp); |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 5669 | case BTRFS_IOC_QUOTA_RESCAN_WAIT: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5670 | return btrfs_ioctl_quota_rescan_wait(fs_info, argp); |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 5671 | case BTRFS_IOC_DEV_REPLACE: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5672 | return btrfs_ioctl_dev_replace(fs_info, argp); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5673 | case BTRFS_IOC_GET_SUPPORTED_FEATURES: |
David Sterba | d5131b6 | 2016-02-17 15:26:27 +0100 | [diff] [blame] | 5674 | return btrfs_ioctl_get_supported_features(argp); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5675 | case BTRFS_IOC_GET_FEATURES: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5676 | return btrfs_ioctl_get_features(fs_info, argp); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5677 | case BTRFS_IOC_SET_FEATURES: |
| 5678 | return btrfs_ioctl_set_features(file, argp); |
David Sterba | e4202ac | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 5679 | case FS_IOC_FSGETXATTR: |
| 5680 | return btrfs_ioctl_fsgetxattr(file, argp); |
David Sterba | 025f212 | 2018-03-26 19:51:16 +0200 | [diff] [blame] | 5681 | case FS_IOC_FSSETXATTR: |
| 5682 | return btrfs_ioctl_fssetxattr(file, argp); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 5683 | case BTRFS_IOC_GET_SUBVOL_INFO: |
| 5684 | return btrfs_ioctl_get_subvol_info(file, argp); |
Tomohiro Misono | 42e4b52 | 2018-05-21 10:09:43 +0900 | [diff] [blame] | 5685 | case BTRFS_IOC_GET_SUBVOL_ROOTREF: |
| 5686 | return btrfs_ioctl_get_subvol_rootref(file, argp); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 5687 | case BTRFS_IOC_INO_LOOKUP_USER: |
| 5688 | return btrfs_ioctl_ino_lookup_user(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5689 | } |
| 5690 | |
| 5691 | return -ENOTTY; |
| 5692 | } |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 5693 | |
| 5694 | #ifdef CONFIG_COMPAT |
| 5695 | long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 5696 | { |
Jeff Mahoney | 2a36224 | 2017-02-06 19:39:09 -0500 | [diff] [blame] | 5697 | /* |
| 5698 | * These all access 32-bit values anyway so no further |
| 5699 | * handling is necessary. |
| 5700 | */ |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 5701 | switch (cmd) { |
| 5702 | case FS_IOC32_GETFLAGS: |
| 5703 | cmd = FS_IOC_GETFLAGS; |
| 5704 | break; |
| 5705 | case FS_IOC32_SETFLAGS: |
| 5706 | cmd = FS_IOC_SETFLAGS; |
| 5707 | break; |
| 5708 | case FS_IOC32_GETVERSION: |
| 5709 | cmd = FS_IOC_GETVERSION; |
| 5710 | break; |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
| 5713 | return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
| 5714 | } |
| 5715 | #endif |