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