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