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