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