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> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 42 | #include "compat.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 43 | #include "ctree.h" |
| 44 | #include "disk-io.h" |
| 45 | #include "transaction.h" |
| 46 | #include "btrfs_inode.h" |
| 47 | #include "ioctl.h" |
| 48 | #include "print-tree.h" |
| 49 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 50 | #include "locking.h" |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 51 | #include "ctree.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, |
| 227 | char *name, int namelen) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 228 | { |
| 229 | struct btrfs_trans_handle *trans; |
| 230 | struct btrfs_key key; |
| 231 | struct btrfs_root_item root_item; |
| 232 | struct btrfs_inode_item *inode_item; |
| 233 | struct extent_buffer *leaf; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 234 | struct btrfs_root *new_root; |
| 235 | struct inode *dir = dentry->d_parent->d_inode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 236 | int ret; |
| 237 | int err; |
| 238 | u64 objectid; |
| 239 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 240 | u64 index = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 241 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 242 | /* |
| 243 | * 1 - inode item |
| 244 | * 2 - refs |
| 245 | * 1 - root item |
| 246 | * 2 - dir items |
| 247 | */ |
| 248 | ret = btrfs_reserve_metadata_space(root, 6); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 249 | if (ret) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 250 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 251 | |
| 252 | trans = btrfs_start_transaction(root, 1); |
| 253 | BUG_ON(!trans); |
| 254 | |
| 255 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 256 | 0, &objectid); |
| 257 | if (ret) |
| 258 | goto fail; |
| 259 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 260 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
| 261 | 0, objectid, NULL, 0, 0, 0); |
Josef Bacik | 8e8a1e3 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 262 | if (IS_ERR(leaf)) { |
| 263 | ret = PTR_ERR(leaf); |
| 264 | goto fail; |
| 265 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 266 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 267 | memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 268 | btrfs_set_header_bytenr(leaf, leaf->start); |
| 269 | btrfs_set_header_generation(leaf, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 270 | btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 271 | btrfs_set_header_owner(leaf, objectid); |
| 272 | |
| 273 | write_extent_buffer(leaf, root->fs_info->fsid, |
| 274 | (unsigned long)btrfs_header_fsid(leaf), |
| 275 | BTRFS_FSID_SIZE); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 276 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 277 | (unsigned long)btrfs_header_chunk_tree_uuid(leaf), |
| 278 | BTRFS_UUID_SIZE); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 279 | btrfs_mark_buffer_dirty(leaf); |
| 280 | |
| 281 | inode_item = &root_item.inode; |
| 282 | memset(inode_item, 0, sizeof(*inode_item)); |
| 283 | inode_item->generation = cpu_to_le64(1); |
| 284 | inode_item->size = cpu_to_le64(3); |
| 285 | inode_item->nlink = cpu_to_le32(1); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 286 | inode_item->nbytes = cpu_to_le64(root->leafsize); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 287 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
| 288 | |
| 289 | btrfs_set_root_bytenr(&root_item, leaf->start); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 290 | btrfs_set_root_generation(&root_item, trans->transid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 291 | btrfs_set_root_level(&root_item, 0); |
| 292 | btrfs_set_root_refs(&root_item, 1); |
Yan, Zheng | 86b9f2e | 2009-11-12 09:36:50 +0000 | [diff] [blame] | 293 | btrfs_set_root_used(&root_item, leaf->len); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 294 | btrfs_set_root_last_snapshot(&root_item, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 295 | |
| 296 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
| 297 | root_item.drop_level = 0; |
| 298 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 299 | btrfs_tree_unlock(leaf); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 300 | free_extent_buffer(leaf); |
| 301 | leaf = NULL; |
| 302 | |
| 303 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 304 | |
| 305 | key.objectid = objectid; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 306 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 307 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 308 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 309 | &root_item); |
| 310 | if (ret) |
| 311 | goto fail; |
| 312 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 313 | key.offset = (u64)-1; |
| 314 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); |
| 315 | BUG_ON(IS_ERR(new_root)); |
| 316 | |
| 317 | btrfs_record_root_in_trans(trans, new_root); |
| 318 | |
| 319 | ret = btrfs_create_subvol_root(trans, new_root, new_dirid, |
| 320 | BTRFS_I(dir)->block_group); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 321 | /* |
| 322 | * insert the directory item |
| 323 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 324 | ret = btrfs_set_inode_index(dir, &index); |
| 325 | BUG_ON(ret); |
| 326 | |
| 327 | ret = btrfs_insert_dir_item(trans, root, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 328 | name, namelen, dir->i_ino, &key, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 329 | BTRFS_FT_DIR, index); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 330 | if (ret) |
| 331 | goto fail; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 332 | |
Yan Zheng | 52c2617 | 2009-01-05 15:43:43 -0500 | [diff] [blame] | 333 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
| 334 | ret = btrfs_update_inode(trans, root, dir); |
| 335 | BUG_ON(ret); |
| 336 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 337 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 338 | objectid, root->root_key.objectid, |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 339 | dir->i_ino, index, name, namelen); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 340 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 341 | BUG_ON(ret); |
| 342 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 343 | d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 344 | fail: |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 345 | err = btrfs_commit_transaction(trans, root); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 346 | if (err && !ret) |
| 347 | ret = err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 348 | |
| 349 | btrfs_unreserve_metadata_space(root, 6); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 350 | return ret; |
| 351 | } |
| 352 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 353 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, |
| 354 | char *name, int namelen) |
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 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 364 | /* |
| 365 | * 1 - inode item |
| 366 | * 2 - refs |
| 367 | * 1 - root item |
| 368 | * 2 - dir items |
| 369 | */ |
| 370 | ret = btrfs_reserve_metadata_space(root, 6); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 371 | if (ret) |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 372 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 373 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 374 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 375 | if (!pending_snapshot) { |
| 376 | ret = -ENOMEM; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 377 | btrfs_unreserve_metadata_space(root, 6); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 378 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 379 | } |
| 380 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); |
| 381 | if (!pending_snapshot->name) { |
| 382 | ret = -ENOMEM; |
| 383 | kfree(pending_snapshot); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 384 | btrfs_unreserve_metadata_space(root, 6); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 385 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 386 | } |
| 387 | memcpy(pending_snapshot->name, name, namelen); |
| 388 | pending_snapshot->name[namelen] = '\0'; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 389 | pending_snapshot->dentry = dentry; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 390 | trans = btrfs_start_transaction(root, 1); |
| 391 | BUG_ON(!trans); |
| 392 | pending_snapshot->root = root; |
| 393 | list_add(&pending_snapshot->list, |
| 394 | &trans->transaction->pending_snapshots); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 395 | ret = btrfs_commit_transaction(trans, root); |
| 396 | BUG_ON(ret); |
| 397 | btrfs_unreserve_metadata_space(root, 6); |
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: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 408 | return ret; |
| 409 | } |
| 410 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 411 | /* copy of may_create in fs/namei.c() */ |
| 412 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) |
| 413 | { |
| 414 | if (child->d_inode) |
| 415 | return -EEXIST; |
| 416 | if (IS_DEADDIR(dir)) |
| 417 | return -ENOENT; |
| 418 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Create a new subvolume below @parent. This is largely modeled after |
| 423 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup |
| 424 | * inside this filesystem so it's quite a bit simpler. |
| 425 | */ |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 426 | static noinline int btrfs_mksubvol(struct path *parent, |
| 427 | char *name, int namelen, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 428 | struct btrfs_root *snap_src) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 429 | { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 430 | struct inode *dir = parent->dentry->d_inode; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 431 | struct dentry *dentry; |
| 432 | int error; |
| 433 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 434 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 435 | |
| 436 | dentry = lookup_one_len(name, parent->dentry, namelen); |
| 437 | error = PTR_ERR(dentry); |
| 438 | if (IS_ERR(dentry)) |
| 439 | goto out_unlock; |
| 440 | |
| 441 | error = -EEXIST; |
| 442 | if (dentry->d_inode) |
| 443 | goto out_dput; |
| 444 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 445 | error = mnt_want_write(parent->mnt); |
| 446 | if (error) |
| 447 | goto out_dput; |
| 448 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 449 | error = btrfs_may_create(dir, dentry); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 450 | if (error) |
| 451 | goto out_drop_write; |
| 452 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 453 | down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
| 454 | |
| 455 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) |
| 456 | goto out_up_read; |
| 457 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 458 | if (snap_src) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 459 | error = create_snapshot(snap_src, dentry, |
| 460 | name, namelen); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 461 | } else { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 462 | error = create_subvol(BTRFS_I(dir)->root, dentry, |
| 463 | name, namelen); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 464 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 465 | if (!error) |
| 466 | fsnotify_mkdir(dir, dentry); |
| 467 | out_up_read: |
| 468 | up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 469 | out_drop_write: |
| 470 | mnt_drop_write(parent->mnt); |
| 471 | out_dput: |
| 472 | dput(dentry); |
| 473 | out_unlock: |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 474 | mutex_unlock(&dir->i_mutex); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 475 | return error; |
| 476 | } |
| 477 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 478 | static int btrfs_defrag_file(struct file *file) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 479 | { |
| 480 | struct inode *inode = fdentry(file)->d_inode; |
| 481 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 482 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 483 | struct btrfs_ordered_extent *ordered; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 484 | struct page *page; |
| 485 | unsigned long last_index; |
| 486 | unsigned long ra_pages = root->fs_info->bdi.ra_pages; |
| 487 | unsigned long total_read = 0; |
| 488 | u64 page_start; |
| 489 | u64 page_end; |
| 490 | unsigned long i; |
| 491 | int ret; |
| 492 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 493 | ret = btrfs_check_data_free_space(root, inode, inode->i_size); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 494 | if (ret) |
| 495 | return -ENOSPC; |
| 496 | |
| 497 | mutex_lock(&inode->i_mutex); |
| 498 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 499 | for (i = 0; i <= last_index; i++) { |
| 500 | if (total_read % ra_pages == 0) { |
| 501 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, |
| 502 | min(last_index, i + ra_pages - 1)); |
| 503 | } |
| 504 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 505 | again: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 506 | page = grab_cache_page(inode->i_mapping, i); |
| 507 | if (!page) |
| 508 | goto out_unlock; |
| 509 | if (!PageUptodate(page)) { |
| 510 | btrfs_readpage(NULL, page); |
| 511 | lock_page(page); |
| 512 | if (!PageUptodate(page)) { |
| 513 | unlock_page(page); |
| 514 | page_cache_release(page); |
| 515 | goto out_unlock; |
| 516 | } |
| 517 | } |
| 518 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 519 | wait_on_page_writeback(page); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 520 | |
| 521 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 522 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 523 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 524 | |
| 525 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 526 | if (ordered) { |
| 527 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 528 | unlock_page(page); |
| 529 | page_cache_release(page); |
| 530 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 531 | btrfs_put_ordered_extent(ordered); |
| 532 | goto again; |
| 533 | } |
| 534 | set_page_extent_mapped(page); |
| 535 | |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 536 | /* |
| 537 | * this makes sure page_mkwrite is called on the |
| 538 | * page if it is dirtied again later |
| 539 | */ |
| 540 | clear_page_dirty_for_io(page); |
| 541 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 542 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 543 | set_page_dirty(page); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 544 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 545 | unlock_page(page); |
| 546 | page_cache_release(page); |
| 547 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); |
| 548 | } |
| 549 | |
| 550 | out_unlock: |
| 551 | mutex_unlock(&inode->i_mutex); |
| 552 | return 0; |
| 553 | } |
| 554 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 555 | static noinline int btrfs_ioctl_resize(struct btrfs_root *root, |
| 556 | void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 557 | { |
| 558 | u64 new_size; |
| 559 | u64 old_size; |
| 560 | u64 devid = 1; |
| 561 | struct btrfs_ioctl_vol_args *vol_args; |
| 562 | struct btrfs_trans_handle *trans; |
| 563 | struct btrfs_device *device = NULL; |
| 564 | char *sizestr; |
| 565 | char *devstr = NULL; |
| 566 | int ret = 0; |
| 567 | int namelen; |
| 568 | int mod = 0; |
| 569 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 570 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 571 | return -EROFS; |
| 572 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 573 | if (!capable(CAP_SYS_ADMIN)) |
| 574 | return -EPERM; |
| 575 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 576 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 577 | if (IS_ERR(vol_args)) |
| 578 | return PTR_ERR(vol_args); |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 579 | |
| 580 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 581 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 582 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 583 | mutex_lock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 584 | sizestr = vol_args->name; |
| 585 | devstr = strchr(sizestr, ':'); |
| 586 | if (devstr) { |
| 587 | char *end; |
| 588 | sizestr = devstr + 1; |
| 589 | *devstr = '\0'; |
| 590 | devstr = vol_args->name; |
| 591 | devid = simple_strtoull(devstr, &end, 10); |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 592 | printk(KERN_INFO "resizing devid %llu\n", |
| 593 | (unsigned long long)devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 594 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 595 | device = btrfs_find_device(root, devid, NULL, NULL); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 596 | if (!device) { |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 597 | printk(KERN_INFO "resizer unable to find device %llu\n", |
| 598 | (unsigned long long)devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 599 | ret = -EINVAL; |
| 600 | goto out_unlock; |
| 601 | } |
| 602 | if (!strcmp(sizestr, "max")) |
| 603 | new_size = device->bdev->bd_inode->i_size; |
| 604 | else { |
| 605 | if (sizestr[0] == '-') { |
| 606 | mod = -1; |
| 607 | sizestr++; |
| 608 | } else if (sizestr[0] == '+') { |
| 609 | mod = 1; |
| 610 | sizestr++; |
| 611 | } |
| 612 | new_size = btrfs_parse_size(sizestr); |
| 613 | if (new_size == 0) { |
| 614 | ret = -EINVAL; |
| 615 | goto out_unlock; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | old_size = device->total_bytes; |
| 620 | |
| 621 | if (mod < 0) { |
| 622 | if (new_size > old_size) { |
| 623 | ret = -EINVAL; |
| 624 | goto out_unlock; |
| 625 | } |
| 626 | new_size = old_size - new_size; |
| 627 | } else if (mod > 0) { |
| 628 | new_size = old_size + new_size; |
| 629 | } |
| 630 | |
| 631 | if (new_size < 256 * 1024 * 1024) { |
| 632 | ret = -EINVAL; |
| 633 | goto out_unlock; |
| 634 | } |
| 635 | if (new_size > device->bdev->bd_inode->i_size) { |
| 636 | ret = -EFBIG; |
| 637 | goto out_unlock; |
| 638 | } |
| 639 | |
| 640 | do_div(new_size, root->sectorsize); |
| 641 | new_size *= root->sectorsize; |
| 642 | |
| 643 | printk(KERN_INFO "new size for %s is %llu\n", |
| 644 | device->name, (unsigned long long)new_size); |
| 645 | |
| 646 | if (new_size > old_size) { |
| 647 | trans = btrfs_start_transaction(root, 1); |
| 648 | ret = btrfs_grow_device(trans, device, new_size); |
| 649 | btrfs_commit_transaction(trans, root); |
| 650 | } else { |
| 651 | ret = btrfs_shrink_device(device, new_size); |
| 652 | } |
| 653 | |
| 654 | out_unlock: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 655 | mutex_unlock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 656 | kfree(vol_args); |
| 657 | return ret; |
| 658 | } |
| 659 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 660 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 661 | void __user *arg, int subvol) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 662 | { |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 663 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 664 | struct btrfs_ioctl_vol_args *vol_args; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 665 | struct file *src_file; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 666 | int namelen; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 667 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 668 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 669 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 670 | return -EROFS; |
| 671 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 672 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 673 | if (IS_ERR(vol_args)) |
| 674 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 675 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 676 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 677 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 678 | if (strchr(vol_args->name, '/')) { |
| 679 | ret = -EINVAL; |
| 680 | goto out; |
| 681 | } |
| 682 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 683 | if (subvol) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 684 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen, |
| 685 | NULL); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 686 | } else { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 687 | struct inode *src_inode; |
| 688 | src_file = fget(vol_args->fd); |
| 689 | if (!src_file) { |
| 690 | ret = -EINVAL; |
| 691 | goto out; |
| 692 | } |
| 693 | |
| 694 | src_inode = src_file->f_path.dentry->d_inode; |
| 695 | 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] | 696 | printk(KERN_INFO "btrfs: Snapshot src from " |
| 697 | "another FS\n"); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 698 | ret = -EINVAL; |
| 699 | fput(src_file); |
| 700 | goto out; |
| 701 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 702 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen, |
| 703 | BTRFS_I(src_inode)->root); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 704 | fput(src_file); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 705 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 706 | out: |
| 707 | kfree(vol_args); |
| 708 | return ret; |
| 709 | } |
| 710 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 711 | /* |
| 712 | * helper to check if the subvolume references other subvolumes |
| 713 | */ |
| 714 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
| 715 | { |
| 716 | struct btrfs_path *path; |
| 717 | struct btrfs_key key; |
| 718 | int ret; |
| 719 | |
| 720 | path = btrfs_alloc_path(); |
| 721 | if (!path) |
| 722 | return -ENOMEM; |
| 723 | |
| 724 | key.objectid = root->root_key.objectid; |
| 725 | key.type = BTRFS_ROOT_REF_KEY; |
| 726 | key.offset = (u64)-1; |
| 727 | |
| 728 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, |
| 729 | &key, path, 0, 0); |
| 730 | if (ret < 0) |
| 731 | goto out; |
| 732 | BUG_ON(ret == 0); |
| 733 | |
| 734 | ret = 0; |
| 735 | if (path->slots[0] > 0) { |
| 736 | path->slots[0]--; |
| 737 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 738 | if (key.objectid == root->root_key.objectid && |
| 739 | key.type == BTRFS_ROOT_REF_KEY) |
| 740 | ret = -ENOTEMPTY; |
| 741 | } |
| 742 | out: |
| 743 | btrfs_free_path(path); |
| 744 | return ret; |
| 745 | } |
| 746 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame^] | 747 | static noinline int key_in_sk(struct btrfs_key *key, |
| 748 | struct btrfs_ioctl_search_key *sk) |
| 749 | { |
| 750 | if (key->objectid < sk->min_objectid) |
| 751 | return 0; |
| 752 | if (key->offset < sk->min_offset) |
| 753 | return 0; |
| 754 | if (key->type < sk->min_type) |
| 755 | return 0; |
| 756 | if (key->objectid > sk->max_objectid) |
| 757 | return 0; |
| 758 | if (key->type > sk->max_type) |
| 759 | return 0; |
| 760 | if (key->offset > sk->max_offset) |
| 761 | return 0; |
| 762 | return 1; |
| 763 | } |
| 764 | |
| 765 | static noinline int copy_to_sk(struct btrfs_root *root, |
| 766 | struct btrfs_path *path, |
| 767 | struct btrfs_key *key, |
| 768 | struct btrfs_ioctl_search_key *sk, |
| 769 | char *buf, |
| 770 | unsigned long *sk_offset, |
| 771 | int *num_found) |
| 772 | { |
| 773 | u64 found_transid; |
| 774 | struct extent_buffer *leaf; |
| 775 | struct btrfs_ioctl_search_header sh; |
| 776 | unsigned long item_off; |
| 777 | unsigned long item_len; |
| 778 | int nritems; |
| 779 | int i; |
| 780 | int slot; |
| 781 | int found = 0; |
| 782 | int ret = 0; |
| 783 | |
| 784 | leaf = path->nodes[0]; |
| 785 | slot = path->slots[0]; |
| 786 | nritems = btrfs_header_nritems(leaf); |
| 787 | |
| 788 | if (btrfs_header_generation(leaf) > sk->max_transid) { |
| 789 | i = nritems; |
| 790 | goto advance_key; |
| 791 | } |
| 792 | found_transid = btrfs_header_generation(leaf); |
| 793 | |
| 794 | for (i = slot; i < nritems; i++) { |
| 795 | item_off = btrfs_item_ptr_offset(leaf, i); |
| 796 | item_len = btrfs_item_size_nr(leaf, i); |
| 797 | |
| 798 | if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) |
| 799 | item_len = 0; |
| 800 | |
| 801 | if (sizeof(sh) + item_len + *sk_offset > |
| 802 | BTRFS_SEARCH_ARGS_BUFSIZE) { |
| 803 | ret = 1; |
| 804 | goto overflow; |
| 805 | } |
| 806 | |
| 807 | btrfs_item_key_to_cpu(leaf, key, i); |
| 808 | if (!key_in_sk(key, sk)) |
| 809 | continue; |
| 810 | |
| 811 | sh.objectid = key->objectid; |
| 812 | sh.offset = key->offset; |
| 813 | sh.type = key->type; |
| 814 | sh.len = item_len; |
| 815 | sh.transid = found_transid; |
| 816 | |
| 817 | /* copy search result header */ |
| 818 | memcpy(buf + *sk_offset, &sh, sizeof(sh)); |
| 819 | *sk_offset += sizeof(sh); |
| 820 | |
| 821 | if (item_len) { |
| 822 | char *p = buf + *sk_offset; |
| 823 | /* copy the item */ |
| 824 | read_extent_buffer(leaf, p, |
| 825 | item_off, item_len); |
| 826 | *sk_offset += item_len; |
| 827 | found++; |
| 828 | } |
| 829 | |
| 830 | if (*num_found >= sk->nr_items) |
| 831 | break; |
| 832 | } |
| 833 | advance_key: |
| 834 | if (key->offset < (u64)-1) |
| 835 | key->offset++; |
| 836 | else if (key->type < (u64)-1) |
| 837 | key->type++; |
| 838 | else if (key->objectid < (u64)-1) |
| 839 | key->objectid++; |
| 840 | ret = 0; |
| 841 | overflow: |
| 842 | *num_found += found; |
| 843 | return ret; |
| 844 | } |
| 845 | |
| 846 | static noinline int search_ioctl(struct inode *inode, |
| 847 | struct btrfs_ioctl_search_args *args) |
| 848 | { |
| 849 | struct btrfs_root *root; |
| 850 | struct btrfs_key key; |
| 851 | struct btrfs_key max_key; |
| 852 | struct btrfs_path *path; |
| 853 | struct btrfs_ioctl_search_key *sk = &args->key; |
| 854 | struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; |
| 855 | int ret; |
| 856 | int num_found = 0; |
| 857 | unsigned long sk_offset = 0; |
| 858 | |
| 859 | path = btrfs_alloc_path(); |
| 860 | if (!path) |
| 861 | return -ENOMEM; |
| 862 | |
| 863 | if (sk->tree_id == 0) { |
| 864 | /* search the root of the inode that was passed */ |
| 865 | root = BTRFS_I(inode)->root; |
| 866 | } else { |
| 867 | key.objectid = sk->tree_id; |
| 868 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 869 | key.offset = (u64)-1; |
| 870 | root = btrfs_read_fs_root_no_name(info, &key); |
| 871 | if (IS_ERR(root)) { |
| 872 | printk(KERN_ERR "could not find root %llu\n", |
| 873 | sk->tree_id); |
| 874 | btrfs_free_path(path); |
| 875 | return -ENOENT; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | key.objectid = sk->min_objectid; |
| 880 | key.type = sk->min_type; |
| 881 | key.offset = sk->min_offset; |
| 882 | |
| 883 | max_key.objectid = sk->max_objectid; |
| 884 | max_key.type = sk->max_type; |
| 885 | max_key.offset = sk->max_offset; |
| 886 | |
| 887 | path->keep_locks = 1; |
| 888 | |
| 889 | while(1) { |
| 890 | ret = btrfs_search_forward(root, &key, &max_key, path, 0, |
| 891 | sk->min_transid); |
| 892 | if (ret != 0) { |
| 893 | if (ret > 0) |
| 894 | ret = 0; |
| 895 | goto err; |
| 896 | } |
| 897 | ret = copy_to_sk(root, path, &key, sk, args->buf, |
| 898 | &sk_offset, &num_found); |
| 899 | btrfs_release_path(root, path); |
| 900 | if (ret || num_found >= sk->nr_items) |
| 901 | break; |
| 902 | |
| 903 | } |
| 904 | ret = 0; |
| 905 | err: |
| 906 | sk->nr_items = num_found; |
| 907 | btrfs_free_path(path); |
| 908 | return ret; |
| 909 | } |
| 910 | |
| 911 | static noinline int btrfs_ioctl_tree_search(struct file *file, |
| 912 | void __user *argp) |
| 913 | { |
| 914 | struct btrfs_ioctl_search_args *args; |
| 915 | struct inode *inode; |
| 916 | int ret; |
| 917 | |
| 918 | if (!capable(CAP_SYS_ADMIN)) |
| 919 | return -EPERM; |
| 920 | |
| 921 | args = kmalloc(sizeof(*args), GFP_KERNEL); |
| 922 | if (!args) |
| 923 | return -ENOMEM; |
| 924 | |
| 925 | if (copy_from_user(args, argp, sizeof(*args))) { |
| 926 | kfree(args); |
| 927 | return -EFAULT; |
| 928 | } |
| 929 | inode = fdentry(file)->d_inode; |
| 930 | ret = search_ioctl(inode, args); |
| 931 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 932 | ret = -EFAULT; |
| 933 | kfree(args); |
| 934 | return ret; |
| 935 | } |
| 936 | |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 937 | /* |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame^] | 938 | * Search INODE_REFs to identify path name of 'dirid' directory |
| 939 | * in a 'tree_id' tree. and sets path name to 'name'. |
| 940 | */ |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 941 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
| 942 | u64 tree_id, u64 dirid, char *name) |
| 943 | { |
| 944 | struct btrfs_root *root; |
| 945 | struct btrfs_key key; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame^] | 946 | char *ptr; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 947 | int ret = -1; |
| 948 | int slot; |
| 949 | int len; |
| 950 | int total_len = 0; |
| 951 | struct btrfs_inode_ref *iref; |
| 952 | struct extent_buffer *l; |
| 953 | struct btrfs_path *path; |
| 954 | |
| 955 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { |
| 956 | name[0]='\0'; |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | path = btrfs_alloc_path(); |
| 961 | if (!path) |
| 962 | return -ENOMEM; |
| 963 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame^] | 964 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 965 | |
| 966 | key.objectid = tree_id; |
| 967 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 968 | key.offset = (u64)-1; |
| 969 | root = btrfs_read_fs_root_no_name(info, &key); |
| 970 | if (IS_ERR(root)) { |
| 971 | printk(KERN_ERR "could not find root %llu\n", tree_id); |
| 972 | return -ENOENT; |
| 973 | } |
| 974 | |
| 975 | key.objectid = dirid; |
| 976 | key.type = BTRFS_INODE_REF_KEY; |
| 977 | key.offset = 0; |
| 978 | |
| 979 | while(1) { |
| 980 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 981 | if (ret < 0) |
| 982 | goto out; |
| 983 | |
| 984 | l = path->nodes[0]; |
| 985 | slot = path->slots[0]; |
| 986 | btrfs_item_key_to_cpu(l, &key, slot); |
| 987 | |
| 988 | if (ret > 0 && (key.objectid != dirid || |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame^] | 989 | key.type != BTRFS_INODE_REF_KEY)) { |
| 990 | ret = -ENOENT; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 991 | goto out; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame^] | 992 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 993 | |
| 994 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); |
| 995 | len = btrfs_inode_ref_name_len(l, iref); |
| 996 | ptr -= len + 1; |
| 997 | total_len += len + 1; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame^] | 998 | if (ptr < name) |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 999 | goto out; |
| 1000 | |
| 1001 | *(ptr + len) = '/'; |
| 1002 | read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); |
| 1003 | |
| 1004 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) |
| 1005 | break; |
| 1006 | |
| 1007 | btrfs_release_path(root, path); |
| 1008 | key.objectid = key.offset; |
| 1009 | key.offset = 0; |
| 1010 | dirid = key.objectid; |
| 1011 | |
| 1012 | } |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame^] | 1013 | if (ptr < name) |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1014 | goto out; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame^] | 1015 | memcpy(name, ptr, total_len); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1016 | name[total_len]='\0'; |
| 1017 | ret = 0; |
| 1018 | out: |
| 1019 | btrfs_free_path(path); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame^] | 1020 | return ret; |
| 1021 | } |
| 1022 | |
| 1023 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, |
| 1024 | void __user *argp) |
| 1025 | { |
| 1026 | struct btrfs_ioctl_ino_lookup_args *args; |
| 1027 | struct inode *inode; |
| 1028 | int ret; |
| 1029 | |
| 1030 | if (!capable(CAP_SYS_ADMIN)) |
| 1031 | return -EPERM; |
| 1032 | |
| 1033 | args = kmalloc(sizeof(*args), GFP_KERNEL); |
| 1034 | if (copy_from_user(args, argp, sizeof(*args))) { |
| 1035 | kfree(args); |
| 1036 | return -EFAULT; |
| 1037 | } |
| 1038 | inode = fdentry(file)->d_inode; |
| 1039 | |
| 1040 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
| 1041 | args->treeid, args->objectid, |
| 1042 | args->name); |
| 1043 | |
| 1044 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 1045 | ret = -EFAULT; |
| 1046 | |
| 1047 | kfree(args); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 1048 | return ret; |
| 1049 | } |
| 1050 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1051 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
| 1052 | void __user *arg) |
| 1053 | { |
| 1054 | struct dentry *parent = fdentry(file); |
| 1055 | struct dentry *dentry; |
| 1056 | struct inode *dir = parent->d_inode; |
| 1057 | struct inode *inode; |
| 1058 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1059 | struct btrfs_root *dest = NULL; |
| 1060 | struct btrfs_ioctl_vol_args *vol_args; |
| 1061 | struct btrfs_trans_handle *trans; |
| 1062 | int namelen; |
| 1063 | int ret; |
| 1064 | int err = 0; |
| 1065 | |
| 1066 | if (!capable(CAP_SYS_ADMIN)) |
| 1067 | return -EPERM; |
| 1068 | |
| 1069 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1070 | if (IS_ERR(vol_args)) |
| 1071 | return PTR_ERR(vol_args); |
| 1072 | |
| 1073 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
| 1074 | namelen = strlen(vol_args->name); |
| 1075 | if (strchr(vol_args->name, '/') || |
| 1076 | strncmp(vol_args->name, "..", namelen) == 0) { |
| 1077 | err = -EINVAL; |
| 1078 | goto out; |
| 1079 | } |
| 1080 | |
| 1081 | err = mnt_want_write(file->f_path.mnt); |
| 1082 | if (err) |
| 1083 | goto out; |
| 1084 | |
| 1085 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
| 1086 | dentry = lookup_one_len(vol_args->name, parent, namelen); |
| 1087 | if (IS_ERR(dentry)) { |
| 1088 | err = PTR_ERR(dentry); |
| 1089 | goto out_unlock_dir; |
| 1090 | } |
| 1091 | |
| 1092 | if (!dentry->d_inode) { |
| 1093 | err = -ENOENT; |
| 1094 | goto out_dput; |
| 1095 | } |
| 1096 | |
| 1097 | inode = dentry->d_inode; |
| 1098 | if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 1099 | err = -EINVAL; |
| 1100 | goto out_dput; |
| 1101 | } |
| 1102 | |
| 1103 | dest = BTRFS_I(inode)->root; |
| 1104 | |
| 1105 | mutex_lock(&inode->i_mutex); |
| 1106 | err = d_invalidate(dentry); |
| 1107 | if (err) |
| 1108 | goto out_unlock; |
| 1109 | |
| 1110 | down_write(&root->fs_info->subvol_sem); |
| 1111 | |
| 1112 | err = may_destroy_subvol(dest); |
| 1113 | if (err) |
| 1114 | goto out_up_write; |
| 1115 | |
| 1116 | trans = btrfs_start_transaction(root, 1); |
| 1117 | ret = btrfs_unlink_subvol(trans, root, dir, |
| 1118 | dest->root_key.objectid, |
| 1119 | dentry->d_name.name, |
| 1120 | dentry->d_name.len); |
| 1121 | BUG_ON(ret); |
| 1122 | |
| 1123 | btrfs_record_root_in_trans(trans, dest); |
| 1124 | |
| 1125 | memset(&dest->root_item.drop_progress, 0, |
| 1126 | sizeof(dest->root_item.drop_progress)); |
| 1127 | dest->root_item.drop_level = 0; |
| 1128 | btrfs_set_root_refs(&dest->root_item, 0); |
| 1129 | |
| 1130 | ret = btrfs_insert_orphan_item(trans, |
| 1131 | root->fs_info->tree_root, |
| 1132 | dest->root_key.objectid); |
| 1133 | BUG_ON(ret); |
| 1134 | |
| 1135 | ret = btrfs_commit_transaction(trans, root); |
| 1136 | BUG_ON(ret); |
| 1137 | inode->i_flags |= S_DEAD; |
| 1138 | out_up_write: |
| 1139 | up_write(&root->fs_info->subvol_sem); |
| 1140 | out_unlock: |
| 1141 | mutex_unlock(&inode->i_mutex); |
| 1142 | if (!err) { |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 1143 | shrink_dcache_sb(root->fs_info->sb); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1144 | btrfs_invalidate_inodes(dest); |
| 1145 | d_delete(dentry); |
| 1146 | } |
| 1147 | out_dput: |
| 1148 | dput(dentry); |
| 1149 | out_unlock_dir: |
| 1150 | mutex_unlock(&dir->i_mutex); |
| 1151 | mnt_drop_write(file->f_path.mnt); |
| 1152 | out: |
| 1153 | kfree(vol_args); |
| 1154 | return err; |
| 1155 | } |
| 1156 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1157 | static int btrfs_ioctl_defrag(struct file *file) |
| 1158 | { |
| 1159 | struct inode *inode = fdentry(file)->d_inode; |
| 1160 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1161 | int ret; |
| 1162 | |
| 1163 | ret = mnt_want_write(file->f_path.mnt); |
| 1164 | if (ret) |
| 1165 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1166 | |
| 1167 | switch (inode->i_mode & S_IFMT) { |
| 1168 | case S_IFDIR: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1169 | if (!capable(CAP_SYS_ADMIN)) { |
| 1170 | ret = -EPERM; |
| 1171 | goto out; |
| 1172 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1173 | btrfs_defrag_root(root, 0); |
| 1174 | btrfs_defrag_root(root->fs_info->extent_root, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1175 | break; |
| 1176 | case S_IFREG: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1177 | if (!(file->f_mode & FMODE_WRITE)) { |
| 1178 | ret = -EINVAL; |
| 1179 | goto out; |
| 1180 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1181 | btrfs_defrag_file(file); |
| 1182 | break; |
| 1183 | } |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1184 | out: |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 1185 | mnt_drop_write(file->f_path.mnt); |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1186 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1187 | } |
| 1188 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1189 | 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] | 1190 | { |
| 1191 | struct btrfs_ioctl_vol_args *vol_args; |
| 1192 | int ret; |
| 1193 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1194 | if (!capable(CAP_SYS_ADMIN)) |
| 1195 | return -EPERM; |
| 1196 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 1197 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1198 | if (IS_ERR(vol_args)) |
| 1199 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1200 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 1201 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1202 | ret = btrfs_init_new_device(root, vol_args->name); |
| 1203 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1204 | kfree(vol_args); |
| 1205 | return ret; |
| 1206 | } |
| 1207 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1208 | 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] | 1209 | { |
| 1210 | struct btrfs_ioctl_vol_args *vol_args; |
| 1211 | int ret; |
| 1212 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1213 | if (!capable(CAP_SYS_ADMIN)) |
| 1214 | return -EPERM; |
| 1215 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1216 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 1217 | return -EROFS; |
| 1218 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 1219 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1220 | if (IS_ERR(vol_args)) |
| 1221 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1222 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 1223 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1224 | ret = btrfs_rm_device(root, vol_args->name); |
| 1225 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1226 | kfree(vol_args); |
| 1227 | return ret; |
| 1228 | } |
| 1229 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1230 | static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, |
| 1231 | u64 off, u64 olen, u64 destoff) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1232 | { |
| 1233 | struct inode *inode = fdentry(file)->d_inode; |
| 1234 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1235 | struct file *src_file; |
| 1236 | struct inode *src; |
| 1237 | struct btrfs_trans_handle *trans; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1238 | struct btrfs_path *path; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1239 | struct extent_buffer *leaf; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1240 | char *buf; |
| 1241 | struct btrfs_key key; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1242 | u32 nritems; |
| 1243 | int slot; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1244 | int ret; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1245 | u64 len = olen; |
| 1246 | u64 bs = root->fs_info->sb->s_blocksize; |
| 1247 | u64 hint_byte; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1248 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1249 | /* |
| 1250 | * TODO: |
| 1251 | * - split compressed inline extents. annoying: we need to |
| 1252 | * decompress into destination's address_space (the file offset |
| 1253 | * may change, so source mapping won't do), then recompress (or |
| 1254 | * otherwise reinsert) a subrange. |
| 1255 | * - allow ranges within the same file to be cloned (provided |
| 1256 | * they don't overlap)? |
| 1257 | */ |
| 1258 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1259 | /* the destination must be opened for writing */ |
| 1260 | if (!(file->f_mode & FMODE_WRITE)) |
| 1261 | return -EINVAL; |
| 1262 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1263 | ret = mnt_want_write(file->f_path.mnt); |
| 1264 | if (ret) |
| 1265 | return ret; |
| 1266 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1267 | src_file = fget(srcfd); |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 1268 | if (!src_file) { |
| 1269 | ret = -EBADF; |
| 1270 | goto out_drop_write; |
| 1271 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1272 | src = src_file->f_dentry->d_inode; |
| 1273 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1274 | ret = -EINVAL; |
| 1275 | if (src == inode) |
| 1276 | goto out_fput; |
| 1277 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1278 | ret = -EISDIR; |
| 1279 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1280 | goto out_fput; |
| 1281 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1282 | ret = -EXDEV; |
| 1283 | if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) |
| 1284 | goto out_fput; |
| 1285 | |
| 1286 | ret = -ENOMEM; |
| 1287 | buf = vmalloc(btrfs_level_size(root, 0)); |
| 1288 | if (!buf) |
| 1289 | goto out_fput; |
| 1290 | |
| 1291 | path = btrfs_alloc_path(); |
| 1292 | if (!path) { |
| 1293 | vfree(buf); |
| 1294 | goto out_fput; |
| 1295 | } |
| 1296 | path->reada = 2; |
| 1297 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1298 | if (inode < src) { |
| 1299 | mutex_lock(&inode->i_mutex); |
| 1300 | mutex_lock(&src->i_mutex); |
| 1301 | } else { |
| 1302 | mutex_lock(&src->i_mutex); |
| 1303 | mutex_lock(&inode->i_mutex); |
| 1304 | } |
| 1305 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1306 | /* determine range to clone */ |
| 1307 | ret = -EINVAL; |
| 1308 | if (off >= src->i_size || off + len > src->i_size) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1309 | goto out_unlock; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1310 | if (len == 0) |
| 1311 | olen = len = src->i_size - off; |
| 1312 | /* if we extend to eof, continue to block boundary */ |
| 1313 | if (off + len == src->i_size) |
| 1314 | len = ((src->i_size + bs-1) & ~(bs-1)) |
| 1315 | - off; |
| 1316 | |
| 1317 | /* verify the end result is block aligned */ |
| 1318 | if ((off & (bs-1)) || |
| 1319 | ((off + len) & (bs-1))) |
| 1320 | goto out_unlock; |
| 1321 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1322 | /* do any pending delalloc/csum calc on src, one way or |
| 1323 | another, and lock file content */ |
| 1324 | while (1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1325 | struct btrfs_ordered_extent *ordered; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1326 | lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
| 1327 | ordered = btrfs_lookup_first_ordered_extent(inode, off+len); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1328 | if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1329 | break; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1330 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1331 | if (ordered) |
| 1332 | btrfs_put_ordered_extent(ordered); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1333 | btrfs_wait_ordered_range(src, off, off+len); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1334 | } |
| 1335 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1336 | trans = btrfs_start_transaction(root, 1); |
| 1337 | BUG_ON(!trans); |
| 1338 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1339 | /* punch hole in destination first */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1340 | btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1341 | |
| 1342 | /* clone data */ |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1343 | key.objectid = src->i_ino; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1344 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 1345 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1346 | |
| 1347 | while (1) { |
| 1348 | /* |
| 1349 | * note the key will change type as we walk through the |
| 1350 | * tree. |
| 1351 | */ |
| 1352 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); |
| 1353 | if (ret < 0) |
| 1354 | goto out; |
| 1355 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1356 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 1357 | if (path->slots[0] >= nritems) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1358 | ret = btrfs_next_leaf(root, path); |
| 1359 | if (ret < 0) |
| 1360 | goto out; |
| 1361 | if (ret > 0) |
| 1362 | break; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1363 | nritems = btrfs_header_nritems(path->nodes[0]); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1364 | } |
| 1365 | leaf = path->nodes[0]; |
| 1366 | slot = path->slots[0]; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1367 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1368 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1369 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1370 | key.objectid != src->i_ino) |
| 1371 | break; |
| 1372 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1373 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
| 1374 | struct btrfs_file_extent_item *extent; |
| 1375 | int type; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1376 | u32 size; |
| 1377 | struct btrfs_key new_key; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1378 | u64 disko = 0, diskl = 0; |
| 1379 | u64 datao = 0, datal = 0; |
| 1380 | u8 comp; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1381 | |
| 1382 | size = btrfs_item_size_nr(leaf, slot); |
| 1383 | read_extent_buffer(leaf, buf, |
| 1384 | btrfs_item_ptr_offset(leaf, slot), |
| 1385 | size); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1386 | |
| 1387 | extent = btrfs_item_ptr(leaf, slot, |
| 1388 | struct btrfs_file_extent_item); |
| 1389 | comp = btrfs_file_extent_compression(leaf, extent); |
| 1390 | type = btrfs_file_extent_type(leaf, extent); |
Chris Mason | c8a894d | 2009-06-27 21:07:03 -0400 | [diff] [blame] | 1391 | if (type == BTRFS_FILE_EXTENT_REG || |
| 1392 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1393 | disko = btrfs_file_extent_disk_bytenr(leaf, |
| 1394 | extent); |
| 1395 | diskl = btrfs_file_extent_disk_num_bytes(leaf, |
| 1396 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1397 | datao = btrfs_file_extent_offset(leaf, extent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1398 | datal = btrfs_file_extent_num_bytes(leaf, |
| 1399 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1400 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 1401 | /* take upper bound, may be compressed */ |
| 1402 | datal = btrfs_file_extent_ram_bytes(leaf, |
| 1403 | extent); |
| 1404 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1405 | btrfs_release_path(root, path); |
| 1406 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1407 | if (key.offset + datal < off || |
| 1408 | key.offset >= off+len) |
| 1409 | goto next; |
| 1410 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1411 | memcpy(&new_key, &key, sizeof(new_key)); |
| 1412 | new_key.objectid = inode->i_ino; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1413 | new_key.offset = key.offset + destoff - off; |
| 1414 | |
Chris Mason | c8a894d | 2009-06-27 21:07:03 -0400 | [diff] [blame] | 1415 | if (type == BTRFS_FILE_EXTENT_REG || |
| 1416 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1417 | ret = btrfs_insert_empty_item(trans, root, path, |
| 1418 | &new_key, size); |
| 1419 | if (ret) |
| 1420 | goto out; |
| 1421 | |
| 1422 | leaf = path->nodes[0]; |
| 1423 | slot = path->slots[0]; |
| 1424 | write_extent_buffer(leaf, buf, |
| 1425 | btrfs_item_ptr_offset(leaf, slot), |
| 1426 | size); |
| 1427 | |
| 1428 | extent = btrfs_item_ptr(leaf, slot, |
| 1429 | struct btrfs_file_extent_item); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1430 | |
| 1431 | if (off > key.offset) { |
| 1432 | datao += off - key.offset; |
| 1433 | datal -= off - key.offset; |
| 1434 | } |
Chris Mason | ac6889c | 2009-10-09 11:29:53 -0400 | [diff] [blame] | 1435 | |
| 1436 | if (key.offset + datal > off + len) |
| 1437 | datal = off + len - key.offset; |
| 1438 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1439 | /* disko == 0 means it's a hole */ |
| 1440 | if (!disko) |
| 1441 | datao = 0; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1442 | |
| 1443 | btrfs_set_file_extent_offset(leaf, extent, |
| 1444 | datao); |
| 1445 | btrfs_set_file_extent_num_bytes(leaf, extent, |
| 1446 | datal); |
| 1447 | if (disko) { |
| 1448 | inode_add_bytes(inode, datal); |
| 1449 | ret = btrfs_inc_extent_ref(trans, root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1450 | disko, diskl, 0, |
| 1451 | root->root_key.objectid, |
| 1452 | inode->i_ino, |
| 1453 | new_key.offset - datao); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1454 | BUG_ON(ret); |
| 1455 | } |
| 1456 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 1457 | u64 skip = 0; |
| 1458 | u64 trim = 0; |
| 1459 | if (off > key.offset) { |
| 1460 | skip = off - key.offset; |
| 1461 | new_key.offset += skip; |
| 1462 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1463 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1464 | if (key.offset + datal > off+len) |
| 1465 | trim = key.offset + datal - (off+len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1466 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1467 | if (comp && (skip || trim)) { |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1468 | ret = -EINVAL; |
| 1469 | goto out; |
| 1470 | } |
| 1471 | size -= skip + trim; |
| 1472 | datal -= skip + trim; |
| 1473 | ret = btrfs_insert_empty_item(trans, root, path, |
| 1474 | &new_key, size); |
| 1475 | if (ret) |
| 1476 | goto out; |
| 1477 | |
| 1478 | if (skip) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1479 | u32 start = |
| 1480 | btrfs_file_extent_calc_inline_size(0); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1481 | memmove(buf+start, buf+start+skip, |
| 1482 | datal); |
| 1483 | } |
| 1484 | |
| 1485 | leaf = path->nodes[0]; |
| 1486 | slot = path->slots[0]; |
| 1487 | write_extent_buffer(leaf, buf, |
| 1488 | btrfs_item_ptr_offset(leaf, slot), |
| 1489 | size); |
| 1490 | inode_add_bytes(inode, datal); |
| 1491 | } |
| 1492 | |
| 1493 | btrfs_mark_buffer_dirty(leaf); |
| 1494 | } |
| 1495 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1496 | next: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1497 | btrfs_release_path(root, path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1498 | key.offset++; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1499 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1500 | ret = 0; |
| 1501 | out: |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1502 | btrfs_release_path(root, path); |
| 1503 | if (ret == 0) { |
| 1504 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1505 | if (destoff + olen > inode->i_size) |
| 1506 | btrfs_i_size_write(inode, destoff + olen); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1507 | BTRFS_I(inode)->flags = BTRFS_I(src)->flags; |
| 1508 | ret = btrfs_update_inode(trans, root, inode); |
| 1509 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1510 | btrfs_end_transaction(trans, root); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1511 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1512 | if (ret) |
| 1513 | vmtruncate(inode, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1514 | out_unlock: |
| 1515 | mutex_unlock(&src->i_mutex); |
| 1516 | mutex_unlock(&inode->i_mutex); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1517 | vfree(buf); |
| 1518 | btrfs_free_path(path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1519 | out_fput: |
| 1520 | fput(src_file); |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 1521 | out_drop_write: |
| 1522 | mnt_drop_write(file->f_path.mnt); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1523 | return ret; |
| 1524 | } |
| 1525 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1526 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1527 | { |
| 1528 | struct btrfs_ioctl_clone_range_args args; |
| 1529 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1530 | if (copy_from_user(&args, argp, sizeof(args))) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1531 | return -EFAULT; |
| 1532 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, |
| 1533 | args.src_length, args.dest_offset); |
| 1534 | } |
| 1535 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1536 | /* |
| 1537 | * there are many ways the trans_start and trans_end ioctls can lead |
| 1538 | * to deadlocks. They should only be used by applications that |
| 1539 | * basically own the machine, and have a very in depth understanding |
| 1540 | * of all the possible deadlocks and enospc problems. |
| 1541 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1542 | static long btrfs_ioctl_trans_start(struct file *file) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1543 | { |
| 1544 | struct inode *inode = fdentry(file)->d_inode; |
| 1545 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1546 | struct btrfs_trans_handle *trans; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1547 | int ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1548 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1549 | ret = -EPERM; |
Christoph Hellwig | df5b552 | 2008-06-11 21:53:58 -0400 | [diff] [blame] | 1550 | if (!capable(CAP_SYS_ADMIN)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1551 | goto out; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1552 | |
| 1553 | ret = -EINPROGRESS; |
| 1554 | if (file->private_data) |
| 1555 | goto out; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1556 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1557 | ret = mnt_want_write(file->f_path.mnt); |
| 1558 | if (ret) |
| 1559 | goto out; |
| 1560 | |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1561 | mutex_lock(&root->fs_info->trans_mutex); |
| 1562 | root->fs_info->open_ioctl_trans++; |
| 1563 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1564 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1565 | ret = -ENOMEM; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1566 | trans = btrfs_start_ioctl_transaction(root, 0); |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1567 | if (!trans) |
| 1568 | goto out_drop; |
| 1569 | |
| 1570 | file->private_data = trans; |
| 1571 | return 0; |
| 1572 | |
| 1573 | out_drop: |
| 1574 | mutex_lock(&root->fs_info->trans_mutex); |
| 1575 | root->fs_info->open_ioctl_trans--; |
| 1576 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1577 | mnt_drop_write(file->f_path.mnt); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1578 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1579 | return ret; |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | * there are many ways the trans_start and trans_end ioctls can lead |
| 1584 | * to deadlocks. They should only be used by applications that |
| 1585 | * basically own the machine, and have a very in depth understanding |
| 1586 | * of all the possible deadlocks and enospc problems. |
| 1587 | */ |
| 1588 | long btrfs_ioctl_trans_end(struct file *file) |
| 1589 | { |
| 1590 | struct inode *inode = fdentry(file)->d_inode; |
| 1591 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1592 | struct btrfs_trans_handle *trans; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1593 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1594 | trans = file->private_data; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1595 | if (!trans) |
| 1596 | return -EINVAL; |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 1597 | file->private_data = NULL; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1598 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1599 | btrfs_end_transaction(trans, root); |
| 1600 | |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1601 | mutex_lock(&root->fs_info->trans_mutex); |
| 1602 | root->fs_info->open_ioctl_trans--; |
| 1603 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1604 | |
Sage Weil | cfc8ea8 | 2008-12-11 16:30:06 -0500 | [diff] [blame] | 1605 | mnt_drop_write(file->f_path.mnt); |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1606 | return 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | long btrfs_ioctl(struct file *file, unsigned int |
| 1610 | cmd, unsigned long arg) |
| 1611 | { |
| 1612 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1613 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1614 | |
| 1615 | switch (cmd) { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1616 | case FS_IOC_GETFLAGS: |
| 1617 | return btrfs_ioctl_getflags(file, argp); |
| 1618 | case FS_IOC_SETFLAGS: |
| 1619 | return btrfs_ioctl_setflags(file, argp); |
| 1620 | case FS_IOC_GETVERSION: |
| 1621 | return btrfs_ioctl_getversion(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1622 | case BTRFS_IOC_SNAP_CREATE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1623 | return btrfs_ioctl_snap_create(file, argp, 0); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1624 | case BTRFS_IOC_SUBVOL_CREATE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1625 | return btrfs_ioctl_snap_create(file, argp, 1); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1626 | case BTRFS_IOC_SNAP_DESTROY: |
| 1627 | return btrfs_ioctl_snap_destroy(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1628 | case BTRFS_IOC_DEFRAG: |
| 1629 | return btrfs_ioctl_defrag(file); |
| 1630 | case BTRFS_IOC_RESIZE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1631 | return btrfs_ioctl_resize(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1632 | case BTRFS_IOC_ADD_DEV: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1633 | return btrfs_ioctl_add_dev(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1634 | case BTRFS_IOC_RM_DEV: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1635 | return btrfs_ioctl_rm_dev(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1636 | case BTRFS_IOC_BALANCE: |
| 1637 | return btrfs_balance(root->fs_info->dev_root); |
| 1638 | case BTRFS_IOC_CLONE: |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1639 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
| 1640 | case BTRFS_IOC_CLONE_RANGE: |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1641 | return btrfs_ioctl_clone_range(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1642 | case BTRFS_IOC_TRANS_START: |
| 1643 | return btrfs_ioctl_trans_start(file); |
| 1644 | case BTRFS_IOC_TRANS_END: |
| 1645 | return btrfs_ioctl_trans_end(file); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame^] | 1646 | case BTRFS_IOC_TREE_SEARCH: |
| 1647 | return btrfs_ioctl_tree_search(file, argp); |
| 1648 | case BTRFS_IOC_INO_LOOKUP: |
| 1649 | return btrfs_ioctl_ino_lookup(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1650 | case BTRFS_IOC_SYNC: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1651 | btrfs_sync_fs(file->f_dentry->d_sb, 1); |
| 1652 | return 0; |
| 1653 | } |
| 1654 | |
| 1655 | return -ENOTTY; |
| 1656 | } |