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