Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/bio.h> |
| 21 | #include <linux/buffer_head.h> |
| 22 | #include <linux/file.h> |
| 23 | #include <linux/fs.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 24 | #include <linux/fsnotify.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/highmem.h> |
| 27 | #include <linux/time.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/string.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 30 | #include <linux/backing-dev.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 31 | #include <linux/mount.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 32 | #include <linux/mpage.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 33 | #include <linux/namei.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 34 | #include <linux/swap.h> |
| 35 | #include <linux/writeback.h> |
| 36 | #include <linux/statfs.h> |
| 37 | #include <linux/compat.h> |
| 38 | #include <linux/bit_spinlock.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 39 | #include <linux/security.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 40 | #include <linux/xattr.h> |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 41 | #include <linux/vmalloc.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 42 | #include "compat.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 43 | #include "ctree.h" |
| 44 | #include "disk-io.h" |
| 45 | #include "transaction.h" |
| 46 | #include "btrfs_inode.h" |
| 47 | #include "ioctl.h" |
| 48 | #include "print-tree.h" |
| 49 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 50 | #include "locking.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 51 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 52 | /* Mask out flags that are inappropriate for the given type of inode. */ |
| 53 | static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) |
| 54 | { |
| 55 | if (S_ISDIR(mode)) |
| 56 | return flags; |
| 57 | else if (S_ISREG(mode)) |
| 58 | return flags & ~FS_DIRSYNC_FL; |
| 59 | else |
| 60 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); |
| 61 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 62 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 63 | /* |
| 64 | * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. |
| 65 | */ |
| 66 | static unsigned int btrfs_flags_to_ioctl(unsigned int flags) |
| 67 | { |
| 68 | unsigned int iflags = 0; |
| 69 | |
| 70 | if (flags & BTRFS_INODE_SYNC) |
| 71 | iflags |= FS_SYNC_FL; |
| 72 | if (flags & BTRFS_INODE_IMMUTABLE) |
| 73 | iflags |= FS_IMMUTABLE_FL; |
| 74 | if (flags & BTRFS_INODE_APPEND) |
| 75 | iflags |= FS_APPEND_FL; |
| 76 | if (flags & BTRFS_INODE_NODUMP) |
| 77 | iflags |= FS_NODUMP_FL; |
| 78 | if (flags & BTRFS_INODE_NOATIME) |
| 79 | iflags |= FS_NOATIME_FL; |
| 80 | if (flags & BTRFS_INODE_DIRSYNC) |
| 81 | iflags |= FS_DIRSYNC_FL; |
| 82 | |
| 83 | return iflags; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Update inode->i_flags based on the btrfs internal flags. |
| 88 | */ |
| 89 | void btrfs_update_iflags(struct inode *inode) |
| 90 | { |
| 91 | struct btrfs_inode *ip = BTRFS_I(inode); |
| 92 | |
| 93 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); |
| 94 | |
| 95 | if (ip->flags & BTRFS_INODE_SYNC) |
| 96 | inode->i_flags |= S_SYNC; |
| 97 | if (ip->flags & BTRFS_INODE_IMMUTABLE) |
| 98 | inode->i_flags |= S_IMMUTABLE; |
| 99 | if (ip->flags & BTRFS_INODE_APPEND) |
| 100 | inode->i_flags |= S_APPEND; |
| 101 | if (ip->flags & BTRFS_INODE_NOATIME) |
| 102 | inode->i_flags |= S_NOATIME; |
| 103 | if (ip->flags & BTRFS_INODE_DIRSYNC) |
| 104 | inode->i_flags |= S_DIRSYNC; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Inherit flags from the parent inode. |
| 109 | * |
| 110 | * Unlike extN we don't have any flags we don't want to inherit currently. |
| 111 | */ |
| 112 | void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 113 | { |
Chris Mason | 0b4dcea | 2009-06-11 11:13:35 -0400 | [diff] [blame] | 114 | unsigned int flags; |
| 115 | |
| 116 | if (!dir) |
| 117 | return; |
| 118 | |
| 119 | flags = BTRFS_I(dir)->flags; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 120 | |
| 121 | if (S_ISREG(inode->i_mode)) |
| 122 | flags &= ~BTRFS_INODE_DIRSYNC; |
| 123 | else if (!S_ISDIR(inode->i_mode)) |
| 124 | flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME); |
| 125 | |
| 126 | BTRFS_I(inode)->flags = flags; |
| 127 | btrfs_update_iflags(inode); |
| 128 | } |
| 129 | |
| 130 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) |
| 131 | { |
| 132 | struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode); |
| 133 | unsigned int flags = btrfs_flags_to_ioctl(ip->flags); |
| 134 | |
| 135 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 136 | return -EFAULT; |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) |
| 141 | { |
| 142 | struct inode *inode = file->f_path.dentry->d_inode; |
| 143 | struct btrfs_inode *ip = BTRFS_I(inode); |
| 144 | struct btrfs_root *root = ip->root; |
| 145 | struct btrfs_trans_handle *trans; |
| 146 | unsigned int flags, oldflags; |
| 147 | int ret; |
| 148 | |
| 149 | if (copy_from_user(&flags, arg, sizeof(flags))) |
| 150 | return -EFAULT; |
| 151 | |
| 152 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
| 153 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
| 154 | FS_SYNC_FL | FS_DIRSYNC_FL)) |
| 155 | return -EOPNOTSUPP; |
| 156 | |
| 157 | if (!is_owner_or_cap(inode)) |
| 158 | return -EACCES; |
| 159 | |
| 160 | mutex_lock(&inode->i_mutex); |
| 161 | |
| 162 | flags = btrfs_mask_flags(inode->i_mode, flags); |
| 163 | oldflags = btrfs_flags_to_ioctl(ip->flags); |
| 164 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
| 165 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 166 | ret = -EPERM; |
| 167 | goto out_unlock; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | ret = mnt_want_write(file->f_path.mnt); |
| 172 | if (ret) |
| 173 | goto out_unlock; |
| 174 | |
| 175 | if (flags & FS_SYNC_FL) |
| 176 | ip->flags |= BTRFS_INODE_SYNC; |
| 177 | else |
| 178 | ip->flags &= ~BTRFS_INODE_SYNC; |
| 179 | if (flags & FS_IMMUTABLE_FL) |
| 180 | ip->flags |= BTRFS_INODE_IMMUTABLE; |
| 181 | else |
| 182 | ip->flags &= ~BTRFS_INODE_IMMUTABLE; |
| 183 | if (flags & FS_APPEND_FL) |
| 184 | ip->flags |= BTRFS_INODE_APPEND; |
| 185 | else |
| 186 | ip->flags &= ~BTRFS_INODE_APPEND; |
| 187 | if (flags & FS_NODUMP_FL) |
| 188 | ip->flags |= BTRFS_INODE_NODUMP; |
| 189 | else |
| 190 | ip->flags &= ~BTRFS_INODE_NODUMP; |
| 191 | if (flags & FS_NOATIME_FL) |
| 192 | ip->flags |= BTRFS_INODE_NOATIME; |
| 193 | else |
| 194 | ip->flags &= ~BTRFS_INODE_NOATIME; |
| 195 | if (flags & FS_DIRSYNC_FL) |
| 196 | ip->flags |= BTRFS_INODE_DIRSYNC; |
| 197 | else |
| 198 | ip->flags &= ~BTRFS_INODE_DIRSYNC; |
| 199 | |
| 200 | |
| 201 | trans = btrfs_join_transaction(root, 1); |
| 202 | BUG_ON(!trans); |
| 203 | |
| 204 | ret = btrfs_update_inode(trans, root, inode); |
| 205 | BUG_ON(ret); |
| 206 | |
| 207 | btrfs_update_iflags(inode); |
| 208 | inode->i_ctime = CURRENT_TIME; |
| 209 | btrfs_end_transaction(trans, root); |
| 210 | |
| 211 | mnt_drop_write(file->f_path.mnt); |
| 212 | out_unlock: |
| 213 | mutex_unlock(&inode->i_mutex); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) |
| 218 | { |
| 219 | struct inode *inode = file->f_path.dentry->d_inode; |
| 220 | |
| 221 | return put_user(inode->i_generation, arg); |
| 222 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 223 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 224 | static noinline int create_subvol(struct btrfs_root *root, |
| 225 | struct dentry *dentry, |
| 226 | char *name, int namelen) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 227 | { |
| 228 | struct btrfs_trans_handle *trans; |
| 229 | struct btrfs_key key; |
| 230 | struct btrfs_root_item root_item; |
| 231 | struct btrfs_inode_item *inode_item; |
| 232 | struct extent_buffer *leaf; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 233 | struct btrfs_root *new_root; |
| 234 | struct inode *dir = dentry->d_parent->d_inode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 235 | int ret; |
| 236 | int err; |
| 237 | u64 objectid; |
| 238 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 239 | u64 index = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 240 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 241 | /* |
| 242 | * 1 - inode item |
| 243 | * 2 - refs |
| 244 | * 1 - root item |
| 245 | * 2 - dir items |
| 246 | */ |
| 247 | ret = btrfs_reserve_metadata_space(root, 6); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 248 | if (ret) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 249 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 250 | |
| 251 | trans = btrfs_start_transaction(root, 1); |
| 252 | BUG_ON(!trans); |
| 253 | |
| 254 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 255 | 0, &objectid); |
| 256 | if (ret) |
| 257 | goto fail; |
| 258 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 259 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
| 260 | 0, objectid, NULL, 0, 0, 0); |
Josef Bacik | 8e8a1e3 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 261 | if (IS_ERR(leaf)) { |
| 262 | ret = PTR_ERR(leaf); |
| 263 | goto fail; |
| 264 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 265 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 266 | memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 267 | btrfs_set_header_bytenr(leaf, leaf->start); |
| 268 | btrfs_set_header_generation(leaf, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 269 | btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 270 | btrfs_set_header_owner(leaf, objectid); |
| 271 | |
| 272 | write_extent_buffer(leaf, root->fs_info->fsid, |
| 273 | (unsigned long)btrfs_header_fsid(leaf), |
| 274 | BTRFS_FSID_SIZE); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 275 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 276 | (unsigned long)btrfs_header_chunk_tree_uuid(leaf), |
| 277 | BTRFS_UUID_SIZE); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 278 | btrfs_mark_buffer_dirty(leaf); |
| 279 | |
| 280 | inode_item = &root_item.inode; |
| 281 | memset(inode_item, 0, sizeof(*inode_item)); |
| 282 | inode_item->generation = cpu_to_le64(1); |
| 283 | inode_item->size = cpu_to_le64(3); |
| 284 | inode_item->nlink = cpu_to_le32(1); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 285 | inode_item->nbytes = cpu_to_le64(root->leafsize); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 286 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
| 287 | |
| 288 | btrfs_set_root_bytenr(&root_item, leaf->start); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 289 | btrfs_set_root_generation(&root_item, trans->transid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 290 | btrfs_set_root_level(&root_item, 0); |
| 291 | btrfs_set_root_refs(&root_item, 1); |
Yan, Zheng | 86b9f2e | 2009-11-12 09:36:50 +0000 | [diff] [blame] | 292 | btrfs_set_root_used(&root_item, leaf->len); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 293 | btrfs_set_root_last_snapshot(&root_item, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 294 | |
| 295 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
| 296 | root_item.drop_level = 0; |
| 297 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 298 | btrfs_tree_unlock(leaf); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 299 | free_extent_buffer(leaf); |
| 300 | leaf = NULL; |
| 301 | |
| 302 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 303 | |
| 304 | key.objectid = objectid; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 305 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 306 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 307 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 308 | &root_item); |
| 309 | if (ret) |
| 310 | goto fail; |
| 311 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 312 | key.offset = (u64)-1; |
| 313 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); |
| 314 | BUG_ON(IS_ERR(new_root)); |
| 315 | |
| 316 | btrfs_record_root_in_trans(trans, new_root); |
| 317 | |
| 318 | ret = btrfs_create_subvol_root(trans, new_root, new_dirid, |
| 319 | BTRFS_I(dir)->block_group); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 320 | /* |
| 321 | * insert the directory item |
| 322 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 323 | ret = btrfs_set_inode_index(dir, &index); |
| 324 | BUG_ON(ret); |
| 325 | |
| 326 | ret = btrfs_insert_dir_item(trans, root, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 327 | name, namelen, dir->i_ino, &key, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 328 | BTRFS_FT_DIR, index); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 329 | if (ret) |
| 330 | goto fail; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 331 | |
Yan Zheng | 52c2617 | 2009-01-05 15:43:43 -0500 | [diff] [blame] | 332 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
| 333 | ret = btrfs_update_inode(trans, root, dir); |
| 334 | BUG_ON(ret); |
| 335 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 336 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 337 | objectid, root->root_key.objectid, |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 338 | dir->i_ino, index, name, namelen); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 339 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 340 | BUG_ON(ret); |
| 341 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 342 | d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 343 | fail: |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 344 | err = btrfs_commit_transaction(trans, root); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 345 | if (err && !ret) |
| 346 | ret = err; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 347 | |
| 348 | btrfs_unreserve_metadata_space(root, 6); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 349 | return ret; |
| 350 | } |
| 351 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 352 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, |
| 353 | char *name, int namelen) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 354 | { |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 355 | struct inode *inode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 356 | struct btrfs_pending_snapshot *pending_snapshot; |
| 357 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 358 | int ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 359 | |
| 360 | if (!root->ref_cows) |
| 361 | return -EINVAL; |
| 362 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 363 | /* |
| 364 | * 1 - inode item |
| 365 | * 2 - refs |
| 366 | * 1 - root item |
| 367 | * 2 - dir items |
| 368 | */ |
| 369 | ret = btrfs_reserve_metadata_space(root, 6); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 370 | if (ret) |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 371 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 372 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 373 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 374 | if (!pending_snapshot) { |
| 375 | ret = -ENOMEM; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 376 | btrfs_unreserve_metadata_space(root, 6); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 377 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 378 | } |
| 379 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); |
| 380 | if (!pending_snapshot->name) { |
| 381 | ret = -ENOMEM; |
| 382 | kfree(pending_snapshot); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 383 | btrfs_unreserve_metadata_space(root, 6); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 384 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 385 | } |
| 386 | memcpy(pending_snapshot->name, name, namelen); |
| 387 | pending_snapshot->name[namelen] = '\0'; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 388 | pending_snapshot->dentry = dentry; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 389 | trans = btrfs_start_transaction(root, 1); |
| 390 | BUG_ON(!trans); |
| 391 | pending_snapshot->root = root; |
| 392 | list_add(&pending_snapshot->list, |
| 393 | &trans->transaction->pending_snapshots); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 394 | ret = btrfs_commit_transaction(trans, root); |
| 395 | BUG_ON(ret); |
| 396 | btrfs_unreserve_metadata_space(root, 6); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 397 | |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 398 | inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); |
| 399 | if (IS_ERR(inode)) { |
| 400 | ret = PTR_ERR(inode); |
| 401 | goto fail; |
| 402 | } |
| 403 | BUG_ON(!inode); |
| 404 | d_instantiate(dentry, inode); |
| 405 | ret = 0; |
| 406 | fail: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 407 | return ret; |
| 408 | } |
| 409 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 410 | /* copy of may_create in fs/namei.c() */ |
| 411 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) |
| 412 | { |
| 413 | if (child->d_inode) |
| 414 | return -EEXIST; |
| 415 | if (IS_DEADDIR(dir)) |
| 416 | return -ENOENT; |
| 417 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Create a new subvolume below @parent. This is largely modeled after |
| 422 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup |
| 423 | * inside this filesystem so it's quite a bit simpler. |
| 424 | */ |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 425 | static noinline int btrfs_mksubvol(struct path *parent, |
| 426 | char *name, int namelen, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 427 | struct btrfs_root *snap_src) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 428 | { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 429 | struct inode *dir = parent->dentry->d_inode; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 430 | struct dentry *dentry; |
| 431 | int error; |
| 432 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 433 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 434 | |
| 435 | dentry = lookup_one_len(name, parent->dentry, namelen); |
| 436 | error = PTR_ERR(dentry); |
| 437 | if (IS_ERR(dentry)) |
| 438 | goto out_unlock; |
| 439 | |
| 440 | error = -EEXIST; |
| 441 | if (dentry->d_inode) |
| 442 | goto out_dput; |
| 443 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 444 | error = mnt_want_write(parent->mnt); |
| 445 | if (error) |
| 446 | goto out_dput; |
| 447 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 448 | error = btrfs_may_create(dir, dentry); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 449 | if (error) |
| 450 | goto out_drop_write; |
| 451 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 452 | down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
| 453 | |
| 454 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) |
| 455 | goto out_up_read; |
| 456 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 457 | if (snap_src) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 458 | error = create_snapshot(snap_src, dentry, |
| 459 | name, namelen); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 460 | } else { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 461 | error = create_subvol(BTRFS_I(dir)->root, dentry, |
| 462 | name, namelen); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 463 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 464 | if (!error) |
| 465 | fsnotify_mkdir(dir, dentry); |
| 466 | out_up_read: |
| 467 | up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 468 | out_drop_write: |
| 469 | mnt_drop_write(parent->mnt); |
| 470 | out_dput: |
| 471 | dput(dentry); |
| 472 | out_unlock: |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 473 | mutex_unlock(&dir->i_mutex); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 474 | return error; |
| 475 | } |
| 476 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 477 | static int btrfs_defrag_file(struct file *file) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 478 | { |
| 479 | struct inode *inode = fdentry(file)->d_inode; |
| 480 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 481 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 482 | struct btrfs_ordered_extent *ordered; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 483 | struct page *page; |
| 484 | unsigned long last_index; |
| 485 | unsigned long ra_pages = root->fs_info->bdi.ra_pages; |
| 486 | unsigned long total_read = 0; |
| 487 | u64 page_start; |
| 488 | u64 page_end; |
| 489 | unsigned long i; |
| 490 | int ret; |
| 491 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 492 | ret = btrfs_check_data_free_space(root, inode, inode->i_size); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 493 | if (ret) |
| 494 | return -ENOSPC; |
| 495 | |
| 496 | mutex_lock(&inode->i_mutex); |
| 497 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 498 | for (i = 0; i <= last_index; i++) { |
| 499 | if (total_read % ra_pages == 0) { |
| 500 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, |
| 501 | min(last_index, i + ra_pages - 1)); |
| 502 | } |
| 503 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 504 | again: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 505 | page = grab_cache_page(inode->i_mapping, i); |
| 506 | if (!page) |
| 507 | goto out_unlock; |
| 508 | if (!PageUptodate(page)) { |
| 509 | btrfs_readpage(NULL, page); |
| 510 | lock_page(page); |
| 511 | if (!PageUptodate(page)) { |
| 512 | unlock_page(page); |
| 513 | page_cache_release(page); |
| 514 | goto out_unlock; |
| 515 | } |
| 516 | } |
| 517 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 518 | wait_on_page_writeback(page); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 519 | |
| 520 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 521 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 522 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 523 | |
| 524 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 525 | if (ordered) { |
| 526 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 527 | unlock_page(page); |
| 528 | page_cache_release(page); |
| 529 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 530 | btrfs_put_ordered_extent(ordered); |
| 531 | goto again; |
| 532 | } |
| 533 | set_page_extent_mapped(page); |
| 534 | |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 535 | /* |
| 536 | * this makes sure page_mkwrite is called on the |
| 537 | * page if it is dirtied again later |
| 538 | */ |
| 539 | clear_page_dirty_for_io(page); |
| 540 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 541 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 542 | set_page_dirty(page); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 543 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 544 | unlock_page(page); |
| 545 | page_cache_release(page); |
| 546 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); |
| 547 | } |
| 548 | |
| 549 | out_unlock: |
| 550 | mutex_unlock(&inode->i_mutex); |
| 551 | return 0; |
| 552 | } |
| 553 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 554 | static noinline int btrfs_ioctl_resize(struct btrfs_root *root, |
| 555 | void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 556 | { |
| 557 | u64 new_size; |
| 558 | u64 old_size; |
| 559 | u64 devid = 1; |
| 560 | struct btrfs_ioctl_vol_args *vol_args; |
| 561 | struct btrfs_trans_handle *trans; |
| 562 | struct btrfs_device *device = NULL; |
| 563 | char *sizestr; |
| 564 | char *devstr = NULL; |
| 565 | int ret = 0; |
| 566 | int namelen; |
| 567 | int mod = 0; |
| 568 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 569 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 570 | return -EROFS; |
| 571 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 572 | if (!capable(CAP_SYS_ADMIN)) |
| 573 | return -EPERM; |
| 574 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 575 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 576 | if (IS_ERR(vol_args)) |
| 577 | return PTR_ERR(vol_args); |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 578 | |
| 579 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 580 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 581 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 582 | mutex_lock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 583 | sizestr = vol_args->name; |
| 584 | devstr = strchr(sizestr, ':'); |
| 585 | if (devstr) { |
| 586 | char *end; |
| 587 | sizestr = devstr + 1; |
| 588 | *devstr = '\0'; |
| 589 | devstr = vol_args->name; |
| 590 | devid = simple_strtoull(devstr, &end, 10); |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 591 | printk(KERN_INFO "resizing devid %llu\n", |
| 592 | (unsigned long long)devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 593 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 594 | device = btrfs_find_device(root, devid, NULL, NULL); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 595 | if (!device) { |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 596 | printk(KERN_INFO "resizer unable to find device %llu\n", |
| 597 | (unsigned long long)devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 598 | ret = -EINVAL; |
| 599 | goto out_unlock; |
| 600 | } |
| 601 | if (!strcmp(sizestr, "max")) |
| 602 | new_size = device->bdev->bd_inode->i_size; |
| 603 | else { |
| 604 | if (sizestr[0] == '-') { |
| 605 | mod = -1; |
| 606 | sizestr++; |
| 607 | } else if (sizestr[0] == '+') { |
| 608 | mod = 1; |
| 609 | sizestr++; |
| 610 | } |
| 611 | new_size = btrfs_parse_size(sizestr); |
| 612 | if (new_size == 0) { |
| 613 | ret = -EINVAL; |
| 614 | goto out_unlock; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | old_size = device->total_bytes; |
| 619 | |
| 620 | if (mod < 0) { |
| 621 | if (new_size > old_size) { |
| 622 | ret = -EINVAL; |
| 623 | goto out_unlock; |
| 624 | } |
| 625 | new_size = old_size - new_size; |
| 626 | } else if (mod > 0) { |
| 627 | new_size = old_size + new_size; |
| 628 | } |
| 629 | |
| 630 | if (new_size < 256 * 1024 * 1024) { |
| 631 | ret = -EINVAL; |
| 632 | goto out_unlock; |
| 633 | } |
| 634 | if (new_size > device->bdev->bd_inode->i_size) { |
| 635 | ret = -EFBIG; |
| 636 | goto out_unlock; |
| 637 | } |
| 638 | |
| 639 | do_div(new_size, root->sectorsize); |
| 640 | new_size *= root->sectorsize; |
| 641 | |
| 642 | printk(KERN_INFO "new size for %s is %llu\n", |
| 643 | device->name, (unsigned long long)new_size); |
| 644 | |
| 645 | if (new_size > old_size) { |
| 646 | trans = btrfs_start_transaction(root, 1); |
| 647 | ret = btrfs_grow_device(trans, device, new_size); |
| 648 | btrfs_commit_transaction(trans, root); |
| 649 | } else { |
| 650 | ret = btrfs_shrink_device(device, new_size); |
| 651 | } |
| 652 | |
| 653 | out_unlock: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 654 | mutex_unlock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 655 | kfree(vol_args); |
| 656 | return ret; |
| 657 | } |
| 658 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 659 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 660 | void __user *arg, int subvol) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 661 | { |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 662 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 663 | struct btrfs_ioctl_vol_args *vol_args; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 664 | struct file *src_file; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 665 | int namelen; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 666 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 667 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 668 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 669 | return -EROFS; |
| 670 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 671 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 672 | if (IS_ERR(vol_args)) |
| 673 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 674 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 675 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 676 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 677 | if (strchr(vol_args->name, '/')) { |
| 678 | ret = -EINVAL; |
| 679 | goto out; |
| 680 | } |
| 681 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 682 | if (subvol) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 683 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen, |
| 684 | NULL); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 685 | } else { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 686 | struct inode *src_inode; |
| 687 | src_file = fget(vol_args->fd); |
| 688 | if (!src_file) { |
| 689 | ret = -EINVAL; |
| 690 | goto out; |
| 691 | } |
| 692 | |
| 693 | src_inode = src_file->f_path.dentry->d_inode; |
| 694 | 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] | 695 | printk(KERN_INFO "btrfs: Snapshot src from " |
| 696 | "another FS\n"); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 697 | ret = -EINVAL; |
| 698 | fput(src_file); |
| 699 | goto out; |
| 700 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 701 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen, |
| 702 | BTRFS_I(src_inode)->root); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 703 | fput(src_file); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 704 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 705 | out: |
| 706 | kfree(vol_args); |
| 707 | return ret; |
| 708 | } |
| 709 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 710 | /* |
| 711 | * helper to check if the subvolume references other subvolumes |
| 712 | */ |
| 713 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
| 714 | { |
| 715 | struct btrfs_path *path; |
| 716 | struct btrfs_key key; |
| 717 | int ret; |
| 718 | |
| 719 | path = btrfs_alloc_path(); |
| 720 | if (!path) |
| 721 | return -ENOMEM; |
| 722 | |
| 723 | key.objectid = root->root_key.objectid; |
| 724 | key.type = BTRFS_ROOT_REF_KEY; |
| 725 | key.offset = (u64)-1; |
| 726 | |
| 727 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, |
| 728 | &key, path, 0, 0); |
| 729 | if (ret < 0) |
| 730 | goto out; |
| 731 | BUG_ON(ret == 0); |
| 732 | |
| 733 | ret = 0; |
| 734 | if (path->slots[0] > 0) { |
| 735 | path->slots[0]--; |
| 736 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 737 | if (key.objectid == root->root_key.objectid && |
| 738 | key.type == BTRFS_ROOT_REF_KEY) |
| 739 | ret = -ENOTEMPTY; |
| 740 | } |
| 741 | out: |
| 742 | btrfs_free_path(path); |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
| 747 | void __user *arg) |
| 748 | { |
| 749 | struct dentry *parent = fdentry(file); |
| 750 | struct dentry *dentry; |
| 751 | struct inode *dir = parent->d_inode; |
| 752 | struct inode *inode; |
| 753 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 754 | struct btrfs_root *dest = NULL; |
| 755 | struct btrfs_ioctl_vol_args *vol_args; |
| 756 | struct btrfs_trans_handle *trans; |
| 757 | int namelen; |
| 758 | int ret; |
| 759 | int err = 0; |
| 760 | |
| 761 | if (!capable(CAP_SYS_ADMIN)) |
| 762 | return -EPERM; |
| 763 | |
| 764 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 765 | if (IS_ERR(vol_args)) |
| 766 | return PTR_ERR(vol_args); |
| 767 | |
| 768 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
| 769 | namelen = strlen(vol_args->name); |
| 770 | if (strchr(vol_args->name, '/') || |
| 771 | strncmp(vol_args->name, "..", namelen) == 0) { |
| 772 | err = -EINVAL; |
| 773 | goto out; |
| 774 | } |
| 775 | |
| 776 | err = mnt_want_write(file->f_path.mnt); |
| 777 | if (err) |
| 778 | goto out; |
| 779 | |
| 780 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
| 781 | dentry = lookup_one_len(vol_args->name, parent, namelen); |
| 782 | if (IS_ERR(dentry)) { |
| 783 | err = PTR_ERR(dentry); |
| 784 | goto out_unlock_dir; |
| 785 | } |
| 786 | |
| 787 | if (!dentry->d_inode) { |
| 788 | err = -ENOENT; |
| 789 | goto out_dput; |
| 790 | } |
| 791 | |
| 792 | inode = dentry->d_inode; |
| 793 | if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 794 | err = -EINVAL; |
| 795 | goto out_dput; |
| 796 | } |
| 797 | |
| 798 | dest = BTRFS_I(inode)->root; |
| 799 | |
| 800 | mutex_lock(&inode->i_mutex); |
| 801 | err = d_invalidate(dentry); |
| 802 | if (err) |
| 803 | goto out_unlock; |
| 804 | |
| 805 | down_write(&root->fs_info->subvol_sem); |
| 806 | |
| 807 | err = may_destroy_subvol(dest); |
| 808 | if (err) |
| 809 | goto out_up_write; |
| 810 | |
| 811 | trans = btrfs_start_transaction(root, 1); |
| 812 | ret = btrfs_unlink_subvol(trans, root, dir, |
| 813 | dest->root_key.objectid, |
| 814 | dentry->d_name.name, |
| 815 | dentry->d_name.len); |
| 816 | BUG_ON(ret); |
| 817 | |
| 818 | btrfs_record_root_in_trans(trans, dest); |
| 819 | |
| 820 | memset(&dest->root_item.drop_progress, 0, |
| 821 | sizeof(dest->root_item.drop_progress)); |
| 822 | dest->root_item.drop_level = 0; |
| 823 | btrfs_set_root_refs(&dest->root_item, 0); |
| 824 | |
| 825 | ret = btrfs_insert_orphan_item(trans, |
| 826 | root->fs_info->tree_root, |
| 827 | dest->root_key.objectid); |
| 828 | BUG_ON(ret); |
| 829 | |
| 830 | ret = btrfs_commit_transaction(trans, root); |
| 831 | BUG_ON(ret); |
| 832 | inode->i_flags |= S_DEAD; |
| 833 | out_up_write: |
| 834 | up_write(&root->fs_info->subvol_sem); |
| 835 | out_unlock: |
| 836 | mutex_unlock(&inode->i_mutex); |
| 837 | if (!err) { |
Yan, Zheng | efefb14 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 838 | shrink_dcache_sb(root->fs_info->sb); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 839 | btrfs_invalidate_inodes(dest); |
| 840 | d_delete(dentry); |
| 841 | } |
| 842 | out_dput: |
| 843 | dput(dentry); |
| 844 | out_unlock_dir: |
| 845 | mutex_unlock(&dir->i_mutex); |
| 846 | mnt_drop_write(file->f_path.mnt); |
| 847 | out: |
| 848 | kfree(vol_args); |
| 849 | return err; |
| 850 | } |
| 851 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 852 | static int btrfs_ioctl_defrag(struct file *file) |
| 853 | { |
| 854 | struct inode *inode = fdentry(file)->d_inode; |
| 855 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 856 | int ret; |
| 857 | |
| 858 | ret = mnt_want_write(file->f_path.mnt); |
| 859 | if (ret) |
| 860 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 861 | |
| 862 | switch (inode->i_mode & S_IFMT) { |
| 863 | case S_IFDIR: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 864 | if (!capable(CAP_SYS_ADMIN)) { |
| 865 | ret = -EPERM; |
| 866 | goto out; |
| 867 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 868 | btrfs_defrag_root(root, 0); |
| 869 | btrfs_defrag_root(root->fs_info->extent_root, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 870 | break; |
| 871 | case S_IFREG: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 872 | if (!(file->f_mode & FMODE_WRITE)) { |
| 873 | ret = -EINVAL; |
| 874 | goto out; |
| 875 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 876 | btrfs_defrag_file(file); |
| 877 | break; |
| 878 | } |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 879 | out: |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 880 | mnt_drop_write(file->f_path.mnt); |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 881 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 882 | } |
| 883 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 884 | 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] | 885 | { |
| 886 | struct btrfs_ioctl_vol_args *vol_args; |
| 887 | int ret; |
| 888 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 889 | if (!capable(CAP_SYS_ADMIN)) |
| 890 | return -EPERM; |
| 891 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 892 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 893 | if (IS_ERR(vol_args)) |
| 894 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 895 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 896 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 897 | ret = btrfs_init_new_device(root, vol_args->name); |
| 898 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 899 | kfree(vol_args); |
| 900 | return ret; |
| 901 | } |
| 902 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 903 | 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] | 904 | { |
| 905 | struct btrfs_ioctl_vol_args *vol_args; |
| 906 | int ret; |
| 907 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 908 | if (!capable(CAP_SYS_ADMIN)) |
| 909 | return -EPERM; |
| 910 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 911 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 912 | return -EROFS; |
| 913 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 914 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 915 | if (IS_ERR(vol_args)) |
| 916 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 917 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 918 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 919 | ret = btrfs_rm_device(root, vol_args->name); |
| 920 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 921 | kfree(vol_args); |
| 922 | return ret; |
| 923 | } |
| 924 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 925 | static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, |
| 926 | u64 off, u64 olen, u64 destoff) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 927 | { |
| 928 | struct inode *inode = fdentry(file)->d_inode; |
| 929 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 930 | struct file *src_file; |
| 931 | struct inode *src; |
| 932 | struct btrfs_trans_handle *trans; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 933 | struct btrfs_path *path; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 934 | struct extent_buffer *leaf; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 935 | char *buf; |
| 936 | struct btrfs_key key; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 937 | u32 nritems; |
| 938 | int slot; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 939 | int ret; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 940 | u64 len = olen; |
| 941 | u64 bs = root->fs_info->sb->s_blocksize; |
| 942 | u64 hint_byte; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 943 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 944 | /* |
| 945 | * TODO: |
| 946 | * - split compressed inline extents. annoying: we need to |
| 947 | * decompress into destination's address_space (the file offset |
| 948 | * may change, so source mapping won't do), then recompress (or |
| 949 | * otherwise reinsert) a subrange. |
| 950 | * - allow ranges within the same file to be cloned (provided |
| 951 | * they don't overlap)? |
| 952 | */ |
| 953 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 954 | /* the destination must be opened for writing */ |
| 955 | if (!(file->f_mode & FMODE_WRITE)) |
| 956 | return -EINVAL; |
| 957 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 958 | ret = mnt_want_write(file->f_path.mnt); |
| 959 | if (ret) |
| 960 | return ret; |
| 961 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 962 | src_file = fget(srcfd); |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 963 | if (!src_file) { |
| 964 | ret = -EBADF; |
| 965 | goto out_drop_write; |
| 966 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 967 | src = src_file->f_dentry->d_inode; |
| 968 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 969 | ret = -EINVAL; |
| 970 | if (src == inode) |
| 971 | goto out_fput; |
| 972 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 973 | ret = -EISDIR; |
| 974 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 975 | goto out_fput; |
| 976 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 977 | ret = -EXDEV; |
| 978 | if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) |
| 979 | goto out_fput; |
| 980 | |
| 981 | ret = -ENOMEM; |
| 982 | buf = vmalloc(btrfs_level_size(root, 0)); |
| 983 | if (!buf) |
| 984 | goto out_fput; |
| 985 | |
| 986 | path = btrfs_alloc_path(); |
| 987 | if (!path) { |
| 988 | vfree(buf); |
| 989 | goto out_fput; |
| 990 | } |
| 991 | path->reada = 2; |
| 992 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 993 | if (inode < src) { |
| 994 | mutex_lock(&inode->i_mutex); |
| 995 | mutex_lock(&src->i_mutex); |
| 996 | } else { |
| 997 | mutex_lock(&src->i_mutex); |
| 998 | mutex_lock(&inode->i_mutex); |
| 999 | } |
| 1000 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1001 | /* determine range to clone */ |
| 1002 | ret = -EINVAL; |
| 1003 | if (off >= src->i_size || off + len > src->i_size) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1004 | goto out_unlock; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1005 | if (len == 0) |
| 1006 | olen = len = src->i_size - off; |
| 1007 | /* if we extend to eof, continue to block boundary */ |
| 1008 | if (off + len == src->i_size) |
| 1009 | len = ((src->i_size + bs-1) & ~(bs-1)) |
| 1010 | - off; |
| 1011 | |
| 1012 | /* verify the end result is block aligned */ |
| 1013 | if ((off & (bs-1)) || |
| 1014 | ((off + len) & (bs-1))) |
| 1015 | goto out_unlock; |
| 1016 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1017 | /* do any pending delalloc/csum calc on src, one way or |
| 1018 | another, and lock file content */ |
| 1019 | while (1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1020 | struct btrfs_ordered_extent *ordered; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1021 | lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
| 1022 | ordered = btrfs_lookup_first_ordered_extent(inode, off+len); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1023 | if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1024 | break; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1025 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1026 | if (ordered) |
| 1027 | btrfs_put_ordered_extent(ordered); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1028 | btrfs_wait_ordered_range(src, off, off+len); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1029 | } |
| 1030 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1031 | trans = btrfs_start_transaction(root, 1); |
| 1032 | BUG_ON(!trans); |
| 1033 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1034 | /* punch hole in destination first */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 1035 | btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1036 | |
| 1037 | /* clone data */ |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1038 | key.objectid = src->i_ino; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1039 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 1040 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1041 | |
| 1042 | while (1) { |
| 1043 | /* |
| 1044 | * note the key will change type as we walk through the |
| 1045 | * tree. |
| 1046 | */ |
| 1047 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); |
| 1048 | if (ret < 0) |
| 1049 | goto out; |
| 1050 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1051 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 1052 | if (path->slots[0] >= nritems) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1053 | ret = btrfs_next_leaf(root, path); |
| 1054 | if (ret < 0) |
| 1055 | goto out; |
| 1056 | if (ret > 0) |
| 1057 | break; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1058 | nritems = btrfs_header_nritems(path->nodes[0]); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1059 | } |
| 1060 | leaf = path->nodes[0]; |
| 1061 | slot = path->slots[0]; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1062 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1063 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1064 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1065 | key.objectid != src->i_ino) |
| 1066 | break; |
| 1067 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1068 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
| 1069 | struct btrfs_file_extent_item *extent; |
| 1070 | int type; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1071 | u32 size; |
| 1072 | struct btrfs_key new_key; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1073 | u64 disko = 0, diskl = 0; |
| 1074 | u64 datao = 0, datal = 0; |
| 1075 | u8 comp; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1076 | |
| 1077 | size = btrfs_item_size_nr(leaf, slot); |
| 1078 | read_extent_buffer(leaf, buf, |
| 1079 | btrfs_item_ptr_offset(leaf, slot), |
| 1080 | size); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1081 | |
| 1082 | extent = btrfs_item_ptr(leaf, slot, |
| 1083 | struct btrfs_file_extent_item); |
| 1084 | comp = btrfs_file_extent_compression(leaf, extent); |
| 1085 | type = btrfs_file_extent_type(leaf, extent); |
Chris Mason | c8a894d | 2009-06-27 21:07:03 -0400 | [diff] [blame] | 1086 | if (type == BTRFS_FILE_EXTENT_REG || |
| 1087 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1088 | disko = btrfs_file_extent_disk_bytenr(leaf, |
| 1089 | extent); |
| 1090 | diskl = btrfs_file_extent_disk_num_bytes(leaf, |
| 1091 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1092 | datao = btrfs_file_extent_offset(leaf, extent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1093 | datal = btrfs_file_extent_num_bytes(leaf, |
| 1094 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1095 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 1096 | /* take upper bound, may be compressed */ |
| 1097 | datal = btrfs_file_extent_ram_bytes(leaf, |
| 1098 | extent); |
| 1099 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1100 | btrfs_release_path(root, path); |
| 1101 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1102 | if (key.offset + datal < off || |
| 1103 | key.offset >= off+len) |
| 1104 | goto next; |
| 1105 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1106 | memcpy(&new_key, &key, sizeof(new_key)); |
| 1107 | new_key.objectid = inode->i_ino; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1108 | new_key.offset = key.offset + destoff - off; |
| 1109 | |
Chris Mason | c8a894d | 2009-06-27 21:07:03 -0400 | [diff] [blame] | 1110 | if (type == BTRFS_FILE_EXTENT_REG || |
| 1111 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1112 | ret = btrfs_insert_empty_item(trans, root, path, |
| 1113 | &new_key, size); |
| 1114 | if (ret) |
| 1115 | goto out; |
| 1116 | |
| 1117 | leaf = path->nodes[0]; |
| 1118 | slot = path->slots[0]; |
| 1119 | write_extent_buffer(leaf, buf, |
| 1120 | btrfs_item_ptr_offset(leaf, slot), |
| 1121 | size); |
| 1122 | |
| 1123 | extent = btrfs_item_ptr(leaf, slot, |
| 1124 | struct btrfs_file_extent_item); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1125 | |
| 1126 | if (off > key.offset) { |
| 1127 | datao += off - key.offset; |
| 1128 | datal -= off - key.offset; |
| 1129 | } |
Chris Mason | ac6889c | 2009-10-09 11:29:53 -0400 | [diff] [blame] | 1130 | |
| 1131 | if (key.offset + datal > off + len) |
| 1132 | datal = off + len - key.offset; |
| 1133 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1134 | /* disko == 0 means it's a hole */ |
| 1135 | if (!disko) |
| 1136 | datao = 0; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1137 | |
| 1138 | btrfs_set_file_extent_offset(leaf, extent, |
| 1139 | datao); |
| 1140 | btrfs_set_file_extent_num_bytes(leaf, extent, |
| 1141 | datal); |
| 1142 | if (disko) { |
| 1143 | inode_add_bytes(inode, datal); |
| 1144 | ret = btrfs_inc_extent_ref(trans, root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1145 | disko, diskl, 0, |
| 1146 | root->root_key.objectid, |
| 1147 | inode->i_ino, |
| 1148 | new_key.offset - datao); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1149 | BUG_ON(ret); |
| 1150 | } |
| 1151 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 1152 | u64 skip = 0; |
| 1153 | u64 trim = 0; |
| 1154 | if (off > key.offset) { |
| 1155 | skip = off - key.offset; |
| 1156 | new_key.offset += skip; |
| 1157 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1158 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1159 | if (key.offset + datal > off+len) |
| 1160 | trim = key.offset + datal - (off+len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1161 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1162 | if (comp && (skip || trim)) { |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1163 | ret = -EINVAL; |
| 1164 | goto out; |
| 1165 | } |
| 1166 | size -= skip + trim; |
| 1167 | datal -= skip + trim; |
| 1168 | ret = btrfs_insert_empty_item(trans, root, path, |
| 1169 | &new_key, size); |
| 1170 | if (ret) |
| 1171 | goto out; |
| 1172 | |
| 1173 | if (skip) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1174 | u32 start = |
| 1175 | btrfs_file_extent_calc_inline_size(0); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1176 | memmove(buf+start, buf+start+skip, |
| 1177 | datal); |
| 1178 | } |
| 1179 | |
| 1180 | leaf = path->nodes[0]; |
| 1181 | slot = path->slots[0]; |
| 1182 | write_extent_buffer(leaf, buf, |
| 1183 | btrfs_item_ptr_offset(leaf, slot), |
| 1184 | size); |
| 1185 | inode_add_bytes(inode, datal); |
| 1186 | } |
| 1187 | |
| 1188 | btrfs_mark_buffer_dirty(leaf); |
| 1189 | } |
| 1190 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1191 | next: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1192 | btrfs_release_path(root, path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1193 | key.offset++; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1194 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1195 | ret = 0; |
| 1196 | out: |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1197 | btrfs_release_path(root, path); |
| 1198 | if (ret == 0) { |
| 1199 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1200 | if (destoff + olen > inode->i_size) |
| 1201 | btrfs_i_size_write(inode, destoff + olen); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1202 | BTRFS_I(inode)->flags = BTRFS_I(src)->flags; |
| 1203 | ret = btrfs_update_inode(trans, root, inode); |
| 1204 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1205 | btrfs_end_transaction(trans, root); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1206 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1207 | if (ret) |
| 1208 | vmtruncate(inode, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1209 | out_unlock: |
| 1210 | mutex_unlock(&src->i_mutex); |
| 1211 | mutex_unlock(&inode->i_mutex); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1212 | vfree(buf); |
| 1213 | btrfs_free_path(path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1214 | out_fput: |
| 1215 | fput(src_file); |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 1216 | out_drop_write: |
| 1217 | mnt_drop_write(file->f_path.mnt); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1218 | return ret; |
| 1219 | } |
| 1220 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1221 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1222 | { |
| 1223 | struct btrfs_ioctl_clone_range_args args; |
| 1224 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1225 | if (copy_from_user(&args, argp, sizeof(args))) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1226 | return -EFAULT; |
| 1227 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, |
| 1228 | args.src_length, args.dest_offset); |
| 1229 | } |
| 1230 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1231 | /* |
| 1232 | * there are many ways the trans_start and trans_end ioctls can lead |
| 1233 | * to deadlocks. They should only be used by applications that |
| 1234 | * basically own the machine, and have a very in depth understanding |
| 1235 | * of all the possible deadlocks and enospc problems. |
| 1236 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1237 | static long btrfs_ioctl_trans_start(struct file *file) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1238 | { |
| 1239 | struct inode *inode = fdentry(file)->d_inode; |
| 1240 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1241 | struct btrfs_trans_handle *trans; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1242 | int ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1243 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1244 | ret = -EPERM; |
Christoph Hellwig | df5b552 | 2008-06-11 21:53:58 -0400 | [diff] [blame] | 1245 | if (!capable(CAP_SYS_ADMIN)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1246 | goto out; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1247 | |
| 1248 | ret = -EINPROGRESS; |
| 1249 | if (file->private_data) |
| 1250 | goto out; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1251 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1252 | ret = mnt_want_write(file->f_path.mnt); |
| 1253 | if (ret) |
| 1254 | goto out; |
| 1255 | |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1256 | mutex_lock(&root->fs_info->trans_mutex); |
| 1257 | root->fs_info->open_ioctl_trans++; |
| 1258 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1259 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1260 | ret = -ENOMEM; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1261 | trans = btrfs_start_ioctl_transaction(root, 0); |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1262 | if (!trans) |
| 1263 | goto out_drop; |
| 1264 | |
| 1265 | file->private_data = trans; |
| 1266 | return 0; |
| 1267 | |
| 1268 | out_drop: |
| 1269 | mutex_lock(&root->fs_info->trans_mutex); |
| 1270 | root->fs_info->open_ioctl_trans--; |
| 1271 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1272 | mnt_drop_write(file->f_path.mnt); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1273 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1274 | return ret; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * there are many ways the trans_start and trans_end ioctls can lead |
| 1279 | * to deadlocks. They should only be used by applications that |
| 1280 | * basically own the machine, and have a very in depth understanding |
| 1281 | * of all the possible deadlocks and enospc problems. |
| 1282 | */ |
| 1283 | long btrfs_ioctl_trans_end(struct file *file) |
| 1284 | { |
| 1285 | struct inode *inode = fdentry(file)->d_inode; |
| 1286 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1287 | struct btrfs_trans_handle *trans; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1288 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1289 | trans = file->private_data; |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1290 | if (!trans) |
| 1291 | return -EINVAL; |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 1292 | file->private_data = NULL; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1293 | |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1294 | btrfs_end_transaction(trans, root); |
| 1295 | |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1296 | mutex_lock(&root->fs_info->trans_mutex); |
| 1297 | root->fs_info->open_ioctl_trans--; |
| 1298 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1299 | |
Sage Weil | cfc8ea8 | 2008-12-11 16:30:06 -0500 | [diff] [blame] | 1300 | mnt_drop_write(file->f_path.mnt); |
Sage Weil | 1ab86ae | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 1301 | return 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | long btrfs_ioctl(struct file *file, unsigned int |
| 1305 | cmd, unsigned long arg) |
| 1306 | { |
| 1307 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1308 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1309 | |
| 1310 | switch (cmd) { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1311 | case FS_IOC_GETFLAGS: |
| 1312 | return btrfs_ioctl_getflags(file, argp); |
| 1313 | case FS_IOC_SETFLAGS: |
| 1314 | return btrfs_ioctl_setflags(file, argp); |
| 1315 | case FS_IOC_GETVERSION: |
| 1316 | return btrfs_ioctl_getversion(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1317 | case BTRFS_IOC_SNAP_CREATE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1318 | return btrfs_ioctl_snap_create(file, argp, 0); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1319 | case BTRFS_IOC_SUBVOL_CREATE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1320 | return btrfs_ioctl_snap_create(file, argp, 1); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1321 | case BTRFS_IOC_SNAP_DESTROY: |
| 1322 | return btrfs_ioctl_snap_destroy(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1323 | case BTRFS_IOC_DEFRAG: |
| 1324 | return btrfs_ioctl_defrag(file); |
| 1325 | case BTRFS_IOC_RESIZE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1326 | return btrfs_ioctl_resize(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1327 | case BTRFS_IOC_ADD_DEV: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1328 | return btrfs_ioctl_add_dev(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1329 | case BTRFS_IOC_RM_DEV: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1330 | return btrfs_ioctl_rm_dev(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1331 | case BTRFS_IOC_BALANCE: |
| 1332 | return btrfs_balance(root->fs_info->dev_root); |
| 1333 | case BTRFS_IOC_CLONE: |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1334 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
| 1335 | case BTRFS_IOC_CLONE_RANGE: |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1336 | return btrfs_ioctl_clone_range(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1337 | case BTRFS_IOC_TRANS_START: |
| 1338 | return btrfs_ioctl_trans_start(file); |
| 1339 | case BTRFS_IOC_TRANS_END: |
| 1340 | return btrfs_ioctl_trans_end(file); |
| 1341 | case BTRFS_IOC_SYNC: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1342 | btrfs_sync_fs(file->f_dentry->d_sb, 1); |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | return -ENOTTY; |
| 1347 | } |