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