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