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" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 53 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 54 | /* Mask out flags that are inappropriate for the given type of inode. */ |
| 55 | static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) |
| 56 | { |
| 57 | if (S_ISDIR(mode)) |
| 58 | return flags; |
| 59 | else if (S_ISREG(mode)) |
| 60 | return flags & ~FS_DIRSYNC_FL; |
| 61 | else |
| 62 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); |
| 63 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 64 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 65 | /* |
| 66 | * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. |
| 67 | */ |
| 68 | static unsigned int btrfs_flags_to_ioctl(unsigned int flags) |
| 69 | { |
| 70 | unsigned int iflags = 0; |
| 71 | |
| 72 | if (flags & BTRFS_INODE_SYNC) |
| 73 | iflags |= FS_SYNC_FL; |
| 74 | if (flags & BTRFS_INODE_IMMUTABLE) |
| 75 | iflags |= FS_IMMUTABLE_FL; |
| 76 | if (flags & BTRFS_INODE_APPEND) |
| 77 | iflags |= FS_APPEND_FL; |
| 78 | if (flags & BTRFS_INODE_NODUMP) |
| 79 | iflags |= FS_NODUMP_FL; |
| 80 | if (flags & BTRFS_INODE_NOATIME) |
| 81 | iflags |= FS_NOATIME_FL; |
| 82 | if (flags & BTRFS_INODE_DIRSYNC) |
| 83 | iflags |= FS_DIRSYNC_FL; |
| 84 | |
| 85 | return iflags; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Update inode->i_flags based on the btrfs internal flags. |
| 90 | */ |
| 91 | void btrfs_update_iflags(struct inode *inode) |
| 92 | { |
| 93 | struct btrfs_inode *ip = BTRFS_I(inode); |
| 94 | |
| 95 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); |
| 96 | |
| 97 | if (ip->flags & BTRFS_INODE_SYNC) |
| 98 | inode->i_flags |= S_SYNC; |
| 99 | if (ip->flags & BTRFS_INODE_IMMUTABLE) |
| 100 | inode->i_flags |= S_IMMUTABLE; |
| 101 | if (ip->flags & BTRFS_INODE_APPEND) |
| 102 | inode->i_flags |= S_APPEND; |
| 103 | if (ip->flags & BTRFS_INODE_NOATIME) |
| 104 | inode->i_flags |= S_NOATIME; |
| 105 | if (ip->flags & BTRFS_INODE_DIRSYNC) |
| 106 | inode->i_flags |= S_DIRSYNC; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Inherit flags from the parent inode. |
| 111 | * |
| 112 | * Unlike extN we don't have any flags we don't want to inherit currently. |
| 113 | */ |
| 114 | void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 115 | { |
Chris Mason | 0b4dcea | 2009-06-11 11:13:35 -0400 | [diff] [blame] | 116 | unsigned int flags; |
| 117 | |
| 118 | if (!dir) |
| 119 | return; |
| 120 | |
| 121 | flags = BTRFS_I(dir)->flags; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 122 | |
| 123 | if (S_ISREG(inode->i_mode)) |
| 124 | flags &= ~BTRFS_INODE_DIRSYNC; |
| 125 | else if (!S_ISDIR(inode->i_mode)) |
| 126 | flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME); |
| 127 | |
| 128 | BTRFS_I(inode)->flags = flags; |
| 129 | btrfs_update_iflags(inode); |
| 130 | } |
| 131 | |
| 132 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) |
| 133 | { |
| 134 | struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode); |
| 135 | unsigned int flags = btrfs_flags_to_ioctl(ip->flags); |
| 136 | |
| 137 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 138 | return -EFAULT; |
| 139 | return 0; |
| 140 | } |
| 141 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 142 | static int check_flags(unsigned int flags) |
| 143 | { |
| 144 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
| 145 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
| 146 | FS_SYNC_FL | FS_DIRSYNC_FL | \ |
| 147 | FS_NOCOMP_FL | FS_COMPR_FL | \ |
| 148 | FS_NOCOW_FL | FS_COW_FL)) |
| 149 | return -EOPNOTSUPP; |
| 150 | |
| 151 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) |
| 152 | return -EINVAL; |
| 153 | |
| 154 | if ((flags & FS_NOCOW_FL) && (flags & FS_COW_FL)) |
| 155 | return -EINVAL; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 160 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) |
| 161 | { |
| 162 | struct inode *inode = file->f_path.dentry->d_inode; |
| 163 | struct btrfs_inode *ip = BTRFS_I(inode); |
| 164 | struct btrfs_root *root = ip->root; |
| 165 | struct btrfs_trans_handle *trans; |
| 166 | unsigned int flags, oldflags; |
| 167 | int ret; |
| 168 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 169 | if (btrfs_root_readonly(root)) |
| 170 | return -EROFS; |
| 171 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 172 | if (copy_from_user(&flags, arg, sizeof(flags))) |
| 173 | return -EFAULT; |
| 174 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 175 | ret = check_flags(flags); |
| 176 | if (ret) |
| 177 | return ret; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 178 | |
| 179 | if (!is_owner_or_cap(inode)) |
| 180 | return -EACCES; |
| 181 | |
| 182 | mutex_lock(&inode->i_mutex); |
| 183 | |
| 184 | flags = btrfs_mask_flags(inode->i_mode, flags); |
| 185 | oldflags = btrfs_flags_to_ioctl(ip->flags); |
| 186 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
| 187 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 188 | ret = -EPERM; |
| 189 | goto out_unlock; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | ret = mnt_want_write(file->f_path.mnt); |
| 194 | if (ret) |
| 195 | goto out_unlock; |
| 196 | |
| 197 | if (flags & FS_SYNC_FL) |
| 198 | ip->flags |= BTRFS_INODE_SYNC; |
| 199 | else |
| 200 | ip->flags &= ~BTRFS_INODE_SYNC; |
| 201 | if (flags & FS_IMMUTABLE_FL) |
| 202 | ip->flags |= BTRFS_INODE_IMMUTABLE; |
| 203 | else |
| 204 | ip->flags &= ~BTRFS_INODE_IMMUTABLE; |
| 205 | if (flags & FS_APPEND_FL) |
| 206 | ip->flags |= BTRFS_INODE_APPEND; |
| 207 | else |
| 208 | ip->flags &= ~BTRFS_INODE_APPEND; |
| 209 | if (flags & FS_NODUMP_FL) |
| 210 | ip->flags |= BTRFS_INODE_NODUMP; |
| 211 | else |
| 212 | ip->flags &= ~BTRFS_INODE_NODUMP; |
| 213 | if (flags & FS_NOATIME_FL) |
| 214 | ip->flags |= BTRFS_INODE_NOATIME; |
| 215 | else |
| 216 | ip->flags &= ~BTRFS_INODE_NOATIME; |
| 217 | if (flags & FS_DIRSYNC_FL) |
| 218 | ip->flags |= BTRFS_INODE_DIRSYNC; |
| 219 | else |
| 220 | ip->flags &= ~BTRFS_INODE_DIRSYNC; |
| 221 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 222 | /* |
| 223 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS |
| 224 | * flag may be changed automatically if compression code won't make |
| 225 | * things smaller. |
| 226 | */ |
| 227 | if (flags & FS_NOCOMP_FL) { |
| 228 | ip->flags &= ~BTRFS_INODE_COMPRESS; |
| 229 | ip->flags |= BTRFS_INODE_NOCOMPRESS; |
| 230 | } else if (flags & FS_COMPR_FL) { |
| 231 | ip->flags |= BTRFS_INODE_COMPRESS; |
| 232 | ip->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 233 | } |
| 234 | if (flags & FS_NOCOW_FL) |
| 235 | ip->flags |= BTRFS_INODE_NODATACOW; |
| 236 | else if (flags & FS_COW_FL) |
| 237 | ip->flags &= ~BTRFS_INODE_NODATACOW; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 238 | |
| 239 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 240 | BUG_ON(IS_ERR(trans)); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 241 | |
| 242 | ret = btrfs_update_inode(trans, root, inode); |
| 243 | BUG_ON(ret); |
| 244 | |
| 245 | btrfs_update_iflags(inode); |
| 246 | inode->i_ctime = CURRENT_TIME; |
| 247 | btrfs_end_transaction(trans, root); |
| 248 | |
| 249 | mnt_drop_write(file->f_path.mnt); |
liubo | 2d4e6f6a | 2011-02-24 09:38:16 +0000 | [diff] [blame^] | 250 | |
| 251 | ret = 0; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 252 | out_unlock: |
| 253 | mutex_unlock(&inode->i_mutex); |
liubo | 2d4e6f6a | 2011-02-24 09:38:16 +0000 | [diff] [blame^] | 254 | return ret; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) |
| 258 | { |
| 259 | struct inode *inode = file->f_path.dentry->d_inode; |
| 260 | |
| 261 | return put_user(inode->i_generation, arg); |
| 262 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 263 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 264 | static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) |
| 265 | { |
| 266 | struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info; |
| 267 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 268 | struct btrfs_device *device; |
| 269 | struct request_queue *q; |
| 270 | struct fstrim_range range; |
| 271 | u64 minlen = ULLONG_MAX; |
| 272 | u64 num_devices = 0; |
| 273 | int ret; |
| 274 | |
| 275 | if (!capable(CAP_SYS_ADMIN)) |
| 276 | return -EPERM; |
| 277 | |
| 278 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 279 | list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) { |
| 280 | if (!device->bdev) |
| 281 | continue; |
| 282 | q = bdev_get_queue(device->bdev); |
| 283 | if (blk_queue_discard(q)) { |
| 284 | num_devices++; |
| 285 | minlen = min((u64)q->limits.discard_granularity, |
| 286 | minlen); |
| 287 | } |
| 288 | } |
| 289 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 290 | if (!num_devices) |
| 291 | return -EOPNOTSUPP; |
| 292 | |
| 293 | if (copy_from_user(&range, arg, sizeof(range))) |
| 294 | return -EFAULT; |
| 295 | |
| 296 | range.minlen = max(range.minlen, minlen); |
| 297 | ret = btrfs_trim_fs(root, &range); |
| 298 | if (ret < 0) |
| 299 | return ret; |
| 300 | |
| 301 | if (copy_to_user(arg, &range, sizeof(range))) |
| 302 | return -EFAULT; |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 307 | static noinline int create_subvol(struct btrfs_root *root, |
| 308 | struct dentry *dentry, |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 309 | char *name, int namelen, |
| 310 | u64 *async_transid) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 311 | { |
| 312 | struct btrfs_trans_handle *trans; |
| 313 | struct btrfs_key key; |
| 314 | struct btrfs_root_item root_item; |
| 315 | struct btrfs_inode_item *inode_item; |
| 316 | struct extent_buffer *leaf; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 317 | struct btrfs_root *new_root; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 318 | struct dentry *parent = dget_parent(dentry); |
| 319 | struct inode *dir; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 320 | int ret; |
| 321 | int err; |
| 322 | u64 objectid; |
| 323 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 324 | u64 index = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 325 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 326 | ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root, |
| 327 | 0, &objectid); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 328 | if (ret) { |
| 329 | dput(parent); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 330 | return ret; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | dir = parent->d_inode; |
| 334 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 335 | /* |
| 336 | * 1 - inode item |
| 337 | * 2 - refs |
| 338 | * 1 - root item |
| 339 | * 2 - dir items |
| 340 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 341 | trans = btrfs_start_transaction(root, 6); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 342 | if (IS_ERR(trans)) { |
| 343 | dput(parent); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 344 | return PTR_ERR(trans); |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 345 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 346 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 347 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
| 348 | 0, objectid, NULL, 0, 0, 0); |
Josef Bacik | 8e8a1e3 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 349 | if (IS_ERR(leaf)) { |
| 350 | ret = PTR_ERR(leaf); |
| 351 | goto fail; |
| 352 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 353 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 354 | memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 355 | btrfs_set_header_bytenr(leaf, leaf->start); |
| 356 | btrfs_set_header_generation(leaf, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 357 | btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 358 | btrfs_set_header_owner(leaf, objectid); |
| 359 | |
| 360 | write_extent_buffer(leaf, root->fs_info->fsid, |
| 361 | (unsigned long)btrfs_header_fsid(leaf), |
| 362 | BTRFS_FSID_SIZE); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 363 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 364 | (unsigned long)btrfs_header_chunk_tree_uuid(leaf), |
| 365 | BTRFS_UUID_SIZE); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 366 | btrfs_mark_buffer_dirty(leaf); |
| 367 | |
| 368 | inode_item = &root_item.inode; |
| 369 | memset(inode_item, 0, sizeof(*inode_item)); |
| 370 | inode_item->generation = cpu_to_le64(1); |
| 371 | inode_item->size = cpu_to_le64(3); |
| 372 | inode_item->nlink = cpu_to_le32(1); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 373 | inode_item->nbytes = cpu_to_le64(root->leafsize); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 374 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
| 375 | |
| 376 | btrfs_set_root_bytenr(&root_item, leaf->start); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 377 | btrfs_set_root_generation(&root_item, trans->transid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 378 | btrfs_set_root_level(&root_item, 0); |
| 379 | btrfs_set_root_refs(&root_item, 1); |
Yan, Zheng | 86b9f2e | 2009-11-12 09:36:50 +0000 | [diff] [blame] | 380 | btrfs_set_root_used(&root_item, leaf->len); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 381 | btrfs_set_root_last_snapshot(&root_item, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 382 | |
| 383 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
| 384 | root_item.drop_level = 0; |
| 385 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 386 | btrfs_tree_unlock(leaf); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 387 | free_extent_buffer(leaf); |
| 388 | leaf = NULL; |
| 389 | |
| 390 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 391 | |
| 392 | key.objectid = objectid; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 393 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 394 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 395 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 396 | &root_item); |
| 397 | if (ret) |
| 398 | goto fail; |
| 399 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 400 | key.offset = (u64)-1; |
| 401 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); |
| 402 | BUG_ON(IS_ERR(new_root)); |
| 403 | |
| 404 | btrfs_record_root_in_trans(trans, new_root); |
| 405 | |
| 406 | ret = btrfs_create_subvol_root(trans, new_root, new_dirid, |
| 407 | BTRFS_I(dir)->block_group); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 408 | /* |
| 409 | * insert the directory item |
| 410 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 411 | ret = btrfs_set_inode_index(dir, &index); |
| 412 | BUG_ON(ret); |
| 413 | |
| 414 | ret = btrfs_insert_dir_item(trans, root, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 415 | name, namelen, dir->i_ino, &key, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 416 | BTRFS_FT_DIR, index); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 417 | if (ret) |
| 418 | goto fail; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 419 | |
Yan Zheng | 52c2617 | 2009-01-05 15:43:43 -0500 | [diff] [blame] | 420 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
| 421 | ret = btrfs_update_inode(trans, root, dir); |
| 422 | BUG_ON(ret); |
| 423 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 424 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 425 | objectid, root->root_key.objectid, |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 426 | dir->i_ino, index, name, namelen); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 427 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 428 | BUG_ON(ret); |
| 429 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 430 | d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 431 | fail: |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 432 | dput(parent); |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 433 | if (async_transid) { |
| 434 | *async_transid = trans->transid; |
| 435 | err = btrfs_commit_transaction_async(trans, root, 1); |
| 436 | } else { |
| 437 | err = btrfs_commit_transaction(trans, root); |
| 438 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 439 | if (err && !ret) |
| 440 | ret = err; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 441 | return ret; |
| 442 | } |
| 443 | |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 444 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 445 | char *name, int namelen, u64 *async_transid, |
| 446 | bool readonly) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 447 | { |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 448 | struct inode *inode; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 449 | struct dentry *parent; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 450 | struct btrfs_pending_snapshot *pending_snapshot; |
| 451 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 452 | int ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 453 | |
| 454 | if (!root->ref_cows) |
| 455 | return -EINVAL; |
| 456 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 457 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
| 458 | if (!pending_snapshot) |
| 459 | return -ENOMEM; |
| 460 | |
| 461 | btrfs_init_block_rsv(&pending_snapshot->block_rsv); |
| 462 | pending_snapshot->dentry = dentry; |
| 463 | pending_snapshot->root = root; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 464 | pending_snapshot->readonly = readonly; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 465 | |
| 466 | trans = btrfs_start_transaction(root->fs_info->extent_root, 5); |
| 467 | if (IS_ERR(trans)) { |
| 468 | ret = PTR_ERR(trans); |
| 469 | goto fail; |
| 470 | } |
| 471 | |
| 472 | ret = btrfs_snap_reserve_metadata(trans, pending_snapshot); |
| 473 | BUG_ON(ret); |
| 474 | |
| 475 | list_add(&pending_snapshot->list, |
| 476 | &trans->transaction->pending_snapshots); |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 477 | if (async_transid) { |
| 478 | *async_transid = trans->transid; |
| 479 | ret = btrfs_commit_transaction_async(trans, |
| 480 | root->fs_info->extent_root, 1); |
| 481 | } else { |
| 482 | ret = btrfs_commit_transaction(trans, |
| 483 | root->fs_info->extent_root); |
| 484 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 485 | BUG_ON(ret); |
| 486 | |
| 487 | ret = pending_snapshot->error; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 488 | if (ret) |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 489 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 490 | |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 491 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
| 492 | if (ret) |
| 493 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 494 | |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 495 | parent = dget_parent(dentry); |
| 496 | inode = btrfs_lookup_dentry(parent->d_inode, dentry); |
| 497 | dput(parent); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 498 | if (IS_ERR(inode)) { |
| 499 | ret = PTR_ERR(inode); |
| 500 | goto fail; |
| 501 | } |
| 502 | BUG_ON(!inode); |
| 503 | d_instantiate(dentry, inode); |
| 504 | ret = 0; |
| 505 | fail: |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 506 | kfree(pending_snapshot); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 507 | return ret; |
| 508 | } |
| 509 | |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 510 | /* copy of check_sticky in fs/namei.c() |
| 511 | * It's inline, so penalty for filesystems that don't use sticky bit is |
| 512 | * minimal. |
| 513 | */ |
| 514 | static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) |
| 515 | { |
| 516 | uid_t fsuid = current_fsuid(); |
| 517 | |
| 518 | if (!(dir->i_mode & S_ISVTX)) |
| 519 | return 0; |
| 520 | if (inode->i_uid == fsuid) |
| 521 | return 0; |
| 522 | if (dir->i_uid == fsuid) |
| 523 | return 0; |
| 524 | return !capable(CAP_FOWNER); |
| 525 | } |
| 526 | |
| 527 | /* copy of may_delete in fs/namei.c() |
| 528 | * Check whether we can remove a link victim from directory dir, check |
| 529 | * whether the type of victim is right. |
| 530 | * 1. We can't do it if dir is read-only (done in permission()) |
| 531 | * 2. We should have write and exec permissions on dir |
| 532 | * 3. We can't remove anything from append-only dir |
| 533 | * 4. We can't do anything with immutable dir (done in permission()) |
| 534 | * 5. If the sticky bit on dir is set we should either |
| 535 | * a. be owner of dir, or |
| 536 | * b. be owner of victim, or |
| 537 | * c. have CAP_FOWNER capability |
| 538 | * 6. If the victim is append-only or immutable we can't do antyhing with |
| 539 | * links pointing to it. |
| 540 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 541 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 542 | * 9. We can't remove a root or mountpoint. |
| 543 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by |
| 544 | * nfs_async_unlink(). |
| 545 | */ |
| 546 | |
| 547 | static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) |
| 548 | { |
| 549 | int error; |
| 550 | |
| 551 | if (!victim->d_inode) |
| 552 | return -ENOENT; |
| 553 | |
| 554 | BUG_ON(victim->d_parent->d_inode != dir); |
| 555 | audit_inode_child(victim, dir); |
| 556 | |
| 557 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 558 | if (error) |
| 559 | return error; |
| 560 | if (IS_APPEND(dir)) |
| 561 | return -EPERM; |
| 562 | if (btrfs_check_sticky(dir, victim->d_inode)|| |
| 563 | IS_APPEND(victim->d_inode)|| |
| 564 | IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) |
| 565 | return -EPERM; |
| 566 | if (isdir) { |
| 567 | if (!S_ISDIR(victim->d_inode->i_mode)) |
| 568 | return -ENOTDIR; |
| 569 | if (IS_ROOT(victim)) |
| 570 | return -EBUSY; |
| 571 | } else if (S_ISDIR(victim->d_inode->i_mode)) |
| 572 | return -EISDIR; |
| 573 | if (IS_DEADDIR(dir)) |
| 574 | return -ENOENT; |
| 575 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 576 | return -EBUSY; |
| 577 | return 0; |
| 578 | } |
| 579 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 580 | /* copy of may_create in fs/namei.c() */ |
| 581 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) |
| 582 | { |
| 583 | if (child->d_inode) |
| 584 | return -EEXIST; |
| 585 | if (IS_DEADDIR(dir)) |
| 586 | return -ENOENT; |
| 587 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Create a new subvolume below @parent. This is largely modeled after |
| 592 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup |
| 593 | * inside this filesystem so it's quite a bit simpler. |
| 594 | */ |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 595 | static noinline int btrfs_mksubvol(struct path *parent, |
| 596 | char *name, int namelen, |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 597 | struct btrfs_root *snap_src, |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 598 | u64 *async_transid, bool readonly) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 599 | { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 600 | struct inode *dir = parent->dentry->d_inode; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 601 | struct dentry *dentry; |
| 602 | int error; |
| 603 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 604 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 605 | |
| 606 | dentry = lookup_one_len(name, parent->dentry, namelen); |
| 607 | error = PTR_ERR(dentry); |
| 608 | if (IS_ERR(dentry)) |
| 609 | goto out_unlock; |
| 610 | |
| 611 | error = -EEXIST; |
| 612 | if (dentry->d_inode) |
| 613 | goto out_dput; |
| 614 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 615 | error = mnt_want_write(parent->mnt); |
| 616 | if (error) |
| 617 | goto out_dput; |
| 618 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 619 | error = btrfs_may_create(dir, dentry); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 620 | if (error) |
| 621 | goto out_drop_write; |
| 622 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 623 | down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
| 624 | |
| 625 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) |
| 626 | goto out_up_read; |
| 627 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 628 | if (snap_src) { |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 629 | error = create_snapshot(snap_src, dentry, |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 630 | name, namelen, async_transid, readonly); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 631 | } else { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 632 | error = create_subvol(BTRFS_I(dir)->root, dentry, |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 633 | name, namelen, async_transid); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 634 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 635 | if (!error) |
| 636 | fsnotify_mkdir(dir, dentry); |
| 637 | out_up_read: |
| 638 | up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 639 | out_drop_write: |
| 640 | mnt_drop_write(parent->mnt); |
| 641 | out_dput: |
| 642 | dput(dentry); |
| 643 | out_unlock: |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 644 | mutex_unlock(&dir->i_mutex); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 645 | return error; |
| 646 | } |
| 647 | |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 648 | static int should_defrag_range(struct inode *inode, u64 start, u64 len, |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 649 | int thresh, u64 *last_len, u64 *skip, |
| 650 | u64 *defrag_end) |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 651 | { |
| 652 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 653 | struct extent_map *em = NULL; |
| 654 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 655 | int ret = 1; |
| 656 | |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 657 | |
| 658 | if (thresh == 0) |
| 659 | thresh = 256 * 1024; |
| 660 | |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 661 | /* |
| 662 | * make sure that once we start defragging and extent, we keep on |
| 663 | * defragging it |
| 664 | */ |
| 665 | if (start < *defrag_end) |
| 666 | return 1; |
| 667 | |
| 668 | *skip = 0; |
| 669 | |
| 670 | /* |
| 671 | * hopefully we have this extent in the tree already, try without |
| 672 | * the full extent lock |
| 673 | */ |
| 674 | read_lock(&em_tree->lock); |
| 675 | em = lookup_extent_mapping(em_tree, start, len); |
| 676 | read_unlock(&em_tree->lock); |
| 677 | |
| 678 | if (!em) { |
| 679 | /* get the big lock and read metadata off disk */ |
| 680 | lock_extent(io_tree, start, start + len - 1, GFP_NOFS); |
| 681 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
| 682 | unlock_extent(io_tree, start, start + len - 1, GFP_NOFS); |
| 683 | |
Dan Carpenter | 6cf8bfb | 2010-03-20 11:22:10 +0000 | [diff] [blame] | 684 | if (IS_ERR(em)) |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | /* this will cover holes, and inline extents */ |
| 689 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) |
| 690 | ret = 0; |
| 691 | |
| 692 | /* |
| 693 | * we hit a real extent, if it is big don't bother defragging it again |
| 694 | */ |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 695 | if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh) |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 696 | ret = 0; |
| 697 | |
| 698 | /* |
| 699 | * last_len ends up being a counter of how many bytes we've defragged. |
| 700 | * every time we choose not to defrag an extent, we reset *last_len |
| 701 | * so that the next tiny extent will force a defrag. |
| 702 | * |
| 703 | * The end result of this is that tiny extents before a single big |
| 704 | * extent will force at least part of that big extent to be defragged. |
| 705 | */ |
| 706 | if (ret) { |
| 707 | *last_len += len; |
| 708 | *defrag_end = extent_map_end(em); |
| 709 | } else { |
| 710 | *last_len = 0; |
| 711 | *skip = extent_map_end(em); |
| 712 | *defrag_end = 0; |
| 713 | } |
| 714 | |
| 715 | free_extent_map(em); |
| 716 | return ret; |
| 717 | } |
| 718 | |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 719 | static int btrfs_defrag_file(struct file *file, |
| 720 | struct btrfs_ioctl_defrag_range_args *range) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 721 | { |
| 722 | struct inode *inode = fdentry(file)->d_inode; |
| 723 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 724 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 725 | struct btrfs_ordered_extent *ordered; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 726 | struct page *page; |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 727 | struct btrfs_super_block *disk_super; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 728 | unsigned long last_index; |
| 729 | unsigned long ra_pages = root->fs_info->bdi.ra_pages; |
| 730 | unsigned long total_read = 0; |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 731 | u64 features; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 732 | u64 page_start; |
| 733 | u64 page_end; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 734 | u64 last_len = 0; |
| 735 | u64 skip = 0; |
| 736 | u64 defrag_end = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 737 | unsigned long i; |
| 738 | int ret; |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 739 | int compress_type = BTRFS_COMPRESS_ZLIB; |
| 740 | |
| 741 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { |
| 742 | if (range->compress_type > BTRFS_COMPRESS_TYPES) |
| 743 | return -EINVAL; |
| 744 | if (range->compress_type) |
| 745 | compress_type = range->compress_type; |
| 746 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 747 | |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 748 | if (inode->i_size == 0) |
| 749 | return 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 750 | |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 751 | if (range->start + range->len > range->start) { |
| 752 | last_index = min_t(u64, inode->i_size - 1, |
| 753 | range->start + range->len - 1) >> PAGE_CACHE_SHIFT; |
| 754 | } else { |
| 755 | last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT; |
| 756 | } |
| 757 | |
| 758 | i = range->start >> PAGE_CACHE_SHIFT; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 759 | while (i <= last_index) { |
| 760 | if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 761 | PAGE_CACHE_SIZE, |
| 762 | range->extent_thresh, |
| 763 | &last_len, &skip, |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 764 | &defrag_end)) { |
| 765 | unsigned long next; |
| 766 | /* |
| 767 | * the should_defrag function tells us how much to skip |
| 768 | * bump our counter by the suggested amount |
| 769 | */ |
| 770 | next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 771 | i = max(i + 1, next); |
| 772 | continue; |
| 773 | } |
| 774 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 775 | if (total_read % ra_pages == 0) { |
| 776 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, |
| 777 | min(last_index, i + ra_pages - 1)); |
| 778 | } |
| 779 | total_read++; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 780 | mutex_lock(&inode->i_mutex); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 781 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 782 | BTRFS_I(inode)->force_compress = compress_type; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 783 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 784 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
| 785 | if (ret) |
| 786 | goto err_unlock; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 787 | again: |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 788 | if (inode->i_size == 0 || |
| 789 | i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) { |
| 790 | ret = 0; |
| 791 | goto err_reservations; |
| 792 | } |
| 793 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 794 | page = grab_cache_page(inode->i_mapping, i); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 795 | if (!page) { |
| 796 | ret = -ENOMEM; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 797 | goto err_reservations; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 798 | } |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 799 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 800 | if (!PageUptodate(page)) { |
| 801 | btrfs_readpage(NULL, page); |
| 802 | lock_page(page); |
| 803 | if (!PageUptodate(page)) { |
| 804 | unlock_page(page); |
| 805 | page_cache_release(page); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 806 | ret = -EIO; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 807 | goto err_reservations; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 811 | if (page->mapping != inode->i_mapping) { |
| 812 | unlock_page(page); |
| 813 | page_cache_release(page); |
| 814 | goto again; |
| 815 | } |
| 816 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 817 | wait_on_page_writeback(page); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 818 | |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 819 | if (PageDirty(page)) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 820 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 821 | goto loop_unlock; |
| 822 | } |
| 823 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 824 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 825 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 826 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 827 | |
| 828 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 829 | if (ordered) { |
| 830 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 831 | unlock_page(page); |
| 832 | page_cache_release(page); |
| 833 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 834 | btrfs_put_ordered_extent(ordered); |
| 835 | goto again; |
| 836 | } |
| 837 | set_page_extent_mapped(page); |
| 838 | |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 839 | /* |
| 840 | * this makes sure page_mkwrite is called on the |
| 841 | * page if it is dirtied again later |
| 842 | */ |
| 843 | clear_page_dirty_for_io(page); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 844 | clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, |
| 845 | page_end, EXTENT_DIRTY | EXTENT_DELALLOC | |
| 846 | EXTENT_DO_ACCOUNTING, GFP_NOFS); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 847 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 848 | btrfs_set_extent_delalloc(inode, page_start, page_end, NULL); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 849 | ClearPageChecked(page); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 850 | set_page_dirty(page); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 851 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 852 | |
| 853 | loop_unlock: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 854 | unlock_page(page); |
| 855 | page_cache_release(page); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 856 | mutex_unlock(&inode->i_mutex); |
| 857 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 858 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 859 | i++; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 860 | } |
| 861 | |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 862 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) |
| 863 | filemap_flush(inode->i_mapping); |
| 864 | |
| 865 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { |
| 866 | /* the filemap_flush will queue IO into the worker threads, but |
| 867 | * we have to make sure the IO is actually started and that |
| 868 | * ordered extents get created before we return |
| 869 | */ |
| 870 | atomic_inc(&root->fs_info->async_submit_draining); |
| 871 | while (atomic_read(&root->fs_info->nr_async_submits) || |
| 872 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 873 | wait_event(root->fs_info->async_submit_wait, |
| 874 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 875 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
| 876 | } |
| 877 | atomic_dec(&root->fs_info->async_submit_draining); |
| 878 | |
| 879 | mutex_lock(&inode->i_mutex); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 880 | BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 881 | mutex_unlock(&inode->i_mutex); |
| 882 | } |
| 883 | |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 884 | disk_super = &root->fs_info->super_copy; |
| 885 | features = btrfs_super_incompat_flags(disk_super); |
| 886 | if (range->compress_type == BTRFS_COMPRESS_LZO) { |
| 887 | features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; |
| 888 | btrfs_set_super_incompat_flags(disk_super, features); |
| 889 | } |
| 890 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 891 | return 0; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 892 | |
| 893 | err_reservations: |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 894 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
| 895 | err_unlock: |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 896 | mutex_unlock(&inode->i_mutex); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 897 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 898 | } |
| 899 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 900 | static noinline int btrfs_ioctl_resize(struct btrfs_root *root, |
| 901 | void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 902 | { |
| 903 | u64 new_size; |
| 904 | u64 old_size; |
| 905 | u64 devid = 1; |
| 906 | struct btrfs_ioctl_vol_args *vol_args; |
| 907 | struct btrfs_trans_handle *trans; |
| 908 | struct btrfs_device *device = NULL; |
| 909 | char *sizestr; |
| 910 | char *devstr = NULL; |
| 911 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 912 | int mod = 0; |
| 913 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 914 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 915 | return -EROFS; |
| 916 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 917 | if (!capable(CAP_SYS_ADMIN)) |
| 918 | return -EPERM; |
| 919 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 920 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 921 | if (IS_ERR(vol_args)) |
| 922 | return PTR_ERR(vol_args); |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 923 | |
| 924 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 925 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 926 | mutex_lock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 927 | sizestr = vol_args->name; |
| 928 | devstr = strchr(sizestr, ':'); |
| 929 | if (devstr) { |
| 930 | char *end; |
| 931 | sizestr = devstr + 1; |
| 932 | *devstr = '\0'; |
| 933 | devstr = vol_args->name; |
| 934 | devid = simple_strtoull(devstr, &end, 10); |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 935 | printk(KERN_INFO "resizing devid %llu\n", |
| 936 | (unsigned long long)devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 937 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 938 | device = btrfs_find_device(root, devid, NULL, NULL); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 939 | if (!device) { |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 940 | printk(KERN_INFO "resizer unable to find device %llu\n", |
| 941 | (unsigned long long)devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 942 | ret = -EINVAL; |
| 943 | goto out_unlock; |
| 944 | } |
| 945 | if (!strcmp(sizestr, "max")) |
| 946 | new_size = device->bdev->bd_inode->i_size; |
| 947 | else { |
| 948 | if (sizestr[0] == '-') { |
| 949 | mod = -1; |
| 950 | sizestr++; |
| 951 | } else if (sizestr[0] == '+') { |
| 952 | mod = 1; |
| 953 | sizestr++; |
| 954 | } |
Akinobu Mita | 9174846 | 2010-02-28 10:59:11 +0000 | [diff] [blame] | 955 | new_size = memparse(sizestr, NULL); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 956 | if (new_size == 0) { |
| 957 | ret = -EINVAL; |
| 958 | goto out_unlock; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | old_size = device->total_bytes; |
| 963 | |
| 964 | if (mod < 0) { |
| 965 | if (new_size > old_size) { |
| 966 | ret = -EINVAL; |
| 967 | goto out_unlock; |
| 968 | } |
| 969 | new_size = old_size - new_size; |
| 970 | } else if (mod > 0) { |
| 971 | new_size = old_size + new_size; |
| 972 | } |
| 973 | |
| 974 | if (new_size < 256 * 1024 * 1024) { |
| 975 | ret = -EINVAL; |
| 976 | goto out_unlock; |
| 977 | } |
| 978 | if (new_size > device->bdev->bd_inode->i_size) { |
| 979 | ret = -EFBIG; |
| 980 | goto out_unlock; |
| 981 | } |
| 982 | |
| 983 | do_div(new_size, root->sectorsize); |
| 984 | new_size *= root->sectorsize; |
| 985 | |
| 986 | printk(KERN_INFO "new size for %s is %llu\n", |
| 987 | device->name, (unsigned long long)new_size); |
| 988 | |
| 989 | if (new_size > old_size) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 990 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 991 | if (IS_ERR(trans)) { |
| 992 | ret = PTR_ERR(trans); |
| 993 | goto out_unlock; |
| 994 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 995 | ret = btrfs_grow_device(trans, device, new_size); |
| 996 | btrfs_commit_transaction(trans, root); |
| 997 | } else { |
| 998 | ret = btrfs_shrink_device(device, new_size); |
| 999 | } |
| 1000 | |
| 1001 | out_unlock: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1002 | mutex_unlock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1003 | kfree(vol_args); |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1007 | static noinline int btrfs_ioctl_snap_create_transid(struct file *file, |
| 1008 | char *name, |
| 1009 | unsigned long fd, |
| 1010 | int subvol, |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1011 | u64 *transid, |
| 1012 | bool readonly) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1013 | { |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1014 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1015 | struct file *src_file; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1016 | int namelen; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1017 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1018 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1019 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 1020 | return -EROFS; |
| 1021 | |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1022 | namelen = strlen(name); |
| 1023 | if (strchr(name, '/')) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1024 | ret = -EINVAL; |
| 1025 | goto out; |
| 1026 | } |
| 1027 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1028 | if (subvol) { |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1029 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1030 | NULL, transid, readonly); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1031 | } else { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1032 | struct inode *src_inode; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1033 | src_file = fget(fd); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1034 | if (!src_file) { |
| 1035 | ret = -EINVAL; |
| 1036 | goto out; |
| 1037 | } |
| 1038 | |
| 1039 | src_inode = src_file->f_path.dentry->d_inode; |
| 1040 | 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] | 1041 | printk(KERN_INFO "btrfs: Snapshot src from " |
| 1042 | "another FS\n"); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1043 | ret = -EINVAL; |
| 1044 | fput(src_file); |
| 1045 | goto out; |
| 1046 | } |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1047 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
| 1048 | BTRFS_I(src_inode)->root, |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1049 | transid, readonly); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1050 | fput(src_file); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1051 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1052 | out: |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1053 | return ret; |
| 1054 | } |
| 1055 | |
| 1056 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1057 | void __user *arg, int subvol) |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1058 | { |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1059 | struct btrfs_ioctl_vol_args *vol_args; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1060 | int ret; |
| 1061 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1062 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1063 | if (IS_ERR(vol_args)) |
| 1064 | return PTR_ERR(vol_args); |
| 1065 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1066 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1067 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1068 | vol_args->fd, subvol, |
| 1069 | NULL, false); |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1070 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1071 | kfree(vol_args); |
| 1072 | return ret; |
| 1073 | } |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1074 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1075 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
| 1076 | void __user *arg, int subvol) |
| 1077 | { |
| 1078 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
| 1079 | int ret; |
| 1080 | u64 transid = 0; |
| 1081 | u64 *ptr = NULL; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1082 | bool readonly = false; |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1083 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1084 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1085 | if (IS_ERR(vol_args)) |
| 1086 | return PTR_ERR(vol_args); |
| 1087 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; |
Sage Weil | 75eaa0e | 2010-12-10 00:36:28 +0000 | [diff] [blame] | 1088 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1089 | if (vol_args->flags & |
| 1090 | ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) { |
| 1091 | ret = -EOPNOTSUPP; |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1092 | goto out; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1093 | } |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1094 | |
| 1095 | if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) |
| 1096 | ptr = &transid; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1097 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
| 1098 | readonly = true; |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1099 | |
| 1100 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1101 | vol_args->fd, subvol, |
| 1102 | ptr, readonly); |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1103 | |
| 1104 | if (ret == 0 && ptr && |
| 1105 | copy_to_user(arg + |
| 1106 | offsetof(struct btrfs_ioctl_vol_args_v2, |
| 1107 | transid), ptr, sizeof(*ptr))) |
| 1108 | ret = -EFAULT; |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1109 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1110 | kfree(vol_args); |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1114 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
| 1115 | void __user *arg) |
| 1116 | { |
| 1117 | struct inode *inode = fdentry(file)->d_inode; |
| 1118 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1119 | int ret = 0; |
| 1120 | u64 flags = 0; |
| 1121 | |
| 1122 | if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) |
| 1123 | return -EINVAL; |
| 1124 | |
| 1125 | down_read(&root->fs_info->subvol_sem); |
| 1126 | if (btrfs_root_readonly(root)) |
| 1127 | flags |= BTRFS_SUBVOL_RDONLY; |
| 1128 | up_read(&root->fs_info->subvol_sem); |
| 1129 | |
| 1130 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 1131 | ret = -EFAULT; |
| 1132 | |
| 1133 | return ret; |
| 1134 | } |
| 1135 | |
| 1136 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, |
| 1137 | void __user *arg) |
| 1138 | { |
| 1139 | struct inode *inode = fdentry(file)->d_inode; |
| 1140 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1141 | struct btrfs_trans_handle *trans; |
| 1142 | u64 root_flags; |
| 1143 | u64 flags; |
| 1144 | int ret = 0; |
| 1145 | |
| 1146 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 1147 | return -EROFS; |
| 1148 | |
| 1149 | if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) |
| 1150 | return -EINVAL; |
| 1151 | |
| 1152 | if (copy_from_user(&flags, arg, sizeof(flags))) |
| 1153 | return -EFAULT; |
| 1154 | |
Li Zefan | b4dc2b8 | 2011-02-16 06:06:34 +0000 | [diff] [blame] | 1155 | if (flags & BTRFS_SUBVOL_CREATE_ASYNC) |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1156 | return -EINVAL; |
| 1157 | |
| 1158 | if (flags & ~BTRFS_SUBVOL_RDONLY) |
| 1159 | return -EOPNOTSUPP; |
| 1160 | |
Li Zefan | b4dc2b8 | 2011-02-16 06:06:34 +0000 | [diff] [blame] | 1161 | if (!is_owner_or_cap(inode)) |
| 1162 | return -EACCES; |
| 1163 | |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1164 | down_write(&root->fs_info->subvol_sem); |
| 1165 | |
| 1166 | /* nothing to do */ |
| 1167 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) |
| 1168 | goto out; |
| 1169 | |
| 1170 | root_flags = btrfs_root_flags(&root->root_item); |
| 1171 | if (flags & BTRFS_SUBVOL_RDONLY) |
| 1172 | btrfs_set_root_flags(&root->root_item, |
| 1173 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); |
| 1174 | else |
| 1175 | btrfs_set_root_flags(&root->root_item, |
| 1176 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); |
| 1177 | |
| 1178 | trans = btrfs_start_transaction(root, 1); |
| 1179 | if (IS_ERR(trans)) { |
| 1180 | ret = PTR_ERR(trans); |
| 1181 | goto out_reset; |
| 1182 | } |
| 1183 | |
Li Zefan | b4dc2b8 | 2011-02-16 06:06:34 +0000 | [diff] [blame] | 1184 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1185 | &root->root_key, &root->root_item); |
| 1186 | |
| 1187 | btrfs_commit_transaction(trans, root); |
| 1188 | out_reset: |
| 1189 | if (ret) |
| 1190 | btrfs_set_root_flags(&root->root_item, root_flags); |
| 1191 | out: |
| 1192 | up_write(&root->fs_info->subvol_sem); |
| 1193 | return ret; |
| 1194 | } |
| 1195 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1196 | /* |
| 1197 | * helper to check if the subvolume references other subvolumes |
| 1198 | */ |
| 1199 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
| 1200 | { |
| 1201 | struct btrfs_path *path; |
| 1202 | struct btrfs_key key; |
| 1203 | int ret; |
| 1204 | |
| 1205 | path = btrfs_alloc_path(); |
| 1206 | if (!path) |
| 1207 | return -ENOMEM; |
| 1208 | |
| 1209 | key.objectid = root->root_key.objectid; |
| 1210 | key.type = BTRFS_ROOT_REF_KEY; |
| 1211 | key.offset = (u64)-1; |
| 1212 | |
| 1213 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, |
| 1214 | &key, path, 0, 0); |
| 1215 | if (ret < 0) |
| 1216 | goto out; |
| 1217 | BUG_ON(ret == 0); |
| 1218 | |
| 1219 | ret = 0; |
| 1220 | if (path->slots[0] > 0) { |
| 1221 | path->slots[0]--; |
| 1222 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 1223 | if (key.objectid == root->root_key.objectid && |
| 1224 | key.type == BTRFS_ROOT_REF_KEY) |
| 1225 | ret = -ENOTEMPTY; |
| 1226 | } |
| 1227 | out: |
| 1228 | btrfs_free_path(path); |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1232 | static noinline int key_in_sk(struct btrfs_key *key, |
| 1233 | struct btrfs_ioctl_search_key *sk) |
| 1234 | { |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 1235 | struct btrfs_key test; |
| 1236 | int ret; |
| 1237 | |
| 1238 | test.objectid = sk->min_objectid; |
| 1239 | test.type = sk->min_type; |
| 1240 | test.offset = sk->min_offset; |
| 1241 | |
| 1242 | ret = btrfs_comp_cpu_keys(key, &test); |
| 1243 | if (ret < 0) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1244 | return 0; |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 1245 | |
| 1246 | test.objectid = sk->max_objectid; |
| 1247 | test.type = sk->max_type; |
| 1248 | test.offset = sk->max_offset; |
| 1249 | |
| 1250 | ret = btrfs_comp_cpu_keys(key, &test); |
| 1251 | if (ret > 0) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1252 | return 0; |
| 1253 | return 1; |
| 1254 | } |
| 1255 | |
| 1256 | static noinline int copy_to_sk(struct btrfs_root *root, |
| 1257 | struct btrfs_path *path, |
| 1258 | struct btrfs_key *key, |
| 1259 | struct btrfs_ioctl_search_key *sk, |
| 1260 | char *buf, |
| 1261 | unsigned long *sk_offset, |
| 1262 | int *num_found) |
| 1263 | { |
| 1264 | u64 found_transid; |
| 1265 | struct extent_buffer *leaf; |
| 1266 | struct btrfs_ioctl_search_header sh; |
| 1267 | unsigned long item_off; |
| 1268 | unsigned long item_len; |
| 1269 | int nritems; |
| 1270 | int i; |
| 1271 | int slot; |
| 1272 | int found = 0; |
| 1273 | int ret = 0; |
| 1274 | |
| 1275 | leaf = path->nodes[0]; |
| 1276 | slot = path->slots[0]; |
| 1277 | nritems = btrfs_header_nritems(leaf); |
| 1278 | |
| 1279 | if (btrfs_header_generation(leaf) > sk->max_transid) { |
| 1280 | i = nritems; |
| 1281 | goto advance_key; |
| 1282 | } |
| 1283 | found_transid = btrfs_header_generation(leaf); |
| 1284 | |
| 1285 | for (i = slot; i < nritems; i++) { |
| 1286 | item_off = btrfs_item_ptr_offset(leaf, i); |
| 1287 | item_len = btrfs_item_size_nr(leaf, i); |
| 1288 | |
| 1289 | if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) |
| 1290 | item_len = 0; |
| 1291 | |
| 1292 | if (sizeof(sh) + item_len + *sk_offset > |
| 1293 | BTRFS_SEARCH_ARGS_BUFSIZE) { |
| 1294 | ret = 1; |
| 1295 | goto overflow; |
| 1296 | } |
| 1297 | |
| 1298 | btrfs_item_key_to_cpu(leaf, key, i); |
| 1299 | if (!key_in_sk(key, sk)) |
| 1300 | continue; |
| 1301 | |
| 1302 | sh.objectid = key->objectid; |
| 1303 | sh.offset = key->offset; |
| 1304 | sh.type = key->type; |
| 1305 | sh.len = item_len; |
| 1306 | sh.transid = found_transid; |
| 1307 | |
| 1308 | /* copy search result header */ |
| 1309 | memcpy(buf + *sk_offset, &sh, sizeof(sh)); |
| 1310 | *sk_offset += sizeof(sh); |
| 1311 | |
| 1312 | if (item_len) { |
| 1313 | char *p = buf + *sk_offset; |
| 1314 | /* copy the item */ |
| 1315 | read_extent_buffer(leaf, p, |
| 1316 | item_off, item_len); |
| 1317 | *sk_offset += item_len; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1318 | } |
Chris Mason | 90fdde1 | 2010-03-18 12:14:54 -0400 | [diff] [blame] | 1319 | found++; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1320 | |
| 1321 | if (*num_found >= sk->nr_items) |
| 1322 | break; |
| 1323 | } |
| 1324 | advance_key: |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1325 | ret = 0; |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 1326 | if (key->offset < (u64)-1 && key->offset < sk->max_offset) |
| 1327 | key->offset++; |
| 1328 | else if (key->type < (u8)-1 && key->type < sk->max_type) { |
| 1329 | key->offset = 0; |
| 1330 | key->type++; |
| 1331 | } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { |
| 1332 | key->offset = 0; |
| 1333 | key->type = 0; |
| 1334 | key->objectid++; |
| 1335 | } else |
| 1336 | ret = 1; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1337 | overflow: |
| 1338 | *num_found += found; |
| 1339 | return ret; |
| 1340 | } |
| 1341 | |
| 1342 | static noinline int search_ioctl(struct inode *inode, |
| 1343 | struct btrfs_ioctl_search_args *args) |
| 1344 | { |
| 1345 | struct btrfs_root *root; |
| 1346 | struct btrfs_key key; |
| 1347 | struct btrfs_key max_key; |
| 1348 | struct btrfs_path *path; |
| 1349 | struct btrfs_ioctl_search_key *sk = &args->key; |
| 1350 | struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; |
| 1351 | int ret; |
| 1352 | int num_found = 0; |
| 1353 | unsigned long sk_offset = 0; |
| 1354 | |
| 1355 | path = btrfs_alloc_path(); |
| 1356 | if (!path) |
| 1357 | return -ENOMEM; |
| 1358 | |
| 1359 | if (sk->tree_id == 0) { |
| 1360 | /* search the root of the inode that was passed */ |
| 1361 | root = BTRFS_I(inode)->root; |
| 1362 | } else { |
| 1363 | key.objectid = sk->tree_id; |
| 1364 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 1365 | key.offset = (u64)-1; |
| 1366 | root = btrfs_read_fs_root_no_name(info, &key); |
| 1367 | if (IS_ERR(root)) { |
| 1368 | printk(KERN_ERR "could not find root %llu\n", |
| 1369 | sk->tree_id); |
| 1370 | btrfs_free_path(path); |
| 1371 | return -ENOENT; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | key.objectid = sk->min_objectid; |
| 1376 | key.type = sk->min_type; |
| 1377 | key.offset = sk->min_offset; |
| 1378 | |
| 1379 | max_key.objectid = sk->max_objectid; |
| 1380 | max_key.type = sk->max_type; |
| 1381 | max_key.offset = sk->max_offset; |
| 1382 | |
| 1383 | path->keep_locks = 1; |
| 1384 | |
| 1385 | while(1) { |
| 1386 | ret = btrfs_search_forward(root, &key, &max_key, path, 0, |
| 1387 | sk->min_transid); |
| 1388 | if (ret != 0) { |
| 1389 | if (ret > 0) |
| 1390 | ret = 0; |
| 1391 | goto err; |
| 1392 | } |
| 1393 | ret = copy_to_sk(root, path, &key, sk, args->buf, |
| 1394 | &sk_offset, &num_found); |
| 1395 | btrfs_release_path(root, path); |
| 1396 | if (ret || num_found >= sk->nr_items) |
| 1397 | break; |
| 1398 | |
| 1399 | } |
| 1400 | ret = 0; |
| 1401 | err: |
| 1402 | sk->nr_items = num_found; |
| 1403 | btrfs_free_path(path); |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
| 1407 | static noinline int btrfs_ioctl_tree_search(struct file *file, |
| 1408 | void __user *argp) |
| 1409 | { |
| 1410 | struct btrfs_ioctl_search_args *args; |
| 1411 | struct inode *inode; |
| 1412 | int ret; |
| 1413 | |
| 1414 | if (!capable(CAP_SYS_ADMIN)) |
| 1415 | return -EPERM; |
| 1416 | |
Julia Lawall | 2354d08 | 2010-10-29 15:14:18 -0400 | [diff] [blame] | 1417 | args = memdup_user(argp, sizeof(*args)); |
| 1418 | if (IS_ERR(args)) |
| 1419 | return PTR_ERR(args); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1420 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1421 | inode = fdentry(file)->d_inode; |
| 1422 | ret = search_ioctl(inode, args); |
| 1423 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 1424 | ret = -EFAULT; |
| 1425 | kfree(args); |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1429 | /* |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1430 | * Search INODE_REFs to identify path name of 'dirid' directory |
| 1431 | * in a 'tree_id' tree. and sets path name to 'name'. |
| 1432 | */ |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1433 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
| 1434 | u64 tree_id, u64 dirid, char *name) |
| 1435 | { |
| 1436 | struct btrfs_root *root; |
| 1437 | struct btrfs_key key; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1438 | char *ptr; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1439 | int ret = -1; |
| 1440 | int slot; |
| 1441 | int len; |
| 1442 | int total_len = 0; |
| 1443 | struct btrfs_inode_ref *iref; |
| 1444 | struct extent_buffer *l; |
| 1445 | struct btrfs_path *path; |
| 1446 | |
| 1447 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { |
| 1448 | name[0]='\0'; |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
| 1452 | path = btrfs_alloc_path(); |
| 1453 | if (!path) |
| 1454 | return -ENOMEM; |
| 1455 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1456 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1457 | |
| 1458 | key.objectid = tree_id; |
| 1459 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 1460 | key.offset = (u64)-1; |
| 1461 | root = btrfs_read_fs_root_no_name(info, &key); |
| 1462 | if (IS_ERR(root)) { |
| 1463 | printk(KERN_ERR "could not find root %llu\n", tree_id); |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 1464 | ret = -ENOENT; |
| 1465 | goto out; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | key.objectid = dirid; |
| 1469 | key.type = BTRFS_INODE_REF_KEY; |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 1470 | key.offset = (u64)-1; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1471 | |
| 1472 | while(1) { |
| 1473 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1474 | if (ret < 0) |
| 1475 | goto out; |
| 1476 | |
| 1477 | l = path->nodes[0]; |
| 1478 | slot = path->slots[0]; |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 1479 | if (ret > 0 && slot > 0) |
| 1480 | slot--; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1481 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1482 | |
| 1483 | if (ret > 0 && (key.objectid != dirid || |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1484 | key.type != BTRFS_INODE_REF_KEY)) { |
| 1485 | ret = -ENOENT; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1486 | goto out; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1487 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1488 | |
| 1489 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); |
| 1490 | len = btrfs_inode_ref_name_len(l, iref); |
| 1491 | ptr -= len + 1; |
| 1492 | total_len += len + 1; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1493 | if (ptr < name) |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1494 | goto out; |
| 1495 | |
| 1496 | *(ptr + len) = '/'; |
| 1497 | read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); |
| 1498 | |
| 1499 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) |
| 1500 | break; |
| 1501 | |
| 1502 | btrfs_release_path(root, path); |
| 1503 | key.objectid = key.offset; |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 1504 | key.offset = (u64)-1; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1505 | dirid = key.objectid; |
| 1506 | |
| 1507 | } |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1508 | if (ptr < name) |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1509 | goto out; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1510 | memcpy(name, ptr, total_len); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1511 | name[total_len]='\0'; |
| 1512 | ret = 0; |
| 1513 | out: |
| 1514 | btrfs_free_path(path); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1515 | return ret; |
| 1516 | } |
| 1517 | |
| 1518 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, |
| 1519 | void __user *argp) |
| 1520 | { |
| 1521 | struct btrfs_ioctl_ino_lookup_args *args; |
| 1522 | struct inode *inode; |
| 1523 | int ret; |
| 1524 | |
| 1525 | if (!capable(CAP_SYS_ADMIN)) |
| 1526 | return -EPERM; |
| 1527 | |
Julia Lawall | 2354d08 | 2010-10-29 15:14:18 -0400 | [diff] [blame] | 1528 | args = memdup_user(argp, sizeof(*args)); |
| 1529 | if (IS_ERR(args)) |
| 1530 | return PTR_ERR(args); |
Dan Carpenter | c2b9692 | 2010-03-20 11:24:15 +0000 | [diff] [blame] | 1531 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1532 | inode = fdentry(file)->d_inode; |
| 1533 | |
Chris Mason | 1b53ac4 | 2010-03-18 12:17:05 -0400 | [diff] [blame] | 1534 | if (args->treeid == 0) |
| 1535 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; |
| 1536 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 1537 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
| 1538 | args->treeid, args->objectid, |
| 1539 | args->name); |
| 1540 | |
| 1541 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 1542 | ret = -EFAULT; |
| 1543 | |
| 1544 | kfree(args); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1545 | return ret; |
| 1546 | } |
| 1547 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1548 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
| 1549 | void __user *arg) |
| 1550 | { |
| 1551 | struct dentry *parent = fdentry(file); |
| 1552 | struct dentry *dentry; |
| 1553 | struct inode *dir = parent->d_inode; |
| 1554 | struct inode *inode; |
| 1555 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1556 | struct btrfs_root *dest = NULL; |
| 1557 | struct btrfs_ioctl_vol_args *vol_args; |
| 1558 | struct btrfs_trans_handle *trans; |
| 1559 | int namelen; |
| 1560 | int ret; |
| 1561 | int err = 0; |
| 1562 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1563 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1564 | if (IS_ERR(vol_args)) |
| 1565 | return PTR_ERR(vol_args); |
| 1566 | |
| 1567 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
| 1568 | namelen = strlen(vol_args->name); |
| 1569 | if (strchr(vol_args->name, '/') || |
| 1570 | strncmp(vol_args->name, "..", namelen) == 0) { |
| 1571 | err = -EINVAL; |
| 1572 | goto out; |
| 1573 | } |
| 1574 | |
| 1575 | err = mnt_want_write(file->f_path.mnt); |
| 1576 | if (err) |
| 1577 | goto out; |
| 1578 | |
| 1579 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
| 1580 | dentry = lookup_one_len(vol_args->name, parent, namelen); |
| 1581 | if (IS_ERR(dentry)) { |
| 1582 | err = PTR_ERR(dentry); |
| 1583 | goto out_unlock_dir; |
| 1584 | } |
| 1585 | |
| 1586 | if (!dentry->d_inode) { |
| 1587 | err = -ENOENT; |
| 1588 | goto out_dput; |
| 1589 | } |
| 1590 | |
| 1591 | inode = dentry->d_inode; |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 1592 | dest = BTRFS_I(inode)->root; |
| 1593 | if (!capable(CAP_SYS_ADMIN)){ |
| 1594 | /* |
| 1595 | * Regular user. Only allow this with a special mount |
| 1596 | * option, when the user has write+exec access to the |
| 1597 | * subvol root, and when rmdir(2) would have been |
| 1598 | * allowed. |
| 1599 | * |
| 1600 | * Note that this is _not_ check that the subvol is |
| 1601 | * empty or doesn't contain data that we wouldn't |
| 1602 | * otherwise be able to delete. |
| 1603 | * |
| 1604 | * Users who want to delete empty subvols should try |
| 1605 | * rmdir(2). |
| 1606 | */ |
| 1607 | err = -EPERM; |
| 1608 | if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) |
| 1609 | goto out_dput; |
| 1610 | |
| 1611 | /* |
| 1612 | * Do not allow deletion if the parent dir is the same |
| 1613 | * as the dir to be deleted. That means the ioctl |
| 1614 | * must be called on the dentry referencing the root |
| 1615 | * of the subvol, not a random directory contained |
| 1616 | * within it. |
| 1617 | */ |
| 1618 | err = -EINVAL; |
| 1619 | if (root == dest) |
| 1620 | goto out_dput; |
| 1621 | |
| 1622 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); |
| 1623 | if (err) |
| 1624 | goto out_dput; |
| 1625 | |
| 1626 | /* check if subvolume may be deleted by a non-root user */ |
| 1627 | err = btrfs_may_delete(dir, dentry, 1); |
| 1628 | if (err) |
| 1629 | goto out_dput; |
| 1630 | } |
| 1631 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1632 | if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 1633 | err = -EINVAL; |
| 1634 | goto out_dput; |
| 1635 | } |
| 1636 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1637 | mutex_lock(&inode->i_mutex); |
| 1638 | err = d_invalidate(dentry); |
| 1639 | if (err) |
| 1640 | goto out_unlock; |
| 1641 | |
| 1642 | down_write(&root->fs_info->subvol_sem); |
| 1643 | |
| 1644 | err = may_destroy_subvol(dest); |
| 1645 | if (err) |
| 1646 | goto out_up_write; |
| 1647 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1648 | trans = btrfs_start_transaction(root, 0); |
| 1649 | if (IS_ERR(trans)) { |
| 1650 | err = PTR_ERR(trans); |
Dan Carpenter | d327099 | 2010-05-29 09:46:47 +0000 | [diff] [blame] | 1651 | goto out_up_write; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1652 | } |
| 1653 | trans->block_rsv = &root->fs_info->global_block_rsv; |
| 1654 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1655 | ret = btrfs_unlink_subvol(trans, root, dir, |
| 1656 | dest->root_key.objectid, |
| 1657 | dentry->d_name.name, |
| 1658 | dentry->d_name.len); |
| 1659 | BUG_ON(ret); |
| 1660 | |
| 1661 | btrfs_record_root_in_trans(trans, dest); |
| 1662 | |
| 1663 | memset(&dest->root_item.drop_progress, 0, |
| 1664 | sizeof(dest->root_item.drop_progress)); |
| 1665 | dest->root_item.drop_level = 0; |
| 1666 | btrfs_set_root_refs(&dest->root_item, 0); |
| 1667 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1668 | if (!xchg(&dest->orphan_item_inserted, 1)) { |
| 1669 | ret = btrfs_insert_orphan_item(trans, |
| 1670 | root->fs_info->tree_root, |
| 1671 | dest->root_key.objectid); |
| 1672 | BUG_ON(ret); |
| 1673 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1674 | |
Sage Weil | 531cb13 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1675 | ret = btrfs_end_transaction(trans, root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1676 | BUG_ON(ret); |
| 1677 | inode->i_flags |= S_DEAD; |
| 1678 | out_up_write: |
| 1679 | up_write(&root->fs_info->subvol_sem); |
| 1680 | out_unlock: |
| 1681 | mutex_unlock(&inode->i_mutex); |
| 1682 | if (!err) { |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 1683 | shrink_dcache_sb(root->fs_info->sb); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1684 | btrfs_invalidate_inodes(dest); |
| 1685 | d_delete(dentry); |
| 1686 | } |
| 1687 | out_dput: |
| 1688 | dput(dentry); |
| 1689 | out_unlock_dir: |
| 1690 | mutex_unlock(&dir->i_mutex); |
| 1691 | mnt_drop_write(file->f_path.mnt); |
| 1692 | out: |
| 1693 | kfree(vol_args); |
| 1694 | return err; |
| 1695 | } |
| 1696 | |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1697 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1698 | { |
| 1699 | struct inode *inode = fdentry(file)->d_inode; |
| 1700 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1701 | struct btrfs_ioctl_defrag_range_args *range; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1702 | int ret; |
| 1703 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1704 | if (btrfs_root_readonly(root)) |
| 1705 | return -EROFS; |
| 1706 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1707 | ret = mnt_want_write(file->f_path.mnt); |
| 1708 | if (ret) |
| 1709 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1710 | |
| 1711 | switch (inode->i_mode & S_IFMT) { |
| 1712 | case S_IFDIR: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1713 | if (!capable(CAP_SYS_ADMIN)) { |
| 1714 | ret = -EPERM; |
| 1715 | goto out; |
| 1716 | } |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1717 | ret = btrfs_defrag_root(root, 0); |
| 1718 | if (ret) |
| 1719 | goto out; |
| 1720 | ret = btrfs_defrag_root(root->fs_info->extent_root, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1721 | break; |
| 1722 | case S_IFREG: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1723 | if (!(file->f_mode & FMODE_WRITE)) { |
| 1724 | ret = -EINVAL; |
| 1725 | goto out; |
| 1726 | } |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1727 | |
| 1728 | range = kzalloc(sizeof(*range), GFP_KERNEL); |
| 1729 | if (!range) { |
| 1730 | ret = -ENOMEM; |
| 1731 | goto out; |
| 1732 | } |
| 1733 | |
| 1734 | if (argp) { |
| 1735 | if (copy_from_user(range, argp, |
| 1736 | sizeof(*range))) { |
| 1737 | ret = -EFAULT; |
| 1738 | kfree(range); |
Dan Carpenter | 683be16 | 2010-03-20 11:24:48 +0000 | [diff] [blame] | 1739 | goto out; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1740 | } |
| 1741 | /* compression requires us to start the IO */ |
| 1742 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { |
| 1743 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; |
| 1744 | range->extent_thresh = (u32)-1; |
| 1745 | } |
| 1746 | } else { |
| 1747 | /* the rest are all set to zero by kzalloc */ |
| 1748 | range->len = (u64)-1; |
| 1749 | } |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1750 | ret = btrfs_defrag_file(file, range); |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1751 | kfree(range); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1752 | break; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 1753 | default: |
| 1754 | ret = -EINVAL; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1755 | } |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1756 | out: |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 1757 | mnt_drop_write(file->f_path.mnt); |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1758 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1759 | } |
| 1760 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1761 | 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] | 1762 | { |
| 1763 | struct btrfs_ioctl_vol_args *vol_args; |
| 1764 | int ret; |
| 1765 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1766 | if (!capable(CAP_SYS_ADMIN)) |
| 1767 | return -EPERM; |
| 1768 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 1769 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1770 | if (IS_ERR(vol_args)) |
| 1771 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1772 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 1773 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1774 | ret = btrfs_init_new_device(root, vol_args->name); |
| 1775 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1776 | kfree(vol_args); |
| 1777 | return ret; |
| 1778 | } |
| 1779 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1780 | 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] | 1781 | { |
| 1782 | struct btrfs_ioctl_vol_args *vol_args; |
| 1783 | int ret; |
| 1784 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1785 | if (!capable(CAP_SYS_ADMIN)) |
| 1786 | return -EPERM; |
| 1787 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1788 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 1789 | return -EROFS; |
| 1790 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 1791 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1792 | if (IS_ERR(vol_args)) |
| 1793 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1794 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 1795 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1796 | ret = btrfs_rm_device(root, vol_args->name); |
| 1797 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1798 | kfree(vol_args); |
| 1799 | return ret; |
| 1800 | } |
| 1801 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1802 | static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, |
| 1803 | u64 off, u64 olen, u64 destoff) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1804 | { |
| 1805 | struct inode *inode = fdentry(file)->d_inode; |
| 1806 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1807 | struct file *src_file; |
| 1808 | struct inode *src; |
| 1809 | struct btrfs_trans_handle *trans; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1810 | struct btrfs_path *path; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1811 | struct extent_buffer *leaf; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1812 | char *buf; |
| 1813 | struct btrfs_key key; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1814 | u32 nritems; |
| 1815 | int slot; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1816 | int ret; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1817 | u64 len = olen; |
| 1818 | u64 bs = root->fs_info->sb->s_blocksize; |
| 1819 | u64 hint_byte; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1820 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1821 | /* |
| 1822 | * TODO: |
| 1823 | * - split compressed inline extents. annoying: we need to |
| 1824 | * decompress into destination's address_space (the file offset |
| 1825 | * may change, so source mapping won't do), then recompress (or |
| 1826 | * otherwise reinsert) a subrange. |
| 1827 | * - allow ranges within the same file to be cloned (provided |
| 1828 | * they don't overlap)? |
| 1829 | */ |
| 1830 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1831 | /* the destination must be opened for writing */ |
Dan Rosenberg | 2ebc346 | 2010-07-19 16:58:20 -0400 | [diff] [blame] | 1832 | if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1833 | return -EINVAL; |
| 1834 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1835 | if (btrfs_root_readonly(root)) |
| 1836 | return -EROFS; |
| 1837 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1838 | ret = mnt_want_write(file->f_path.mnt); |
| 1839 | if (ret) |
| 1840 | return ret; |
| 1841 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1842 | src_file = fget(srcfd); |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 1843 | if (!src_file) { |
| 1844 | ret = -EBADF; |
| 1845 | goto out_drop_write; |
| 1846 | } |
Dan Rosenberg | 5dc6416 | 2010-05-15 11:27:37 -0400 | [diff] [blame] | 1847 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1848 | src = src_file->f_dentry->d_inode; |
| 1849 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1850 | ret = -EINVAL; |
| 1851 | if (src == inode) |
| 1852 | goto out_fput; |
| 1853 | |
Dan Rosenberg | 5dc6416 | 2010-05-15 11:27:37 -0400 | [diff] [blame] | 1854 | /* the src must be open for reading */ |
| 1855 | if (!(src_file->f_mode & FMODE_READ)) |
| 1856 | goto out_fput; |
| 1857 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1858 | ret = -EISDIR; |
| 1859 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1860 | goto out_fput; |
| 1861 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1862 | ret = -EXDEV; |
| 1863 | if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) |
| 1864 | goto out_fput; |
| 1865 | |
| 1866 | ret = -ENOMEM; |
| 1867 | buf = vmalloc(btrfs_level_size(root, 0)); |
| 1868 | if (!buf) |
| 1869 | goto out_fput; |
| 1870 | |
| 1871 | path = btrfs_alloc_path(); |
| 1872 | if (!path) { |
| 1873 | vfree(buf); |
| 1874 | goto out_fput; |
| 1875 | } |
| 1876 | path->reada = 2; |
| 1877 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1878 | if (inode < src) { |
Sage Weil | fccdae4 | 2010-10-29 15:37:33 -0400 | [diff] [blame] | 1879 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); |
| 1880 | mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1881 | } else { |
Sage Weil | fccdae4 | 2010-10-29 15:37:33 -0400 | [diff] [blame] | 1882 | mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); |
| 1883 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1884 | } |
| 1885 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1886 | /* determine range to clone */ |
| 1887 | ret = -EINVAL; |
Dan Rosenberg | 2ebc346 | 2010-07-19 16:58:20 -0400 | [diff] [blame] | 1888 | if (off + len > src->i_size || off + len < off) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1889 | goto out_unlock; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1890 | if (len == 0) |
| 1891 | olen = len = src->i_size - off; |
| 1892 | /* if we extend to eof, continue to block boundary */ |
| 1893 | if (off + len == src->i_size) |
Li Zefan | 2a6b8da | 2010-11-19 01:36:10 +0000 | [diff] [blame] | 1894 | len = ALIGN(src->i_size, bs) - off; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1895 | |
| 1896 | /* verify the end result is block aligned */ |
Li Zefan | 2a6b8da | 2010-11-19 01:36:10 +0000 | [diff] [blame] | 1897 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || |
| 1898 | !IS_ALIGNED(destoff, bs)) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1899 | goto out_unlock; |
| 1900 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1901 | /* do any pending delalloc/csum calc on src, one way or |
| 1902 | another, and lock file content */ |
| 1903 | while (1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1904 | struct btrfs_ordered_extent *ordered; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1905 | lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
Sage Weil | 9a01919 | 2010-10-29 15:37:33 -0400 | [diff] [blame] | 1906 | ordered = btrfs_lookup_first_ordered_extent(src, off+len); |
| 1907 | if (!ordered && |
| 1908 | !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len, |
| 1909 | EXTENT_DELALLOC, 0, NULL)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1910 | break; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1911 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1912 | if (ordered) |
| 1913 | btrfs_put_ordered_extent(ordered); |
Sage Weil | 9a01919 | 2010-10-29 15:37:33 -0400 | [diff] [blame] | 1914 | btrfs_wait_ordered_range(src, off, len); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1915 | } |
| 1916 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1917 | /* clone data */ |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1918 | key.objectid = src->i_ino; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1919 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 1920 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1921 | |
| 1922 | while (1) { |
| 1923 | /* |
| 1924 | * note the key will change type as we walk through the |
| 1925 | * tree. |
| 1926 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1927 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1928 | if (ret < 0) |
| 1929 | goto out; |
| 1930 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1931 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 1932 | if (path->slots[0] >= nritems) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1933 | ret = btrfs_next_leaf(root, path); |
| 1934 | if (ret < 0) |
| 1935 | goto out; |
| 1936 | if (ret > 0) |
| 1937 | break; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1938 | nritems = btrfs_header_nritems(path->nodes[0]); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1939 | } |
| 1940 | leaf = path->nodes[0]; |
| 1941 | slot = path->slots[0]; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1942 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1943 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1944 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1945 | key.objectid != src->i_ino) |
| 1946 | break; |
| 1947 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1948 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
| 1949 | struct btrfs_file_extent_item *extent; |
| 1950 | int type; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1951 | u32 size; |
| 1952 | struct btrfs_key new_key; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1953 | u64 disko = 0, diskl = 0; |
| 1954 | u64 datao = 0, datal = 0; |
| 1955 | u8 comp; |
Sage Weil | b5384d4 | 2010-06-12 22:31:14 +0000 | [diff] [blame] | 1956 | u64 endoff; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1957 | |
| 1958 | size = btrfs_item_size_nr(leaf, slot); |
| 1959 | read_extent_buffer(leaf, buf, |
| 1960 | btrfs_item_ptr_offset(leaf, slot), |
| 1961 | size); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1962 | |
| 1963 | extent = btrfs_item_ptr(leaf, slot, |
| 1964 | struct btrfs_file_extent_item); |
| 1965 | comp = btrfs_file_extent_compression(leaf, extent); |
| 1966 | type = btrfs_file_extent_type(leaf, extent); |
Chris Mason | c8a894d | 2009-06-27 21:07:03 -0400 | [diff] [blame] | 1967 | if (type == BTRFS_FILE_EXTENT_REG || |
| 1968 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1969 | disko = btrfs_file_extent_disk_bytenr(leaf, |
| 1970 | extent); |
| 1971 | diskl = btrfs_file_extent_disk_num_bytes(leaf, |
| 1972 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1973 | datao = btrfs_file_extent_offset(leaf, extent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1974 | datal = btrfs_file_extent_num_bytes(leaf, |
| 1975 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1976 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 1977 | /* take upper bound, may be compressed */ |
| 1978 | datal = btrfs_file_extent_ram_bytes(leaf, |
| 1979 | extent); |
| 1980 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1981 | btrfs_release_path(root, path); |
| 1982 | |
Sage Weil | 050006a | 2010-10-29 15:37:33 -0400 | [diff] [blame] | 1983 | if (key.offset + datal <= off || |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1984 | key.offset >= off+len) |
| 1985 | goto next; |
| 1986 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1987 | memcpy(&new_key, &key, sizeof(new_key)); |
| 1988 | new_key.objectid = inode->i_ino; |
Li Zefan | 4d728ec | 2011-01-26 14:10:43 +0800 | [diff] [blame] | 1989 | if (off <= key.offset) |
| 1990 | new_key.offset = key.offset + destoff - off; |
| 1991 | else |
| 1992 | new_key.offset = destoff; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1993 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1994 | trans = btrfs_start_transaction(root, 1); |
| 1995 | if (IS_ERR(trans)) { |
| 1996 | ret = PTR_ERR(trans); |
| 1997 | goto out; |
| 1998 | } |
| 1999 | |
Chris Mason | c8a894d | 2009-06-27 21:07:03 -0400 | [diff] [blame] | 2000 | if (type == BTRFS_FILE_EXTENT_REG || |
| 2001 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2002 | if (off > key.offset) { |
| 2003 | datao += off - key.offset; |
| 2004 | datal -= off - key.offset; |
| 2005 | } |
| 2006 | |
| 2007 | if (key.offset + datal > off + len) |
| 2008 | datal = off + len - key.offset; |
| 2009 | |
| 2010 | ret = btrfs_drop_extents(trans, inode, |
| 2011 | new_key.offset, |
| 2012 | new_key.offset + datal, |
| 2013 | &hint_byte, 1); |
| 2014 | BUG_ON(ret); |
| 2015 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2016 | ret = btrfs_insert_empty_item(trans, root, path, |
| 2017 | &new_key, size); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2018 | BUG_ON(ret); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2019 | |
| 2020 | leaf = path->nodes[0]; |
| 2021 | slot = path->slots[0]; |
| 2022 | write_extent_buffer(leaf, buf, |
| 2023 | btrfs_item_ptr_offset(leaf, slot), |
| 2024 | size); |
| 2025 | |
| 2026 | extent = btrfs_item_ptr(leaf, slot, |
| 2027 | struct btrfs_file_extent_item); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2028 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2029 | /* disko == 0 means it's a hole */ |
| 2030 | if (!disko) |
| 2031 | datao = 0; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2032 | |
| 2033 | btrfs_set_file_extent_offset(leaf, extent, |
| 2034 | datao); |
| 2035 | btrfs_set_file_extent_num_bytes(leaf, extent, |
| 2036 | datal); |
| 2037 | if (disko) { |
| 2038 | inode_add_bytes(inode, datal); |
| 2039 | ret = btrfs_inc_extent_ref(trans, root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2040 | disko, diskl, 0, |
| 2041 | root->root_key.objectid, |
| 2042 | inode->i_ino, |
| 2043 | new_key.offset - datao); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2044 | BUG_ON(ret); |
| 2045 | } |
| 2046 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 2047 | u64 skip = 0; |
| 2048 | u64 trim = 0; |
| 2049 | if (off > key.offset) { |
| 2050 | skip = off - key.offset; |
| 2051 | new_key.offset += skip; |
| 2052 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2053 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2054 | if (key.offset + datal > off+len) |
| 2055 | trim = key.offset + datal - (off+len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2056 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2057 | if (comp && (skip || trim)) { |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2058 | ret = -EINVAL; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2059 | btrfs_end_transaction(trans, root); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2060 | goto out; |
| 2061 | } |
| 2062 | size -= skip + trim; |
| 2063 | datal -= skip + trim; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2064 | |
| 2065 | ret = btrfs_drop_extents(trans, inode, |
| 2066 | new_key.offset, |
| 2067 | new_key.offset + datal, |
| 2068 | &hint_byte, 1); |
| 2069 | BUG_ON(ret); |
| 2070 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2071 | ret = btrfs_insert_empty_item(trans, root, path, |
| 2072 | &new_key, size); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2073 | BUG_ON(ret); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2074 | |
| 2075 | if (skip) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2076 | u32 start = |
| 2077 | btrfs_file_extent_calc_inline_size(0); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2078 | memmove(buf+start, buf+start+skip, |
| 2079 | datal); |
| 2080 | } |
| 2081 | |
| 2082 | leaf = path->nodes[0]; |
| 2083 | slot = path->slots[0]; |
| 2084 | write_extent_buffer(leaf, buf, |
| 2085 | btrfs_item_ptr_offset(leaf, slot), |
| 2086 | size); |
| 2087 | inode_add_bytes(inode, datal); |
| 2088 | } |
| 2089 | |
| 2090 | btrfs_mark_buffer_dirty(leaf); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2091 | btrfs_release_path(root, path); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2092 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2093 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Sage Weil | b5384d4 | 2010-06-12 22:31:14 +0000 | [diff] [blame] | 2094 | |
| 2095 | /* |
| 2096 | * we round up to the block size at eof when |
| 2097 | * determining which extents to clone above, |
| 2098 | * but shouldn't round up the file size |
| 2099 | */ |
| 2100 | endoff = new_key.offset + datal; |
Li Zefan | 5f3888f | 2010-11-19 01:36:34 +0000 | [diff] [blame] | 2101 | if (endoff > destoff+olen) |
| 2102 | endoff = destoff+olen; |
Sage Weil | b5384d4 | 2010-06-12 22:31:14 +0000 | [diff] [blame] | 2103 | if (endoff > inode->i_size) |
| 2104 | btrfs_i_size_write(inode, endoff); |
| 2105 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2106 | BTRFS_I(inode)->flags = BTRFS_I(src)->flags; |
| 2107 | ret = btrfs_update_inode(trans, root, inode); |
| 2108 | BUG_ON(ret); |
| 2109 | btrfs_end_transaction(trans, root); |
| 2110 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2111 | next: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2112 | btrfs_release_path(root, path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2113 | key.offset++; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2114 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2115 | ret = 0; |
| 2116 | out: |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2117 | btrfs_release_path(root, path); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2118 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2119 | out_unlock: |
| 2120 | mutex_unlock(&src->i_mutex); |
| 2121 | mutex_unlock(&inode->i_mutex); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 2122 | vfree(buf); |
| 2123 | btrfs_free_path(path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2124 | out_fput: |
| 2125 | fput(src_file); |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 2126 | out_drop_write: |
| 2127 | mnt_drop_write(file->f_path.mnt); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2128 | return ret; |
| 2129 | } |
| 2130 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 2131 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2132 | { |
| 2133 | struct btrfs_ioctl_clone_range_args args; |
| 2134 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 2135 | if (copy_from_user(&args, argp, sizeof(args))) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2136 | return -EFAULT; |
| 2137 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, |
| 2138 | args.src_length, args.dest_offset); |
| 2139 | } |
| 2140 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2141 | /* |
| 2142 | * there are many ways the trans_start and trans_end ioctls can lead |
| 2143 | * to deadlocks. They should only be used by applications that |
| 2144 | * basically own the machine, and have a very in depth understanding |
| 2145 | * of all the possible deadlocks and enospc problems. |
| 2146 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2147 | static long btrfs_ioctl_trans_start(struct file *file) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2148 | { |
| 2149 | struct inode *inode = fdentry(file)->d_inode; |
| 2150 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2151 | struct btrfs_trans_handle *trans; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 2152 | int ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2153 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 2154 | ret = -EPERM; |
Christoph Hellwig | df5b552 | 2008-06-11 21:53:58 -0400 | [diff] [blame] | 2155 | if (!capable(CAP_SYS_ADMIN)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2156 | goto out; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 2157 | |
| 2158 | ret = -EINPROGRESS; |
| 2159 | if (file->private_data) |
| 2160 | goto out; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 2161 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 2162 | ret = -EROFS; |
| 2163 | if (btrfs_root_readonly(root)) |
| 2164 | goto out; |
| 2165 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2166 | ret = mnt_want_write(file->f_path.mnt); |
| 2167 | if (ret) |
| 2168 | goto out; |
| 2169 | |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 2170 | mutex_lock(&root->fs_info->trans_mutex); |
| 2171 | root->fs_info->open_ioctl_trans++; |
| 2172 | mutex_unlock(&root->fs_info->trans_mutex); |
| 2173 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 2174 | ret = -ENOMEM; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 2175 | trans = btrfs_start_ioctl_transaction(root, 0); |
Tsutomu Itoh | abd30bb | 2011-01-24 00:57:10 +0000 | [diff] [blame] | 2176 | if (IS_ERR(trans)) |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 2177 | goto out_drop; |
| 2178 | |
| 2179 | file->private_data = trans; |
| 2180 | return 0; |
| 2181 | |
| 2182 | out_drop: |
| 2183 | mutex_lock(&root->fs_info->trans_mutex); |
| 2184 | root->fs_info->open_ioctl_trans--; |
| 2185 | mutex_unlock(&root->fs_info->trans_mutex); |
| 2186 | mnt_drop_write(file->f_path.mnt); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2187 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2188 | return ret; |
| 2189 | } |
| 2190 | |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 2191 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
| 2192 | { |
| 2193 | struct inode *inode = fdentry(file)->d_inode; |
| 2194 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2195 | struct btrfs_root *new_root; |
| 2196 | struct btrfs_dir_item *di; |
| 2197 | struct btrfs_trans_handle *trans; |
| 2198 | struct btrfs_path *path; |
| 2199 | struct btrfs_key location; |
| 2200 | struct btrfs_disk_key disk_key; |
| 2201 | struct btrfs_super_block *disk_super; |
| 2202 | u64 features; |
| 2203 | u64 objectid = 0; |
| 2204 | u64 dir_id; |
| 2205 | |
| 2206 | if (!capable(CAP_SYS_ADMIN)) |
| 2207 | return -EPERM; |
| 2208 | |
| 2209 | if (copy_from_user(&objectid, argp, sizeof(objectid))) |
| 2210 | return -EFAULT; |
| 2211 | |
| 2212 | if (!objectid) |
| 2213 | objectid = root->root_key.objectid; |
| 2214 | |
| 2215 | location.objectid = objectid; |
| 2216 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 2217 | location.offset = (u64)-1; |
| 2218 | |
| 2219 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 2220 | if (IS_ERR(new_root)) |
| 2221 | return PTR_ERR(new_root); |
| 2222 | |
| 2223 | if (btrfs_root_refs(&new_root->root_item) == 0) |
| 2224 | return -ENOENT; |
| 2225 | |
| 2226 | path = btrfs_alloc_path(); |
| 2227 | if (!path) |
| 2228 | return -ENOMEM; |
| 2229 | path->leave_spinning = 1; |
| 2230 | |
| 2231 | trans = btrfs_start_transaction(root, 1); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2232 | if (IS_ERR(trans)) { |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 2233 | btrfs_free_path(path); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2234 | return PTR_ERR(trans); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | dir_id = btrfs_super_root_dir(&root->fs_info->super_copy); |
| 2238 | di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, |
| 2239 | dir_id, "default", 7, 1); |
Dan Carpenter | cf1e99a | 2010-05-29 09:47:24 +0000 | [diff] [blame] | 2240 | if (IS_ERR_OR_NULL(di)) { |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 2241 | btrfs_free_path(path); |
| 2242 | btrfs_end_transaction(trans, root); |
| 2243 | printk(KERN_ERR "Umm, you don't have the default dir item, " |
| 2244 | "this isn't going to work\n"); |
| 2245 | return -ENOENT; |
| 2246 | } |
| 2247 | |
| 2248 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); |
| 2249 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); |
| 2250 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 2251 | btrfs_free_path(path); |
| 2252 | |
| 2253 | disk_super = &root->fs_info->super_copy; |
| 2254 | features = btrfs_super_incompat_flags(disk_super); |
| 2255 | if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) { |
| 2256 | features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL; |
| 2257 | btrfs_set_super_incompat_flags(disk_super, features); |
| 2258 | } |
| 2259 | btrfs_end_transaction(trans, root); |
| 2260 | |
| 2261 | return 0; |
| 2262 | } |
| 2263 | |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2264 | static void get_block_group_info(struct list_head *groups_list, |
| 2265 | struct btrfs_ioctl_space_info *space) |
| 2266 | { |
| 2267 | struct btrfs_block_group_cache *block_group; |
| 2268 | |
| 2269 | space->total_bytes = 0; |
| 2270 | space->used_bytes = 0; |
| 2271 | space->flags = 0; |
| 2272 | list_for_each_entry(block_group, groups_list, list) { |
| 2273 | space->flags = block_group->flags; |
| 2274 | space->total_bytes += block_group->key.offset; |
| 2275 | space->used_bytes += |
| 2276 | btrfs_block_group_used(&block_group->item); |
| 2277 | } |
| 2278 | } |
| 2279 | |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 2280 | long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) |
| 2281 | { |
| 2282 | struct btrfs_ioctl_space_args space_args; |
| 2283 | struct btrfs_ioctl_space_info space; |
| 2284 | struct btrfs_ioctl_space_info *dest; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 2285 | struct btrfs_ioctl_space_info *dest_orig; |
| 2286 | struct btrfs_ioctl_space_info *user_dest; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 2287 | struct btrfs_space_info *info; |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2288 | u64 types[] = {BTRFS_BLOCK_GROUP_DATA, |
| 2289 | BTRFS_BLOCK_GROUP_SYSTEM, |
| 2290 | BTRFS_BLOCK_GROUP_METADATA, |
| 2291 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; |
| 2292 | int num_types = 4; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 2293 | int alloc_size; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 2294 | int ret = 0; |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 2295 | u64 slot_count = 0; |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2296 | int i, c; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 2297 | |
| 2298 | if (copy_from_user(&space_args, |
| 2299 | (struct btrfs_ioctl_space_args __user *)arg, |
| 2300 | sizeof(space_args))) |
| 2301 | return -EFAULT; |
| 2302 | |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2303 | for (i = 0; i < num_types; i++) { |
| 2304 | struct btrfs_space_info *tmp; |
| 2305 | |
| 2306 | info = NULL; |
| 2307 | rcu_read_lock(); |
| 2308 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, |
| 2309 | list) { |
| 2310 | if (tmp->flags == types[i]) { |
| 2311 | info = tmp; |
| 2312 | break; |
| 2313 | } |
| 2314 | } |
| 2315 | rcu_read_unlock(); |
| 2316 | |
| 2317 | if (!info) |
| 2318 | continue; |
| 2319 | |
| 2320 | down_read(&info->groups_sem); |
| 2321 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 2322 | if (!list_empty(&info->block_groups[c])) |
| 2323 | slot_count++; |
| 2324 | } |
| 2325 | up_read(&info->groups_sem); |
| 2326 | } |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 2327 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 2328 | /* space_slots == 0 means they are asking for a count */ |
| 2329 | if (space_args.space_slots == 0) { |
| 2330 | space_args.total_spaces = slot_count; |
| 2331 | goto out; |
| 2332 | } |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2333 | |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 2334 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2335 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 2336 | alloc_size = sizeof(*dest) * slot_count; |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2337 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 2338 | /* we generally have at most 6 or so space infos, one for each raid |
| 2339 | * level. So, a whole page should be more than enough for everyone |
| 2340 | */ |
| 2341 | if (alloc_size > PAGE_CACHE_SIZE) |
| 2342 | return -ENOMEM; |
| 2343 | |
| 2344 | space_args.total_spaces = 0; |
| 2345 | dest = kmalloc(alloc_size, GFP_NOFS); |
| 2346 | if (!dest) |
| 2347 | return -ENOMEM; |
| 2348 | dest_orig = dest; |
| 2349 | |
| 2350 | /* now we have a buffer to copy into */ |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2351 | for (i = 0; i < num_types; i++) { |
| 2352 | struct btrfs_space_info *tmp; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 2353 | |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 2354 | if (!slot_count) |
| 2355 | break; |
| 2356 | |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2357 | info = NULL; |
| 2358 | rcu_read_lock(); |
| 2359 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, |
| 2360 | list) { |
| 2361 | if (tmp->flags == types[i]) { |
| 2362 | info = tmp; |
| 2363 | break; |
| 2364 | } |
| 2365 | } |
| 2366 | rcu_read_unlock(); |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 2367 | |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2368 | if (!info) |
| 2369 | continue; |
| 2370 | down_read(&info->groups_sem); |
| 2371 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 2372 | if (!list_empty(&info->block_groups[c])) { |
| 2373 | get_block_group_info(&info->block_groups[c], |
| 2374 | &space); |
| 2375 | memcpy(dest, &space, sizeof(space)); |
| 2376 | dest++; |
| 2377 | space_args.total_spaces++; |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 2378 | slot_count--; |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2379 | } |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 2380 | if (!slot_count) |
| 2381 | break; |
Josef Bacik | bf5fc09 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 2382 | } |
| 2383 | up_read(&info->groups_sem); |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 2384 | } |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 2385 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 2386 | user_dest = (struct btrfs_ioctl_space_info *) |
| 2387 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
| 2388 | |
| 2389 | if (copy_to_user(user_dest, dest_orig, alloc_size)) |
| 2390 | ret = -EFAULT; |
| 2391 | |
| 2392 | kfree(dest_orig); |
| 2393 | out: |
| 2394 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 2395 | ret = -EFAULT; |
| 2396 | |
| 2397 | return ret; |
| 2398 | } |
| 2399 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2400 | /* |
| 2401 | * there are many ways the trans_start and trans_end ioctls can lead |
| 2402 | * to deadlocks. They should only be used by applications that |
| 2403 | * basically own the machine, and have a very in depth understanding |
| 2404 | * of all the possible deadlocks and enospc problems. |
| 2405 | */ |
| 2406 | long btrfs_ioctl_trans_end(struct file *file) |
| 2407 | { |
| 2408 | struct inode *inode = fdentry(file)->d_inode; |
| 2409 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2410 | struct btrfs_trans_handle *trans; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2411 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2412 | trans = file->private_data; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 2413 | if (!trans) |
| 2414 | return -EINVAL; |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 2415 | file->private_data = NULL; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 2416 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 2417 | btrfs_end_transaction(trans, root); |
| 2418 | |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 2419 | mutex_lock(&root->fs_info->trans_mutex); |
| 2420 | root->fs_info->open_ioctl_trans--; |
| 2421 | mutex_unlock(&root->fs_info->trans_mutex); |
| 2422 | |
Sage Weil | cfc8ea8 | 2008-12-11 16:30:06 -0500 | [diff] [blame] | 2423 | mnt_drop_write(file->f_path.mnt); |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 2424 | return 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2425 | } |
| 2426 | |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 2427 | static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp) |
| 2428 | { |
| 2429 | struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; |
| 2430 | struct btrfs_trans_handle *trans; |
| 2431 | u64 transid; |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 2432 | int ret; |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 2433 | |
| 2434 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2435 | if (IS_ERR(trans)) |
| 2436 | return PTR_ERR(trans); |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 2437 | transid = trans->transid; |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 2438 | ret = btrfs_commit_transaction_async(trans, root, 0); |
| 2439 | if (ret) |
| 2440 | return ret; |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 2441 | |
| 2442 | if (argp) |
| 2443 | if (copy_to_user(argp, &transid, sizeof(transid))) |
| 2444 | return -EFAULT; |
| 2445 | return 0; |
| 2446 | } |
| 2447 | |
| 2448 | static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp) |
| 2449 | { |
| 2450 | struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; |
| 2451 | u64 transid; |
| 2452 | |
| 2453 | if (argp) { |
| 2454 | if (copy_from_user(&transid, argp, sizeof(transid))) |
| 2455 | return -EFAULT; |
| 2456 | } else { |
| 2457 | transid = 0; /* current trans */ |
| 2458 | } |
| 2459 | return btrfs_wait_for_commit(root, transid); |
| 2460 | } |
| 2461 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2462 | long btrfs_ioctl(struct file *file, unsigned int |
| 2463 | cmd, unsigned long arg) |
| 2464 | { |
| 2465 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 2466 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2467 | |
| 2468 | switch (cmd) { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 2469 | case FS_IOC_GETFLAGS: |
| 2470 | return btrfs_ioctl_getflags(file, argp); |
| 2471 | case FS_IOC_SETFLAGS: |
| 2472 | return btrfs_ioctl_setflags(file, argp); |
| 2473 | case FS_IOC_GETVERSION: |
| 2474 | return btrfs_ioctl_getversion(file, argp); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2475 | case FITRIM: |
| 2476 | return btrfs_ioctl_fitrim(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2477 | case BTRFS_IOC_SNAP_CREATE: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 2478 | return btrfs_ioctl_snap_create(file, argp, 0); |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 2479 | case BTRFS_IOC_SNAP_CREATE_V2: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 2480 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 2481 | case BTRFS_IOC_SUBVOL_CREATE: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 2482 | return btrfs_ioctl_snap_create(file, argp, 1); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2483 | case BTRFS_IOC_SNAP_DESTROY: |
| 2484 | return btrfs_ioctl_snap_destroy(file, argp); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2485 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
| 2486 | return btrfs_ioctl_subvol_getflags(file, argp); |
| 2487 | case BTRFS_IOC_SUBVOL_SETFLAGS: |
| 2488 | return btrfs_ioctl_subvol_setflags(file, argp); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 2489 | case BTRFS_IOC_DEFAULT_SUBVOL: |
| 2490 | return btrfs_ioctl_default_subvol(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2491 | case BTRFS_IOC_DEFRAG: |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 2492 | return btrfs_ioctl_defrag(file, NULL); |
| 2493 | case BTRFS_IOC_DEFRAG_RANGE: |
| 2494 | return btrfs_ioctl_defrag(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2495 | case BTRFS_IOC_RESIZE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 2496 | return btrfs_ioctl_resize(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2497 | case BTRFS_IOC_ADD_DEV: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 2498 | return btrfs_ioctl_add_dev(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2499 | case BTRFS_IOC_RM_DEV: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 2500 | return btrfs_ioctl_rm_dev(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2501 | case BTRFS_IOC_BALANCE: |
| 2502 | return btrfs_balance(root->fs_info->dev_root); |
| 2503 | case BTRFS_IOC_CLONE: |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 2504 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
| 2505 | case BTRFS_IOC_CLONE_RANGE: |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 2506 | return btrfs_ioctl_clone_range(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2507 | case BTRFS_IOC_TRANS_START: |
| 2508 | return btrfs_ioctl_trans_start(file); |
| 2509 | case BTRFS_IOC_TRANS_END: |
| 2510 | return btrfs_ioctl_trans_end(file); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2511 | case BTRFS_IOC_TREE_SEARCH: |
| 2512 | return btrfs_ioctl_tree_search(file, argp); |
| 2513 | case BTRFS_IOC_INO_LOOKUP: |
| 2514 | return btrfs_ioctl_ino_lookup(file, argp); |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 2515 | case BTRFS_IOC_SPACE_INFO: |
| 2516 | return btrfs_ioctl_space_info(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2517 | case BTRFS_IOC_SYNC: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2518 | btrfs_sync_fs(file->f_dentry->d_sb, 1); |
| 2519 | return 0; |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 2520 | case BTRFS_IOC_START_SYNC: |
| 2521 | return btrfs_ioctl_start_sync(file, argp); |
| 2522 | case BTRFS_IOC_WAIT_SYNC: |
| 2523 | return btrfs_ioctl_wait_sync(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | return -ENOTTY; |
| 2527 | } |