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