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