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