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