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