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