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