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> |
| 30 | #include <linux/smp_lock.h> |
| 31 | #include <linux/backing-dev.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 32 | #include <linux/mount.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 33 | #include <linux/mpage.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 34 | #include <linux/namei.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 35 | #include <linux/swap.h> |
| 36 | #include <linux/writeback.h> |
| 37 | #include <linux/statfs.h> |
| 38 | #include <linux/compat.h> |
| 39 | #include <linux/bit_spinlock.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 40 | #include <linux/security.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 41 | #include <linux/xattr.h> |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 42 | #include <linux/vmalloc.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; |
| 234 | struct btrfs_root *new_root = root; |
| 235 | struct inode *dir; |
| 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 | unsigned long nr = 1; |
| 242 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 243 | ret = btrfs_check_metadata_free_space(root); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 244 | if (ret) |
| 245 | goto fail_commit; |
| 246 | |
| 247 | trans = btrfs_start_transaction(root, 1); |
| 248 | BUG_ON(!trans); |
| 249 | |
| 250 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 251 | 0, &objectid); |
| 252 | if (ret) |
| 253 | goto fail; |
| 254 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 255 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
| 256 | 0, objectid, NULL, 0, 0, 0); |
Josef Bacik | 8e8a1e3 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 257 | if (IS_ERR(leaf)) { |
| 258 | ret = PTR_ERR(leaf); |
| 259 | goto fail; |
| 260 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 261 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 262 | memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 263 | btrfs_set_header_bytenr(leaf, leaf->start); |
| 264 | btrfs_set_header_generation(leaf, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 265 | btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 266 | btrfs_set_header_owner(leaf, objectid); |
| 267 | |
| 268 | write_extent_buffer(leaf, root->fs_info->fsid, |
| 269 | (unsigned long)btrfs_header_fsid(leaf), |
| 270 | BTRFS_FSID_SIZE); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 271 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 272 | (unsigned long)btrfs_header_chunk_tree_uuid(leaf), |
| 273 | BTRFS_UUID_SIZE); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 274 | btrfs_mark_buffer_dirty(leaf); |
| 275 | |
| 276 | inode_item = &root_item.inode; |
| 277 | memset(inode_item, 0, sizeof(*inode_item)); |
| 278 | inode_item->generation = cpu_to_le64(1); |
| 279 | inode_item->size = cpu_to_le64(3); |
| 280 | inode_item->nlink = cpu_to_le32(1); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 281 | inode_item->nbytes = cpu_to_le64(root->leafsize); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 282 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
| 283 | |
| 284 | btrfs_set_root_bytenr(&root_item, leaf->start); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 285 | btrfs_set_root_generation(&root_item, trans->transid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 286 | btrfs_set_root_level(&root_item, 0); |
| 287 | btrfs_set_root_refs(&root_item, 1); |
| 288 | btrfs_set_root_used(&root_item, 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 289 | btrfs_set_root_last_snapshot(&root_item, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 290 | |
| 291 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
| 292 | root_item.drop_level = 0; |
| 293 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 294 | btrfs_tree_unlock(leaf); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 295 | free_extent_buffer(leaf); |
| 296 | leaf = NULL; |
| 297 | |
| 298 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 299 | |
| 300 | key.objectid = objectid; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 301 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 302 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 303 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 304 | &root_item); |
| 305 | if (ret) |
| 306 | goto fail; |
| 307 | |
| 308 | /* |
| 309 | * insert the directory item |
| 310 | */ |
| 311 | key.offset = (u64)-1; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 312 | dir = dentry->d_parent->d_inode; |
| 313 | ret = btrfs_set_inode_index(dir, &index); |
| 314 | BUG_ON(ret); |
| 315 | |
| 316 | ret = btrfs_insert_dir_item(trans, root, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 317 | name, namelen, dir->i_ino, &key, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 318 | BTRFS_FT_DIR, index); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 319 | if (ret) |
| 320 | goto fail; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 321 | |
Yan Zheng | 52c2617 | 2009-01-05 15:43:43 -0500 | [diff] [blame] | 322 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
| 323 | ret = btrfs_update_inode(trans, root, dir); |
| 324 | BUG_ON(ret); |
| 325 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 326 | /* add the backref first */ |
| 327 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 328 | objectid, BTRFS_ROOT_BACKREF_KEY, |
| 329 | root->root_key.objectid, |
| 330 | dir->i_ino, index, name, namelen); |
| 331 | |
| 332 | BUG_ON(ret); |
| 333 | |
| 334 | /* now add the forward ref */ |
| 335 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 336 | root->root_key.objectid, BTRFS_ROOT_REF_KEY, |
| 337 | objectid, |
| 338 | dir->i_ino, index, name, namelen); |
| 339 | |
| 340 | BUG_ON(ret); |
| 341 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 342 | ret = btrfs_commit_transaction(trans, root); |
| 343 | if (ret) |
| 344 | goto fail_commit; |
| 345 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 346 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 347 | BUG_ON(!new_root); |
| 348 | |
| 349 | trans = btrfs_start_transaction(new_root, 1); |
| 350 | BUG_ON(!trans); |
| 351 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 352 | ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 353 | BTRFS_I(dir)->block_group); |
| 354 | if (ret) |
| 355 | goto fail; |
| 356 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 357 | fail: |
| 358 | nr = trans->blocks_used; |
| 359 | err = btrfs_commit_transaction(trans, new_root); |
| 360 | if (err && !ret) |
| 361 | ret = err; |
| 362 | fail_commit: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 363 | btrfs_btree_balance_dirty(root, nr); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 364 | return ret; |
| 365 | } |
| 366 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 367 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, |
| 368 | char *name, int namelen) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 369 | { |
| 370 | struct btrfs_pending_snapshot *pending_snapshot; |
| 371 | struct btrfs_trans_handle *trans; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 372 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 373 | int err; |
| 374 | unsigned long nr = 0; |
| 375 | |
| 376 | if (!root->ref_cows) |
| 377 | return -EINVAL; |
| 378 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 379 | ret = btrfs_check_metadata_free_space(root); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 380 | if (ret) |
| 381 | goto fail_unlock; |
| 382 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 383 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 384 | if (!pending_snapshot) { |
| 385 | ret = -ENOMEM; |
| 386 | goto fail_unlock; |
| 387 | } |
| 388 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); |
| 389 | if (!pending_snapshot->name) { |
| 390 | ret = -ENOMEM; |
| 391 | kfree(pending_snapshot); |
| 392 | goto fail_unlock; |
| 393 | } |
| 394 | memcpy(pending_snapshot->name, name, namelen); |
| 395 | pending_snapshot->name[namelen] = '\0'; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 396 | pending_snapshot->dentry = dentry; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 397 | trans = btrfs_start_transaction(root, 1); |
| 398 | BUG_ON(!trans); |
| 399 | pending_snapshot->root = root; |
| 400 | list_add(&pending_snapshot->list, |
| 401 | &trans->transaction->pending_snapshots); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 402 | err = btrfs_commit_transaction(trans, root); |
| 403 | |
| 404 | fail_unlock: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 405 | btrfs_btree_balance_dirty(root, nr); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 406 | return ret; |
| 407 | } |
| 408 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 409 | /* copy of may_create in fs/namei.c() */ |
| 410 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) |
| 411 | { |
| 412 | if (child->d_inode) |
| 413 | return -EEXIST; |
| 414 | if (IS_DEADDIR(dir)) |
| 415 | return -ENOENT; |
| 416 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Create a new subvolume below @parent. This is largely modeled after |
| 421 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup |
| 422 | * inside this filesystem so it's quite a bit simpler. |
| 423 | */ |
| 424 | static noinline int btrfs_mksubvol(struct path *parent, char *name, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 425 | int mode, int namelen, |
| 426 | struct btrfs_root *snap_src) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 427 | { |
| 428 | struct dentry *dentry; |
| 429 | int error; |
| 430 | |
| 431 | mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
| 432 | |
| 433 | dentry = lookup_one_len(name, parent->dentry, namelen); |
| 434 | error = PTR_ERR(dentry); |
| 435 | if (IS_ERR(dentry)) |
| 436 | goto out_unlock; |
| 437 | |
| 438 | error = -EEXIST; |
| 439 | if (dentry->d_inode) |
| 440 | goto out_dput; |
| 441 | |
| 442 | if (!IS_POSIXACL(parent->dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 443 | mode &= ~current_umask(); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 444 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 445 | error = mnt_want_write(parent->mnt); |
| 446 | if (error) |
| 447 | goto out_dput; |
| 448 | |
| 449 | error = btrfs_may_create(parent->dentry->d_inode, dentry); |
| 450 | if (error) |
| 451 | goto out_drop_write; |
| 452 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 453 | /* |
| 454 | * Actually perform the low-level subvolume creation after all |
| 455 | * this VFS fuzz. |
| 456 | * |
| 457 | * Eventually we want to pass in an inode under which we create this |
| 458 | * subvolume, but for now all are under the filesystem root. |
| 459 | * |
| 460 | * Also we should pass on the mode eventually to allow creating new |
| 461 | * subvolume with specific mode bits. |
| 462 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 463 | if (snap_src) { |
Chris Mason | ea9e8b1 | 2008-11-17 21:14:24 -0500 | [diff] [blame] | 464 | struct dentry *dir = dentry->d_parent; |
| 465 | struct dentry *test = dir->d_parent; |
| 466 | struct btrfs_path *path = btrfs_alloc_path(); |
| 467 | int ret; |
| 468 | u64 test_oid; |
| 469 | u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid; |
| 470 | |
| 471 | test_oid = snap_src->root_key.objectid; |
| 472 | |
| 473 | ret = btrfs_find_root_ref(snap_src->fs_info->tree_root, |
| 474 | path, parent_oid, test_oid); |
| 475 | if (ret == 0) |
| 476 | goto create; |
| 477 | btrfs_release_path(snap_src->fs_info->tree_root, path); |
| 478 | |
| 479 | /* we need to make sure we aren't creating a directory loop |
| 480 | * by taking a snapshot of something that has our current |
| 481 | * subvol in its directory tree. So, this loops through |
| 482 | * the dentries and checks the forward refs for each subvolume |
| 483 | * to see if is references the subvolume where we are |
| 484 | * placing this new snapshot. |
| 485 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 486 | while (1) { |
Chris Mason | ea9e8b1 | 2008-11-17 21:14:24 -0500 | [diff] [blame] | 487 | if (!test || |
| 488 | dir == snap_src->fs_info->sb->s_root || |
| 489 | test == snap_src->fs_info->sb->s_root || |
| 490 | test->d_inode->i_sb != snap_src->fs_info->sb) { |
| 491 | break; |
| 492 | } |
| 493 | if (S_ISLNK(test->d_inode->i_mode)) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 494 | printk(KERN_INFO "Btrfs symlink in snapshot " |
| 495 | "path, failed\n"); |
Chris Mason | ea9e8b1 | 2008-11-17 21:14:24 -0500 | [diff] [blame] | 496 | error = -EMLINK; |
| 497 | btrfs_free_path(path); |
| 498 | goto out_drop_write; |
| 499 | } |
| 500 | test_oid = |
| 501 | BTRFS_I(test->d_inode)->root->root_key.objectid; |
| 502 | ret = btrfs_find_root_ref(snap_src->fs_info->tree_root, |
| 503 | path, test_oid, parent_oid); |
| 504 | if (ret == 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 505 | printk(KERN_INFO "Btrfs snapshot creation " |
| 506 | "failed, looping\n"); |
Chris Mason | ea9e8b1 | 2008-11-17 21:14:24 -0500 | [diff] [blame] | 507 | error = -EMLINK; |
| 508 | btrfs_free_path(path); |
| 509 | goto out_drop_write; |
| 510 | } |
| 511 | btrfs_release_path(snap_src->fs_info->tree_root, path); |
| 512 | test = test->d_parent; |
| 513 | } |
| 514 | create: |
| 515 | btrfs_free_path(path); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 516 | error = create_snapshot(snap_src, dentry, name, namelen); |
| 517 | } else { |
| 518 | error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root, |
| 519 | dentry, name, namelen); |
| 520 | } |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 521 | if (error) |
| 522 | goto out_drop_write; |
| 523 | |
| 524 | fsnotify_mkdir(parent->dentry->d_inode, dentry); |
| 525 | out_drop_write: |
| 526 | mnt_drop_write(parent->mnt); |
| 527 | out_dput: |
| 528 | dput(dentry); |
| 529 | out_unlock: |
| 530 | mutex_unlock(&parent->dentry->d_inode->i_mutex); |
| 531 | return error; |
| 532 | } |
| 533 | |
| 534 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 535 | static int btrfs_defrag_file(struct file *file) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 536 | { |
| 537 | struct inode *inode = fdentry(file)->d_inode; |
| 538 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 539 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 540 | struct btrfs_ordered_extent *ordered; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 541 | struct page *page; |
| 542 | unsigned long last_index; |
| 543 | unsigned long ra_pages = root->fs_info->bdi.ra_pages; |
| 544 | unsigned long total_read = 0; |
| 545 | u64 page_start; |
| 546 | u64 page_end; |
| 547 | unsigned long i; |
| 548 | int ret; |
| 549 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 550 | ret = btrfs_check_data_free_space(root, inode, inode->i_size); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 551 | if (ret) |
| 552 | return -ENOSPC; |
| 553 | |
| 554 | mutex_lock(&inode->i_mutex); |
| 555 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 556 | for (i = 0; i <= last_index; i++) { |
| 557 | if (total_read % ra_pages == 0) { |
| 558 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, |
| 559 | min(last_index, i + ra_pages - 1)); |
| 560 | } |
| 561 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 562 | again: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 563 | page = grab_cache_page(inode->i_mapping, i); |
| 564 | if (!page) |
| 565 | goto out_unlock; |
| 566 | if (!PageUptodate(page)) { |
| 567 | btrfs_readpage(NULL, page); |
| 568 | lock_page(page); |
| 569 | if (!PageUptodate(page)) { |
| 570 | unlock_page(page); |
| 571 | page_cache_release(page); |
| 572 | goto out_unlock; |
| 573 | } |
| 574 | } |
| 575 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 576 | wait_on_page_writeback(page); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 577 | |
| 578 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 579 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 580 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 581 | |
| 582 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 583 | if (ordered) { |
| 584 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 585 | unlock_page(page); |
| 586 | page_cache_release(page); |
| 587 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 588 | btrfs_put_ordered_extent(ordered); |
| 589 | goto again; |
| 590 | } |
| 591 | set_page_extent_mapped(page); |
| 592 | |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 593 | /* |
| 594 | * this makes sure page_mkwrite is called on the |
| 595 | * page if it is dirtied again later |
| 596 | */ |
| 597 | clear_page_dirty_for_io(page); |
| 598 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 599 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 600 | |
| 601 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 602 | set_page_dirty(page); |
| 603 | unlock_page(page); |
| 604 | page_cache_release(page); |
| 605 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); |
| 606 | } |
| 607 | |
| 608 | out_unlock: |
| 609 | mutex_unlock(&inode->i_mutex); |
| 610 | return 0; |
| 611 | } |
| 612 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 613 | static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg) |
| 614 | { |
| 615 | u64 new_size; |
| 616 | u64 old_size; |
| 617 | u64 devid = 1; |
| 618 | struct btrfs_ioctl_vol_args *vol_args; |
| 619 | struct btrfs_trans_handle *trans; |
| 620 | struct btrfs_device *device = NULL; |
| 621 | char *sizestr; |
| 622 | char *devstr = NULL; |
| 623 | int ret = 0; |
| 624 | int namelen; |
| 625 | int mod = 0; |
| 626 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 627 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 628 | return -EROFS; |
| 629 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 630 | if (!capable(CAP_SYS_ADMIN)) |
| 631 | return -EPERM; |
| 632 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 633 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 634 | if (IS_ERR(vol_args)) |
| 635 | return PTR_ERR(vol_args); |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 636 | |
| 637 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 638 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 639 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 640 | mutex_lock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 641 | sizestr = vol_args->name; |
| 642 | devstr = strchr(sizestr, ':'); |
| 643 | if (devstr) { |
| 644 | char *end; |
| 645 | sizestr = devstr + 1; |
| 646 | *devstr = '\0'; |
| 647 | devstr = vol_args->name; |
| 648 | devid = simple_strtoull(devstr, &end, 10); |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 649 | printk(KERN_INFO "resizing devid %llu\n", |
| 650 | (unsigned long long)devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 651 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 652 | device = btrfs_find_device(root, devid, NULL, NULL); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 653 | if (!device) { |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 654 | printk(KERN_INFO "resizer unable to find device %llu\n", |
| 655 | (unsigned long long)devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 656 | ret = -EINVAL; |
| 657 | goto out_unlock; |
| 658 | } |
| 659 | if (!strcmp(sizestr, "max")) |
| 660 | new_size = device->bdev->bd_inode->i_size; |
| 661 | else { |
| 662 | if (sizestr[0] == '-') { |
| 663 | mod = -1; |
| 664 | sizestr++; |
| 665 | } else if (sizestr[0] == '+') { |
| 666 | mod = 1; |
| 667 | sizestr++; |
| 668 | } |
| 669 | new_size = btrfs_parse_size(sizestr); |
| 670 | if (new_size == 0) { |
| 671 | ret = -EINVAL; |
| 672 | goto out_unlock; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | old_size = device->total_bytes; |
| 677 | |
| 678 | if (mod < 0) { |
| 679 | if (new_size > old_size) { |
| 680 | ret = -EINVAL; |
| 681 | goto out_unlock; |
| 682 | } |
| 683 | new_size = old_size - new_size; |
| 684 | } else if (mod > 0) { |
| 685 | new_size = old_size + new_size; |
| 686 | } |
| 687 | |
| 688 | if (new_size < 256 * 1024 * 1024) { |
| 689 | ret = -EINVAL; |
| 690 | goto out_unlock; |
| 691 | } |
| 692 | if (new_size > device->bdev->bd_inode->i_size) { |
| 693 | ret = -EFBIG; |
| 694 | goto out_unlock; |
| 695 | } |
| 696 | |
| 697 | do_div(new_size, root->sectorsize); |
| 698 | new_size *= root->sectorsize; |
| 699 | |
| 700 | printk(KERN_INFO "new size for %s is %llu\n", |
| 701 | device->name, (unsigned long long)new_size); |
| 702 | |
| 703 | if (new_size > old_size) { |
| 704 | trans = btrfs_start_transaction(root, 1); |
| 705 | ret = btrfs_grow_device(trans, device, new_size); |
| 706 | btrfs_commit_transaction(trans, root); |
| 707 | } else { |
| 708 | ret = btrfs_shrink_device(device, new_size); |
| 709 | } |
| 710 | |
| 711 | out_unlock: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 712 | mutex_unlock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 713 | kfree(vol_args); |
| 714 | return ret; |
| 715 | } |
| 716 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 717 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 718 | void __user *arg, int subvol) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 719 | { |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 720 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 721 | struct btrfs_ioctl_vol_args *vol_args; |
| 722 | struct btrfs_dir_item *di; |
| 723 | struct btrfs_path *path; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 724 | struct file *src_file; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 725 | u64 root_dirid; |
| 726 | int namelen; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 727 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 728 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 729 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 730 | return -EROFS; |
| 731 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 732 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 733 | if (IS_ERR(vol_args)) |
| 734 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 735 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 736 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 737 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 738 | if (strchr(vol_args->name, '/')) { |
| 739 | ret = -EINVAL; |
| 740 | goto out; |
| 741 | } |
| 742 | |
| 743 | path = btrfs_alloc_path(); |
| 744 | if (!path) { |
| 745 | ret = -ENOMEM; |
| 746 | goto out; |
| 747 | } |
| 748 | |
| 749 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 750 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
| 751 | path, root_dirid, |
| 752 | vol_args->name, namelen, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 753 | btrfs_free_path(path); |
| 754 | |
| 755 | if (di && !IS_ERR(di)) { |
| 756 | ret = -EEXIST; |
| 757 | goto out; |
| 758 | } |
| 759 | |
| 760 | if (IS_ERR(di)) { |
| 761 | ret = PTR_ERR(di); |
| 762 | goto out; |
| 763 | } |
| 764 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 765 | if (subvol) { |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 766 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, |
| 767 | file->f_path.dentry->d_inode->i_mode, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 768 | namelen, NULL); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 769 | } else { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 770 | struct inode *src_inode; |
| 771 | src_file = fget(vol_args->fd); |
| 772 | if (!src_file) { |
| 773 | ret = -EINVAL; |
| 774 | goto out; |
| 775 | } |
| 776 | |
| 777 | src_inode = src_file->f_path.dentry->d_inode; |
| 778 | 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] | 779 | printk(KERN_INFO "btrfs: Snapshot src from " |
| 780 | "another FS\n"); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 781 | ret = -EINVAL; |
| 782 | fput(src_file); |
| 783 | goto out; |
| 784 | } |
| 785 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, |
| 786 | file->f_path.dentry->d_inode->i_mode, |
| 787 | namelen, BTRFS_I(src_inode)->root); |
| 788 | fput(src_file); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 789 | } |
| 790 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 791 | out: |
| 792 | kfree(vol_args); |
| 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | static int btrfs_ioctl_defrag(struct file *file) |
| 797 | { |
| 798 | struct inode *inode = fdentry(file)->d_inode; |
| 799 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 800 | int ret; |
| 801 | |
| 802 | ret = mnt_want_write(file->f_path.mnt); |
| 803 | if (ret) |
| 804 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 805 | |
| 806 | switch (inode->i_mode & S_IFMT) { |
| 807 | case S_IFDIR: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 808 | if (!capable(CAP_SYS_ADMIN)) { |
| 809 | ret = -EPERM; |
| 810 | goto out; |
| 811 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 812 | btrfs_defrag_root(root, 0); |
| 813 | btrfs_defrag_root(root->fs_info->extent_root, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 814 | break; |
| 815 | case S_IFREG: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 816 | if (!(file->f_mode & FMODE_WRITE)) { |
| 817 | ret = -EINVAL; |
| 818 | goto out; |
| 819 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 820 | btrfs_defrag_file(file); |
| 821 | break; |
| 822 | } |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 823 | out: |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 824 | mnt_drop_write(file->f_path.mnt); |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 825 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 826 | } |
| 827 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 828 | 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] | 829 | { |
| 830 | struct btrfs_ioctl_vol_args *vol_args; |
| 831 | int ret; |
| 832 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 833 | if (!capable(CAP_SYS_ADMIN)) |
| 834 | return -EPERM; |
| 835 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 836 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 837 | if (IS_ERR(vol_args)) |
| 838 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 839 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 840 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 841 | ret = btrfs_init_new_device(root, vol_args->name); |
| 842 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 843 | kfree(vol_args); |
| 844 | return ret; |
| 845 | } |
| 846 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 847 | 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] | 848 | { |
| 849 | struct btrfs_ioctl_vol_args *vol_args; |
| 850 | int ret; |
| 851 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 852 | if (!capable(CAP_SYS_ADMIN)) |
| 853 | return -EPERM; |
| 854 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 855 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 856 | return -EROFS; |
| 857 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 858 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 859 | if (IS_ERR(vol_args)) |
| 860 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 861 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 862 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 863 | ret = btrfs_rm_device(root, vol_args->name); |
| 864 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 865 | kfree(vol_args); |
| 866 | return ret; |
| 867 | } |
| 868 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 869 | static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, |
| 870 | u64 off, u64 olen, u64 destoff) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 871 | { |
| 872 | struct inode *inode = fdentry(file)->d_inode; |
| 873 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 874 | struct file *src_file; |
| 875 | struct inode *src; |
| 876 | struct btrfs_trans_handle *trans; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 877 | struct btrfs_path *path; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 878 | struct extent_buffer *leaf; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 879 | char *buf; |
| 880 | struct btrfs_key key; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 881 | u32 nritems; |
| 882 | int slot; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 883 | int ret; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 884 | u64 len = olen; |
| 885 | u64 bs = root->fs_info->sb->s_blocksize; |
| 886 | u64 hint_byte; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 887 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 888 | /* |
| 889 | * TODO: |
| 890 | * - split compressed inline extents. annoying: we need to |
| 891 | * decompress into destination's address_space (the file offset |
| 892 | * may change, so source mapping won't do), then recompress (or |
| 893 | * otherwise reinsert) a subrange. |
| 894 | * - allow ranges within the same file to be cloned (provided |
| 895 | * they don't overlap)? |
| 896 | */ |
| 897 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 898 | /* the destination must be opened for writing */ |
| 899 | if (!(file->f_mode & FMODE_WRITE)) |
| 900 | return -EINVAL; |
| 901 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 902 | ret = mnt_want_write(file->f_path.mnt); |
| 903 | if (ret) |
| 904 | return ret; |
| 905 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 906 | src_file = fget(srcfd); |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 907 | if (!src_file) { |
| 908 | ret = -EBADF; |
| 909 | goto out_drop_write; |
| 910 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 911 | src = src_file->f_dentry->d_inode; |
| 912 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 913 | ret = -EINVAL; |
| 914 | if (src == inode) |
| 915 | goto out_fput; |
| 916 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 917 | ret = -EISDIR; |
| 918 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 919 | goto out_fput; |
| 920 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 921 | ret = -EXDEV; |
| 922 | if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) |
| 923 | goto out_fput; |
| 924 | |
| 925 | ret = -ENOMEM; |
| 926 | buf = vmalloc(btrfs_level_size(root, 0)); |
| 927 | if (!buf) |
| 928 | goto out_fput; |
| 929 | |
| 930 | path = btrfs_alloc_path(); |
| 931 | if (!path) { |
| 932 | vfree(buf); |
| 933 | goto out_fput; |
| 934 | } |
| 935 | path->reada = 2; |
| 936 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 937 | if (inode < src) { |
| 938 | mutex_lock(&inode->i_mutex); |
| 939 | mutex_lock(&src->i_mutex); |
| 940 | } else { |
| 941 | mutex_lock(&src->i_mutex); |
| 942 | mutex_lock(&inode->i_mutex); |
| 943 | } |
| 944 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 945 | /* determine range to clone */ |
| 946 | ret = -EINVAL; |
| 947 | if (off >= src->i_size || off + len > src->i_size) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 948 | goto out_unlock; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 949 | if (len == 0) |
| 950 | olen = len = src->i_size - off; |
| 951 | /* if we extend to eof, continue to block boundary */ |
| 952 | if (off + len == src->i_size) |
| 953 | len = ((src->i_size + bs-1) & ~(bs-1)) |
| 954 | - off; |
| 955 | |
| 956 | /* verify the end result is block aligned */ |
| 957 | if ((off & (bs-1)) || |
| 958 | ((off + len) & (bs-1))) |
| 959 | goto out_unlock; |
| 960 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 961 | /* do any pending delalloc/csum calc on src, one way or |
| 962 | another, and lock file content */ |
| 963 | while (1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 964 | struct btrfs_ordered_extent *ordered; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 965 | lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
| 966 | ordered = btrfs_lookup_first_ordered_extent(inode, off+len); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 967 | if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 968 | break; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 969 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 970 | if (ordered) |
| 971 | btrfs_put_ordered_extent(ordered); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 972 | btrfs_wait_ordered_range(src, off, off+len); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 973 | } |
| 974 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 975 | trans = btrfs_start_transaction(root, 1); |
| 976 | BUG_ON(!trans); |
| 977 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 978 | /* punch hole in destination first */ |
Chris Mason | e980b50 | 2009-04-24 14:39:24 -0400 | [diff] [blame] | 979 | btrfs_drop_extents(trans, root, inode, off, off + len, |
| 980 | off + len, 0, &hint_byte); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 981 | |
| 982 | /* clone data */ |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 983 | key.objectid = src->i_ino; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 984 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 985 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 986 | |
| 987 | while (1) { |
| 988 | /* |
| 989 | * note the key will change type as we walk through the |
| 990 | * tree. |
| 991 | */ |
| 992 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); |
| 993 | if (ret < 0) |
| 994 | goto out; |
| 995 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 996 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 997 | if (path->slots[0] >= nritems) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 998 | ret = btrfs_next_leaf(root, path); |
| 999 | if (ret < 0) |
| 1000 | goto out; |
| 1001 | if (ret > 0) |
| 1002 | break; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1003 | nritems = btrfs_header_nritems(path->nodes[0]); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1004 | } |
| 1005 | leaf = path->nodes[0]; |
| 1006 | slot = path->slots[0]; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1007 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1008 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 1009 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1010 | key.objectid != src->i_ino) |
| 1011 | break; |
| 1012 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1013 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
| 1014 | struct btrfs_file_extent_item *extent; |
| 1015 | int type; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1016 | u32 size; |
| 1017 | struct btrfs_key new_key; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1018 | u64 disko = 0, diskl = 0; |
| 1019 | u64 datao = 0, datal = 0; |
| 1020 | u8 comp; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1021 | |
| 1022 | size = btrfs_item_size_nr(leaf, slot); |
| 1023 | read_extent_buffer(leaf, buf, |
| 1024 | btrfs_item_ptr_offset(leaf, slot), |
| 1025 | size); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1026 | |
| 1027 | extent = btrfs_item_ptr(leaf, slot, |
| 1028 | struct btrfs_file_extent_item); |
| 1029 | comp = btrfs_file_extent_compression(leaf, extent); |
| 1030 | type = btrfs_file_extent_type(leaf, extent); |
Chris Mason | c8a894d | 2009-06-27 21:07:03 -0400 | [diff] [blame] | 1031 | if (type == BTRFS_FILE_EXTENT_REG || |
| 1032 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1033 | disko = btrfs_file_extent_disk_bytenr(leaf, |
| 1034 | extent); |
| 1035 | diskl = btrfs_file_extent_disk_num_bytes(leaf, |
| 1036 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1037 | datao = btrfs_file_extent_offset(leaf, extent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1038 | datal = btrfs_file_extent_num_bytes(leaf, |
| 1039 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1040 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 1041 | /* take upper bound, may be compressed */ |
| 1042 | datal = btrfs_file_extent_ram_bytes(leaf, |
| 1043 | extent); |
| 1044 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1045 | btrfs_release_path(root, path); |
| 1046 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1047 | if (key.offset + datal < off || |
| 1048 | key.offset >= off+len) |
| 1049 | goto next; |
| 1050 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1051 | memcpy(&new_key, &key, sizeof(new_key)); |
| 1052 | new_key.objectid = inode->i_ino; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1053 | new_key.offset = key.offset + destoff - off; |
| 1054 | |
Chris Mason | c8a894d | 2009-06-27 21:07:03 -0400 | [diff] [blame] | 1055 | if (type == BTRFS_FILE_EXTENT_REG || |
| 1056 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1057 | ret = btrfs_insert_empty_item(trans, root, path, |
| 1058 | &new_key, size); |
| 1059 | if (ret) |
| 1060 | goto out; |
| 1061 | |
| 1062 | leaf = path->nodes[0]; |
| 1063 | slot = path->slots[0]; |
| 1064 | write_extent_buffer(leaf, buf, |
| 1065 | btrfs_item_ptr_offset(leaf, slot), |
| 1066 | size); |
| 1067 | |
| 1068 | extent = btrfs_item_ptr(leaf, slot, |
| 1069 | struct btrfs_file_extent_item); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1070 | |
| 1071 | if (off > key.offset) { |
| 1072 | datao += off - key.offset; |
| 1073 | datal -= off - key.offset; |
| 1074 | } |
| 1075 | if (key.offset + datao + datal + key.offset > |
| 1076 | off + len) |
| 1077 | datal = off + len - key.offset - datao; |
| 1078 | /* disko == 0 means it's a hole */ |
| 1079 | if (!disko) |
| 1080 | datao = 0; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1081 | |
| 1082 | btrfs_set_file_extent_offset(leaf, extent, |
| 1083 | datao); |
| 1084 | btrfs_set_file_extent_num_bytes(leaf, extent, |
| 1085 | datal); |
| 1086 | if (disko) { |
| 1087 | inode_add_bytes(inode, datal); |
| 1088 | ret = btrfs_inc_extent_ref(trans, root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1089 | disko, diskl, 0, |
| 1090 | root->root_key.objectid, |
| 1091 | inode->i_ino, |
| 1092 | new_key.offset - datao); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1093 | BUG_ON(ret); |
| 1094 | } |
| 1095 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 1096 | u64 skip = 0; |
| 1097 | u64 trim = 0; |
| 1098 | if (off > key.offset) { |
| 1099 | skip = off - key.offset; |
| 1100 | new_key.offset += skip; |
| 1101 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1102 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1103 | if (key.offset + datal > off+len) |
| 1104 | trim = key.offset + datal - (off+len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1105 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1106 | if (comp && (skip || trim)) { |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1107 | ret = -EINVAL; |
| 1108 | goto out; |
| 1109 | } |
| 1110 | size -= skip + trim; |
| 1111 | datal -= skip + trim; |
| 1112 | ret = btrfs_insert_empty_item(trans, root, path, |
| 1113 | &new_key, size); |
| 1114 | if (ret) |
| 1115 | goto out; |
| 1116 | |
| 1117 | if (skip) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1118 | u32 start = |
| 1119 | btrfs_file_extent_calc_inline_size(0); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1120 | memmove(buf+start, buf+start+skip, |
| 1121 | datal); |
| 1122 | } |
| 1123 | |
| 1124 | leaf = path->nodes[0]; |
| 1125 | slot = path->slots[0]; |
| 1126 | write_extent_buffer(leaf, buf, |
| 1127 | btrfs_item_ptr_offset(leaf, slot), |
| 1128 | size); |
| 1129 | inode_add_bytes(inode, datal); |
| 1130 | } |
| 1131 | |
| 1132 | btrfs_mark_buffer_dirty(leaf); |
| 1133 | } |
| 1134 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1135 | next: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1136 | btrfs_release_path(root, path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1137 | key.offset++; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1138 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1139 | ret = 0; |
| 1140 | out: |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1141 | btrfs_release_path(root, path); |
| 1142 | if (ret == 0) { |
| 1143 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1144 | if (destoff + olen > inode->i_size) |
| 1145 | btrfs_i_size_write(inode, destoff + olen); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1146 | BTRFS_I(inode)->flags = BTRFS_I(src)->flags; |
| 1147 | ret = btrfs_update_inode(trans, root, inode); |
| 1148 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1149 | btrfs_end_transaction(trans, root); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1150 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1151 | if (ret) |
| 1152 | vmtruncate(inode, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1153 | out_unlock: |
| 1154 | mutex_unlock(&src->i_mutex); |
| 1155 | mutex_unlock(&inode->i_mutex); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1156 | vfree(buf); |
| 1157 | btrfs_free_path(path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1158 | out_fput: |
| 1159 | fput(src_file); |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 1160 | out_drop_write: |
| 1161 | mnt_drop_write(file->f_path.mnt); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1162 | return ret; |
| 1163 | } |
| 1164 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1165 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1166 | { |
| 1167 | struct btrfs_ioctl_clone_range_args args; |
| 1168 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1169 | if (copy_from_user(&args, argp, sizeof(args))) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1170 | return -EFAULT; |
| 1171 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, |
| 1172 | args.src_length, args.dest_offset); |
| 1173 | } |
| 1174 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1175 | /* |
| 1176 | * there are many ways the trans_start and trans_end ioctls can lead |
| 1177 | * to deadlocks. They should only be used by applications that |
| 1178 | * basically own the machine, and have a very in depth understanding |
| 1179 | * of all the possible deadlocks and enospc problems. |
| 1180 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1181 | static long btrfs_ioctl_trans_start(struct file *file) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1182 | { |
| 1183 | struct inode *inode = fdentry(file)->d_inode; |
| 1184 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1185 | struct btrfs_trans_handle *trans; |
| 1186 | int ret = 0; |
| 1187 | |
Christoph Hellwig | df5b552 | 2008-06-11 21:53:58 -0400 | [diff] [blame] | 1188 | if (!capable(CAP_SYS_ADMIN)) |
| 1189 | return -EPERM; |
| 1190 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1191 | if (file->private_data) { |
| 1192 | ret = -EINPROGRESS; |
| 1193 | goto out; |
| 1194 | } |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1195 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1196 | ret = mnt_want_write(file->f_path.mnt); |
| 1197 | if (ret) |
| 1198 | goto out; |
| 1199 | |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1200 | mutex_lock(&root->fs_info->trans_mutex); |
| 1201 | root->fs_info->open_ioctl_trans++; |
| 1202 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1203 | |
| 1204 | trans = btrfs_start_ioctl_transaction(root, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1205 | if (trans) |
| 1206 | file->private_data = trans; |
| 1207 | else |
| 1208 | ret = -ENOMEM; |
| 1209 | /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/ |
| 1210 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1211 | return ret; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * there are many ways the trans_start and trans_end ioctls can lead |
| 1216 | * to deadlocks. They should only be used by applications that |
| 1217 | * basically own the machine, and have a very in depth understanding |
| 1218 | * of all the possible deadlocks and enospc problems. |
| 1219 | */ |
| 1220 | long btrfs_ioctl_trans_end(struct file *file) |
| 1221 | { |
| 1222 | struct inode *inode = fdentry(file)->d_inode; |
| 1223 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1224 | struct btrfs_trans_handle *trans; |
| 1225 | int ret = 0; |
| 1226 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1227 | trans = file->private_data; |
| 1228 | if (!trans) { |
| 1229 | ret = -EINVAL; |
| 1230 | goto out; |
| 1231 | } |
| 1232 | btrfs_end_transaction(trans, root); |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 1233 | file->private_data = NULL; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1234 | |
| 1235 | mutex_lock(&root->fs_info->trans_mutex); |
| 1236 | root->fs_info->open_ioctl_trans--; |
| 1237 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1238 | |
Sage Weil | cfc8ea8 | 2008-12-11 16:30:06 -0500 | [diff] [blame] | 1239 | mnt_drop_write(file->f_path.mnt); |
| 1240 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1241 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1242 | return ret; |
| 1243 | } |
| 1244 | |
| 1245 | long btrfs_ioctl(struct file *file, unsigned int |
| 1246 | cmd, unsigned long arg) |
| 1247 | { |
| 1248 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1249 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1250 | |
| 1251 | switch (cmd) { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 1252 | case FS_IOC_GETFLAGS: |
| 1253 | return btrfs_ioctl_getflags(file, argp); |
| 1254 | case FS_IOC_SETFLAGS: |
| 1255 | return btrfs_ioctl_setflags(file, argp); |
| 1256 | case FS_IOC_GETVERSION: |
| 1257 | return btrfs_ioctl_getversion(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1258 | case BTRFS_IOC_SNAP_CREATE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1259 | return btrfs_ioctl_snap_create(file, argp, 0); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1260 | case BTRFS_IOC_SUBVOL_CREATE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1261 | return btrfs_ioctl_snap_create(file, argp, 1); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1262 | case BTRFS_IOC_DEFRAG: |
| 1263 | return btrfs_ioctl_defrag(file); |
| 1264 | case BTRFS_IOC_RESIZE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1265 | return btrfs_ioctl_resize(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1266 | case BTRFS_IOC_ADD_DEV: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1267 | return btrfs_ioctl_add_dev(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1268 | case BTRFS_IOC_RM_DEV: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1269 | return btrfs_ioctl_rm_dev(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1270 | case BTRFS_IOC_BALANCE: |
| 1271 | return btrfs_balance(root->fs_info->dev_root); |
| 1272 | case BTRFS_IOC_CLONE: |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1273 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
| 1274 | case BTRFS_IOC_CLONE_RANGE: |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1275 | return btrfs_ioctl_clone_range(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1276 | case BTRFS_IOC_TRANS_START: |
| 1277 | return btrfs_ioctl_trans_start(file); |
| 1278 | case BTRFS_IOC_TRANS_END: |
| 1279 | return btrfs_ioctl_trans_end(file); |
| 1280 | case BTRFS_IOC_SYNC: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1281 | btrfs_sync_fs(file->f_dentry->d_sb, 1); |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | return -ENOTTY; |
| 1286 | } |