Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/bio.h> |
| 21 | #include <linux/buffer_head.h> |
| 22 | #include <linux/file.h> |
| 23 | #include <linux/fs.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 24 | #include <linux/fsnotify.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/highmem.h> |
| 27 | #include <linux/time.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/string.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 30 | #include <linux/backing-dev.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 31 | #include <linux/mount.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 32 | #include <linux/mpage.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 33 | #include <linux/namei.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 34 | #include <linux/swap.h> |
| 35 | #include <linux/writeback.h> |
| 36 | #include <linux/statfs.h> |
| 37 | #include <linux/compat.h> |
| 38 | #include <linux/bit_spinlock.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 39 | #include <linux/security.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 40 | #include <linux/xattr.h> |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 41 | #include <linux/vmalloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 43 | #include <linux/blkdev.h> |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 44 | #include <linux/uuid.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 45 | #include <linux/btrfs.h> |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 46 | #include <linux/uaccess.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 47 | #include "compat.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 48 | #include "ctree.h" |
| 49 | #include "disk-io.h" |
| 50 | #include "transaction.h" |
| 51 | #include "btrfs_inode.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 52 | #include "print-tree.h" |
| 53 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 54 | #include "locking.h" |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 55 | #include "inode-map.h" |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 56 | #include "backref.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 57 | #include "rcu-string.h" |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 58 | #include "send.h" |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 59 | #include "dev-replace.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 60 | |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 61 | static int btrfs_clone(struct inode *src, struct inode *inode, |
| 62 | u64 off, u64 olen, u64 olen_aligned, u64 destoff); |
| 63 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 64 | /* Mask out flags that are inappropriate for the given type of inode. */ |
| 65 | static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) |
| 66 | { |
| 67 | if (S_ISDIR(mode)) |
| 68 | return flags; |
| 69 | else if (S_ISREG(mode)) |
| 70 | return flags & ~FS_DIRSYNC_FL; |
| 71 | else |
| 72 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); |
| 73 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 74 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 75 | /* |
| 76 | * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. |
| 77 | */ |
| 78 | static unsigned int btrfs_flags_to_ioctl(unsigned int flags) |
| 79 | { |
| 80 | unsigned int iflags = 0; |
| 81 | |
| 82 | if (flags & BTRFS_INODE_SYNC) |
| 83 | iflags |= FS_SYNC_FL; |
| 84 | if (flags & BTRFS_INODE_IMMUTABLE) |
| 85 | iflags |= FS_IMMUTABLE_FL; |
| 86 | if (flags & BTRFS_INODE_APPEND) |
| 87 | iflags |= FS_APPEND_FL; |
| 88 | if (flags & BTRFS_INODE_NODUMP) |
| 89 | iflags |= FS_NODUMP_FL; |
| 90 | if (flags & BTRFS_INODE_NOATIME) |
| 91 | iflags |= FS_NOATIME_FL; |
| 92 | if (flags & BTRFS_INODE_DIRSYNC) |
| 93 | iflags |= FS_DIRSYNC_FL; |
Li Zefan | d0092bd | 2011-04-15 03:03:06 +0000 | [diff] [blame] | 94 | if (flags & BTRFS_INODE_NODATACOW) |
| 95 | iflags |= FS_NOCOW_FL; |
| 96 | |
| 97 | if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) |
| 98 | iflags |= FS_COMPR_FL; |
| 99 | else if (flags & BTRFS_INODE_NOCOMPRESS) |
| 100 | iflags |= FS_NOCOMP_FL; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 101 | |
| 102 | return iflags; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Update inode->i_flags based on the btrfs internal flags. |
| 107 | */ |
| 108 | void btrfs_update_iflags(struct inode *inode) |
| 109 | { |
| 110 | struct btrfs_inode *ip = BTRFS_I(inode); |
| 111 | |
| 112 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); |
| 113 | |
| 114 | if (ip->flags & BTRFS_INODE_SYNC) |
| 115 | inode->i_flags |= S_SYNC; |
| 116 | if (ip->flags & BTRFS_INODE_IMMUTABLE) |
| 117 | inode->i_flags |= S_IMMUTABLE; |
| 118 | if (ip->flags & BTRFS_INODE_APPEND) |
| 119 | inode->i_flags |= S_APPEND; |
| 120 | if (ip->flags & BTRFS_INODE_NOATIME) |
| 121 | inode->i_flags |= S_NOATIME; |
| 122 | if (ip->flags & BTRFS_INODE_DIRSYNC) |
| 123 | inode->i_flags |= S_DIRSYNC; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Inherit flags from the parent inode. |
| 128 | * |
Josef Bacik | e27425d | 2011-09-27 11:01:30 -0400 | [diff] [blame] | 129 | * Currently only the compression flags and the cow flags are inherited. |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 130 | */ |
| 131 | void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 132 | { |
Chris Mason | 0b4dcea | 2009-06-11 11:13:35 -0400 | [diff] [blame] | 133 | unsigned int flags; |
| 134 | |
| 135 | if (!dir) |
| 136 | return; |
| 137 | |
| 138 | flags = BTRFS_I(dir)->flags; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 139 | |
Josef Bacik | e27425d | 2011-09-27 11:01:30 -0400 | [diff] [blame] | 140 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 141 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 142 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 143 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 144 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 145 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 146 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 147 | |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 148 | if (flags & BTRFS_INODE_NODATACOW) { |
Josef Bacik | e27425d | 2011-09-27 11:01:30 -0400 | [diff] [blame] | 149 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
Liu Bo | 213490b | 2012-09-11 08:33:50 -0600 | [diff] [blame] | 150 | if (S_ISREG(inode->i_mode)) |
| 151 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 152 | } |
Josef Bacik | e27425d | 2011-09-27 11:01:30 -0400 | [diff] [blame] | 153 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 154 | btrfs_update_iflags(inode); |
| 155 | } |
| 156 | |
| 157 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) |
| 158 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 159 | struct btrfs_inode *ip = BTRFS_I(file_inode(file)); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 160 | unsigned int flags = btrfs_flags_to_ioctl(ip->flags); |
| 161 | |
| 162 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 163 | return -EFAULT; |
| 164 | return 0; |
| 165 | } |
| 166 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 167 | static int check_flags(unsigned int flags) |
| 168 | { |
| 169 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
| 170 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
| 171 | FS_SYNC_FL | FS_DIRSYNC_FL | \ |
Li Zefan | e1e8fb6 | 2011-04-15 03:02:49 +0000 | [diff] [blame] | 172 | FS_NOCOMP_FL | FS_COMPR_FL | |
| 173 | FS_NOCOW_FL)) |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 174 | return -EOPNOTSUPP; |
| 175 | |
| 176 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) |
| 177 | return -EINVAL; |
| 178 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 182 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) |
| 183 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 184 | struct inode *inode = file_inode(file); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 185 | struct btrfs_inode *ip = BTRFS_I(inode); |
| 186 | struct btrfs_root *root = ip->root; |
| 187 | struct btrfs_trans_handle *trans; |
| 188 | unsigned int flags, oldflags; |
| 189 | int ret; |
Li Zefan | f062abf | 2011-12-29 13:36:45 +0800 | [diff] [blame] | 190 | u64 ip_oldflags; |
| 191 | unsigned int i_oldflags; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 192 | umode_t mode; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 193 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 194 | if (btrfs_root_readonly(root)) |
| 195 | return -EROFS; |
| 196 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 197 | if (copy_from_user(&flags, arg, sizeof(flags))) |
| 198 | return -EFAULT; |
| 199 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 200 | ret = check_flags(flags); |
| 201 | if (ret) |
| 202 | return ret; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 203 | |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 204 | if (!inode_owner_or_capable(inode)) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 205 | return -EACCES; |
| 206 | |
Jan Kara | e784868 | 2012-06-12 16:20:32 +0200 | [diff] [blame] | 207 | ret = mnt_want_write_file(file); |
| 208 | if (ret) |
| 209 | return ret; |
| 210 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 211 | mutex_lock(&inode->i_mutex); |
| 212 | |
Li Zefan | f062abf | 2011-12-29 13:36:45 +0800 | [diff] [blame] | 213 | ip_oldflags = ip->flags; |
| 214 | i_oldflags = inode->i_flags; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 215 | mode = inode->i_mode; |
Li Zefan | f062abf | 2011-12-29 13:36:45 +0800 | [diff] [blame] | 216 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 217 | flags = btrfs_mask_flags(inode->i_mode, flags); |
| 218 | oldflags = btrfs_flags_to_ioctl(ip->flags); |
| 219 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
| 220 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 221 | ret = -EPERM; |
| 222 | goto out_unlock; |
| 223 | } |
| 224 | } |
| 225 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 226 | if (flags & FS_SYNC_FL) |
| 227 | ip->flags |= BTRFS_INODE_SYNC; |
| 228 | else |
| 229 | ip->flags &= ~BTRFS_INODE_SYNC; |
| 230 | if (flags & FS_IMMUTABLE_FL) |
| 231 | ip->flags |= BTRFS_INODE_IMMUTABLE; |
| 232 | else |
| 233 | ip->flags &= ~BTRFS_INODE_IMMUTABLE; |
| 234 | if (flags & FS_APPEND_FL) |
| 235 | ip->flags |= BTRFS_INODE_APPEND; |
| 236 | else |
| 237 | ip->flags &= ~BTRFS_INODE_APPEND; |
| 238 | if (flags & FS_NODUMP_FL) |
| 239 | ip->flags |= BTRFS_INODE_NODUMP; |
| 240 | else |
| 241 | ip->flags &= ~BTRFS_INODE_NODUMP; |
| 242 | if (flags & FS_NOATIME_FL) |
| 243 | ip->flags |= BTRFS_INODE_NOATIME; |
| 244 | else |
| 245 | ip->flags &= ~BTRFS_INODE_NOATIME; |
| 246 | if (flags & FS_DIRSYNC_FL) |
| 247 | ip->flags |= BTRFS_INODE_DIRSYNC; |
| 248 | else |
| 249 | ip->flags &= ~BTRFS_INODE_DIRSYNC; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 250 | if (flags & FS_NOCOW_FL) { |
| 251 | if (S_ISREG(mode)) { |
| 252 | /* |
| 253 | * It's safe to turn csums off here, no extents exist. |
| 254 | * Otherwise we want the flag to reflect the real COW |
| 255 | * status of the file and will not set it. |
| 256 | */ |
| 257 | if (inode->i_size == 0) |
| 258 | ip->flags |= BTRFS_INODE_NODATACOW |
| 259 | | BTRFS_INODE_NODATASUM; |
| 260 | } else { |
| 261 | ip->flags |= BTRFS_INODE_NODATACOW; |
| 262 | } |
| 263 | } else { |
| 264 | /* |
| 265 | * Revert back under same assuptions as above |
| 266 | */ |
| 267 | if (S_ISREG(mode)) { |
| 268 | if (inode->i_size == 0) |
| 269 | ip->flags &= ~(BTRFS_INODE_NODATACOW |
| 270 | | BTRFS_INODE_NODATASUM); |
| 271 | } else { |
| 272 | ip->flags &= ~BTRFS_INODE_NODATACOW; |
| 273 | } |
| 274 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 275 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 276 | /* |
| 277 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS |
| 278 | * flag may be changed automatically if compression code won't make |
| 279 | * things smaller. |
| 280 | */ |
| 281 | if (flags & FS_NOCOMP_FL) { |
| 282 | ip->flags &= ~BTRFS_INODE_COMPRESS; |
| 283 | ip->flags |= BTRFS_INODE_NOCOMPRESS; |
| 284 | } else if (flags & FS_COMPR_FL) { |
| 285 | ip->flags |= BTRFS_INODE_COMPRESS; |
| 286 | ip->flags &= ~BTRFS_INODE_NOCOMPRESS; |
Li Zefan | ebcb904 | 2011-04-15 03:03:17 +0000 | [diff] [blame] | 287 | } else { |
| 288 | ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 289 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 290 | |
Li Zefan | 4da6f1a | 2011-12-29 13:39:50 +0800 | [diff] [blame] | 291 | trans = btrfs_start_transaction(root, 1); |
Li Zefan | f062abf | 2011-12-29 13:36:45 +0800 | [diff] [blame] | 292 | if (IS_ERR(trans)) { |
| 293 | ret = PTR_ERR(trans); |
| 294 | goto out_drop; |
| 295 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 296 | |
Li Zefan | 306424cc | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 297 | btrfs_update_iflags(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 298 | inode_inc_iversion(inode); |
Li Zefan | 306424cc | 2011-12-14 20:12:02 -0500 | [diff] [blame] | 299 | inode->i_ctime = CURRENT_TIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 300 | ret = btrfs_update_inode(trans, root, inode); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 301 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 302 | btrfs_end_transaction(trans, root); |
Li Zefan | f062abf | 2011-12-29 13:36:45 +0800 | [diff] [blame] | 303 | out_drop: |
| 304 | if (ret) { |
| 305 | ip->flags = ip_oldflags; |
| 306 | inode->i_flags = i_oldflags; |
| 307 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 308 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 309 | out_unlock: |
| 310 | mutex_unlock(&inode->i_mutex); |
Jan Kara | e784868 | 2012-06-12 16:20:32 +0200 | [diff] [blame] | 311 | mnt_drop_write_file(file); |
liubo | 2d4e6f6 | 2011-02-24 09:38:16 +0000 | [diff] [blame] | 312 | return ret; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) |
| 316 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 317 | struct inode *inode = file_inode(file); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 318 | |
| 319 | return put_user(inode->i_generation, arg); |
| 320 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 321 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 322 | static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) |
| 323 | { |
Al Viro | 815745c | 2011-11-17 15:40:49 -0500 | [diff] [blame] | 324 | struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 325 | struct btrfs_device *device; |
| 326 | struct request_queue *q; |
| 327 | struct fstrim_range range; |
| 328 | u64 minlen = ULLONG_MAX; |
| 329 | u64 num_devices = 0; |
Al Viro | 815745c | 2011-11-17 15:40:49 -0500 | [diff] [blame] | 330 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 331 | int ret; |
| 332 | |
| 333 | if (!capable(CAP_SYS_ADMIN)) |
| 334 | return -EPERM; |
| 335 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 336 | rcu_read_lock(); |
| 337 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, |
| 338 | dev_list) { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 339 | if (!device->bdev) |
| 340 | continue; |
| 341 | q = bdev_get_queue(device->bdev); |
| 342 | if (blk_queue_discard(q)) { |
| 343 | num_devices++; |
| 344 | minlen = min((u64)q->limits.discard_granularity, |
| 345 | minlen); |
| 346 | } |
| 347 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 348 | rcu_read_unlock(); |
Lukas Czerner | f4c697e | 2011-09-05 16:34:54 +0200 | [diff] [blame] | 349 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 350 | if (!num_devices) |
| 351 | return -EOPNOTSUPP; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 352 | if (copy_from_user(&range, arg, sizeof(range))) |
| 353 | return -EFAULT; |
Lukas Czerner | e515c18 | 2012-10-16 09:34:36 +0000 | [diff] [blame] | 354 | if (range.start > total_bytes || |
| 355 | range.len < fs_info->sb->s_blocksize) |
Lukas Czerner | f4c697e | 2011-09-05 16:34:54 +0200 | [diff] [blame] | 356 | return -EINVAL; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 357 | |
Lukas Czerner | f4c697e | 2011-09-05 16:34:54 +0200 | [diff] [blame] | 358 | range.len = min(range.len, total_bytes - range.start); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 359 | range.minlen = max(range.minlen, minlen); |
Al Viro | 815745c | 2011-11-17 15:40:49 -0500 | [diff] [blame] | 360 | ret = btrfs_trim_fs(fs_info->tree_root, &range); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 361 | if (ret < 0) |
| 362 | return ret; |
| 363 | |
| 364 | if (copy_to_user(arg, &range, sizeof(range))) |
| 365 | return -EFAULT; |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 370 | int btrfs_is_empty_uuid(u8 *uuid) |
| 371 | { |
| 372 | static char empty_uuid[BTRFS_UUID_SIZE] = {0}; |
| 373 | |
| 374 | return !memcmp(uuid, empty_uuid, BTRFS_UUID_SIZE); |
| 375 | } |
| 376 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 377 | static noinline int create_subvol(struct inode *dir, |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 378 | struct dentry *dentry, |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 379 | char *name, int namelen, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 380 | u64 *async_transid, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 381 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 382 | { |
| 383 | struct btrfs_trans_handle *trans; |
| 384 | struct btrfs_key key; |
| 385 | struct btrfs_root_item root_item; |
| 386 | struct btrfs_inode_item *inode_item; |
| 387 | struct extent_buffer *leaf; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 388 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 389 | struct btrfs_root *new_root; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 390 | struct btrfs_block_rsv block_rsv; |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 391 | struct timespec cur_time = CURRENT_TIME; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 392 | int ret; |
| 393 | int err; |
| 394 | u64 objectid; |
| 395 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 396 | u64 index = 0; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 397 | u64 qgroup_reserved; |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 398 | uuid_le new_uuid; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 399 | |
Li Zefan | 581bb05 | 2011-04-20 10:06:11 +0800 | [diff] [blame] | 400 | ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); |
Al Viro | 2fbe8c8 | 2011-07-16 21:38:06 -0400 | [diff] [blame] | 401 | if (ret) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 402 | return ret; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 403 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 404 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 405 | /* |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 406 | * The same as the snapshot creation, please see the comment |
| 407 | * of create_snapshot(). |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 408 | */ |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 409 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 410 | 8, &qgroup_reserved, false); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 411 | if (ret) |
| 412 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 413 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 414 | trans = btrfs_start_transaction(root, 0); |
| 415 | if (IS_ERR(trans)) { |
| 416 | ret = PTR_ERR(trans); |
| 417 | goto out; |
| 418 | } |
| 419 | trans->block_rsv = &block_rsv; |
| 420 | trans->bytes_reserved = block_rsv.size; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 421 | |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 422 | ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit); |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 423 | if (ret) |
| 424 | goto fail; |
| 425 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 426 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 427 | 0, objectid, NULL, 0, 0, 0); |
Josef Bacik | 8e8a1e3 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 428 | if (IS_ERR(leaf)) { |
| 429 | ret = PTR_ERR(leaf); |
| 430 | goto fail; |
| 431 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 432 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 433 | memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 434 | btrfs_set_header_bytenr(leaf, leaf->start); |
| 435 | btrfs_set_header_generation(leaf, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 436 | btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 437 | btrfs_set_header_owner(leaf, objectid); |
| 438 | |
Geert Uytterhoeven | fba6aa7 | 2013-08-20 13:20:14 +0200 | [diff] [blame^] | 439 | write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(leaf), |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 440 | BTRFS_FSID_SIZE); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 441 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 442 | (unsigned long)btrfs_header_chunk_tree_uuid(leaf), |
| 443 | BTRFS_UUID_SIZE); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 444 | btrfs_mark_buffer_dirty(leaf); |
| 445 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 446 | memset(&root_item, 0, sizeof(root_item)); |
| 447 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 448 | inode_item = &root_item.inode; |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 449 | btrfs_set_stack_inode_generation(inode_item, 1); |
| 450 | btrfs_set_stack_inode_size(inode_item, 3); |
| 451 | btrfs_set_stack_inode_nlink(inode_item, 1); |
| 452 | btrfs_set_stack_inode_nbytes(inode_item, root->leafsize); |
| 453 | btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 454 | |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 455 | btrfs_set_root_flags(&root_item, 0); |
| 456 | btrfs_set_root_limit(&root_item, 0); |
| 457 | btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); |
Li Zefan | 08fe4db | 2011-03-28 02:01:25 +0000 | [diff] [blame] | 458 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 459 | btrfs_set_root_bytenr(&root_item, leaf->start); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 460 | btrfs_set_root_generation(&root_item, trans->transid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 461 | btrfs_set_root_level(&root_item, 0); |
| 462 | btrfs_set_root_refs(&root_item, 1); |
Yan, Zheng | 86b9f2e | 2009-11-12 09:36:50 +0000 | [diff] [blame] | 463 | btrfs_set_root_used(&root_item, leaf->len); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 464 | btrfs_set_root_last_snapshot(&root_item, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 465 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 466 | btrfs_set_root_generation_v2(&root_item, |
| 467 | btrfs_root_generation(&root_item)); |
| 468 | uuid_le_gen(&new_uuid); |
| 469 | memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 470 | btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec); |
| 471 | btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 472 | root_item.ctime = root_item.otime; |
| 473 | btrfs_set_root_ctransid(&root_item, trans->transid); |
| 474 | btrfs_set_root_otransid(&root_item, trans->transid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 475 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 476 | btrfs_tree_unlock(leaf); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 477 | free_extent_buffer(leaf); |
| 478 | leaf = NULL; |
| 479 | |
| 480 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 481 | |
| 482 | key.objectid = objectid; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 483 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 484 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 485 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 486 | &root_item); |
| 487 | if (ret) |
| 488 | goto fail; |
| 489 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 490 | key.offset = (u64)-1; |
| 491 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 492 | if (IS_ERR(new_root)) { |
| 493 | btrfs_abort_transaction(trans, root, PTR_ERR(new_root)); |
| 494 | ret = PTR_ERR(new_root); |
| 495 | goto fail; |
| 496 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 497 | |
| 498 | btrfs_record_root_in_trans(trans, new_root); |
| 499 | |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 500 | ret = btrfs_create_subvol_root(trans, new_root, new_dirid); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 501 | if (ret) { |
| 502 | /* We potentially lose an unused inode item here */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 503 | btrfs_abort_transaction(trans, root, ret); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 504 | goto fail; |
| 505 | } |
| 506 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 507 | /* |
| 508 | * insert the directory item |
| 509 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 510 | ret = btrfs_set_inode_index(dir, &index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 511 | if (ret) { |
| 512 | btrfs_abort_transaction(trans, root, ret); |
| 513 | goto fail; |
| 514 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 515 | |
| 516 | ret = btrfs_insert_dir_item(trans, root, |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 517 | name, namelen, dir, &key, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 518 | BTRFS_FT_DIR, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 519 | if (ret) { |
| 520 | btrfs_abort_transaction(trans, root, ret); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 521 | goto fail; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 522 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 523 | |
Yan Zheng | 52c2617 | 2009-01-05 15:43:43 -0500 | [diff] [blame] | 524 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
| 525 | ret = btrfs_update_inode(trans, root, dir); |
| 526 | BUG_ON(ret); |
| 527 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 528 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 529 | objectid, root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 530 | btrfs_ino(dir), index, name, namelen); |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 531 | BUG_ON(ret); |
| 532 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 533 | ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root, |
| 534 | root_item.uuid, BTRFS_UUID_KEY_SUBVOL, |
| 535 | objectid); |
| 536 | if (ret) |
| 537 | btrfs_abort_transaction(trans, root, ret); |
| 538 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 539 | fail: |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 540 | trans->block_rsv = NULL; |
| 541 | trans->bytes_reserved = 0; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 542 | if (async_transid) { |
| 543 | *async_transid = trans->transid; |
| 544 | err = btrfs_commit_transaction_async(trans, root, 1); |
Miao Xie | 00d71c9 | 2013-03-04 09:45:06 +0000 | [diff] [blame] | 545 | if (err) |
| 546 | err = btrfs_commit_transaction(trans, root); |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 547 | } else { |
| 548 | err = btrfs_commit_transaction(trans, root); |
| 549 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 550 | if (err && !ret) |
| 551 | ret = err; |
Chris Mason | 1a65e24 | 2013-02-06 12:06:02 -0500 | [diff] [blame] | 552 | |
| 553 | if (!ret) |
| 554 | d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 555 | out: |
| 556 | btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 557 | return ret; |
| 558 | } |
| 559 | |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 560 | static int create_snapshot(struct btrfs_root *root, struct inode *dir, |
| 561 | struct dentry *dentry, char *name, int namelen, |
| 562 | u64 *async_transid, bool readonly, |
| 563 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 564 | { |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 565 | struct inode *inode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 566 | struct btrfs_pending_snapshot *pending_snapshot; |
| 567 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 568 | int ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 569 | |
| 570 | if (!root->ref_cows) |
| 571 | return -EINVAL; |
| 572 | |
Miao Xie | 6a03843 | 2013-05-15 07:48:24 +0000 | [diff] [blame] | 573 | ret = btrfs_start_delalloc_inodes(root, 0); |
| 574 | if (ret) |
| 575 | return ret; |
| 576 | |
| 577 | btrfs_wait_ordered_extents(root, 0); |
| 578 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 579 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
| 580 | if (!pending_snapshot) |
| 581 | return -ENOMEM; |
| 582 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 583 | btrfs_init_block_rsv(&pending_snapshot->block_rsv, |
| 584 | BTRFS_BLOCK_RSV_TEMP); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 585 | /* |
| 586 | * 1 - parent dir inode |
| 587 | * 2 - dir entries |
| 588 | * 1 - root item |
| 589 | * 2 - root ref/backref |
| 590 | * 1 - root of snapshot |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 591 | * 1 - UUID item |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 592 | */ |
| 593 | ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 594 | &pending_snapshot->block_rsv, 8, |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 595 | &pending_snapshot->qgroup_reserved, |
| 596 | false); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 597 | if (ret) |
| 598 | goto out; |
| 599 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 600 | pending_snapshot->dentry = dentry; |
| 601 | pending_snapshot->root = root; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 602 | pending_snapshot->readonly = readonly; |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 603 | pending_snapshot->dir = dir; |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 604 | pending_snapshot->inherit = inherit; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 605 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 606 | trans = btrfs_start_transaction(root, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 607 | if (IS_ERR(trans)) { |
| 608 | ret = PTR_ERR(trans); |
| 609 | goto fail; |
| 610 | } |
| 611 | |
Josef Bacik | 8351583 | 2011-06-14 15:16:14 -0400 | [diff] [blame] | 612 | spin_lock(&root->fs_info->trans_lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 613 | list_add(&pending_snapshot->list, |
| 614 | &trans->transaction->pending_snapshots); |
Josef Bacik | 8351583 | 2011-06-14 15:16:14 -0400 | [diff] [blame] | 615 | spin_unlock(&root->fs_info->trans_lock); |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 616 | if (async_transid) { |
| 617 | *async_transid = trans->transid; |
| 618 | ret = btrfs_commit_transaction_async(trans, |
| 619 | root->fs_info->extent_root, 1); |
Miao Xie | 00d71c9 | 2013-03-04 09:45:06 +0000 | [diff] [blame] | 620 | if (ret) |
| 621 | ret = btrfs_commit_transaction(trans, root); |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 622 | } else { |
| 623 | ret = btrfs_commit_transaction(trans, |
| 624 | root->fs_info->extent_root); |
| 625 | } |
Miao Xie | aec8030 | 2013-03-04 09:44:29 +0000 | [diff] [blame] | 626 | if (ret) |
Josef Bacik | c37b2b6 | 2012-10-22 15:51:44 -0400 | [diff] [blame] | 627 | goto fail; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 628 | |
| 629 | ret = pending_snapshot->error; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 630 | if (ret) |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 631 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 632 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 633 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
| 634 | if (ret) |
| 635 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 636 | |
Al Viro | 2fbe8c8 | 2011-07-16 21:38:06 -0400 | [diff] [blame] | 637 | inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 638 | if (IS_ERR(inode)) { |
| 639 | ret = PTR_ERR(inode); |
| 640 | goto fail; |
| 641 | } |
| 642 | BUG_ON(!inode); |
| 643 | d_instantiate(dentry, inode); |
| 644 | ret = 0; |
| 645 | fail: |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 646 | btrfs_subvolume_release_metadata(BTRFS_I(dir)->root, |
| 647 | &pending_snapshot->block_rsv, |
| 648 | pending_snapshot->qgroup_reserved); |
| 649 | out: |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 650 | kfree(pending_snapshot); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 651 | return ret; |
| 652 | } |
| 653 | |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 654 | /* copy of check_sticky in fs/namei.c() |
| 655 | * It's inline, so penalty for filesystems that don't use sticky bit is |
| 656 | * minimal. |
| 657 | */ |
| 658 | static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) |
| 659 | { |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 660 | kuid_t fsuid = current_fsuid(); |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 661 | |
| 662 | if (!(dir->i_mode & S_ISVTX)) |
| 663 | return 0; |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 664 | if (uid_eq(inode->i_uid, fsuid)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 665 | return 0; |
Eric W. Biederman | 2f2f43d | 2012-02-10 11:05:07 -0800 | [diff] [blame] | 666 | if (uid_eq(dir->i_uid, fsuid)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 667 | return 0; |
| 668 | return !capable(CAP_FOWNER); |
| 669 | } |
| 670 | |
| 671 | /* copy of may_delete in fs/namei.c() |
| 672 | * Check whether we can remove a link victim from directory dir, check |
| 673 | * whether the type of victim is right. |
| 674 | * 1. We can't do it if dir is read-only (done in permission()) |
| 675 | * 2. We should have write and exec permissions on dir |
| 676 | * 3. We can't remove anything from append-only dir |
| 677 | * 4. We can't do anything with immutable dir (done in permission()) |
| 678 | * 5. If the sticky bit on dir is set we should either |
| 679 | * a. be owner of dir, or |
| 680 | * b. be owner of victim, or |
| 681 | * c. have CAP_FOWNER capability |
| 682 | * 6. If the victim is append-only or immutable we can't do antyhing with |
| 683 | * links pointing to it. |
| 684 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 685 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 686 | * 9. We can't remove a root or mountpoint. |
| 687 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by |
| 688 | * nfs_async_unlink(). |
| 689 | */ |
| 690 | |
| 691 | static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) |
| 692 | { |
| 693 | int error; |
| 694 | |
| 695 | if (!victim->d_inode) |
| 696 | return -ENOENT; |
| 697 | |
| 698 | BUG_ON(victim->d_parent->d_inode != dir); |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 699 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 700 | |
| 701 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 702 | if (error) |
| 703 | return error; |
| 704 | if (IS_APPEND(dir)) |
| 705 | return -EPERM; |
| 706 | if (btrfs_check_sticky(dir, victim->d_inode)|| |
| 707 | IS_APPEND(victim->d_inode)|| |
| 708 | IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) |
| 709 | return -EPERM; |
| 710 | if (isdir) { |
| 711 | if (!S_ISDIR(victim->d_inode->i_mode)) |
| 712 | return -ENOTDIR; |
| 713 | if (IS_ROOT(victim)) |
| 714 | return -EBUSY; |
| 715 | } else if (S_ISDIR(victim->d_inode->i_mode)) |
| 716 | return -EISDIR; |
| 717 | if (IS_DEADDIR(dir)) |
| 718 | return -ENOENT; |
| 719 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 720 | return -EBUSY; |
| 721 | return 0; |
| 722 | } |
| 723 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 724 | /* copy of may_create in fs/namei.c() */ |
| 725 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) |
| 726 | { |
| 727 | if (child->d_inode) |
| 728 | return -EEXIST; |
| 729 | if (IS_DEADDIR(dir)) |
| 730 | return -ENOENT; |
| 731 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * Create a new subvolume below @parent. This is largely modeled after |
| 736 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup |
| 737 | * inside this filesystem so it's quite a bit simpler. |
| 738 | */ |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 739 | static noinline int btrfs_mksubvol(struct path *parent, |
| 740 | char *name, int namelen, |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 741 | struct btrfs_root *snap_src, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 742 | u64 *async_transid, bool readonly, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 743 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 744 | { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 745 | struct inode *dir = parent->dentry->d_inode; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 746 | struct dentry *dentry; |
| 747 | int error; |
| 748 | |
David Sterba | 5c50c9b | 2013-03-22 18:12:51 +0000 | [diff] [blame] | 749 | error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); |
| 750 | if (error == -EINTR) |
| 751 | return error; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 752 | |
| 753 | dentry = lookup_one_len(name, parent->dentry, namelen); |
| 754 | error = PTR_ERR(dentry); |
| 755 | if (IS_ERR(dentry)) |
| 756 | goto out_unlock; |
| 757 | |
| 758 | error = -EEXIST; |
| 759 | if (dentry->d_inode) |
| 760 | goto out_dput; |
| 761 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 762 | error = btrfs_may_create(dir, dentry); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 763 | if (error) |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 764 | goto out_dput; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 765 | |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 766 | /* |
| 767 | * even if this name doesn't exist, we may get hash collisions. |
| 768 | * check for them now when we can safely fail |
| 769 | */ |
| 770 | error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, |
| 771 | dir->i_ino, name, |
| 772 | namelen); |
| 773 | if (error) |
| 774 | goto out_dput; |
| 775 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 776 | down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
| 777 | |
| 778 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) |
| 779 | goto out_up_read; |
| 780 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 781 | if (snap_src) { |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 782 | error = create_snapshot(snap_src, dir, dentry, name, namelen, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 783 | async_transid, readonly, inherit); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 784 | } else { |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 785 | error = create_subvol(dir, dentry, name, namelen, |
| 786 | async_transid, inherit); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 787 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 788 | if (!error) |
| 789 | fsnotify_mkdir(dir, dentry); |
| 790 | out_up_read: |
| 791 | up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 792 | out_dput: |
| 793 | dput(dentry); |
| 794 | out_unlock: |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 795 | mutex_unlock(&dir->i_mutex); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 796 | return error; |
| 797 | } |
| 798 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 799 | /* |
| 800 | * When we're defragging a range, we don't want to kick it off again |
| 801 | * if it is really just waiting for delalloc to send it down. |
| 802 | * If we find a nice big extent or delalloc range for the bytes in the |
| 803 | * file you want to defrag, we return 0 to let you know to skip this |
| 804 | * part of the file |
| 805 | */ |
| 806 | static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) |
| 807 | { |
| 808 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 809 | struct extent_map *em = NULL; |
| 810 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 811 | u64 end; |
| 812 | |
| 813 | read_lock(&em_tree->lock); |
| 814 | em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); |
| 815 | read_unlock(&em_tree->lock); |
| 816 | |
| 817 | if (em) { |
| 818 | end = extent_map_end(em); |
| 819 | free_extent_map(em); |
| 820 | if (end - offset > thresh) |
| 821 | return 0; |
| 822 | } |
| 823 | /* if we already have a nice delalloc here, just stop */ |
| 824 | thresh /= 2; |
| 825 | end = count_range_bits(io_tree, &offset, offset + thresh, |
| 826 | thresh, EXTENT_DELALLOC, 1); |
| 827 | if (end >= thresh) |
| 828 | return 0; |
| 829 | return 1; |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | * helper function to walk through a file and find extents |
| 834 | * newer than a specific transid, and smaller than thresh. |
| 835 | * |
| 836 | * This is used by the defragging code to find new and small |
| 837 | * extents |
| 838 | */ |
| 839 | static int find_new_extents(struct btrfs_root *root, |
| 840 | struct inode *inode, u64 newer_than, |
| 841 | u64 *off, int thresh) |
| 842 | { |
| 843 | struct btrfs_path *path; |
| 844 | struct btrfs_key min_key; |
| 845 | struct btrfs_key max_key; |
| 846 | struct extent_buffer *leaf; |
| 847 | struct btrfs_file_extent_item *extent; |
| 848 | int type; |
| 849 | int ret; |
David Sterba | a4689d2 | 2011-05-31 17:08:14 +0000 | [diff] [blame] | 850 | u64 ino = btrfs_ino(inode); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 851 | |
| 852 | path = btrfs_alloc_path(); |
| 853 | if (!path) |
| 854 | return -ENOMEM; |
| 855 | |
David Sterba | a4689d2 | 2011-05-31 17:08:14 +0000 | [diff] [blame] | 856 | min_key.objectid = ino; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 857 | min_key.type = BTRFS_EXTENT_DATA_KEY; |
| 858 | min_key.offset = *off; |
| 859 | |
David Sterba | a4689d2 | 2011-05-31 17:08:14 +0000 | [diff] [blame] | 860 | max_key.objectid = ino; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 861 | max_key.type = (u8)-1; |
| 862 | max_key.offset = (u64)-1; |
| 863 | |
| 864 | path->keep_locks = 1; |
| 865 | |
| 866 | while(1) { |
| 867 | ret = btrfs_search_forward(root, &min_key, &max_key, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 868 | path, newer_than); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 869 | if (ret != 0) |
| 870 | goto none; |
David Sterba | a4689d2 | 2011-05-31 17:08:14 +0000 | [diff] [blame] | 871 | if (min_key.objectid != ino) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 872 | goto none; |
| 873 | if (min_key.type != BTRFS_EXTENT_DATA_KEY) |
| 874 | goto none; |
| 875 | |
| 876 | leaf = path->nodes[0]; |
| 877 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 878 | struct btrfs_file_extent_item); |
| 879 | |
| 880 | type = btrfs_file_extent_type(leaf, extent); |
| 881 | if (type == BTRFS_FILE_EXTENT_REG && |
| 882 | btrfs_file_extent_num_bytes(leaf, extent) < thresh && |
| 883 | check_defrag_in_cache(inode, min_key.offset, thresh)) { |
| 884 | *off = min_key.offset; |
| 885 | btrfs_free_path(path); |
| 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | if (min_key.offset == (u64)-1) |
| 890 | goto none; |
| 891 | |
| 892 | min_key.offset++; |
| 893 | btrfs_release_path(path); |
| 894 | } |
| 895 | none: |
| 896 | btrfs_free_path(path); |
| 897 | return -ENOENT; |
| 898 | } |
| 899 | |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 900 | static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 901 | { |
| 902 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 903 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 904 | struct extent_map *em; |
| 905 | u64 len = PAGE_CACHE_SIZE; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 906 | |
| 907 | /* |
| 908 | * hopefully we have this extent in the tree already, try without |
| 909 | * the full extent lock |
| 910 | */ |
| 911 | read_lock(&em_tree->lock); |
| 912 | em = lookup_extent_mapping(em_tree, start, len); |
| 913 | read_unlock(&em_tree->lock); |
| 914 | |
| 915 | if (!em) { |
| 916 | /* get the big lock and read metadata off disk */ |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 917 | lock_extent(io_tree, start, start + len - 1); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 918 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 919 | unlock_extent(io_tree, start, start + len - 1); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 920 | |
Dan Carpenter | 6cf8bfb | 2010-03-20 11:22:10 +0000 | [diff] [blame] | 921 | if (IS_ERR(em)) |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 922 | return NULL; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 923 | } |
| 924 | |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 925 | return em; |
| 926 | } |
| 927 | |
| 928 | static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) |
| 929 | { |
| 930 | struct extent_map *next; |
| 931 | bool ret = true; |
| 932 | |
| 933 | /* this is the last extent */ |
| 934 | if (em->start + em->len >= i_size_read(inode)) |
| 935 | return false; |
| 936 | |
| 937 | next = defrag_lookup_extent(inode, em->start + em->len); |
| 938 | if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) |
| 939 | ret = false; |
| 940 | |
| 941 | free_extent_map(next); |
| 942 | return ret; |
| 943 | } |
| 944 | |
| 945 | static int should_defrag_range(struct inode *inode, u64 start, int thresh, |
Andrew Mahone | a43a211 | 2012-06-19 21:08:32 -0400 | [diff] [blame] | 946 | u64 *last_len, u64 *skip, u64 *defrag_end, |
| 947 | int compress) |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 948 | { |
| 949 | struct extent_map *em; |
| 950 | int ret = 1; |
| 951 | bool next_mergeable = true; |
| 952 | |
| 953 | /* |
| 954 | * make sure that once we start defragging an extent, we keep on |
| 955 | * defragging it |
| 956 | */ |
| 957 | if (start < *defrag_end) |
| 958 | return 1; |
| 959 | |
| 960 | *skip = 0; |
| 961 | |
| 962 | em = defrag_lookup_extent(inode, start); |
| 963 | if (!em) |
| 964 | return 0; |
| 965 | |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 966 | /* this will cover holes, and inline extents */ |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 967 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 968 | ret = 0; |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 969 | goto out; |
| 970 | } |
| 971 | |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 972 | next_mergeable = defrag_check_next_extent(inode, em); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 973 | |
| 974 | /* |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 975 | * we hit a real extent, if it is big or the next extent is not a |
| 976 | * real extent, don't bother defragging it |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 977 | */ |
Andrew Mahone | a43a211 | 2012-06-19 21:08:32 -0400 | [diff] [blame] | 978 | if (!compress && (*last_len == 0 || *last_len >= thresh) && |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 979 | (em->len >= thresh || !next_mergeable)) |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 980 | ret = 0; |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 981 | out: |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 982 | /* |
| 983 | * last_len ends up being a counter of how many bytes we've defragged. |
| 984 | * every time we choose not to defrag an extent, we reset *last_len |
| 985 | * so that the next tiny extent will force a defrag. |
| 986 | * |
| 987 | * The end result of this is that tiny extents before a single big |
| 988 | * extent will force at least part of that big extent to be defragged. |
| 989 | */ |
| 990 | if (ret) { |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 991 | *defrag_end = extent_map_end(em); |
| 992 | } else { |
| 993 | *last_len = 0; |
| 994 | *skip = extent_map_end(em); |
| 995 | *defrag_end = 0; |
| 996 | } |
| 997 | |
| 998 | free_extent_map(em); |
| 999 | return ret; |
| 1000 | } |
| 1001 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1002 | /* |
| 1003 | * it doesn't do much good to defrag one or two pages |
| 1004 | * at a time. This pulls in a nice chunk of pages |
| 1005 | * to COW and defrag. |
| 1006 | * |
| 1007 | * It also makes sure the delalloc code has enough |
| 1008 | * dirty data to avoid making new small extents as part |
| 1009 | * of the defrag |
| 1010 | * |
| 1011 | * It's a good idea to start RA on this range |
| 1012 | * before calling this. |
| 1013 | */ |
| 1014 | static int cluster_pages_for_defrag(struct inode *inode, |
| 1015 | struct page **pages, |
| 1016 | unsigned long start_index, |
| 1017 | int num_pages) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1018 | { |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1019 | unsigned long file_end; |
| 1020 | u64 isize = i_size_read(inode); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1021 | u64 page_start; |
| 1022 | u64 page_end; |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1023 | u64 page_cnt; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1024 | int ret; |
| 1025 | int i; |
| 1026 | int i_done; |
| 1027 | struct btrfs_ordered_extent *ordered; |
| 1028 | struct extent_state *cached_state = NULL; |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1029 | struct extent_io_tree *tree; |
Josef Bacik | 3b16a4e | 2011-09-21 15:05:58 -0400 | [diff] [blame] | 1030 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1031 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1032 | file_end = (isize - 1) >> PAGE_CACHE_SHIFT; |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1033 | if (!isize || start_index > file_end) |
| 1034 | return 0; |
| 1035 | |
| 1036 | 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] | 1037 | |
| 1038 | ret = btrfs_delalloc_reserve_space(inode, |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1039 | page_cnt << PAGE_CACHE_SHIFT); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1040 | if (ret) |
| 1041 | return ret; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1042 | i_done = 0; |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1043 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1044 | |
| 1045 | /* step one, lock all the pages */ |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1046 | for (i = 0; i < page_cnt; i++) { |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1047 | struct page *page; |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1048 | again: |
Josef Bacik | a94733d | 2011-07-11 10:47:06 -0400 | [diff] [blame] | 1049 | page = find_or_create_page(inode->i_mapping, |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1050 | start_index + i, mask); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1051 | if (!page) |
| 1052 | break; |
| 1053 | |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1054 | page_start = page_offset(page); |
| 1055 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 1056 | while (1) { |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1057 | lock_extent(tree, page_start, page_end); |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1058 | ordered = btrfs_lookup_ordered_extent(inode, |
| 1059 | page_start); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1060 | unlock_extent(tree, page_start, page_end); |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1061 | if (!ordered) |
| 1062 | break; |
| 1063 | |
| 1064 | unlock_page(page); |
| 1065 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1066 | btrfs_put_ordered_extent(ordered); |
| 1067 | lock_page(page); |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1068 | /* |
| 1069 | * we unlocked the page above, so we need check if |
| 1070 | * it was released or not. |
| 1071 | */ |
| 1072 | if (page->mapping != inode->i_mapping) { |
| 1073 | unlock_page(page); |
| 1074 | page_cache_release(page); |
| 1075 | goto again; |
| 1076 | } |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1077 | } |
| 1078 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1079 | if (!PageUptodate(page)) { |
| 1080 | btrfs_readpage(NULL, page); |
| 1081 | lock_page(page); |
| 1082 | if (!PageUptodate(page)) { |
| 1083 | unlock_page(page); |
| 1084 | page_cache_release(page); |
| 1085 | ret = -EIO; |
| 1086 | break; |
| 1087 | } |
| 1088 | } |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1089 | |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1090 | if (page->mapping != inode->i_mapping) { |
| 1091 | unlock_page(page); |
| 1092 | page_cache_release(page); |
| 1093 | goto again; |
| 1094 | } |
| 1095 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1096 | pages[i] = page; |
| 1097 | i_done++; |
| 1098 | } |
| 1099 | if (!i_done || ret) |
| 1100 | goto out; |
| 1101 | |
| 1102 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) |
| 1103 | goto out; |
| 1104 | |
| 1105 | /* |
| 1106 | * so now we have a nice long stream of locked |
| 1107 | * and up to date pages, lets wait on them |
| 1108 | */ |
| 1109 | for (i = 0; i < i_done; i++) |
| 1110 | wait_on_page_writeback(pages[i]); |
| 1111 | |
| 1112 | page_start = page_offset(pages[0]); |
| 1113 | page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; |
| 1114 | |
| 1115 | lock_extent_bits(&BTRFS_I(inode)->io_tree, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1116 | page_start, page_end - 1, 0, &cached_state); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1117 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, |
| 1118 | page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 1119 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, |
| 1120 | &cached_state, GFP_NOFS); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1121 | |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1122 | if (i_done != page_cnt) { |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 1123 | spin_lock(&BTRFS_I(inode)->lock); |
| 1124 | BTRFS_I(inode)->outstanding_extents++; |
| 1125 | spin_unlock(&BTRFS_I(inode)->lock); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1126 | btrfs_delalloc_release_space(inode, |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1127 | (page_cnt - i_done) << PAGE_CACHE_SHIFT); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | |
Liu Bo | 9e8a4a8 | 2012-09-05 19:10:51 -0600 | [diff] [blame] | 1131 | set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, |
| 1132 | &cached_state, GFP_NOFS); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1133 | |
| 1134 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 1135 | page_start, page_end - 1, &cached_state, |
| 1136 | GFP_NOFS); |
| 1137 | |
| 1138 | for (i = 0; i < i_done; i++) { |
| 1139 | clear_page_dirty_for_io(pages[i]); |
| 1140 | ClearPageChecked(pages[i]); |
| 1141 | set_page_extent_mapped(pages[i]); |
| 1142 | set_page_dirty(pages[i]); |
| 1143 | unlock_page(pages[i]); |
| 1144 | page_cache_release(pages[i]); |
| 1145 | } |
| 1146 | return i_done; |
| 1147 | out: |
| 1148 | for (i = 0; i < i_done; i++) { |
| 1149 | unlock_page(pages[i]); |
| 1150 | page_cache_release(pages[i]); |
| 1151 | } |
Liu Bo | 1f12bd0 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1152 | btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1153 | return ret; |
| 1154 | |
| 1155 | } |
| 1156 | |
| 1157 | int btrfs_defrag_file(struct inode *inode, struct file *file, |
| 1158 | struct btrfs_ioctl_defrag_range_args *range, |
| 1159 | u64 newer_than, unsigned long max_to_defrag) |
| 1160 | { |
| 1161 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1162 | struct file_ra_state *ra = NULL; |
| 1163 | unsigned long last_index; |
Li Zefan | 151a31b | 2011-09-02 15:56:39 +0800 | [diff] [blame] | 1164 | u64 isize = i_size_read(inode); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1165 | u64 last_len = 0; |
| 1166 | u64 skip = 0; |
| 1167 | u64 defrag_end = 0; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1168 | u64 newer_off = range->start; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1169 | unsigned long i; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1170 | unsigned long ra_index = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1171 | int ret; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1172 | int defrag_count = 0; |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1173 | int compress_type = BTRFS_COMPRESS_ZLIB; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1174 | int extent_thresh = range->extent_thresh; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1175 | int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; |
| 1176 | int cluster = max_cluster; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1177 | u64 new_align = ~((u64)128 * 1024 - 1); |
| 1178 | struct page **pages = NULL; |
| 1179 | |
Liu Bo | 0abd5b1 | 2013-04-16 09:20:28 +0000 | [diff] [blame] | 1180 | if (isize == 0) |
| 1181 | return 0; |
| 1182 | |
| 1183 | if (range->start >= isize) |
| 1184 | return -EINVAL; |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1185 | |
| 1186 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { |
| 1187 | if (range->compress_type > BTRFS_COMPRESS_TYPES) |
| 1188 | return -EINVAL; |
| 1189 | if (range->compress_type) |
| 1190 | compress_type = range->compress_type; |
| 1191 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1192 | |
Liu Bo | 0abd5b1 | 2013-04-16 09:20:28 +0000 | [diff] [blame] | 1193 | if (extent_thresh == 0) |
| 1194 | extent_thresh = 256 * 1024; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1195 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1196 | /* |
| 1197 | * if we were not given a file, allocate a readahead |
| 1198 | * context |
| 1199 | */ |
| 1200 | if (!file) { |
| 1201 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
| 1202 | if (!ra) |
| 1203 | return -ENOMEM; |
| 1204 | file_ra_state_init(ra, inode->i_mapping); |
| 1205 | } else { |
| 1206 | ra = &file->f_ra; |
| 1207 | } |
| 1208 | |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1209 | pages = kmalloc(sizeof(struct page *) * max_cluster, |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1210 | GFP_NOFS); |
| 1211 | if (!pages) { |
| 1212 | ret = -ENOMEM; |
| 1213 | goto out_ra; |
| 1214 | } |
| 1215 | |
| 1216 | /* find the last page to defrag */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1217 | if (range->start + range->len > range->start) { |
Li Zefan | 151a31b | 2011-09-02 15:56:39 +0800 | [diff] [blame] | 1218 | last_index = min_t(u64, isize - 1, |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1219 | range->start + range->len - 1) >> PAGE_CACHE_SHIFT; |
| 1220 | } else { |
Li Zefan | 151a31b | 2011-09-02 15:56:39 +0800 | [diff] [blame] | 1221 | last_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1222 | } |
| 1223 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1224 | if (newer_than) { |
| 1225 | ret = find_new_extents(root, inode, newer_than, |
| 1226 | &newer_off, 64 * 1024); |
| 1227 | if (!ret) { |
| 1228 | range->start = newer_off; |
| 1229 | /* |
| 1230 | * we always align our defrag to help keep |
| 1231 | * the extents in the file evenly spaced |
| 1232 | */ |
| 1233 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1234 | } else |
| 1235 | goto out_ra; |
| 1236 | } else { |
| 1237 | i = range->start >> PAGE_CACHE_SHIFT; |
| 1238 | } |
| 1239 | if (!max_to_defrag) |
Liu Bo | 7ec31b5 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 1240 | max_to_defrag = last_index + 1; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1241 | |
Li Zefan | 2a0f7f5 | 2011-10-10 15:43:34 -0400 | [diff] [blame] | 1242 | /* |
| 1243 | * make writeback starts from i, so the defrag range can be |
| 1244 | * written sequentially. |
| 1245 | */ |
| 1246 | if (i < inode->i_mapping->writeback_index) |
| 1247 | inode->i_mapping->writeback_index = i; |
| 1248 | |
Chris Mason | f7f43cc | 2011-10-11 11:41:40 -0400 | [diff] [blame] | 1249 | while (i <= last_index && defrag_count < max_to_defrag && |
| 1250 | (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> |
| 1251 | PAGE_CACHE_SHIFT)) { |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1252 | /* |
| 1253 | * make sure we stop running if someone unmounts |
| 1254 | * the FS |
| 1255 | */ |
| 1256 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) |
| 1257 | break; |
| 1258 | |
David Sterba | 210549e | 2013-02-09 23:38:06 +0000 | [diff] [blame] | 1259 | if (btrfs_defrag_cancelled(root->fs_info)) { |
| 1260 | printk(KERN_DEBUG "btrfs: defrag_file cancelled\n"); |
| 1261 | ret = -EAGAIN; |
| 1262 | break; |
| 1263 | } |
| 1264 | |
Liu Bo | 66c2689 | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1265 | if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1266 | extent_thresh, &last_len, &skip, |
Andrew Mahone | a43a211 | 2012-06-19 21:08:32 -0400 | [diff] [blame] | 1267 | &defrag_end, range->flags & |
| 1268 | BTRFS_DEFRAG_RANGE_COMPRESS)) { |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1269 | unsigned long next; |
| 1270 | /* |
| 1271 | * the should_defrag function tells us how much to skip |
| 1272 | * bump our counter by the suggested amount |
| 1273 | */ |
| 1274 | next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 1275 | i = max(i + 1, next); |
| 1276 | continue; |
| 1277 | } |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1278 | |
| 1279 | if (!newer_than) { |
| 1280 | cluster = (PAGE_CACHE_ALIGN(defrag_end) >> |
| 1281 | PAGE_CACHE_SHIFT) - i; |
| 1282 | cluster = min(cluster, max_cluster); |
| 1283 | } else { |
| 1284 | cluster = max_cluster; |
| 1285 | } |
| 1286 | |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1287 | if (i + cluster > ra_index) { |
| 1288 | ra_index = max(i, ra_index); |
| 1289 | btrfs_force_ra(inode->i_mapping, ra, file, ra_index, |
| 1290 | cluster); |
| 1291 | ra_index += max_cluster; |
| 1292 | } |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1293 | |
Liu Bo | ecb8bea | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1294 | mutex_lock(&inode->i_mutex); |
Filipe David Borba Manana | 633085c | 2013-08-16 15:23:33 +0100 | [diff] [blame] | 1295 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) |
| 1296 | BTRFS_I(inode)->force_compress = compress_type; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1297 | ret = cluster_pages_for_defrag(inode, pages, i, cluster); |
Liu Bo | ecb8bea | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1298 | if (ret < 0) { |
| 1299 | mutex_unlock(&inode->i_mutex); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1300 | goto out_ra; |
Liu Bo | ecb8bea | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1301 | } |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1302 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1303 | defrag_count += ret; |
Namjae Jeon | d0e1d66 | 2012-12-11 16:00:21 -0800 | [diff] [blame] | 1304 | balance_dirty_pages_ratelimited(inode->i_mapping); |
Liu Bo | ecb8bea | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1305 | mutex_unlock(&inode->i_mutex); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1306 | |
| 1307 | if (newer_than) { |
| 1308 | if (newer_off == (u64)-1) |
| 1309 | break; |
| 1310 | |
Liu Bo | e1f041e | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1311 | if (ret > 0) |
| 1312 | i += ret; |
| 1313 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1314 | newer_off = max(newer_off + 1, |
| 1315 | (u64)i << PAGE_CACHE_SHIFT); |
| 1316 | |
| 1317 | ret = find_new_extents(root, inode, |
| 1318 | newer_than, &newer_off, |
| 1319 | 64 * 1024); |
| 1320 | if (!ret) { |
| 1321 | range->start = newer_off; |
| 1322 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1323 | } else { |
| 1324 | break; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1325 | } |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1326 | } else { |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1327 | if (ret > 0) { |
Li Zefan | cbcc832 | 2011-09-02 15:56:25 +0800 | [diff] [blame] | 1328 | i += ret; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1329 | last_len += ret << PAGE_CACHE_SHIFT; |
| 1330 | } else { |
Li Zefan | cbcc832 | 2011-09-02 15:56:25 +0800 | [diff] [blame] | 1331 | i++; |
Li Zefan | 008873e | 2011-09-02 15:57:07 +0800 | [diff] [blame] | 1332 | last_len = 0; |
| 1333 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1334 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1335 | } |
| 1336 | |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1337 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) |
| 1338 | filemap_flush(inode->i_mapping); |
| 1339 | |
| 1340 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { |
| 1341 | /* the filemap_flush will queue IO into the worker threads, but |
| 1342 | * we have to make sure the IO is actually started and that |
| 1343 | * ordered extents get created before we return |
| 1344 | */ |
| 1345 | atomic_inc(&root->fs_info->async_submit_draining); |
| 1346 | while (atomic_read(&root->fs_info->nr_async_submits) || |
| 1347 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1348 | wait_event(root->fs_info->async_submit_wait, |
| 1349 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 1350 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
| 1351 | } |
| 1352 | atomic_dec(&root->fs_info->async_submit_draining); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1353 | } |
| 1354 | |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1355 | if (range->compress_type == BTRFS_COMPRESS_LZO) { |
Mitch Harder | 2b0ce2c | 2012-07-24 11:58:43 -0600 | [diff] [blame] | 1356 | btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO); |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1357 | } |
| 1358 | |
Diego Calleja | 60ccf82 | 2011-09-01 16:33:57 +0200 | [diff] [blame] | 1359 | ret = defrag_count; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1360 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1361 | out_ra: |
Filipe David Borba Manana | 633085c | 2013-08-16 15:23:33 +0100 | [diff] [blame] | 1362 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { |
| 1363 | mutex_lock(&inode->i_mutex); |
| 1364 | BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; |
| 1365 | mutex_unlock(&inode->i_mutex); |
| 1366 | } |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1367 | if (!file) |
| 1368 | kfree(ra); |
| 1369 | kfree(pages); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1370 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1371 | } |
| 1372 | |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 1373 | static noinline int btrfs_ioctl_resize(struct file *file, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1374 | void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1375 | { |
| 1376 | u64 new_size; |
| 1377 | u64 old_size; |
| 1378 | u64 devid = 1; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1379 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1380 | struct btrfs_ioctl_vol_args *vol_args; |
| 1381 | struct btrfs_trans_handle *trans; |
| 1382 | struct btrfs_device *device = NULL; |
| 1383 | char *sizestr; |
| 1384 | char *devstr = NULL; |
| 1385 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1386 | int mod = 0; |
| 1387 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1388 | if (!capable(CAP_SYS_ADMIN)) |
| 1389 | return -EPERM; |
| 1390 | |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 1391 | ret = mnt_want_write_file(file); |
| 1392 | if (ret) |
| 1393 | return ret; |
| 1394 | |
Stefan Behrens | 5ac00ad | 2012-11-05 17:54:08 +0100 | [diff] [blame] | 1395 | if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, |
| 1396 | 1)) { |
| 1397 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); |
Miao Xie | 9754767 | 2012-12-21 10:38:50 +0000 | [diff] [blame] | 1398 | mnt_drop_write_file(file); |
Ilya Dryomov | 2c0c9da | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 1399 | return -EINVAL; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1400 | } |
| 1401 | |
Stefan Behrens | 5ac00ad | 2012-11-05 17:54:08 +0100 | [diff] [blame] | 1402 | mutex_lock(&root->fs_info->volume_mutex); |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 1403 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1404 | if (IS_ERR(vol_args)) { |
| 1405 | ret = PTR_ERR(vol_args); |
| 1406 | goto out; |
| 1407 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 1408 | |
| 1409 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1410 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1411 | sizestr = vol_args->name; |
| 1412 | devstr = strchr(sizestr, ':'); |
| 1413 | if (devstr) { |
| 1414 | char *end; |
| 1415 | sizestr = devstr + 1; |
| 1416 | *devstr = '\0'; |
| 1417 | devstr = vol_args->name; |
| 1418 | devid = simple_strtoull(devstr, &end, 10); |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1419 | if (!devid) { |
| 1420 | ret = -EINVAL; |
| 1421 | goto out_free; |
| 1422 | } |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1423 | printk(KERN_INFO "btrfs: resizing devid %llu\n", devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1424 | } |
Miao Xie | dba60f3 | 2012-12-21 09:19:51 +0000 | [diff] [blame] | 1425 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1426 | device = btrfs_find_device(root->fs_info, devid, NULL, NULL); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1427 | if (!device) { |
Arnd Hannemann | 5bb1468 | 2011-11-20 07:33:38 -0500 | [diff] [blame] | 1428 | printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1429 | devid); |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1430 | ret = -ENODEV; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1431 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1432 | } |
Miao Xie | dba60f3 | 2012-12-21 09:19:51 +0000 | [diff] [blame] | 1433 | |
| 1434 | if (!device->writeable) { |
Liu Bo | 4e42ae1 | 2012-06-14 02:23:19 -0600 | [diff] [blame] | 1435 | printk(KERN_INFO "btrfs: resizer unable to apply on " |
Miao Xie | dba60f3 | 2012-12-21 09:19:51 +0000 | [diff] [blame] | 1436 | "readonly device %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1437 | devid); |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1438 | ret = -EPERM; |
Liu Bo | 4e42ae1 | 2012-06-14 02:23:19 -0600 | [diff] [blame] | 1439 | goto out_free; |
| 1440 | } |
| 1441 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1442 | if (!strcmp(sizestr, "max")) |
| 1443 | new_size = device->bdev->bd_inode->i_size; |
| 1444 | else { |
| 1445 | if (sizestr[0] == '-') { |
| 1446 | mod = -1; |
| 1447 | sizestr++; |
| 1448 | } else if (sizestr[0] == '+') { |
| 1449 | mod = 1; |
| 1450 | sizestr++; |
| 1451 | } |
Akinobu Mita | 9174846 | 2010-02-28 10:59:11 +0000 | [diff] [blame] | 1452 | new_size = memparse(sizestr, NULL); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1453 | if (new_size == 0) { |
| 1454 | ret = -EINVAL; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1455 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1459 | if (device->is_tgtdev_for_dev_replace) { |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1460 | ret = -EPERM; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1461 | goto out_free; |
| 1462 | } |
| 1463 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1464 | old_size = device->total_bytes; |
| 1465 | |
| 1466 | if (mod < 0) { |
| 1467 | if (new_size > old_size) { |
| 1468 | ret = -EINVAL; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1469 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1470 | } |
| 1471 | new_size = old_size - new_size; |
| 1472 | } else if (mod > 0) { |
| 1473 | new_size = old_size + new_size; |
| 1474 | } |
| 1475 | |
| 1476 | if (new_size < 256 * 1024 * 1024) { |
| 1477 | ret = -EINVAL; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1478 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1479 | } |
| 1480 | if (new_size > device->bdev->bd_inode->i_size) { |
| 1481 | ret = -EFBIG; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1482 | goto out_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | do_div(new_size, root->sectorsize); |
| 1486 | new_size *= root->sectorsize; |
| 1487 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1488 | printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1489 | rcu_str_deref(device->name), new_size); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1490 | |
| 1491 | if (new_size > old_size) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1492 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1493 | if (IS_ERR(trans)) { |
| 1494 | ret = PTR_ERR(trans); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1495 | goto out_free; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1496 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1497 | ret = btrfs_grow_device(trans, device, new_size); |
| 1498 | btrfs_commit_transaction(trans, root); |
Mike Fleetwood | ece7d20 | 2011-11-18 18:55:01 +0000 | [diff] [blame] | 1499 | } else if (new_size < old_size) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1500 | ret = btrfs_shrink_device(device, new_size); |
jeff.liu | 0253f40 | 2012-10-27 12:06:39 +0000 | [diff] [blame] | 1501 | } /* equal, nothing need to do */ |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1502 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1503 | out_free: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1504 | kfree(vol_args); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1505 | out: |
| 1506 | mutex_unlock(&root->fs_info->volume_mutex); |
Stefan Behrens | 5ac00ad | 2012-11-05 17:54:08 +0100 | [diff] [blame] | 1507 | atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | 18f39c4 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 1508 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1509 | return ret; |
| 1510 | } |
| 1511 | |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1512 | static noinline int btrfs_ioctl_snap_create_transid(struct file *file, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1513 | char *name, unsigned long fd, int subvol, |
| 1514 | u64 *transid, bool readonly, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 1515 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1516 | { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1517 | int namelen; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1518 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1519 | |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1520 | ret = mnt_want_write_file(file); |
| 1521 | if (ret) |
| 1522 | goto out; |
| 1523 | |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1524 | namelen = strlen(name); |
| 1525 | if (strchr(name, '/')) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1526 | ret = -EINVAL; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1527 | goto out_drop_write; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1528 | } |
| 1529 | |
Chris Mason | 16780ca | 2012-02-20 22:14:55 -0500 | [diff] [blame] | 1530 | if (name[0] == '.' && |
| 1531 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { |
| 1532 | ret = -EEXIST; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1533 | goto out_drop_write; |
Chris Mason | 16780ca | 2012-02-20 22:14:55 -0500 | [diff] [blame] | 1534 | } |
| 1535 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1536 | if (subvol) { |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1537 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1538 | NULL, transid, readonly, inherit); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1539 | } else { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1540 | struct fd src = fdget(fd); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1541 | struct inode *src_inode; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1542 | if (!src.file) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1543 | ret = -EINVAL; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1544 | goto out_drop_write; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1545 | } |
| 1546 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1547 | src_inode = file_inode(src.file); |
| 1548 | if (src_inode->i_sb != file_inode(file)->i_sb) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1549 | printk(KERN_INFO "btrfs: Snapshot src from " |
| 1550 | "another FS\n"); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1551 | ret = -EINVAL; |
Al Viro | ecd1881 | 2012-08-26 21:20:24 -0400 | [diff] [blame] | 1552 | } else { |
| 1553 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
| 1554 | BTRFS_I(src_inode)->root, |
| 1555 | transid, readonly, inherit); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1556 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1557 | fdput(src); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1558 | } |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1559 | out_drop_write: |
| 1560 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1561 | out: |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1562 | return ret; |
| 1563 | } |
| 1564 | |
| 1565 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1566 | void __user *arg, int subvol) |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1567 | { |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1568 | struct btrfs_ioctl_vol_args *vol_args; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1569 | int ret; |
| 1570 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1571 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1572 | if (IS_ERR(vol_args)) |
| 1573 | return PTR_ERR(vol_args); |
| 1574 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1575 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1576 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1577 | vol_args->fd, subvol, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1578 | NULL, false, NULL); |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1579 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1580 | kfree(vol_args); |
| 1581 | return ret; |
| 1582 | } |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1583 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1584 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
| 1585 | void __user *arg, int subvol) |
| 1586 | { |
| 1587 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
| 1588 | int ret; |
| 1589 | u64 transid = 0; |
| 1590 | u64 *ptr = NULL; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1591 | bool readonly = false; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1592 | struct btrfs_qgroup_inherit *inherit = NULL; |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1593 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1594 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1595 | if (IS_ERR(vol_args)) |
| 1596 | return PTR_ERR(vol_args); |
| 1597 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; |
Sage Weil | 75eaa0e | 2010-12-10 00:36:28 +0000 | [diff] [blame] | 1598 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1599 | if (vol_args->flags & |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1600 | ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | |
| 1601 | BTRFS_SUBVOL_QGROUP_INHERIT)) { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1602 | ret = -EOPNOTSUPP; |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1603 | goto out; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1604 | } |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1605 | |
| 1606 | if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) |
| 1607 | ptr = &transid; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1608 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
| 1609 | readonly = true; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1610 | if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { |
| 1611 | if (vol_args->size > PAGE_CACHE_SIZE) { |
| 1612 | ret = -EINVAL; |
| 1613 | goto out; |
| 1614 | } |
| 1615 | inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); |
| 1616 | if (IS_ERR(inherit)) { |
| 1617 | ret = PTR_ERR(inherit); |
| 1618 | goto out; |
| 1619 | } |
| 1620 | } |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1621 | |
| 1622 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1623 | vol_args->fd, subvol, ptr, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 1624 | readonly, inherit); |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1625 | |
| 1626 | if (ret == 0 && ptr && |
| 1627 | copy_to_user(arg + |
| 1628 | offsetof(struct btrfs_ioctl_vol_args_v2, |
| 1629 | transid), ptr, sizeof(*ptr))) |
| 1630 | ret = -EFAULT; |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1631 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1632 | kfree(vol_args); |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1633 | kfree(inherit); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1634 | return ret; |
| 1635 | } |
| 1636 | |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1637 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
| 1638 | void __user *arg) |
| 1639 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1640 | struct inode *inode = file_inode(file); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1641 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1642 | int ret = 0; |
| 1643 | u64 flags = 0; |
| 1644 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 1645 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1646 | return -EINVAL; |
| 1647 | |
| 1648 | down_read(&root->fs_info->subvol_sem); |
| 1649 | if (btrfs_root_readonly(root)) |
| 1650 | flags |= BTRFS_SUBVOL_RDONLY; |
| 1651 | up_read(&root->fs_info->subvol_sem); |
| 1652 | |
| 1653 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 1654 | ret = -EFAULT; |
| 1655 | |
| 1656 | return ret; |
| 1657 | } |
| 1658 | |
| 1659 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, |
| 1660 | void __user *arg) |
| 1661 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1662 | struct inode *inode = file_inode(file); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1663 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1664 | struct btrfs_trans_handle *trans; |
| 1665 | u64 root_flags; |
| 1666 | u64 flags; |
| 1667 | int ret = 0; |
| 1668 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1669 | ret = mnt_want_write_file(file); |
| 1670 | if (ret) |
| 1671 | goto out; |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1672 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1673 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { |
| 1674 | ret = -EINVAL; |
| 1675 | goto out_drop_write; |
| 1676 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1677 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1678 | if (copy_from_user(&flags, arg, sizeof(flags))) { |
| 1679 | ret = -EFAULT; |
| 1680 | goto out_drop_write; |
| 1681 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1682 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1683 | if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { |
| 1684 | ret = -EINVAL; |
| 1685 | goto out_drop_write; |
| 1686 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1687 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1688 | if (flags & ~BTRFS_SUBVOL_RDONLY) { |
| 1689 | ret = -EOPNOTSUPP; |
| 1690 | goto out_drop_write; |
| 1691 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1692 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1693 | if (!inode_owner_or_capable(inode)) { |
| 1694 | ret = -EACCES; |
| 1695 | goto out_drop_write; |
| 1696 | } |
Li Zefan | b4dc2b8 | 2011-02-16 06:06:34 +0000 | [diff] [blame] | 1697 | |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1698 | down_write(&root->fs_info->subvol_sem); |
| 1699 | |
| 1700 | /* nothing to do */ |
| 1701 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1702 | goto out_drop_sem; |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1703 | |
| 1704 | root_flags = btrfs_root_flags(&root->root_item); |
| 1705 | if (flags & BTRFS_SUBVOL_RDONLY) |
| 1706 | btrfs_set_root_flags(&root->root_item, |
| 1707 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); |
| 1708 | else |
| 1709 | btrfs_set_root_flags(&root->root_item, |
| 1710 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); |
| 1711 | |
| 1712 | trans = btrfs_start_transaction(root, 1); |
| 1713 | if (IS_ERR(trans)) { |
| 1714 | ret = PTR_ERR(trans); |
| 1715 | goto out_reset; |
| 1716 | } |
| 1717 | |
Li Zefan | b4dc2b8 | 2011-02-16 06:06:34 +0000 | [diff] [blame] | 1718 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1719 | &root->root_key, &root->root_item); |
| 1720 | |
| 1721 | btrfs_commit_transaction(trans, root); |
| 1722 | out_reset: |
| 1723 | if (ret) |
| 1724 | btrfs_set_root_flags(&root->root_item, root_flags); |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1725 | out_drop_sem: |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1726 | up_write(&root->fs_info->subvol_sem); |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1727 | out_drop_write: |
| 1728 | mnt_drop_write_file(file); |
| 1729 | out: |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1730 | return ret; |
| 1731 | } |
| 1732 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1733 | /* |
| 1734 | * helper to check if the subvolume references other subvolumes |
| 1735 | */ |
| 1736 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
| 1737 | { |
| 1738 | struct btrfs_path *path; |
Josef Bacik | 175a2b8 | 2013-08-12 15:36:44 -0400 | [diff] [blame] | 1739 | struct btrfs_dir_item *di; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1740 | struct btrfs_key key; |
Josef Bacik | 175a2b8 | 2013-08-12 15:36:44 -0400 | [diff] [blame] | 1741 | u64 dir_id; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1742 | int ret; |
| 1743 | |
| 1744 | path = btrfs_alloc_path(); |
| 1745 | if (!path) |
| 1746 | return -ENOMEM; |
| 1747 | |
Josef Bacik | 175a2b8 | 2013-08-12 15:36:44 -0400 | [diff] [blame] | 1748 | /* Make sure this root isn't set as the default subvol */ |
| 1749 | dir_id = btrfs_super_root_dir(root->fs_info->super_copy); |
| 1750 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path, |
| 1751 | dir_id, "default", 7, 0); |
| 1752 | if (di && !IS_ERR(di)) { |
| 1753 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 1754 | if (key.objectid == root->root_key.objectid) { |
| 1755 | ret = -ENOTEMPTY; |
| 1756 | goto out; |
| 1757 | } |
| 1758 | btrfs_release_path(path); |
| 1759 | } |
| 1760 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1761 | key.objectid = root->root_key.objectid; |
| 1762 | key.type = BTRFS_ROOT_REF_KEY; |
| 1763 | key.offset = (u64)-1; |
| 1764 | |
| 1765 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, |
| 1766 | &key, path, 0, 0); |
| 1767 | if (ret < 0) |
| 1768 | goto out; |
| 1769 | BUG_ON(ret == 0); |
| 1770 | |
| 1771 | ret = 0; |
| 1772 | if (path->slots[0] > 0) { |
| 1773 | path->slots[0]--; |
| 1774 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 1775 | if (key.objectid == root->root_key.objectid && |
| 1776 | key.type == BTRFS_ROOT_REF_KEY) |
| 1777 | ret = -ENOTEMPTY; |
| 1778 | } |
| 1779 | out: |
| 1780 | btrfs_free_path(path); |
| 1781 | return ret; |
| 1782 | } |
| 1783 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1784 | static noinline int key_in_sk(struct btrfs_key *key, |
| 1785 | struct btrfs_ioctl_search_key *sk) |
| 1786 | { |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 1787 | struct btrfs_key test; |
| 1788 | int ret; |
| 1789 | |
| 1790 | test.objectid = sk->min_objectid; |
| 1791 | test.type = sk->min_type; |
| 1792 | test.offset = sk->min_offset; |
| 1793 | |
| 1794 | ret = btrfs_comp_cpu_keys(key, &test); |
| 1795 | if (ret < 0) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1796 | return 0; |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 1797 | |
| 1798 | test.objectid = sk->max_objectid; |
| 1799 | test.type = sk->max_type; |
| 1800 | test.offset = sk->max_offset; |
| 1801 | |
| 1802 | ret = btrfs_comp_cpu_keys(key, &test); |
| 1803 | if (ret > 0) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1804 | return 0; |
| 1805 | return 1; |
| 1806 | } |
| 1807 | |
| 1808 | static noinline int copy_to_sk(struct btrfs_root *root, |
| 1809 | struct btrfs_path *path, |
| 1810 | struct btrfs_key *key, |
| 1811 | struct btrfs_ioctl_search_key *sk, |
| 1812 | char *buf, |
| 1813 | unsigned long *sk_offset, |
| 1814 | int *num_found) |
| 1815 | { |
| 1816 | u64 found_transid; |
| 1817 | struct extent_buffer *leaf; |
| 1818 | struct btrfs_ioctl_search_header sh; |
| 1819 | unsigned long item_off; |
| 1820 | unsigned long item_len; |
| 1821 | int nritems; |
| 1822 | int i; |
| 1823 | int slot; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1824 | int ret = 0; |
| 1825 | |
| 1826 | leaf = path->nodes[0]; |
| 1827 | slot = path->slots[0]; |
| 1828 | nritems = btrfs_header_nritems(leaf); |
| 1829 | |
| 1830 | if (btrfs_header_generation(leaf) > sk->max_transid) { |
| 1831 | i = nritems; |
| 1832 | goto advance_key; |
| 1833 | } |
| 1834 | found_transid = btrfs_header_generation(leaf); |
| 1835 | |
| 1836 | for (i = slot; i < nritems; i++) { |
| 1837 | item_off = btrfs_item_ptr_offset(leaf, i); |
| 1838 | item_len = btrfs_item_size_nr(leaf, i); |
| 1839 | |
Gabriel de Perthuis | 03b71c6 | 2013-05-06 17:40:18 +0000 | [diff] [blame] | 1840 | btrfs_item_key_to_cpu(leaf, key, i); |
| 1841 | if (!key_in_sk(key, sk)) |
| 1842 | continue; |
| 1843 | |
| 1844 | if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1845 | item_len = 0; |
| 1846 | |
| 1847 | if (sizeof(sh) + item_len + *sk_offset > |
| 1848 | BTRFS_SEARCH_ARGS_BUFSIZE) { |
| 1849 | ret = 1; |
| 1850 | goto overflow; |
| 1851 | } |
| 1852 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1853 | sh.objectid = key->objectid; |
| 1854 | sh.offset = key->offset; |
| 1855 | sh.type = key->type; |
| 1856 | sh.len = item_len; |
| 1857 | sh.transid = found_transid; |
| 1858 | |
| 1859 | /* copy search result header */ |
| 1860 | memcpy(buf + *sk_offset, &sh, sizeof(sh)); |
| 1861 | *sk_offset += sizeof(sh); |
| 1862 | |
| 1863 | if (item_len) { |
| 1864 | char *p = buf + *sk_offset; |
| 1865 | /* copy the item */ |
| 1866 | read_extent_buffer(leaf, p, |
| 1867 | item_off, item_len); |
| 1868 | *sk_offset += item_len; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1869 | } |
Hugo Mills | e215686 | 2011-05-14 17:43:41 +0000 | [diff] [blame] | 1870 | (*num_found)++; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1871 | |
| 1872 | if (*num_found >= sk->nr_items) |
| 1873 | break; |
| 1874 | } |
| 1875 | advance_key: |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1876 | ret = 0; |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 1877 | if (key->offset < (u64)-1 && key->offset < sk->max_offset) |
| 1878 | key->offset++; |
| 1879 | else if (key->type < (u8)-1 && key->type < sk->max_type) { |
| 1880 | key->offset = 0; |
| 1881 | key->type++; |
| 1882 | } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { |
| 1883 | key->offset = 0; |
| 1884 | key->type = 0; |
| 1885 | key->objectid++; |
| 1886 | } else |
| 1887 | ret = 1; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1888 | overflow: |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1889 | return ret; |
| 1890 | } |
| 1891 | |
| 1892 | static noinline int search_ioctl(struct inode *inode, |
| 1893 | struct btrfs_ioctl_search_args *args) |
| 1894 | { |
| 1895 | struct btrfs_root *root; |
| 1896 | struct btrfs_key key; |
| 1897 | struct btrfs_key max_key; |
| 1898 | struct btrfs_path *path; |
| 1899 | struct btrfs_ioctl_search_key *sk = &args->key; |
| 1900 | struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; |
| 1901 | int ret; |
| 1902 | int num_found = 0; |
| 1903 | unsigned long sk_offset = 0; |
| 1904 | |
| 1905 | path = btrfs_alloc_path(); |
| 1906 | if (!path) |
| 1907 | return -ENOMEM; |
| 1908 | |
| 1909 | if (sk->tree_id == 0) { |
| 1910 | /* search the root of the inode that was passed */ |
| 1911 | root = BTRFS_I(inode)->root; |
| 1912 | } else { |
| 1913 | key.objectid = sk->tree_id; |
| 1914 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 1915 | key.offset = (u64)-1; |
| 1916 | root = btrfs_read_fs_root_no_name(info, &key); |
| 1917 | if (IS_ERR(root)) { |
| 1918 | printk(KERN_ERR "could not find root %llu\n", |
| 1919 | sk->tree_id); |
| 1920 | btrfs_free_path(path); |
| 1921 | return -ENOENT; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | key.objectid = sk->min_objectid; |
| 1926 | key.type = sk->min_type; |
| 1927 | key.offset = sk->min_offset; |
| 1928 | |
| 1929 | max_key.objectid = sk->max_objectid; |
| 1930 | max_key.type = sk->max_type; |
| 1931 | max_key.offset = sk->max_offset; |
| 1932 | |
| 1933 | path->keep_locks = 1; |
| 1934 | |
| 1935 | while(1) { |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 1936 | ret = btrfs_search_forward(root, &key, &max_key, path, |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1937 | sk->min_transid); |
| 1938 | if (ret != 0) { |
| 1939 | if (ret > 0) |
| 1940 | ret = 0; |
| 1941 | goto err; |
| 1942 | } |
| 1943 | ret = copy_to_sk(root, path, &key, sk, args->buf, |
| 1944 | &sk_offset, &num_found); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1945 | btrfs_release_path(path); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1946 | if (ret || num_found >= sk->nr_items) |
| 1947 | break; |
| 1948 | |
| 1949 | } |
| 1950 | ret = 0; |
| 1951 | err: |
| 1952 | sk->nr_items = num_found; |
| 1953 | btrfs_free_path(path); |
| 1954 | return ret; |
| 1955 | } |
| 1956 | |
| 1957 | static noinline int btrfs_ioctl_tree_search(struct file *file, |
| 1958 | void __user *argp) |
| 1959 | { |
| 1960 | struct btrfs_ioctl_search_args *args; |
| 1961 | struct inode *inode; |
| 1962 | int ret; |
| 1963 | |
| 1964 | if (!capable(CAP_SYS_ADMIN)) |
| 1965 | return -EPERM; |
| 1966 | |
Julia Lawall | 2354d08 | 2010-10-29 15:14:18 -0400 | [diff] [blame] | 1967 | args = memdup_user(argp, sizeof(*args)); |
| 1968 | if (IS_ERR(args)) |
| 1969 | return PTR_ERR(args); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1970 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1971 | inode = file_inode(file); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1972 | ret = search_ioctl(inode, args); |
| 1973 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 1974 | ret = -EFAULT; |
| 1975 | kfree(args); |
| 1976 | return ret; |
| 1977 | } |
| 1978 | |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1979 | /* |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1980 | * Search INODE_REFs to identify path name of 'dirid' directory |
| 1981 | * in a 'tree_id' tree. and sets path name to 'name'. |
| 1982 | */ |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1983 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
| 1984 | u64 tree_id, u64 dirid, char *name) |
| 1985 | { |
| 1986 | struct btrfs_root *root; |
| 1987 | struct btrfs_key key; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1988 | char *ptr; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1989 | int ret = -1; |
| 1990 | int slot; |
| 1991 | int len; |
| 1992 | int total_len = 0; |
| 1993 | struct btrfs_inode_ref *iref; |
| 1994 | struct extent_buffer *l; |
| 1995 | struct btrfs_path *path; |
| 1996 | |
| 1997 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { |
| 1998 | name[0]='\0'; |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
| 2002 | path = btrfs_alloc_path(); |
| 2003 | if (!path) |
| 2004 | return -ENOMEM; |
| 2005 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2006 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2007 | |
| 2008 | key.objectid = tree_id; |
| 2009 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 2010 | key.offset = (u64)-1; |
| 2011 | root = btrfs_read_fs_root_no_name(info, &key); |
| 2012 | if (IS_ERR(root)) { |
| 2013 | printk(KERN_ERR "could not find root %llu\n", tree_id); |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 2014 | ret = -ENOENT; |
| 2015 | goto out; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | key.objectid = dirid; |
| 2019 | key.type = BTRFS_INODE_REF_KEY; |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 2020 | key.offset = (u64)-1; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2021 | |
| 2022 | while(1) { |
| 2023 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2024 | if (ret < 0) |
| 2025 | goto out; |
Filipe David Borba Manana | 18674c6 | 2013-08-14 03:00:21 +0100 | [diff] [blame] | 2026 | else if (ret > 0) { |
| 2027 | ret = btrfs_previous_item(root, path, dirid, |
| 2028 | BTRFS_INODE_REF_KEY); |
| 2029 | if (ret < 0) |
| 2030 | goto out; |
| 2031 | else if (ret > 0) { |
| 2032 | ret = -ENOENT; |
| 2033 | goto out; |
| 2034 | } |
| 2035 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2036 | |
| 2037 | l = path->nodes[0]; |
| 2038 | slot = path->slots[0]; |
| 2039 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2040 | |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2041 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); |
| 2042 | len = btrfs_inode_ref_name_len(l, iref); |
| 2043 | ptr -= len + 1; |
| 2044 | total_len += len + 1; |
Filipe David Borba Manana | a696cf3 | 2013-08-14 03:00:20 +0100 | [diff] [blame] | 2045 | if (ptr < name) { |
| 2046 | ret = -ENAMETOOLONG; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2047 | goto out; |
Filipe David Borba Manana | a696cf3 | 2013-08-14 03:00:20 +0100 | [diff] [blame] | 2048 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2049 | |
| 2050 | *(ptr + len) = '/'; |
| 2051 | read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); |
| 2052 | |
| 2053 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) |
| 2054 | break; |
| 2055 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2056 | btrfs_release_path(path); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2057 | key.objectid = key.offset; |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 2058 | key.offset = (u64)-1; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2059 | dirid = key.objectid; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2060 | } |
Li Zefan | 77906a50 | 2011-07-14 03:16:00 +0000 | [diff] [blame] | 2061 | memmove(name, ptr, total_len); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2062 | name[total_len]='\0'; |
| 2063 | ret = 0; |
| 2064 | out: |
| 2065 | btrfs_free_path(path); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2066 | return ret; |
| 2067 | } |
| 2068 | |
| 2069 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, |
| 2070 | void __user *argp) |
| 2071 | { |
| 2072 | struct btrfs_ioctl_ino_lookup_args *args; |
| 2073 | struct inode *inode; |
| 2074 | int ret; |
| 2075 | |
| 2076 | if (!capable(CAP_SYS_ADMIN)) |
| 2077 | return -EPERM; |
| 2078 | |
Julia Lawall | 2354d08 | 2010-10-29 15:14:18 -0400 | [diff] [blame] | 2079 | args = memdup_user(argp, sizeof(*args)); |
| 2080 | if (IS_ERR(args)) |
| 2081 | return PTR_ERR(args); |
Dan Carpenter | c2b9692 | 2010-03-20 11:24:15 +0000 | [diff] [blame] | 2082 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2083 | inode = file_inode(file); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2084 | |
Chris Mason | 1b53ac4 | 2010-03-18 12:17:05 -0400 | [diff] [blame] | 2085 | if (args->treeid == 0) |
| 2086 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; |
| 2087 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2088 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
| 2089 | args->treeid, args->objectid, |
| 2090 | args->name); |
| 2091 | |
| 2092 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 2093 | ret = -EFAULT; |
| 2094 | |
| 2095 | kfree(args); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2096 | return ret; |
| 2097 | } |
| 2098 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2099 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
| 2100 | void __user *arg) |
| 2101 | { |
| 2102 | struct dentry *parent = fdentry(file); |
| 2103 | struct dentry *dentry; |
| 2104 | struct inode *dir = parent->d_inode; |
| 2105 | struct inode *inode; |
| 2106 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2107 | struct btrfs_root *dest = NULL; |
| 2108 | struct btrfs_ioctl_vol_args *vol_args; |
| 2109 | struct btrfs_trans_handle *trans; |
Miao Xie | c58aaad | 2013-02-28 10:05:36 +0000 | [diff] [blame] | 2110 | struct btrfs_block_rsv block_rsv; |
| 2111 | u64 qgroup_reserved; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2112 | int namelen; |
| 2113 | int ret; |
| 2114 | int err = 0; |
| 2115 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2116 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 2117 | if (IS_ERR(vol_args)) |
| 2118 | return PTR_ERR(vol_args); |
| 2119 | |
| 2120 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
| 2121 | namelen = strlen(vol_args->name); |
| 2122 | if (strchr(vol_args->name, '/') || |
| 2123 | strncmp(vol_args->name, "..", namelen) == 0) { |
| 2124 | err = -EINVAL; |
| 2125 | goto out; |
| 2126 | } |
| 2127 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 2128 | err = mnt_want_write_file(file); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2129 | if (err) |
| 2130 | goto out; |
| 2131 | |
David Sterba | 5c50c9b | 2013-03-22 18:12:51 +0000 | [diff] [blame] | 2132 | err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); |
| 2133 | if (err == -EINTR) |
| 2134 | goto out; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2135 | dentry = lookup_one_len(vol_args->name, parent, namelen); |
| 2136 | if (IS_ERR(dentry)) { |
| 2137 | err = PTR_ERR(dentry); |
| 2138 | goto out_unlock_dir; |
| 2139 | } |
| 2140 | |
| 2141 | if (!dentry->d_inode) { |
| 2142 | err = -ENOENT; |
| 2143 | goto out_dput; |
| 2144 | } |
| 2145 | |
| 2146 | inode = dentry->d_inode; |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 2147 | dest = BTRFS_I(inode)->root; |
| 2148 | if (!capable(CAP_SYS_ADMIN)){ |
| 2149 | /* |
| 2150 | * Regular user. Only allow this with a special mount |
| 2151 | * option, when the user has write+exec access to the |
| 2152 | * subvol root, and when rmdir(2) would have been |
| 2153 | * allowed. |
| 2154 | * |
| 2155 | * Note that this is _not_ check that the subvol is |
| 2156 | * empty or doesn't contain data that we wouldn't |
| 2157 | * otherwise be able to delete. |
| 2158 | * |
| 2159 | * Users who want to delete empty subvols should try |
| 2160 | * rmdir(2). |
| 2161 | */ |
| 2162 | err = -EPERM; |
| 2163 | if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) |
| 2164 | goto out_dput; |
| 2165 | |
| 2166 | /* |
| 2167 | * Do not allow deletion if the parent dir is the same |
| 2168 | * as the dir to be deleted. That means the ioctl |
| 2169 | * must be called on the dentry referencing the root |
| 2170 | * of the subvol, not a random directory contained |
| 2171 | * within it. |
| 2172 | */ |
| 2173 | err = -EINVAL; |
| 2174 | if (root == dest) |
| 2175 | goto out_dput; |
| 2176 | |
| 2177 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); |
| 2178 | if (err) |
| 2179 | goto out_dput; |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 2180 | } |
| 2181 | |
Miao Xie | 5c39da5 | 2012-10-22 11:39:53 +0000 | [diff] [blame] | 2182 | /* check if subvolume may be deleted by a user */ |
| 2183 | err = btrfs_may_delete(dir, dentry, 1); |
| 2184 | if (err) |
| 2185 | goto out_dput; |
| 2186 | |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2187 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2188 | err = -EINVAL; |
| 2189 | goto out_dput; |
| 2190 | } |
| 2191 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2192 | mutex_lock(&inode->i_mutex); |
| 2193 | err = d_invalidate(dentry); |
| 2194 | if (err) |
| 2195 | goto out_unlock; |
| 2196 | |
| 2197 | down_write(&root->fs_info->subvol_sem); |
| 2198 | |
| 2199 | err = may_destroy_subvol(dest); |
| 2200 | if (err) |
| 2201 | goto out_up_write; |
| 2202 | |
Miao Xie | c58aaad | 2013-02-28 10:05:36 +0000 | [diff] [blame] | 2203 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 2204 | /* |
| 2205 | * One for dir inode, two for dir entries, two for root |
| 2206 | * ref/backref. |
| 2207 | */ |
| 2208 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 2209 | 5, &qgroup_reserved, true); |
Miao Xie | c58aaad | 2013-02-28 10:05:36 +0000 | [diff] [blame] | 2210 | if (err) |
| 2211 | goto out_up_write; |
| 2212 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2213 | trans = btrfs_start_transaction(root, 0); |
| 2214 | if (IS_ERR(trans)) { |
| 2215 | err = PTR_ERR(trans); |
Miao Xie | c58aaad | 2013-02-28 10:05:36 +0000 | [diff] [blame] | 2216 | goto out_release; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2217 | } |
Miao Xie | c58aaad | 2013-02-28 10:05:36 +0000 | [diff] [blame] | 2218 | trans->block_rsv = &block_rsv; |
| 2219 | trans->bytes_reserved = block_rsv.size; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2220 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2221 | ret = btrfs_unlink_subvol(trans, root, dir, |
| 2222 | dest->root_key.objectid, |
| 2223 | dentry->d_name.name, |
| 2224 | dentry->d_name.len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2225 | if (ret) { |
| 2226 | err = ret; |
| 2227 | btrfs_abort_transaction(trans, root, ret); |
| 2228 | goto out_end_trans; |
| 2229 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2230 | |
| 2231 | btrfs_record_root_in_trans(trans, dest); |
| 2232 | |
| 2233 | memset(&dest->root_item.drop_progress, 0, |
| 2234 | sizeof(dest->root_item.drop_progress)); |
| 2235 | dest->root_item.drop_level = 0; |
| 2236 | btrfs_set_root_refs(&dest->root_item, 0); |
| 2237 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2238 | if (!xchg(&dest->orphan_item_inserted, 1)) { |
| 2239 | ret = btrfs_insert_orphan_item(trans, |
| 2240 | root->fs_info->tree_root, |
| 2241 | dest->root_key.objectid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2242 | if (ret) { |
| 2243 | btrfs_abort_transaction(trans, root, ret); |
| 2244 | err = ret; |
| 2245 | goto out_end_trans; |
| 2246 | } |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2247 | } |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 2248 | |
| 2249 | ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, |
| 2250 | dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL, |
| 2251 | dest->root_key.objectid); |
| 2252 | if (ret && ret != -ENOENT) { |
| 2253 | btrfs_abort_transaction(trans, root, ret); |
| 2254 | err = ret; |
| 2255 | goto out_end_trans; |
| 2256 | } |
| 2257 | if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { |
| 2258 | ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, |
| 2259 | dest->root_item.received_uuid, |
| 2260 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 2261 | dest->root_key.objectid); |
| 2262 | if (ret && ret != -ENOENT) { |
| 2263 | btrfs_abort_transaction(trans, root, ret); |
| 2264 | err = ret; |
| 2265 | goto out_end_trans; |
| 2266 | } |
| 2267 | } |
| 2268 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2269 | out_end_trans: |
Miao Xie | c58aaad | 2013-02-28 10:05:36 +0000 | [diff] [blame] | 2270 | trans->block_rsv = NULL; |
| 2271 | trans->bytes_reserved = 0; |
Sage Weil | 531cb13 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 2272 | ret = btrfs_end_transaction(trans, root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2273 | if (ret && !err) |
| 2274 | err = ret; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2275 | inode->i_flags |= S_DEAD; |
Miao Xie | c58aaad | 2013-02-28 10:05:36 +0000 | [diff] [blame] | 2276 | out_release: |
| 2277 | btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2278 | out_up_write: |
| 2279 | up_write(&root->fs_info->subvol_sem); |
| 2280 | out_unlock: |
| 2281 | mutex_unlock(&inode->i_mutex); |
| 2282 | if (!err) { |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 2283 | shrink_dcache_sb(root->fs_info->sb); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2284 | btrfs_invalidate_inodes(dest); |
| 2285 | d_delete(dentry); |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 2286 | |
| 2287 | /* the last ref */ |
| 2288 | if (dest->cache_inode) { |
| 2289 | iput(dest->cache_inode); |
| 2290 | dest->cache_inode = NULL; |
| 2291 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2292 | } |
| 2293 | out_dput: |
| 2294 | dput(dentry); |
| 2295 | out_unlock_dir: |
| 2296 | mutex_unlock(&dir->i_mutex); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 2297 | mnt_drop_write_file(file); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2298 | out: |
| 2299 | kfree(vol_args); |
| 2300 | return err; |
| 2301 | } |
| 2302 | |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 2303 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2304 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2305 | struct inode *inode = file_inode(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2306 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 2307 | struct btrfs_ioctl_defrag_range_args *range; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2308 | int ret; |
| 2309 | |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 2310 | ret = mnt_want_write_file(file); |
| 2311 | if (ret) |
| 2312 | return ret; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 2313 | |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 2314 | if (btrfs_root_readonly(root)) { |
| 2315 | ret = -EROFS; |
| 2316 | goto out; |
Stefan Behrens | 5ac00ad | 2012-11-05 17:54:08 +0100 | [diff] [blame] | 2317 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2318 | |
| 2319 | switch (inode->i_mode & S_IFMT) { |
| 2320 | case S_IFDIR: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 2321 | if (!capable(CAP_SYS_ADMIN)) { |
| 2322 | ret = -EPERM; |
| 2323 | goto out; |
| 2324 | } |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 2325 | ret = btrfs_defrag_root(root); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2326 | if (ret) |
| 2327 | goto out; |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 2328 | ret = btrfs_defrag_root(root->fs_info->extent_root); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2329 | break; |
| 2330 | case S_IFREG: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 2331 | if (!(file->f_mode & FMODE_WRITE)) { |
| 2332 | ret = -EINVAL; |
| 2333 | goto out; |
| 2334 | } |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 2335 | |
| 2336 | range = kzalloc(sizeof(*range), GFP_KERNEL); |
| 2337 | if (!range) { |
| 2338 | ret = -ENOMEM; |
| 2339 | goto out; |
| 2340 | } |
| 2341 | |
| 2342 | if (argp) { |
| 2343 | if (copy_from_user(range, argp, |
| 2344 | sizeof(*range))) { |
| 2345 | ret = -EFAULT; |
| 2346 | kfree(range); |
Dan Carpenter | 683be16 | 2010-03-20 11:24:48 +0000 | [diff] [blame] | 2347 | goto out; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 2348 | } |
| 2349 | /* compression requires us to start the IO */ |
| 2350 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { |
| 2351 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; |
| 2352 | range->extent_thresh = (u32)-1; |
| 2353 | } |
| 2354 | } else { |
| 2355 | /* the rest are all set to zero by kzalloc */ |
| 2356 | range->len = (u64)-1; |
| 2357 | } |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2358 | ret = btrfs_defrag_file(file_inode(file), file, |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 2359 | range, 0, 0); |
| 2360 | if (ret > 0) |
| 2361 | ret = 0; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 2362 | kfree(range); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2363 | break; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2364 | default: |
| 2365 | ret = -EINVAL; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2366 | } |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 2367 | out: |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 2368 | mnt_drop_write_file(file); |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 2369 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2370 | } |
| 2371 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2372 | static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2373 | { |
| 2374 | struct btrfs_ioctl_vol_args *vol_args; |
| 2375 | int ret; |
| 2376 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 2377 | if (!capable(CAP_SYS_ADMIN)) |
| 2378 | return -EPERM; |
| 2379 | |
Stefan Behrens | 5ac00ad | 2012-11-05 17:54:08 +0100 | [diff] [blame] | 2380 | if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, |
| 2381 | 1)) { |
| 2382 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); |
Ilya Dryomov | 2c0c9da | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 2383 | return -EINVAL; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2384 | } |
| 2385 | |
Stefan Behrens | 5ac00ad | 2012-11-05 17:54:08 +0100 | [diff] [blame] | 2386 | mutex_lock(&root->fs_info->volume_mutex); |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 2387 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2388 | if (IS_ERR(vol_args)) { |
| 2389 | ret = PTR_ERR(vol_args); |
| 2390 | goto out; |
| 2391 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2392 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 2393 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2394 | ret = btrfs_init_new_device(root, vol_args->name); |
| 2395 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2396 | kfree(vol_args); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2397 | out: |
| 2398 | mutex_unlock(&root->fs_info->volume_mutex); |
Stefan Behrens | 5ac00ad | 2012-11-05 17:54:08 +0100 | [diff] [blame] | 2399 | atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2400 | return ret; |
| 2401 | } |
| 2402 | |
Miao Xie | da24927 | 2012-11-26 08:44:50 +0000 | [diff] [blame] | 2403 | static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2404 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2405 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2406 | struct btrfs_ioctl_vol_args *vol_args; |
| 2407 | int ret; |
| 2408 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 2409 | if (!capable(CAP_SYS_ADMIN)) |
| 2410 | return -EPERM; |
| 2411 | |
Miao Xie | da24927 | 2012-11-26 08:44:50 +0000 | [diff] [blame] | 2412 | ret = mnt_want_write_file(file); |
| 2413 | if (ret) |
| 2414 | return ret; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2415 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 2416 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2417 | if (IS_ERR(vol_args)) { |
| 2418 | ret = PTR_ERR(vol_args); |
| 2419 | goto out; |
| 2420 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2421 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 2422 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2423 | |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2424 | if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, |
| 2425 | 1)) { |
| 2426 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
| 2427 | goto out; |
| 2428 | } |
| 2429 | |
| 2430 | mutex_lock(&root->fs_info->volume_mutex); |
| 2431 | ret = btrfs_rm_device(root, vol_args->name); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2432 | mutex_unlock(&root->fs_info->volume_mutex); |
Stefan Behrens | 5ac00ad | 2012-11-05 17:54:08 +0100 | [diff] [blame] | 2433 | atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2434 | |
| 2435 | out: |
| 2436 | kfree(vol_args); |
Ilya Dryomov | 4ac20c7 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 2437 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2438 | return ret; |
| 2439 | } |
| 2440 | |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2441 | static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) |
| 2442 | { |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 2443 | struct btrfs_ioctl_fs_info_args *fi_args; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2444 | struct btrfs_device *device; |
| 2445 | struct btrfs_device *next; |
| 2446 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 2447 | int ret = 0; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2448 | |
| 2449 | if (!capable(CAP_SYS_ADMIN)) |
| 2450 | return -EPERM; |
| 2451 | |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 2452 | fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); |
| 2453 | if (!fi_args) |
| 2454 | return -ENOMEM; |
| 2455 | |
| 2456 | fi_args->num_devices = fs_devices->num_devices; |
| 2457 | memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2458 | |
| 2459 | mutex_lock(&fs_devices->device_list_mutex); |
| 2460 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 2461 | if (device->devid > fi_args->max_id) |
| 2462 | fi_args->max_id = device->devid; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2463 | } |
| 2464 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2465 | |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 2466 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) |
| 2467 | ret = -EFAULT; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2468 | |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 2469 | kfree(fi_args); |
| 2470 | return ret; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) |
| 2474 | { |
| 2475 | struct btrfs_ioctl_dev_info_args *di_args; |
| 2476 | struct btrfs_device *dev; |
| 2477 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 2478 | int ret = 0; |
| 2479 | char *s_uuid = NULL; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2480 | |
| 2481 | if (!capable(CAP_SYS_ADMIN)) |
| 2482 | return -EPERM; |
| 2483 | |
| 2484 | di_args = memdup_user(arg, sizeof(*di_args)); |
| 2485 | if (IS_ERR(di_args)) |
| 2486 | return PTR_ERR(di_args); |
| 2487 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 2488 | if (!btrfs_is_empty_uuid(di_args->uuid)) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2489 | s_uuid = di_args->uuid; |
| 2490 | |
| 2491 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2492 | dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2493 | |
| 2494 | if (!dev) { |
| 2495 | ret = -ENODEV; |
| 2496 | goto out; |
| 2497 | } |
| 2498 | |
| 2499 | di_args->devid = dev->devid; |
| 2500 | di_args->bytes_used = dev->bytes_used; |
| 2501 | di_args->total_bytes = dev->total_bytes; |
| 2502 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 2503 | if (dev->name) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2504 | struct rcu_string *name; |
| 2505 | |
| 2506 | rcu_read_lock(); |
| 2507 | name = rcu_dereference(dev->name); |
| 2508 | strncpy(di_args->path, name->str, sizeof(di_args->path)); |
| 2509 | rcu_read_unlock(); |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 2510 | di_args->path[sizeof(di_args->path) - 1] = 0; |
| 2511 | } else { |
Stefan Behrens | 99ba55a | 2012-03-19 16:17:22 +0100 | [diff] [blame] | 2512 | di_args->path[0] = '\0'; |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 2513 | } |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2514 | |
| 2515 | out: |
David Sterba | 55793c0 | 2013-04-26 15:20:23 +0000 | [diff] [blame] | 2516 | mutex_unlock(&fs_devices->device_list_mutex); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 2517 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) |
| 2518 | ret = -EFAULT; |
| 2519 | |
| 2520 | kfree(di_args); |
| 2521 | return ret; |
| 2522 | } |
| 2523 | |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 2524 | static struct page *extent_same_get_page(struct inode *inode, u64 off) |
| 2525 | { |
| 2526 | struct page *page; |
| 2527 | pgoff_t index; |
| 2528 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2529 | |
| 2530 | index = off >> PAGE_CACHE_SHIFT; |
| 2531 | |
| 2532 | page = grab_cache_page(inode->i_mapping, index); |
| 2533 | if (!page) |
| 2534 | return NULL; |
| 2535 | |
| 2536 | if (!PageUptodate(page)) { |
| 2537 | if (extent_read_full_page_nolock(tree, page, btrfs_get_extent, |
| 2538 | 0)) |
| 2539 | return NULL; |
| 2540 | lock_page(page); |
| 2541 | if (!PageUptodate(page)) { |
| 2542 | unlock_page(page); |
| 2543 | page_cache_release(page); |
| 2544 | return NULL; |
| 2545 | } |
| 2546 | } |
| 2547 | unlock_page(page); |
| 2548 | |
| 2549 | return page; |
| 2550 | } |
| 2551 | |
Mark Fasheh | 77fe20dc | 2013-08-06 11:42:48 -0700 | [diff] [blame] | 2552 | static inline void lock_extent_range(struct inode *inode, u64 off, u64 len) |
| 2553 | { |
| 2554 | /* do any pending delalloc/csum calc on src, one way or |
| 2555 | another, and lock file content */ |
| 2556 | while (1) { |
| 2557 | struct btrfs_ordered_extent *ordered; |
| 2558 | lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); |
| 2559 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 2560 | off + len - 1); |
| 2561 | if (!ordered && |
| 2562 | !test_range_bit(&BTRFS_I(inode)->io_tree, off, |
| 2563 | off + len - 1, EXTENT_DELALLOC, 0, NULL)) |
| 2564 | break; |
| 2565 | unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); |
| 2566 | if (ordered) |
| 2567 | btrfs_put_ordered_extent(ordered); |
| 2568 | btrfs_wait_ordered_range(inode, off, len); |
| 2569 | } |
| 2570 | } |
| 2571 | |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 2572 | static void btrfs_double_unlock(struct inode *inode1, u64 loff1, |
| 2573 | struct inode *inode2, u64 loff2, u64 len) |
| 2574 | { |
| 2575 | unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); |
| 2576 | unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); |
| 2577 | |
| 2578 | mutex_unlock(&inode1->i_mutex); |
| 2579 | mutex_unlock(&inode2->i_mutex); |
| 2580 | } |
| 2581 | |
| 2582 | static void btrfs_double_lock(struct inode *inode1, u64 loff1, |
| 2583 | struct inode *inode2, u64 loff2, u64 len) |
| 2584 | { |
| 2585 | if (inode1 < inode2) { |
| 2586 | swap(inode1, inode2); |
| 2587 | swap(loff1, loff2); |
| 2588 | } |
| 2589 | |
| 2590 | mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT); |
| 2591 | lock_extent_range(inode1, loff1, len); |
| 2592 | if (inode1 != inode2) { |
| 2593 | mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD); |
| 2594 | lock_extent_range(inode2, loff2, len); |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst, |
| 2599 | u64 dst_loff, u64 len) |
| 2600 | { |
| 2601 | int ret = 0; |
| 2602 | struct page *src_page, *dst_page; |
| 2603 | unsigned int cmp_len = PAGE_CACHE_SIZE; |
| 2604 | void *addr, *dst_addr; |
| 2605 | |
| 2606 | while (len) { |
| 2607 | if (len < PAGE_CACHE_SIZE) |
| 2608 | cmp_len = len; |
| 2609 | |
| 2610 | src_page = extent_same_get_page(src, loff); |
| 2611 | if (!src_page) |
| 2612 | return -EINVAL; |
| 2613 | dst_page = extent_same_get_page(dst, dst_loff); |
| 2614 | if (!dst_page) { |
| 2615 | page_cache_release(src_page); |
| 2616 | return -EINVAL; |
| 2617 | } |
| 2618 | addr = kmap_atomic(src_page); |
| 2619 | dst_addr = kmap_atomic(dst_page); |
| 2620 | |
| 2621 | flush_dcache_page(src_page); |
| 2622 | flush_dcache_page(dst_page); |
| 2623 | |
| 2624 | if (memcmp(addr, dst_addr, cmp_len)) |
| 2625 | ret = BTRFS_SAME_DATA_DIFFERS; |
| 2626 | |
| 2627 | kunmap_atomic(addr); |
| 2628 | kunmap_atomic(dst_addr); |
| 2629 | page_cache_release(src_page); |
| 2630 | page_cache_release(dst_page); |
| 2631 | |
| 2632 | if (ret) |
| 2633 | break; |
| 2634 | |
| 2635 | loff += cmp_len; |
| 2636 | dst_loff += cmp_len; |
| 2637 | len -= cmp_len; |
| 2638 | } |
| 2639 | |
| 2640 | return ret; |
| 2641 | } |
| 2642 | |
| 2643 | static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len) |
| 2644 | { |
| 2645 | u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; |
| 2646 | |
| 2647 | if (off + len > inode->i_size || off + len < off) |
| 2648 | return -EINVAL; |
| 2649 | /* Check that we are block aligned - btrfs_clone() requires this */ |
| 2650 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) |
| 2651 | return -EINVAL; |
| 2652 | |
| 2653 | return 0; |
| 2654 | } |
| 2655 | |
| 2656 | static int btrfs_extent_same(struct inode *src, u64 loff, u64 len, |
| 2657 | struct inode *dst, u64 dst_loff) |
| 2658 | { |
| 2659 | int ret; |
| 2660 | |
| 2661 | /* |
| 2662 | * btrfs_clone() can't handle extents in the same file |
| 2663 | * yet. Once that works, we can drop this check and replace it |
| 2664 | * with a check for the same inode, but overlapping extents. |
| 2665 | */ |
| 2666 | if (src == dst) |
| 2667 | return -EINVAL; |
| 2668 | |
| 2669 | btrfs_double_lock(src, loff, dst, dst_loff, len); |
| 2670 | |
| 2671 | ret = extent_same_check_offsets(src, loff, len); |
| 2672 | if (ret) |
| 2673 | goto out_unlock; |
| 2674 | |
| 2675 | ret = extent_same_check_offsets(dst, dst_loff, len); |
| 2676 | if (ret) |
| 2677 | goto out_unlock; |
| 2678 | |
| 2679 | /* don't make the dst file partly checksummed */ |
| 2680 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != |
| 2681 | (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { |
| 2682 | ret = -EINVAL; |
| 2683 | goto out_unlock; |
| 2684 | } |
| 2685 | |
| 2686 | ret = btrfs_cmp_data(src, loff, dst, dst_loff, len); |
| 2687 | if (ret == 0) |
| 2688 | ret = btrfs_clone(src, dst, loff, len, len, dst_loff); |
| 2689 | |
| 2690 | out_unlock: |
| 2691 | btrfs_double_unlock(src, loff, dst, dst_loff, len); |
| 2692 | |
| 2693 | return ret; |
| 2694 | } |
| 2695 | |
| 2696 | #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024) |
| 2697 | |
| 2698 | static long btrfs_ioctl_file_extent_same(struct file *file, |
| 2699 | void __user *argp) |
| 2700 | { |
| 2701 | struct btrfs_ioctl_same_args *args = argp; |
| 2702 | struct btrfs_ioctl_same_args same; |
| 2703 | struct btrfs_ioctl_same_extent_info info; |
| 2704 | struct inode *src = file->f_dentry->d_inode; |
| 2705 | struct file *dst_file = NULL; |
| 2706 | struct inode *dst; |
| 2707 | u64 off; |
| 2708 | u64 len; |
| 2709 | int i; |
| 2710 | int ret; |
| 2711 | u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; |
| 2712 | bool is_admin = capable(CAP_SYS_ADMIN); |
| 2713 | |
| 2714 | if (!(file->f_mode & FMODE_READ)) |
| 2715 | return -EINVAL; |
| 2716 | |
| 2717 | ret = mnt_want_write_file(file); |
| 2718 | if (ret) |
| 2719 | return ret; |
| 2720 | |
| 2721 | if (copy_from_user(&same, |
| 2722 | (struct btrfs_ioctl_same_args __user *)argp, |
| 2723 | sizeof(same))) { |
| 2724 | ret = -EFAULT; |
| 2725 | goto out; |
| 2726 | } |
| 2727 | |
| 2728 | off = same.logical_offset; |
| 2729 | len = same.length; |
| 2730 | |
| 2731 | /* |
| 2732 | * Limit the total length we will dedupe for each operation. |
| 2733 | * This is intended to bound the total time spent in this |
| 2734 | * ioctl to something sane. |
| 2735 | */ |
| 2736 | if (len > BTRFS_MAX_DEDUPE_LEN) |
| 2737 | len = BTRFS_MAX_DEDUPE_LEN; |
| 2738 | |
| 2739 | if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) { |
| 2740 | /* |
| 2741 | * Btrfs does not support blocksize < page_size. As a |
| 2742 | * result, btrfs_cmp_data() won't correctly handle |
| 2743 | * this situation without an update. |
| 2744 | */ |
| 2745 | ret = -EINVAL; |
| 2746 | goto out; |
| 2747 | } |
| 2748 | |
| 2749 | ret = -EISDIR; |
| 2750 | if (S_ISDIR(src->i_mode)) |
| 2751 | goto out; |
| 2752 | |
| 2753 | ret = -EACCES; |
| 2754 | if (!S_ISREG(src->i_mode)) |
| 2755 | goto out; |
| 2756 | |
| 2757 | ret = 0; |
| 2758 | for (i = 0; i < same.dest_count; i++) { |
| 2759 | if (copy_from_user(&info, &args->info[i], sizeof(info))) { |
| 2760 | ret = -EFAULT; |
| 2761 | goto out; |
| 2762 | } |
| 2763 | |
| 2764 | info.bytes_deduped = 0; |
| 2765 | |
| 2766 | dst_file = fget(info.fd); |
| 2767 | if (!dst_file) { |
| 2768 | info.status = -EBADF; |
| 2769 | goto next; |
| 2770 | } |
| 2771 | |
| 2772 | if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) { |
| 2773 | info.status = -EINVAL; |
| 2774 | goto next; |
| 2775 | } |
| 2776 | |
| 2777 | info.status = -EXDEV; |
| 2778 | if (file->f_path.mnt != dst_file->f_path.mnt) |
| 2779 | goto next; |
| 2780 | |
| 2781 | dst = dst_file->f_dentry->d_inode; |
| 2782 | if (src->i_sb != dst->i_sb) |
| 2783 | goto next; |
| 2784 | |
| 2785 | if (S_ISDIR(dst->i_mode)) { |
| 2786 | info.status = -EISDIR; |
| 2787 | goto next; |
| 2788 | } |
| 2789 | |
| 2790 | if (!S_ISREG(dst->i_mode)) { |
| 2791 | info.status = -EACCES; |
| 2792 | goto next; |
| 2793 | } |
| 2794 | |
| 2795 | info.status = btrfs_extent_same(src, off, len, dst, |
| 2796 | info.logical_offset); |
| 2797 | if (info.status == 0) |
| 2798 | info.bytes_deduped += len; |
| 2799 | |
| 2800 | next: |
| 2801 | if (dst_file) |
| 2802 | fput(dst_file); |
| 2803 | |
| 2804 | if (__put_user_unaligned(info.status, &args->info[i].status) || |
| 2805 | __put_user_unaligned(info.bytes_deduped, |
| 2806 | &args->info[i].bytes_deduped)) { |
| 2807 | ret = -EFAULT; |
| 2808 | goto out; |
| 2809 | } |
| 2810 | } |
| 2811 | |
| 2812 | out: |
| 2813 | mnt_drop_write_file(file); |
| 2814 | return ret; |
| 2815 | } |
| 2816 | |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 2817 | /** |
| 2818 | * btrfs_clone() - clone a range from inode file to another |
| 2819 | * |
| 2820 | * @src: Inode to clone from |
| 2821 | * @inode: Inode to clone to |
| 2822 | * @off: Offset within source to start clone from |
| 2823 | * @olen: Original length, passed by user, of range to clone |
| 2824 | * @olen_aligned: Block-aligned value of olen, extent_same uses |
| 2825 | * identical values here |
| 2826 | * @destoff: Offset within @inode to start clone |
| 2827 | */ |
| 2828 | static int btrfs_clone(struct inode *src, struct inode *inode, |
| 2829 | u64 off, u64 olen, u64 olen_aligned, u64 destoff) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2830 | { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2831 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 2832 | struct btrfs_path *path = NULL; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2833 | struct extent_buffer *leaf; |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 2834 | struct btrfs_trans_handle *trans; |
| 2835 | char *buf = NULL; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2836 | struct btrfs_key key; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2837 | u32 nritems; |
| 2838 | int slot; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2839 | int ret; |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 2840 | u64 len = olen_aligned; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2841 | |
| 2842 | ret = -ENOMEM; |
| 2843 | buf = vmalloc(btrfs_level_size(root, 0)); |
| 2844 | if (!buf) |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 2845 | return ret; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2846 | |
| 2847 | path = btrfs_alloc_path(); |
| 2848 | if (!path) { |
| 2849 | vfree(buf); |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 2850 | return ret; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2851 | } |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 2852 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2853 | path->reada = 2; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2854 | /* clone data */ |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2855 | key.objectid = btrfs_ino(src); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2856 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2857 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2858 | |
| 2859 | while (1) { |
| 2860 | /* |
| 2861 | * note the key will change type as we walk through the |
| 2862 | * tree. |
| 2863 | */ |
David Sterba | 362a20c | 2011-08-01 18:11:57 +0200 | [diff] [blame] | 2864 | ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, |
| 2865 | 0, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2866 | if (ret < 0) |
| 2867 | goto out; |
| 2868 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2869 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2870 | if (path->slots[0] >= nritems) { |
David Sterba | 362a20c | 2011-08-01 18:11:57 +0200 | [diff] [blame] | 2871 | ret = btrfs_next_leaf(BTRFS_I(src)->root, path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2872 | if (ret < 0) |
| 2873 | goto out; |
| 2874 | if (ret > 0) |
| 2875 | break; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2876 | nritems = btrfs_header_nritems(path->nodes[0]); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2877 | } |
| 2878 | leaf = path->nodes[0]; |
| 2879 | slot = path->slots[0]; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2880 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2881 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 2882 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2883 | key.objectid != btrfs_ino(src)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2884 | break; |
| 2885 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2886 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
| 2887 | struct btrfs_file_extent_item *extent; |
| 2888 | int type; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2889 | u32 size; |
| 2890 | struct btrfs_key new_key; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2891 | u64 disko = 0, diskl = 0; |
| 2892 | u64 datao = 0, datal = 0; |
| 2893 | u8 comp; |
Sage Weil | b5384d4 | 2010-06-12 22:31:14 +0000 | [diff] [blame] | 2894 | u64 endoff; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2895 | |
| 2896 | size = btrfs_item_size_nr(leaf, slot); |
| 2897 | read_extent_buffer(leaf, buf, |
| 2898 | btrfs_item_ptr_offset(leaf, slot), |
| 2899 | size); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2900 | |
| 2901 | extent = btrfs_item_ptr(leaf, slot, |
| 2902 | struct btrfs_file_extent_item); |
| 2903 | comp = btrfs_file_extent_compression(leaf, extent); |
| 2904 | type = btrfs_file_extent_type(leaf, extent); |
Chris Mason | c8a894d | 2009-06-27 21:07:03 -0400 | [diff] [blame] | 2905 | if (type == BTRFS_FILE_EXTENT_REG || |
| 2906 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2907 | disko = btrfs_file_extent_disk_bytenr(leaf, |
| 2908 | extent); |
| 2909 | diskl = btrfs_file_extent_disk_num_bytes(leaf, |
| 2910 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2911 | datao = btrfs_file_extent_offset(leaf, extent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2912 | datal = btrfs_file_extent_num_bytes(leaf, |
| 2913 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2914 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 2915 | /* take upper bound, may be compressed */ |
| 2916 | datal = btrfs_file_extent_ram_bytes(leaf, |
| 2917 | extent); |
| 2918 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2919 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2920 | |
Sage Weil | 050006a | 2010-10-29 15:37:33 -0400 | [diff] [blame] | 2921 | if (key.offset + datal <= off || |
Liu Bo | aa42ffd | 2012-09-18 03:52:23 -0600 | [diff] [blame] | 2922 | key.offset >= off + len - 1) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2923 | goto next; |
| 2924 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2925 | memcpy(&new_key, &key, sizeof(new_key)); |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 2926 | new_key.objectid = btrfs_ino(inode); |
Li Zefan | 4d728ec | 2011-01-26 14:10:43 +0800 | [diff] [blame] | 2927 | if (off <= key.offset) |
| 2928 | new_key.offset = key.offset + destoff - off; |
| 2929 | else |
| 2930 | new_key.offset = destoff; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2931 | |
Sage Weil | b6f3409 | 2011-09-20 14:48:51 -0400 | [diff] [blame] | 2932 | /* |
| 2933 | * 1 - adjusting old extent (we may have to split it) |
| 2934 | * 1 - add new extent |
| 2935 | * 1 - inode update |
| 2936 | */ |
| 2937 | trans = btrfs_start_transaction(root, 3); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2938 | if (IS_ERR(trans)) { |
| 2939 | ret = PTR_ERR(trans); |
| 2940 | goto out; |
| 2941 | } |
| 2942 | |
Chris Mason | c8a894d | 2009-06-27 21:07:03 -0400 | [diff] [blame] | 2943 | if (type == BTRFS_FILE_EXTENT_REG || |
| 2944 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
Li Zefan | d72c084 | 2011-09-11 10:52:25 -0400 | [diff] [blame] | 2945 | /* |
| 2946 | * a | --- range to clone ---| b |
| 2947 | * | ------------- extent ------------- | |
| 2948 | */ |
| 2949 | |
| 2950 | /* substract range b */ |
| 2951 | if (key.offset + datal > off + len) |
| 2952 | datal = off + len - key.offset; |
| 2953 | |
| 2954 | /* substract range a */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2955 | if (off > key.offset) { |
| 2956 | datao += off - key.offset; |
| 2957 | datal -= off - key.offset; |
| 2958 | } |
| 2959 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 2960 | ret = btrfs_drop_extents(trans, root, inode, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2961 | new_key.offset, |
| 2962 | new_key.offset + datal, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 2963 | 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2964 | if (ret) { |
| 2965 | btrfs_abort_transaction(trans, root, |
| 2966 | ret); |
| 2967 | btrfs_end_transaction(trans, root); |
| 2968 | goto out; |
| 2969 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2970 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2971 | ret = btrfs_insert_empty_item(trans, root, path, |
| 2972 | &new_key, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2973 | if (ret) { |
| 2974 | btrfs_abort_transaction(trans, root, |
| 2975 | ret); |
| 2976 | btrfs_end_transaction(trans, root); |
| 2977 | goto out; |
| 2978 | } |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2979 | |
| 2980 | leaf = path->nodes[0]; |
| 2981 | slot = path->slots[0]; |
| 2982 | write_extent_buffer(leaf, buf, |
| 2983 | btrfs_item_ptr_offset(leaf, slot), |
| 2984 | size); |
| 2985 | |
| 2986 | extent = btrfs_item_ptr(leaf, slot, |
| 2987 | struct btrfs_file_extent_item); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2988 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2989 | /* disko == 0 means it's a hole */ |
| 2990 | if (!disko) |
| 2991 | datao = 0; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2992 | |
| 2993 | btrfs_set_file_extent_offset(leaf, extent, |
| 2994 | datao); |
| 2995 | btrfs_set_file_extent_num_bytes(leaf, extent, |
| 2996 | datal); |
| 2997 | if (disko) { |
| 2998 | inode_add_bytes(inode, datal); |
| 2999 | ret = btrfs_inc_extent_ref(trans, root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3000 | disko, diskl, 0, |
| 3001 | root->root_key.objectid, |
Li Zefan | 33345d01 | 2011-04-20 10:31:50 +0800 | [diff] [blame] | 3002 | btrfs_ino(inode), |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3003 | new_key.offset - datao, |
| 3004 | 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3005 | if (ret) { |
| 3006 | btrfs_abort_transaction(trans, |
| 3007 | root, |
| 3008 | ret); |
| 3009 | btrfs_end_transaction(trans, |
| 3010 | root); |
| 3011 | goto out; |
| 3012 | |
| 3013 | } |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 3014 | } |
| 3015 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 3016 | u64 skip = 0; |
| 3017 | u64 trim = 0; |
| 3018 | if (off > key.offset) { |
| 3019 | skip = off - key.offset; |
| 3020 | new_key.offset += skip; |
| 3021 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3022 | |
Liu Bo | aa42ffd | 2012-09-18 03:52:23 -0600 | [diff] [blame] | 3023 | if (key.offset + datal > off + len) |
| 3024 | trim = key.offset + datal - (off + len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3025 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 3026 | if (comp && (skip || trim)) { |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 3027 | ret = -EINVAL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3028 | btrfs_end_transaction(trans, root); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 3029 | goto out; |
| 3030 | } |
| 3031 | size -= skip + trim; |
| 3032 | datal -= skip + trim; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3033 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 3034 | ret = btrfs_drop_extents(trans, root, inode, |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3035 | new_key.offset, |
| 3036 | new_key.offset + datal, |
Josef Bacik | 2671485 | 2012-08-29 12:24:27 -0400 | [diff] [blame] | 3037 | 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3038 | if (ret) { |
| 3039 | btrfs_abort_transaction(trans, root, |
| 3040 | ret); |
| 3041 | btrfs_end_transaction(trans, root); |
| 3042 | goto out; |
| 3043 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3044 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 3045 | ret = btrfs_insert_empty_item(trans, root, path, |
| 3046 | &new_key, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3047 | if (ret) { |
| 3048 | btrfs_abort_transaction(trans, root, |
| 3049 | ret); |
| 3050 | btrfs_end_transaction(trans, root); |
| 3051 | goto out; |
| 3052 | } |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 3053 | |
| 3054 | if (skip) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3055 | u32 start = |
| 3056 | btrfs_file_extent_calc_inline_size(0); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 3057 | memmove(buf+start, buf+start+skip, |
| 3058 | datal); |
| 3059 | } |
| 3060 | |
| 3061 | leaf = path->nodes[0]; |
| 3062 | slot = path->slots[0]; |
| 3063 | write_extent_buffer(leaf, buf, |
| 3064 | btrfs_item_ptr_offset(leaf, slot), |
| 3065 | size); |
| 3066 | inode_add_bytes(inode, datal); |
| 3067 | } |
| 3068 | |
| 3069 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3070 | btrfs_release_path(path); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 3071 | |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 3072 | inode_inc_iversion(inode); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3073 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Sage Weil | b5384d4 | 2010-06-12 22:31:14 +0000 | [diff] [blame] | 3074 | |
| 3075 | /* |
| 3076 | * we round up to the block size at eof when |
| 3077 | * determining which extents to clone above, |
| 3078 | * but shouldn't round up the file size |
| 3079 | */ |
| 3080 | endoff = new_key.offset + datal; |
Li Zefan | 5f3888f | 2010-11-19 01:36:34 +0000 | [diff] [blame] | 3081 | if (endoff > destoff+olen) |
| 3082 | endoff = destoff+olen; |
Sage Weil | b5384d4 | 2010-06-12 22:31:14 +0000 | [diff] [blame] | 3083 | if (endoff > inode->i_size) |
| 3084 | btrfs_i_size_write(inode, endoff); |
| 3085 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3086 | ret = btrfs_update_inode(trans, root, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3087 | if (ret) { |
| 3088 | btrfs_abort_transaction(trans, root, ret); |
| 3089 | btrfs_end_transaction(trans, root); |
| 3090 | goto out; |
| 3091 | } |
| 3092 | ret = btrfs_end_transaction(trans, root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3093 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3094 | next: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3095 | btrfs_release_path(path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3096 | key.offset++; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3097 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3098 | ret = 0; |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3099 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3100 | out: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3101 | btrfs_release_path(path); |
Mark Fasheh | 32b7c68 | 2013-08-06 11:42:49 -0700 | [diff] [blame] | 3102 | btrfs_free_path(path); |
| 3103 | vfree(buf); |
| 3104 | return ret; |
| 3105 | } |
| 3106 | |
| 3107 | static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, |
| 3108 | u64 off, u64 olen, u64 destoff) |
| 3109 | { |
| 3110 | struct inode *inode = fdentry(file)->d_inode; |
| 3111 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3112 | struct fd src_file; |
| 3113 | struct inode *src; |
| 3114 | int ret; |
| 3115 | u64 len = olen; |
| 3116 | u64 bs = root->fs_info->sb->s_blocksize; |
| 3117 | int same_inode = 0; |
| 3118 | |
| 3119 | /* |
| 3120 | * TODO: |
| 3121 | * - split compressed inline extents. annoying: we need to |
| 3122 | * decompress into destination's address_space (the file offset |
| 3123 | * may change, so source mapping won't do), then recompress (or |
| 3124 | * otherwise reinsert) a subrange. |
| 3125 | * - allow ranges within the same file to be cloned (provided |
| 3126 | * they don't overlap)? |
| 3127 | */ |
| 3128 | |
| 3129 | /* the destination must be opened for writing */ |
| 3130 | if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) |
| 3131 | return -EINVAL; |
| 3132 | |
| 3133 | if (btrfs_root_readonly(root)) |
| 3134 | return -EROFS; |
| 3135 | |
| 3136 | ret = mnt_want_write_file(file); |
| 3137 | if (ret) |
| 3138 | return ret; |
| 3139 | |
| 3140 | src_file = fdget(srcfd); |
| 3141 | if (!src_file.file) { |
| 3142 | ret = -EBADF; |
| 3143 | goto out_drop_write; |
| 3144 | } |
| 3145 | |
| 3146 | ret = -EXDEV; |
| 3147 | if (src_file.file->f_path.mnt != file->f_path.mnt) |
| 3148 | goto out_fput; |
| 3149 | |
| 3150 | src = file_inode(src_file.file); |
| 3151 | |
| 3152 | ret = -EINVAL; |
| 3153 | if (src == inode) |
| 3154 | same_inode = 1; |
| 3155 | |
| 3156 | /* the src must be open for reading */ |
| 3157 | if (!(src_file.file->f_mode & FMODE_READ)) |
| 3158 | goto out_fput; |
| 3159 | |
| 3160 | /* don't make the dst file partly checksummed */ |
| 3161 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != |
| 3162 | (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) |
| 3163 | goto out_fput; |
| 3164 | |
| 3165 | ret = -EISDIR; |
| 3166 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
| 3167 | goto out_fput; |
| 3168 | |
| 3169 | ret = -EXDEV; |
| 3170 | if (src->i_sb != inode->i_sb) |
| 3171 | goto out_fput; |
| 3172 | |
| 3173 | if (!same_inode) { |
| 3174 | if (inode < src) { |
| 3175 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); |
| 3176 | mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); |
| 3177 | } else { |
| 3178 | mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); |
| 3179 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); |
| 3180 | } |
| 3181 | } else { |
| 3182 | mutex_lock(&src->i_mutex); |
| 3183 | } |
| 3184 | |
| 3185 | /* determine range to clone */ |
| 3186 | ret = -EINVAL; |
| 3187 | if (off + len > src->i_size || off + len < off) |
| 3188 | goto out_unlock; |
| 3189 | if (len == 0) |
| 3190 | olen = len = src->i_size - off; |
| 3191 | /* if we extend to eof, continue to block boundary */ |
| 3192 | if (off + len == src->i_size) |
| 3193 | len = ALIGN(src->i_size, bs) - off; |
| 3194 | |
| 3195 | /* verify the end result is block aligned */ |
| 3196 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || |
| 3197 | !IS_ALIGNED(destoff, bs)) |
| 3198 | goto out_unlock; |
| 3199 | |
| 3200 | /* verify if ranges are overlapped within the same file */ |
| 3201 | if (same_inode) { |
| 3202 | if (destoff + len > off && destoff < off + len) |
| 3203 | goto out_unlock; |
| 3204 | } |
| 3205 | |
| 3206 | if (destoff > inode->i_size) { |
| 3207 | ret = btrfs_cont_expand(inode, inode->i_size, destoff); |
| 3208 | if (ret) |
| 3209 | goto out_unlock; |
| 3210 | } |
| 3211 | |
| 3212 | /* truncate page cache pages from target inode range */ |
| 3213 | truncate_inode_pages_range(&inode->i_data, destoff, |
| 3214 | PAGE_CACHE_ALIGN(destoff + len) - 1); |
| 3215 | |
| 3216 | lock_extent_range(src, off, len); |
| 3217 | |
| 3218 | ret = btrfs_clone(src, inode, off, olen, len, destoff); |
| 3219 | |
Liu Bo | aa42ffd | 2012-09-18 03:52:23 -0600 | [diff] [blame] | 3220 | unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3221 | out_unlock: |
| 3222 | mutex_unlock(&src->i_mutex); |
Liu Bo | a96fbc7 | 2013-05-26 13:50:31 +0000 | [diff] [blame] | 3223 | if (!same_inode) |
| 3224 | mutex_unlock(&inode->i_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3225 | out_fput: |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 3226 | fdput(src_file); |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 3227 | out_drop_write: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 3228 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3229 | return ret; |
| 3230 | } |
| 3231 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 3232 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 3233 | { |
| 3234 | struct btrfs_ioctl_clone_range_args args; |
| 3235 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 3236 | if (copy_from_user(&args, argp, sizeof(args))) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 3237 | return -EFAULT; |
| 3238 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, |
| 3239 | args.src_length, args.dest_offset); |
| 3240 | } |
| 3241 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3242 | /* |
| 3243 | * there are many ways the trans_start and trans_end ioctls can lead |
| 3244 | * to deadlocks. They should only be used by applications that |
| 3245 | * basically own the machine, and have a very in depth understanding |
| 3246 | * of all the possible deadlocks and enospc problems. |
| 3247 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 3248 | static long btrfs_ioctl_trans_start(struct file *file) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3249 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3250 | struct inode *inode = file_inode(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3251 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3252 | struct btrfs_trans_handle *trans; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3253 | int ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3254 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3255 | ret = -EPERM; |
Christoph Hellwig | df5b552 | 2008-06-11 21:53:58 -0400 | [diff] [blame] | 3256 | if (!capable(CAP_SYS_ADMIN)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3257 | goto out; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3258 | |
| 3259 | ret = -EINPROGRESS; |
| 3260 | if (file->private_data) |
| 3261 | goto out; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 3262 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3263 | ret = -EROFS; |
| 3264 | if (btrfs_root_readonly(root)) |
| 3265 | goto out; |
| 3266 | |
Al Viro | a561be7 | 2011-11-23 11:57:51 -0500 | [diff] [blame] | 3267 | ret = mnt_want_write_file(file); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3268 | if (ret) |
| 3269 | goto out; |
| 3270 | |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 3271 | atomic_inc(&root->fs_info->open_ioctl_trans); |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 3272 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3273 | ret = -ENOMEM; |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3274 | trans = btrfs_start_ioctl_transaction(root); |
Tsutomu Itoh | abd30bb | 2011-01-24 00:57:10 +0000 | [diff] [blame] | 3275 | if (IS_ERR(trans)) |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3276 | goto out_drop; |
| 3277 | |
| 3278 | file->private_data = trans; |
| 3279 | return 0; |
| 3280 | |
| 3281 | out_drop: |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 3282 | atomic_dec(&root->fs_info->open_ioctl_trans); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 3283 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3284 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3285 | return ret; |
| 3286 | } |
| 3287 | |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3288 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
| 3289 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3290 | struct inode *inode = file_inode(file); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3291 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3292 | struct btrfs_root *new_root; |
| 3293 | struct btrfs_dir_item *di; |
| 3294 | struct btrfs_trans_handle *trans; |
| 3295 | struct btrfs_path *path; |
| 3296 | struct btrfs_key location; |
| 3297 | struct btrfs_disk_key disk_key; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3298 | u64 objectid = 0; |
| 3299 | u64 dir_id; |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3300 | int ret; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3301 | |
| 3302 | if (!capable(CAP_SYS_ADMIN)) |
| 3303 | return -EPERM; |
| 3304 | |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3305 | ret = mnt_want_write_file(file); |
| 3306 | if (ret) |
| 3307 | return ret; |
| 3308 | |
| 3309 | if (copy_from_user(&objectid, argp, sizeof(objectid))) { |
| 3310 | ret = -EFAULT; |
| 3311 | goto out; |
| 3312 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3313 | |
| 3314 | if (!objectid) |
| 3315 | objectid = root->root_key.objectid; |
| 3316 | |
| 3317 | location.objectid = objectid; |
| 3318 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 3319 | location.offset = (u64)-1; |
| 3320 | |
| 3321 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3322 | if (IS_ERR(new_root)) { |
| 3323 | ret = PTR_ERR(new_root); |
| 3324 | goto out; |
| 3325 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3326 | |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3327 | path = btrfs_alloc_path(); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3328 | if (!path) { |
| 3329 | ret = -ENOMEM; |
| 3330 | goto out; |
| 3331 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3332 | path->leave_spinning = 1; |
| 3333 | |
| 3334 | trans = btrfs_start_transaction(root, 1); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3335 | if (IS_ERR(trans)) { |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3336 | btrfs_free_path(path); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3337 | ret = PTR_ERR(trans); |
| 3338 | goto out; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3341 | dir_id = btrfs_super_root_dir(root->fs_info->super_copy); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3342 | di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, |
| 3343 | dir_id, "default", 7, 1); |
Dan Carpenter | cf1e99a | 2010-05-29 09:47:24 +0000 | [diff] [blame] | 3344 | if (IS_ERR_OR_NULL(di)) { |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3345 | btrfs_free_path(path); |
| 3346 | btrfs_end_transaction(trans, root); |
| 3347 | printk(KERN_ERR "Umm, you don't have the default dir item, " |
| 3348 | "this isn't going to work\n"); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3349 | ret = -ENOENT; |
| 3350 | goto out; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); |
| 3354 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); |
| 3355 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3356 | btrfs_free_path(path); |
| 3357 | |
Mitch Harder | 2b0ce2c | 2012-07-24 11:58:43 -0600 | [diff] [blame] | 3358 | btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3359 | btrfs_end_transaction(trans, root); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3360 | out: |
| 3361 | mnt_drop_write_file(file); |
| 3362 | return ret; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3365 | void btrfs_get_block_group_info(struct list_head *groups_list, |
| 3366 | struct btrfs_ioctl_space_info *space) |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3367 | { |
| 3368 | struct btrfs_block_group_cache *block_group; |
| 3369 | |
| 3370 | space->total_bytes = 0; |
| 3371 | space->used_bytes = 0; |
| 3372 | space->flags = 0; |
| 3373 | list_for_each_entry(block_group, groups_list, list) { |
| 3374 | space->flags = block_group->flags; |
| 3375 | space->total_bytes += block_group->key.offset; |
| 3376 | space->used_bytes += |
| 3377 | btrfs_block_group_used(&block_group->item); |
| 3378 | } |
| 3379 | } |
| 3380 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3381 | static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3382 | { |
| 3383 | struct btrfs_ioctl_space_args space_args; |
| 3384 | struct btrfs_ioctl_space_info space; |
| 3385 | struct btrfs_ioctl_space_info *dest; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3386 | struct btrfs_ioctl_space_info *dest_orig; |
Daniel J Blueman | 13f2696 | 2011-04-11 15:56:31 +0000 | [diff] [blame] | 3387 | struct btrfs_ioctl_space_info __user *user_dest; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3388 | struct btrfs_space_info *info; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3389 | u64 types[] = {BTRFS_BLOCK_GROUP_DATA, |
| 3390 | BTRFS_BLOCK_GROUP_SYSTEM, |
| 3391 | BTRFS_BLOCK_GROUP_METADATA, |
| 3392 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; |
| 3393 | int num_types = 4; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3394 | int alloc_size; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3395 | int ret = 0; |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3396 | u64 slot_count = 0; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3397 | int i, c; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3398 | |
| 3399 | if (copy_from_user(&space_args, |
| 3400 | (struct btrfs_ioctl_space_args __user *)arg, |
| 3401 | sizeof(space_args))) |
| 3402 | return -EFAULT; |
| 3403 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3404 | for (i = 0; i < num_types; i++) { |
| 3405 | struct btrfs_space_info *tmp; |
| 3406 | |
| 3407 | info = NULL; |
| 3408 | rcu_read_lock(); |
| 3409 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, |
| 3410 | list) { |
| 3411 | if (tmp->flags == types[i]) { |
| 3412 | info = tmp; |
| 3413 | break; |
| 3414 | } |
| 3415 | } |
| 3416 | rcu_read_unlock(); |
| 3417 | |
| 3418 | if (!info) |
| 3419 | continue; |
| 3420 | |
| 3421 | down_read(&info->groups_sem); |
| 3422 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 3423 | if (!list_empty(&info->block_groups[c])) |
| 3424 | slot_count++; |
| 3425 | } |
| 3426 | up_read(&info->groups_sem); |
| 3427 | } |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3428 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3429 | /* space_slots == 0 means they are asking for a count */ |
| 3430 | if (space_args.space_slots == 0) { |
| 3431 | space_args.total_spaces = slot_count; |
| 3432 | goto out; |
| 3433 | } |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3434 | |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3435 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3436 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3437 | alloc_size = sizeof(*dest) * slot_count; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3438 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3439 | /* we generally have at most 6 or so space infos, one for each raid |
| 3440 | * level. So, a whole page should be more than enough for everyone |
| 3441 | */ |
| 3442 | if (alloc_size > PAGE_CACHE_SIZE) |
| 3443 | return -ENOMEM; |
| 3444 | |
| 3445 | space_args.total_spaces = 0; |
| 3446 | dest = kmalloc(alloc_size, GFP_NOFS); |
| 3447 | if (!dest) |
| 3448 | return -ENOMEM; |
| 3449 | dest_orig = dest; |
| 3450 | |
| 3451 | /* now we have a buffer to copy into */ |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3452 | for (i = 0; i < num_types; i++) { |
| 3453 | struct btrfs_space_info *tmp; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3454 | |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3455 | if (!slot_count) |
| 3456 | break; |
| 3457 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3458 | info = NULL; |
| 3459 | rcu_read_lock(); |
| 3460 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, |
| 3461 | list) { |
| 3462 | if (tmp->flags == types[i]) { |
| 3463 | info = tmp; |
| 3464 | break; |
| 3465 | } |
| 3466 | } |
| 3467 | rcu_read_unlock(); |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3468 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3469 | if (!info) |
| 3470 | continue; |
| 3471 | down_read(&info->groups_sem); |
| 3472 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 3473 | if (!list_empty(&info->block_groups[c])) { |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3474 | btrfs_get_block_group_info( |
| 3475 | &info->block_groups[c], &space); |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3476 | memcpy(dest, &space, sizeof(space)); |
| 3477 | dest++; |
| 3478 | space_args.total_spaces++; |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3479 | slot_count--; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3480 | } |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3481 | if (!slot_count) |
| 3482 | break; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3483 | } |
| 3484 | up_read(&info->groups_sem); |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3485 | } |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3486 | |
Daniel J Blueman | 2eec6c8 | 2012-04-26 00:37:14 +0800 | [diff] [blame] | 3487 | user_dest = (struct btrfs_ioctl_space_info __user *) |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3488 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
| 3489 | |
| 3490 | if (copy_to_user(user_dest, dest_orig, alloc_size)) |
| 3491 | ret = -EFAULT; |
| 3492 | |
| 3493 | kfree(dest_orig); |
| 3494 | out: |
| 3495 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3496 | ret = -EFAULT; |
| 3497 | |
| 3498 | return ret; |
| 3499 | } |
| 3500 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3501 | /* |
| 3502 | * there are many ways the trans_start and trans_end ioctls can lead |
| 3503 | * to deadlocks. They should only be used by applications that |
| 3504 | * basically own the machine, and have a very in depth understanding |
| 3505 | * of all the possible deadlocks and enospc problems. |
| 3506 | */ |
| 3507 | long btrfs_ioctl_trans_end(struct file *file) |
| 3508 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3509 | struct inode *inode = file_inode(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3510 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3511 | struct btrfs_trans_handle *trans; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3512 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3513 | trans = file->private_data; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3514 | if (!trans) |
| 3515 | return -EINVAL; |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 3516 | file->private_data = NULL; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 3517 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3518 | btrfs_end_transaction(trans, root); |
| 3519 | |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 3520 | atomic_dec(&root->fs_info->open_ioctl_trans); |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 3521 | |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 3522 | mnt_drop_write_file(file); |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3523 | return 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3524 | } |
| 3525 | |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 3526 | static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, |
| 3527 | void __user *argp) |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3528 | { |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3529 | struct btrfs_trans_handle *trans; |
| 3530 | u64 transid; |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 3531 | int ret; |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3532 | |
Miao Xie | d4edf39 | 2013-02-20 09:17:06 +0000 | [diff] [blame] | 3533 | trans = btrfs_attach_transaction_barrier(root); |
Miao Xie | ff7c1d3 | 2012-11-26 08:41:29 +0000 | [diff] [blame] | 3534 | if (IS_ERR(trans)) { |
| 3535 | if (PTR_ERR(trans) != -ENOENT) |
| 3536 | return PTR_ERR(trans); |
| 3537 | |
| 3538 | /* No running transaction, don't bother */ |
| 3539 | transid = root->fs_info->last_trans_committed; |
| 3540 | goto out; |
| 3541 | } |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3542 | transid = trans->transid; |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 3543 | ret = btrfs_commit_transaction_async(trans, root, 0); |
Tsutomu Itoh | 8b2b2d3 | 2011-04-04 01:52:13 +0000 | [diff] [blame] | 3544 | if (ret) { |
| 3545 | btrfs_end_transaction(trans, root); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 3546 | return ret; |
Tsutomu Itoh | 8b2b2d3 | 2011-04-04 01:52:13 +0000 | [diff] [blame] | 3547 | } |
Miao Xie | ff7c1d3 | 2012-11-26 08:41:29 +0000 | [diff] [blame] | 3548 | out: |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3549 | if (argp) |
| 3550 | if (copy_to_user(argp, &transid, sizeof(transid))) |
| 3551 | return -EFAULT; |
| 3552 | return 0; |
| 3553 | } |
| 3554 | |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 3555 | static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, |
| 3556 | void __user *argp) |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3557 | { |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3558 | u64 transid; |
| 3559 | |
| 3560 | if (argp) { |
| 3561 | if (copy_from_user(&transid, argp, sizeof(transid))) |
| 3562 | return -EFAULT; |
| 3563 | } else { |
| 3564 | transid = 0; /* current trans */ |
| 3565 | } |
| 3566 | return btrfs_wait_for_commit(root, transid); |
| 3567 | } |
| 3568 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3569 | static long btrfs_ioctl_scrub(struct file *file, void __user *arg) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3570 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3571 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3572 | struct btrfs_ioctl_scrub_args *sa; |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3573 | int ret; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3574 | |
| 3575 | if (!capable(CAP_SYS_ADMIN)) |
| 3576 | return -EPERM; |
| 3577 | |
| 3578 | sa = memdup_user(arg, sizeof(*sa)); |
| 3579 | if (IS_ERR(sa)) |
| 3580 | return PTR_ERR(sa); |
| 3581 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3582 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) { |
| 3583 | ret = mnt_want_write_file(file); |
| 3584 | if (ret) |
| 3585 | goto out; |
| 3586 | } |
| 3587 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 3588 | ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3589 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, |
| 3590 | 0); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3591 | |
| 3592 | if (copy_to_user(arg, sa, sizeof(*sa))) |
| 3593 | ret = -EFAULT; |
| 3594 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3595 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) |
| 3596 | mnt_drop_write_file(file); |
| 3597 | out: |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3598 | kfree(sa); |
| 3599 | return ret; |
| 3600 | } |
| 3601 | |
| 3602 | static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) |
| 3603 | { |
| 3604 | if (!capable(CAP_SYS_ADMIN)) |
| 3605 | return -EPERM; |
| 3606 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 3607 | return btrfs_scrub_cancel(root->fs_info); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, |
| 3611 | void __user *arg) |
| 3612 | { |
| 3613 | struct btrfs_ioctl_scrub_args *sa; |
| 3614 | int ret; |
| 3615 | |
| 3616 | if (!capable(CAP_SYS_ADMIN)) |
| 3617 | return -EPERM; |
| 3618 | |
| 3619 | sa = memdup_user(arg, sizeof(*sa)); |
| 3620 | if (IS_ERR(sa)) |
| 3621 | return PTR_ERR(sa); |
| 3622 | |
| 3623 | ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); |
| 3624 | |
| 3625 | if (copy_to_user(arg, sa, sizeof(*sa))) |
| 3626 | ret = -EFAULT; |
| 3627 | |
| 3628 | kfree(sa); |
| 3629 | return ret; |
| 3630 | } |
| 3631 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3632 | static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 3633 | void __user *arg) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3634 | { |
| 3635 | struct btrfs_ioctl_get_dev_stats *sa; |
| 3636 | int ret; |
| 3637 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3638 | sa = memdup_user(arg, sizeof(*sa)); |
| 3639 | if (IS_ERR(sa)) |
| 3640 | return PTR_ERR(sa); |
| 3641 | |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 3642 | if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { |
| 3643 | kfree(sa); |
| 3644 | return -EPERM; |
| 3645 | } |
| 3646 | |
| 3647 | ret = btrfs_get_dev_stats(root, sa); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3648 | |
| 3649 | if (copy_to_user(arg, sa, sizeof(*sa))) |
| 3650 | ret = -EFAULT; |
| 3651 | |
| 3652 | kfree(sa); |
| 3653 | return ret; |
| 3654 | } |
| 3655 | |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3656 | static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) |
| 3657 | { |
| 3658 | struct btrfs_ioctl_dev_replace_args *p; |
| 3659 | int ret; |
| 3660 | |
| 3661 | if (!capable(CAP_SYS_ADMIN)) |
| 3662 | return -EPERM; |
| 3663 | |
| 3664 | p = memdup_user(arg, sizeof(*p)); |
| 3665 | if (IS_ERR(p)) |
| 3666 | return PTR_ERR(p); |
| 3667 | |
| 3668 | switch (p->cmd) { |
| 3669 | case BTRFS_IOCTL_DEV_REPLACE_CMD_START: |
Stefan Behrens | bbb651e | 2013-08-19 18:51:13 +0200 | [diff] [blame] | 3670 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 3671 | return -EROFS; |
| 3672 | |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3673 | if (atomic_xchg( |
| 3674 | &root->fs_info->mutually_exclusive_operation_running, |
| 3675 | 1)) { |
| 3676 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); |
| 3677 | ret = -EINPROGRESS; |
| 3678 | } else { |
| 3679 | ret = btrfs_dev_replace_start(root, p); |
| 3680 | atomic_set( |
| 3681 | &root->fs_info->mutually_exclusive_operation_running, |
| 3682 | 0); |
| 3683 | } |
| 3684 | break; |
| 3685 | case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: |
| 3686 | btrfs_dev_replace_status(root->fs_info, p); |
| 3687 | ret = 0; |
| 3688 | break; |
| 3689 | case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: |
| 3690 | ret = btrfs_dev_replace_cancel(root->fs_info, p); |
| 3691 | break; |
| 3692 | default: |
| 3693 | ret = -EINVAL; |
| 3694 | break; |
| 3695 | } |
| 3696 | |
| 3697 | if (copy_to_user(arg, p, sizeof(*p))) |
| 3698 | ret = -EFAULT; |
| 3699 | |
| 3700 | kfree(p); |
| 3701 | return ret; |
| 3702 | } |
| 3703 | |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3704 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
| 3705 | { |
| 3706 | int ret = 0; |
| 3707 | int i; |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 3708 | u64 rel_ptr; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3709 | int size; |
Chris Mason | 806468f | 2011-11-06 03:07:10 -0500 | [diff] [blame] | 3710 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3711 | struct inode_fs_paths *ipath = NULL; |
| 3712 | struct btrfs_path *path; |
| 3713 | |
Kusanagi Kouichi | 82b22ac | 2013-01-28 11:33:31 +0000 | [diff] [blame] | 3714 | if (!capable(CAP_DAC_READ_SEARCH)) |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3715 | return -EPERM; |
| 3716 | |
| 3717 | path = btrfs_alloc_path(); |
| 3718 | if (!path) { |
| 3719 | ret = -ENOMEM; |
| 3720 | goto out; |
| 3721 | } |
| 3722 | |
| 3723 | ipa = memdup_user(arg, sizeof(*ipa)); |
| 3724 | if (IS_ERR(ipa)) { |
| 3725 | ret = PTR_ERR(ipa); |
| 3726 | ipa = NULL; |
| 3727 | goto out; |
| 3728 | } |
| 3729 | |
| 3730 | size = min_t(u32, ipa->size, 4096); |
| 3731 | ipath = init_ipath(size, root, path); |
| 3732 | if (IS_ERR(ipath)) { |
| 3733 | ret = PTR_ERR(ipath); |
| 3734 | ipath = NULL; |
| 3735 | goto out; |
| 3736 | } |
| 3737 | |
| 3738 | ret = paths_from_inode(ipa->inum, ipath); |
| 3739 | if (ret < 0) |
| 3740 | goto out; |
| 3741 | |
| 3742 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { |
Jeff Mahoney | 745c4d8 | 2011-11-20 07:31:57 -0500 | [diff] [blame] | 3743 | rel_ptr = ipath->fspath->val[i] - |
| 3744 | (u64)(unsigned long)ipath->fspath->val; |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 3745 | ipath->fspath->val[i] = rel_ptr; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3746 | } |
| 3747 | |
Jeff Mahoney | 745c4d8 | 2011-11-20 07:31:57 -0500 | [diff] [blame] | 3748 | ret = copy_to_user((void *)(unsigned long)ipa->fspath, |
| 3749 | (void *)(unsigned long)ipath->fspath, size); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3750 | if (ret) { |
| 3751 | ret = -EFAULT; |
| 3752 | goto out; |
| 3753 | } |
| 3754 | |
| 3755 | out: |
| 3756 | btrfs_free_path(path); |
| 3757 | free_ipath(ipath); |
| 3758 | kfree(ipa); |
| 3759 | |
| 3760 | return ret; |
| 3761 | } |
| 3762 | |
| 3763 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) |
| 3764 | { |
| 3765 | struct btrfs_data_container *inodes = ctx; |
| 3766 | const size_t c = 3 * sizeof(u64); |
| 3767 | |
| 3768 | if (inodes->bytes_left >= c) { |
| 3769 | inodes->bytes_left -= c; |
| 3770 | inodes->val[inodes->elem_cnt] = inum; |
| 3771 | inodes->val[inodes->elem_cnt + 1] = offset; |
| 3772 | inodes->val[inodes->elem_cnt + 2] = root; |
| 3773 | inodes->elem_cnt += 3; |
| 3774 | } else { |
| 3775 | inodes->bytes_missing += c - inodes->bytes_left; |
| 3776 | inodes->bytes_left = 0; |
| 3777 | inodes->elem_missed += 3; |
| 3778 | } |
| 3779 | |
| 3780 | return 0; |
| 3781 | } |
| 3782 | |
| 3783 | static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, |
| 3784 | void __user *arg) |
| 3785 | { |
| 3786 | int ret = 0; |
| 3787 | int size; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3788 | struct btrfs_ioctl_logical_ino_args *loi; |
| 3789 | struct btrfs_data_container *inodes = NULL; |
| 3790 | struct btrfs_path *path = NULL; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3791 | |
| 3792 | if (!capable(CAP_SYS_ADMIN)) |
| 3793 | return -EPERM; |
| 3794 | |
| 3795 | loi = memdup_user(arg, sizeof(*loi)); |
| 3796 | if (IS_ERR(loi)) { |
| 3797 | ret = PTR_ERR(loi); |
| 3798 | loi = NULL; |
| 3799 | goto out; |
| 3800 | } |
| 3801 | |
| 3802 | path = btrfs_alloc_path(); |
| 3803 | if (!path) { |
| 3804 | ret = -ENOMEM; |
| 3805 | goto out; |
| 3806 | } |
| 3807 | |
Liu Bo | 425d17a | 2012-09-07 20:01:30 -0600 | [diff] [blame] | 3808 | size = min_t(u32, loi->size, 64 * 1024); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3809 | inodes = init_data_container(size); |
| 3810 | if (IS_ERR(inodes)) { |
| 3811 | ret = PTR_ERR(inodes); |
| 3812 | inodes = NULL; |
| 3813 | goto out; |
| 3814 | } |
| 3815 | |
Liu Bo | df031f0 | 2012-09-07 20:01:29 -0600 | [diff] [blame] | 3816 | ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, |
| 3817 | build_ino_list, inodes); |
| 3818 | if (ret == -EINVAL) |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3819 | ret = -ENOENT; |
| 3820 | if (ret < 0) |
| 3821 | goto out; |
| 3822 | |
Jeff Mahoney | 745c4d8 | 2011-11-20 07:31:57 -0500 | [diff] [blame] | 3823 | ret = copy_to_user((void *)(unsigned long)loi->inodes, |
| 3824 | (void *)(unsigned long)inodes, size); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3825 | if (ret) |
| 3826 | ret = -EFAULT; |
| 3827 | |
| 3828 | out: |
| 3829 | btrfs_free_path(path); |
Liu Bo | 425d17a | 2012-09-07 20:01:30 -0600 | [diff] [blame] | 3830 | vfree(inodes); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3831 | kfree(loi); |
| 3832 | |
| 3833 | return ret; |
| 3834 | } |
| 3835 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3836 | void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3837 | struct btrfs_ioctl_balance_args *bargs) |
| 3838 | { |
| 3839 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3840 | |
| 3841 | bargs->flags = bctl->flags; |
| 3842 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3843 | if (atomic_read(&fs_info->balance_running)) |
| 3844 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; |
| 3845 | if (atomic_read(&fs_info->balance_pause_req)) |
| 3846 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3847 | if (atomic_read(&fs_info->balance_cancel_req)) |
| 3848 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3849 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3850 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
| 3851 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); |
| 3852 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3853 | |
| 3854 | if (lock) { |
| 3855 | spin_lock(&fs_info->balance_lock); |
| 3856 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); |
| 3857 | spin_unlock(&fs_info->balance_lock); |
| 3858 | } else { |
| 3859 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); |
| 3860 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3861 | } |
| 3862 | |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 3863 | static long btrfs_ioctl_balance(struct file *file, void __user *arg) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3864 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3865 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3866 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3867 | struct btrfs_ioctl_balance_args *bargs; |
| 3868 | struct btrfs_balance_control *bctl; |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3869 | bool need_unlock; /* for mut. excl. ops lock */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3870 | int ret; |
| 3871 | |
| 3872 | if (!capable(CAP_SYS_ADMIN)) |
| 3873 | return -EPERM; |
| 3874 | |
Liu Bo | e54bfa3 | 2012-06-29 03:58:48 -0600 | [diff] [blame] | 3875 | ret = mnt_want_write_file(file); |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 3876 | if (ret) |
| 3877 | return ret; |
| 3878 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3879 | again: |
| 3880 | if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) { |
| 3881 | mutex_lock(&fs_info->volume_mutex); |
| 3882 | mutex_lock(&fs_info->balance_mutex); |
| 3883 | need_unlock = true; |
| 3884 | goto locked; |
| 3885 | } |
| 3886 | |
| 3887 | /* |
| 3888 | * mut. excl. ops lock is locked. Three possibilites: |
| 3889 | * (1) some other op is running |
| 3890 | * (2) balance is running |
| 3891 | * (3) balance is paused -- special case (think resume) |
| 3892 | */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3893 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3894 | if (fs_info->balance_ctl) { |
| 3895 | /* this is either (2) or (3) */ |
| 3896 | if (!atomic_read(&fs_info->balance_running)) { |
| 3897 | mutex_unlock(&fs_info->balance_mutex); |
| 3898 | if (!mutex_trylock(&fs_info->volume_mutex)) |
| 3899 | goto again; |
| 3900 | mutex_lock(&fs_info->balance_mutex); |
| 3901 | |
| 3902 | if (fs_info->balance_ctl && |
| 3903 | !atomic_read(&fs_info->balance_running)) { |
| 3904 | /* this is (3) */ |
| 3905 | need_unlock = false; |
| 3906 | goto locked; |
| 3907 | } |
| 3908 | |
| 3909 | mutex_unlock(&fs_info->balance_mutex); |
| 3910 | mutex_unlock(&fs_info->volume_mutex); |
| 3911 | goto again; |
| 3912 | } else { |
| 3913 | /* this is (2) */ |
| 3914 | mutex_unlock(&fs_info->balance_mutex); |
| 3915 | ret = -EINPROGRESS; |
| 3916 | goto out; |
| 3917 | } |
| 3918 | } else { |
| 3919 | /* this is (1) */ |
| 3920 | mutex_unlock(&fs_info->balance_mutex); |
| 3921 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); |
| 3922 | ret = -EINVAL; |
| 3923 | goto out; |
| 3924 | } |
| 3925 | |
| 3926 | locked: |
| 3927 | BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3928 | |
| 3929 | if (arg) { |
| 3930 | bargs = memdup_user(arg, sizeof(*bargs)); |
| 3931 | if (IS_ERR(bargs)) { |
| 3932 | ret = PTR_ERR(bargs); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3933 | goto out_unlock; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3934 | } |
Ilya Dryomov | de32226 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3935 | |
| 3936 | if (bargs->flags & BTRFS_BALANCE_RESUME) { |
| 3937 | if (!fs_info->balance_ctl) { |
| 3938 | ret = -ENOTCONN; |
| 3939 | goto out_bargs; |
| 3940 | } |
| 3941 | |
| 3942 | bctl = fs_info->balance_ctl; |
| 3943 | spin_lock(&fs_info->balance_lock); |
| 3944 | bctl->flags |= BTRFS_BALANCE_RESUME; |
| 3945 | spin_unlock(&fs_info->balance_lock); |
| 3946 | |
| 3947 | goto do_balance; |
| 3948 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3949 | } else { |
| 3950 | bargs = NULL; |
| 3951 | } |
| 3952 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3953 | if (fs_info->balance_ctl) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3954 | ret = -EINPROGRESS; |
| 3955 | goto out_bargs; |
| 3956 | } |
| 3957 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3958 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3959 | if (!bctl) { |
| 3960 | ret = -ENOMEM; |
| 3961 | goto out_bargs; |
| 3962 | } |
| 3963 | |
| 3964 | bctl->fs_info = fs_info; |
| 3965 | if (arg) { |
| 3966 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); |
| 3967 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); |
| 3968 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); |
| 3969 | |
| 3970 | bctl->flags = bargs->flags; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3971 | } else { |
| 3972 | /* balance everything - no filters */ |
| 3973 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3974 | } |
| 3975 | |
Ilya Dryomov | de32226 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3976 | do_balance: |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3977 | /* |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3978 | * Ownership of bctl and mutually_exclusive_operation_running |
| 3979 | * goes to to btrfs_balance. bctl is freed in __cancel_balance, |
| 3980 | * or, if restriper was paused all the way until unmount, in |
| 3981 | * free_fs_info. mutually_exclusive_operation_running is |
| 3982 | * cleared in __cancel_balance. |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3983 | */ |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3984 | need_unlock = false; |
| 3985 | |
| 3986 | ret = btrfs_balance(bctl, bargs); |
| 3987 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3988 | if (arg) { |
| 3989 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
| 3990 | ret = -EFAULT; |
| 3991 | } |
| 3992 | |
| 3993 | out_bargs: |
| 3994 | kfree(bargs); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3995 | out_unlock: |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3996 | mutex_unlock(&fs_info->balance_mutex); |
| 3997 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3998 | if (need_unlock) |
| 3999 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 4000 | out: |
Liu Bo | e54bfa3 | 2012-06-29 03:58:48 -0600 | [diff] [blame] | 4001 | mnt_drop_write_file(file); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4002 | return ret; |
| 4003 | } |
| 4004 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4005 | static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) |
| 4006 | { |
| 4007 | if (!capable(CAP_SYS_ADMIN)) |
| 4008 | return -EPERM; |
| 4009 | |
| 4010 | switch (cmd) { |
| 4011 | case BTRFS_BALANCE_CTL_PAUSE: |
| 4012 | return btrfs_pause_balance(root->fs_info); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4013 | case BTRFS_BALANCE_CTL_CANCEL: |
| 4014 | return btrfs_cancel_balance(root->fs_info); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | return -EINVAL; |
| 4018 | } |
| 4019 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4020 | static long btrfs_ioctl_balance_progress(struct btrfs_root *root, |
| 4021 | void __user *arg) |
| 4022 | { |
| 4023 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4024 | struct btrfs_ioctl_balance_args *bargs; |
| 4025 | int ret = 0; |
| 4026 | |
| 4027 | if (!capable(CAP_SYS_ADMIN)) |
| 4028 | return -EPERM; |
| 4029 | |
| 4030 | mutex_lock(&fs_info->balance_mutex); |
| 4031 | if (!fs_info->balance_ctl) { |
| 4032 | ret = -ENOTCONN; |
| 4033 | goto out; |
| 4034 | } |
| 4035 | |
| 4036 | bargs = kzalloc(sizeof(*bargs), GFP_NOFS); |
| 4037 | if (!bargs) { |
| 4038 | ret = -ENOMEM; |
| 4039 | goto out; |
| 4040 | } |
| 4041 | |
| 4042 | update_ioctl_balance_args(fs_info, 1, bargs); |
| 4043 | |
| 4044 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
| 4045 | ret = -EFAULT; |
| 4046 | |
| 4047 | kfree(bargs); |
| 4048 | out: |
| 4049 | mutex_unlock(&fs_info->balance_mutex); |
| 4050 | return ret; |
| 4051 | } |
| 4052 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4053 | static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4054 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4055 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4056 | struct btrfs_ioctl_quota_ctl_args *sa; |
| 4057 | struct btrfs_trans_handle *trans = NULL; |
| 4058 | int ret; |
| 4059 | int err; |
| 4060 | |
| 4061 | if (!capable(CAP_SYS_ADMIN)) |
| 4062 | return -EPERM; |
| 4063 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4064 | ret = mnt_want_write_file(file); |
| 4065 | if (ret) |
| 4066 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4067 | |
| 4068 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4069 | if (IS_ERR(sa)) { |
| 4070 | ret = PTR_ERR(sa); |
| 4071 | goto drop_write; |
| 4072 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4073 | |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 4074 | down_write(&root->fs_info->subvol_sem); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4075 | trans = btrfs_start_transaction(root->fs_info->tree_root, 2); |
| 4076 | if (IS_ERR(trans)) { |
| 4077 | ret = PTR_ERR(trans); |
| 4078 | goto out; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | switch (sa->cmd) { |
| 4082 | case BTRFS_QUOTA_CTL_ENABLE: |
| 4083 | ret = btrfs_quota_enable(trans, root->fs_info); |
| 4084 | break; |
| 4085 | case BTRFS_QUOTA_CTL_DISABLE: |
| 4086 | ret = btrfs_quota_disable(trans, root->fs_info); |
| 4087 | break; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4088 | default: |
| 4089 | ret = -EINVAL; |
| 4090 | break; |
| 4091 | } |
| 4092 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4093 | err = btrfs_commit_transaction(trans, root->fs_info->tree_root); |
| 4094 | if (err && !ret) |
| 4095 | ret = err; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4096 | out: |
| 4097 | kfree(sa); |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 4098 | up_write(&root->fs_info->subvol_sem); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4099 | drop_write: |
| 4100 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4101 | return ret; |
| 4102 | } |
| 4103 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4104 | static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4105 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4106 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4107 | struct btrfs_ioctl_qgroup_assign_args *sa; |
| 4108 | struct btrfs_trans_handle *trans; |
| 4109 | int ret; |
| 4110 | int err; |
| 4111 | |
| 4112 | if (!capable(CAP_SYS_ADMIN)) |
| 4113 | return -EPERM; |
| 4114 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4115 | ret = mnt_want_write_file(file); |
| 4116 | if (ret) |
| 4117 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4118 | |
| 4119 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4120 | if (IS_ERR(sa)) { |
| 4121 | ret = PTR_ERR(sa); |
| 4122 | goto drop_write; |
| 4123 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4124 | |
| 4125 | trans = btrfs_join_transaction(root); |
| 4126 | if (IS_ERR(trans)) { |
| 4127 | ret = PTR_ERR(trans); |
| 4128 | goto out; |
| 4129 | } |
| 4130 | |
| 4131 | /* FIXME: check if the IDs really exist */ |
| 4132 | if (sa->assign) { |
| 4133 | ret = btrfs_add_qgroup_relation(trans, root->fs_info, |
| 4134 | sa->src, sa->dst); |
| 4135 | } else { |
| 4136 | ret = btrfs_del_qgroup_relation(trans, root->fs_info, |
| 4137 | sa->src, sa->dst); |
| 4138 | } |
| 4139 | |
| 4140 | err = btrfs_end_transaction(trans, root); |
| 4141 | if (err && !ret) |
| 4142 | ret = err; |
| 4143 | |
| 4144 | out: |
| 4145 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4146 | drop_write: |
| 4147 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4148 | return ret; |
| 4149 | } |
| 4150 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4151 | static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4152 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4153 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4154 | struct btrfs_ioctl_qgroup_create_args *sa; |
| 4155 | struct btrfs_trans_handle *trans; |
| 4156 | int ret; |
| 4157 | int err; |
| 4158 | |
| 4159 | if (!capable(CAP_SYS_ADMIN)) |
| 4160 | return -EPERM; |
| 4161 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4162 | ret = mnt_want_write_file(file); |
| 4163 | if (ret) |
| 4164 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4165 | |
| 4166 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4167 | if (IS_ERR(sa)) { |
| 4168 | ret = PTR_ERR(sa); |
| 4169 | goto drop_write; |
| 4170 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4171 | |
Miao Xie | d86e56c | 2012-11-15 11:35:41 +0000 | [diff] [blame] | 4172 | if (!sa->qgroupid) { |
| 4173 | ret = -EINVAL; |
| 4174 | goto out; |
| 4175 | } |
| 4176 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4177 | trans = btrfs_join_transaction(root); |
| 4178 | if (IS_ERR(trans)) { |
| 4179 | ret = PTR_ERR(trans); |
| 4180 | goto out; |
| 4181 | } |
| 4182 | |
| 4183 | /* FIXME: check if the IDs really exist */ |
| 4184 | if (sa->create) { |
| 4185 | ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid, |
| 4186 | NULL); |
| 4187 | } else { |
| 4188 | ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); |
| 4189 | } |
| 4190 | |
| 4191 | err = btrfs_end_transaction(trans, root); |
| 4192 | if (err && !ret) |
| 4193 | ret = err; |
| 4194 | |
| 4195 | out: |
| 4196 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4197 | drop_write: |
| 4198 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4199 | return ret; |
| 4200 | } |
| 4201 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4202 | static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4203 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4204 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4205 | struct btrfs_ioctl_qgroup_limit_args *sa; |
| 4206 | struct btrfs_trans_handle *trans; |
| 4207 | int ret; |
| 4208 | int err; |
| 4209 | u64 qgroupid; |
| 4210 | |
| 4211 | if (!capable(CAP_SYS_ADMIN)) |
| 4212 | return -EPERM; |
| 4213 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4214 | ret = mnt_want_write_file(file); |
| 4215 | if (ret) |
| 4216 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4217 | |
| 4218 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4219 | if (IS_ERR(sa)) { |
| 4220 | ret = PTR_ERR(sa); |
| 4221 | goto drop_write; |
| 4222 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4223 | |
| 4224 | trans = btrfs_join_transaction(root); |
| 4225 | if (IS_ERR(trans)) { |
| 4226 | ret = PTR_ERR(trans); |
| 4227 | goto out; |
| 4228 | } |
| 4229 | |
| 4230 | qgroupid = sa->qgroupid; |
| 4231 | if (!qgroupid) { |
| 4232 | /* take the current subvol as qgroup */ |
| 4233 | qgroupid = root->root_key.objectid; |
| 4234 | } |
| 4235 | |
| 4236 | /* FIXME: check if the IDs really exist */ |
| 4237 | ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); |
| 4238 | |
| 4239 | err = btrfs_end_transaction(trans, root); |
| 4240 | if (err && !ret) |
| 4241 | ret = err; |
| 4242 | |
| 4243 | out: |
| 4244 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4245 | drop_write: |
| 4246 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4247 | return ret; |
| 4248 | } |
| 4249 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4250 | static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) |
| 4251 | { |
Al Viro | 6d0379e | 2013-06-16 19:32:35 +0400 | [diff] [blame] | 4252 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4253 | struct btrfs_ioctl_quota_rescan_args *qsa; |
| 4254 | int ret; |
| 4255 | |
| 4256 | if (!capable(CAP_SYS_ADMIN)) |
| 4257 | return -EPERM; |
| 4258 | |
| 4259 | ret = mnt_want_write_file(file); |
| 4260 | if (ret) |
| 4261 | return ret; |
| 4262 | |
| 4263 | qsa = memdup_user(arg, sizeof(*qsa)); |
| 4264 | if (IS_ERR(qsa)) { |
| 4265 | ret = PTR_ERR(qsa); |
| 4266 | goto drop_write; |
| 4267 | } |
| 4268 | |
| 4269 | if (qsa->flags) { |
| 4270 | ret = -EINVAL; |
| 4271 | goto out; |
| 4272 | } |
| 4273 | |
| 4274 | ret = btrfs_qgroup_rescan(root->fs_info); |
| 4275 | |
| 4276 | out: |
| 4277 | kfree(qsa); |
| 4278 | drop_write: |
| 4279 | mnt_drop_write_file(file); |
| 4280 | return ret; |
| 4281 | } |
| 4282 | |
| 4283 | static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) |
| 4284 | { |
Al Viro | 6d0379e | 2013-06-16 19:32:35 +0400 | [diff] [blame] | 4285 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4286 | struct btrfs_ioctl_quota_rescan_args *qsa; |
| 4287 | int ret = 0; |
| 4288 | |
| 4289 | if (!capable(CAP_SYS_ADMIN)) |
| 4290 | return -EPERM; |
| 4291 | |
| 4292 | qsa = kzalloc(sizeof(*qsa), GFP_NOFS); |
| 4293 | if (!qsa) |
| 4294 | return -ENOMEM; |
| 4295 | |
| 4296 | if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
| 4297 | qsa->flags = 1; |
| 4298 | qsa->progress = root->fs_info->qgroup_rescan_progress.objectid; |
| 4299 | } |
| 4300 | |
| 4301 | if (copy_to_user(arg, qsa, sizeof(*qsa))) |
| 4302 | ret = -EFAULT; |
| 4303 | |
| 4304 | kfree(qsa); |
| 4305 | return ret; |
| 4306 | } |
| 4307 | |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 4308 | static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) |
| 4309 | { |
| 4310 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
| 4311 | |
| 4312 | if (!capable(CAP_SYS_ADMIN)) |
| 4313 | return -EPERM; |
| 4314 | |
| 4315 | return btrfs_qgroup_wait_for_completion(root->fs_info); |
| 4316 | } |
| 4317 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4318 | static long btrfs_ioctl_set_received_subvol(struct file *file, |
| 4319 | void __user *arg) |
| 4320 | { |
| 4321 | struct btrfs_ioctl_received_subvol_args *sa = NULL; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4322 | struct inode *inode = file_inode(file); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4323 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4324 | struct btrfs_root_item *root_item = &root->root_item; |
| 4325 | struct btrfs_trans_handle *trans; |
| 4326 | struct timespec ct = CURRENT_TIME; |
| 4327 | int ret = 0; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4328 | int received_uuid_changed; |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4329 | |
| 4330 | ret = mnt_want_write_file(file); |
| 4331 | if (ret < 0) |
| 4332 | return ret; |
| 4333 | |
| 4334 | down_write(&root->fs_info->subvol_sem); |
| 4335 | |
| 4336 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { |
| 4337 | ret = -EINVAL; |
| 4338 | goto out; |
| 4339 | } |
| 4340 | |
| 4341 | if (btrfs_root_readonly(root)) { |
| 4342 | ret = -EROFS; |
| 4343 | goto out; |
| 4344 | } |
| 4345 | |
| 4346 | if (!inode_owner_or_capable(inode)) { |
| 4347 | ret = -EACCES; |
| 4348 | goto out; |
| 4349 | } |
| 4350 | |
| 4351 | sa = memdup_user(arg, sizeof(*sa)); |
| 4352 | if (IS_ERR(sa)) { |
| 4353 | ret = PTR_ERR(sa); |
| 4354 | sa = NULL; |
| 4355 | goto out; |
| 4356 | } |
| 4357 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4358 | /* |
| 4359 | * 1 - root item |
| 4360 | * 2 - uuid items (received uuid + subvol uuid) |
| 4361 | */ |
| 4362 | trans = btrfs_start_transaction(root, 3); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4363 | if (IS_ERR(trans)) { |
| 4364 | ret = PTR_ERR(trans); |
| 4365 | trans = NULL; |
| 4366 | goto out; |
| 4367 | } |
| 4368 | |
| 4369 | sa->rtransid = trans->transid; |
| 4370 | sa->rtime.sec = ct.tv_sec; |
| 4371 | sa->rtime.nsec = ct.tv_nsec; |
| 4372 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4373 | received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, |
| 4374 | BTRFS_UUID_SIZE); |
| 4375 | if (received_uuid_changed && |
| 4376 | !btrfs_is_empty_uuid(root_item->received_uuid)) |
| 4377 | btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, |
| 4378 | root_item->received_uuid, |
| 4379 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4380 | root->root_key.objectid); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4381 | memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); |
| 4382 | btrfs_set_root_stransid(root_item, sa->stransid); |
| 4383 | btrfs_set_root_rtransid(root_item, sa->rtransid); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 4384 | btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); |
| 4385 | btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); |
| 4386 | btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); |
| 4387 | btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4388 | |
| 4389 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 4390 | &root->root_key, &root->root_item); |
| 4391 | if (ret < 0) { |
| 4392 | btrfs_end_transaction(trans, root); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4393 | goto out; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4394 | } |
| 4395 | if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { |
| 4396 | ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root, |
| 4397 | sa->uuid, |
| 4398 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4399 | root->root_key.objectid); |
| 4400 | if (ret < 0 && ret != -EEXIST) { |
| 4401 | btrfs_abort_transaction(trans, root, ret); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4402 | goto out; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | ret = btrfs_commit_transaction(trans, root); |
| 4406 | if (ret < 0) { |
| 4407 | btrfs_abort_transaction(trans, root, ret); |
| 4408 | goto out; |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | ret = copy_to_user(arg, sa, sizeof(*sa)); |
| 4412 | if (ret) |
| 4413 | ret = -EFAULT; |
| 4414 | |
| 4415 | out: |
| 4416 | kfree(sa); |
| 4417 | up_write(&root->fs_info->subvol_sem); |
| 4418 | mnt_drop_write_file(file); |
| 4419 | return ret; |
| 4420 | } |
| 4421 | |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4422 | static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) |
| 4423 | { |
Al Viro | 6d0379e | 2013-06-16 19:32:35 +0400 | [diff] [blame] | 4424 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 4425 | size_t len; |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4426 | int ret; |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 4427 | char label[BTRFS_LABEL_SIZE]; |
| 4428 | |
| 4429 | spin_lock(&root->fs_info->super_lock); |
| 4430 | memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE); |
| 4431 | spin_unlock(&root->fs_info->super_lock); |
| 4432 | |
| 4433 | len = strnlen(label, BTRFS_LABEL_SIZE); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4434 | |
| 4435 | if (len == BTRFS_LABEL_SIZE) { |
| 4436 | pr_warn("btrfs: label is too long, return the first %zu bytes\n", |
| 4437 | --len); |
| 4438 | } |
| 4439 | |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4440 | ret = copy_to_user(arg, label, len); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4441 | |
| 4442 | return ret ? -EFAULT : 0; |
| 4443 | } |
| 4444 | |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4445 | static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) |
| 4446 | { |
Al Viro | 6d0379e | 2013-06-16 19:32:35 +0400 | [diff] [blame] | 4447 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4448 | struct btrfs_super_block *super_block = root->fs_info->super_copy; |
| 4449 | struct btrfs_trans_handle *trans; |
| 4450 | char label[BTRFS_LABEL_SIZE]; |
| 4451 | int ret; |
| 4452 | |
| 4453 | if (!capable(CAP_SYS_ADMIN)) |
| 4454 | return -EPERM; |
| 4455 | |
| 4456 | if (copy_from_user(label, arg, sizeof(label))) |
| 4457 | return -EFAULT; |
| 4458 | |
| 4459 | if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { |
| 4460 | pr_err("btrfs: unable to set label with more than %d bytes\n", |
| 4461 | BTRFS_LABEL_SIZE - 1); |
| 4462 | return -EINVAL; |
| 4463 | } |
| 4464 | |
| 4465 | ret = mnt_want_write_file(file); |
| 4466 | if (ret) |
| 4467 | return ret; |
| 4468 | |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4469 | trans = btrfs_start_transaction(root, 0); |
| 4470 | if (IS_ERR(trans)) { |
| 4471 | ret = PTR_ERR(trans); |
| 4472 | goto out_unlock; |
| 4473 | } |
| 4474 | |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 4475 | spin_lock(&root->fs_info->super_lock); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4476 | strcpy(super_block->label, label); |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 4477 | spin_unlock(&root->fs_info->super_lock); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4478 | ret = btrfs_end_transaction(trans, root); |
| 4479 | |
| 4480 | out_unlock: |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4481 | mnt_drop_write_file(file); |
| 4482 | return ret; |
| 4483 | } |
| 4484 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4485 | long btrfs_ioctl(struct file *file, unsigned int |
| 4486 | cmd, unsigned long arg) |
| 4487 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4488 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 4489 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4490 | |
| 4491 | switch (cmd) { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4492 | case FS_IOC_GETFLAGS: |
| 4493 | return btrfs_ioctl_getflags(file, argp); |
| 4494 | case FS_IOC_SETFLAGS: |
| 4495 | return btrfs_ioctl_setflags(file, argp); |
| 4496 | case FS_IOC_GETVERSION: |
| 4497 | return btrfs_ioctl_getversion(file, argp); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 4498 | case FITRIM: |
| 4499 | return btrfs_ioctl_fitrim(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4500 | case BTRFS_IOC_SNAP_CREATE: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 4501 | return btrfs_ioctl_snap_create(file, argp, 0); |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 4502 | case BTRFS_IOC_SNAP_CREATE_V2: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 4503 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4504 | case BTRFS_IOC_SUBVOL_CREATE: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 4505 | return btrfs_ioctl_snap_create(file, argp, 1); |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 4506 | case BTRFS_IOC_SUBVOL_CREATE_V2: |
| 4507 | return btrfs_ioctl_snap_create_v2(file, argp, 1); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4508 | case BTRFS_IOC_SNAP_DESTROY: |
| 4509 | return btrfs_ioctl_snap_destroy(file, argp); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 4510 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
| 4511 | return btrfs_ioctl_subvol_getflags(file, argp); |
| 4512 | case BTRFS_IOC_SUBVOL_SETFLAGS: |
| 4513 | return btrfs_ioctl_subvol_setflags(file, argp); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4514 | case BTRFS_IOC_DEFAULT_SUBVOL: |
| 4515 | return btrfs_ioctl_default_subvol(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4516 | case BTRFS_IOC_DEFRAG: |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 4517 | return btrfs_ioctl_defrag(file, NULL); |
| 4518 | case BTRFS_IOC_DEFRAG_RANGE: |
| 4519 | return btrfs_ioctl_defrag(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4520 | case BTRFS_IOC_RESIZE: |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 4521 | return btrfs_ioctl_resize(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4522 | case BTRFS_IOC_ADD_DEV: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 4523 | return btrfs_ioctl_add_dev(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4524 | case BTRFS_IOC_RM_DEV: |
Miao Xie | da24927 | 2012-11-26 08:44:50 +0000 | [diff] [blame] | 4525 | return btrfs_ioctl_rm_dev(file, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4526 | case BTRFS_IOC_FS_INFO: |
| 4527 | return btrfs_ioctl_fs_info(root, argp); |
| 4528 | case BTRFS_IOC_DEV_INFO: |
| 4529 | return btrfs_ioctl_dev_info(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4530 | case BTRFS_IOC_BALANCE: |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 4531 | return btrfs_ioctl_balance(file, NULL); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4532 | case BTRFS_IOC_CLONE: |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 4533 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
| 4534 | case BTRFS_IOC_CLONE_RANGE: |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 4535 | return btrfs_ioctl_clone_range(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4536 | case BTRFS_IOC_TRANS_START: |
| 4537 | return btrfs_ioctl_trans_start(file); |
| 4538 | case BTRFS_IOC_TRANS_END: |
| 4539 | return btrfs_ioctl_trans_end(file); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 4540 | case BTRFS_IOC_TREE_SEARCH: |
| 4541 | return btrfs_ioctl_tree_search(file, argp); |
| 4542 | case BTRFS_IOC_INO_LOOKUP: |
| 4543 | return btrfs_ioctl_ino_lookup(file, argp); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4544 | case BTRFS_IOC_INO_PATHS: |
| 4545 | return btrfs_ioctl_ino_to_path(root, argp); |
| 4546 | case BTRFS_IOC_LOGICAL_INO: |
| 4547 | return btrfs_ioctl_logical_to_ino(root, argp); |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 4548 | case BTRFS_IOC_SPACE_INFO: |
| 4549 | return btrfs_ioctl_space_info(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4550 | case BTRFS_IOC_SYNC: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4551 | btrfs_sync_fs(file->f_dentry->d_sb, 1); |
| 4552 | return 0; |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4553 | case BTRFS_IOC_START_SYNC: |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 4554 | return btrfs_ioctl_start_sync(root, argp); |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4555 | case BTRFS_IOC_WAIT_SYNC: |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 4556 | return btrfs_ioctl_wait_sync(root, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4557 | case BTRFS_IOC_SCRUB: |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 4558 | return btrfs_ioctl_scrub(file, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4559 | case BTRFS_IOC_SCRUB_CANCEL: |
| 4560 | return btrfs_ioctl_scrub_cancel(root, argp); |
| 4561 | case BTRFS_IOC_SCRUB_PROGRESS: |
| 4562 | return btrfs_ioctl_scrub_progress(root, argp); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4563 | case BTRFS_IOC_BALANCE_V2: |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 4564 | return btrfs_ioctl_balance(file, argp); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4565 | case BTRFS_IOC_BALANCE_CTL: |
| 4566 | return btrfs_ioctl_balance_ctl(root, arg); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4567 | case BTRFS_IOC_BALANCE_PROGRESS: |
| 4568 | return btrfs_ioctl_balance_progress(root, argp); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4569 | case BTRFS_IOC_SET_RECEIVED_SUBVOL: |
| 4570 | return btrfs_ioctl_set_received_subvol(file, argp); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4571 | case BTRFS_IOC_SEND: |
| 4572 | return btrfs_ioctl_send(file, argp); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 4573 | case BTRFS_IOC_GET_DEV_STATS: |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 4574 | return btrfs_ioctl_get_dev_stats(root, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4575 | case BTRFS_IOC_QUOTA_CTL: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4576 | return btrfs_ioctl_quota_ctl(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4577 | case BTRFS_IOC_QGROUP_ASSIGN: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4578 | return btrfs_ioctl_qgroup_assign(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4579 | case BTRFS_IOC_QGROUP_CREATE: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4580 | return btrfs_ioctl_qgroup_create(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4581 | case BTRFS_IOC_QGROUP_LIMIT: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4582 | return btrfs_ioctl_qgroup_limit(file, argp); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4583 | case BTRFS_IOC_QUOTA_RESCAN: |
| 4584 | return btrfs_ioctl_quota_rescan(file, argp); |
| 4585 | case BTRFS_IOC_QUOTA_RESCAN_STATUS: |
| 4586 | return btrfs_ioctl_quota_rescan_status(file, argp); |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 4587 | case BTRFS_IOC_QUOTA_RESCAN_WAIT: |
| 4588 | return btrfs_ioctl_quota_rescan_wait(file, argp); |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 4589 | case BTRFS_IOC_DEV_REPLACE: |
| 4590 | return btrfs_ioctl_dev_replace(root, argp); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4591 | case BTRFS_IOC_GET_FSLABEL: |
| 4592 | return btrfs_ioctl_get_fslabel(file, argp); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4593 | case BTRFS_IOC_SET_FSLABEL: |
| 4594 | return btrfs_ioctl_set_fslabel(file, argp); |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 4595 | case BTRFS_IOC_FILE_EXTENT_SAME: |
| 4596 | return btrfs_ioctl_file_extent_same(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | return -ENOTTY; |
| 4600 | } |