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