| 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> |
| 24 | #include <linux/fsnotify.h> |
| 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> |
| 30 | #include <linux/backing-dev.h> |
| 31 | #include <linux/mount.h> |
| 32 | #include <linux/mpage.h> |
| 33 | #include <linux/namei.h> |
| 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> |
| 39 | #include <linux/security.h> |
| 40 | #include <linux/xattr.h> |
| 41 | #include <linux/vmalloc.h> |
| 42 | #include <linux/slab.h> |
| 43 | #include <linux/blkdev.h> |
| 44 | #include <linux/uuid.h> |
| 45 | #include <linux/btrfs.h> |
| 46 | #include "compat.h" |
| 47 | #include "ctree.h" |
| 48 | #include "disk-io.h" |
| 49 | #include "transaction.h" |
| 50 | #include "btrfs_inode.h" |
| 51 | #include "print-tree.h" |
| 52 | #include "volumes.h" |
| 53 | #include "locking.h" |
| 54 | #include "inode-map.h" |
| 55 | #include "backref.h" |
| 56 | #include "rcu-string.h" |
| 57 | #include "send.h" |
| 58 | #include "dev-replace.h" |
| 59 | |
| 60 | /* Mask out flags that are inappropriate for the given type of inode. */ |
| 61 | static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) |
| 62 | { |
| 63 | if (S_ISDIR(mode)) |
| 64 | return flags; |
| 65 | else if (S_ISREG(mode)) |
| 66 | return flags & ~FS_DIRSYNC_FL; |
| 67 | else |
| 68 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. |
| 73 | */ |
| 74 | static unsigned int btrfs_flags_to_ioctl(unsigned int flags) |
| 75 | { |
| 76 | unsigned int iflags = 0; |
| 77 | |
| 78 | if (flags & BTRFS_INODE_SYNC) |
| 79 | iflags |= FS_SYNC_FL; |
| 80 | if (flags & BTRFS_INODE_IMMUTABLE) |
| 81 | iflags |= FS_IMMUTABLE_FL; |
| 82 | if (flags & BTRFS_INODE_APPEND) |
| 83 | iflags |= FS_APPEND_FL; |
| 84 | if (flags & BTRFS_INODE_NODUMP) |
| 85 | iflags |= FS_NODUMP_FL; |
| 86 | if (flags & BTRFS_INODE_NOATIME) |
| 87 | iflags |= FS_NOATIME_FL; |
| 88 | if (flags & BTRFS_INODE_DIRSYNC) |
| 89 | iflags |= FS_DIRSYNC_FL; |
| 90 | if (flags & BTRFS_INODE_NODATACOW) |
| 91 | iflags |= FS_NOCOW_FL; |
| 92 | |
| 93 | if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) |
| 94 | iflags |= FS_COMPR_FL; |
| 95 | else if (flags & BTRFS_INODE_NOCOMPRESS) |
| 96 | iflags |= FS_NOCOMP_FL; |
| 97 | |
| 98 | return iflags; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Update inode->i_flags based on the btrfs internal flags. |
| 103 | */ |
| 104 | void btrfs_update_iflags(struct inode *inode) |
| 105 | { |
| 106 | struct btrfs_inode *ip = BTRFS_I(inode); |
| 107 | |
| 108 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); |
| 109 | |
| 110 | if (ip->flags & BTRFS_INODE_SYNC) |
| 111 | inode->i_flags |= S_SYNC; |
| 112 | if (ip->flags & BTRFS_INODE_IMMUTABLE) |
| 113 | inode->i_flags |= S_IMMUTABLE; |
| 114 | if (ip->flags & BTRFS_INODE_APPEND) |
| 115 | inode->i_flags |= S_APPEND; |
| 116 | if (ip->flags & BTRFS_INODE_NOATIME) |
| 117 | inode->i_flags |= S_NOATIME; |
| 118 | if (ip->flags & BTRFS_INODE_DIRSYNC) |
| 119 | inode->i_flags |= S_DIRSYNC; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Inherit flags from the parent inode. |
| 124 | * |
| 125 | * Currently only the compression flags and the cow flags are inherited. |
| 126 | */ |
| 127 | void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
| 128 | { |
| 129 | unsigned int flags; |
| 130 | |
| 131 | if (!dir) |
| 132 | return; |
| 133 | |
| 134 | flags = BTRFS_I(dir)->flags; |
| 135 | |
| 136 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
| 137 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
| 138 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
| 139 | } else if (flags & BTRFS_INODE_COMPRESS) { |
| 140 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 141 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
| 142 | } |
| 143 | |
| 144 | if (flags & BTRFS_INODE_NODATACOW) { |
| 145 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
| 146 | if (S_ISREG(inode->i_mode)) |
| 147 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
| 148 | } |
| 149 | |
| 150 | btrfs_update_iflags(inode); |
| 151 | } |
| 152 | |
| 153 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) |
| 154 | { |
| 155 | struct btrfs_inode *ip = BTRFS_I(file_inode(file)); |
| 156 | unsigned int flags = btrfs_flags_to_ioctl(ip->flags); |
| 157 | |
| 158 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 159 | return -EFAULT; |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static int check_flags(unsigned int flags) |
| 164 | { |
| 165 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
| 166 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
| 167 | FS_SYNC_FL | FS_DIRSYNC_FL | \ |
| 168 | FS_NOCOMP_FL | FS_COMPR_FL | |
| 169 | FS_NOCOW_FL)) |
| 170 | return -EOPNOTSUPP; |
| 171 | |
| 172 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) |
| 173 | return -EINVAL; |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) |
| 179 | { |
| 180 | struct inode *inode = file_inode(file); |
| 181 | struct btrfs_inode *ip = BTRFS_I(inode); |
| 182 | struct btrfs_root *root = ip->root; |
| 183 | struct btrfs_trans_handle *trans; |
| 184 | unsigned int flags, oldflags; |
| 185 | int ret; |
| 186 | u64 ip_oldflags; |
| 187 | unsigned int i_oldflags; |
| 188 | umode_t mode; |
| 189 | |
| 190 | if (btrfs_root_readonly(root)) |
| 191 | return -EROFS; |
| 192 | |
| 193 | if (copy_from_user(&flags, arg, sizeof(flags))) |
| 194 | return -EFAULT; |
| 195 | |
| 196 | ret = check_flags(flags); |
| 197 | if (ret) |
| 198 | return ret; |
| 199 | |
| 200 | if (!inode_owner_or_capable(inode)) |
| 201 | return -EACCES; |
| 202 | |
| 203 | ret = mnt_want_write_file(file); |
| 204 | if (ret) |
| 205 | return ret; |
| 206 | |
| 207 | mutex_lock(&inode->i_mutex); |
| 208 | |
| 209 | ip_oldflags = ip->flags; |
| 210 | i_oldflags = inode->i_flags; |
| 211 | mode = inode->i_mode; |
| 212 | |
| 213 | flags = btrfs_mask_flags(inode->i_mode, flags); |
| 214 | oldflags = btrfs_flags_to_ioctl(ip->flags); |
| 215 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
| 216 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 217 | ret = -EPERM; |
| 218 | goto out_unlock; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (flags & FS_SYNC_FL) |
| 223 | ip->flags |= BTRFS_INODE_SYNC; |
| 224 | else |
| 225 | ip->flags &= ~BTRFS_INODE_SYNC; |
| 226 | if (flags & FS_IMMUTABLE_FL) |
| 227 | ip->flags |= BTRFS_INODE_IMMUTABLE; |
| 228 | else |
| 229 | ip->flags &= ~BTRFS_INODE_IMMUTABLE; |
| 230 | if (flags & FS_APPEND_FL) |
| 231 | ip->flags |= BTRFS_INODE_APPEND; |
| 232 | else |
| 233 | ip->flags &= ~BTRFS_INODE_APPEND; |
| 234 | if (flags & FS_NODUMP_FL) |
| 235 | ip->flags |= BTRFS_INODE_NODUMP; |
| 236 | else |
| 237 | ip->flags &= ~BTRFS_INODE_NODUMP; |
| 238 | if (flags & FS_NOATIME_FL) |
| 239 | ip->flags |= BTRFS_INODE_NOATIME; |
| 240 | else |
| 241 | ip->flags &= ~BTRFS_INODE_NOATIME; |
| 242 | if (flags & FS_DIRSYNC_FL) |
| 243 | ip->flags |= BTRFS_INODE_DIRSYNC; |
| 244 | else |
| 245 | ip->flags &= ~BTRFS_INODE_DIRSYNC; |
| 246 | if (flags & FS_NOCOW_FL) { |
| 247 | if (S_ISREG(mode)) { |
| 248 | /* |
| 249 | * It's safe to turn csums off here, no extents exist. |
| 250 | * Otherwise we want the flag to reflect the real COW |
| 251 | * status of the file and will not set it. |
| 252 | */ |
| 253 | if (inode->i_size == 0) |
| 254 | ip->flags |= BTRFS_INODE_NODATACOW |
| 255 | | BTRFS_INODE_NODATASUM; |
| 256 | } else { |
| 257 | ip->flags |= BTRFS_INODE_NODATACOW; |
| 258 | } |
| 259 | } else { |
| 260 | /* |
| 261 | * Revert back under same assuptions as above |
| 262 | */ |
| 263 | if (S_ISREG(mode)) { |
| 264 | if (inode->i_size == 0) |
| 265 | ip->flags &= ~(BTRFS_INODE_NODATACOW |
| 266 | | BTRFS_INODE_NODATASUM); |
| 267 | } else { |
| 268 | ip->flags &= ~BTRFS_INODE_NODATACOW; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS |
| 274 | * flag may be changed automatically if compression code won't make |
| 275 | * things smaller. |
| 276 | */ |
| 277 | if (flags & FS_NOCOMP_FL) { |
| 278 | ip->flags &= ~BTRFS_INODE_COMPRESS; |
| 279 | ip->flags |= BTRFS_INODE_NOCOMPRESS; |
| 280 | } else if (flags & FS_COMPR_FL) { |
| 281 | ip->flags |= BTRFS_INODE_COMPRESS; |
| 282 | ip->flags &= ~BTRFS_INODE_NOCOMPRESS; |
| 283 | } else { |
| 284 | ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); |
| 285 | } |
| 286 | |
| 287 | trans = btrfs_start_transaction(root, 1); |
| 288 | if (IS_ERR(trans)) { |
| 289 | ret = PTR_ERR(trans); |
| 290 | goto out_drop; |
| 291 | } |
| 292 | |
| 293 | btrfs_update_iflags(inode); |
| 294 | inode_inc_iversion(inode); |
| 295 | inode->i_ctime = CURRENT_TIME; |
| 296 | ret = btrfs_update_inode(trans, root, inode); |
| 297 | |
| 298 | btrfs_end_transaction(trans, root); |
| 299 | out_drop: |
| 300 | if (ret) { |
| 301 | ip->flags = ip_oldflags; |
| 302 | inode->i_flags = i_oldflags; |
| 303 | } |
| 304 | |
| 305 | out_unlock: |
| 306 | mutex_unlock(&inode->i_mutex); |
| 307 | mnt_drop_write_file(file); |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) |
| 312 | { |
| 313 | struct inode *inode = file_inode(file); |
| 314 | |
| 315 | return put_user(inode->i_generation, arg); |
| 316 | } |
| 317 | |
| 318 | static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) |
| 319 | { |
| 320 | struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb); |
| 321 | struct btrfs_device *device; |
| 322 | struct request_queue *q; |
| 323 | struct fstrim_range range; |
| 324 | u64 minlen = ULLONG_MAX; |
| 325 | u64 num_devices = 0; |
| 326 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
| 327 | int ret; |
| 328 | |
| 329 | if (!capable(CAP_SYS_ADMIN)) |
| 330 | return -EPERM; |
| 331 | |
| 332 | rcu_read_lock(); |
| 333 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, |
| 334 | dev_list) { |
| 335 | if (!device->bdev) |
| 336 | continue; |
| 337 | q = bdev_get_queue(device->bdev); |
| 338 | if (blk_queue_discard(q)) { |
| 339 | num_devices++; |
| 340 | minlen = min((u64)q->limits.discard_granularity, |
| 341 | minlen); |
| 342 | } |
| 343 | } |
| 344 | rcu_read_unlock(); |
| 345 | |
| 346 | if (!num_devices) |
| 347 | return -EOPNOTSUPP; |
| 348 | if (copy_from_user(&range, arg, sizeof(range))) |
| 349 | return -EFAULT; |
| 350 | if (range.start > total_bytes || |
| 351 | range.len < fs_info->sb->s_blocksize) |
| 352 | return -EINVAL; |
| 353 | |
| 354 | range.len = min(range.len, total_bytes - range.start); |
| 355 | range.minlen = max(range.minlen, minlen); |
| 356 | ret = btrfs_trim_fs(fs_info->tree_root, &range); |
| 357 | if (ret < 0) |
| 358 | return ret; |
| 359 | |
| 360 | if (copy_to_user(arg, &range, sizeof(range))) |
| 361 | return -EFAULT; |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static noinline int create_subvol(struct inode *dir, |
| 367 | struct dentry *dentry, |
| 368 | char *name, int namelen, |
| 369 | u64 *async_transid, |
| 370 | struct btrfs_qgroup_inherit *inherit) |
| 371 | { |
| 372 | struct btrfs_trans_handle *trans; |
| 373 | struct btrfs_key key; |
| 374 | struct btrfs_root_item root_item; |
| 375 | struct btrfs_inode_item *inode_item; |
| 376 | struct extent_buffer *leaf; |
| 377 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 378 | struct btrfs_root *new_root; |
| 379 | struct btrfs_block_rsv block_rsv; |
| 380 | struct timespec cur_time = CURRENT_TIME; |
| 381 | int ret; |
| 382 | int err; |
| 383 | u64 objectid; |
| 384 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
| 385 | u64 index = 0; |
| 386 | u64 qgroup_reserved; |
| 387 | uuid_le new_uuid; |
| 388 | |
| 389 | ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); |
| 390 | if (ret) |
| 391 | return ret; |
| 392 | |
| 393 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 394 | /* |
| 395 | * The same as the snapshot creation, please see the comment |
| 396 | * of create_snapshot(). |
| 397 | */ |
| 398 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, |
| 399 | 7, &qgroup_reserved, false); |
| 400 | if (ret) |
| 401 | return ret; |
| 402 | |
| 403 | trans = btrfs_start_transaction(root, 0); |
| 404 | if (IS_ERR(trans)) { |
| 405 | ret = PTR_ERR(trans); |
| 406 | goto out; |
| 407 | } |
| 408 | trans->block_rsv = &block_rsv; |
| 409 | trans->bytes_reserved = block_rsv.size; |
| 410 | |
| 411 | ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit); |
| 412 | if (ret) |
| 413 | goto fail; |
| 414 | |
| 415 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
| 416 | 0, objectid, NULL, 0, 0, 0); |
| 417 | if (IS_ERR(leaf)) { |
| 418 | ret = PTR_ERR(leaf); |
| 419 | goto fail; |
| 420 | } |
| 421 | |
| 422 | memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); |
| 423 | btrfs_set_header_bytenr(leaf, leaf->start); |
| 424 | btrfs_set_header_generation(leaf, trans->transid); |
| 425 | btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); |
| 426 | btrfs_set_header_owner(leaf, objectid); |
| 427 | |
| 428 | write_extent_buffer(leaf, root->fs_info->fsid, |
| 429 | (unsigned long)btrfs_header_fsid(leaf), |
| 430 | BTRFS_FSID_SIZE); |
| 431 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
| 432 | (unsigned long)btrfs_header_chunk_tree_uuid(leaf), |
| 433 | BTRFS_UUID_SIZE); |
| 434 | btrfs_mark_buffer_dirty(leaf); |
| 435 | |
| 436 | memset(&root_item, 0, sizeof(root_item)); |
| 437 | |
| 438 | inode_item = &root_item.inode; |
| 439 | btrfs_set_stack_inode_generation(inode_item, 1); |
| 440 | btrfs_set_stack_inode_size(inode_item, 3); |
| 441 | btrfs_set_stack_inode_nlink(inode_item, 1); |
| 442 | btrfs_set_stack_inode_nbytes(inode_item, root->leafsize); |
| 443 | btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); |
| 444 | |
| 445 | btrfs_set_root_flags(&root_item, 0); |
| 446 | btrfs_set_root_limit(&root_item, 0); |
| 447 | btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); |
| 448 | |
| 449 | btrfs_set_root_bytenr(&root_item, leaf->start); |
| 450 | btrfs_set_root_generation(&root_item, trans->transid); |
| 451 | btrfs_set_root_level(&root_item, 0); |
| 452 | btrfs_set_root_refs(&root_item, 1); |
| 453 | btrfs_set_root_used(&root_item, leaf->len); |
| 454 | btrfs_set_root_last_snapshot(&root_item, 0); |
| 455 | |
| 456 | btrfs_set_root_generation_v2(&root_item, |
| 457 | btrfs_root_generation(&root_item)); |
| 458 | uuid_le_gen(&new_uuid); |
| 459 | memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE); |
| 460 | btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec); |
| 461 | btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec); |
| 462 | root_item.ctime = root_item.otime; |
| 463 | btrfs_set_root_ctransid(&root_item, trans->transid); |
| 464 | btrfs_set_root_otransid(&root_item, trans->transid); |
| 465 | |
| 466 | btrfs_tree_unlock(leaf); |
| 467 | free_extent_buffer(leaf); |
| 468 | leaf = NULL; |
| 469 | |
| 470 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 471 | |
| 472 | key.objectid = objectid; |
| 473 | key.offset = 0; |
| 474 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 475 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 476 | &root_item); |
| 477 | if (ret) |
| 478 | goto fail; |
| 479 | |
| 480 | key.offset = (u64)-1; |
| 481 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); |
| 482 | if (IS_ERR(new_root)) { |
| 483 | btrfs_abort_transaction(trans, root, PTR_ERR(new_root)); |
| 484 | ret = PTR_ERR(new_root); |
| 485 | goto fail; |
| 486 | } |
| 487 | |
| 488 | btrfs_record_root_in_trans(trans, new_root); |
| 489 | |
| 490 | ret = btrfs_create_subvol_root(trans, new_root, new_dirid); |
| 491 | if (ret) { |
| 492 | /* We potentially lose an unused inode item here */ |
| 493 | btrfs_abort_transaction(trans, root, ret); |
| 494 | goto fail; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * insert the directory item |
| 499 | */ |
| 500 | ret = btrfs_set_inode_index(dir, &index); |
| 501 | if (ret) { |
| 502 | btrfs_abort_transaction(trans, root, ret); |
| 503 | goto fail; |
| 504 | } |
| 505 | |
| 506 | ret = btrfs_insert_dir_item(trans, root, |
| 507 | name, namelen, dir, &key, |
| 508 | BTRFS_FT_DIR, index); |
| 509 | if (ret) { |
| 510 | btrfs_abort_transaction(trans, root, ret); |
| 511 | goto fail; |
| 512 | } |
| 513 | |
| 514 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
| 515 | ret = btrfs_update_inode(trans, root, dir); |
| 516 | BUG_ON(ret); |
| 517 | |
| 518 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 519 | objectid, root->root_key.objectid, |
| 520 | btrfs_ino(dir), index, name, namelen); |
| 521 | |
| 522 | BUG_ON(ret); |
| 523 | |
| 524 | fail: |
| 525 | trans->block_rsv = NULL; |
| 526 | trans->bytes_reserved = 0; |
| 527 | if (async_transid) { |
| 528 | *async_transid = trans->transid; |
| 529 | err = btrfs_commit_transaction_async(trans, root, 1); |
| 530 | if (err) |
| 531 | err = btrfs_commit_transaction(trans, root); |
| 532 | } else { |
| 533 | err = btrfs_commit_transaction(trans, root); |
| 534 | } |
| 535 | if (err && !ret) |
| 536 | ret = err; |
| 537 | |
| 538 | if (!ret) |
| 539 | d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); |
| 540 | out: |
| 541 | btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | static int create_snapshot(struct btrfs_root *root, struct inode *dir, |
| 546 | struct dentry *dentry, char *name, int namelen, |
| 547 | u64 *async_transid, bool readonly, |
| 548 | struct btrfs_qgroup_inherit *inherit) |
| 549 | { |
| 550 | struct inode *inode; |
| 551 | struct btrfs_pending_snapshot *pending_snapshot; |
| 552 | struct btrfs_trans_handle *trans; |
| 553 | int ret; |
| 554 | |
| 555 | if (!root->ref_cows) |
| 556 | return -EINVAL; |
| 557 | |
| 558 | ret = btrfs_start_delalloc_inodes(root, 0); |
| 559 | if (ret) |
| 560 | return ret; |
| 561 | |
| 562 | btrfs_wait_ordered_extents(root, 0); |
| 563 | |
| 564 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
| 565 | if (!pending_snapshot) |
| 566 | return -ENOMEM; |
| 567 | |
| 568 | btrfs_init_block_rsv(&pending_snapshot->block_rsv, |
| 569 | BTRFS_BLOCK_RSV_TEMP); |
| 570 | /* |
| 571 | * 1 - parent dir inode |
| 572 | * 2 - dir entries |
| 573 | * 1 - root item |
| 574 | * 2 - root ref/backref |
| 575 | * 1 - root of snapshot |
| 576 | */ |
| 577 | ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, |
| 578 | &pending_snapshot->block_rsv, 7, |
| 579 | &pending_snapshot->qgroup_reserved, |
| 580 | false); |
| 581 | if (ret) |
| 582 | goto out; |
| 583 | |
| 584 | pending_snapshot->dentry = dentry; |
| 585 | pending_snapshot->root = root; |
| 586 | pending_snapshot->readonly = readonly; |
| 587 | pending_snapshot->dir = dir; |
| 588 | pending_snapshot->inherit = inherit; |
| 589 | |
| 590 | trans = btrfs_start_transaction(root, 0); |
| 591 | if (IS_ERR(trans)) { |
| 592 | ret = PTR_ERR(trans); |
| 593 | goto fail; |
| 594 | } |
| 595 | |
| 596 | spin_lock(&root->fs_info->trans_lock); |
| 597 | list_add(&pending_snapshot->list, |
| 598 | &trans->transaction->pending_snapshots); |
| 599 | spin_unlock(&root->fs_info->trans_lock); |
| 600 | if (async_transid) { |
| 601 | *async_transid = trans->transid; |
| 602 | ret = btrfs_commit_transaction_async(trans, |
| 603 | root->fs_info->extent_root, 1); |
| 604 | if (ret) |
| 605 | ret = btrfs_commit_transaction(trans, root); |
| 606 | } else { |
| 607 | ret = btrfs_commit_transaction(trans, |
| 608 | root->fs_info->extent_root); |
| 609 | } |
| 610 | if (ret) |
| 611 | goto fail; |
| 612 | |
| 613 | ret = pending_snapshot->error; |
| 614 | if (ret) |
| 615 | goto fail; |
| 616 | |
| 617 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
| 618 | if (ret) |
| 619 | goto fail; |
| 620 | |
| 621 | inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); |
| 622 | if (IS_ERR(inode)) { |
| 623 | ret = PTR_ERR(inode); |
| 624 | goto fail; |
| 625 | } |
| 626 | BUG_ON(!inode); |
| 627 | d_instantiate(dentry, inode); |
| 628 | ret = 0; |
| 629 | fail: |
| 630 | btrfs_subvolume_release_metadata(BTRFS_I(dir)->root, |
| 631 | &pending_snapshot->block_rsv, |
| 632 | pending_snapshot->qgroup_reserved); |
| 633 | out: |
| 634 | kfree(pending_snapshot); |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | /* copy of check_sticky in fs/namei.c() |
| 639 | * It's inline, so penalty for filesystems that don't use sticky bit is |
| 640 | * minimal. |
| 641 | */ |
| 642 | static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) |
| 643 | { |
| 644 | kuid_t fsuid = current_fsuid(); |
| 645 | |
| 646 | if (!(dir->i_mode & S_ISVTX)) |
| 647 | return 0; |
| 648 | if (uid_eq(inode->i_uid, fsuid)) |
| 649 | return 0; |
| 650 | if (uid_eq(dir->i_uid, fsuid)) |
| 651 | return 0; |
| 652 | return !capable(CAP_FOWNER); |
| 653 | } |
| 654 | |
| 655 | /* copy of may_delete in fs/namei.c() |
| 656 | * Check whether we can remove a link victim from directory dir, check |
| 657 | * whether the type of victim is right. |
| 658 | * 1. We can't do it if dir is read-only (done in permission()) |
| 659 | * 2. We should have write and exec permissions on dir |
| 660 | * 3. We can't remove anything from append-only dir |
| 661 | * 4. We can't do anything with immutable dir (done in permission()) |
| 662 | * 5. If the sticky bit on dir is set we should either |
| 663 | * a. be owner of dir, or |
| 664 | * b. be owner of victim, or |
| 665 | * c. have CAP_FOWNER capability |
| 666 | * 6. If the victim is append-only or immutable we can't do antyhing with |
| 667 | * links pointing to it. |
| 668 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 669 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 670 | * 9. We can't remove a root or mountpoint. |
| 671 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by |
| 672 | * nfs_async_unlink(). |
| 673 | */ |
| 674 | |
| 675 | static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) |
| 676 | { |
| 677 | int error; |
| 678 | |
| 679 | if (!victim->d_inode) |
| 680 | return -ENOENT; |
| 681 | |
| 682 | BUG_ON(victim->d_parent->d_inode != dir); |
| 683 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
| 684 | |
| 685 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 686 | if (error) |
| 687 | return error; |
| 688 | if (IS_APPEND(dir)) |
| 689 | return -EPERM; |
| 690 | if (btrfs_check_sticky(dir, victim->d_inode)|| |
| 691 | IS_APPEND(victim->d_inode)|| |
| 692 | IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) |
| 693 | return -EPERM; |
| 694 | if (isdir) { |
| 695 | if (!S_ISDIR(victim->d_inode->i_mode)) |
| 696 | return -ENOTDIR; |
| 697 | if (IS_ROOT(victim)) |
| 698 | return -EBUSY; |
| 699 | } else if (S_ISDIR(victim->d_inode->i_mode)) |
| 700 | return -EISDIR; |
| 701 | if (IS_DEADDIR(dir)) |
| 702 | return -ENOENT; |
| 703 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 704 | return -EBUSY; |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | /* copy of may_create in fs/namei.c() */ |
| 709 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) |
| 710 | { |
| 711 | if (child->d_inode) |
| 712 | return -EEXIST; |
| 713 | if (IS_DEADDIR(dir)) |
| 714 | return -ENOENT; |
| 715 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * Create a new subvolume below @parent. This is largely modeled after |
| 720 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup |
| 721 | * inside this filesystem so it's quite a bit simpler. |
| 722 | */ |
| 723 | static noinline int btrfs_mksubvol(struct path *parent, |
| 724 | char *name, int namelen, |
| 725 | struct btrfs_root *snap_src, |
| 726 | u64 *async_transid, bool readonly, |
| 727 | struct btrfs_qgroup_inherit *inherit) |
| 728 | { |
| 729 | struct inode *dir = parent->dentry->d_inode; |
| 730 | struct dentry *dentry; |
| 731 | int error; |
| 732 | |
| 733 | error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); |
| 734 | if (error == -EINTR) |
| 735 | return error; |
| 736 | |
| 737 | dentry = lookup_one_len(name, parent->dentry, namelen); |
| 738 | error = PTR_ERR(dentry); |
| 739 | if (IS_ERR(dentry)) |
| 740 | goto out_unlock; |
| 741 | |
| 742 | error = -EEXIST; |
| 743 | if (dentry->d_inode) |
| 744 | goto out_dput; |
| 745 | |
| 746 | error = btrfs_may_create(dir, dentry); |
| 747 | if (error) |
| 748 | goto out_dput; |
| 749 | |
| 750 | /* |
| 751 | * even if this name doesn't exist, we may get hash collisions. |
| 752 | * check for them now when we can safely fail |
| 753 | */ |
| 754 | error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, |
| 755 | dir->i_ino, name, |
| 756 | namelen); |
| 757 | if (error) |
| 758 | goto out_dput; |
| 759 | |
| 760 | down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
| 761 | |
| 762 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) |
| 763 | goto out_up_read; |
| 764 | |
| 765 | if (snap_src) { |
| 766 | error = create_snapshot(snap_src, dir, dentry, name, namelen, |
| 767 | async_transid, readonly, inherit); |
| 768 | } else { |
| 769 | error = create_subvol(dir, dentry, name, namelen, |
| 770 | async_transid, inherit); |
| 771 | } |
| 772 | if (!error) |
| 773 | fsnotify_mkdir(dir, dentry); |
| 774 | out_up_read: |
| 775 | up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
| 776 | out_dput: |
| 777 | dput(dentry); |
| 778 | out_unlock: |
| 779 | mutex_unlock(&dir->i_mutex); |
| 780 | return error; |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | * When we're defragging a range, we don't want to kick it off again |
| 785 | * if it is really just waiting for delalloc to send it down. |
| 786 | * If we find a nice big extent or delalloc range for the bytes in the |
| 787 | * file you want to defrag, we return 0 to let you know to skip this |
| 788 | * part of the file |
| 789 | */ |
| 790 | static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) |
| 791 | { |
| 792 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 793 | struct extent_map *em = NULL; |
| 794 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 795 | u64 end; |
| 796 | |
| 797 | read_lock(&em_tree->lock); |
| 798 | em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); |
| 799 | read_unlock(&em_tree->lock); |
| 800 | |
| 801 | if (em) { |
| 802 | end = extent_map_end(em); |
| 803 | free_extent_map(em); |
| 804 | if (end - offset > thresh) |
| 805 | return 0; |
| 806 | } |
| 807 | /* if we already have a nice delalloc here, just stop */ |
| 808 | thresh /= 2; |
| 809 | end = count_range_bits(io_tree, &offset, offset + thresh, |
| 810 | thresh, EXTENT_DELALLOC, 1); |
| 811 | if (end >= thresh) |
| 812 | return 0; |
| 813 | return 1; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * helper function to walk through a file and find extents |
| 818 | * newer than a specific transid, and smaller than thresh. |
| 819 | * |
| 820 | * This is used by the defragging code to find new and small |
| 821 | * extents |
| 822 | */ |
| 823 | static int find_new_extents(struct btrfs_root *root, |
| 824 | struct inode *inode, u64 newer_than, |
| 825 | u64 *off, int thresh) |
| 826 | { |
| 827 | struct btrfs_path *path; |
| 828 | struct btrfs_key min_key; |
| 829 | struct btrfs_key max_key; |
| 830 | struct extent_buffer *leaf; |
| 831 | struct btrfs_file_extent_item *extent; |
| 832 | int type; |
| 833 | int ret; |
| 834 | u64 ino = btrfs_ino(inode); |
| 835 | |
| 836 | path = btrfs_alloc_path(); |
| 837 | if (!path) |
| 838 | return -ENOMEM; |
| 839 | |
| 840 | min_key.objectid = ino; |
| 841 | min_key.type = BTRFS_EXTENT_DATA_KEY; |
| 842 | min_key.offset = *off; |
| 843 | |
| 844 | max_key.objectid = ino; |
| 845 | max_key.type = (u8)-1; |
| 846 | max_key.offset = (u64)-1; |
| 847 | |
| 848 | path->keep_locks = 1; |
| 849 | |
| 850 | while(1) { |
| 851 | ret = btrfs_search_forward(root, &min_key, &max_key, |
| 852 | path, newer_than); |
| 853 | if (ret != 0) |
| 854 | goto none; |
| 855 | if (min_key.objectid != ino) |
| 856 | goto none; |
| 857 | if (min_key.type != BTRFS_EXTENT_DATA_KEY) |
| 858 | goto none; |
| 859 | |
| 860 | leaf = path->nodes[0]; |
| 861 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 862 | struct btrfs_file_extent_item); |
| 863 | |
| 864 | type = btrfs_file_extent_type(leaf, extent); |
| 865 | if (type == BTRFS_FILE_EXTENT_REG && |
| 866 | btrfs_file_extent_num_bytes(leaf, extent) < thresh && |
| 867 | check_defrag_in_cache(inode, min_key.offset, thresh)) { |
| 868 | *off = min_key.offset; |
| 869 | btrfs_free_path(path); |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | if (min_key.offset == (u64)-1) |
| 874 | goto none; |
| 875 | |
| 876 | min_key.offset++; |
| 877 | btrfs_release_path(path); |
| 878 | } |
| 879 | none: |
| 880 | btrfs_free_path(path); |
| 881 | return -ENOENT; |
| 882 | } |
| 883 | |
| 884 | static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) |
| 885 | { |
| 886 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 887 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 888 | struct extent_map *em; |
| 889 | u64 len = PAGE_CACHE_SIZE; |
| 890 | |
| 891 | /* |
| 892 | * hopefully we have this extent in the tree already, try without |
| 893 | * the full extent lock |
| 894 | */ |
| 895 | read_lock(&em_tree->lock); |
| 896 | em = lookup_extent_mapping(em_tree, start, len); |
| 897 | read_unlock(&em_tree->lock); |
| 898 | |
| 899 | if (!em) { |
| 900 | /* get the big lock and read metadata off disk */ |
| 901 | lock_extent(io_tree, start, start + len - 1); |
| 902 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
| 903 | unlock_extent(io_tree, start, start + len - 1); |
| 904 | |
| 905 | if (IS_ERR(em)) |
| 906 | return NULL; |
| 907 | } |
| 908 | |
| 909 | return em; |
| 910 | } |
| 911 | |
| 912 | static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) |
| 913 | { |
| 914 | struct extent_map *next; |
| 915 | bool ret = true; |
| 916 | |
| 917 | /* this is the last extent */ |
| 918 | if (em->start + em->len >= i_size_read(inode)) |
| 919 | return false; |
| 920 | |
| 921 | next = defrag_lookup_extent(inode, em->start + em->len); |
| 922 | if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) |
| 923 | ret = false; |
| 924 | |
| 925 | free_extent_map(next); |
| 926 | return ret; |
| 927 | } |
| 928 | |
| 929 | static int should_defrag_range(struct inode *inode, u64 start, int thresh, |
| 930 | u64 *last_len, u64 *skip, u64 *defrag_end, |
| 931 | int compress) |
| 932 | { |
| 933 | struct extent_map *em; |
| 934 | int ret = 1; |
| 935 | bool next_mergeable = true; |
| 936 | |
| 937 | /* |
| 938 | * make sure that once we start defragging an extent, we keep on |
| 939 | * defragging it |
| 940 | */ |
| 941 | if (start < *defrag_end) |
| 942 | return 1; |
| 943 | |
| 944 | *skip = 0; |
| 945 | |
| 946 | em = defrag_lookup_extent(inode, start); |
| 947 | if (!em) |
| 948 | return 0; |
| 949 | |
| 950 | /* this will cover holes, and inline extents */ |
| 951 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
| 952 | ret = 0; |
| 953 | goto out; |
| 954 | } |
| 955 | |
| 956 | next_mergeable = defrag_check_next_extent(inode, em); |
| 957 | |
| 958 | /* |
| 959 | * we hit a real extent, if it is big or the next extent is not a |
| 960 | * real extent, don't bother defragging it |
| 961 | */ |
| 962 | if (!compress && (*last_len == 0 || *last_len >= thresh) && |
| 963 | (em->len >= thresh || !next_mergeable)) |
| 964 | ret = 0; |
| 965 | out: |
| 966 | /* |
| 967 | * last_len ends up being a counter of how many bytes we've defragged. |
| 968 | * every time we choose not to defrag an extent, we reset *last_len |
| 969 | * so that the next tiny extent will force a defrag. |
| 970 | * |
| 971 | * The end result of this is that tiny extents before a single big |
| 972 | * extent will force at least part of that big extent to be defragged. |
| 973 | */ |
| 974 | if (ret) { |
| 975 | *defrag_end = extent_map_end(em); |
| 976 | } else { |
| 977 | *last_len = 0; |
| 978 | *skip = extent_map_end(em); |
| 979 | *defrag_end = 0; |
| 980 | } |
| 981 | |
| 982 | free_extent_map(em); |
| 983 | return ret; |
| 984 | } |
| 985 | |
| 986 | /* |
| 987 | * it doesn't do much good to defrag one or two pages |
| 988 | * at a time. This pulls in a nice chunk of pages |
| 989 | * to COW and defrag. |
| 990 | * |
| 991 | * It also makes sure the delalloc code has enough |
| 992 | * dirty data to avoid making new small extents as part |
| 993 | * of the defrag |
| 994 | * |
| 995 | * It's a good idea to start RA on this range |
| 996 | * before calling this. |
| 997 | */ |
| 998 | static int cluster_pages_for_defrag(struct inode *inode, |
| 999 | struct page **pages, |
| 1000 | unsigned long start_index, |
| 1001 | int num_pages) |
| 1002 | { |
| 1003 | unsigned long file_end; |
| 1004 | u64 isize = i_size_read(inode); |
| 1005 | u64 page_start; |
| 1006 | u64 page_end; |
| 1007 | u64 page_cnt; |
| 1008 | int ret; |
| 1009 | int i; |
| 1010 | int i_done; |
| 1011 | struct btrfs_ordered_extent *ordered; |
| 1012 | struct extent_state *cached_state = NULL; |
| 1013 | struct extent_io_tree *tree; |
| 1014 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
| 1015 | |
| 1016 | file_end = (isize - 1) >> PAGE_CACHE_SHIFT; |
| 1017 | if (!isize || start_index > file_end) |
| 1018 | return 0; |
| 1019 | |
| 1020 | page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); |
| 1021 | |
| 1022 | ret = btrfs_delalloc_reserve_space(inode, |
| 1023 | page_cnt << PAGE_CACHE_SHIFT); |
| 1024 | if (ret) |
| 1025 | return ret; |
| 1026 | i_done = 0; |
| 1027 | tree = &BTRFS_I(inode)->io_tree; |
| 1028 | |
| 1029 | /* step one, lock all the pages */ |
| 1030 | for (i = 0; i < page_cnt; i++) { |
| 1031 | struct page *page; |
| 1032 | again: |
| 1033 | page = find_or_create_page(inode->i_mapping, |
| 1034 | start_index + i, mask); |
| 1035 | if (!page) |
| 1036 | break; |
| 1037 | |
| 1038 | page_start = page_offset(page); |
| 1039 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 1040 | while (1) { |
| 1041 | lock_extent(tree, page_start, page_end); |
| 1042 | ordered = btrfs_lookup_ordered_extent(inode, |
| 1043 | page_start); |
| 1044 | unlock_extent(tree, page_start, page_end); |
| 1045 | if (!ordered) |
| 1046 | break; |
| 1047 | |
| 1048 | unlock_page(page); |
| 1049 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 1050 | btrfs_put_ordered_extent(ordered); |
| 1051 | lock_page(page); |
| 1052 | /* |
| 1053 | * we unlocked the page above, so we need check if |
| 1054 | * it was released or not. |
| 1055 | */ |
| 1056 | if (page->mapping != inode->i_mapping) { |
| 1057 | unlock_page(page); |
| 1058 | page_cache_release(page); |
| 1059 | goto again; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | if (!PageUptodate(page)) { |
| 1064 | btrfs_readpage(NULL, page); |
| 1065 | lock_page(page); |
| 1066 | if (!PageUptodate(page)) { |
| 1067 | unlock_page(page); |
| 1068 | page_cache_release(page); |
| 1069 | ret = -EIO; |
| 1070 | break; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | if (page->mapping != inode->i_mapping) { |
| 1075 | unlock_page(page); |
| 1076 | page_cache_release(page); |
| 1077 | goto again; |
| 1078 | } |
| 1079 | |
| 1080 | pages[i] = page; |
| 1081 | i_done++; |
| 1082 | } |
| 1083 | if (!i_done || ret) |
| 1084 | goto out; |
| 1085 | |
| 1086 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) |
| 1087 | goto out; |
| 1088 | |
| 1089 | /* |
| 1090 | * so now we have a nice long stream of locked |
| 1091 | * and up to date pages, lets wait on them |
| 1092 | */ |
| 1093 | for (i = 0; i < i_done; i++) |
| 1094 | wait_on_page_writeback(pages[i]); |
| 1095 | |
| 1096 | page_start = page_offset(pages[0]); |
| 1097 | page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; |
| 1098 | |
| 1099 | lock_extent_bits(&BTRFS_I(inode)->io_tree, |
| 1100 | page_start, page_end - 1, 0, &cached_state); |
| 1101 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, |
| 1102 | page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | |
| 1103 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, |
| 1104 | &cached_state, GFP_NOFS); |
| 1105 | |
| 1106 | if (i_done != page_cnt) { |
| 1107 | spin_lock(&BTRFS_I(inode)->lock); |
| 1108 | BTRFS_I(inode)->outstanding_extents++; |
| 1109 | spin_unlock(&BTRFS_I(inode)->lock); |
| 1110 | btrfs_delalloc_release_space(inode, |
| 1111 | (page_cnt - i_done) << PAGE_CACHE_SHIFT); |
| 1112 | } |
| 1113 | |
| 1114 | |
| 1115 | set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, |
| 1116 | &cached_state, GFP_NOFS); |
| 1117 | |
| 1118 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 1119 | page_start, page_end - 1, &cached_state, |
| 1120 | GFP_NOFS); |
| 1121 | |
| 1122 | for (i = 0; i < i_done; i++) { |
| 1123 | clear_page_dirty_for_io(pages[i]); |
| 1124 | ClearPageChecked(pages[i]); |
| 1125 | set_page_extent_mapped(pages[i]); |
| 1126 | set_page_dirty(pages[i]); |
| 1127 | unlock_page(pages[i]); |
| 1128 | page_cache_release(pages[i]); |
| 1129 | } |
| 1130 | return i_done; |
| 1131 | out: |
| 1132 | for (i = 0; i < i_done; i++) { |
| 1133 | unlock_page(pages[i]); |
| 1134 | page_cache_release(pages[i]); |
| 1135 | } |
| 1136 | btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT); |
| 1137 | return ret; |
| 1138 | |
| 1139 | } |
| 1140 | |
| 1141 | int btrfs_defrag_file(struct inode *inode, struct file *file, |
| 1142 | struct btrfs_ioctl_defrag_range_args *range, |
| 1143 | u64 newer_than, unsigned long max_to_defrag) |
| 1144 | { |
| 1145 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1146 | struct file_ra_state *ra = NULL; |
| 1147 | unsigned long last_index; |
| 1148 | u64 isize = i_size_read(inode); |
| 1149 | u64 last_len = 0; |
| 1150 | u64 skip = 0; |
| 1151 | u64 defrag_end = 0; |
| 1152 | u64 newer_off = range->start; |
| 1153 | unsigned long i; |
| 1154 | unsigned long ra_index = 0; |
| 1155 | int ret; |
| 1156 | int defrag_count = 0; |
| 1157 | int compress_type = BTRFS_COMPRESS_ZLIB; |
| 1158 | int extent_thresh = range->extent_thresh; |
| 1159 | int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; |
| 1160 | int cluster = max_cluster; |
| 1161 | u64 new_align = ~((u64)128 * 1024 - 1); |
| 1162 | struct page **pages = NULL; |
| 1163 | |
| 1164 | if (isize == 0) |
| 1165 | return 0; |
| 1166 | |
| 1167 | if (range->start >= isize) |
| 1168 | return -EINVAL; |
| 1169 | |
| 1170 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { |
| 1171 | if (range->compress_type > BTRFS_COMPRESS_TYPES) |
| 1172 | return -EINVAL; |
| 1173 | if (range->compress_type) |
| 1174 | compress_type = range->compress_type; |
| 1175 | } |
| 1176 | |
| 1177 | if (extent_thresh == 0) |
| 1178 | extent_thresh = 256 * 1024; |
| 1179 | |
| 1180 | /* |
| 1181 | * if we were not given a file, allocate a readahead |
| 1182 | * context |
| 1183 | */ |
| 1184 | if (!file) { |
| 1185 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
| 1186 | if (!ra) |
| 1187 | return -ENOMEM; |
| 1188 | file_ra_state_init(ra, inode->i_mapping); |
| 1189 | } else { |
| 1190 | ra = &file->f_ra; |
| 1191 | } |
| 1192 | |
| 1193 | pages = kmalloc(sizeof(struct page *) * max_cluster, |
| 1194 | GFP_NOFS); |
| 1195 | if (!pages) { |
| 1196 | ret = -ENOMEM; |
| 1197 | goto out_ra; |
| 1198 | } |
| 1199 | |
| 1200 | /* find the last page to defrag */ |
| 1201 | if (range->start + range->len > range->start) { |
| 1202 | last_index = min_t(u64, isize - 1, |
| 1203 | range->start + range->len - 1) >> PAGE_CACHE_SHIFT; |
| 1204 | } else { |
| 1205 | last_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
| 1206 | } |
| 1207 | |
| 1208 | if (newer_than) { |
| 1209 | ret = find_new_extents(root, inode, newer_than, |
| 1210 | &newer_off, 64 * 1024); |
| 1211 | if (!ret) { |
| 1212 | range->start = newer_off; |
| 1213 | /* |
| 1214 | * we always align our defrag to help keep |
| 1215 | * the extents in the file evenly spaced |
| 1216 | */ |
| 1217 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; |
| 1218 | } else |
| 1219 | goto out_ra; |
| 1220 | } else { |
| 1221 | i = range->start >> PAGE_CACHE_SHIFT; |
| 1222 | } |
| 1223 | if (!max_to_defrag) |
| 1224 | max_to_defrag = last_index + 1; |
| 1225 | |
| 1226 | /* |
| 1227 | * make writeback starts from i, so the defrag range can be |
| 1228 | * written sequentially. |
| 1229 | */ |
| 1230 | if (i < inode->i_mapping->writeback_index) |
| 1231 | inode->i_mapping->writeback_index = i; |
| 1232 | |
| 1233 | while (i <= last_index && defrag_count < max_to_defrag && |
| 1234 | (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> |
| 1235 | PAGE_CACHE_SHIFT)) { |
| 1236 | /* |
| 1237 | * make sure we stop running if someone unmounts |
| 1238 | * the FS |
| 1239 | */ |
| 1240 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) |
| 1241 | break; |
| 1242 | |
| 1243 | if (btrfs_defrag_cancelled(root->fs_info)) { |
| 1244 | printk(KERN_DEBUG "btrfs: defrag_file cancelled\n"); |
| 1245 | ret = -EAGAIN; |
| 1246 | break; |
| 1247 | } |
| 1248 | |
| 1249 | if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, |
| 1250 | extent_thresh, &last_len, &skip, |
| 1251 | &defrag_end, range->flags & |
| 1252 | BTRFS_DEFRAG_RANGE_COMPRESS)) { |
| 1253 | unsigned long next; |
| 1254 | /* |
| 1255 | * the should_defrag function tells us how much to skip |
| 1256 | * bump our counter by the suggested amount |
| 1257 | */ |
| 1258 | next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 1259 | i = max(i + 1, next); |
| 1260 | continue; |
| 1261 | } |
| 1262 | |
| 1263 | if (!newer_than) { |
| 1264 | cluster = (PAGE_CACHE_ALIGN(defrag_end) >> |
| 1265 | PAGE_CACHE_SHIFT) - i; |
| 1266 | cluster = min(cluster, max_cluster); |
| 1267 | } else { |
| 1268 | cluster = max_cluster; |
| 1269 | } |
| 1270 | |
| 1271 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) |
| 1272 | BTRFS_I(inode)->force_compress = compress_type; |
| 1273 | |
| 1274 | if (i + cluster > ra_index) { |
| 1275 | ra_index = max(i, ra_index); |
| 1276 | btrfs_force_ra(inode->i_mapping, ra, file, ra_index, |
| 1277 | cluster); |
| 1278 | ra_index += max_cluster; |
| 1279 | } |
| 1280 | |
| 1281 | mutex_lock(&inode->i_mutex); |
| 1282 | ret = cluster_pages_for_defrag(inode, pages, i, cluster); |
| 1283 | if (ret < 0) { |
| 1284 | mutex_unlock(&inode->i_mutex); |
| 1285 | goto out_ra; |
| 1286 | } |
| 1287 | |
| 1288 | defrag_count += ret; |
| 1289 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 1290 | mutex_unlock(&inode->i_mutex); |
| 1291 | |
| 1292 | if (newer_than) { |
| 1293 | if (newer_off == (u64)-1) |
| 1294 | break; |
| 1295 | |
| 1296 | if (ret > 0) |
| 1297 | i += ret; |
| 1298 | |
| 1299 | newer_off = max(newer_off + 1, |
| 1300 | (u64)i << PAGE_CACHE_SHIFT); |
| 1301 | |
| 1302 | ret = find_new_extents(root, inode, |
| 1303 | newer_than, &newer_off, |
| 1304 | 64 * 1024); |
| 1305 | if (!ret) { |
| 1306 | range->start = newer_off; |
| 1307 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; |
| 1308 | } else { |
| 1309 | break; |
| 1310 | } |
| 1311 | } else { |
| 1312 | if (ret > 0) { |
| 1313 | i += ret; |
| 1314 | last_len += ret << PAGE_CACHE_SHIFT; |
| 1315 | } else { |
| 1316 | i++; |
| 1317 | last_len = 0; |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) |
| 1323 | filemap_flush(inode->i_mapping); |
| 1324 | |
| 1325 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { |
| 1326 | /* the filemap_flush will queue IO into the worker threads, but |
| 1327 | * we have to make sure the IO is actually started and that |
| 1328 | * ordered extents get created before we return |
| 1329 | */ |
| 1330 | atomic_inc(&root->fs_info->async_submit_draining); |
| 1331 | while (atomic_read(&root->fs_info->nr_async_submits) || |
| 1332 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
| 1333 | wait_event(root->fs_info->async_submit_wait, |
| 1334 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
| 1335 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
| 1336 | } |
| 1337 | atomic_dec(&root->fs_info->async_submit_draining); |
| 1338 | |
| 1339 | mutex_lock(&inode->i_mutex); |
| 1340 | BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; |
| 1341 | mutex_unlock(&inode->i_mutex); |
| 1342 | } |
| 1343 | |
| 1344 | if (range->compress_type == BTRFS_COMPRESS_LZO) { |
| 1345 | btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO); |
| 1346 | } |
| 1347 | |
| 1348 | ret = defrag_count; |
| 1349 | |
| 1350 | out_ra: |
| 1351 | if (!file) |
| 1352 | kfree(ra); |
| 1353 | kfree(pages); |
| 1354 | return ret; |
| 1355 | } |
| 1356 | |
| 1357 | static noinline int btrfs_ioctl_resize(struct file *file, |
| 1358 | void __user *arg) |
| 1359 | { |
| 1360 | u64 new_size; |
| 1361 | u64 old_size; |
| 1362 | u64 devid = 1; |
| 1363 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 1364 | struct btrfs_ioctl_vol_args *vol_args; |
| 1365 | struct btrfs_trans_handle *trans; |
| 1366 | struct btrfs_device *device = NULL; |
| 1367 | char *sizestr; |
| 1368 | char *devstr = NULL; |
| 1369 | int ret = 0; |
| 1370 | int mod = 0; |
| 1371 | |
| 1372 | if (!capable(CAP_SYS_ADMIN)) |
| 1373 | return -EPERM; |
| 1374 | |
| 1375 | ret = mnt_want_write_file(file); |
| 1376 | if (ret) |
| 1377 | return ret; |
| 1378 | |
| 1379 | if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, |
| 1380 | 1)) { |
| 1381 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); |
| 1382 | mnt_drop_write_file(file); |
| 1383 | return -EINVAL; |
| 1384 | } |
| 1385 | |
| 1386 | mutex_lock(&root->fs_info->volume_mutex); |
| 1387 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1388 | if (IS_ERR(vol_args)) { |
| 1389 | ret = PTR_ERR(vol_args); |
| 1390 | goto out; |
| 1391 | } |
| 1392 | |
| 1393 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
| 1394 | |
| 1395 | sizestr = vol_args->name; |
| 1396 | devstr = strchr(sizestr, ':'); |
| 1397 | if (devstr) { |
| 1398 | char *end; |
| 1399 | sizestr = devstr + 1; |
| 1400 | *devstr = '\0'; |
| 1401 | devstr = vol_args->name; |
| 1402 | devid = simple_strtoull(devstr, &end, 10); |
| 1403 | if (!devid) { |
| 1404 | ret = -EINVAL; |
| 1405 | goto out_free; |
| 1406 | } |
| 1407 | printk(KERN_INFO "btrfs: resizing devid %llu\n", |
| 1408 | (unsigned long long)devid); |
| 1409 | } |
| 1410 | |
| 1411 | device = btrfs_find_device(root->fs_info, devid, NULL, NULL); |
| 1412 | if (!device) { |
| 1413 | printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", |
| 1414 | (unsigned long long)devid); |
| 1415 | ret = -ENODEV; |
| 1416 | goto out_free; |
| 1417 | } |
| 1418 | |
| 1419 | if (!device->writeable) { |
| 1420 | printk(KERN_INFO "btrfs: resizer unable to apply on " |
| 1421 | "readonly device %llu\n", |
| 1422 | (unsigned long long)devid); |
| 1423 | ret = -EPERM; |
| 1424 | goto out_free; |
| 1425 | } |
| 1426 | |
| 1427 | if (!strcmp(sizestr, "max")) |
| 1428 | new_size = device->bdev->bd_inode->i_size; |
| 1429 | else { |
| 1430 | if (sizestr[0] == '-') { |
| 1431 | mod = -1; |
| 1432 | sizestr++; |
| 1433 | } else if (sizestr[0] == '+') { |
| 1434 | mod = 1; |
| 1435 | sizestr++; |
| 1436 | } |
| 1437 | new_size = memparse(sizestr, NULL); |
| 1438 | if (new_size == 0) { |
| 1439 | ret = -EINVAL; |
| 1440 | goto out_free; |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | if (device->is_tgtdev_for_dev_replace) { |
| 1445 | ret = -EPERM; |
| 1446 | goto out_free; |
| 1447 | } |
| 1448 | |
| 1449 | old_size = device->total_bytes; |
| 1450 | |
| 1451 | if (mod < 0) { |
| 1452 | if (new_size > old_size) { |
| 1453 | ret = -EINVAL; |
| 1454 | goto out_free; |
| 1455 | } |
| 1456 | new_size = old_size - new_size; |
| 1457 | } else if (mod > 0) { |
| 1458 | new_size = old_size + new_size; |
| 1459 | } |
| 1460 | |
| 1461 | if (new_size < 256 * 1024 * 1024) { |
| 1462 | ret = -EINVAL; |
| 1463 | goto out_free; |
| 1464 | } |
| 1465 | if (new_size > device->bdev->bd_inode->i_size) { |
| 1466 | ret = -EFBIG; |
| 1467 | goto out_free; |
| 1468 | } |
| 1469 | |
| 1470 | do_div(new_size, root->sectorsize); |
| 1471 | new_size *= root->sectorsize; |
| 1472 | |
| 1473 | printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n", |
| 1474 | rcu_str_deref(device->name), |
| 1475 | (unsigned long long)new_size); |
| 1476 | |
| 1477 | if (new_size > old_size) { |
| 1478 | trans = btrfs_start_transaction(root, 0); |
| 1479 | if (IS_ERR(trans)) { |
| 1480 | ret = PTR_ERR(trans); |
| 1481 | goto out_free; |
| 1482 | } |
| 1483 | ret = btrfs_grow_device(trans, device, new_size); |
| 1484 | btrfs_commit_transaction(trans, root); |
| 1485 | } else if (new_size < old_size) { |
| 1486 | ret = btrfs_shrink_device(device, new_size); |
| 1487 | } /* equal, nothing need to do */ |
| 1488 | |
| 1489 | out_free: |
| 1490 | kfree(vol_args); |
| 1491 | out: |
| 1492 | mutex_unlock(&root->fs_info->volume_mutex); |
| 1493 | atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); |
| 1494 | mnt_drop_write_file(file); |
| 1495 | return ret; |
| 1496 | } |
| 1497 | |
| 1498 | static noinline int btrfs_ioctl_snap_create_transid(struct file *file, |
| 1499 | char *name, unsigned long fd, int subvol, |
| 1500 | u64 *transid, bool readonly, |
| 1501 | struct btrfs_qgroup_inherit *inherit) |
| 1502 | { |
| 1503 | int namelen; |
| 1504 | int ret = 0; |
| 1505 | |
| 1506 | ret = mnt_want_write_file(file); |
| 1507 | if (ret) |
| 1508 | goto out; |
| 1509 | |
| 1510 | namelen = strlen(name); |
| 1511 | if (strchr(name, '/')) { |
| 1512 | ret = -EINVAL; |
| 1513 | goto out_drop_write; |
| 1514 | } |
| 1515 | |
| 1516 | if (name[0] == '.' && |
| 1517 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { |
| 1518 | ret = -EEXIST; |
| 1519 | goto out_drop_write; |
| 1520 | } |
| 1521 | |
| 1522 | if (subvol) { |
| 1523 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
| 1524 | NULL, transid, readonly, inherit); |
| 1525 | } else { |
| 1526 | struct fd src = fdget(fd); |
| 1527 | struct inode *src_inode; |
| 1528 | if (!src.file) { |
| 1529 | ret = -EINVAL; |
| 1530 | goto out_drop_write; |
| 1531 | } |
| 1532 | |
| 1533 | src_inode = file_inode(src.file); |
| 1534 | if (src_inode->i_sb != file_inode(file)->i_sb) { |
| 1535 | printk(KERN_INFO "btrfs: Snapshot src from " |
| 1536 | "another FS\n"); |
| 1537 | ret = -EINVAL; |
| 1538 | } else { |
| 1539 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
| 1540 | BTRFS_I(src_inode)->root, |
| 1541 | transid, readonly, inherit); |
| 1542 | } |
| 1543 | fdput(src); |
| 1544 | } |
| 1545 | out_drop_write: |
| 1546 | mnt_drop_write_file(file); |
| 1547 | out: |
| 1548 | return ret; |
| 1549 | } |
| 1550 | |
| 1551 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
| 1552 | void __user *arg, int subvol) |
| 1553 | { |
| 1554 | struct btrfs_ioctl_vol_args *vol_args; |
| 1555 | int ret; |
| 1556 | |
| 1557 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1558 | if (IS_ERR(vol_args)) |
| 1559 | return PTR_ERR(vol_args); |
| 1560 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
| 1561 | |
| 1562 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
| 1563 | vol_args->fd, subvol, |
| 1564 | NULL, false, NULL); |
| 1565 | |
| 1566 | kfree(vol_args); |
| 1567 | return ret; |
| 1568 | } |
| 1569 | |
| 1570 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
| 1571 | void __user *arg, int subvol) |
| 1572 | { |
| 1573 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
| 1574 | int ret; |
| 1575 | u64 transid = 0; |
| 1576 | u64 *ptr = NULL; |
| 1577 | bool readonly = false; |
| 1578 | struct btrfs_qgroup_inherit *inherit = NULL; |
| 1579 | |
| 1580 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1581 | if (IS_ERR(vol_args)) |
| 1582 | return PTR_ERR(vol_args); |
| 1583 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; |
| 1584 | |
| 1585 | if (vol_args->flags & |
| 1586 | ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | |
| 1587 | BTRFS_SUBVOL_QGROUP_INHERIT)) { |
| 1588 | ret = -EOPNOTSUPP; |
| 1589 | goto out; |
| 1590 | } |
| 1591 | |
| 1592 | if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) |
| 1593 | ptr = &transid; |
| 1594 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
| 1595 | readonly = true; |
| 1596 | if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { |
| 1597 | if (vol_args->size > PAGE_CACHE_SIZE) { |
| 1598 | ret = -EINVAL; |
| 1599 | goto out; |
| 1600 | } |
| 1601 | inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); |
| 1602 | if (IS_ERR(inherit)) { |
| 1603 | ret = PTR_ERR(inherit); |
| 1604 | goto out; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
| 1609 | vol_args->fd, subvol, ptr, |
| 1610 | readonly, inherit); |
| 1611 | |
| 1612 | if (ret == 0 && ptr && |
| 1613 | copy_to_user(arg + |
| 1614 | offsetof(struct btrfs_ioctl_vol_args_v2, |
| 1615 | transid), ptr, sizeof(*ptr))) |
| 1616 | ret = -EFAULT; |
| 1617 | out: |
| 1618 | kfree(vol_args); |
| 1619 | kfree(inherit); |
| 1620 | return ret; |
| 1621 | } |
| 1622 | |
| 1623 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
| 1624 | void __user *arg) |
| 1625 | { |
| 1626 | struct inode *inode = file_inode(file); |
| 1627 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1628 | int ret = 0; |
| 1629 | u64 flags = 0; |
| 1630 | |
| 1631 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) |
| 1632 | return -EINVAL; |
| 1633 | |
| 1634 | down_read(&root->fs_info->subvol_sem); |
| 1635 | if (btrfs_root_readonly(root)) |
| 1636 | flags |= BTRFS_SUBVOL_RDONLY; |
| 1637 | up_read(&root->fs_info->subvol_sem); |
| 1638 | |
| 1639 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 1640 | ret = -EFAULT; |
| 1641 | |
| 1642 | return ret; |
| 1643 | } |
| 1644 | |
| 1645 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, |
| 1646 | void __user *arg) |
| 1647 | { |
| 1648 | struct inode *inode = file_inode(file); |
| 1649 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1650 | struct btrfs_trans_handle *trans; |
| 1651 | u64 root_flags; |
| 1652 | u64 flags; |
| 1653 | int ret = 0; |
| 1654 | |
| 1655 | ret = mnt_want_write_file(file); |
| 1656 | if (ret) |
| 1657 | goto out; |
| 1658 | |
| 1659 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { |
| 1660 | ret = -EINVAL; |
| 1661 | goto out_drop_write; |
| 1662 | } |
| 1663 | |
| 1664 | if (copy_from_user(&flags, arg, sizeof(flags))) { |
| 1665 | ret = -EFAULT; |
| 1666 | goto out_drop_write; |
| 1667 | } |
| 1668 | |
| 1669 | if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { |
| 1670 | ret = -EINVAL; |
| 1671 | goto out_drop_write; |
| 1672 | } |
| 1673 | |
| 1674 | if (flags & ~BTRFS_SUBVOL_RDONLY) { |
| 1675 | ret = -EOPNOTSUPP; |
| 1676 | goto out_drop_write; |
| 1677 | } |
| 1678 | |
| 1679 | if (!inode_owner_or_capable(inode)) { |
| 1680 | ret = -EACCES; |
| 1681 | goto out_drop_write; |
| 1682 | } |
| 1683 | |
| 1684 | down_write(&root->fs_info->subvol_sem); |
| 1685 | |
| 1686 | /* nothing to do */ |
| 1687 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) |
| 1688 | goto out_drop_sem; |
| 1689 | |
| 1690 | root_flags = btrfs_root_flags(&root->root_item); |
| 1691 | if (flags & BTRFS_SUBVOL_RDONLY) |
| 1692 | btrfs_set_root_flags(&root->root_item, |
| 1693 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); |
| 1694 | else |
| 1695 | btrfs_set_root_flags(&root->root_item, |
| 1696 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); |
| 1697 | |
| 1698 | trans = btrfs_start_transaction(root, 1); |
| 1699 | if (IS_ERR(trans)) { |
| 1700 | ret = PTR_ERR(trans); |
| 1701 | goto out_reset; |
| 1702 | } |
| 1703 | |
| 1704 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 1705 | &root->root_key, &root->root_item); |
| 1706 | |
| 1707 | btrfs_commit_transaction(trans, root); |
| 1708 | out_reset: |
| 1709 | if (ret) |
| 1710 | btrfs_set_root_flags(&root->root_item, root_flags); |
| 1711 | out_drop_sem: |
| 1712 | up_write(&root->fs_info->subvol_sem); |
| 1713 | out_drop_write: |
| 1714 | mnt_drop_write_file(file); |
| 1715 | out: |
| 1716 | return ret; |
| 1717 | } |
| 1718 | |
| 1719 | /* |
| 1720 | * helper to check if the subvolume references other subvolumes |
| 1721 | */ |
| 1722 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
| 1723 | { |
| 1724 | struct btrfs_path *path; |
| 1725 | struct btrfs_key key; |
| 1726 | int ret; |
| 1727 | |
| 1728 | path = btrfs_alloc_path(); |
| 1729 | if (!path) |
| 1730 | return -ENOMEM; |
| 1731 | |
| 1732 | key.objectid = root->root_key.objectid; |
| 1733 | key.type = BTRFS_ROOT_REF_KEY; |
| 1734 | key.offset = (u64)-1; |
| 1735 | |
| 1736 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, |
| 1737 | &key, path, 0, 0); |
| 1738 | if (ret < 0) |
| 1739 | goto out; |
| 1740 | BUG_ON(ret == 0); |
| 1741 | |
| 1742 | ret = 0; |
| 1743 | if (path->slots[0] > 0) { |
| 1744 | path->slots[0]--; |
| 1745 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 1746 | if (key.objectid == root->root_key.objectid && |
| 1747 | key.type == BTRFS_ROOT_REF_KEY) |
| 1748 | ret = -ENOTEMPTY; |
| 1749 | } |
| 1750 | out: |
| 1751 | btrfs_free_path(path); |
| 1752 | return ret; |
| 1753 | } |
| 1754 | |
| 1755 | static noinline int key_in_sk(struct btrfs_key *key, |
| 1756 | struct btrfs_ioctl_search_key *sk) |
| 1757 | { |
| 1758 | struct btrfs_key test; |
| 1759 | int ret; |
| 1760 | |
| 1761 | test.objectid = sk->min_objectid; |
| 1762 | test.type = sk->min_type; |
| 1763 | test.offset = sk->min_offset; |
| 1764 | |
| 1765 | ret = btrfs_comp_cpu_keys(key, &test); |
| 1766 | if (ret < 0) |
| 1767 | return 0; |
| 1768 | |
| 1769 | test.objectid = sk->max_objectid; |
| 1770 | test.type = sk->max_type; |
| 1771 | test.offset = sk->max_offset; |
| 1772 | |
| 1773 | ret = btrfs_comp_cpu_keys(key, &test); |
| 1774 | if (ret > 0) |
| 1775 | return 0; |
| 1776 | return 1; |
| 1777 | } |
| 1778 | |
| 1779 | static noinline int copy_to_sk(struct btrfs_root *root, |
| 1780 | struct btrfs_path *path, |
| 1781 | struct btrfs_key *key, |
| 1782 | struct btrfs_ioctl_search_key *sk, |
| 1783 | char *buf, |
| 1784 | unsigned long *sk_offset, |
| 1785 | int *num_found) |
| 1786 | { |
| 1787 | u64 found_transid; |
| 1788 | struct extent_buffer *leaf; |
| 1789 | struct btrfs_ioctl_search_header sh; |
| 1790 | unsigned long item_off; |
| 1791 | unsigned long item_len; |
| 1792 | int nritems; |
| 1793 | int i; |
| 1794 | int slot; |
| 1795 | int ret = 0; |
| 1796 | |
| 1797 | leaf = path->nodes[0]; |
| 1798 | slot = path->slots[0]; |
| 1799 | nritems = btrfs_header_nritems(leaf); |
| 1800 | |
| 1801 | if (btrfs_header_generation(leaf) > sk->max_transid) { |
| 1802 | i = nritems; |
| 1803 | goto advance_key; |
| 1804 | } |
| 1805 | found_transid = btrfs_header_generation(leaf); |
| 1806 | |
| 1807 | for (i = slot; i < nritems; i++) { |
| 1808 | item_off = btrfs_item_ptr_offset(leaf, i); |
| 1809 | item_len = btrfs_item_size_nr(leaf, i); |
| 1810 | |
| 1811 | btrfs_item_key_to_cpu(leaf, key, i); |
| 1812 | if (!key_in_sk(key, sk)) |
| 1813 | continue; |
| 1814 | |
| 1815 | if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE) |
| 1816 | item_len = 0; |
| 1817 | |
| 1818 | if (sizeof(sh) + item_len + *sk_offset > |
| 1819 | BTRFS_SEARCH_ARGS_BUFSIZE) { |
| 1820 | ret = 1; |
| 1821 | goto overflow; |
| 1822 | } |
| 1823 | |
| 1824 | sh.objectid = key->objectid; |
| 1825 | sh.offset = key->offset; |
| 1826 | sh.type = key->type; |
| 1827 | sh.len = item_len; |
| 1828 | sh.transid = found_transid; |
| 1829 | |
| 1830 | /* copy search result header */ |
| 1831 | memcpy(buf + *sk_offset, &sh, sizeof(sh)); |
| 1832 | *sk_offset += sizeof(sh); |
| 1833 | |
| 1834 | if (item_len) { |
| 1835 | char *p = buf + *sk_offset; |
| 1836 | /* copy the item */ |
| 1837 | read_extent_buffer(leaf, p, |
| 1838 | item_off, item_len); |
| 1839 | *sk_offset += item_len; |
| 1840 | } |
| 1841 | (*num_found)++; |
| 1842 | |
| 1843 | if (*num_found >= sk->nr_items) |
| 1844 | break; |
| 1845 | } |
| 1846 | advance_key: |
| 1847 | ret = 0; |
| 1848 | if (key->offset < (u64)-1 && key->offset < sk->max_offset) |
| 1849 | key->offset++; |
| 1850 | else if (key->type < (u8)-1 && key->type < sk->max_type) { |
| 1851 | key->offset = 0; |
| 1852 | key->type++; |
| 1853 | } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { |
| 1854 | key->offset = 0; |
| 1855 | key->type = 0; |
| 1856 | key->objectid++; |
| 1857 | } else |
| 1858 | ret = 1; |
| 1859 | overflow: |
| 1860 | return ret; |
| 1861 | } |
| 1862 | |
| 1863 | static noinline int search_ioctl(struct inode *inode, |
| 1864 | struct btrfs_ioctl_search_args *args) |
| 1865 | { |
| 1866 | struct btrfs_root *root; |
| 1867 | struct btrfs_key key; |
| 1868 | struct btrfs_key max_key; |
| 1869 | struct btrfs_path *path; |
| 1870 | struct btrfs_ioctl_search_key *sk = &args->key; |
| 1871 | struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; |
| 1872 | int ret; |
| 1873 | int num_found = 0; |
| 1874 | unsigned long sk_offset = 0; |
| 1875 | |
| 1876 | path = btrfs_alloc_path(); |
| 1877 | if (!path) |
| 1878 | return -ENOMEM; |
| 1879 | |
| 1880 | if (sk->tree_id == 0) { |
| 1881 | /* search the root of the inode that was passed */ |
| 1882 | root = BTRFS_I(inode)->root; |
| 1883 | } else { |
| 1884 | key.objectid = sk->tree_id; |
| 1885 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 1886 | key.offset = (u64)-1; |
| 1887 | root = btrfs_read_fs_root_no_name(info, &key); |
| 1888 | if (IS_ERR(root)) { |
| 1889 | printk(KERN_ERR "could not find root %llu\n", |
| 1890 | sk->tree_id); |
| 1891 | btrfs_free_path(path); |
| 1892 | return -ENOENT; |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | key.objectid = sk->min_objectid; |
| 1897 | key.type = sk->min_type; |
| 1898 | key.offset = sk->min_offset; |
| 1899 | |
| 1900 | max_key.objectid = sk->max_objectid; |
| 1901 | max_key.type = sk->max_type; |
| 1902 | max_key.offset = sk->max_offset; |
| 1903 | |
| 1904 | path->keep_locks = 1; |
| 1905 | |
| 1906 | while(1) { |
| 1907 | ret = btrfs_search_forward(root, &key, &max_key, path, |
| 1908 | sk->min_transid); |
| 1909 | if (ret != 0) { |
| 1910 | if (ret > 0) |
| 1911 | ret = 0; |
| 1912 | goto err; |
| 1913 | } |
| 1914 | ret = copy_to_sk(root, path, &key, sk, args->buf, |
| 1915 | &sk_offset, &num_found); |
| 1916 | btrfs_release_path(path); |
| 1917 | if (ret || num_found >= sk->nr_items) |
| 1918 | break; |
| 1919 | |
| 1920 | } |
| 1921 | ret = 0; |
| 1922 | err: |
| 1923 | sk->nr_items = num_found; |
| 1924 | btrfs_free_path(path); |
| 1925 | return ret; |
| 1926 | } |
| 1927 | |
| 1928 | static noinline int btrfs_ioctl_tree_search(struct file *file, |
| 1929 | void __user *argp) |
| 1930 | { |
| 1931 | struct btrfs_ioctl_search_args *args; |
| 1932 | struct inode *inode; |
| 1933 | int ret; |
| 1934 | |
| 1935 | if (!capable(CAP_SYS_ADMIN)) |
| 1936 | return -EPERM; |
| 1937 | |
| 1938 | args = memdup_user(argp, sizeof(*args)); |
| 1939 | if (IS_ERR(args)) |
| 1940 | return PTR_ERR(args); |
| 1941 | |
| 1942 | inode = file_inode(file); |
| 1943 | ret = search_ioctl(inode, args); |
| 1944 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 1945 | ret = -EFAULT; |
| 1946 | kfree(args); |
| 1947 | return ret; |
| 1948 | } |
| 1949 | |
| 1950 | /* |
| 1951 | * Search INODE_REFs to identify path name of 'dirid' directory |
| 1952 | * in a 'tree_id' tree. and sets path name to 'name'. |
| 1953 | */ |
| 1954 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
| 1955 | u64 tree_id, u64 dirid, char *name) |
| 1956 | { |
| 1957 | struct btrfs_root *root; |
| 1958 | struct btrfs_key key; |
| 1959 | char *ptr; |
| 1960 | int ret = -1; |
| 1961 | int slot; |
| 1962 | int len; |
| 1963 | int total_len = 0; |
| 1964 | struct btrfs_inode_ref *iref; |
| 1965 | struct extent_buffer *l; |
| 1966 | struct btrfs_path *path; |
| 1967 | |
| 1968 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { |
| 1969 | name[0]='\0'; |
| 1970 | return 0; |
| 1971 | } |
| 1972 | |
| 1973 | path = btrfs_alloc_path(); |
| 1974 | if (!path) |
| 1975 | return -ENOMEM; |
| 1976 | |
| 1977 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; |
| 1978 | |
| 1979 | key.objectid = tree_id; |
| 1980 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 1981 | key.offset = (u64)-1; |
| 1982 | root = btrfs_read_fs_root_no_name(info, &key); |
| 1983 | if (IS_ERR(root)) { |
| 1984 | printk(KERN_ERR "could not find root %llu\n", tree_id); |
| 1985 | ret = -ENOENT; |
| 1986 | goto out; |
| 1987 | } |
| 1988 | |
| 1989 | key.objectid = dirid; |
| 1990 | key.type = BTRFS_INODE_REF_KEY; |
| 1991 | key.offset = (u64)-1; |
| 1992 | |
| 1993 | while(1) { |
| 1994 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1995 | if (ret < 0) |
| 1996 | goto out; |
| 1997 | |
| 1998 | l = path->nodes[0]; |
| 1999 | slot = path->slots[0]; |
| 2000 | if (ret > 0 && slot > 0) |
| 2001 | slot--; |
| 2002 | btrfs_item_key_to_cpu(l, &key, slot); |
| 2003 | |
| 2004 | if (ret > 0 && (key.objectid != dirid || |
| 2005 | key.type != BTRFS_INODE_REF_KEY)) { |
| 2006 | ret = -ENOENT; |
| 2007 | goto out; |
| 2008 | } |
| 2009 | |
| 2010 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); |
| 2011 | len = btrfs_inode_ref_name_len(l, iref); |
| 2012 | ptr -= len + 1; |
| 2013 | total_len += len + 1; |
| 2014 | if (ptr < name) |
| 2015 | goto out; |
| 2016 | |
| 2017 | *(ptr + len) = '/'; |
| 2018 | read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); |
| 2019 | |
| 2020 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) |
| 2021 | break; |
| 2022 | |
| 2023 | btrfs_release_path(path); |
| 2024 | key.objectid = key.offset; |
| 2025 | key.offset = (u64)-1; |
| 2026 | dirid = key.objectid; |
| 2027 | } |
| 2028 | if (ptr < name) |
| 2029 | goto out; |
| 2030 | memmove(name, ptr, total_len); |
| 2031 | name[total_len]='\0'; |
| 2032 | ret = 0; |
| 2033 | out: |
| 2034 | btrfs_free_path(path); |
| 2035 | return ret; |
| 2036 | } |
| 2037 | |
| 2038 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, |
| 2039 | void __user *argp) |
| 2040 | { |
| 2041 | struct btrfs_ioctl_ino_lookup_args *args; |
| 2042 | struct inode *inode; |
| 2043 | int ret; |
| 2044 | |
| 2045 | if (!capable(CAP_SYS_ADMIN)) |
| 2046 | return -EPERM; |
| 2047 | |
| 2048 | args = memdup_user(argp, sizeof(*args)); |
| 2049 | if (IS_ERR(args)) |
| 2050 | return PTR_ERR(args); |
| 2051 | |
| 2052 | inode = file_inode(file); |
| 2053 | |
| 2054 | if (args->treeid == 0) |
| 2055 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; |
| 2056 | |
| 2057 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
| 2058 | args->treeid, args->objectid, |
| 2059 | args->name); |
| 2060 | |
| 2061 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 2062 | ret = -EFAULT; |
| 2063 | |
| 2064 | kfree(args); |
| 2065 | return ret; |
| 2066 | } |
| 2067 | |
| 2068 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
| 2069 | void __user *arg) |
| 2070 | { |
| 2071 | struct dentry *parent = fdentry(file); |
| 2072 | struct dentry *dentry; |
| 2073 | struct inode *dir = parent->d_inode; |
| 2074 | struct inode *inode; |
| 2075 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2076 | struct btrfs_root *dest = NULL; |
| 2077 | struct btrfs_ioctl_vol_args *vol_args; |
| 2078 | struct btrfs_trans_handle *trans; |
| 2079 | struct btrfs_block_rsv block_rsv; |
| 2080 | u64 qgroup_reserved; |
| 2081 | int namelen; |
| 2082 | int ret; |
| 2083 | int err = 0; |
| 2084 | |
| 2085 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 2086 | if (IS_ERR(vol_args)) |
| 2087 | return PTR_ERR(vol_args); |
| 2088 | |
| 2089 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
| 2090 | namelen = strlen(vol_args->name); |
| 2091 | if (strchr(vol_args->name, '/') || |
| 2092 | strncmp(vol_args->name, "..", namelen) == 0) { |
| 2093 | err = -EINVAL; |
| 2094 | goto out; |
| 2095 | } |
| 2096 | |
| 2097 | err = mnt_want_write_file(file); |
| 2098 | if (err) |
| 2099 | goto out; |
| 2100 | |
| 2101 | err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); |
| 2102 | if (err == -EINTR) |
| 2103 | goto out; |
| 2104 | dentry = lookup_one_len(vol_args->name, parent, namelen); |
| 2105 | if (IS_ERR(dentry)) { |
| 2106 | err = PTR_ERR(dentry); |
| 2107 | goto out_unlock_dir; |
| 2108 | } |
| 2109 | |
| 2110 | if (!dentry->d_inode) { |
| 2111 | err = -ENOENT; |
| 2112 | goto out_dput; |
| 2113 | } |
| 2114 | |
| 2115 | inode = dentry->d_inode; |
| 2116 | dest = BTRFS_I(inode)->root; |
| 2117 | if (!capable(CAP_SYS_ADMIN)){ |
| 2118 | /* |
| 2119 | * Regular user. Only allow this with a special mount |
| 2120 | * option, when the user has write+exec access to the |
| 2121 | * subvol root, and when rmdir(2) would have been |
| 2122 | * allowed. |
| 2123 | * |
| 2124 | * Note that this is _not_ check that the subvol is |
| 2125 | * empty or doesn't contain data that we wouldn't |
| 2126 | * otherwise be able to delete. |
| 2127 | * |
| 2128 | * Users who want to delete empty subvols should try |
| 2129 | * rmdir(2). |
| 2130 | */ |
| 2131 | err = -EPERM; |
| 2132 | if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) |
| 2133 | goto out_dput; |
| 2134 | |
| 2135 | /* |
| 2136 | * Do not allow deletion if the parent dir is the same |
| 2137 | * as the dir to be deleted. That means the ioctl |
| 2138 | * must be called on the dentry referencing the root |
| 2139 | * of the subvol, not a random directory contained |
| 2140 | * within it. |
| 2141 | */ |
| 2142 | err = -EINVAL; |
| 2143 | if (root == dest) |
| 2144 | goto out_dput; |
| 2145 | |
| 2146 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); |
| 2147 | if (err) |
| 2148 | goto out_dput; |
| 2149 | } |
| 2150 | |
| 2151 | /* check if subvolume may be deleted by a user */ |
| 2152 | err = btrfs_may_delete(dir, dentry, 1); |
| 2153 | if (err) |
| 2154 | goto out_dput; |
| 2155 | |
| 2156 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { |
| 2157 | err = -EINVAL; |
| 2158 | goto out_dput; |
| 2159 | } |
| 2160 | |
| 2161 | mutex_lock(&inode->i_mutex); |
| 2162 | err = d_invalidate(dentry); |
| 2163 | if (err) |
| 2164 | goto out_unlock; |
| 2165 | |
| 2166 | down_write(&root->fs_info->subvol_sem); |
| 2167 | |
| 2168 | err = may_destroy_subvol(dest); |
| 2169 | if (err) |
| 2170 | goto out_up_write; |
| 2171 | |
| 2172 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
| 2173 | /* |
| 2174 | * One for dir inode, two for dir entries, two for root |
| 2175 | * ref/backref. |
| 2176 | */ |
| 2177 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, |
| 2178 | 5, &qgroup_reserved, true); |
| 2179 | if (err) |
| 2180 | goto out_up_write; |
| 2181 | |
| 2182 | trans = btrfs_start_transaction(root, 0); |
| 2183 | if (IS_ERR(trans)) { |
| 2184 | err = PTR_ERR(trans); |
| 2185 | goto out_release; |
| 2186 | } |
| 2187 | trans->block_rsv = &block_rsv; |
| 2188 | trans->bytes_reserved = block_rsv.size; |
| 2189 | |
| 2190 | ret = btrfs_unlink_subvol(trans, root, dir, |
| 2191 | dest->root_key.objectid, |
| 2192 | dentry->d_name.name, |
| 2193 | dentry->d_name.len); |
| 2194 | if (ret) { |
| 2195 | err = ret; |
| 2196 | btrfs_abort_transaction(trans, root, ret); |
| 2197 | goto out_end_trans; |
| 2198 | } |
| 2199 | |
| 2200 | btrfs_record_root_in_trans(trans, dest); |
| 2201 | |
| 2202 | memset(&dest->root_item.drop_progress, 0, |
| 2203 | sizeof(dest->root_item.drop_progress)); |
| 2204 | dest->root_item.drop_level = 0; |
| 2205 | btrfs_set_root_refs(&dest->root_item, 0); |
| 2206 | |
| 2207 | if (!xchg(&dest->orphan_item_inserted, 1)) { |
| 2208 | ret = btrfs_insert_orphan_item(trans, |
| 2209 | root->fs_info->tree_root, |
| 2210 | dest->root_key.objectid); |
| 2211 | if (ret) { |
| 2212 | btrfs_abort_transaction(trans, root, ret); |
| 2213 | err = ret; |
| 2214 | goto out_end_trans; |
| 2215 | } |
| 2216 | } |
| 2217 | out_end_trans: |
| 2218 | trans->block_rsv = NULL; |
| 2219 | trans->bytes_reserved = 0; |
| 2220 | ret = btrfs_end_transaction(trans, root); |
| 2221 | if (ret && !err) |
| 2222 | err = ret; |
| 2223 | inode->i_flags |= S_DEAD; |
| 2224 | out_release: |
| 2225 | btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); |
| 2226 | out_up_write: |
| 2227 | up_write(&root->fs_info->subvol_sem); |
| 2228 | out_unlock: |
| 2229 | mutex_unlock(&inode->i_mutex); |
| 2230 | if (!err) { |
| 2231 | shrink_dcache_sb(root->fs_info->sb); |
| 2232 | btrfs_invalidate_inodes(dest); |
| 2233 | d_delete(dentry); |
| 2234 | |
| 2235 | /* the last ref */ |
| 2236 | if (dest->cache_inode) { |
| 2237 | iput(dest->cache_inode); |
| 2238 | dest->cache_inode = NULL; |
| 2239 | } |
| 2240 | } |
| 2241 | out_dput: |
| 2242 | dput(dentry); |
| 2243 | out_unlock_dir: |
| 2244 | mutex_unlock(&dir->i_mutex); |
| 2245 | mnt_drop_write_file(file); |
| 2246 | out: |
| 2247 | kfree(vol_args); |
| 2248 | return err; |
| 2249 | } |
| 2250 | |
| 2251 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
| 2252 | { |
| 2253 | struct inode *inode = file_inode(file); |
| 2254 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2255 | struct btrfs_ioctl_defrag_range_args *range; |
| 2256 | int ret; |
| 2257 | |
| 2258 | ret = mnt_want_write_file(file); |
| 2259 | if (ret) |
| 2260 | return ret; |
| 2261 | |
| 2262 | if (btrfs_root_readonly(root)) { |
| 2263 | ret = -EROFS; |
| 2264 | goto out; |
| 2265 | } |
| 2266 | |
| 2267 | switch (inode->i_mode & S_IFMT) { |
| 2268 | case S_IFDIR: |
| 2269 | if (!capable(CAP_SYS_ADMIN)) { |
| 2270 | ret = -EPERM; |
| 2271 | goto out; |
| 2272 | } |
| 2273 | ret = btrfs_defrag_root(root); |
| 2274 | if (ret) |
| 2275 | goto out; |
| 2276 | ret = btrfs_defrag_root(root->fs_info->extent_root); |
| 2277 | break; |
| 2278 | case S_IFREG: |
| 2279 | if (!(file->f_mode & FMODE_WRITE)) { |
| 2280 | ret = -EINVAL; |
| 2281 | goto out; |
| 2282 | } |
| 2283 | |
| 2284 | range = kzalloc(sizeof(*range), GFP_KERNEL); |
| 2285 | if (!range) { |
| 2286 | ret = -ENOMEM; |
| 2287 | goto out; |
| 2288 | } |
| 2289 | |
| 2290 | if (argp) { |
| 2291 | if (copy_from_user(range, argp, |
| 2292 | sizeof(*range))) { |
| 2293 | ret = -EFAULT; |
| 2294 | kfree(range); |
| 2295 | goto out; |
| 2296 | } |
| 2297 | /* compression requires us to start the IO */ |
| 2298 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { |
| 2299 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; |
| 2300 | range->extent_thresh = (u32)-1; |
| 2301 | } |
| 2302 | } else { |
| 2303 | /* the rest are all set to zero by kzalloc */ |
| 2304 | range->len = (u64)-1; |
| 2305 | } |
| 2306 | ret = btrfs_defrag_file(file_inode(file), file, |
| 2307 | range, 0, 0); |
| 2308 | if (ret > 0) |
| 2309 | ret = 0; |
| 2310 | kfree(range); |
| 2311 | break; |
| 2312 | default: |
| 2313 | ret = -EINVAL; |
| 2314 | } |
| 2315 | out: |
| 2316 | mnt_drop_write_file(file); |
| 2317 | return ret; |
| 2318 | } |
| 2319 | |
| 2320 | static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) |
| 2321 | { |
| 2322 | struct btrfs_ioctl_vol_args *vol_args; |
| 2323 | int ret; |
| 2324 | |
| 2325 | if (!capable(CAP_SYS_ADMIN)) |
| 2326 | return -EPERM; |
| 2327 | |
| 2328 | if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, |
| 2329 | 1)) { |
| 2330 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); |
| 2331 | return -EINVAL; |
| 2332 | } |
| 2333 | |
| 2334 | mutex_lock(&root->fs_info->volume_mutex); |
| 2335 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 2336 | if (IS_ERR(vol_args)) { |
| 2337 | ret = PTR_ERR(vol_args); |
| 2338 | goto out; |
| 2339 | } |
| 2340 | |
| 2341 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
| 2342 | ret = btrfs_init_new_device(root, vol_args->name); |
| 2343 | |
| 2344 | kfree(vol_args); |
| 2345 | out: |
| 2346 | mutex_unlock(&root->fs_info->volume_mutex); |
| 2347 | atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); |
| 2348 | return ret; |
| 2349 | } |
| 2350 | |
| 2351 | static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) |
| 2352 | { |
| 2353 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 2354 | struct btrfs_ioctl_vol_args *vol_args; |
| 2355 | int ret; |
| 2356 | |
| 2357 | if (!capable(CAP_SYS_ADMIN)) |
| 2358 | return -EPERM; |
| 2359 | |
| 2360 | ret = mnt_want_write_file(file); |
| 2361 | if (ret) |
| 2362 | return ret; |
| 2363 | |
| 2364 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 2365 | if (IS_ERR(vol_args)) { |
| 2366 | ret = PTR_ERR(vol_args); |
| 2367 | goto out; |
| 2368 | } |
| 2369 | |
| 2370 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
| 2371 | |
| 2372 | if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, |
| 2373 | 1)) { |
| 2374 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
| 2375 | goto out; |
| 2376 | } |
| 2377 | |
| 2378 | mutex_lock(&root->fs_info->volume_mutex); |
| 2379 | ret = btrfs_rm_device(root, vol_args->name); |
| 2380 | mutex_unlock(&root->fs_info->volume_mutex); |
| 2381 | atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); |
| 2382 | |
| 2383 | out: |
| 2384 | kfree(vol_args); |
| 2385 | mnt_drop_write_file(file); |
| 2386 | return ret; |
| 2387 | } |
| 2388 | |
| 2389 | static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) |
| 2390 | { |
| 2391 | struct btrfs_ioctl_fs_info_args *fi_args; |
| 2392 | struct btrfs_device *device; |
| 2393 | struct btrfs_device *next; |
| 2394 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 2395 | int ret = 0; |
| 2396 | |
| 2397 | if (!capable(CAP_SYS_ADMIN)) |
| 2398 | return -EPERM; |
| 2399 | |
| 2400 | fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); |
| 2401 | if (!fi_args) |
| 2402 | return -ENOMEM; |
| 2403 | |
| 2404 | fi_args->num_devices = fs_devices->num_devices; |
| 2405 | memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); |
| 2406 | |
| 2407 | mutex_lock(&fs_devices->device_list_mutex); |
| 2408 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
| 2409 | if (device->devid > fi_args->max_id) |
| 2410 | fi_args->max_id = device->devid; |
| 2411 | } |
| 2412 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2413 | |
| 2414 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) |
| 2415 | ret = -EFAULT; |
| 2416 | |
| 2417 | kfree(fi_args); |
| 2418 | return ret; |
| 2419 | } |
| 2420 | |
| 2421 | static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) |
| 2422 | { |
| 2423 | struct btrfs_ioctl_dev_info_args *di_args; |
| 2424 | struct btrfs_device *dev; |
| 2425 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 2426 | int ret = 0; |
| 2427 | char *s_uuid = NULL; |
| 2428 | char empty_uuid[BTRFS_UUID_SIZE] = {0}; |
| 2429 | |
| 2430 | if (!capable(CAP_SYS_ADMIN)) |
| 2431 | return -EPERM; |
| 2432 | |
| 2433 | di_args = memdup_user(arg, sizeof(*di_args)); |
| 2434 | if (IS_ERR(di_args)) |
| 2435 | return PTR_ERR(di_args); |
| 2436 | |
| 2437 | if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0) |
| 2438 | s_uuid = di_args->uuid; |
| 2439 | |
| 2440 | mutex_lock(&fs_devices->device_list_mutex); |
| 2441 | dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); |
| 2442 | |
| 2443 | if (!dev) { |
| 2444 | ret = -ENODEV; |
| 2445 | goto out; |
| 2446 | } |
| 2447 | |
| 2448 | di_args->devid = dev->devid; |
| 2449 | di_args->bytes_used = dev->bytes_used; |
| 2450 | di_args->total_bytes = dev->total_bytes; |
| 2451 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); |
| 2452 | if (dev->name) { |
| 2453 | struct rcu_string *name; |
| 2454 | |
| 2455 | rcu_read_lock(); |
| 2456 | name = rcu_dereference(dev->name); |
| 2457 | strncpy(di_args->path, name->str, sizeof(di_args->path)); |
| 2458 | rcu_read_unlock(); |
| 2459 | di_args->path[sizeof(di_args->path) - 1] = 0; |
| 2460 | } else { |
| 2461 | di_args->path[0] = '\0'; |
| 2462 | } |
| 2463 | |
| 2464 | out: |
| 2465 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2466 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) |
| 2467 | ret = -EFAULT; |
| 2468 | |
| 2469 | kfree(di_args); |
| 2470 | return ret; |
| 2471 | } |
| 2472 | |
| 2473 | static inline void lock_extent_range(struct inode *inode, u64 off, u64 len) |
| 2474 | { |
| 2475 | /* do any pending delalloc/csum calc on src, one way or |
| 2476 | another, and lock file content */ |
| 2477 | while (1) { |
| 2478 | struct btrfs_ordered_extent *ordered; |
| 2479 | lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); |
| 2480 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 2481 | off + len - 1); |
| 2482 | if (!ordered && |
| 2483 | !test_range_bit(&BTRFS_I(inode)->io_tree, off, |
| 2484 | off + len - 1, EXTENT_DELALLOC, 0, NULL)) |
| 2485 | break; |
| 2486 | unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); |
| 2487 | if (ordered) |
| 2488 | btrfs_put_ordered_extent(ordered); |
| 2489 | btrfs_wait_ordered_range(inode, off, len); |
| 2490 | } |
| 2491 | } |
| 2492 | |
| 2493 | /** |
| 2494 | * btrfs_clone() - clone a range from inode file to another |
| 2495 | * |
| 2496 | * @src: Inode to clone from |
| 2497 | * @inode: Inode to clone to |
| 2498 | * @off: Offset within source to start clone from |
| 2499 | * @olen: Original length, passed by user, of range to clone |
| 2500 | * @olen_aligned: Block-aligned value of olen, extent_same uses |
| 2501 | * identical values here |
| 2502 | * @destoff: Offset within @inode to start clone |
| 2503 | */ |
| 2504 | static int btrfs_clone(struct inode *src, struct inode *inode, |
| 2505 | u64 off, u64 olen, u64 olen_aligned, u64 destoff) |
| 2506 | { |
| 2507 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2508 | struct btrfs_path *path = NULL; |
| 2509 | struct extent_buffer *leaf; |
| 2510 | struct btrfs_trans_handle *trans; |
| 2511 | char *buf = NULL; |
| 2512 | struct btrfs_key key; |
| 2513 | u32 nritems; |
| 2514 | int slot; |
| 2515 | int ret; |
| 2516 | u64 len = olen_aligned; |
| 2517 | |
| 2518 | ret = -ENOMEM; |
| 2519 | buf = vmalloc(btrfs_level_size(root, 0)); |
| 2520 | if (!buf) |
| 2521 | return ret; |
| 2522 | |
| 2523 | path = btrfs_alloc_path(); |
| 2524 | if (!path) { |
| 2525 | vfree(buf); |
| 2526 | return ret; |
| 2527 | } |
| 2528 | |
| 2529 | path->reada = 2; |
| 2530 | /* clone data */ |
| 2531 | key.objectid = btrfs_ino(src); |
| 2532 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 2533 | key.offset = 0; |
| 2534 | |
| 2535 | while (1) { |
| 2536 | /* |
| 2537 | * note the key will change type as we walk through the |
| 2538 | * tree. |
| 2539 | */ |
| 2540 | ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, |
| 2541 | 0, 0); |
| 2542 | if (ret < 0) |
| 2543 | goto out; |
| 2544 | |
| 2545 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2546 | if (path->slots[0] >= nritems) { |
| 2547 | ret = btrfs_next_leaf(BTRFS_I(src)->root, path); |
| 2548 | if (ret < 0) |
| 2549 | goto out; |
| 2550 | if (ret > 0) |
| 2551 | break; |
| 2552 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 2553 | } |
| 2554 | leaf = path->nodes[0]; |
| 2555 | slot = path->slots[0]; |
| 2556 | |
| 2557 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2558 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
| 2559 | key.objectid != btrfs_ino(src)) |
| 2560 | break; |
| 2561 | |
| 2562 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
| 2563 | struct btrfs_file_extent_item *extent; |
| 2564 | int type; |
| 2565 | u32 size; |
| 2566 | struct btrfs_key new_key; |
| 2567 | u64 disko = 0, diskl = 0; |
| 2568 | u64 datao = 0, datal = 0; |
| 2569 | u8 comp; |
| 2570 | u64 endoff; |
| 2571 | |
| 2572 | size = btrfs_item_size_nr(leaf, slot); |
| 2573 | read_extent_buffer(leaf, buf, |
| 2574 | btrfs_item_ptr_offset(leaf, slot), |
| 2575 | size); |
| 2576 | |
| 2577 | extent = btrfs_item_ptr(leaf, slot, |
| 2578 | struct btrfs_file_extent_item); |
| 2579 | comp = btrfs_file_extent_compression(leaf, extent); |
| 2580 | type = btrfs_file_extent_type(leaf, extent); |
| 2581 | if (type == BTRFS_FILE_EXTENT_REG || |
| 2582 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 2583 | disko = btrfs_file_extent_disk_bytenr(leaf, |
| 2584 | extent); |
| 2585 | diskl = btrfs_file_extent_disk_num_bytes(leaf, |
| 2586 | extent); |
| 2587 | datao = btrfs_file_extent_offset(leaf, extent); |
| 2588 | datal = btrfs_file_extent_num_bytes(leaf, |
| 2589 | extent); |
| 2590 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 2591 | /* take upper bound, may be compressed */ |
| 2592 | datal = btrfs_file_extent_ram_bytes(leaf, |
| 2593 | extent); |
| 2594 | } |
| 2595 | btrfs_release_path(path); |
| 2596 | |
| 2597 | if (key.offset + datal <= off || |
| 2598 | key.offset >= off + len - 1) |
| 2599 | goto next; |
| 2600 | |
| 2601 | memcpy(&new_key, &key, sizeof(new_key)); |
| 2602 | new_key.objectid = btrfs_ino(inode); |
| 2603 | if (off <= key.offset) |
| 2604 | new_key.offset = key.offset + destoff - off; |
| 2605 | else |
| 2606 | new_key.offset = destoff; |
| 2607 | |
| 2608 | /* |
| 2609 | * 1 - adjusting old extent (we may have to split it) |
| 2610 | * 1 - add new extent |
| 2611 | * 1 - inode update |
| 2612 | */ |
| 2613 | trans = btrfs_start_transaction(root, 3); |
| 2614 | if (IS_ERR(trans)) { |
| 2615 | ret = PTR_ERR(trans); |
| 2616 | goto out; |
| 2617 | } |
| 2618 | |
| 2619 | if (type == BTRFS_FILE_EXTENT_REG || |
| 2620 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 2621 | /* |
| 2622 | * a | --- range to clone ---| b |
| 2623 | * | ------------- extent ------------- | |
| 2624 | */ |
| 2625 | |
| 2626 | /* substract range b */ |
| 2627 | if (key.offset + datal > off + len) |
| 2628 | datal = off + len - key.offset; |
| 2629 | |
| 2630 | /* substract range a */ |
| 2631 | if (off > key.offset) { |
| 2632 | datao += off - key.offset; |
| 2633 | datal -= off - key.offset; |
| 2634 | } |
| 2635 | |
| 2636 | ret = btrfs_drop_extents(trans, root, inode, |
| 2637 | new_key.offset, |
| 2638 | new_key.offset + datal, |
| 2639 | 1); |
| 2640 | if (ret) { |
| 2641 | btrfs_abort_transaction(trans, root, |
| 2642 | ret); |
| 2643 | btrfs_end_transaction(trans, root); |
| 2644 | goto out; |
| 2645 | } |
| 2646 | |
| 2647 | ret = btrfs_insert_empty_item(trans, root, path, |
| 2648 | &new_key, size); |
| 2649 | if (ret) { |
| 2650 | btrfs_abort_transaction(trans, root, |
| 2651 | ret); |
| 2652 | btrfs_end_transaction(trans, root); |
| 2653 | goto out; |
| 2654 | } |
| 2655 | |
| 2656 | leaf = path->nodes[0]; |
| 2657 | slot = path->slots[0]; |
| 2658 | write_extent_buffer(leaf, buf, |
| 2659 | btrfs_item_ptr_offset(leaf, slot), |
| 2660 | size); |
| 2661 | |
| 2662 | extent = btrfs_item_ptr(leaf, slot, |
| 2663 | struct btrfs_file_extent_item); |
| 2664 | |
| 2665 | /* disko == 0 means it's a hole */ |
| 2666 | if (!disko) |
| 2667 | datao = 0; |
| 2668 | |
| 2669 | btrfs_set_file_extent_offset(leaf, extent, |
| 2670 | datao); |
| 2671 | btrfs_set_file_extent_num_bytes(leaf, extent, |
| 2672 | datal); |
| 2673 | if (disko) { |
| 2674 | inode_add_bytes(inode, datal); |
| 2675 | ret = btrfs_inc_extent_ref(trans, root, |
| 2676 | disko, diskl, 0, |
| 2677 | root->root_key.objectid, |
| 2678 | btrfs_ino(inode), |
| 2679 | new_key.offset - datao, |
| 2680 | 0); |
| 2681 | if (ret) { |
| 2682 | btrfs_abort_transaction(trans, |
| 2683 | root, |
| 2684 | ret); |
| 2685 | btrfs_end_transaction(trans, |
| 2686 | root); |
| 2687 | goto out; |
| 2688 | |
| 2689 | } |
| 2690 | } |
| 2691 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 2692 | u64 skip = 0; |
| 2693 | u64 trim = 0; |
| 2694 | if (off > key.offset) { |
| 2695 | skip = off - key.offset; |
| 2696 | new_key.offset += skip; |
| 2697 | } |
| 2698 | |
| 2699 | if (key.offset + datal > off + len) |
| 2700 | trim = key.offset + datal - (off + len); |
| 2701 | |
| 2702 | if (comp && (skip || trim)) { |
| 2703 | ret = -EINVAL; |
| 2704 | btrfs_end_transaction(trans, root); |
| 2705 | goto out; |
| 2706 | } |
| 2707 | size -= skip + trim; |
| 2708 | datal -= skip + trim; |
| 2709 | |
| 2710 | ret = btrfs_drop_extents(trans, root, inode, |
| 2711 | new_key.offset, |
| 2712 | new_key.offset + datal, |
| 2713 | 1); |
| 2714 | if (ret) { |
| 2715 | btrfs_abort_transaction(trans, root, |
| 2716 | ret); |
| 2717 | btrfs_end_transaction(trans, root); |
| 2718 | goto out; |
| 2719 | } |
| 2720 | |
| 2721 | ret = btrfs_insert_empty_item(trans, root, path, |
| 2722 | &new_key, size); |
| 2723 | if (ret) { |
| 2724 | btrfs_abort_transaction(trans, root, |
| 2725 | ret); |
| 2726 | btrfs_end_transaction(trans, root); |
| 2727 | goto out; |
| 2728 | } |
| 2729 | |
| 2730 | if (skip) { |
| 2731 | u32 start = |
| 2732 | btrfs_file_extent_calc_inline_size(0); |
| 2733 | memmove(buf+start, buf+start+skip, |
| 2734 | datal); |
| 2735 | } |
| 2736 | |
| 2737 | leaf = path->nodes[0]; |
| 2738 | slot = path->slots[0]; |
| 2739 | write_extent_buffer(leaf, buf, |
| 2740 | btrfs_item_ptr_offset(leaf, slot), |
| 2741 | size); |
| 2742 | inode_add_bytes(inode, datal); |
| 2743 | } |
| 2744 | |
| 2745 | btrfs_mark_buffer_dirty(leaf); |
| 2746 | btrfs_release_path(path); |
| 2747 | |
| 2748 | inode_inc_iversion(inode); |
| 2749 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 2750 | |
| 2751 | /* |
| 2752 | * we round up to the block size at eof when |
| 2753 | * determining which extents to clone above, |
| 2754 | * but shouldn't round up the file size |
| 2755 | */ |
| 2756 | endoff = new_key.offset + datal; |
| 2757 | if (endoff > destoff+olen) |
| 2758 | endoff = destoff+olen; |
| 2759 | if (endoff > inode->i_size) |
| 2760 | btrfs_i_size_write(inode, endoff); |
| 2761 | |
| 2762 | ret = btrfs_update_inode(trans, root, inode); |
| 2763 | if (ret) { |
| 2764 | btrfs_abort_transaction(trans, root, ret); |
| 2765 | btrfs_end_transaction(trans, root); |
| 2766 | goto out; |
| 2767 | } |
| 2768 | ret = btrfs_end_transaction(trans, root); |
| 2769 | } |
| 2770 | next: |
| 2771 | btrfs_release_path(path); |
| 2772 | key.offset++; |
| 2773 | } |
| 2774 | ret = 0; |
| 2775 | |
| 2776 | out: |
| 2777 | btrfs_release_path(path); |
| 2778 | btrfs_free_path(path); |
| 2779 | vfree(buf); |
| 2780 | return ret; |
| 2781 | } |
| 2782 | |
| 2783 | static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, |
| 2784 | u64 off, u64 olen, u64 destoff) |
| 2785 | { |
| 2786 | struct inode *inode = fdentry(file)->d_inode; |
| 2787 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2788 | struct fd src_file; |
| 2789 | struct inode *src; |
| 2790 | int ret; |
| 2791 | u64 len = olen; |
| 2792 | u64 bs = root->fs_info->sb->s_blocksize; |
| 2793 | int same_inode = 0; |
| 2794 | |
| 2795 | /* |
| 2796 | * TODO: |
| 2797 | * - split compressed inline extents. annoying: we need to |
| 2798 | * decompress into destination's address_space (the file offset |
| 2799 | * may change, so source mapping won't do), then recompress (or |
| 2800 | * otherwise reinsert) a subrange. |
| 2801 | * - allow ranges within the same file to be cloned (provided |
| 2802 | * they don't overlap)? |
| 2803 | */ |
| 2804 | |
| 2805 | /* the destination must be opened for writing */ |
| 2806 | if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) |
| 2807 | return -EINVAL; |
| 2808 | |
| 2809 | if (btrfs_root_readonly(root)) |
| 2810 | return -EROFS; |
| 2811 | |
| 2812 | ret = mnt_want_write_file(file); |
| 2813 | if (ret) |
| 2814 | return ret; |
| 2815 | |
| 2816 | src_file = fdget(srcfd); |
| 2817 | if (!src_file.file) { |
| 2818 | ret = -EBADF; |
| 2819 | goto out_drop_write; |
| 2820 | } |
| 2821 | |
| 2822 | ret = -EXDEV; |
| 2823 | if (src_file.file->f_path.mnt != file->f_path.mnt) |
| 2824 | goto out_fput; |
| 2825 | |
| 2826 | src = file_inode(src_file.file); |
| 2827 | |
| 2828 | ret = -EINVAL; |
| 2829 | if (src == inode) |
| 2830 | same_inode = 1; |
| 2831 | |
| 2832 | /* the src must be open for reading */ |
| 2833 | if (!(src_file.file->f_mode & FMODE_READ)) |
| 2834 | goto out_fput; |
| 2835 | |
| 2836 | /* don't make the dst file partly checksummed */ |
| 2837 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != |
| 2838 | (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) |
| 2839 | goto out_fput; |
| 2840 | |
| 2841 | ret = -EISDIR; |
| 2842 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
| 2843 | goto out_fput; |
| 2844 | |
| 2845 | ret = -EXDEV; |
| 2846 | if (src->i_sb != inode->i_sb) |
| 2847 | goto out_fput; |
| 2848 | |
| 2849 | if (!same_inode) { |
| 2850 | if (inode < src) { |
| 2851 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); |
| 2852 | mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); |
| 2853 | } else { |
| 2854 | mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); |
| 2855 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); |
| 2856 | } |
| 2857 | } else { |
| 2858 | mutex_lock(&src->i_mutex); |
| 2859 | } |
| 2860 | |
| 2861 | /* determine range to clone */ |
| 2862 | ret = -EINVAL; |
| 2863 | if (off + len > src->i_size || off + len < off) |
| 2864 | goto out_unlock; |
| 2865 | if (len == 0) |
| 2866 | olen = len = src->i_size - off; |
| 2867 | /* if we extend to eof, continue to block boundary */ |
| 2868 | if (off + len == src->i_size) |
| 2869 | len = ALIGN(src->i_size, bs) - off; |
| 2870 | |
| 2871 | /* verify the end result is block aligned */ |
| 2872 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || |
| 2873 | !IS_ALIGNED(destoff, bs)) |
| 2874 | goto out_unlock; |
| 2875 | |
| 2876 | /* verify if ranges are overlapped within the same file */ |
| 2877 | if (same_inode) { |
| 2878 | if (destoff + len > off && destoff < off + len) |
| 2879 | goto out_unlock; |
| 2880 | } |
| 2881 | |
| 2882 | if (destoff > inode->i_size) { |
| 2883 | ret = btrfs_cont_expand(inode, inode->i_size, destoff); |
| 2884 | if (ret) |
| 2885 | goto out_unlock; |
| 2886 | } |
| 2887 | |
| 2888 | /* truncate page cache pages from target inode range */ |
| 2889 | truncate_inode_pages_range(&inode->i_data, destoff, |
| 2890 | PAGE_CACHE_ALIGN(destoff + len) - 1); |
| 2891 | |
| 2892 | lock_extent_range(src, off, len); |
| 2893 | |
| 2894 | ret = btrfs_clone(src, inode, off, olen, len, destoff); |
| 2895 | |
| 2896 | unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); |
| 2897 | out_unlock: |
| 2898 | mutex_unlock(&src->i_mutex); |
| 2899 | if (!same_inode) |
| 2900 | mutex_unlock(&inode->i_mutex); |
| 2901 | out_fput: |
| 2902 | fdput(src_file); |
| 2903 | out_drop_write: |
| 2904 | mnt_drop_write_file(file); |
| 2905 | return ret; |
| 2906 | } |
| 2907 | |
| 2908 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
| 2909 | { |
| 2910 | struct btrfs_ioctl_clone_range_args args; |
| 2911 | |
| 2912 | if (copy_from_user(&args, argp, sizeof(args))) |
| 2913 | return -EFAULT; |
| 2914 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, |
| 2915 | args.src_length, args.dest_offset); |
| 2916 | } |
| 2917 | |
| 2918 | /* |
| 2919 | * there are many ways the trans_start and trans_end ioctls can lead |
| 2920 | * to deadlocks. They should only be used by applications that |
| 2921 | * basically own the machine, and have a very in depth understanding |
| 2922 | * of all the possible deadlocks and enospc problems. |
| 2923 | */ |
| 2924 | static long btrfs_ioctl_trans_start(struct file *file) |
| 2925 | { |
| 2926 | struct inode *inode = file_inode(file); |
| 2927 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2928 | struct btrfs_trans_handle *trans; |
| 2929 | int ret; |
| 2930 | |
| 2931 | ret = -EPERM; |
| 2932 | if (!capable(CAP_SYS_ADMIN)) |
| 2933 | goto out; |
| 2934 | |
| 2935 | ret = -EINPROGRESS; |
| 2936 | if (file->private_data) |
| 2937 | goto out; |
| 2938 | |
| 2939 | ret = -EROFS; |
| 2940 | if (btrfs_root_readonly(root)) |
| 2941 | goto out; |
| 2942 | |
| 2943 | ret = mnt_want_write_file(file); |
| 2944 | if (ret) |
| 2945 | goto out; |
| 2946 | |
| 2947 | atomic_inc(&root->fs_info->open_ioctl_trans); |
| 2948 | |
| 2949 | ret = -ENOMEM; |
| 2950 | trans = btrfs_start_ioctl_transaction(root); |
| 2951 | if (IS_ERR(trans)) |
| 2952 | goto out_drop; |
| 2953 | |
| 2954 | file->private_data = trans; |
| 2955 | return 0; |
| 2956 | |
| 2957 | out_drop: |
| 2958 | atomic_dec(&root->fs_info->open_ioctl_trans); |
| 2959 | mnt_drop_write_file(file); |
| 2960 | out: |
| 2961 | return ret; |
| 2962 | } |
| 2963 | |
| 2964 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
| 2965 | { |
| 2966 | struct inode *inode = file_inode(file); |
| 2967 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2968 | struct btrfs_root *new_root; |
| 2969 | struct btrfs_dir_item *di; |
| 2970 | struct btrfs_trans_handle *trans; |
| 2971 | struct btrfs_path *path; |
| 2972 | struct btrfs_key location; |
| 2973 | struct btrfs_disk_key disk_key; |
| 2974 | u64 objectid = 0; |
| 2975 | u64 dir_id; |
| 2976 | int ret; |
| 2977 | |
| 2978 | if (!capable(CAP_SYS_ADMIN)) |
| 2979 | return -EPERM; |
| 2980 | |
| 2981 | ret = mnt_want_write_file(file); |
| 2982 | if (ret) |
| 2983 | return ret; |
| 2984 | |
| 2985 | if (copy_from_user(&objectid, argp, sizeof(objectid))) { |
| 2986 | ret = -EFAULT; |
| 2987 | goto out; |
| 2988 | } |
| 2989 | |
| 2990 | if (!objectid) |
| 2991 | objectid = root->root_key.objectid; |
| 2992 | |
| 2993 | location.objectid = objectid; |
| 2994 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 2995 | location.offset = (u64)-1; |
| 2996 | |
| 2997 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 2998 | if (IS_ERR(new_root)) { |
| 2999 | ret = PTR_ERR(new_root); |
| 3000 | goto out; |
| 3001 | } |
| 3002 | |
| 3003 | path = btrfs_alloc_path(); |
| 3004 | if (!path) { |
| 3005 | ret = -ENOMEM; |
| 3006 | goto out; |
| 3007 | } |
| 3008 | path->leave_spinning = 1; |
| 3009 | |
| 3010 | trans = btrfs_start_transaction(root, 1); |
| 3011 | if (IS_ERR(trans)) { |
| 3012 | btrfs_free_path(path); |
| 3013 | ret = PTR_ERR(trans); |
| 3014 | goto out; |
| 3015 | } |
| 3016 | |
| 3017 | dir_id = btrfs_super_root_dir(root->fs_info->super_copy); |
| 3018 | di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, |
| 3019 | dir_id, "default", 7, 1); |
| 3020 | if (IS_ERR_OR_NULL(di)) { |
| 3021 | btrfs_free_path(path); |
| 3022 | btrfs_end_transaction(trans, root); |
| 3023 | printk(KERN_ERR "Umm, you don't have the default dir item, " |
| 3024 | "this isn't going to work\n"); |
| 3025 | ret = -ENOENT; |
| 3026 | goto out; |
| 3027 | } |
| 3028 | |
| 3029 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); |
| 3030 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); |
| 3031 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3032 | btrfs_free_path(path); |
| 3033 | |
| 3034 | btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); |
| 3035 | btrfs_end_transaction(trans, root); |
| 3036 | out: |
| 3037 | mnt_drop_write_file(file); |
| 3038 | return ret; |
| 3039 | } |
| 3040 | |
| 3041 | void btrfs_get_block_group_info(struct list_head *groups_list, |
| 3042 | struct btrfs_ioctl_space_info *space) |
| 3043 | { |
| 3044 | struct btrfs_block_group_cache *block_group; |
| 3045 | |
| 3046 | space->total_bytes = 0; |
| 3047 | space->used_bytes = 0; |
| 3048 | space->flags = 0; |
| 3049 | list_for_each_entry(block_group, groups_list, list) { |
| 3050 | space->flags = block_group->flags; |
| 3051 | space->total_bytes += block_group->key.offset; |
| 3052 | space->used_bytes += |
| 3053 | btrfs_block_group_used(&block_group->item); |
| 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) |
| 3058 | { |
| 3059 | struct btrfs_ioctl_space_args space_args; |
| 3060 | struct btrfs_ioctl_space_info space; |
| 3061 | struct btrfs_ioctl_space_info *dest; |
| 3062 | struct btrfs_ioctl_space_info *dest_orig; |
| 3063 | struct btrfs_ioctl_space_info __user *user_dest; |
| 3064 | struct btrfs_space_info *info; |
| 3065 | u64 types[] = {BTRFS_BLOCK_GROUP_DATA, |
| 3066 | BTRFS_BLOCK_GROUP_SYSTEM, |
| 3067 | BTRFS_BLOCK_GROUP_METADATA, |
| 3068 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; |
| 3069 | int num_types = 4; |
| 3070 | int alloc_size; |
| 3071 | int ret = 0; |
| 3072 | u64 slot_count = 0; |
| 3073 | int i, c; |
| 3074 | |
| 3075 | if (copy_from_user(&space_args, |
| 3076 | (struct btrfs_ioctl_space_args __user *)arg, |
| 3077 | sizeof(space_args))) |
| 3078 | return -EFAULT; |
| 3079 | |
| 3080 | for (i = 0; i < num_types; i++) { |
| 3081 | struct btrfs_space_info *tmp; |
| 3082 | |
| 3083 | info = NULL; |
| 3084 | rcu_read_lock(); |
| 3085 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, |
| 3086 | list) { |
| 3087 | if (tmp->flags == types[i]) { |
| 3088 | info = tmp; |
| 3089 | break; |
| 3090 | } |
| 3091 | } |
| 3092 | rcu_read_unlock(); |
| 3093 | |
| 3094 | if (!info) |
| 3095 | continue; |
| 3096 | |
| 3097 | down_read(&info->groups_sem); |
| 3098 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 3099 | if (!list_empty(&info->block_groups[c])) |
| 3100 | slot_count++; |
| 3101 | } |
| 3102 | up_read(&info->groups_sem); |
| 3103 | } |
| 3104 | |
| 3105 | /* space_slots == 0 means they are asking for a count */ |
| 3106 | if (space_args.space_slots == 0) { |
| 3107 | space_args.total_spaces = slot_count; |
| 3108 | goto out; |
| 3109 | } |
| 3110 | |
| 3111 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
| 3112 | |
| 3113 | alloc_size = sizeof(*dest) * slot_count; |
| 3114 | |
| 3115 | /* we generally have at most 6 or so space infos, one for each raid |
| 3116 | * level. So, a whole page should be more than enough for everyone |
| 3117 | */ |
| 3118 | if (alloc_size > PAGE_CACHE_SIZE) |
| 3119 | return -ENOMEM; |
| 3120 | |
| 3121 | space_args.total_spaces = 0; |
| 3122 | dest = kmalloc(alloc_size, GFP_NOFS); |
| 3123 | if (!dest) |
| 3124 | return -ENOMEM; |
| 3125 | dest_orig = dest; |
| 3126 | |
| 3127 | /* now we have a buffer to copy into */ |
| 3128 | for (i = 0; i < num_types; i++) { |
| 3129 | struct btrfs_space_info *tmp; |
| 3130 | |
| 3131 | if (!slot_count) |
| 3132 | break; |
| 3133 | |
| 3134 | info = NULL; |
| 3135 | rcu_read_lock(); |
| 3136 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, |
| 3137 | list) { |
| 3138 | if (tmp->flags == types[i]) { |
| 3139 | info = tmp; |
| 3140 | break; |
| 3141 | } |
| 3142 | } |
| 3143 | rcu_read_unlock(); |
| 3144 | |
| 3145 | if (!info) |
| 3146 | continue; |
| 3147 | down_read(&info->groups_sem); |
| 3148 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 3149 | if (!list_empty(&info->block_groups[c])) { |
| 3150 | btrfs_get_block_group_info( |
| 3151 | &info->block_groups[c], &space); |
| 3152 | memcpy(dest, &space, sizeof(space)); |
| 3153 | dest++; |
| 3154 | space_args.total_spaces++; |
| 3155 | slot_count--; |
| 3156 | } |
| 3157 | if (!slot_count) |
| 3158 | break; |
| 3159 | } |
| 3160 | up_read(&info->groups_sem); |
| 3161 | } |
| 3162 | |
| 3163 | user_dest = (struct btrfs_ioctl_space_info __user *) |
| 3164 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
| 3165 | |
| 3166 | if (copy_to_user(user_dest, dest_orig, alloc_size)) |
| 3167 | ret = -EFAULT; |
| 3168 | |
| 3169 | kfree(dest_orig); |
| 3170 | out: |
| 3171 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) |
| 3172 | ret = -EFAULT; |
| 3173 | |
| 3174 | return ret; |
| 3175 | } |
| 3176 | |
| 3177 | /* |
| 3178 | * there are many ways the trans_start and trans_end ioctls can lead |
| 3179 | * to deadlocks. They should only be used by applications that |
| 3180 | * basically own the machine, and have a very in depth understanding |
| 3181 | * of all the possible deadlocks and enospc problems. |
| 3182 | */ |
| 3183 | long btrfs_ioctl_trans_end(struct file *file) |
| 3184 | { |
| 3185 | struct inode *inode = file_inode(file); |
| 3186 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3187 | struct btrfs_trans_handle *trans; |
| 3188 | |
| 3189 | trans = file->private_data; |
| 3190 | if (!trans) |
| 3191 | return -EINVAL; |
| 3192 | file->private_data = NULL; |
| 3193 | |
| 3194 | btrfs_end_transaction(trans, root); |
| 3195 | |
| 3196 | atomic_dec(&root->fs_info->open_ioctl_trans); |
| 3197 | |
| 3198 | mnt_drop_write_file(file); |
| 3199 | return 0; |
| 3200 | } |
| 3201 | |
| 3202 | static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, |
| 3203 | void __user *argp) |
| 3204 | { |
| 3205 | struct btrfs_trans_handle *trans; |
| 3206 | u64 transid; |
| 3207 | int ret; |
| 3208 | |
| 3209 | trans = btrfs_attach_transaction_barrier(root); |
| 3210 | if (IS_ERR(trans)) { |
| 3211 | if (PTR_ERR(trans) != -ENOENT) |
| 3212 | return PTR_ERR(trans); |
| 3213 | |
| 3214 | /* No running transaction, don't bother */ |
| 3215 | transid = root->fs_info->last_trans_committed; |
| 3216 | goto out; |
| 3217 | } |
| 3218 | transid = trans->transid; |
| 3219 | ret = btrfs_commit_transaction_async(trans, root, 0); |
| 3220 | if (ret) { |
| 3221 | btrfs_end_transaction(trans, root); |
| 3222 | return ret; |
| 3223 | } |
| 3224 | out: |
| 3225 | if (argp) |
| 3226 | if (copy_to_user(argp, &transid, sizeof(transid))) |
| 3227 | return -EFAULT; |
| 3228 | return 0; |
| 3229 | } |
| 3230 | |
| 3231 | static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, |
| 3232 | void __user *argp) |
| 3233 | { |
| 3234 | u64 transid; |
| 3235 | |
| 3236 | if (argp) { |
| 3237 | if (copy_from_user(&transid, argp, sizeof(transid))) |
| 3238 | return -EFAULT; |
| 3239 | } else { |
| 3240 | transid = 0; /* current trans */ |
| 3241 | } |
| 3242 | return btrfs_wait_for_commit(root, transid); |
| 3243 | } |
| 3244 | |
| 3245 | static long btrfs_ioctl_scrub(struct file *file, void __user *arg) |
| 3246 | { |
| 3247 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 3248 | struct btrfs_ioctl_scrub_args *sa; |
| 3249 | int ret; |
| 3250 | |
| 3251 | if (!capable(CAP_SYS_ADMIN)) |
| 3252 | return -EPERM; |
| 3253 | |
| 3254 | sa = memdup_user(arg, sizeof(*sa)); |
| 3255 | if (IS_ERR(sa)) |
| 3256 | return PTR_ERR(sa); |
| 3257 | |
| 3258 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) { |
| 3259 | ret = mnt_want_write_file(file); |
| 3260 | if (ret) |
| 3261 | goto out; |
| 3262 | } |
| 3263 | |
| 3264 | ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, |
| 3265 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, |
| 3266 | 0); |
| 3267 | |
| 3268 | if (copy_to_user(arg, sa, sizeof(*sa))) |
| 3269 | ret = -EFAULT; |
| 3270 | |
| 3271 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) |
| 3272 | mnt_drop_write_file(file); |
| 3273 | out: |
| 3274 | kfree(sa); |
| 3275 | return ret; |
| 3276 | } |
| 3277 | |
| 3278 | static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) |
| 3279 | { |
| 3280 | if (!capable(CAP_SYS_ADMIN)) |
| 3281 | return -EPERM; |
| 3282 | |
| 3283 | return btrfs_scrub_cancel(root->fs_info); |
| 3284 | } |
| 3285 | |
| 3286 | static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, |
| 3287 | void __user *arg) |
| 3288 | { |
| 3289 | struct btrfs_ioctl_scrub_args *sa; |
| 3290 | int ret; |
| 3291 | |
| 3292 | if (!capable(CAP_SYS_ADMIN)) |
| 3293 | return -EPERM; |
| 3294 | |
| 3295 | sa = memdup_user(arg, sizeof(*sa)); |
| 3296 | if (IS_ERR(sa)) |
| 3297 | return PTR_ERR(sa); |
| 3298 | |
| 3299 | ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); |
| 3300 | |
| 3301 | if (copy_to_user(arg, sa, sizeof(*sa))) |
| 3302 | ret = -EFAULT; |
| 3303 | |
| 3304 | kfree(sa); |
| 3305 | return ret; |
| 3306 | } |
| 3307 | |
| 3308 | static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, |
| 3309 | void __user *arg) |
| 3310 | { |
| 3311 | struct btrfs_ioctl_get_dev_stats *sa; |
| 3312 | int ret; |
| 3313 | |
| 3314 | sa = memdup_user(arg, sizeof(*sa)); |
| 3315 | if (IS_ERR(sa)) |
| 3316 | return PTR_ERR(sa); |
| 3317 | |
| 3318 | if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { |
| 3319 | kfree(sa); |
| 3320 | return -EPERM; |
| 3321 | } |
| 3322 | |
| 3323 | ret = btrfs_get_dev_stats(root, sa); |
| 3324 | |
| 3325 | if (copy_to_user(arg, sa, sizeof(*sa))) |
| 3326 | ret = -EFAULT; |
| 3327 | |
| 3328 | kfree(sa); |
| 3329 | return ret; |
| 3330 | } |
| 3331 | |
| 3332 | static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) |
| 3333 | { |
| 3334 | struct btrfs_ioctl_dev_replace_args *p; |
| 3335 | int ret; |
| 3336 | |
| 3337 | if (!capable(CAP_SYS_ADMIN)) |
| 3338 | return -EPERM; |
| 3339 | |
| 3340 | p = memdup_user(arg, sizeof(*p)); |
| 3341 | if (IS_ERR(p)) |
| 3342 | return PTR_ERR(p); |
| 3343 | |
| 3344 | switch (p->cmd) { |
| 3345 | case BTRFS_IOCTL_DEV_REPLACE_CMD_START: |
| 3346 | if (atomic_xchg( |
| 3347 | &root->fs_info->mutually_exclusive_operation_running, |
| 3348 | 1)) { |
| 3349 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); |
| 3350 | ret = -EINPROGRESS; |
| 3351 | } else { |
| 3352 | ret = btrfs_dev_replace_start(root, p); |
| 3353 | atomic_set( |
| 3354 | &root->fs_info->mutually_exclusive_operation_running, |
| 3355 | 0); |
| 3356 | } |
| 3357 | break; |
| 3358 | case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: |
| 3359 | btrfs_dev_replace_status(root->fs_info, p); |
| 3360 | ret = 0; |
| 3361 | break; |
| 3362 | case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: |
| 3363 | ret = btrfs_dev_replace_cancel(root->fs_info, p); |
| 3364 | break; |
| 3365 | default: |
| 3366 | ret = -EINVAL; |
| 3367 | break; |
| 3368 | } |
| 3369 | |
| 3370 | if (copy_to_user(arg, p, sizeof(*p))) |
| 3371 | ret = -EFAULT; |
| 3372 | |
| 3373 | kfree(p); |
| 3374 | return ret; |
| 3375 | } |
| 3376 | |
| 3377 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
| 3378 | { |
| 3379 | int ret = 0; |
| 3380 | int i; |
| 3381 | u64 rel_ptr; |
| 3382 | int size; |
| 3383 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
| 3384 | struct inode_fs_paths *ipath = NULL; |
| 3385 | struct btrfs_path *path; |
| 3386 | |
| 3387 | if (!capable(CAP_DAC_READ_SEARCH)) |
| 3388 | return -EPERM; |
| 3389 | |
| 3390 | path = btrfs_alloc_path(); |
| 3391 | if (!path) { |
| 3392 | ret = -ENOMEM; |
| 3393 | goto out; |
| 3394 | } |
| 3395 | |
| 3396 | ipa = memdup_user(arg, sizeof(*ipa)); |
| 3397 | if (IS_ERR(ipa)) { |
| 3398 | ret = PTR_ERR(ipa); |
| 3399 | ipa = NULL; |
| 3400 | goto out; |
| 3401 | } |
| 3402 | |
| 3403 | size = min_t(u32, ipa->size, 4096); |
| 3404 | ipath = init_ipath(size, root, path); |
| 3405 | if (IS_ERR(ipath)) { |
| 3406 | ret = PTR_ERR(ipath); |
| 3407 | ipath = NULL; |
| 3408 | goto out; |
| 3409 | } |
| 3410 | |
| 3411 | ret = paths_from_inode(ipa->inum, ipath); |
| 3412 | if (ret < 0) |
| 3413 | goto out; |
| 3414 | |
| 3415 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { |
| 3416 | rel_ptr = ipath->fspath->val[i] - |
| 3417 | (u64)(unsigned long)ipath->fspath->val; |
| 3418 | ipath->fspath->val[i] = rel_ptr; |
| 3419 | } |
| 3420 | |
| 3421 | ret = copy_to_user((void *)(unsigned long)ipa->fspath, |
| 3422 | (void *)(unsigned long)ipath->fspath, size); |
| 3423 | if (ret) { |
| 3424 | ret = -EFAULT; |
| 3425 | goto out; |
| 3426 | } |
| 3427 | |
| 3428 | out: |
| 3429 | btrfs_free_path(path); |
| 3430 | free_ipath(ipath); |
| 3431 | kfree(ipa); |
| 3432 | |
| 3433 | return ret; |
| 3434 | } |
| 3435 | |
| 3436 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) |
| 3437 | { |
| 3438 | struct btrfs_data_container *inodes = ctx; |
| 3439 | const size_t c = 3 * sizeof(u64); |
| 3440 | |
| 3441 | if (inodes->bytes_left >= c) { |
| 3442 | inodes->bytes_left -= c; |
| 3443 | inodes->val[inodes->elem_cnt] = inum; |
| 3444 | inodes->val[inodes->elem_cnt + 1] = offset; |
| 3445 | inodes->val[inodes->elem_cnt + 2] = root; |
| 3446 | inodes->elem_cnt += 3; |
| 3447 | } else { |
| 3448 | inodes->bytes_missing += c - inodes->bytes_left; |
| 3449 | inodes->bytes_left = 0; |
| 3450 | inodes->elem_missed += 3; |
| 3451 | } |
| 3452 | |
| 3453 | return 0; |
| 3454 | } |
| 3455 | |
| 3456 | static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, |
| 3457 | void __user *arg) |
| 3458 | { |
| 3459 | int ret = 0; |
| 3460 | int size; |
| 3461 | struct btrfs_ioctl_logical_ino_args *loi; |
| 3462 | struct btrfs_data_container *inodes = NULL; |
| 3463 | struct btrfs_path *path = NULL; |
| 3464 | |
| 3465 | if (!capable(CAP_SYS_ADMIN)) |
| 3466 | return -EPERM; |
| 3467 | |
| 3468 | loi = memdup_user(arg, sizeof(*loi)); |
| 3469 | if (IS_ERR(loi)) { |
| 3470 | ret = PTR_ERR(loi); |
| 3471 | loi = NULL; |
| 3472 | goto out; |
| 3473 | } |
| 3474 | |
| 3475 | path = btrfs_alloc_path(); |
| 3476 | if (!path) { |
| 3477 | ret = -ENOMEM; |
| 3478 | goto out; |
| 3479 | } |
| 3480 | |
| 3481 | size = min_t(u32, loi->size, 64 * 1024); |
| 3482 | inodes = init_data_container(size); |
| 3483 | if (IS_ERR(inodes)) { |
| 3484 | ret = PTR_ERR(inodes); |
| 3485 | inodes = NULL; |
| 3486 | goto out; |
| 3487 | } |
| 3488 | |
| 3489 | ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, |
| 3490 | build_ino_list, inodes); |
| 3491 | if (ret == -EINVAL) |
| 3492 | ret = -ENOENT; |
| 3493 | if (ret < 0) |
| 3494 | goto out; |
| 3495 | |
| 3496 | ret = copy_to_user((void *)(unsigned long)loi->inodes, |
| 3497 | (void *)(unsigned long)inodes, size); |
| 3498 | if (ret) |
| 3499 | ret = -EFAULT; |
| 3500 | |
| 3501 | out: |
| 3502 | btrfs_free_path(path); |
| 3503 | vfree(inodes); |
| 3504 | kfree(loi); |
| 3505 | |
| 3506 | return ret; |
| 3507 | } |
| 3508 | |
| 3509 | void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, |
| 3510 | struct btrfs_ioctl_balance_args *bargs) |
| 3511 | { |
| 3512 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3513 | |
| 3514 | bargs->flags = bctl->flags; |
| 3515 | |
| 3516 | if (atomic_read(&fs_info->balance_running)) |
| 3517 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; |
| 3518 | if (atomic_read(&fs_info->balance_pause_req)) |
| 3519 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; |
| 3520 | if (atomic_read(&fs_info->balance_cancel_req)) |
| 3521 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; |
| 3522 | |
| 3523 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
| 3524 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); |
| 3525 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); |
| 3526 | |
| 3527 | if (lock) { |
| 3528 | spin_lock(&fs_info->balance_lock); |
| 3529 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); |
| 3530 | spin_unlock(&fs_info->balance_lock); |
| 3531 | } else { |
| 3532 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); |
| 3533 | } |
| 3534 | } |
| 3535 | |
| 3536 | static long btrfs_ioctl_balance(struct file *file, void __user *arg) |
| 3537 | { |
| 3538 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 3539 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3540 | struct btrfs_ioctl_balance_args *bargs; |
| 3541 | struct btrfs_balance_control *bctl; |
| 3542 | bool need_unlock; /* for mut. excl. ops lock */ |
| 3543 | int ret; |
| 3544 | |
| 3545 | if (!capable(CAP_SYS_ADMIN)) |
| 3546 | return -EPERM; |
| 3547 | |
| 3548 | ret = mnt_want_write_file(file); |
| 3549 | if (ret) |
| 3550 | return ret; |
| 3551 | |
| 3552 | again: |
| 3553 | if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) { |
| 3554 | mutex_lock(&fs_info->volume_mutex); |
| 3555 | mutex_lock(&fs_info->balance_mutex); |
| 3556 | need_unlock = true; |
| 3557 | goto locked; |
| 3558 | } |
| 3559 | |
| 3560 | /* |
| 3561 | * mut. excl. ops lock is locked. Three possibilites: |
| 3562 | * (1) some other op is running |
| 3563 | * (2) balance is running |
| 3564 | * (3) balance is paused -- special case (think resume) |
| 3565 | */ |
| 3566 | mutex_lock(&fs_info->balance_mutex); |
| 3567 | if (fs_info->balance_ctl) { |
| 3568 | /* this is either (2) or (3) */ |
| 3569 | if (!atomic_read(&fs_info->balance_running)) { |
| 3570 | mutex_unlock(&fs_info->balance_mutex); |
| 3571 | if (!mutex_trylock(&fs_info->volume_mutex)) |
| 3572 | goto again; |
| 3573 | mutex_lock(&fs_info->balance_mutex); |
| 3574 | |
| 3575 | if (fs_info->balance_ctl && |
| 3576 | !atomic_read(&fs_info->balance_running)) { |
| 3577 | /* this is (3) */ |
| 3578 | need_unlock = false; |
| 3579 | goto locked; |
| 3580 | } |
| 3581 | |
| 3582 | mutex_unlock(&fs_info->balance_mutex); |
| 3583 | mutex_unlock(&fs_info->volume_mutex); |
| 3584 | goto again; |
| 3585 | } else { |
| 3586 | /* this is (2) */ |
| 3587 | mutex_unlock(&fs_info->balance_mutex); |
| 3588 | ret = -EINPROGRESS; |
| 3589 | goto out; |
| 3590 | } |
| 3591 | } else { |
| 3592 | /* this is (1) */ |
| 3593 | mutex_unlock(&fs_info->balance_mutex); |
| 3594 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); |
| 3595 | ret = -EINVAL; |
| 3596 | goto out; |
| 3597 | } |
| 3598 | |
| 3599 | locked: |
| 3600 | BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running)); |
| 3601 | |
| 3602 | if (arg) { |
| 3603 | bargs = memdup_user(arg, sizeof(*bargs)); |
| 3604 | if (IS_ERR(bargs)) { |
| 3605 | ret = PTR_ERR(bargs); |
| 3606 | goto out_unlock; |
| 3607 | } |
| 3608 | |
| 3609 | if (bargs->flags & BTRFS_BALANCE_RESUME) { |
| 3610 | if (!fs_info->balance_ctl) { |
| 3611 | ret = -ENOTCONN; |
| 3612 | goto out_bargs; |
| 3613 | } |
| 3614 | |
| 3615 | bctl = fs_info->balance_ctl; |
| 3616 | spin_lock(&fs_info->balance_lock); |
| 3617 | bctl->flags |= BTRFS_BALANCE_RESUME; |
| 3618 | spin_unlock(&fs_info->balance_lock); |
| 3619 | |
| 3620 | goto do_balance; |
| 3621 | } |
| 3622 | } else { |
| 3623 | bargs = NULL; |
| 3624 | } |
| 3625 | |
| 3626 | if (fs_info->balance_ctl) { |
| 3627 | ret = -EINPROGRESS; |
| 3628 | goto out_bargs; |
| 3629 | } |
| 3630 | |
| 3631 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3632 | if (!bctl) { |
| 3633 | ret = -ENOMEM; |
| 3634 | goto out_bargs; |
| 3635 | } |
| 3636 | |
| 3637 | bctl->fs_info = fs_info; |
| 3638 | if (arg) { |
| 3639 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); |
| 3640 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); |
| 3641 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); |
| 3642 | |
| 3643 | bctl->flags = bargs->flags; |
| 3644 | } else { |
| 3645 | /* balance everything - no filters */ |
| 3646 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; |
| 3647 | } |
| 3648 | |
| 3649 | do_balance: |
| 3650 | /* |
| 3651 | * Ownership of bctl and mutually_exclusive_operation_running |
| 3652 | * goes to to btrfs_balance. bctl is freed in __cancel_balance, |
| 3653 | * or, if restriper was paused all the way until unmount, in |
| 3654 | * free_fs_info. mutually_exclusive_operation_running is |
| 3655 | * cleared in __cancel_balance. |
| 3656 | */ |
| 3657 | need_unlock = false; |
| 3658 | |
| 3659 | ret = btrfs_balance(bctl, bargs); |
| 3660 | |
| 3661 | if (arg) { |
| 3662 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
| 3663 | ret = -EFAULT; |
| 3664 | } |
| 3665 | |
| 3666 | out_bargs: |
| 3667 | kfree(bargs); |
| 3668 | out_unlock: |
| 3669 | mutex_unlock(&fs_info->balance_mutex); |
| 3670 | mutex_unlock(&fs_info->volume_mutex); |
| 3671 | if (need_unlock) |
| 3672 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3673 | out: |
| 3674 | mnt_drop_write_file(file); |
| 3675 | return ret; |
| 3676 | } |
| 3677 | |
| 3678 | static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) |
| 3679 | { |
| 3680 | if (!capable(CAP_SYS_ADMIN)) |
| 3681 | return -EPERM; |
| 3682 | |
| 3683 | switch (cmd) { |
| 3684 | case BTRFS_BALANCE_CTL_PAUSE: |
| 3685 | return btrfs_pause_balance(root->fs_info); |
| 3686 | case BTRFS_BALANCE_CTL_CANCEL: |
| 3687 | return btrfs_cancel_balance(root->fs_info); |
| 3688 | } |
| 3689 | |
| 3690 | return -EINVAL; |
| 3691 | } |
| 3692 | |
| 3693 | static long btrfs_ioctl_balance_progress(struct btrfs_root *root, |
| 3694 | void __user *arg) |
| 3695 | { |
| 3696 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3697 | struct btrfs_ioctl_balance_args *bargs; |
| 3698 | int ret = 0; |
| 3699 | |
| 3700 | if (!capable(CAP_SYS_ADMIN)) |
| 3701 | return -EPERM; |
| 3702 | |
| 3703 | mutex_lock(&fs_info->balance_mutex); |
| 3704 | if (!fs_info->balance_ctl) { |
| 3705 | ret = -ENOTCONN; |
| 3706 | goto out; |
| 3707 | } |
| 3708 | |
| 3709 | bargs = kzalloc(sizeof(*bargs), GFP_NOFS); |
| 3710 | if (!bargs) { |
| 3711 | ret = -ENOMEM; |
| 3712 | goto out; |
| 3713 | } |
| 3714 | |
| 3715 | update_ioctl_balance_args(fs_info, 1, bargs); |
| 3716 | |
| 3717 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
| 3718 | ret = -EFAULT; |
| 3719 | |
| 3720 | kfree(bargs); |
| 3721 | out: |
| 3722 | mutex_unlock(&fs_info->balance_mutex); |
| 3723 | return ret; |
| 3724 | } |
| 3725 | |
| 3726 | static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) |
| 3727 | { |
| 3728 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 3729 | struct btrfs_ioctl_quota_ctl_args *sa; |
| 3730 | struct btrfs_trans_handle *trans = NULL; |
| 3731 | int ret; |
| 3732 | int err; |
| 3733 | |
| 3734 | if (!capable(CAP_SYS_ADMIN)) |
| 3735 | return -EPERM; |
| 3736 | |
| 3737 | ret = mnt_want_write_file(file); |
| 3738 | if (ret) |
| 3739 | return ret; |
| 3740 | |
| 3741 | sa = memdup_user(arg, sizeof(*sa)); |
| 3742 | if (IS_ERR(sa)) { |
| 3743 | ret = PTR_ERR(sa); |
| 3744 | goto drop_write; |
| 3745 | } |
| 3746 | |
| 3747 | down_write(&root->fs_info->subvol_sem); |
| 3748 | trans = btrfs_start_transaction(root->fs_info->tree_root, 2); |
| 3749 | if (IS_ERR(trans)) { |
| 3750 | ret = PTR_ERR(trans); |
| 3751 | goto out; |
| 3752 | } |
| 3753 | |
| 3754 | switch (sa->cmd) { |
| 3755 | case BTRFS_QUOTA_CTL_ENABLE: |
| 3756 | ret = btrfs_quota_enable(trans, root->fs_info); |
| 3757 | break; |
| 3758 | case BTRFS_QUOTA_CTL_DISABLE: |
| 3759 | ret = btrfs_quota_disable(trans, root->fs_info); |
| 3760 | break; |
| 3761 | default: |
| 3762 | ret = -EINVAL; |
| 3763 | break; |
| 3764 | } |
| 3765 | |
| 3766 | err = btrfs_commit_transaction(trans, root->fs_info->tree_root); |
| 3767 | if (err && !ret) |
| 3768 | ret = err; |
| 3769 | out: |
| 3770 | kfree(sa); |
| 3771 | up_write(&root->fs_info->subvol_sem); |
| 3772 | drop_write: |
| 3773 | mnt_drop_write_file(file); |
| 3774 | return ret; |
| 3775 | } |
| 3776 | |
| 3777 | static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) |
| 3778 | { |
| 3779 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 3780 | struct btrfs_ioctl_qgroup_assign_args *sa; |
| 3781 | struct btrfs_trans_handle *trans; |
| 3782 | int ret; |
| 3783 | int err; |
| 3784 | |
| 3785 | if (!capable(CAP_SYS_ADMIN)) |
| 3786 | return -EPERM; |
| 3787 | |
| 3788 | ret = mnt_want_write_file(file); |
| 3789 | if (ret) |
| 3790 | return ret; |
| 3791 | |
| 3792 | sa = memdup_user(arg, sizeof(*sa)); |
| 3793 | if (IS_ERR(sa)) { |
| 3794 | ret = PTR_ERR(sa); |
| 3795 | goto drop_write; |
| 3796 | } |
| 3797 | |
| 3798 | trans = btrfs_join_transaction(root); |
| 3799 | if (IS_ERR(trans)) { |
| 3800 | ret = PTR_ERR(trans); |
| 3801 | goto out; |
| 3802 | } |
| 3803 | |
| 3804 | /* FIXME: check if the IDs really exist */ |
| 3805 | if (sa->assign) { |
| 3806 | ret = btrfs_add_qgroup_relation(trans, root->fs_info, |
| 3807 | sa->src, sa->dst); |
| 3808 | } else { |
| 3809 | ret = btrfs_del_qgroup_relation(trans, root->fs_info, |
| 3810 | sa->src, sa->dst); |
| 3811 | } |
| 3812 | |
| 3813 | err = btrfs_end_transaction(trans, root); |
| 3814 | if (err && !ret) |
| 3815 | ret = err; |
| 3816 | |
| 3817 | out: |
| 3818 | kfree(sa); |
| 3819 | drop_write: |
| 3820 | mnt_drop_write_file(file); |
| 3821 | return ret; |
| 3822 | } |
| 3823 | |
| 3824 | static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) |
| 3825 | { |
| 3826 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 3827 | struct btrfs_ioctl_qgroup_create_args *sa; |
| 3828 | struct btrfs_trans_handle *trans; |
| 3829 | int ret; |
| 3830 | int err; |
| 3831 | |
| 3832 | if (!capable(CAP_SYS_ADMIN)) |
| 3833 | return -EPERM; |
| 3834 | |
| 3835 | ret = mnt_want_write_file(file); |
| 3836 | if (ret) |
| 3837 | return ret; |
| 3838 | |
| 3839 | sa = memdup_user(arg, sizeof(*sa)); |
| 3840 | if (IS_ERR(sa)) { |
| 3841 | ret = PTR_ERR(sa); |
| 3842 | goto drop_write; |
| 3843 | } |
| 3844 | |
| 3845 | if (!sa->qgroupid) { |
| 3846 | ret = -EINVAL; |
| 3847 | goto out; |
| 3848 | } |
| 3849 | |
| 3850 | trans = btrfs_join_transaction(root); |
| 3851 | if (IS_ERR(trans)) { |
| 3852 | ret = PTR_ERR(trans); |
| 3853 | goto out; |
| 3854 | } |
| 3855 | |
| 3856 | /* FIXME: check if the IDs really exist */ |
| 3857 | if (sa->create) { |
| 3858 | ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid, |
| 3859 | NULL); |
| 3860 | } else { |
| 3861 | ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); |
| 3862 | } |
| 3863 | |
| 3864 | err = btrfs_end_transaction(trans, root); |
| 3865 | if (err && !ret) |
| 3866 | ret = err; |
| 3867 | |
| 3868 | out: |
| 3869 | kfree(sa); |
| 3870 | drop_write: |
| 3871 | mnt_drop_write_file(file); |
| 3872 | return ret; |
| 3873 | } |
| 3874 | |
| 3875 | static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) |
| 3876 | { |
| 3877 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 3878 | struct btrfs_ioctl_qgroup_limit_args *sa; |
| 3879 | struct btrfs_trans_handle *trans; |
| 3880 | int ret; |
| 3881 | int err; |
| 3882 | u64 qgroupid; |
| 3883 | |
| 3884 | if (!capable(CAP_SYS_ADMIN)) |
| 3885 | return -EPERM; |
| 3886 | |
| 3887 | ret = mnt_want_write_file(file); |
| 3888 | if (ret) |
| 3889 | return ret; |
| 3890 | |
| 3891 | sa = memdup_user(arg, sizeof(*sa)); |
| 3892 | if (IS_ERR(sa)) { |
| 3893 | ret = PTR_ERR(sa); |
| 3894 | goto drop_write; |
| 3895 | } |
| 3896 | |
| 3897 | trans = btrfs_join_transaction(root); |
| 3898 | if (IS_ERR(trans)) { |
| 3899 | ret = PTR_ERR(trans); |
| 3900 | goto out; |
| 3901 | } |
| 3902 | |
| 3903 | qgroupid = sa->qgroupid; |
| 3904 | if (!qgroupid) { |
| 3905 | /* take the current subvol as qgroup */ |
| 3906 | qgroupid = root->root_key.objectid; |
| 3907 | } |
| 3908 | |
| 3909 | /* FIXME: check if the IDs really exist */ |
| 3910 | ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); |
| 3911 | |
| 3912 | err = btrfs_end_transaction(trans, root); |
| 3913 | if (err && !ret) |
| 3914 | ret = err; |
| 3915 | |
| 3916 | out: |
| 3917 | kfree(sa); |
| 3918 | drop_write: |
| 3919 | mnt_drop_write_file(file); |
| 3920 | return ret; |
| 3921 | } |
| 3922 | |
| 3923 | static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) |
| 3924 | { |
| 3925 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 3926 | struct btrfs_ioctl_quota_rescan_args *qsa; |
| 3927 | int ret; |
| 3928 | |
| 3929 | if (!capable(CAP_SYS_ADMIN)) |
| 3930 | return -EPERM; |
| 3931 | |
| 3932 | ret = mnt_want_write_file(file); |
| 3933 | if (ret) |
| 3934 | return ret; |
| 3935 | |
| 3936 | qsa = memdup_user(arg, sizeof(*qsa)); |
| 3937 | if (IS_ERR(qsa)) { |
| 3938 | ret = PTR_ERR(qsa); |
| 3939 | goto drop_write; |
| 3940 | } |
| 3941 | |
| 3942 | if (qsa->flags) { |
| 3943 | ret = -EINVAL; |
| 3944 | goto out; |
| 3945 | } |
| 3946 | |
| 3947 | ret = btrfs_qgroup_rescan(root->fs_info); |
| 3948 | |
| 3949 | out: |
| 3950 | kfree(qsa); |
| 3951 | drop_write: |
| 3952 | mnt_drop_write_file(file); |
| 3953 | return ret; |
| 3954 | } |
| 3955 | |
| 3956 | static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) |
| 3957 | { |
| 3958 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 3959 | struct btrfs_ioctl_quota_rescan_args *qsa; |
| 3960 | int ret = 0; |
| 3961 | |
| 3962 | if (!capable(CAP_SYS_ADMIN)) |
| 3963 | return -EPERM; |
| 3964 | |
| 3965 | qsa = kzalloc(sizeof(*qsa), GFP_NOFS); |
| 3966 | if (!qsa) |
| 3967 | return -ENOMEM; |
| 3968 | |
| 3969 | if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
| 3970 | qsa->flags = 1; |
| 3971 | qsa->progress = root->fs_info->qgroup_rescan_progress.objectid; |
| 3972 | } |
| 3973 | |
| 3974 | if (copy_to_user(arg, qsa, sizeof(*qsa))) |
| 3975 | ret = -EFAULT; |
| 3976 | |
| 3977 | kfree(qsa); |
| 3978 | return ret; |
| 3979 | } |
| 3980 | |
| 3981 | static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) |
| 3982 | { |
| 3983 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
| 3984 | |
| 3985 | if (!capable(CAP_SYS_ADMIN)) |
| 3986 | return -EPERM; |
| 3987 | |
| 3988 | return btrfs_qgroup_wait_for_completion(root->fs_info); |
| 3989 | } |
| 3990 | |
| 3991 | static long btrfs_ioctl_set_received_subvol(struct file *file, |
| 3992 | void __user *arg) |
| 3993 | { |
| 3994 | struct btrfs_ioctl_received_subvol_args *sa = NULL; |
| 3995 | struct inode *inode = file_inode(file); |
| 3996 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3997 | struct btrfs_root_item *root_item = &root->root_item; |
| 3998 | struct btrfs_trans_handle *trans; |
| 3999 | struct timespec ct = CURRENT_TIME; |
| 4000 | int ret = 0; |
| 4001 | |
| 4002 | ret = mnt_want_write_file(file); |
| 4003 | if (ret < 0) |
| 4004 | return ret; |
| 4005 | |
| 4006 | down_write(&root->fs_info->subvol_sem); |
| 4007 | |
| 4008 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { |
| 4009 | ret = -EINVAL; |
| 4010 | goto out; |
| 4011 | } |
| 4012 | |
| 4013 | if (btrfs_root_readonly(root)) { |
| 4014 | ret = -EROFS; |
| 4015 | goto out; |
| 4016 | } |
| 4017 | |
| 4018 | if (!inode_owner_or_capable(inode)) { |
| 4019 | ret = -EACCES; |
| 4020 | goto out; |
| 4021 | } |
| 4022 | |
| 4023 | sa = memdup_user(arg, sizeof(*sa)); |
| 4024 | if (IS_ERR(sa)) { |
| 4025 | ret = PTR_ERR(sa); |
| 4026 | sa = NULL; |
| 4027 | goto out; |
| 4028 | } |
| 4029 | |
| 4030 | trans = btrfs_start_transaction(root, 1); |
| 4031 | if (IS_ERR(trans)) { |
| 4032 | ret = PTR_ERR(trans); |
| 4033 | trans = NULL; |
| 4034 | goto out; |
| 4035 | } |
| 4036 | |
| 4037 | sa->rtransid = trans->transid; |
| 4038 | sa->rtime.sec = ct.tv_sec; |
| 4039 | sa->rtime.nsec = ct.tv_nsec; |
| 4040 | |
| 4041 | memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); |
| 4042 | btrfs_set_root_stransid(root_item, sa->stransid); |
| 4043 | btrfs_set_root_rtransid(root_item, sa->rtransid); |
| 4044 | btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); |
| 4045 | btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); |
| 4046 | btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); |
| 4047 | btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); |
| 4048 | |
| 4049 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 4050 | &root->root_key, &root->root_item); |
| 4051 | if (ret < 0) { |
| 4052 | btrfs_end_transaction(trans, root); |
| 4053 | trans = NULL; |
| 4054 | goto out; |
| 4055 | } else { |
| 4056 | ret = btrfs_commit_transaction(trans, root); |
| 4057 | if (ret < 0) |
| 4058 | goto out; |
| 4059 | } |
| 4060 | |
| 4061 | ret = copy_to_user(arg, sa, sizeof(*sa)); |
| 4062 | if (ret) |
| 4063 | ret = -EFAULT; |
| 4064 | |
| 4065 | out: |
| 4066 | kfree(sa); |
| 4067 | up_write(&root->fs_info->subvol_sem); |
| 4068 | mnt_drop_write_file(file); |
| 4069 | return ret; |
| 4070 | } |
| 4071 | |
| 4072 | static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) |
| 4073 | { |
| 4074 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 4075 | size_t len; |
| 4076 | int ret; |
| 4077 | char label[BTRFS_LABEL_SIZE]; |
| 4078 | |
| 4079 | spin_lock(&root->fs_info->super_lock); |
| 4080 | memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE); |
| 4081 | spin_unlock(&root->fs_info->super_lock); |
| 4082 | |
| 4083 | len = strnlen(label, BTRFS_LABEL_SIZE); |
| 4084 | |
| 4085 | if (len == BTRFS_LABEL_SIZE) { |
| 4086 | pr_warn("btrfs: label is too long, return the first %zu bytes\n", |
| 4087 | --len); |
| 4088 | } |
| 4089 | |
| 4090 | ret = copy_to_user(arg, label, len); |
| 4091 | |
| 4092 | return ret ? -EFAULT : 0; |
| 4093 | } |
| 4094 | |
| 4095 | static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) |
| 4096 | { |
| 4097 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 4098 | struct btrfs_super_block *super_block = root->fs_info->super_copy; |
| 4099 | struct btrfs_trans_handle *trans; |
| 4100 | char label[BTRFS_LABEL_SIZE]; |
| 4101 | int ret; |
| 4102 | |
| 4103 | if (!capable(CAP_SYS_ADMIN)) |
| 4104 | return -EPERM; |
| 4105 | |
| 4106 | if (copy_from_user(label, arg, sizeof(label))) |
| 4107 | return -EFAULT; |
| 4108 | |
| 4109 | if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { |
| 4110 | pr_err("btrfs: unable to set label with more than %d bytes\n", |
| 4111 | BTRFS_LABEL_SIZE - 1); |
| 4112 | return -EINVAL; |
| 4113 | } |
| 4114 | |
| 4115 | ret = mnt_want_write_file(file); |
| 4116 | if (ret) |
| 4117 | return ret; |
| 4118 | |
| 4119 | trans = btrfs_start_transaction(root, 0); |
| 4120 | if (IS_ERR(trans)) { |
| 4121 | ret = PTR_ERR(trans); |
| 4122 | goto out_unlock; |
| 4123 | } |
| 4124 | |
| 4125 | spin_lock(&root->fs_info->super_lock); |
| 4126 | strcpy(super_block->label, label); |
| 4127 | spin_unlock(&root->fs_info->super_lock); |
| 4128 | ret = btrfs_end_transaction(trans, root); |
| 4129 | |
| 4130 | out_unlock: |
| 4131 | mnt_drop_write_file(file); |
| 4132 | return ret; |
| 4133 | } |
| 4134 | |
| 4135 | long btrfs_ioctl(struct file *file, unsigned int |
| 4136 | cmd, unsigned long arg) |
| 4137 | { |
| 4138 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
| 4139 | void __user *argp = (void __user *)arg; |
| 4140 | |
| 4141 | switch (cmd) { |
| 4142 | case FS_IOC_GETFLAGS: |
| 4143 | return btrfs_ioctl_getflags(file, argp); |
| 4144 | case FS_IOC_SETFLAGS: |
| 4145 | return btrfs_ioctl_setflags(file, argp); |
| 4146 | case FS_IOC_GETVERSION: |
| 4147 | return btrfs_ioctl_getversion(file, argp); |
| 4148 | case FITRIM: |
| 4149 | return btrfs_ioctl_fitrim(file, argp); |
| 4150 | case BTRFS_IOC_SNAP_CREATE: |
| 4151 | return btrfs_ioctl_snap_create(file, argp, 0); |
| 4152 | case BTRFS_IOC_SNAP_CREATE_V2: |
| 4153 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
| 4154 | case BTRFS_IOC_SUBVOL_CREATE: |
| 4155 | return btrfs_ioctl_snap_create(file, argp, 1); |
| 4156 | case BTRFS_IOC_SUBVOL_CREATE_V2: |
| 4157 | return btrfs_ioctl_snap_create_v2(file, argp, 1); |
| 4158 | case BTRFS_IOC_SNAP_DESTROY: |
| 4159 | return btrfs_ioctl_snap_destroy(file, argp); |
| 4160 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
| 4161 | return btrfs_ioctl_subvol_getflags(file, argp); |
| 4162 | case BTRFS_IOC_SUBVOL_SETFLAGS: |
| 4163 | return btrfs_ioctl_subvol_setflags(file, argp); |
| 4164 | case BTRFS_IOC_DEFAULT_SUBVOL: |
| 4165 | return btrfs_ioctl_default_subvol(file, argp); |
| 4166 | case BTRFS_IOC_DEFRAG: |
| 4167 | return btrfs_ioctl_defrag(file, NULL); |
| 4168 | case BTRFS_IOC_DEFRAG_RANGE: |
| 4169 | return btrfs_ioctl_defrag(file, argp); |
| 4170 | case BTRFS_IOC_RESIZE: |
| 4171 | return btrfs_ioctl_resize(file, argp); |
| 4172 | case BTRFS_IOC_ADD_DEV: |
| 4173 | return btrfs_ioctl_add_dev(root, argp); |
| 4174 | case BTRFS_IOC_RM_DEV: |
| 4175 | return btrfs_ioctl_rm_dev(file, argp); |
| 4176 | case BTRFS_IOC_FS_INFO: |
| 4177 | return btrfs_ioctl_fs_info(root, argp); |
| 4178 | case BTRFS_IOC_DEV_INFO: |
| 4179 | return btrfs_ioctl_dev_info(root, argp); |
| 4180 | case BTRFS_IOC_BALANCE: |
| 4181 | return btrfs_ioctl_balance(file, NULL); |
| 4182 | case BTRFS_IOC_CLONE: |
| 4183 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
| 4184 | case BTRFS_IOC_CLONE_RANGE: |
| 4185 | return btrfs_ioctl_clone_range(file, argp); |
| 4186 | case BTRFS_IOC_TRANS_START: |
| 4187 | return btrfs_ioctl_trans_start(file); |
| 4188 | case BTRFS_IOC_TRANS_END: |
| 4189 | return btrfs_ioctl_trans_end(file); |
| 4190 | case BTRFS_IOC_TREE_SEARCH: |
| 4191 | return btrfs_ioctl_tree_search(file, argp); |
| 4192 | case BTRFS_IOC_INO_LOOKUP: |
| 4193 | return btrfs_ioctl_ino_lookup(file, argp); |
| 4194 | case BTRFS_IOC_INO_PATHS: |
| 4195 | return btrfs_ioctl_ino_to_path(root, argp); |
| 4196 | case BTRFS_IOC_LOGICAL_INO: |
| 4197 | return btrfs_ioctl_logical_to_ino(root, argp); |
| 4198 | case BTRFS_IOC_SPACE_INFO: |
| 4199 | return btrfs_ioctl_space_info(root, argp); |
| 4200 | case BTRFS_IOC_SYNC: |
| 4201 | btrfs_sync_fs(file->f_dentry->d_sb, 1); |
| 4202 | return 0; |
| 4203 | case BTRFS_IOC_START_SYNC: |
| 4204 | return btrfs_ioctl_start_sync(root, argp); |
| 4205 | case BTRFS_IOC_WAIT_SYNC: |
| 4206 | return btrfs_ioctl_wait_sync(root, argp); |
| 4207 | case BTRFS_IOC_SCRUB: |
| 4208 | return btrfs_ioctl_scrub(file, argp); |
| 4209 | case BTRFS_IOC_SCRUB_CANCEL: |
| 4210 | return btrfs_ioctl_scrub_cancel(root, argp); |
| 4211 | case BTRFS_IOC_SCRUB_PROGRESS: |
| 4212 | return btrfs_ioctl_scrub_progress(root, argp); |
| 4213 | case BTRFS_IOC_BALANCE_V2: |
| 4214 | return btrfs_ioctl_balance(file, argp); |
| 4215 | case BTRFS_IOC_BALANCE_CTL: |
| 4216 | return btrfs_ioctl_balance_ctl(root, arg); |
| 4217 | case BTRFS_IOC_BALANCE_PROGRESS: |
| 4218 | return btrfs_ioctl_balance_progress(root, argp); |
| 4219 | case BTRFS_IOC_SET_RECEIVED_SUBVOL: |
| 4220 | return btrfs_ioctl_set_received_subvol(file, argp); |
| 4221 | case BTRFS_IOC_SEND: |
| 4222 | return btrfs_ioctl_send(file, argp); |
| 4223 | case BTRFS_IOC_GET_DEV_STATS: |
| 4224 | return btrfs_ioctl_get_dev_stats(root, argp); |
| 4225 | case BTRFS_IOC_QUOTA_CTL: |
| 4226 | return btrfs_ioctl_quota_ctl(file, argp); |
| 4227 | case BTRFS_IOC_QGROUP_ASSIGN: |
| 4228 | return btrfs_ioctl_qgroup_assign(file, argp); |
| 4229 | case BTRFS_IOC_QGROUP_CREATE: |
| 4230 | return btrfs_ioctl_qgroup_create(file, argp); |
| 4231 | case BTRFS_IOC_QGROUP_LIMIT: |
| 4232 | return btrfs_ioctl_qgroup_limit(file, argp); |
| 4233 | case BTRFS_IOC_QUOTA_RESCAN: |
| 4234 | return btrfs_ioctl_quota_rescan(file, argp); |
| 4235 | case BTRFS_IOC_QUOTA_RESCAN_STATUS: |
| 4236 | return btrfs_ioctl_quota_rescan_status(file, argp); |
| 4237 | case BTRFS_IOC_QUOTA_RESCAN_WAIT: |
| 4238 | return btrfs_ioctl_quota_rescan_wait(file, argp); |
| 4239 | case BTRFS_IOC_DEV_REPLACE: |
| 4240 | return btrfs_ioctl_dev_replace(root, argp); |
| 4241 | case BTRFS_IOC_GET_FSLABEL: |
| 4242 | return btrfs_ioctl_get_fslabel(file, argp); |
| 4243 | case BTRFS_IOC_SET_FSLABEL: |
| 4244 | return btrfs_ioctl_set_fslabel(file, argp); |
| 4245 | } |
| 4246 | |
| 4247 | return -ENOTTY; |
| 4248 | } |