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