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