blob: 2cfd1bfb387181449ffce9eea3357a2de834f515 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004 */
5
6#include <linux/kernel.h>
7#include <linux/bio.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008#include <linux/file.h>
9#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040010#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040011#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040014#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040015#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040016#include <linux/mount.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040017#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040018#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040019#include <linux/compat.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040020#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040021#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020022#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000024#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020025#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000026#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070027#include <linux/uaccess.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050028#include <linux/iversion.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040029#include "ctree.h"
30#include "disk-io.h"
31#include "transaction.h"
32#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040033#include "print-tree.h"
34#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040035#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080036#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020037#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040038#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020039#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010040#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000041#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040042#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070043#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000044#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080045#include "compression.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040046
Hugo Millsabccd002014-01-30 20:17:00 +000047#ifdef CONFIG_64BIT
48/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
49 * structures are incorrect, as the timespec structure from userspace
50 * is 4 bytes too small. We define these alternatives here to teach
51 * the kernel about the 32-bit struct packing.
52 */
53struct btrfs_ioctl_timespec_32 {
54 __u64 sec;
55 __u32 nsec;
56} __attribute__ ((__packed__));
57
58struct btrfs_ioctl_received_subvol_args_32 {
59 char uuid[BTRFS_UUID_SIZE]; /* in */
60 __u64 stransid; /* in */
61 __u64 rtransid; /* out */
62 struct btrfs_ioctl_timespec_32 stime; /* in */
63 struct btrfs_ioctl_timespec_32 rtime; /* out */
64 __u64 flags; /* in */
65 __u64 reserved[16]; /* in */
66} __attribute__ ((__packed__));
67
68#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
69 struct btrfs_ioctl_received_subvol_args_32)
70#endif
71
Josef Bacik2351f432017-09-27 10:43:13 -040072#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
73struct btrfs_ioctl_send_args_32 {
74 __s64 send_fd; /* in */
75 __u64 clone_sources_count; /* in */
76 compat_uptr_t clone_sources; /* in */
77 __u64 parent_root; /* in */
78 __u64 flags; /* in */
79 __u64 reserved[4]; /* in */
80} __attribute__ ((__packed__));
81
82#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
83 struct btrfs_ioctl_send_args_32)
84#endif
Hugo Millsabccd002014-01-30 20:17:00 +000085
Mark Fasheh416161d2013-08-06 11:42:51 -070086static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070087 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
88 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070089
Christoph Hellwig6cbff002009-04-17 10:37:41 +020090/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020091static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
92 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093{
David Sterba1905a0f2018-03-26 18:52:15 +020094 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +020096 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020097 return flags & ~FS_DIRSYNC_FL;
98 else
99 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
100}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400101
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200102/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200103 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
104 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200105 */
David Sterbaa157d4f2018-03-26 19:12:25 +0200106static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200107{
108 unsigned int iflags = 0;
109
110 if (flags & BTRFS_INODE_SYNC)
111 iflags |= FS_SYNC_FL;
112 if (flags & BTRFS_INODE_IMMUTABLE)
113 iflags |= FS_IMMUTABLE_FL;
114 if (flags & BTRFS_INODE_APPEND)
115 iflags |= FS_APPEND_FL;
116 if (flags & BTRFS_INODE_NODUMP)
117 iflags |= FS_NODUMP_FL;
118 if (flags & BTRFS_INODE_NOATIME)
119 iflags |= FS_NOATIME_FL;
120 if (flags & BTRFS_INODE_DIRSYNC)
121 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000122 if (flags & BTRFS_INODE_NODATACOW)
123 iflags |= FS_NOCOW_FL;
124
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900125 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000126 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900127 else if (flags & BTRFS_INODE_COMPRESS)
128 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200129
130 return iflags;
131}
132
133/*
134 * Update inode->i_flags based on the btrfs internal flags.
135 */
David Sterba7b6a2212018-03-26 18:40:21 +0200136void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200137{
David Sterba5c57b8b2018-04-23 15:45:18 +0200138 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100139 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200140
David Sterba5c57b8b2018-04-23 15:45:18 +0200141 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100142 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200143 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200145 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100146 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200147 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200149 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_DIRSYNC;
151
152 set_mask_bits(&inode->i_flags,
153 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
154 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200155}
156
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200157static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158{
David Sterba5c57b8b2018-04-23 15:45:18 +0200159 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
160 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165}
166
David Sterba5ba76ab2018-03-26 18:52:15 +0200167/* Check if @flags are a supported and valid set of FS_*_FL flags */
168static int check_fsflags(unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000169{
170 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
171 FS_NOATIME_FL | FS_NODUMP_FL | \
172 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000173 FS_NOCOMP_FL | FS_COMPR_FL |
174 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000175 return -EOPNOTSUPP;
176
177 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
178 return -EINVAL;
179
Liu Bo75e7cb72011-03-22 10:12:20 +0000180 return 0;
181}
182
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200183static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
184{
Al Viro496ad9a2013-01-23 17:07:38 -0500185 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400186 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200187 struct btrfs_inode *binode = BTRFS_I(inode);
188 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200189 struct btrfs_trans_handle *trans;
Anand Jain3c8d8b62019-04-20 19:48:56 +0800190 unsigned int fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200191 int ret;
Anand Jainff9fef552019-04-20 19:48:53 +0800192 const char *comp = NULL;
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800193 u32 binode_flags = binode->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200194
David Sterbabd60ea02014-01-16 15:50:22 +0100195 if (!inode_owner_or_capable(inode))
196 return -EPERM;
197
Li Zefanb83cc962010-12-20 16:04:08 +0800198 if (btrfs_root_readonly(root))
199 return -EROFS;
200
David Sterba5c57b8b2018-04-23 15:45:18 +0200201 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200202 return -EFAULT;
203
David Sterba5c57b8b2018-04-23 15:45:18 +0200204 ret = check_fsflags(fsflags);
Liu Bo75e7cb72011-03-22 10:12:20 +0000205 if (ret)
206 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200207
Jan Karae7848682012-06-12 16:20:32 +0200208 ret = mnt_want_write_file(file);
209 if (ret)
210 return ret;
211
Al Viro59551022016-01-22 15:40:57 -0500212 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200213
David Sterba5c57b8b2018-04-23 15:45:18 +0200214 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
Anand Jain3c8d8b62019-04-20 19:48:56 +0800215 if ((fsflags ^ btrfs_inode_flags_to_fsflags(binode->flags)) &
216 (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200217 if (!capable(CAP_LINUX_IMMUTABLE)) {
218 ret = -EPERM;
219 goto out_unlock;
220 }
221 }
222
David Sterba5c57b8b2018-04-23 15:45:18 +0200223 if (fsflags & FS_SYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800224 binode_flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200225 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800226 binode_flags &= ~BTRFS_INODE_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200227 if (fsflags & FS_IMMUTABLE_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800228 binode_flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200229 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800230 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200231 if (fsflags & FS_APPEND_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800232 binode_flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200233 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800234 binode_flags &= ~BTRFS_INODE_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200235 if (fsflags & FS_NODUMP_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800236 binode_flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200237 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800238 binode_flags &= ~BTRFS_INODE_NODUMP;
David Sterba5c57b8b2018-04-23 15:45:18 +0200239 if (fsflags & FS_NOATIME_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800240 binode_flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200241 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800242 binode_flags &= ~BTRFS_INODE_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200243 if (fsflags & FS_DIRSYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800244 binode_flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200245 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800246 binode_flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200247 if (fsflags & FS_NOCOW_FL) {
Anand Jain44e51942019-04-20 19:48:57 +0800248 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600249 /*
250 * It's safe to turn csums off here, no extents exist.
251 * Otherwise we want the flag to reflect the real COW
252 * status of the file and will not set it.
253 */
254 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800255 binode_flags |= BTRFS_INODE_NODATACOW |
256 BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600257 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800258 binode_flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600259 }
260 } else {
261 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400262 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600263 */
Anand Jain44e51942019-04-20 19:48:57 +0800264 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600265 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800266 binode_flags &= ~(BTRFS_INODE_NODATACOW |
267 BTRFS_INODE_NODATASUM);
David Sterba7e97b8d2012-09-07 05:56:55 -0600268 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800269 binode_flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600270 }
271 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200272
Liu Bo75e7cb72011-03-22 10:12:20 +0000273 /*
274 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
275 * flag may be changed automatically if compression code won't make
276 * things smaller.
277 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200278 if (fsflags & FS_NOCOMP_FL) {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800279 binode_flags &= ~BTRFS_INODE_COMPRESS;
280 binode_flags |= BTRFS_INODE_NOCOMPRESS;
David Sterba5c57b8b2018-04-23 15:45:18 +0200281 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000282
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700283 if (IS_SWAPFILE(inode)) {
284 ret = -ETXTBSY;
285 goto out_unlock;
286 }
287
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800288 binode_flags |= BTRFS_INODE_COMPRESS;
289 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000290
David Sterba93370502017-10-31 17:32:41 +0100291 comp = btrfs_compress_type2str(fs_info->compress_type);
292 if (!comp || comp[0] == 0)
293 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
Li Zefanebcb9042011-04-15 03:03:17 +0000294 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800295 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000296 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200297
Anand Jainff9fef552019-04-20 19:48:53 +0800298 /*
299 * 1 for inode item
300 * 2 for properties
301 */
302 trans = btrfs_start_transaction(root, 3);
Li Zefanf062abf02011-12-29 13:36:45 +0800303 if (IS_ERR(trans)) {
304 ret = PTR_ERR(trans);
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800305 goto out_unlock;
Li Zefanf062abf02011-12-29 13:36:45 +0800306 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200307
Anand Jainff9fef552019-04-20 19:48:53 +0800308 if (comp) {
309 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
310 strlen(comp), 0);
311 if (ret) {
312 btrfs_abort_transaction(trans, ret);
313 goto out_end_trans;
314 }
315 set_bit(BTRFS_INODE_COPY_EVERYTHING,
316 &BTRFS_I(inode)->runtime_flags);
317 } else {
318 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
319 0, 0);
320 if (ret && ret != -ENODATA) {
321 btrfs_abort_transaction(trans, ret);
322 goto out_end_trans;
323 }
324 }
325
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800326 binode->flags = binode_flags;
David Sterba7b6a2212018-03-26 18:40:21 +0200327 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400328 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700329 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200330 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200331
Anand Jainff9fef552019-04-20 19:48:53 +0800332 out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400333 btrfs_end_transaction(trans);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200334 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500335 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200336 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000337 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200338}
339
David Sterba19f93b32018-03-26 19:42:05 +0200340/*
341 * Translate btrfs internal inode flags to xflags as expected by the
342 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
343 * silently dropped.
344 */
345static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
346{
347 unsigned int xflags = 0;
348
349 if (flags & BTRFS_INODE_APPEND)
350 xflags |= FS_XFLAG_APPEND;
351 if (flags & BTRFS_INODE_IMMUTABLE)
352 xflags |= FS_XFLAG_IMMUTABLE;
353 if (flags & BTRFS_INODE_NOATIME)
354 xflags |= FS_XFLAG_NOATIME;
355 if (flags & BTRFS_INODE_NODUMP)
356 xflags |= FS_XFLAG_NODUMP;
357 if (flags & BTRFS_INODE_SYNC)
358 xflags |= FS_XFLAG_SYNC;
359
360 return xflags;
361}
362
363/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
364static int check_xflags(unsigned int flags)
365{
366 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
367 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
368 return -EOPNOTSUPP;
369 return 0;
370}
371
David Sterbae4202ac2018-03-26 19:51:16 +0200372/*
373 * Set the xflags from the internal inode flags. The remaining items of fsxattr
374 * are zeroed.
375 */
376static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
377{
378 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
379 struct fsxattr fa;
380
381 memset(&fa, 0, sizeof(fa));
382 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
383
384 if (copy_to_user(arg, &fa, sizeof(fa)))
385 return -EFAULT;
386
387 return 0;
388}
389
David Sterba025f2122018-03-26 19:51:16 +0200390static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
391{
392 struct inode *inode = file_inode(file);
393 struct btrfs_inode *binode = BTRFS_I(inode);
394 struct btrfs_root *root = binode->root;
395 struct btrfs_trans_handle *trans;
396 struct fsxattr fa;
397 unsigned old_flags;
398 unsigned old_i_flags;
399 int ret = 0;
400
401 if (!inode_owner_or_capable(inode))
402 return -EPERM;
403
404 if (btrfs_root_readonly(root))
405 return -EROFS;
406
407 memset(&fa, 0, sizeof(fa));
408 if (copy_from_user(&fa, arg, sizeof(fa)))
409 return -EFAULT;
410
411 ret = check_xflags(fa.fsx_xflags);
412 if (ret)
413 return ret;
414
415 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
416 return -EOPNOTSUPP;
417
418 ret = mnt_want_write_file(file);
419 if (ret)
420 return ret;
421
422 inode_lock(inode);
423
424 old_flags = binode->flags;
425 old_i_flags = inode->i_flags;
426
427 /* We need the capabilities to change append-only or immutable inode */
428 if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
429 (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
430 !capable(CAP_LINUX_IMMUTABLE)) {
431 ret = -EPERM;
432 goto out_unlock;
433 }
434
435 if (fa.fsx_xflags & FS_XFLAG_SYNC)
436 binode->flags |= BTRFS_INODE_SYNC;
437 else
438 binode->flags &= ~BTRFS_INODE_SYNC;
439 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
440 binode->flags |= BTRFS_INODE_IMMUTABLE;
441 else
442 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
443 if (fa.fsx_xflags & FS_XFLAG_APPEND)
444 binode->flags |= BTRFS_INODE_APPEND;
445 else
446 binode->flags &= ~BTRFS_INODE_APPEND;
447 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
448 binode->flags |= BTRFS_INODE_NODUMP;
449 else
450 binode->flags &= ~BTRFS_INODE_NODUMP;
451 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
452 binode->flags |= BTRFS_INODE_NOATIME;
453 else
454 binode->flags &= ~BTRFS_INODE_NOATIME;
455
456 /* 1 item for the inode */
457 trans = btrfs_start_transaction(root, 1);
458 if (IS_ERR(trans)) {
459 ret = PTR_ERR(trans);
460 goto out_unlock;
461 }
462
463 btrfs_sync_inode_flags_to_i_flags(inode);
464 inode_inc_iversion(inode);
465 inode->i_ctime = current_time(inode);
466 ret = btrfs_update_inode(trans, root, inode);
467
468 btrfs_end_transaction(trans);
469
470out_unlock:
471 if (ret) {
472 binode->flags = old_flags;
473 inode->i_flags = old_i_flags;
474 }
475
476 inode_unlock(inode);
477 mnt_drop_write_file(file);
478
479 return ret;
480}
481
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200482static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
483{
Al Viro496ad9a2013-01-23 17:07:38 -0500484 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200485
486 return put_user(inode->i_generation, arg);
487}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400488
Li Dongyangf7039b12011-03-24 10:24:28 +0000489static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
490{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400491 struct inode *inode = file_inode(file);
492 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000493 struct btrfs_device *device;
494 struct request_queue *q;
495 struct fstrim_range range;
496 u64 minlen = ULLONG_MAX;
497 u64 num_devices = 0;
498 int ret;
499
500 if (!capable(CAP_SYS_ADMIN))
501 return -EPERM;
502
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000503 /*
504 * If the fs is mounted with nologreplay, which requires it to be
505 * mounted in RO mode as well, we can not allow discard on free space
506 * inside block groups, because log trees refer to extents that are not
507 * pinned in a block group's free space cache (pinning the extents is
508 * precisely the first phase of replaying a log tree).
509 */
510 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
511 return -EROFS;
512
Xiao Guangrong1f781602011-04-20 10:09:16 +0000513 rcu_read_lock();
514 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
515 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000516 if (!device->bdev)
517 continue;
518 q = bdev_get_queue(device->bdev);
519 if (blk_queue_discard(q)) {
520 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100521 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000522 minlen);
523 }
524 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000525 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200526
Li Dongyangf7039b12011-03-24 10:24:28 +0000527 if (!num_devices)
528 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000529 if (copy_from_user(&range, arg, sizeof(range)))
530 return -EFAULT;
Qu Wenruo6ba9fc82018-09-07 14:16:24 +0800531
532 /*
533 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
534 * block group is in the logical address space, which can be any
535 * sectorsize aligned bytenr in the range [0, U64_MAX].
536 */
537 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200538 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000539
540 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400541 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000542 if (ret < 0)
543 return ret;
544
545 if (copy_to_user(arg, &range, sizeof(range)))
546 return -EFAULT;
547
548 return 0;
549}
550
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200551int btrfs_is_empty_uuid(u8 *uuid)
552{
Chris Mason46e0f662013-11-15 12:14:55 +0100553 int i;
554
555 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
556 if (uuid[i])
557 return 0;
558 }
559 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200560}
561
Miao Xied5c12072013-02-28 10:04:33 +0000562static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400563 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100564 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200565 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000566 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400567{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400568 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400569 struct btrfs_trans_handle *trans;
570 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100571 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400572 struct btrfs_inode_item *inode_item;
573 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000574 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400575 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000576 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700577 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900578 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400579 int ret;
580 int err;
581 u64 objectid;
582 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500583 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200584 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400585
David Sterba49a3c4d2016-03-24 17:49:22 +0100586 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
587 if (!root_item)
588 return -ENOMEM;
589
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400590 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400591 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100592 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000593
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800594 /*
595 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400596 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800597 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100598 if (btrfs_qgroup_level(objectid)) {
599 ret = -ENOSPC;
600 goto fail_free;
601 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800602
Miao Xied5c12072013-02-28 10:04:33 +0000603 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400604 /*
Miao Xied5c12072013-02-28 10:04:33 +0000605 * The same as the snapshot creation, please see the comment
606 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400607 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800608 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000609 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100610 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400611
Miao Xied5c12072013-02-28 10:04:33 +0000612 trans = btrfs_start_transaction(root, 0);
613 if (IS_ERR(trans)) {
614 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100615 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100616 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000617 }
618 trans->block_rsv = &block_rsv;
619 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400620
Lu Fengqia93774222018-07-18 14:45:41 +0800621 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200622 if (ret)
623 goto fail;
624
David Sterba4d75f8a2014-06-15 01:54:12 +0200625 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400626 if (IS_ERR(leaf)) {
627 ret = PTR_ERR(leaf);
628 goto fail;
629 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400630
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400631 btrfs_mark_buffer_dirty(leaf);
632
David Sterba49a3c4d2016-03-24 17:49:22 +0100633 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800634 btrfs_set_stack_inode_generation(inode_item, 1);
635 btrfs_set_stack_inode_size(inode_item, 3);
636 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400637 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400638 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800639 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400640
David Sterba49a3c4d2016-03-24 17:49:22 +0100641 btrfs_set_root_flags(root_item, 0);
642 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800643 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000644
David Sterba49a3c4d2016-03-24 17:49:22 +0100645 btrfs_set_root_bytenr(root_item, leaf->start);
646 btrfs_set_root_generation(root_item, trans->transid);
647 btrfs_set_root_level(root_item, 0);
648 btrfs_set_root_refs(root_item, 1);
649 btrfs_set_root_used(root_item, leaf->len);
650 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400651
David Sterba49a3c4d2016-03-24 17:49:22 +0100652 btrfs_set_root_generation_v2(root_item,
653 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200654 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100655 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
656 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
657 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
658 root_item->ctime = root_item->otime;
659 btrfs_set_root_ctransid(root_item, trans->transid);
660 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400661
Chris Mason925baed2008-06-25 16:01:30 -0400662 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400663 free_extent_buffer(leaf);
664 leaf = NULL;
665
David Sterba49a3c4d2016-03-24 17:49:22 +0100666 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400667
668 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400669 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200670 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400671 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100672 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400673 if (ret)
674 goto fail;
675
Yan, Zheng76dda932009-09-21 16:00:26 -0400676 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400677 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100678 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100679 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400680 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100681 goto fail;
682 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400683
684 btrfs_record_root_in_trans(trans, new_root);
685
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000686 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700687 if (ret) {
688 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400689 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700690 goto fail;
691 }
692
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530693 mutex_lock(&new_root->objectid_mutex);
694 new_root->highest_objectid = new_dirid;
695 mutex_unlock(&new_root->objectid_mutex);
696
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400697 /*
698 * insert the directory item
699 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200700 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100701 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400702 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100703 goto fail;
704 }
Chris Mason3de45862008-11-17 21:02:50 -0500705
Lu Fengqi684572d2018-08-04 21:10:57 +0800706 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500707 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100708 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400709 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400710 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100711 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500712
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200713 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500714 ret = btrfs_update_inode(trans, root, dir);
715 BUG_ON(ret);
716
Lu Fengqi6025c192018-08-01 11:32:29 +0800717 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200718 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500719 BUG_ON(ret);
720
Lu Fengqicdb345a2018-05-29 15:01:53 +0800721 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400722 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200723 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400724 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200725
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400726fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100727 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000728 trans->block_rsv = NULL;
729 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100730 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800731
Sage Weil72fd0322010-10-29 15:41:32 -0400732 if (async_transid) {
733 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400734 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000735 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400736 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400737 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400738 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400739 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400740 if (err && !ret)
741 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500742
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900743 if (!ret) {
744 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800745 if (IS_ERR(inode))
746 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900747 d_instantiate(dentry, inode);
748 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400749 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100750
751fail_free:
752 kfree(root_item);
753 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400754}
755
Miao Xiee9662f72013-02-28 10:01:15 +0000756static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100757 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000758 u64 *async_transid, bool readonly,
759 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400760{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400761 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000762 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400763 struct btrfs_pending_snapshot *pending_snapshot;
764 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000765 int ret;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800766 bool snapshot_force_cow = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400767
Miao Xie27cdeb72014-04-02 19:51:05 +0800768 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400769 return -EINVAL;
770
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700771 if (atomic_read(&root->nr_swapfiles)) {
772 btrfs_warn(fs_info,
773 "cannot snapshot subvolume with active swapfile");
774 return -ETXTBSY;
775 }
776
David Sterba23269bf2017-02-13 11:03:44 +0100777 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100778 if (!pending_snapshot)
779 return -ENOMEM;
780
David Sterbab0c0ea62015-11-10 18:54:00 +0100781 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100782 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100783 pending_snapshot->path = btrfs_alloc_path();
784 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100785 ret = -ENOMEM;
786 goto free_pending;
787 }
788
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800789 /*
790 * Force new buffered writes to reserve space even when NOCOW is
791 * possible. This is to avoid later writeback (running dealloc) to
792 * fallback to COW mode and unexpectedly fail with ENOSPC.
793 */
David Sterbaea14b57f2017-06-22 02:19:11 +0200794 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100795 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600796 /* wait for no snapshot writes */
797 wait_event(root->subv_writers->wait,
798 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800799
Ethan Lien3cd24c62018-11-01 14:49:03 +0800800 ret = btrfs_start_delalloc_snapshot(root);
Miao Xie6a038432013-05-15 07:48:24 +0000801 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100802 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000803
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800804 /*
805 * All previous writes have started writeback in NOCOW mode, so now
806 * we force future writes to fallback to COW mode during snapshot
807 * creation.
808 */
809 atomic_inc(&root->snapshot_force_cow);
810 snapshot_force_cow = true;
811
Chris Mason6374e57a2017-06-23 09:48:21 -0700812 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000813
Miao Xie66d8f3d2012-09-06 04:02:28 -0600814 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
815 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000816 /*
817 * 1 - parent dir inode
818 * 2 - dir entries
819 * 1 - root item
820 * 2 - root ref/backref
821 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200822 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000823 */
824 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200825 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400826 false);
Miao Xied5c12072013-02-28 10:04:33 +0000827 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100828 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000829
Yan, Zhenga22285a2010-05-16 10:48:46 -0400830 pending_snapshot->dentry = dentry;
831 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800832 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000833 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000834 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400835
Miao Xied5c12072013-02-28 10:04:33 +0000836 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400837 if (IS_ERR(trans)) {
838 ret = PTR_ERR(trans);
839 goto fail;
840 }
841
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400842 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400843 list_add(&pending_snapshot->list,
844 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400845 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400846 if (async_transid) {
847 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400848 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000849 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400850 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400851 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400852 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400853 }
Miao Xieaec80302013-03-04 09:44:29 +0000854 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400855 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400856
857 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400858 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000859 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400860
Chris Masond3797302014-10-15 13:50:56 -0700861 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
862 if (ret)
863 goto fail;
864
David Howells2b0143b2015-03-17 22:25:59 +0000865 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000866 if (IS_ERR(inode)) {
867 ret = PTR_ERR(inode);
868 goto fail;
869 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900870
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000871 d_instantiate(dentry, inode);
872 ret = 0;
873fail:
David Sterba7775c812017-02-10 19:18:18 +0100874 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100875dec_and_free:
Robbie Ko8ecebf4d2018-08-06 10:30:30 +0800876 if (snapshot_force_cow)
877 atomic_dec(&root->snapshot_force_cow);
David Sterbaea14b57f2017-06-22 02:19:11 +0200878 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100879 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100880free_pending:
881 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100882 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100883 kfree(pending_snapshot);
884
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400885 return ret;
886}
887
Sage Weil4260f7c2010-10-29 15:46:43 -0400888/* copy of may_delete in fs/namei.c()
889 * Check whether we can remove a link victim from directory dir, check
890 * whether the type of victim is right.
891 * 1. We can't do it if dir is read-only (done in permission())
892 * 2. We should have write and exec permissions on dir
893 * 3. We can't remove anything from append-only dir
894 * 4. We can't do anything with immutable dir (done in permission())
895 * 5. If the sticky bit on dir is set we should either
896 * a. be owner of dir, or
897 * b. be owner of victim, or
898 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400899 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400900 * links pointing to it.
901 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
902 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
903 * 9. We can't remove a root or mountpoint.
904 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
905 * nfs_async_unlink().
906 */
907
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530908static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400909{
910 int error;
911
David Howells2b0143b2015-03-17 22:25:59 +0000912 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400913 return -ENOENT;
914
David Howells2b0143b2015-03-17 22:25:59 +0000915 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400916 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400917
918 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
919 if (error)
920 return error;
921 if (IS_APPEND(dir))
922 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000923 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
924 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400925 return -EPERM;
926 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000927 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400928 return -ENOTDIR;
929 if (IS_ROOT(victim))
930 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000931 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400932 return -EISDIR;
933 if (IS_DEADDIR(dir))
934 return -ENOENT;
935 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
936 return -EBUSY;
937 return 0;
938}
939
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400940/* copy of may_create in fs/namei.c() */
941static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
942{
David Howells2b0143b2015-03-17 22:25:59 +0000943 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400944 return -EEXIST;
945 if (IS_DEADDIR(dir))
946 return -ENOENT;
947 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
948}
949
950/*
951 * Create a new subvolume below @parent. This is largely modeled after
952 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
953 * inside this filesystem so it's quite a bit simpler.
954 */
Al Viro92872092016-11-20 19:34:31 -0500955static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100956 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400957 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200958 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000959 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400960{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400961 struct inode *dir = d_inode(parent->dentry);
962 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400963 struct dentry *dentry;
964 int error;
965
Al Viro00235412016-05-26 00:05:12 -0400966 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
967 if (error == -EINTR)
968 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400969
970 dentry = lookup_one_len(name, parent->dentry, namelen);
971 error = PTR_ERR(dentry);
972 if (IS_ERR(dentry))
973 goto out_unlock;
974
Yan, Zheng76dda932009-09-21 16:00:26 -0400975 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400976 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600977 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400978
Chris Mason9c520572012-12-17 14:26:57 -0500979 /*
980 * even if this name doesn't exist, we may get hash collisions.
981 * check for them now when we can safely fail
982 */
983 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
984 dir->i_ino, name,
985 namelen);
986 if (error)
987 goto out_dput;
988
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400989 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400990
991 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
992 goto out_up_read;
993
Chris Mason3de45862008-11-17 21:02:50 -0500994 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100995 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200996 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500997 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000998 error = create_subvol(dir, dentry, name, namelen,
999 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001000 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001001 if (!error)
1002 fsnotify_mkdir(dir, dentry);
1003out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001004 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001005out_dput:
1006 dput(dentry);
1007out_unlock:
Al Viro59551022016-01-22 15:40:57 -05001008 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001009 return error;
1010}
1011
Chris Mason4cb53002011-05-24 15:35:30 -04001012/*
1013 * When we're defragging a range, we don't want to kick it off again
1014 * if it is really just waiting for delalloc to send it down.
1015 * If we find a nice big extent or delalloc range for the bytes in the
1016 * file you want to defrag, we return 0 to let you know to skip this
1017 * part of the file
1018 */
David Sterbaaab110a2014-07-29 17:32:10 +02001019static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001020{
1021 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1022 struct extent_map *em = NULL;
1023 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1024 u64 end;
1025
1026 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001027 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001028 read_unlock(&em_tree->lock);
1029
1030 if (em) {
1031 end = extent_map_end(em);
1032 free_extent_map(em);
1033 if (end - offset > thresh)
1034 return 0;
1035 }
1036 /* if we already have a nice delalloc here, just stop */
1037 thresh /= 2;
1038 end = count_range_bits(io_tree, &offset, offset + thresh,
1039 thresh, EXTENT_DELALLOC, 1);
1040 if (end >= thresh)
1041 return 0;
1042 return 1;
1043}
1044
1045/*
1046 * helper function to walk through a file and find extents
1047 * newer than a specific transid, and smaller than thresh.
1048 *
1049 * This is used by the defragging code to find new and small
1050 * extents
1051 */
1052static int find_new_extents(struct btrfs_root *root,
1053 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001054 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001055{
1056 struct btrfs_path *path;
1057 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001058 struct extent_buffer *leaf;
1059 struct btrfs_file_extent_item *extent;
1060 int type;
1061 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001062 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001063
1064 path = btrfs_alloc_path();
1065 if (!path)
1066 return -ENOMEM;
1067
David Sterbaa4689d22011-05-31 17:08:14 +00001068 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001069 min_key.type = BTRFS_EXTENT_DATA_KEY;
1070 min_key.offset = *off;
1071
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301072 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001073 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001074 if (ret != 0)
1075 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001076process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001077 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001078 goto none;
1079 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1080 goto none;
1081
1082 leaf = path->nodes[0];
1083 extent = btrfs_item_ptr(leaf, path->slots[0],
1084 struct btrfs_file_extent_item);
1085
1086 type = btrfs_file_extent_type(leaf, extent);
1087 if (type == BTRFS_FILE_EXTENT_REG &&
1088 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1089 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1090 *off = min_key.offset;
1091 btrfs_free_path(path);
1092 return 0;
1093 }
1094
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001095 path->slots[0]++;
1096 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1097 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1098 goto process_slot;
1099 }
1100
Chris Mason4cb53002011-05-24 15:35:30 -04001101 if (min_key.offset == (u64)-1)
1102 goto none;
1103
1104 min_key.offset++;
1105 btrfs_release_path(path);
1106 }
1107none:
1108 btrfs_free_path(path);
1109 return -ENOENT;
1110}
1111
Li Zefan6c282eb2012-06-11 16:03:35 +08001112static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001113{
1114 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001115 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001116 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001117 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001118
1119 /*
1120 * hopefully we have this extent in the tree already, try without
1121 * the full extent lock
1122 */
1123 read_lock(&em_tree->lock);
1124 em = lookup_extent_mapping(em_tree, start, len);
1125 read_unlock(&em_tree->lock);
1126
1127 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001128 struct extent_state *cached = NULL;
1129 u64 end = start + len - 1;
1130
Chris Mason940100a2010-03-10 10:52:59 -05001131 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001132 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02001133 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001134 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001135
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001136 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001137 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001138 }
1139
Li Zefan6c282eb2012-06-11 16:03:35 +08001140 return em;
1141}
1142
1143static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1144{
1145 struct extent_map *next;
1146 bool ret = true;
1147
1148 /* this is the last extent */
1149 if (em->start + em->len >= i_size_read(inode))
1150 return false;
1151
1152 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001153 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1154 ret = false;
1155 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001156 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001157 ret = false;
1158
1159 free_extent_map(next);
1160 return ret;
1161}
1162
David Sterbaaab110a2014-07-29 17:32:10 +02001163static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001164 u64 *last_len, u64 *skip, u64 *defrag_end,
1165 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001166{
1167 struct extent_map *em;
1168 int ret = 1;
1169 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001170 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001171
1172 /*
1173 * make sure that once we start defragging an extent, we keep on
1174 * defragging it
1175 */
1176 if (start < *defrag_end)
1177 return 1;
1178
1179 *skip = 0;
1180
1181 em = defrag_lookup_extent(inode, start);
1182 if (!em)
1183 return 0;
1184
Chris Mason940100a2010-03-10 10:52:59 -05001185 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001186 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001187 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001188 goto out;
1189 }
1190
Liu Bo4a3560c2015-08-07 16:48:41 +08001191 if (!*defrag_end)
1192 prev_mergeable = false;
1193
Li Zefan6c282eb2012-06-11 16:03:35 +08001194 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001195 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001196 * we hit a real extent, if it is big or the next extent is not a
1197 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001198 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001199 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001200 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001201 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001202out:
Chris Mason940100a2010-03-10 10:52:59 -05001203 /*
1204 * last_len ends up being a counter of how many bytes we've defragged.
1205 * every time we choose not to defrag an extent, we reset *last_len
1206 * so that the next tiny extent will force a defrag.
1207 *
1208 * The end result of this is that tiny extents before a single big
1209 * extent will force at least part of that big extent to be defragged.
1210 */
1211 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001212 *defrag_end = extent_map_end(em);
1213 } else {
1214 *last_len = 0;
1215 *skip = extent_map_end(em);
1216 *defrag_end = 0;
1217 }
1218
1219 free_extent_map(em);
1220 return ret;
1221}
1222
Chris Mason4cb53002011-05-24 15:35:30 -04001223/*
1224 * it doesn't do much good to defrag one or two pages
1225 * at a time. This pulls in a nice chunk of pages
1226 * to COW and defrag.
1227 *
1228 * It also makes sure the delalloc code has enough
1229 * dirty data to avoid making new small extents as part
1230 * of the defrag
1231 *
1232 * It's a good idea to start RA on this range
1233 * before calling this.
1234 */
1235static int cluster_pages_for_defrag(struct inode *inode,
1236 struct page **pages,
1237 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001238 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001239{
Chris Mason4cb53002011-05-24 15:35:30 -04001240 unsigned long file_end;
1241 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001242 u64 page_start;
1243 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001244 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001245 int ret;
1246 int i;
1247 int i_done;
1248 struct btrfs_ordered_extent *ordered;
1249 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001250 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001251 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001252 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001253
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001254 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001255 if (!isize || start_index > file_end)
1256 return 0;
1257
1258 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001259
Qu Wenruo364ecf32017-02-27 15:10:38 +08001260 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001261 start_index << PAGE_SHIFT,
1262 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001263 if (ret)
1264 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001265 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001266 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001267
1268 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001269 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001270 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001271again:
Josef Bacika94733d2011-07-11 10:47:06 -04001272 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001273 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001274 if (!page)
1275 break;
1276
Miao Xie600a45e2012-02-16 15:01:24 +08001277 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001278 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001279 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001280 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001281 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001282 ordered = btrfs_lookup_ordered_extent(inode,
1283 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001284 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001285 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001286 if (!ordered)
1287 break;
1288
1289 unlock_page(page);
1290 btrfs_start_ordered_extent(inode, ordered, 1);
1291 btrfs_put_ordered_extent(ordered);
1292 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001293 /*
1294 * we unlocked the page above, so we need check if
1295 * it was released or not.
1296 */
1297 if (page->mapping != inode->i_mapping) {
1298 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001299 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001300 goto again;
1301 }
Miao Xie600a45e2012-02-16 15:01:24 +08001302 }
1303
Chris Mason4cb53002011-05-24 15:35:30 -04001304 if (!PageUptodate(page)) {
1305 btrfs_readpage(NULL, page);
1306 lock_page(page);
1307 if (!PageUptodate(page)) {
1308 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001309 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001310 ret = -EIO;
1311 break;
1312 }
1313 }
Miao Xie600a45e2012-02-16 15:01:24 +08001314
Miao Xie600a45e2012-02-16 15:01:24 +08001315 if (page->mapping != inode->i_mapping) {
1316 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001317 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001318 goto again;
1319 }
1320
Chris Mason4cb53002011-05-24 15:35:30 -04001321 pages[i] = page;
1322 i_done++;
1323 }
1324 if (!i_done || ret)
1325 goto out;
1326
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001327 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001328 goto out;
1329
1330 /*
1331 * so now we have a nice long stream of locked
1332 * and up to date pages, lets wait on them
1333 */
1334 for (i = 0; i < i_done; i++)
1335 wait_on_page_writeback(pages[i]);
1336
1337 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001338 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001339
1340 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001341 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001342 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1343 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001344 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001345 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001346
Liu Bo1f12bd02012-03-29 09:57:44 -04001347 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001348 spin_lock(&BTRFS_I(inode)->lock);
Su Yue28c4a3e2018-09-05 11:07:33 +08001349 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001350 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001351 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001352 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001353 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001354 }
1355
1356
Liu Bo9e8a4a82012-09-05 19:10:51 -06001357 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001358 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001359
1360 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001361 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001362
1363 for (i = 0; i < i_done; i++) {
1364 clear_page_dirty_for_io(pages[i]);
1365 ClearPageChecked(pages[i]);
1366 set_page_extent_mapped(pages[i]);
1367 set_page_dirty(pages[i]);
1368 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001369 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001370 }
Qu Wenruo43b18592017-12-12 15:34:32 +08001371 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1372 false);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001373 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001374 return i_done;
1375out:
1376 for (i = 0; i < i_done; i++) {
1377 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001378 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001379 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001380 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001381 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001382 page_cnt << PAGE_SHIFT, true);
1383 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1384 true);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001385 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001386 return ret;
1387
1388}
1389
1390int btrfs_defrag_file(struct inode *inode, struct file *file,
1391 struct btrfs_ioctl_defrag_range_args *range,
1392 u64 newer_than, unsigned long max_to_defrag)
1393{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001394 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001395 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001396 struct file_ra_state *ra = NULL;
1397 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001398 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001399 u64 last_len = 0;
1400 u64 skip = 0;
1401 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001402 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001403 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001404 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001405 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001406 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001407 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001408 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001409 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001410 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001411 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001412 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001413 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001414
Liu Bo0abd5b12013-04-16 09:20:28 +00001415 if (isize == 0)
1416 return 0;
1417
1418 if (range->start >= isize)
1419 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001420
David Sterba1e2ef462017-07-17 20:01:59 +02001421 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001422 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1423 return -EINVAL;
1424 if (range->compress_type)
1425 compress_type = range->compress_type;
1426 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001427
Liu Bo0abd5b12013-04-16 09:20:28 +00001428 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001429 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001430
Chris Mason4cb53002011-05-24 15:35:30 -04001431 /*
David Sterba0a52d102017-06-22 03:22:58 +02001432 * If we were not given a file, allocate a readahead context. As
1433 * readahead is just an optimization, defrag will work without it so
1434 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001435 */
1436 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001437 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001438 if (ra)
1439 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001440 } else {
1441 ra = &file->f_ra;
1442 }
1443
David Sterba63e727e2017-06-22 03:13:02 +02001444 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001445 if (!pages) {
1446 ret = -ENOMEM;
1447 goto out_ra;
1448 }
1449
1450 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001451 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001452 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001453 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001454 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001455 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001456 }
1457
Chris Mason4cb53002011-05-24 15:35:30 -04001458 if (newer_than) {
1459 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001460 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001461 if (!ret) {
1462 range->start = newer_off;
1463 /*
1464 * we always align our defrag to help keep
1465 * the extents in the file evenly spaced
1466 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001467 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001468 } else
1469 goto out_ra;
1470 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001471 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001472 }
1473 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301474 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001475
Li Zefan2a0f7f52011-10-10 15:43:34 -04001476 /*
1477 * make writeback starts from i, so the defrag range can be
1478 * written sequentially.
1479 */
1480 if (i < inode->i_mapping->writeback_index)
1481 inode->i_mapping->writeback_index = i;
1482
Chris Masonf7f43cc2011-10-11 11:41:40 -04001483 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001484 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001485 /*
1486 * make sure we stop running if someone unmounts
1487 * the FS
1488 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001489 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001490 break;
1491
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001492 if (btrfs_defrag_cancelled(fs_info)) {
1493 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001494 ret = -EAGAIN;
1495 break;
1496 }
1497
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001498 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001499 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001500 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001501 unsigned long next;
1502 /*
1503 * the should_defrag function tells us how much to skip
1504 * bump our counter by the suggested amount
1505 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001506 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001507 i = max(i + 1, next);
1508 continue;
1509 }
Li Zefan008873e2011-09-02 15:57:07 +08001510
1511 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001512 cluster = (PAGE_ALIGN(defrag_end) >>
1513 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001514 cluster = min(cluster, max_cluster);
1515 } else {
1516 cluster = max_cluster;
1517 }
1518
Li Zefan008873e2011-09-02 15:57:07 +08001519 if (i + cluster > ra_index) {
1520 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001521 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001522 page_cache_sync_readahead(inode->i_mapping, ra,
1523 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301524 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001525 }
Chris Mason940100a2010-03-10 10:52:59 -05001526
Al Viro59551022016-01-22 15:40:57 -05001527 inode_lock(inode);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001528 if (IS_SWAPFILE(inode)) {
1529 ret = -ETXTBSY;
1530 } else {
1531 if (do_compress)
1532 BTRFS_I(inode)->defrag_compress = compress_type;
1533 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1534 }
Liu Boecb8bea2012-03-29 09:57:44 -04001535 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001536 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001537 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001538 }
Chris Mason940100a2010-03-10 10:52:59 -05001539
Chris Mason4cb53002011-05-24 15:35:30 -04001540 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001541 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001542 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001543
1544 if (newer_than) {
1545 if (newer_off == (u64)-1)
1546 break;
1547
Liu Boe1f041e2012-03-29 09:57:45 -04001548 if (ret > 0)
1549 i += ret;
1550
Chris Mason4cb53002011-05-24 15:35:30 -04001551 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001552 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001553
Byongho Leeee221842015-12-15 01:42:10 +09001554 ret = find_new_extents(root, inode, newer_than,
1555 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001556 if (!ret) {
1557 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001558 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001559 } else {
1560 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001561 }
Chris Mason4cb53002011-05-24 15:35:30 -04001562 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001563 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001564 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001565 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001566 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001567 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001568 last_len = 0;
1569 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001570 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001571 }
1572
Filipe Mananadec8ef92014-03-01 10:55:54 +00001573 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001574 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001575 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1576 &BTRFS_I(inode)->runtime_flags))
1577 filemap_flush(inode->i_mapping);
1578 }
Chris Mason1e701a32010-03-11 09:42:04 -05001579
Li Zefan1a419d82010-10-25 15:12:50 +08001580 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001581 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001582 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1583 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001584 }
1585
Diego Calleja60ccf822011-09-01 16:33:57 +02001586 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001587
Chris Mason4cb53002011-05-24 15:35:30 -04001588out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001589 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001590 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001591 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001592 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001593 }
Chris Mason4cb53002011-05-24 15:35:30 -04001594 if (!file)
1595 kfree(ra);
1596 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001597 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001598}
1599
Miao Xie198605a2012-11-26 08:43:45 +00001600static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001601 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001602{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001603 struct inode *inode = file_inode(file);
1604 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001605 u64 new_size;
1606 u64 old_size;
1607 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001608 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001609 struct btrfs_ioctl_vol_args *vol_args;
1610 struct btrfs_trans_handle *trans;
1611 struct btrfs_device *device = NULL;
1612 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001613 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001614 char *devstr = NULL;
1615 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001616 int mod = 0;
1617
Chris Masone441d542009-01-05 16:57:23 -05001618 if (!capable(CAP_SYS_ADMIN))
1619 return -EPERM;
1620
Miao Xie198605a2012-11-26 08:43:45 +00001621 ret = mnt_want_write_file(file);
1622 if (ret)
1623 return ret;
1624
David Sterba171938e2017-03-28 14:44:21 +02001625 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001626 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001627 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001628 }
1629
Li Zefandae7b662009-04-08 15:06:54 +08001630 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001631 if (IS_ERR(vol_args)) {
1632 ret = PTR_ERR(vol_args);
1633 goto out;
1634 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001635
1636 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001637
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001638 sizestr = vol_args->name;
1639 devstr = strchr(sizestr, ':');
1640 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001641 sizestr = devstr + 1;
1642 *devstr = '\0';
1643 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001644 ret = kstrtoull(devstr, 10, &devid);
1645 if (ret)
1646 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001647 if (!devid) {
1648 ret = -EINVAL;
1649 goto out_free;
1650 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001651 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001652 }
Miao Xiedba60f32012-12-21 09:19:51 +00001653
Anand Jain09ba3bc2019-01-19 14:48:55 +08001654 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001655 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001656 btrfs_info(fs_info, "resizer unable to find device %llu",
1657 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001658 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001659 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001660 }
Miao Xiedba60f32012-12-21 09:19:51 +00001661
Anand Jainebbede42017-12-04 12:54:52 +08001662 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001663 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001664 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001665 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001666 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001667 goto out_free;
1668 }
1669
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001670 if (!strcmp(sizestr, "max"))
1671 new_size = device->bdev->bd_inode->i_size;
1672 else {
1673 if (sizestr[0] == '-') {
1674 mod = -1;
1675 sizestr++;
1676 } else if (sizestr[0] == '+') {
1677 mod = 1;
1678 sizestr++;
1679 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001680 new_size = memparse(sizestr, &retptr);
1681 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001682 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001683 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001684 }
1685 }
1686
Anand Jain401e29c2017-12-04 12:54:55 +08001687 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001688 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001689 goto out_free;
1690 }
1691
Miao Xie7cc8e582014-09-03 21:35:38 +08001692 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001693
1694 if (mod < 0) {
1695 if (new_size > old_size) {
1696 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001697 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001698 }
1699 new_size = old_size - new_size;
1700 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001701 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001702 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001703 goto out_free;
1704 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001705 new_size = old_size + new_size;
1706 }
1707
Byongho Leeee221842015-12-15 01:42:10 +09001708 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001709 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001710 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001711 }
1712 if (new_size > device->bdev->bd_inode->i_size) {
1713 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001714 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001715 }
1716
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001717 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001718
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001719 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1720 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001721
1722 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001723 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001724 if (IS_ERR(trans)) {
1725 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001726 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001727 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001728 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001729 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001730 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001731 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001732 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001733
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001734out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001735 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001736out:
David Sterba171938e2017-03-28 14:44:21 +02001737 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001738 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001739 return ret;
1740}
1741
Sage Weil72fd0322010-10-29 15:41:32 -04001742static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001743 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001744 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001745 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001746{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001747 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001748 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001749
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001750 if (!S_ISDIR(file_inode(file)->i_mode))
1751 return -ENOTDIR;
1752
Liu Boa874a632012-06-29 03:58:46 -06001753 ret = mnt_want_write_file(file);
1754 if (ret)
1755 goto out;
1756
Sage Weil72fd0322010-10-29 15:41:32 -04001757 namelen = strlen(name);
1758 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001759 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001760 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001761 }
1762
Chris Mason16780ca2012-02-20 22:14:55 -05001763 if (name[0] == '.' &&
1764 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1765 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001766 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001767 }
1768
Chris Mason3de45862008-11-17 21:02:50 -05001769 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001770 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001771 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001772 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001773 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001774 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001775 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001776 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001777 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001778 }
1779
Al Viro496ad9a2013-01-23 17:07:38 -05001780 src_inode = file_inode(src.file);
1781 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001782 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001783 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001784 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001785 } else if (!inode_owner_or_capable(src_inode)) {
1786 /*
1787 * Subvolume creation is not restricted, but snapshots
1788 * are limited to own subvolumes only
1789 */
1790 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001791 } else {
1792 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1793 BTRFS_I(src_inode)->root,
1794 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001795 }
Al Viro2903ff02012-08-28 12:52:22 -04001796 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001797 }
Liu Boa874a632012-06-29 03:58:46 -06001798out_drop_write:
1799 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001800out:
Sage Weil72fd0322010-10-29 15:41:32 -04001801 return ret;
1802}
1803
1804static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001805 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001806{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001807 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001808 int ret;
1809
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001810 if (!S_ISDIR(file_inode(file)->i_mode))
1811 return -ENOTDIR;
1812
Li Zefanfa0d2b92010-12-20 15:53:28 +08001813 vol_args = memdup_user(arg, sizeof(*vol_args));
1814 if (IS_ERR(vol_args))
1815 return PTR_ERR(vol_args);
1816 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001817
Li Zefanfa0d2b92010-12-20 15:53:28 +08001818 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001819 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001820 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001821
Li Zefanfa0d2b92010-12-20 15:53:28 +08001822 kfree(vol_args);
1823 return ret;
1824}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001825
Li Zefanfa0d2b92010-12-20 15:53:28 +08001826static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1827 void __user *arg, int subvol)
1828{
1829 struct btrfs_ioctl_vol_args_v2 *vol_args;
1830 int ret;
1831 u64 transid = 0;
1832 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001833 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001834 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001835
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001836 if (!S_ISDIR(file_inode(file)->i_mode))
1837 return -ENOTDIR;
1838
Li Zefanfa0d2b92010-12-20 15:53:28 +08001839 vol_args = memdup_user(arg, sizeof(*vol_args));
1840 if (IS_ERR(vol_args))
1841 return PTR_ERR(vol_args);
1842 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001843
Li Zefanb83cc962010-12-20 16:04:08 +08001844 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001845 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1846 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001847 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001848 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001849 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001850
1851 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1852 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001853 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1854 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001855 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001856 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001857 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001858 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001859 }
1860 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1861 if (IS_ERR(inherit)) {
1862 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001863 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001864 }
1865 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001866
1867 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001868 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001869 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001870 if (ret)
1871 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001872
Dan Carpenterc47ca322014-09-04 14:09:15 +03001873 if (ptr && copy_to_user(arg +
1874 offsetof(struct btrfs_ioctl_vol_args_v2,
1875 transid),
1876 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001877 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001878
1879free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001880 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001881free_args:
1882 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001883 return ret;
1884}
1885
Li Zefan0caa1022010-12-20 16:30:25 +08001886static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1887 void __user *arg)
1888{
Al Viro496ad9a2013-01-23 17:07:38 -05001889 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001890 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001891 struct btrfs_root *root = BTRFS_I(inode)->root;
1892 int ret = 0;
1893 u64 flags = 0;
1894
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001895 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001896 return -EINVAL;
1897
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001898 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001899 if (btrfs_root_readonly(root))
1900 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001901 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001902
1903 if (copy_to_user(arg, &flags, sizeof(flags)))
1904 ret = -EFAULT;
1905
1906 return ret;
1907}
1908
1909static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1910 void __user *arg)
1911{
Al Viro496ad9a2013-01-23 17:07:38 -05001912 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001913 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001914 struct btrfs_root *root = BTRFS_I(inode)->root;
1915 struct btrfs_trans_handle *trans;
1916 u64 root_flags;
1917 u64 flags;
1918 int ret = 0;
1919
David Sterbabd60ea02014-01-16 15:50:22 +01001920 if (!inode_owner_or_capable(inode))
1921 return -EPERM;
1922
Liu Bob9ca0662012-06-29 03:58:49 -06001923 ret = mnt_want_write_file(file);
1924 if (ret)
1925 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001926
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001927 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001928 ret = -EINVAL;
1929 goto out_drop_write;
1930 }
Li Zefan0caa1022010-12-20 16:30:25 +08001931
Liu Bob9ca0662012-06-29 03:58:49 -06001932 if (copy_from_user(&flags, arg, sizeof(flags))) {
1933 ret = -EFAULT;
1934 goto out_drop_write;
1935 }
Li Zefan0caa1022010-12-20 16:30:25 +08001936
Liu Bob9ca0662012-06-29 03:58:49 -06001937 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1938 ret = -EINVAL;
1939 goto out_drop_write;
1940 }
Li Zefan0caa1022010-12-20 16:30:25 +08001941
Liu Bob9ca0662012-06-29 03:58:49 -06001942 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1943 ret = -EOPNOTSUPP;
1944 goto out_drop_write;
1945 }
Li Zefan0caa1022010-12-20 16:30:25 +08001946
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001947 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001948
1949 /* nothing to do */
1950 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001951 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001952
1953 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001954 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001955 btrfs_set_root_flags(&root->root_item,
1956 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001957 } else {
1958 /*
1959 * Block RO -> RW transition if this subvolume is involved in
1960 * send
1961 */
1962 spin_lock(&root->root_item_lock);
1963 if (root->send_in_progress == 0) {
1964 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001965 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001966 spin_unlock(&root->root_item_lock);
1967 } else {
1968 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001969 btrfs_warn(fs_info,
1970 "Attempt to set subvolume %llu read-write during send",
1971 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001972 ret = -EPERM;
1973 goto out_drop_sem;
1974 }
1975 }
Li Zefan0caa1022010-12-20 16:30:25 +08001976
1977 trans = btrfs_start_transaction(root, 1);
1978 if (IS_ERR(trans)) {
1979 ret = PTR_ERR(trans);
1980 goto out_reset;
1981 }
1982
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001983 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001984 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001985 if (ret < 0) {
1986 btrfs_end_transaction(trans);
1987 goto out_reset;
1988 }
Li Zefan0caa1022010-12-20 16:30:25 +08001989
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001990 ret = btrfs_commit_transaction(trans);
1991
Li Zefan0caa1022010-12-20 16:30:25 +08001992out_reset:
1993 if (ret)
1994 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001995out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001996 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001997out_drop_write:
1998 mnt_drop_write_file(file);
1999out:
Li Zefan0caa1022010-12-20 16:30:25 +08002000 return ret;
2001}
2002
Chris Masonac8e9812010-02-28 15:39:26 -05002003static noinline int key_in_sk(struct btrfs_key *key,
2004 struct btrfs_ioctl_search_key *sk)
2005{
Chris Masonabc6e132010-03-18 12:10:08 -04002006 struct btrfs_key test;
2007 int ret;
2008
2009 test.objectid = sk->min_objectid;
2010 test.type = sk->min_type;
2011 test.offset = sk->min_offset;
2012
2013 ret = btrfs_comp_cpu_keys(key, &test);
2014 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002015 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002016
2017 test.objectid = sk->max_objectid;
2018 test.type = sk->max_type;
2019 test.offset = sk->max_offset;
2020
2021 ret = btrfs_comp_cpu_keys(key, &test);
2022 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002023 return 0;
2024 return 1;
2025}
2026
Jeff Mahoneydf397562016-06-21 20:18:21 -04002027static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002028 struct btrfs_key *key,
2029 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002030 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002031 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002032 unsigned long *sk_offset,
2033 int *num_found)
2034{
2035 u64 found_transid;
2036 struct extent_buffer *leaf;
2037 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002038 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002039 unsigned long item_off;
2040 unsigned long item_len;
2041 int nritems;
2042 int i;
2043 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002044 int ret = 0;
2045
2046 leaf = path->nodes[0];
2047 slot = path->slots[0];
2048 nritems = btrfs_header_nritems(leaf);
2049
2050 if (btrfs_header_generation(leaf) > sk->max_transid) {
2051 i = nritems;
2052 goto advance_key;
2053 }
2054 found_transid = btrfs_header_generation(leaf);
2055
2056 for (i = slot; i < nritems; i++) {
2057 item_off = btrfs_item_ptr_offset(leaf, i);
2058 item_len = btrfs_item_size_nr(leaf, i);
2059
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002060 btrfs_item_key_to_cpu(leaf, key, i);
2061 if (!key_in_sk(key, sk))
2062 continue;
2063
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002064 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002065 if (*num_found) {
2066 ret = 1;
2067 goto out;
2068 }
Chris Masonac8e9812010-02-28 15:39:26 -05002069
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002070 /*
2071 * return one empty item back for v1, which does not
2072 * handle -EOVERFLOW
2073 */
2074
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002075 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002076 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002077 ret = -EOVERFLOW;
2078 }
Chris Masonac8e9812010-02-28 15:39:26 -05002079
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002080 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002081 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002082 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002083 }
2084
Chris Masonac8e9812010-02-28 15:39:26 -05002085 sh.objectid = key->objectid;
2086 sh.offset = key->offset;
2087 sh.type = key->type;
2088 sh.len = item_len;
2089 sh.transid = found_transid;
2090
2091 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002092 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2093 ret = -EFAULT;
2094 goto out;
2095 }
2096
Chris Masonac8e9812010-02-28 15:39:26 -05002097 *sk_offset += sizeof(sh);
2098
2099 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002100 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002101 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002102 if (read_extent_buffer_to_user(leaf, up,
2103 item_off, item_len)) {
2104 ret = -EFAULT;
2105 goto out;
2106 }
2107
Chris Masonac8e9812010-02-28 15:39:26 -05002108 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002109 }
Hugo Millse2156862011-05-14 17:43:41 +00002110 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002111
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002112 if (ret) /* -EOVERFLOW from above */
2113 goto out;
2114
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002115 if (*num_found >= sk->nr_items) {
2116 ret = 1;
2117 goto out;
2118 }
Chris Masonac8e9812010-02-28 15:39:26 -05002119 }
2120advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002121 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002122 test.objectid = sk->max_objectid;
2123 test.type = sk->max_type;
2124 test.offset = sk->max_offset;
2125 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2126 ret = 1;
2127 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002128 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002129 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002130 key->offset = 0;
2131 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002132 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002133 key->offset = 0;
2134 key->type = 0;
2135 key->objectid++;
2136 } else
2137 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002138out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002139 /*
2140 * 0: all items from this leaf copied, continue with next
2141 * 1: * more items can be copied, but unused buffer is too small
2142 * * all items were found
2143 * Either way, it will stops the loop which iterates to the next
2144 * leaf
2145 * -EOVERFLOW: item was to large for buffer
2146 * -EFAULT: could not copy extent buffer back to userspace
2147 */
Chris Masonac8e9812010-02-28 15:39:26 -05002148 return ret;
2149}
2150
2151static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002152 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002153 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002154 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002155{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002156 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002157 struct btrfs_root *root;
2158 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002159 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002160 int ret;
2161 int num_found = 0;
2162 unsigned long sk_offset = 0;
2163
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002164 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2165 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002166 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002167 }
Gerhard Heift12544442014-01-30 16:23:58 +01002168
Chris Masonac8e9812010-02-28 15:39:26 -05002169 path = btrfs_alloc_path();
2170 if (!path)
2171 return -ENOMEM;
2172
2173 if (sk->tree_id == 0) {
2174 /* search the root of the inode that was passed */
2175 root = BTRFS_I(inode)->root;
2176 } else {
2177 key.objectid = sk->tree_id;
2178 key.type = BTRFS_ROOT_ITEM_KEY;
2179 key.offset = (u64)-1;
2180 root = btrfs_read_fs_root_no_name(info, &key);
2181 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002182 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002183 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002184 }
2185 }
2186
2187 key.objectid = sk->min_objectid;
2188 key.type = sk->min_type;
2189 key.offset = sk->min_offset;
2190
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302191 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002192 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002193 if (ret != 0) {
2194 if (ret > 0)
2195 ret = 0;
2196 goto err;
2197 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002198 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002199 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002200 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002201 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002202 break;
2203
2204 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002205 if (ret > 0)
2206 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002207err:
2208 sk->nr_items = num_found;
2209 btrfs_free_path(path);
2210 return ret;
2211}
2212
2213static noinline int btrfs_ioctl_tree_search(struct file *file,
2214 void __user *argp)
2215{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002216 struct btrfs_ioctl_search_args __user *uargs;
2217 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002218 struct inode *inode;
2219 int ret;
2220 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002221
2222 if (!capable(CAP_SYS_ADMIN))
2223 return -EPERM;
2224
Gerhard Heiftba346b32014-01-30 16:24:02 +01002225 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002226
Gerhard Heiftba346b32014-01-30 16:24:02 +01002227 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2228 return -EFAULT;
2229
2230 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002231
Al Viro496ad9a2013-01-23 17:07:38 -05002232 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002233 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002234
2235 /*
2236 * In the origin implementation an overflow is handled by returning a
2237 * search header with a len of zero, so reset ret.
2238 */
2239 if (ret == -EOVERFLOW)
2240 ret = 0;
2241
Gerhard Heiftba346b32014-01-30 16:24:02 +01002242 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002243 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002244 return ret;
2245}
2246
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002247static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2248 void __user *argp)
2249{
2250 struct btrfs_ioctl_search_args_v2 __user *uarg;
2251 struct btrfs_ioctl_search_args_v2 args;
2252 struct inode *inode;
2253 int ret;
2254 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002255 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002256
2257 if (!capable(CAP_SYS_ADMIN))
2258 return -EPERM;
2259
2260 /* copy search header and buffer size */
2261 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2262 if (copy_from_user(&args, uarg, sizeof(args)))
2263 return -EFAULT;
2264
2265 buf_size = args.buf_size;
2266
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002267 /* limit result size to 16MB */
2268 if (buf_size > buf_limit)
2269 buf_size = buf_limit;
2270
2271 inode = file_inode(file);
2272 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002273 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002274 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2275 ret = -EFAULT;
2276 else if (ret == -EOVERFLOW &&
2277 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2278 ret = -EFAULT;
2279
Yan, Zheng76dda932009-09-21 16:00:26 -04002280 return ret;
2281}
2282
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002283/*
Chris Masonac8e9812010-02-28 15:39:26 -05002284 * Search INODE_REFs to identify path name of 'dirid' directory
2285 * in a 'tree_id' tree. and sets path name to 'name'.
2286 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002287static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2288 u64 tree_id, u64 dirid, char *name)
2289{
2290 struct btrfs_root *root;
2291 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002292 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002293 int ret = -1;
2294 int slot;
2295 int len;
2296 int total_len = 0;
2297 struct btrfs_inode_ref *iref;
2298 struct extent_buffer *l;
2299 struct btrfs_path *path;
2300
2301 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2302 name[0]='\0';
2303 return 0;
2304 }
2305
2306 path = btrfs_alloc_path();
2307 if (!path)
2308 return -ENOMEM;
2309
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002310 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002311
2312 key.objectid = tree_id;
2313 key.type = BTRFS_ROOT_ITEM_KEY;
2314 key.offset = (u64)-1;
2315 root = btrfs_read_fs_root_no_name(info, &key);
2316 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002317 ret = PTR_ERR(root);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002318 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002319 }
2320
2321 key.objectid = dirid;
2322 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002323 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002324
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302325 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002326 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2327 if (ret < 0)
2328 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002329 else if (ret > 0) {
2330 ret = btrfs_previous_item(root, path, dirid,
2331 BTRFS_INODE_REF_KEY);
2332 if (ret < 0)
2333 goto out;
2334 else if (ret > 0) {
2335 ret = -ENOENT;
2336 goto out;
2337 }
2338 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002339
2340 l = path->nodes[0];
2341 slot = path->slots[0];
2342 btrfs_item_key_to_cpu(l, &key, slot);
2343
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002344 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2345 len = btrfs_inode_ref_name_len(l, iref);
2346 ptr -= len + 1;
2347 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002348 if (ptr < name) {
2349 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002350 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002351 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002352
2353 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302354 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002355
2356 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2357 break;
2358
David Sterbab3b4aa72011-04-21 01:20:15 +02002359 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002360 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002361 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002362 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002363 }
Li Zefan77906a502011-07-14 03:16:00 +00002364 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302365 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002366 ret = 0;
2367out:
2368 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002369 return ret;
2370}
2371
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002372static int btrfs_search_path_in_tree_user(struct inode *inode,
2373 struct btrfs_ioctl_ino_lookup_user_args *args)
2374{
2375 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2376 struct super_block *sb = inode->i_sb;
2377 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2378 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2379 u64 dirid = args->dirid;
2380 unsigned long item_off;
2381 unsigned long item_len;
2382 struct btrfs_inode_ref *iref;
2383 struct btrfs_root_ref *rref;
2384 struct btrfs_root *root;
2385 struct btrfs_path *path;
2386 struct btrfs_key key, key2;
2387 struct extent_buffer *leaf;
2388 struct inode *temp_inode;
2389 char *ptr;
2390 int slot;
2391 int len;
2392 int total_len = 0;
2393 int ret;
2394
2395 path = btrfs_alloc_path();
2396 if (!path)
2397 return -ENOMEM;
2398
2399 /*
2400 * If the bottom subvolume does not exist directly under upper_limit,
2401 * construct the path in from the bottom up.
2402 */
2403 if (dirid != upper_limit.objectid) {
2404 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2405
2406 key.objectid = treeid;
2407 key.type = BTRFS_ROOT_ITEM_KEY;
2408 key.offset = (u64)-1;
2409 root = btrfs_read_fs_root_no_name(fs_info, &key);
2410 if (IS_ERR(root)) {
2411 ret = PTR_ERR(root);
2412 goto out;
2413 }
2414
2415 key.objectid = dirid;
2416 key.type = BTRFS_INODE_REF_KEY;
2417 key.offset = (u64)-1;
2418 while (1) {
2419 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2420 if (ret < 0) {
2421 goto out;
2422 } else if (ret > 0) {
2423 ret = btrfs_previous_item(root, path, dirid,
2424 BTRFS_INODE_REF_KEY);
2425 if (ret < 0) {
2426 goto out;
2427 } else if (ret > 0) {
2428 ret = -ENOENT;
2429 goto out;
2430 }
2431 }
2432
2433 leaf = path->nodes[0];
2434 slot = path->slots[0];
2435 btrfs_item_key_to_cpu(leaf, &key, slot);
2436
2437 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2438 len = btrfs_inode_ref_name_len(leaf, iref);
2439 ptr -= len + 1;
2440 total_len += len + 1;
2441 if (ptr < args->path) {
2442 ret = -ENAMETOOLONG;
2443 goto out;
2444 }
2445
2446 *(ptr + len) = '/';
2447 read_extent_buffer(leaf, ptr,
2448 (unsigned long)(iref + 1), len);
2449
2450 /* Check the read+exec permission of this directory */
2451 ret = btrfs_previous_item(root, path, dirid,
2452 BTRFS_INODE_ITEM_KEY);
2453 if (ret < 0) {
2454 goto out;
2455 } else if (ret > 0) {
2456 ret = -ENOENT;
2457 goto out;
2458 }
2459
2460 leaf = path->nodes[0];
2461 slot = path->slots[0];
2462 btrfs_item_key_to_cpu(leaf, &key2, slot);
2463 if (key2.objectid != dirid) {
2464 ret = -ENOENT;
2465 goto out;
2466 }
2467
2468 temp_inode = btrfs_iget(sb, &key2, root, NULL);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002469 if (IS_ERR(temp_inode)) {
2470 ret = PTR_ERR(temp_inode);
2471 goto out;
2472 }
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002473 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2474 iput(temp_inode);
2475 if (ret) {
2476 ret = -EACCES;
2477 goto out;
2478 }
2479
2480 if (key.offset == upper_limit.objectid)
2481 break;
2482 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2483 ret = -EACCES;
2484 goto out;
2485 }
2486
2487 btrfs_release_path(path);
2488 key.objectid = key.offset;
2489 key.offset = (u64)-1;
2490 dirid = key.objectid;
2491 }
2492
2493 memmove(args->path, ptr, total_len);
2494 args->path[total_len] = '\0';
2495 btrfs_release_path(path);
2496 }
2497
2498 /* Get the bottom subvolume's name from ROOT_REF */
2499 root = fs_info->tree_root;
2500 key.objectid = treeid;
2501 key.type = BTRFS_ROOT_REF_KEY;
2502 key.offset = args->treeid;
2503 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2504 if (ret < 0) {
2505 goto out;
2506 } else if (ret > 0) {
2507 ret = -ENOENT;
2508 goto out;
2509 }
2510
2511 leaf = path->nodes[0];
2512 slot = path->slots[0];
2513 btrfs_item_key_to_cpu(leaf, &key, slot);
2514
2515 item_off = btrfs_item_ptr_offset(leaf, slot);
2516 item_len = btrfs_item_size_nr(leaf, slot);
2517 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2518 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2519 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2520 ret = -EINVAL;
2521 goto out;
2522 }
2523
2524 /* Copy subvolume's name */
2525 item_off += sizeof(struct btrfs_root_ref);
2526 item_len -= sizeof(struct btrfs_root_ref);
2527 read_extent_buffer(leaf, args->name, item_off, item_len);
2528 args->name[item_len] = 0;
2529
2530out:
2531 btrfs_free_path(path);
2532 return ret;
2533}
2534
Chris Masonac8e9812010-02-28 15:39:26 -05002535static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2536 void __user *argp)
2537{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002538 struct btrfs_ioctl_ino_lookup_args *args;
2539 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002540 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002541
Julia Lawall2354d08f2010-10-29 15:14:18 -04002542 args = memdup_user(argp, sizeof(*args));
2543 if (IS_ERR(args))
2544 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002545
Al Viro496ad9a2013-01-23 17:07:38 -05002546 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002547
David Sterba01b810b2015-05-12 19:14:49 +02002548 /*
2549 * Unprivileged query to obtain the containing subvolume root id. The
2550 * path is reset so it's consistent with btrfs_search_path_in_tree.
2551 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002552 if (args->treeid == 0)
2553 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2554
David Sterba01b810b2015-05-12 19:14:49 +02002555 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2556 args->name[0] = 0;
2557 goto out;
2558 }
2559
2560 if (!capable(CAP_SYS_ADMIN)) {
2561 ret = -EPERM;
2562 goto out;
2563 }
2564
Chris Masonac8e9812010-02-28 15:39:26 -05002565 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2566 args->treeid, args->objectid,
2567 args->name);
2568
David Sterba01b810b2015-05-12 19:14:49 +02002569out:
Chris Masonac8e9812010-02-28 15:39:26 -05002570 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2571 ret = -EFAULT;
2572
2573 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002574 return ret;
2575}
2576
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002577/*
2578 * Version of ino_lookup ioctl (unprivileged)
2579 *
2580 * The main differences from ino_lookup ioctl are:
2581 *
2582 * 1. Read + Exec permission will be checked using inode_permission() during
2583 * path construction. -EACCES will be returned in case of failure.
2584 * 2. Path construction will be stopped at the inode number which corresponds
2585 * to the fd with which this ioctl is called. If constructed path does not
2586 * exist under fd's inode, -EACCES will be returned.
2587 * 3. The name of bottom subvolume is also searched and filled.
2588 */
2589static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2590{
2591 struct btrfs_ioctl_ino_lookup_user_args *args;
2592 struct inode *inode;
2593 int ret;
2594
2595 args = memdup_user(argp, sizeof(*args));
2596 if (IS_ERR(args))
2597 return PTR_ERR(args);
2598
2599 inode = file_inode(file);
2600
2601 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2602 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2603 /*
2604 * The subvolume does not exist under fd with which this is
2605 * called
2606 */
2607 kfree(args);
2608 return -EACCES;
2609 }
2610
2611 ret = btrfs_search_path_in_tree_user(inode, args);
2612
2613 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2614 ret = -EFAULT;
2615
2616 kfree(args);
2617 return ret;
2618}
2619
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002620/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2621static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2622{
2623 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2624 struct btrfs_fs_info *fs_info;
2625 struct btrfs_root *root;
2626 struct btrfs_path *path;
2627 struct btrfs_key key;
2628 struct btrfs_root_item *root_item;
2629 struct btrfs_root_ref *rref;
2630 struct extent_buffer *leaf;
2631 unsigned long item_off;
2632 unsigned long item_len;
2633 struct inode *inode;
2634 int slot;
2635 int ret = 0;
2636
2637 path = btrfs_alloc_path();
2638 if (!path)
2639 return -ENOMEM;
2640
2641 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2642 if (!subvol_info) {
2643 btrfs_free_path(path);
2644 return -ENOMEM;
2645 }
2646
2647 inode = file_inode(file);
2648 fs_info = BTRFS_I(inode)->root->fs_info;
2649
2650 /* Get root_item of inode's subvolume */
2651 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2652 key.type = BTRFS_ROOT_ITEM_KEY;
2653 key.offset = (u64)-1;
2654 root = btrfs_read_fs_root_no_name(fs_info, &key);
2655 if (IS_ERR(root)) {
2656 ret = PTR_ERR(root);
2657 goto out;
2658 }
2659 root_item = &root->root_item;
2660
2661 subvol_info->treeid = key.objectid;
2662
2663 subvol_info->generation = btrfs_root_generation(root_item);
2664 subvol_info->flags = btrfs_root_flags(root_item);
2665
2666 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2667 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2668 BTRFS_UUID_SIZE);
2669 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2670 BTRFS_UUID_SIZE);
2671
2672 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2673 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2674 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2675
2676 subvol_info->otransid = btrfs_root_otransid(root_item);
2677 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2678 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2679
2680 subvol_info->stransid = btrfs_root_stransid(root_item);
2681 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2682 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2683
2684 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2685 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2686 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2687
2688 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2689 /* Search root tree for ROOT_BACKREF of this subvolume */
2690 root = fs_info->tree_root;
2691
2692 key.type = BTRFS_ROOT_BACKREF_KEY;
2693 key.offset = 0;
2694 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2695 if (ret < 0) {
2696 goto out;
2697 } else if (path->slots[0] >=
2698 btrfs_header_nritems(path->nodes[0])) {
2699 ret = btrfs_next_leaf(root, path);
2700 if (ret < 0) {
2701 goto out;
2702 } else if (ret > 0) {
2703 ret = -EUCLEAN;
2704 goto out;
2705 }
2706 }
2707
2708 leaf = path->nodes[0];
2709 slot = path->slots[0];
2710 btrfs_item_key_to_cpu(leaf, &key, slot);
2711 if (key.objectid == subvol_info->treeid &&
2712 key.type == BTRFS_ROOT_BACKREF_KEY) {
2713 subvol_info->parent_id = key.offset;
2714
2715 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2716 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2717
2718 item_off = btrfs_item_ptr_offset(leaf, slot)
2719 + sizeof(struct btrfs_root_ref);
2720 item_len = btrfs_item_size_nr(leaf, slot)
2721 - sizeof(struct btrfs_root_ref);
2722 read_extent_buffer(leaf, subvol_info->name,
2723 item_off, item_len);
2724 } else {
2725 ret = -ENOENT;
2726 goto out;
2727 }
2728 }
2729
2730 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2731 ret = -EFAULT;
2732
2733out:
2734 btrfs_free_path(path);
2735 kzfree(subvol_info);
2736 return ret;
2737}
2738
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002739/*
2740 * Return ROOT_REF information of the subvolume containing this inode
2741 * except the subvolume name.
2742 */
2743static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2744{
2745 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2746 struct btrfs_root_ref *rref;
2747 struct btrfs_root *root;
2748 struct btrfs_path *path;
2749 struct btrfs_key key;
2750 struct extent_buffer *leaf;
2751 struct inode *inode;
2752 u64 objectid;
2753 int slot;
2754 int ret;
2755 u8 found;
2756
2757 path = btrfs_alloc_path();
2758 if (!path)
2759 return -ENOMEM;
2760
2761 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2762 if (IS_ERR(rootrefs)) {
2763 btrfs_free_path(path);
2764 return PTR_ERR(rootrefs);
2765 }
2766
2767 inode = file_inode(file);
2768 root = BTRFS_I(inode)->root->fs_info->tree_root;
2769 objectid = BTRFS_I(inode)->root->root_key.objectid;
2770
2771 key.objectid = objectid;
2772 key.type = BTRFS_ROOT_REF_KEY;
2773 key.offset = rootrefs->min_treeid;
2774 found = 0;
2775
2776 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2777 if (ret < 0) {
2778 goto out;
2779 } else if (path->slots[0] >=
2780 btrfs_header_nritems(path->nodes[0])) {
2781 ret = btrfs_next_leaf(root, path);
2782 if (ret < 0) {
2783 goto out;
2784 } else if (ret > 0) {
2785 ret = -EUCLEAN;
2786 goto out;
2787 }
2788 }
2789 while (1) {
2790 leaf = path->nodes[0];
2791 slot = path->slots[0];
2792
2793 btrfs_item_key_to_cpu(leaf, &key, slot);
2794 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2795 ret = 0;
2796 goto out;
2797 }
2798
2799 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2800 ret = -EOVERFLOW;
2801 goto out;
2802 }
2803
2804 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2805 rootrefs->rootref[found].treeid = key.offset;
2806 rootrefs->rootref[found].dirid =
2807 btrfs_root_ref_dirid(leaf, rref);
2808 found++;
2809
2810 ret = btrfs_next_item(root, path);
2811 if (ret < 0) {
2812 goto out;
2813 } else if (ret > 0) {
2814 ret = -EUCLEAN;
2815 goto out;
2816 }
2817 }
2818
2819out:
2820 if (!ret || ret == -EOVERFLOW) {
2821 rootrefs->num_items = found;
2822 /* update min_treeid for next search */
2823 if (found)
2824 rootrefs->min_treeid =
2825 rootrefs->rootref[found - 1].treeid + 1;
2826 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2827 ret = -EFAULT;
2828 }
2829
2830 kfree(rootrefs);
2831 btrfs_free_path(path);
2832
2833 return ret;
2834}
2835
Yan, Zheng76dda932009-09-21 16:00:26 -04002836static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2837 void __user *arg)
2838{
Al Viro54563d42013-09-01 15:57:51 -04002839 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002840 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002841 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002842 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002843 struct inode *inode;
2844 struct btrfs_root *root = BTRFS_I(dir)->root;
2845 struct btrfs_root *dest = NULL;
2846 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002847 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002848 int err = 0;
2849
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002850 if (!S_ISDIR(dir->i_mode))
2851 return -ENOTDIR;
2852
Yan, Zheng76dda932009-09-21 16:00:26 -04002853 vol_args = memdup_user(arg, sizeof(*vol_args));
2854 if (IS_ERR(vol_args))
2855 return PTR_ERR(vol_args);
2856
2857 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2858 namelen = strlen(vol_args->name);
2859 if (strchr(vol_args->name, '/') ||
2860 strncmp(vol_args->name, "..", namelen) == 0) {
2861 err = -EINVAL;
2862 goto out;
2863 }
2864
Al Viroa561be72011-11-23 11:57:51 -05002865 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002866 if (err)
2867 goto out;
2868
David Sterba521e0542014-04-15 16:41:44 +02002869
Al Viro00235412016-05-26 00:05:12 -04002870 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2871 if (err == -EINTR)
2872 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002873 dentry = lookup_one_len(vol_args->name, parent, namelen);
2874 if (IS_ERR(dentry)) {
2875 err = PTR_ERR(dentry);
2876 goto out_unlock_dir;
2877 }
2878
David Howells2b0143b2015-03-17 22:25:59 +00002879 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002880 err = -ENOENT;
2881 goto out_dput;
2882 }
2883
David Howells2b0143b2015-03-17 22:25:59 +00002884 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002885 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302886 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002887 /*
2888 * Regular user. Only allow this with a special mount
2889 * option, when the user has write+exec access to the
2890 * subvol root, and when rmdir(2) would have been
2891 * allowed.
2892 *
2893 * Note that this is _not_ check that the subvol is
2894 * empty or doesn't contain data that we wouldn't
2895 * otherwise be able to delete.
2896 *
2897 * Users who want to delete empty subvols should try
2898 * rmdir(2).
2899 */
2900 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002901 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002902 goto out_dput;
2903
2904 /*
2905 * Do not allow deletion if the parent dir is the same
2906 * as the dir to be deleted. That means the ioctl
2907 * must be called on the dentry referencing the root
2908 * of the subvol, not a random directory contained
2909 * within it.
2910 */
2911 err = -EINVAL;
2912 if (root == dest)
2913 goto out_dput;
2914
2915 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2916 if (err)
2917 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002918 }
2919
Miao Xie5c39da52012-10-22 11:39:53 +00002920 /* check if subvolume may be deleted by a user */
2921 err = btrfs_may_delete(dir, dentry, 1);
2922 if (err)
2923 goto out_dput;
2924
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002925 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002926 err = -EINVAL;
2927 goto out_dput;
2928 }
2929
Al Viro59551022016-01-22 15:40:57 -05002930 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002931 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002932 inode_unlock(inode);
Amir Goldstein46008d92019-05-26 17:34:03 +03002933 if (!err) {
2934 fsnotify_rmdir(dir, dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04002935 d_delete(dentry);
Amir Goldstein46008d92019-05-26 17:34:03 +03002936 }
Liu Bofa6ac872013-02-20 14:10:23 +00002937
Yan, Zheng76dda932009-09-21 16:00:26 -04002938out_dput:
2939 dput(dentry);
2940out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002941 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002942out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002943 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002944out:
2945 kfree(vol_args);
2946 return err;
2947}
2948
Chris Mason1e701a32010-03-11 09:42:04 -05002949static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002950{
Al Viro496ad9a2013-01-23 17:07:38 -05002951 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002952 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002953 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002954 int ret;
2955
Ilya Dryomov25122d12013-01-20 15:57:57 +02002956 ret = mnt_want_write_file(file);
2957 if (ret)
2958 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002959
Ilya Dryomov25122d12013-01-20 15:57:57 +02002960 if (btrfs_root_readonly(root)) {
2961 ret = -EROFS;
2962 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002963 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002964
2965 switch (inode->i_mode & S_IFMT) {
2966 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002967 if (!capable(CAP_SYS_ADMIN)) {
2968 ret = -EPERM;
2969 goto out;
2970 }
Eric Sandeende78b512013-01-31 18:21:12 +00002971 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002972 break;
2973 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02002974 /*
2975 * Note that this does not check the file descriptor for write
2976 * access. This prevents defragmenting executables that are
2977 * running and allows defrag on files open in read-only mode.
2978 */
2979 if (!capable(CAP_SYS_ADMIN) &&
2980 inode_permission(inode, MAY_WRITE)) {
2981 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05002982 goto out;
2983 }
Chris Mason1e701a32010-03-11 09:42:04 -05002984
2985 range = kzalloc(sizeof(*range), GFP_KERNEL);
2986 if (!range) {
2987 ret = -ENOMEM;
2988 goto out;
2989 }
2990
2991 if (argp) {
2992 if (copy_from_user(range, argp,
2993 sizeof(*range))) {
2994 ret = -EFAULT;
2995 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002996 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002997 }
2998 /* compression requires us to start the IO */
2999 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3000 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3001 range->extent_thresh = (u32)-1;
3002 }
3003 } else {
3004 /* the rest are all set to zero by kzalloc */
3005 range->len = (u64)-1;
3006 }
Al Viro496ad9a2013-01-23 17:07:38 -05003007 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08003008 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04003009 if (ret > 0)
3010 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05003011 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003012 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003013 default:
3014 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003015 }
Chris Masone441d542009-01-05 16:57:23 -05003016out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003017 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003018 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003019}
3020
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003021static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003022{
3023 struct btrfs_ioctl_vol_args *vol_args;
3024 int ret;
3025
Chris Masone441d542009-01-05 16:57:23 -05003026 if (!capable(CAP_SYS_ADMIN))
3027 return -EPERM;
3028
David Sterba171938e2017-03-28 14:44:21 +02003029 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08003030 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003031
Li Zefandae7b662009-04-08 15:06:54 +08003032 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003033 if (IS_ERR(vol_args)) {
3034 ret = PTR_ERR(vol_args);
3035 goto out;
3036 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003037
Mark Fasheh5516e592008-07-24 12:20:14 -04003038 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003039 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003040
Anand Jain43d20762014-07-01 00:58:56 +08003041 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003042 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003043
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003044 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003045out:
David Sterba171938e2017-03-28 14:44:21 +02003046 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003047 return ret;
3048}
3049
Anand Jain6b526ed2016-02-13 10:01:39 +08003050static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003051{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003052 struct inode *inode = file_inode(file);
3053 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003054 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003055 int ret;
3056
Chris Masone441d542009-01-05 16:57:23 -05003057 if (!capable(CAP_SYS_ADMIN))
3058 return -EPERM;
3059
Miao Xieda249272012-11-26 08:44:50 +00003060 ret = mnt_want_write_file(file);
3061 if (ret)
3062 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05003063
Li Zefandae7b662009-04-08 15:06:54 +08003064 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003065 if (IS_ERR(vol_args)) {
3066 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003067 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003068 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003069
Anand Jain6b526ed2016-02-13 10:01:39 +08003070 /* Check for compatibility reject unknown flags */
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003071 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3072 ret = -EOPNOTSUPP;
3073 goto out;
3074 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003075
David Sterba171938e2017-03-28 14:44:21 +02003076 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00003077 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3078 goto out;
3079 }
3080
David Sterba735654e2016-02-15 18:15:21 +01003081 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003082 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08003083 } else {
3084 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003085 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08003086 }
David Sterba171938e2017-03-28 14:44:21 +02003087 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00003088
Anand Jain6b526ed2016-02-13 10:01:39 +08003089 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003090 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003091 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003092 vol_args->devid);
3093 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003094 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003095 vol_args->name);
3096 }
Anand Jain183860f2013-05-17 10:52:45 +00003097out:
3098 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003099err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003100 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003101 return ret;
3102}
3103
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003104static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3105{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003106 struct inode *inode = file_inode(file);
3107 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003108 struct btrfs_ioctl_vol_args *vol_args;
3109 int ret;
3110
3111 if (!capable(CAP_SYS_ADMIN))
3112 return -EPERM;
3113
3114 ret = mnt_want_write_file(file);
3115 if (ret)
3116 return ret;
3117
David Sterba171938e2017-03-28 14:44:21 +02003118 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003119 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02003120 goto out_drop_write;
3121 }
3122
3123 vol_args = memdup_user(arg, sizeof(*vol_args));
3124 if (IS_ERR(vol_args)) {
3125 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003126 goto out;
3127 }
3128
David Sterba58d7bbf2016-05-04 14:10:47 +02003129 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003130 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003131
3132 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003133 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003134 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02003135out:
David Sterba171938e2017-03-28 14:44:21 +02003136 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02003137out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003138 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02003139
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003140 return ret;
3141}
3142
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003143static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3144 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003145{
Li Zefan027ed2f2011-06-08 08:27:56 +00003146 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003147 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003148 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00003149 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003150
Li Zefan027ed2f2011-06-08 08:27:56 +00003151 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3152 if (!fi_args)
3153 return -ENOMEM;
3154
David Sterbad03262c2017-06-16 00:09:21 +02003155 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003156 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003157
David Sterbad03262c2017-06-16 00:09:21 +02003158 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003159 if (device->devid > fi_args->max_id)
3160 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003161 }
David Sterbad03262c2017-06-16 00:09:21 +02003162 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003163
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003164 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003165 fi_args->nodesize = fs_info->nodesize;
3166 fi_args->sectorsize = fs_info->sectorsize;
3167 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003168
Li Zefan027ed2f2011-06-08 08:27:56 +00003169 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3170 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003171
Li Zefan027ed2f2011-06-08 08:27:56 +00003172 kfree(fi_args);
3173 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003174}
3175
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003176static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3177 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003178{
3179 struct btrfs_ioctl_dev_info_args *di_args;
3180 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003181 int ret = 0;
3182 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01003183
Jan Schmidt475f6382011-03-11 15:41:01 +01003184 di_args = memdup_user(arg, sizeof(*di_args));
3185 if (IS_ERR(di_args))
3186 return PTR_ERR(di_args);
3187
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003188 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01003189 s_uuid = di_args->uuid;
3190
David Sterbac5593ca2017-06-16 00:09:21 +02003191 rcu_read_lock();
Anand Jaine4319cd2019-01-17 23:32:31 +08003192 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08003193 NULL, true);
Jan Schmidt475f6382011-03-11 15:41:01 +01003194
3195 if (!dev) {
3196 ret = -ENODEV;
3197 goto out;
3198 }
3199
3200 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003201 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3202 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003203 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003204 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003205 strncpy(di_args->path, rcu_str_deref(dev->name),
3206 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003207 di_args->path[sizeof(di_args->path) - 1] = 0;
3208 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003209 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003210 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003211
3212out:
David Sterbac5593ca2017-06-16 00:09:21 +02003213 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003214 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3215 ret = -EFAULT;
3216
3217 kfree(di_args);
3218 return ret;
3219}
3220
Filipe Mananad8b55242019-01-08 11:43:07 +00003221static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3222 struct inode *inode2, u64 loff2, u64 len)
3223{
3224 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3225 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3226}
3227
3228static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3229 struct inode *inode2, u64 loff2, u64 len)
3230{
3231 if (inode1 < inode2) {
3232 swap(inode1, inode2);
3233 swap(loff1, loff2);
3234 } else if (inode1 == inode2 && loff2 < loff1) {
3235 swap(loff1, loff2);
3236 }
3237 lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3238 lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3239}
3240
Filipe Manana57a50e22018-12-12 18:05:59 +00003241static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len,
Filipe Manana34a28e32018-12-07 15:25:38 +00003242 struct inode *dst, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003243{
3244 int ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003245
Filipe Manana34a28e32018-12-07 15:25:38 +00003246 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003247 * Lock destination range to serialize with concurrent readpages() and
3248 * source range to serialize with relocation.
Filipe Manana34a28e32018-12-07 15:25:38 +00003249 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003250 btrfs_double_extent_lock(src, loff, dst, dst_loff, len);
Filipe Manana57a50e22018-12-12 18:05:59 +00003251 ret = btrfs_clone(src, dst, loff, len, len, dst_loff, 1);
Filipe Mananad8b55242019-01-08 11:43:07 +00003252 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Timofey Titovets39739092018-05-02 08:15:36 +03003253
3254 return ret;
3255}
3256
Timofey Titovetsb6728762018-05-02 08:15:37 +03003257#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3258
Timofey Titovets39739092018-05-02 08:15:36 +03003259static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3260 struct inode *dst, u64 dst_loff)
3261{
3262 int ret;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003263 u64 i, tail_len, chunk_count;
Filipe Manana62d54f32019-04-22 16:43:42 +01003264 struct btrfs_root *root_dst = BTRFS_I(dst)->root;
3265
3266 spin_lock(&root_dst->root_item_lock);
3267 if (root_dst->send_in_progress) {
3268 btrfs_warn_rl(root_dst->fs_info,
3269"cannot deduplicate to root %llu while send operations are using it (%d in progress)",
3270 root_dst->root_key.objectid,
3271 root_dst->send_in_progress);
3272 spin_unlock(&root_dst->root_item_lock);
3273 return -EAGAIN;
3274 }
3275 root_dst->dedupe_in_progress++;
3276 spin_unlock(&root_dst->root_item_lock);
Timofey Titovets39739092018-05-02 08:15:36 +03003277
Timofey Titovetsb6728762018-05-02 08:15:37 +03003278 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3279 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003280
Timofey Titovetsb6728762018-05-02 08:15:37 +03003281 for (i = 0; i < chunk_count; i++) {
3282 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Filipe Manana34a28e32018-12-07 15:25:38 +00003283 dst, dst_loff);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003284 if (ret)
Filipe Manana62d54f32019-04-22 16:43:42 +01003285 goto out;
Timofey Titovetsb6728762018-05-02 08:15:37 +03003286
3287 loff += BTRFS_MAX_DEDUPE_LEN;
3288 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3289 }
3290
3291 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003292 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
Filipe Manana34a28e32018-12-07 15:25:38 +00003293 dst_loff);
Filipe Manana62d54f32019-04-22 16:43:42 +01003294out:
3295 spin_lock(&root_dst->root_item_lock);
3296 root_dst->dedupe_in_progress--;
3297 spin_unlock(&root_dst->root_item_lock);
Mark Fasheh416161d2013-08-06 11:42:51 -07003298
3299 return ret;
3300}
3301
Filipe Mananaf82a9902014-06-01 01:50:28 +01003302static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3303 struct inode *inode,
3304 u64 endoff,
3305 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003306 const u64 olen,
3307 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003308{
3309 struct btrfs_root *root = BTRFS_I(inode)->root;
3310 int ret;
3311
3312 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003313 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003314 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003315 /*
3316 * We round up to the block size at eof when determining which
3317 * extents to clone above, but shouldn't round up the file size.
3318 */
3319 if (endoff > destoff + olen)
3320 endoff = destoff + olen;
3321 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003322 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003323
3324 ret = btrfs_update_inode(trans, root, inode);
3325 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003326 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003327 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003328 goto out;
3329 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003330 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003331out:
3332 return ret;
3333}
3334
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003335static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003336 const struct btrfs_trans_handle *trans,
3337 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003338 const u64 hole_offset,
3339 const u64 hole_len)
3340{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003341 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003342 struct extent_map *em;
3343 int ret;
3344
3345 em = alloc_extent_map();
3346 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003347 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003348 return;
3349 }
3350
Filipe Manana14f59792014-06-29 21:45:40 +01003351 if (path) {
3352 struct btrfs_file_extent_item *fi;
3353
3354 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3355 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003356 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003357 em->generation = -1;
3358 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3359 BTRFS_FILE_EXTENT_INLINE)
3360 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003361 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003362 } else {
3363 em->start = hole_offset;
3364 em->len = hole_len;
3365 em->ram_bytes = em->len;
3366 em->orig_start = hole_offset;
3367 em->block_start = EXTENT_MAP_HOLE;
3368 em->block_len = 0;
3369 em->orig_block_len = 0;
3370 em->compress_type = BTRFS_COMPRESS_NONE;
3371 em->generation = trans->transid;
3372 }
3373
3374 while (1) {
3375 write_lock(&em_tree->lock);
3376 ret = add_extent_mapping(em_tree, em, 1);
3377 write_unlock(&em_tree->lock);
3378 if (ret != -EEXIST) {
3379 free_extent_map(em);
3380 break;
3381 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003382 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003383 em->start + em->len - 1, 0);
3384 }
3385
David Sterbaee39b432014-09-30 01:33:33 +02003386 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003387 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003388}
3389
Filipe Manana8039d872015-10-13 15:15:00 +01003390/*
3391 * Make sure we do not end up inserting an inline extent into a file that has
3392 * already other (non-inline) extents. If a file has an inline extent it can
3393 * not have any other extents and the (single) inline extent must start at the
3394 * file offset 0. Failing to respect these rules will lead to file corruption,
3395 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3396 *
3397 * We can have extents that have been already written to disk or we can have
3398 * dirty ranges still in delalloc, in which case the extent maps and items are
3399 * created only when we run delalloc, and the delalloc ranges might fall outside
3400 * the range we are currently locking in the inode's io tree. So we check the
3401 * inode's i_size because of that (i_size updates are done while holding the
3402 * i_mutex, which we are holding here).
3403 * We also check to see if the inode has a size not greater than "datal" but has
3404 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3405 * protected against such concurrent fallocate calls by the i_mutex).
3406 *
3407 * If the file has no extents but a size greater than datal, do not allow the
3408 * copy because we would need turn the inline extent into a non-inline one (even
3409 * with NO_HOLES enabled). If we find our destination inode only has one inline
3410 * extent, just overwrite it with the source inline extent if its size is less
3411 * than the source extent's size, or we could copy the source inline extent's
3412 * data into the destination inode's inline extent if the later is greater then
3413 * the former.
3414 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003415static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003416 struct btrfs_trans_handle *trans,
3417 struct btrfs_path *path,
3418 struct btrfs_key *new_key,
3419 const u64 drop_start,
3420 const u64 datal,
3421 const u64 skip,
3422 const u64 size,
3423 char *inline_data)
3424{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003425 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003426 struct btrfs_root *root = BTRFS_I(dst)->root;
3427 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003428 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003429 int ret;
3430 struct btrfs_key key;
3431
3432 if (new_key->offset > 0)
3433 return -EOPNOTSUPP;
3434
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003435 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003436 key.type = BTRFS_EXTENT_DATA_KEY;
3437 key.offset = 0;
3438 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3439 if (ret < 0) {
3440 return ret;
3441 } else if (ret > 0) {
3442 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3443 ret = btrfs_next_leaf(root, path);
3444 if (ret < 0)
3445 return ret;
3446 else if (ret > 0)
3447 goto copy_inline_extent;
3448 }
3449 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003450 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003451 key.type == BTRFS_EXTENT_DATA_KEY) {
3452 ASSERT(key.offset > 0);
3453 return -EOPNOTSUPP;
3454 }
3455 } else if (i_size_read(dst) <= datal) {
3456 struct btrfs_file_extent_item *ei;
3457 u64 ext_len;
3458
3459 /*
3460 * If the file size is <= datal, make sure there are no other
3461 * extents following (can happen do to an fallocate call with
3462 * the flag FALLOC_FL_KEEP_SIZE).
3463 */
3464 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3465 struct btrfs_file_extent_item);
3466 /*
3467 * If it's an inline extent, it can not have other extents
3468 * following it.
3469 */
3470 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3471 BTRFS_FILE_EXTENT_INLINE)
3472 goto copy_inline_extent;
3473
3474 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3475 if (ext_len > aligned_end)
3476 return -EOPNOTSUPP;
3477
3478 ret = btrfs_next_item(root, path);
3479 if (ret < 0) {
3480 return ret;
3481 } else if (ret == 0) {
3482 btrfs_item_key_to_cpu(path->nodes[0], &key,
3483 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003484 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003485 key.type == BTRFS_EXTENT_DATA_KEY)
3486 return -EOPNOTSUPP;
3487 }
3488 }
3489
3490copy_inline_extent:
3491 /*
3492 * We have no extent items, or we have an extent at offset 0 which may
3493 * or may not be inlined. All these cases are dealt the same way.
3494 */
3495 if (i_size_read(dst) > datal) {
3496 /*
3497 * If the destination inode has an inline extent...
3498 * This would require copying the data from the source inline
3499 * extent into the beginning of the destination's inline extent.
3500 * But this is really complex, both extents can be compressed
3501 * or just one of them, which would require decompressing and
3502 * re-compressing data (which could increase the new compressed
3503 * size, not allowing the compressed data to fit anymore in an
3504 * inline extent).
3505 * So just don't support this case for now (it should be rare,
3506 * we are not really saving space when cloning inline extents).
3507 */
3508 return -EOPNOTSUPP;
3509 }
3510
3511 btrfs_release_path(path);
3512 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3513 if (ret)
3514 return ret;
3515 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3516 if (ret)
3517 return ret;
3518
3519 if (skip) {
3520 const u32 start = btrfs_file_extent_calc_inline_size(0);
3521
3522 memmove(inline_data + start, inline_data + start + skip, datal);
3523 }
3524
3525 write_extent_buffer(path->nodes[0], inline_data,
3526 btrfs_item_ptr_offset(path->nodes[0],
3527 path->slots[0]),
3528 size);
3529 inode_add_bytes(dst, datal);
3530
3531 return 0;
3532}
3533
Mark Fasheh32b7c682013-08-06 11:42:49 -07003534/**
3535 * btrfs_clone() - clone a range from inode file to another
3536 *
3537 * @src: Inode to clone from
3538 * @inode: Inode to clone to
3539 * @off: Offset within source to start clone from
3540 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003541 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003542 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003543 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003544 */
3545static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003546 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003547 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003548{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003549 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003550 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003551 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003552 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003553 struct btrfs_trans_handle *trans;
3554 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003555 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003556 u32 nritems;
3557 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003558 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003559 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003560 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003561
3562 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003563 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3564 if (!buf)
3565 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003566
3567 path = btrfs_alloc_path();
3568 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003569 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003570 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003571 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003572
David Sterbae4058b52015-11-27 16:31:35 +01003573 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003574 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003575 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003576 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003577 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003578
3579 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003580 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003581
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003582 /*
3583 * note the key will change type as we walk through the
3584 * tree.
3585 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003586 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003587 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3588 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003589 if (ret < 0)
3590 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003591 /*
3592 * First search, if no extent item that starts at offset off was
3593 * found but the previous item is an extent item, it's possible
3594 * it might overlap our target range, therefore process it.
3595 */
3596 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3597 btrfs_item_key_to_cpu(path->nodes[0], &key,
3598 path->slots[0] - 1);
3599 if (key.type == BTRFS_EXTENT_DATA_KEY)
3600 path->slots[0]--;
3601 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003602
Yan Zhengae01a0a2008-08-04 23:23:47 -04003603 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003604process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003605 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003606 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003607 if (ret < 0)
3608 goto out;
3609 if (ret > 0)
3610 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003611 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003612 }
3613 leaf = path->nodes[0];
3614 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003615
Yan Zhengae01a0a2008-08-04 23:23:47 -04003616 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003617 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003618 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003619 break;
3620
David Sterba962a2982014-06-04 18:41:45 +02003621 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003622 struct btrfs_file_extent_item *extent;
3623 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003624 u32 size;
3625 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003626 u64 disko = 0, diskl = 0;
3627 u64 datao = 0, datal = 0;
3628 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003629 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003630
Sage Weilc5c9cd42008-11-12 14:32:25 -05003631 extent = btrfs_item_ptr(leaf, slot,
3632 struct btrfs_file_extent_item);
3633 comp = btrfs_file_extent_compression(leaf, extent);
3634 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003635 if (type == BTRFS_FILE_EXTENT_REG ||
3636 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003637 disko = btrfs_file_extent_disk_bytenr(leaf,
3638 extent);
3639 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3640 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003641 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003642 datal = btrfs_file_extent_num_bytes(leaf,
3643 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003644 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3645 /* take upper bound, may be compressed */
3646 datal = btrfs_file_extent_ram_bytes(leaf,
3647 extent);
3648 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003649
Filipe Manana2c463822014-05-31 02:31:05 +01003650 /*
3651 * The first search might have left us at an extent
3652 * item that ends before our target range's start, can
3653 * happen if we have holes and NO_HOLES feature enabled.
3654 */
3655 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003656 path->slots[0]++;
3657 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003658 } else if (key.offset >= off + len) {
3659 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003660 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003661 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003662 size = btrfs_item_size_nr(leaf, slot);
3663 read_extent_buffer(leaf, buf,
3664 btrfs_item_ptr_offset(leaf, slot),
3665 size);
3666
3667 btrfs_release_path(path);
3668 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003669
Zheng Yan31840ae2008-09-23 13:14:14 -04003670 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003671 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003672 if (off <= key.offset)
3673 new_key.offset = key.offset + destoff - off;
3674 else
3675 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003676
Sage Weilb6f34092011-09-20 14:48:51 -04003677 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003678 * Deal with a hole that doesn't have an extent item
3679 * that represents it (NO_HOLES feature enabled).
3680 * This hole is either in the middle of the cloning
3681 * range or at the beginning (fully overlaps it or
3682 * partially overlaps it).
3683 */
3684 if (new_key.offset != last_dest_end)
3685 drop_start = last_dest_end;
3686 else
3687 drop_start = new_key.offset;
3688
3689 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003690 * 1 - adjusting old extent (we may have to split it)
3691 * 1 - add new extent
3692 * 1 - inode update
3693 */
3694 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003695 if (IS_ERR(trans)) {
3696 ret = PTR_ERR(trans);
3697 goto out;
3698 }
3699
Chris Masonc8a894d2009-06-27 21:07:03 -04003700 if (type == BTRFS_FILE_EXTENT_REG ||
3701 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003702 /*
3703 * a | --- range to clone ---| b
3704 * | ------------- extent ------------- |
3705 */
3706
Antonio Ospite93915582014-06-04 14:03:48 +02003707 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003708 if (key.offset + datal > off + len)
3709 datal = off + len - key.offset;
3710
Antonio Ospite93915582014-06-04 14:03:48 +02003711 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003712 if (off > key.offset) {
3713 datao += off - key.offset;
3714 datal -= off - key.offset;
3715 }
3716
Josef Bacik5dc562c2012-08-17 13:14:17 -04003717 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003718 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003719 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003720 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003721 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003722 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003723 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003724 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003725 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003726 goto out;
3727 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003728
Sage Weilc5c9cd42008-11-12 14:32:25 -05003729 ret = btrfs_insert_empty_item(trans, root, path,
3730 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003731 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003732 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003733 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003734 goto out;
3735 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003736
3737 leaf = path->nodes[0];
3738 slot = path->slots[0];
3739 write_extent_buffer(leaf, buf,
3740 btrfs_item_ptr_offset(leaf, slot),
3741 size);
3742
3743 extent = btrfs_item_ptr(leaf, slot,
3744 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003745
Sage Weilc5c9cd42008-11-12 14:32:25 -05003746 /* disko == 0 means it's a hole */
3747 if (!disko)
3748 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003749
3750 btrfs_set_file_extent_offset(leaf, extent,
3751 datao);
3752 btrfs_set_file_extent_num_bytes(leaf, extent,
3753 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003754
Sage Weilc5c9cd42008-11-12 14:32:25 -05003755 if (disko) {
Qu Wenruo82fa1132019-04-04 14:45:35 +08003756 struct btrfs_ref ref = { 0 };
Sage Weilc5c9cd42008-11-12 14:32:25 -05003757 inode_add_bytes(inode, datal);
Qu Wenruo82fa1132019-04-04 14:45:35 +08003758 btrfs_init_generic_ref(&ref,
3759 BTRFS_ADD_DELAYED_REF, disko,
3760 diskl, 0);
3761 btrfs_init_data_ref(&ref,
3762 root->root_key.objectid,
3763 btrfs_ino(BTRFS_I(inode)),
3764 new_key.offset - datao);
3765 ret = btrfs_inc_extent_ref(trans, &ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003766 if (ret) {
3767 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003768 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003769 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003770 goto out;
3771
3772 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003773 }
3774 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3775 u64 skip = 0;
3776 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003777
Sage Weilc5c9cd42008-11-12 14:32:25 -05003778 if (off > key.offset) {
3779 skip = off - key.offset;
3780 new_key.offset += skip;
3781 }
Chris Masond3977122009-01-05 21:25:51 -05003782
Liu Boaa42ffd2012-09-18 03:52:23 -06003783 if (key.offset + datal > off + len)
3784 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003785
Sage Weilc5c9cd42008-11-12 14:32:25 -05003786 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003787 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003788 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003789 goto out;
3790 }
3791 size -= skip + trim;
3792 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003793
David Sterba4a0ab9d2017-02-10 20:18:49 +01003794 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003795 trans, path,
3796 &new_key,
3797 drop_start,
3798 datal,
3799 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003800 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003801 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003802 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003803 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003804 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 goto out;
3806 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003807 leaf = path->nodes[0];
3808 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003809 }
3810
Filipe Manana7ffbb592014-06-09 03:48:05 +01003811 /* If we have an implicit hole (NO_HOLES feature). */
3812 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003813 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003814 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003815 new_key.offset - drop_start);
3816
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003817 clone_update_extent_map(BTRFS_I(inode), trans,
3818 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003819
Sage Weilc5c9cd42008-11-12 14:32:25 -05003820 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003821 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003822
Filipe Manana62e23902014-08-08 02:47:06 +01003823 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003824 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003825 ret = clone_finish_inode_update(trans, inode,
3826 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003827 destoff, olen,
3828 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003829 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003830 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003831 if (new_key.offset + datal >= destoff + len)
3832 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003833 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003834 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003835 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003836
3837 if (fatal_signal_pending(current)) {
3838 ret = -EINTR;
3839 goto out;
3840 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003841 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003842 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003843
Filipe Mananaf82a9902014-06-01 01:50:28 +01003844 if (last_dest_end < destoff + len) {
3845 /*
3846 * We have an implicit hole (NO_HOLES feature is enabled) that
3847 * fully or partially overlaps our cloning range at its end.
3848 */
3849 btrfs_release_path(path);
3850
3851 /*
3852 * 1 - remove extent(s)
3853 * 1 - inode update
3854 */
3855 trans = btrfs_start_transaction(root, 2);
3856 if (IS_ERR(trans)) {
3857 ret = PTR_ERR(trans);
3858 goto out;
3859 }
3860 ret = btrfs_drop_extents(trans, root, inode,
3861 last_dest_end, destoff + len, 1);
3862 if (ret) {
3863 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003864 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003865 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003866 goto out;
3867 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003868 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3869 last_dest_end,
3870 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003871 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003872 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003873 }
3874
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003875out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003876 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003877 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003878 return ret;
3879}
3880
Zach Brown3db11b22015-11-10 16:53:32 -05003881static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3882 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003883{
Al Viro54563d42013-09-01 15:57:51 -04003884 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003885 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003886 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003887 int ret;
3888 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003889 u64 bs = fs_info->sb->s_blocksize;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003890
3891 /*
3892 * TODO:
3893 * - split compressed inline extents. annoying: we need to
3894 * decompress into destination's address_space (the file offset
3895 * may change, so source mapping won't do), then recompress (or
3896 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003897 *
3898 * - split destination inode's inline extents. The inline extents can
3899 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003900 */
3901
Filipe Mananaac765f82018-11-05 11:14:17 +00003902 /*
Filipe Manana34a28e32018-12-07 15:25:38 +00003903 * VFS's generic_remap_file_range_prep() protects us from cloning the
3904 * eof block into the middle of a file, which would result in corruption
3905 * if the file size is not blocksize aligned. So we don't need to check
3906 * for that case here.
Filipe Mananaac765f82018-11-05 11:14:17 +00003907 */
Filipe Manana34a28e32018-12-07 15:25:38 +00003908 if (off + len == src->i_size)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003909 len = ALIGN(src->i_size, bs) - off;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003910
3911 if (destoff > inode->i_size) {
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003912 const u64 wb_start = ALIGN_DOWN(inode->i_size, bs);
3913
Mark Fasheh32b7c682013-08-06 11:42:49 -07003914 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3915 if (ret)
Filipe Manana34a28e32018-12-07 15:25:38 +00003916 return ret;
Filipe Mananaf7fa1102019-01-08 11:42:54 +00003917 /*
3918 * We may have truncated the last block if the inode's size is
3919 * not sector size aligned, so we need to wait for writeback to
3920 * complete before proceeding further, otherwise we can race
3921 * with cloning and attempt to increment a reference to an
3922 * extent that no longer exists (writeback completed right after
3923 * we found the previous extent covering eof and before we
3924 * attempted to increment its reference count).
3925 */
3926 ret = btrfs_wait_ordered_range(inode, wb_start,
3927 destoff - wb_start);
3928 if (ret)
3929 return ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003930 }
3931
Filipe Mananac125b8b2014-05-23 05:03:34 +01003932 /*
Filipe Mananad8b55242019-01-08 11:43:07 +00003933 * Lock destination range to serialize with concurrent readpages() and
3934 * source range to serialize with relocation.
Filipe Mananac125b8b2014-05-23 05:03:34 +01003935 */
Filipe Mananad8b55242019-01-08 11:43:07 +00003936 btrfs_double_extent_lock(src, off, inode, destoff, len);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003937 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Filipe Mananad8b55242019-01-08 11:43:07 +00003938 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003939 /*
3940 * Truncate page cache pages so that future reads will see the cloned
3941 * data immediately and not the previous data.
3942 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303943 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003944 round_down(destoff, PAGE_SIZE),
3945 round_up(destoff + len, PAGE_SIZE) - 1);
Filipe Manana34a28e32018-12-07 15:25:38 +00003946
3947 return ret;
3948}
3949
3950static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
3951 struct file *file_out, loff_t pos_out,
3952 loff_t *len, unsigned int remap_flags)
3953{
3954 struct inode *inode_in = file_inode(file_in);
3955 struct inode *inode_out = file_inode(file_out);
3956 u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize;
3957 bool same_inode = inode_out == inode_in;
3958 u64 wb_len;
3959 int ret;
3960
3961 if (!(remap_flags & REMAP_FILE_DEDUP)) {
3962 struct btrfs_root *root_out = BTRFS_I(inode_out)->root;
3963
3964 if (btrfs_root_readonly(root_out))
3965 return -EROFS;
3966
3967 if (file_in->f_path.mnt != file_out->f_path.mnt ||
3968 inode_in->i_sb != inode_out->i_sb)
3969 return -EXDEV;
3970 }
3971
Filipe Manana500710d2018-12-12 18:05:56 +00003972 /* don't make the dst file partly checksummed */
3973 if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) !=
3974 (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) {
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06003975 return -EINVAL;
Filipe Manana500710d2018-12-12 18:05:56 +00003976 }
3977
Filipe Manana34a28e32018-12-07 15:25:38 +00003978 /*
3979 * Now that the inodes are locked, we need to start writeback ourselves
3980 * and can not rely on the writeback from the VFS's generic helper
3981 * generic_remap_file_range_prep() because:
3982 *
3983 * 1) For compression we must call filemap_fdatawrite_range() range
3984 * twice (btrfs_fdatawrite_range() does it for us), and the generic
3985 * helper only calls it once;
3986 *
3987 * 2) filemap_fdatawrite_range(), called by the generic helper only
3988 * waits for the writeback to complete, i.e. for IO to be done, and
3989 * not for the ordered extents to complete. We need to wait for them
3990 * to complete so that new file extent items are in the fs tree.
3991 */
3992 if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP))
3993 wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs);
3994 else
3995 wb_len = ALIGN(*len, bs);
3996
3997 /*
3998 * Since we don't lock ranges, wait for ongoing lockless dio writes (as
3999 * any in progress could create its ordered extents after we wait for
4000 * existing ordered extents below).
4001 */
4002 inode_dio_wait(inode_in);
4003 if (!same_inode)
4004 inode_dio_wait(inode_out);
4005
4006 ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs),
4007 wb_len);
4008 if (ret < 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004009 return ret;
Filipe Manana34a28e32018-12-07 15:25:38 +00004010 ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs),
4011 wb_len);
4012 if (ret < 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004013 return ret;
Filipe Manana34a28e32018-12-07 15:25:38 +00004014
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004015 return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out,
Filipe Manana34a28e32018-12-07 15:25:38 +00004016 len, remap_flags);
Zach Brown3db11b22015-11-10 16:53:32 -05004017}
4018
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004019loff_t btrfs_remap_file_range(struct file *src_file, loff_t off,
4020 struct file *dst_file, loff_t destoff, loff_t len,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004021 unsigned int remap_flags)
Zach Brown3db11b22015-11-10 16:53:32 -05004022{
Filipe Manana34a28e32018-12-07 15:25:38 +00004023 struct inode *src_inode = file_inode(src_file);
4024 struct inode *dst_inode = file_inode(dst_file);
4025 bool same_inode = dst_inode == src_inode;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004026 int ret;
4027
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004028 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
4029 return -EINVAL;
4030
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004031 if (same_inode)
4032 inode_lock(src_inode);
4033 else
4034 lock_two_nondirectories(src_inode, dst_inode);
4035
Filipe Manana34a28e32018-12-07 15:25:38 +00004036 ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff,
4037 &len, remap_flags);
4038 if (ret < 0 || len == 0)
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004039 goto out_unlock;
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +11004040
Filipe Manana34a28e32018-12-07 15:25:38 +00004041 if (remap_flags & REMAP_FILE_DEDUP)
4042 ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff);
4043 else
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004044 ret = btrfs_clone_files(dst_file, src_file, off, len, destoff);
Filipe Manana34a28e32018-12-07 15:25:38 +00004045
Goldwyn Rodrigues7984ae52019-02-25 13:07:44 -06004046out_unlock:
Filipe Manana34a28e32018-12-07 15:25:38 +00004047 if (same_inode)
4048 inode_unlock(src_inode);
4049 else
Filipe Manana4ea748e2019-02-26 12:06:09 +00004050 unlock_two_nondirectories(src_inode, dst_inode);
Filipe Manana34a28e32018-12-07 15:25:38 +00004051
Darrick J. Wong42ec3d42018-10-30 10:41:49 +11004052 return ret < 0 ? ret : len;
Sage Weilc5c9cd42008-11-12 14:32:25 -05004053}
4054
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004055static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4056{
Al Viro496ad9a2013-01-23 17:07:38 -05004057 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004058 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004059 struct btrfs_root *root = BTRFS_I(inode)->root;
4060 struct btrfs_root *new_root;
4061 struct btrfs_dir_item *di;
4062 struct btrfs_trans_handle *trans;
4063 struct btrfs_path *path;
4064 struct btrfs_key location;
4065 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004066 u64 objectid = 0;
4067 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004068 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004069
4070 if (!capable(CAP_SYS_ADMIN))
4071 return -EPERM;
4072
Miao Xie3c04ce02012-11-26 08:43:07 +00004073 ret = mnt_want_write_file(file);
4074 if (ret)
4075 return ret;
4076
4077 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4078 ret = -EFAULT;
4079 goto out;
4080 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004081
4082 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304083 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004084
4085 location.objectid = objectid;
4086 location.type = BTRFS_ROOT_ITEM_KEY;
4087 location.offset = (u64)-1;
4088
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004089 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004090 if (IS_ERR(new_root)) {
4091 ret = PTR_ERR(new_root);
4092 goto out;
4093 }
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09004094 if (!is_fstree(new_root->root_key.objectid)) {
satoru takeuchi6d6d2822017-09-12 22:42:52 +09004095 ret = -ENOENT;
4096 goto out;
4097 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004098
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004099 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004100 if (!path) {
4101 ret = -ENOMEM;
4102 goto out;
4103 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004104 path->leave_spinning = 1;
4105
4106 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004107 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004108 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004109 ret = PTR_ERR(trans);
4110 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004111 }
4112
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004113 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4114 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004115 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004116 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004117 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004118 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004119 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004120 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004121 ret = -ENOENT;
4122 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004123 }
4124
4125 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4126 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4127 btrfs_mark_buffer_dirty(path->nodes[0]);
4128 btrfs_free_path(path);
4129
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004130 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004131 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004132out:
4133 mnt_drop_write_file(file);
4134 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004135}
4136
Su Yuec065f5b12018-04-02 17:24:11 +08004137static void get_block_group_info(struct list_head *groups_list,
4138 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004139{
4140 struct btrfs_block_group_cache *block_group;
4141
4142 space->total_bytes = 0;
4143 space->used_bytes = 0;
4144 space->flags = 0;
4145 list_for_each_entry(block_group, groups_list, list) {
4146 space->flags = block_group->flags;
4147 space->total_bytes += block_group->key.offset;
4148 space->used_bytes +=
4149 btrfs_block_group_used(&block_group->item);
4150 }
4151}
4152
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004153static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4154 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004155{
4156 struct btrfs_ioctl_space_args space_args;
4157 struct btrfs_ioctl_space_info space;
4158 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004159 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004160 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004161 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004162 static const u64 types[] = {
4163 BTRFS_BLOCK_GROUP_DATA,
4164 BTRFS_BLOCK_GROUP_SYSTEM,
4165 BTRFS_BLOCK_GROUP_METADATA,
4166 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4167 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004168 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004169 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004170 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004171 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004172 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004173
4174 if (copy_from_user(&space_args,
4175 (struct btrfs_ioctl_space_args __user *)arg,
4176 sizeof(space_args)))
4177 return -EFAULT;
4178
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004179 for (i = 0; i < num_types; i++) {
4180 struct btrfs_space_info *tmp;
4181
4182 info = NULL;
4183 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004184 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004185 list) {
4186 if (tmp->flags == types[i]) {
4187 info = tmp;
4188 break;
4189 }
4190 }
4191 rcu_read_unlock();
4192
4193 if (!info)
4194 continue;
4195
4196 down_read(&info->groups_sem);
4197 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4198 if (!list_empty(&info->block_groups[c]))
4199 slot_count++;
4200 }
4201 up_read(&info->groups_sem);
4202 }
Josef Bacik1406e432010-01-13 18:19:06 +00004203
David Sterba36523e952014-02-07 14:34:12 +01004204 /*
4205 * Global block reserve, exported as a space_info
4206 */
4207 slot_count++;
4208
Chris Mason7fde62b2010-03-16 15:40:10 -04004209 /* space_slots == 0 means they are asking for a count */
4210 if (space_args.space_slots == 0) {
4211 space_args.total_spaces = slot_count;
4212 goto out;
4213 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004214
Dan Rosenberg51788b12011-02-14 16:04:23 -05004215 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004216
Chris Mason7fde62b2010-03-16 15:40:10 -04004217 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004218
Chris Mason7fde62b2010-03-16 15:40:10 -04004219 /* we generally have at most 6 or so space infos, one for each raid
4220 * level. So, a whole page should be more than enough for everyone
4221 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004222 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004223 return -ENOMEM;
4224
4225 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004226 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004227 if (!dest)
4228 return -ENOMEM;
4229 dest_orig = dest;
4230
4231 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004232 for (i = 0; i < num_types; i++) {
4233 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004234
Dan Rosenberg51788b12011-02-14 16:04:23 -05004235 if (!slot_count)
4236 break;
4237
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004238 info = NULL;
4239 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004240 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004241 list) {
4242 if (tmp->flags == types[i]) {
4243 info = tmp;
4244 break;
4245 }
4246 }
4247 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004248
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004249 if (!info)
4250 continue;
4251 down_read(&info->groups_sem);
4252 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4253 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004254 get_block_group_info(&info->block_groups[c],
4255 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004256 memcpy(dest, &space, sizeof(space));
4257 dest++;
4258 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004259 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004260 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004261 if (!slot_count)
4262 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004263 }
4264 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004265 }
Josef Bacik1406e432010-01-13 18:19:06 +00004266
David Sterba36523e952014-02-07 14:34:12 +01004267 /*
4268 * Add global block reserve
4269 */
4270 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004271 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004272
4273 spin_lock(&block_rsv->lock);
4274 space.total_bytes = block_rsv->size;
4275 space.used_bytes = block_rsv->size - block_rsv->reserved;
4276 spin_unlock(&block_rsv->lock);
4277 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4278 memcpy(dest, &space, sizeof(space));
4279 space_args.total_spaces++;
4280 }
4281
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004282 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004283 (arg + sizeof(struct btrfs_ioctl_space_args));
4284
4285 if (copy_to_user(user_dest, dest_orig, alloc_size))
4286 ret = -EFAULT;
4287
4288 kfree(dest_orig);
4289out:
4290 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004291 ret = -EFAULT;
4292
4293 return ret;
4294}
4295
Miao Xie9a8c28b2012-11-26 08:40:43 +00004296static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4297 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004298{
Sage Weil46204592010-10-29 15:41:32 -04004299 struct btrfs_trans_handle *trans;
4300 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004301 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004302
Miao Xied4edf392013-02-20 09:17:06 +00004303 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004304 if (IS_ERR(trans)) {
4305 if (PTR_ERR(trans) != -ENOENT)
4306 return PTR_ERR(trans);
4307
4308 /* No running transaction, don't bother */
4309 transid = root->fs_info->last_trans_committed;
4310 goto out;
4311 }
Sage Weil46204592010-10-29 15:41:32 -04004312 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004313 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004314 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004315 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004316 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004317 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004318out:
Sage Weil46204592010-10-29 15:41:32 -04004319 if (argp)
4320 if (copy_to_user(argp, &transid, sizeof(transid)))
4321 return -EFAULT;
4322 return 0;
4323}
4324
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004325static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004326 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004327{
Sage Weil46204592010-10-29 15:41:32 -04004328 u64 transid;
4329
4330 if (argp) {
4331 if (copy_from_user(&transid, argp, sizeof(transid)))
4332 return -EFAULT;
4333 } else {
4334 transid = 0; /* current trans */
4335 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004336 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004337}
4338
Miao Xieb8e95482012-11-26 08:48:01 +00004339static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004340{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004341 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004342 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004343 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004344
4345 if (!capable(CAP_SYS_ADMIN))
4346 return -EPERM;
4347
4348 sa = memdup_user(arg, sizeof(*sa));
4349 if (IS_ERR(sa))
4350 return PTR_ERR(sa);
4351
Miao Xieb8e95482012-11-26 08:48:01 +00004352 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4353 ret = mnt_want_write_file(file);
4354 if (ret)
4355 goto out;
4356 }
4357
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004358 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004359 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4360 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004361
Filipe Manana06fe39a2018-12-14 19:50:17 +00004362 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004363 ret = -EFAULT;
4364
Miao Xieb8e95482012-11-26 08:48:01 +00004365 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4366 mnt_drop_write_file(file);
4367out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004368 kfree(sa);
4369 return ret;
4370}
4371
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004372static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004373{
4374 if (!capable(CAP_SYS_ADMIN))
4375 return -EPERM;
4376
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004377 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004378}
4379
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004380static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004381 void __user *arg)
4382{
4383 struct btrfs_ioctl_scrub_args *sa;
4384 int ret;
4385
4386 if (!capable(CAP_SYS_ADMIN))
4387 return -EPERM;
4388
4389 sa = memdup_user(arg, sizeof(*sa));
4390 if (IS_ERR(sa))
4391 return PTR_ERR(sa);
4392
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004393 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004394
Filipe Manana4fa99b02018-12-14 19:45:13 +00004395 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01004396 ret = -EFAULT;
4397
4398 kfree(sa);
4399 return ret;
4400}
4401
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004402static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004403 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004404{
4405 struct btrfs_ioctl_get_dev_stats *sa;
4406 int ret;
4407
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004408 sa = memdup_user(arg, sizeof(*sa));
4409 if (IS_ERR(sa))
4410 return PTR_ERR(sa);
4411
David Sterbab27f7c02012-06-22 06:30:39 -06004412 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4413 kfree(sa);
4414 return -EPERM;
4415 }
4416
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004417 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004418
Filipe Mananaeee99572018-12-14 19:45:22 +00004419 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004420 ret = -EFAULT;
4421
4422 kfree(sa);
4423 return ret;
4424}
4425
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004426static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4427 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004428{
4429 struct btrfs_ioctl_dev_replace_args *p;
4430 int ret;
4431
4432 if (!capable(CAP_SYS_ADMIN))
4433 return -EPERM;
4434
4435 p = memdup_user(arg, sizeof(*p));
4436 if (IS_ERR(p))
4437 return PTR_ERR(p);
4438
4439 switch (p->cmd) {
4440 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004441 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004442 ret = -EROFS;
4443 goto out;
4444 }
David Sterba171938e2017-03-28 14:44:21 +02004445 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004446 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004447 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004448 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004449 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004450 }
4451 break;
4452 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004453 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004454 ret = 0;
4455 break;
4456 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004457 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004458 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004459 break;
4460 default:
4461 ret = -EINVAL;
4462 break;
4463 }
4464
Filipe Mananad3a53282019-01-08 11:42:09 +00004465 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004466 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004467out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004468 kfree(p);
4469 return ret;
4470}
4471
Jan Schmidtd7728c92011-07-07 16:48:38 +02004472static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4473{
4474 int ret = 0;
4475 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004476 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004477 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004478 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004479 struct inode_fs_paths *ipath = NULL;
4480 struct btrfs_path *path;
4481
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004482 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004483 return -EPERM;
4484
4485 path = btrfs_alloc_path();
4486 if (!path) {
4487 ret = -ENOMEM;
4488 goto out;
4489 }
4490
4491 ipa = memdup_user(arg, sizeof(*ipa));
4492 if (IS_ERR(ipa)) {
4493 ret = PTR_ERR(ipa);
4494 ipa = NULL;
4495 goto out;
4496 }
4497
4498 size = min_t(u32, ipa->size, 4096);
4499 ipath = init_ipath(size, root, path);
4500 if (IS_ERR(ipath)) {
4501 ret = PTR_ERR(ipath);
4502 ipath = NULL;
4503 goto out;
4504 }
4505
4506 ret = paths_from_inode(ipa->inum, ipath);
4507 if (ret < 0)
4508 goto out;
4509
4510 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004511 rel_ptr = ipath->fspath->val[i] -
4512 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004513 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004514 }
4515
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004516 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4517 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004518 if (ret) {
4519 ret = -EFAULT;
4520 goto out;
4521 }
4522
4523out:
4524 btrfs_free_path(path);
4525 free_ipath(ipath);
4526 kfree(ipa);
4527
4528 return ret;
4529}
4530
4531static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4532{
4533 struct btrfs_data_container *inodes = ctx;
4534 const size_t c = 3 * sizeof(u64);
4535
4536 if (inodes->bytes_left >= c) {
4537 inodes->bytes_left -= c;
4538 inodes->val[inodes->elem_cnt] = inum;
4539 inodes->val[inodes->elem_cnt + 1] = offset;
4540 inodes->val[inodes->elem_cnt + 2] = root;
4541 inodes->elem_cnt += 3;
4542 } else {
4543 inodes->bytes_missing += c - inodes->bytes_left;
4544 inodes->bytes_left = 0;
4545 inodes->elem_missed += 3;
4546 }
4547
4548 return 0;
4549}
4550
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004551static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004552 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004553{
4554 int ret = 0;
4555 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004556 struct btrfs_ioctl_logical_ino_args *loi;
4557 struct btrfs_data_container *inodes = NULL;
4558 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004559 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004560
4561 if (!capable(CAP_SYS_ADMIN))
4562 return -EPERM;
4563
4564 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304565 if (IS_ERR(loi))
4566 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004567
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004568 if (version == 1) {
4569 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004570 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004571 } else {
4572 /* All reserved bits must be 0 for now */
4573 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4574 ret = -EINVAL;
4575 goto out_loi;
4576 }
4577 /* Only accept flags we have defined so far */
4578 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4579 ret = -EINVAL;
4580 goto out_loi;
4581 }
4582 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004583 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004584 }
4585
Jan Schmidtd7728c92011-07-07 16:48:38 +02004586 path = btrfs_alloc_path();
4587 if (!path) {
4588 ret = -ENOMEM;
4589 goto out;
4590 }
4591
Jan Schmidtd7728c92011-07-07 16:48:38 +02004592 inodes = init_data_container(size);
4593 if (IS_ERR(inodes)) {
4594 ret = PTR_ERR(inodes);
4595 inodes = NULL;
4596 goto out;
4597 }
4598
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004599 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004600 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004601 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004602 ret = -ENOENT;
4603 if (ret < 0)
4604 goto out;
4605
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004606 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4607 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004608 if (ret)
4609 ret = -EFAULT;
4610
4611out:
4612 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004613 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004614out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004615 kfree(loi);
4616
4617 return ret;
4618}
4619
David Sterba008ef092018-03-21 02:05:27 +01004620void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004621 struct btrfs_ioctl_balance_args *bargs)
4622{
4623 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4624
4625 bargs->flags = bctl->flags;
4626
David Sterba3009a622018-03-21 01:31:04 +01004627 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004628 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4629 if (atomic_read(&fs_info->balance_pause_req))
4630 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004631 if (atomic_read(&fs_info->balance_cancel_req))
4632 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004633
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004634 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4635 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4636 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004637
David Sterba008ef092018-03-21 02:05:27 +01004638 spin_lock(&fs_info->balance_lock);
4639 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4640 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004641}
4642
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004643static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004644{
Al Viro496ad9a2013-01-23 17:07:38 -05004645 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004646 struct btrfs_fs_info *fs_info = root->fs_info;
4647 struct btrfs_ioctl_balance_args *bargs;
4648 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004649 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004650 int ret;
4651
4652 if (!capable(CAP_SYS_ADMIN))
4653 return -EPERM;
4654
Liu Boe54bfa312012-06-29 03:58:48 -06004655 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004656 if (ret)
4657 return ret;
4658
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004659again:
David Sterba171938e2017-03-28 14:44:21 +02004660 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004661 mutex_lock(&fs_info->balance_mutex);
4662 need_unlock = true;
4663 goto locked;
4664 }
4665
4666 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004667 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004668 * (1) some other op is running
4669 * (2) balance is running
4670 * (3) balance is paused -- special case (think resume)
4671 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004672 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004673 if (fs_info->balance_ctl) {
4674 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004675 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004676 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004677 /*
4678 * Lock released to allow other waiters to continue,
4679 * we'll reexamine the status again.
4680 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004681 mutex_lock(&fs_info->balance_mutex);
4682
4683 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004684 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004685 /* this is (3) */
4686 need_unlock = false;
4687 goto locked;
4688 }
4689
4690 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004691 goto again;
4692 } else {
4693 /* this is (2) */
4694 mutex_unlock(&fs_info->balance_mutex);
4695 ret = -EINPROGRESS;
4696 goto out;
4697 }
4698 } else {
4699 /* this is (1) */
4700 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004701 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004702 goto out;
4703 }
4704
4705locked:
David Sterba171938e2017-03-28 14:44:21 +02004706 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004707
4708 if (arg) {
4709 bargs = memdup_user(arg, sizeof(*bargs));
4710 if (IS_ERR(bargs)) {
4711 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004712 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004713 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004714
4715 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4716 if (!fs_info->balance_ctl) {
4717 ret = -ENOTCONN;
4718 goto out_bargs;
4719 }
4720
4721 bctl = fs_info->balance_ctl;
4722 spin_lock(&fs_info->balance_lock);
4723 bctl->flags |= BTRFS_BALANCE_RESUME;
4724 spin_unlock(&fs_info->balance_lock);
4725
4726 goto do_balance;
4727 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004728 } else {
4729 bargs = NULL;
4730 }
4731
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004732 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004733 ret = -EINPROGRESS;
4734 goto out_bargs;
4735 }
4736
David Sterba8d2db782015-11-04 15:38:29 +01004737 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004738 if (!bctl) {
4739 ret = -ENOMEM;
4740 goto out_bargs;
4741 }
4742
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004743 if (arg) {
4744 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4745 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4746 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4747
4748 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004749 } else {
4750 /* balance everything - no filters */
4751 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004752 }
4753
David Sterba8eb93452015-10-12 16:55:54 +02004754 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4755 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004756 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004757 }
4758
Ilya Dryomovde322262012-01-16 22:04:49 +02004759do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004760 /*
David Sterba149196a2018-03-20 20:23:09 +01004761 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4762 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4763 * restriper was paused all the way until unmount, in free_fs_info.
4764 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004765 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004766 need_unlock = false;
4767
David Sterba6fcf6e22018-05-07 17:44:03 +02004768 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004769 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004770
Filipe Mananad00c2d92019-01-08 11:42:01 +00004771 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004772 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4773 ret = -EFAULT;
4774 }
4775
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004776out_bctl:
4777 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004778out_bargs:
4779 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004780out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004781 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004782 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004783 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004784out:
Liu Boe54bfa312012-06-29 03:58:48 -06004785 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004786 return ret;
4787}
4788
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004789static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004790{
4791 if (!capable(CAP_SYS_ADMIN))
4792 return -EPERM;
4793
4794 switch (cmd) {
4795 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004796 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004797 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004798 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004799 }
4800
4801 return -EINVAL;
4802}
4803
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004804static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004805 void __user *arg)
4806{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004807 struct btrfs_ioctl_balance_args *bargs;
4808 int ret = 0;
4809
4810 if (!capable(CAP_SYS_ADMIN))
4811 return -EPERM;
4812
4813 mutex_lock(&fs_info->balance_mutex);
4814 if (!fs_info->balance_ctl) {
4815 ret = -ENOTCONN;
4816 goto out;
4817 }
4818
David Sterba8d2db782015-11-04 15:38:29 +01004819 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004820 if (!bargs) {
4821 ret = -ENOMEM;
4822 goto out;
4823 }
4824
David Sterba008ef092018-03-21 02:05:27 +01004825 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004826
4827 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4828 ret = -EFAULT;
4829
4830 kfree(bargs);
4831out:
4832 mutex_unlock(&fs_info->balance_mutex);
4833 return ret;
4834}
4835
Miao Xie905b0dd2012-11-26 08:50:11 +00004836static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004837{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004838 struct inode *inode = file_inode(file);
4839 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004840 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004841 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004842
4843 if (!capable(CAP_SYS_ADMIN))
4844 return -EPERM;
4845
Miao Xie905b0dd2012-11-26 08:50:11 +00004846 ret = mnt_want_write_file(file);
4847 if (ret)
4848 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004849
4850 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004851 if (IS_ERR(sa)) {
4852 ret = PTR_ERR(sa);
4853 goto drop_write;
4854 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004855
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004856 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004857
4858 switch (sa->cmd) {
4859 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004860 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004861 break;
4862 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004863 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004864 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004865 default:
4866 ret = -EINVAL;
4867 break;
4868 }
4869
Arne Jansen5d13a372011-09-14 15:53:51 +02004870 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004871 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004872drop_write:
4873 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004874 return ret;
4875}
4876
Miao Xie905b0dd2012-11-26 08:50:11 +00004877static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004878{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004879 struct inode *inode = file_inode(file);
4880 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4881 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004882 struct btrfs_ioctl_qgroup_assign_args *sa;
4883 struct btrfs_trans_handle *trans;
4884 int ret;
4885 int err;
4886
4887 if (!capable(CAP_SYS_ADMIN))
4888 return -EPERM;
4889
Miao Xie905b0dd2012-11-26 08:50:11 +00004890 ret = mnt_want_write_file(file);
4891 if (ret)
4892 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004893
4894 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004895 if (IS_ERR(sa)) {
4896 ret = PTR_ERR(sa);
4897 goto drop_write;
4898 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004899
4900 trans = btrfs_join_transaction(root);
4901 if (IS_ERR(trans)) {
4902 ret = PTR_ERR(trans);
4903 goto out;
4904 }
4905
Arne Jansen5d13a372011-09-14 15:53:51 +02004906 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08004907 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004908 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004909 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004910 }
4911
Qu Wenruoe082f562015-02-27 16:24:28 +08004912 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004913 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004914 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004915 btrfs_handle_fs_error(fs_info, err,
4916 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004917 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004918 if (err && !ret)
4919 ret = err;
4920
4921out:
4922 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004923drop_write:
4924 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004925 return ret;
4926}
4927
Miao Xie905b0dd2012-11-26 08:50:11 +00004928static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004929{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004930 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004931 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004932 struct btrfs_ioctl_qgroup_create_args *sa;
4933 struct btrfs_trans_handle *trans;
4934 int ret;
4935 int err;
4936
4937 if (!capable(CAP_SYS_ADMIN))
4938 return -EPERM;
4939
Miao Xie905b0dd2012-11-26 08:50:11 +00004940 ret = mnt_want_write_file(file);
4941 if (ret)
4942 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004943
4944 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004945 if (IS_ERR(sa)) {
4946 ret = PTR_ERR(sa);
4947 goto drop_write;
4948 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004949
Miao Xied86e56c2012-11-15 11:35:41 +00004950 if (!sa->qgroupid) {
4951 ret = -EINVAL;
4952 goto out;
4953 }
4954
Arne Jansen5d13a372011-09-14 15:53:51 +02004955 trans = btrfs_join_transaction(root);
4956 if (IS_ERR(trans)) {
4957 ret = PTR_ERR(trans);
4958 goto out;
4959 }
4960
Arne Jansen5d13a372011-09-14 15:53:51 +02004961 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004962 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004963 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004964 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004965 }
4966
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004967 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004968 if (err && !ret)
4969 ret = err;
4970
4971out:
4972 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004973drop_write:
4974 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004975 return ret;
4976}
4977
Miao Xie905b0dd2012-11-26 08:50:11 +00004978static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004979{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004980 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004981 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004982 struct btrfs_ioctl_qgroup_limit_args *sa;
4983 struct btrfs_trans_handle *trans;
4984 int ret;
4985 int err;
4986 u64 qgroupid;
4987
4988 if (!capable(CAP_SYS_ADMIN))
4989 return -EPERM;
4990
Miao Xie905b0dd2012-11-26 08:50:11 +00004991 ret = mnt_want_write_file(file);
4992 if (ret)
4993 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004994
4995 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004996 if (IS_ERR(sa)) {
4997 ret = PTR_ERR(sa);
4998 goto drop_write;
4999 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005000
5001 trans = btrfs_join_transaction(root);
5002 if (IS_ERR(trans)) {
5003 ret = PTR_ERR(trans);
5004 goto out;
5005 }
5006
5007 qgroupid = sa->qgroupid;
5008 if (!qgroupid) {
5009 /* take the current subvol as qgroup */
5010 qgroupid = root->root_key.objectid;
5011 }
5012
Lu Fengqif0042d52018-07-18 14:45:35 +08005013 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02005014
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005015 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005016 if (err && !ret)
5017 ret = err;
5018
5019out:
5020 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005021drop_write:
5022 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005023 return ret;
5024}
5025
Jan Schmidt2f232032013-04-25 16:04:51 +00005026static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5027{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005028 struct inode *inode = file_inode(file);
5029 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005030 struct btrfs_ioctl_quota_rescan_args *qsa;
5031 int ret;
5032
5033 if (!capable(CAP_SYS_ADMIN))
5034 return -EPERM;
5035
5036 ret = mnt_want_write_file(file);
5037 if (ret)
5038 return ret;
5039
5040 qsa = memdup_user(arg, sizeof(*qsa));
5041 if (IS_ERR(qsa)) {
5042 ret = PTR_ERR(qsa);
5043 goto drop_write;
5044 }
5045
5046 if (qsa->flags) {
5047 ret = -EINVAL;
5048 goto out;
5049 }
5050
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005051 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005052
5053out:
5054 kfree(qsa);
5055drop_write:
5056 mnt_drop_write_file(file);
5057 return ret;
5058}
5059
5060static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5061{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005062 struct inode *inode = file_inode(file);
5063 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005064 struct btrfs_ioctl_quota_rescan_args *qsa;
5065 int ret = 0;
5066
5067 if (!capable(CAP_SYS_ADMIN))
5068 return -EPERM;
5069
David Sterba8d2db782015-11-04 15:38:29 +01005070 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005071 if (!qsa)
5072 return -ENOMEM;
5073
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005074 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005075 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005076 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005077 }
5078
5079 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5080 ret = -EFAULT;
5081
5082 kfree(qsa);
5083 return ret;
5084}
5085
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005086static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5087{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005088 struct inode *inode = file_inode(file);
5089 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005090
5091 if (!capable(CAP_SYS_ADMIN))
5092 return -EPERM;
5093
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005094 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005095}
5096
Hugo Millsabccd002014-01-30 20:17:00 +00005097static long _btrfs_ioctl_set_received_subvol(struct file *file,
5098 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005099{
Al Viro496ad9a2013-01-23 17:07:38 -05005100 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005101 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005102 struct btrfs_root *root = BTRFS_I(inode)->root;
5103 struct btrfs_root_item *root_item = &root->root_item;
5104 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07005105 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005106 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005107 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005108
David Sterbabd60ea02014-01-16 15:50:22 +01005109 if (!inode_owner_or_capable(inode))
5110 return -EPERM;
5111
Alexander Block8ea05e32012-07-25 17:35:53 +02005112 ret = mnt_want_write_file(file);
5113 if (ret < 0)
5114 return ret;
5115
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005116 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005117
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005118 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005119 ret = -EINVAL;
5120 goto out;
5121 }
5122
5123 if (btrfs_root_readonly(root)) {
5124 ret = -EROFS;
5125 goto out;
5126 }
5127
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005128 /*
5129 * 1 - root item
5130 * 2 - uuid items (received uuid + subvol uuid)
5131 */
5132 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005133 if (IS_ERR(trans)) {
5134 ret = PTR_ERR(trans);
5135 trans = NULL;
5136 goto out;
5137 }
5138
5139 sa->rtransid = trans->transid;
5140 sa->rtime.sec = ct.tv_sec;
5141 sa->rtime.nsec = ct.tv_nsec;
5142
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005143 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5144 BTRFS_UUID_SIZE);
5145 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005146 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005147 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005148 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5149 root->root_key.objectid);
5150 if (ret && ret != -ENOENT) {
5151 btrfs_abort_transaction(trans, ret);
5152 btrfs_end_transaction(trans);
5153 goto out;
5154 }
5155 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005156 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5157 btrfs_set_root_stransid(root_item, sa->stransid);
5158 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005159 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5160 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5161 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5162 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005163
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005164 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005165 &root->root_key, &root->root_item);
5166 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005167 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005168 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005169 }
5170 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005171 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005172 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5173 root->root_key.objectid);
5174 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005175 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005176 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005177 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005178 }
5179 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005180 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005181out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005182 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005183 mnt_drop_write_file(file);
5184 return ret;
5185}
5186
5187#ifdef CONFIG_64BIT
5188static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5189 void __user *arg)
5190{
5191 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5192 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5193 int ret = 0;
5194
5195 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305196 if (IS_ERR(args32))
5197 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005198
David Sterba8d2db782015-11-04 15:38:29 +01005199 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005200 if (!args64) {
5201 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005202 goto out;
5203 }
5204
5205 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5206 args64->stransid = args32->stransid;
5207 args64->rtransid = args32->rtransid;
5208 args64->stime.sec = args32->stime.sec;
5209 args64->stime.nsec = args32->stime.nsec;
5210 args64->rtime.sec = args32->rtime.sec;
5211 args64->rtime.nsec = args32->rtime.nsec;
5212 args64->flags = args32->flags;
5213
5214 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5215 if (ret)
5216 goto out;
5217
5218 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5219 args32->stransid = args64->stransid;
5220 args32->rtransid = args64->rtransid;
5221 args32->stime.sec = args64->stime.sec;
5222 args32->stime.nsec = args64->stime.nsec;
5223 args32->rtime.sec = args64->rtime.sec;
5224 args32->rtime.nsec = args64->rtime.nsec;
5225 args32->flags = args64->flags;
5226
5227 ret = copy_to_user(arg, args32, sizeof(*args32));
5228 if (ret)
5229 ret = -EFAULT;
5230
5231out:
5232 kfree(args32);
5233 kfree(args64);
5234 return ret;
5235}
5236#endif
5237
5238static long btrfs_ioctl_set_received_subvol(struct file *file,
5239 void __user *arg)
5240{
5241 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5242 int ret = 0;
5243
5244 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305245 if (IS_ERR(sa))
5246 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005247
5248 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5249
5250 if (ret)
5251 goto out;
5252
Alexander Block8ea05e32012-07-25 17:35:53 +02005253 ret = copy_to_user(arg, sa, sizeof(*sa));
5254 if (ret)
5255 ret = -EFAULT;
5256
5257out:
5258 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005259 return ret;
5260}
5261
jeff.liu867ab662013-01-05 02:48:01 +00005262static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005264 struct inode *inode = file_inode(file);
5265 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005266 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005267 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005268 char label[BTRFS_LABEL_SIZE];
5269
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005270 spin_lock(&fs_info->super_lock);
5271 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5272 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005273
5274 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005275
5276 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005277 btrfs_warn(fs_info,
5278 "label is too long, return the first %zu bytes",
5279 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005280 }
5281
jeff.liu867ab662013-01-05 02:48:01 +00005282 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005283
5284 return ret ? -EFAULT : 0;
5285}
5286
jeff.liua8bfd4a2013-01-05 02:48:08 +00005287static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5288{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005289 struct inode *inode = file_inode(file);
5290 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5291 struct btrfs_root *root = BTRFS_I(inode)->root;
5292 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005293 struct btrfs_trans_handle *trans;
5294 char label[BTRFS_LABEL_SIZE];
5295 int ret;
5296
5297 if (!capable(CAP_SYS_ADMIN))
5298 return -EPERM;
5299
5300 if (copy_from_user(label, arg, sizeof(label)))
5301 return -EFAULT;
5302
5303 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005304 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005305 "unable to set label with more than %d bytes",
5306 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005307 return -EINVAL;
5308 }
5309
5310 ret = mnt_want_write_file(file);
5311 if (ret)
5312 return ret;
5313
jeff.liua8bfd4a2013-01-05 02:48:08 +00005314 trans = btrfs_start_transaction(root, 0);
5315 if (IS_ERR(trans)) {
5316 ret = PTR_ERR(trans);
5317 goto out_unlock;
5318 }
5319
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005320 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005321 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005322 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005323 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005324
5325out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005326 mnt_drop_write_file(file);
5327 return ret;
5328}
5329
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005330#define INIT_FEATURE_FLAGS(suffix) \
5331 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5332 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5333 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5334
David Sterbad5131b62016-02-17 15:26:27 +01005335int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005336{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005337 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005338 INIT_FEATURE_FLAGS(SUPP),
5339 INIT_FEATURE_FLAGS(SAFE_SET),
5340 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5341 };
5342
5343 if (copy_to_user(arg, &features, sizeof(features)))
5344 return -EFAULT;
5345
5346 return 0;
5347}
5348
5349static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5350{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005351 struct inode *inode = file_inode(file);
5352 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5353 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005354 struct btrfs_ioctl_feature_flags features;
5355
5356 features.compat_flags = btrfs_super_compat_flags(super_block);
5357 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5358 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5359
5360 if (copy_to_user(arg, &features, sizeof(features)))
5361 return -EFAULT;
5362
5363 return 0;
5364}
5365
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005366static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005367 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005368 u64 change_mask, u64 flags, u64 supported_flags,
5369 u64 safe_set, u64 safe_clear)
5370{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005371 const char *type = btrfs_feature_set_names[set];
5372 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005373 u64 disallowed, unsupported;
5374 u64 set_mask = flags & change_mask;
5375 u64 clear_mask = ~flags & change_mask;
5376
5377 unsupported = set_mask & ~supported_flags;
5378 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005379 names = btrfs_printable_features(set, unsupported);
5380 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005381 btrfs_warn(fs_info,
5382 "this kernel does not support the %s feature bit%s",
5383 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005384 kfree(names);
5385 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005386 btrfs_warn(fs_info,
5387 "this kernel does not support %s bits 0x%llx",
5388 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005389 return -EOPNOTSUPP;
5390 }
5391
5392 disallowed = set_mask & ~safe_set;
5393 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005394 names = btrfs_printable_features(set, disallowed);
5395 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005396 btrfs_warn(fs_info,
5397 "can't set the %s feature bit%s while mounted",
5398 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005399 kfree(names);
5400 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005401 btrfs_warn(fs_info,
5402 "can't set %s bits 0x%llx while mounted",
5403 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005404 return -EPERM;
5405 }
5406
5407 disallowed = clear_mask & ~safe_clear;
5408 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005409 names = btrfs_printable_features(set, disallowed);
5410 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005411 btrfs_warn(fs_info,
5412 "can't clear the %s feature bit%s while mounted",
5413 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005414 kfree(names);
5415 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005416 btrfs_warn(fs_info,
5417 "can't clear %s bits 0x%llx while mounted",
5418 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005419 return -EPERM;
5420 }
5421
5422 return 0;
5423}
5424
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005425#define check_feature(fs_info, change_mask, flags, mask_base) \
5426check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005427 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5428 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5429 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5430
5431static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5432{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005433 struct inode *inode = file_inode(file);
5434 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5435 struct btrfs_root *root = BTRFS_I(inode)->root;
5436 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005437 struct btrfs_ioctl_feature_flags flags[2];
5438 struct btrfs_trans_handle *trans;
5439 u64 newflags;
5440 int ret;
5441
5442 if (!capable(CAP_SYS_ADMIN))
5443 return -EPERM;
5444
5445 if (copy_from_user(flags, arg, sizeof(flags)))
5446 return -EFAULT;
5447
5448 /* Nothing to do */
5449 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5450 !flags[0].incompat_flags)
5451 return 0;
5452
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005453 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005454 flags[1].compat_flags, COMPAT);
5455 if (ret)
5456 return ret;
5457
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005458 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005459 flags[1].compat_ro_flags, COMPAT_RO);
5460 if (ret)
5461 return ret;
5462
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005463 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005464 flags[1].incompat_flags, INCOMPAT);
5465 if (ret)
5466 return ret;
5467
David Sterba7ab19622016-05-04 11:32:00 +02005468 ret = mnt_want_write_file(file);
5469 if (ret)
5470 return ret;
5471
David Sterba8051aa12014-02-07 14:34:04 +01005472 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005473 if (IS_ERR(trans)) {
5474 ret = PTR_ERR(trans);
5475 goto out_drop_write;
5476 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005477
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005478 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005479 newflags = btrfs_super_compat_flags(super_block);
5480 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5481 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5482 btrfs_set_super_compat_flags(super_block, newflags);
5483
5484 newflags = btrfs_super_compat_ro_flags(super_block);
5485 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5486 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5487 btrfs_set_super_compat_ro_flags(super_block, newflags);
5488
5489 newflags = btrfs_super_incompat_flags(super_block);
5490 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5491 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5492 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005493 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005494
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005495 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005496out_drop_write:
5497 mnt_drop_write_file(file);
5498
5499 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005500}
5501
Josef Bacik2351f432017-09-27 10:43:13 -04005502static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5503{
5504 struct btrfs_ioctl_send_args *arg;
5505 int ret;
5506
5507 if (compat) {
5508#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5509 struct btrfs_ioctl_send_args_32 args32;
5510
5511 ret = copy_from_user(&args32, argp, sizeof(args32));
5512 if (ret)
5513 return -EFAULT;
5514 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5515 if (!arg)
5516 return -ENOMEM;
5517 arg->send_fd = args32.send_fd;
5518 arg->clone_sources_count = args32.clone_sources_count;
5519 arg->clone_sources = compat_ptr(args32.clone_sources);
5520 arg->parent_root = args32.parent_root;
5521 arg->flags = args32.flags;
5522 memcpy(arg->reserved, args32.reserved,
5523 sizeof(args32.reserved));
5524#else
5525 return -ENOTTY;
5526#endif
5527 } else {
5528 arg = memdup_user(argp, sizeof(*arg));
5529 if (IS_ERR(arg))
5530 return PTR_ERR(arg);
5531 }
5532 ret = btrfs_ioctl_send(file, arg);
5533 kfree(arg);
5534 return ret;
5535}
5536
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005537long btrfs_ioctl(struct file *file, unsigned int
5538 cmd, unsigned long arg)
5539{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005540 struct inode *inode = file_inode(file);
5541 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5542 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005543 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005544
5545 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005546 case FS_IOC_GETFLAGS:
5547 return btrfs_ioctl_getflags(file, argp);
5548 case FS_IOC_SETFLAGS:
5549 return btrfs_ioctl_setflags(file, argp);
5550 case FS_IOC_GETVERSION:
5551 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005552 case FITRIM:
5553 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005554 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005555 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005556 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005557 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005558 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005559 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005560 case BTRFS_IOC_SUBVOL_CREATE_V2:
5561 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005562 case BTRFS_IOC_SNAP_DESTROY:
5563 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005564 case BTRFS_IOC_SUBVOL_GETFLAGS:
5565 return btrfs_ioctl_subvol_getflags(file, argp);
5566 case BTRFS_IOC_SUBVOL_SETFLAGS:
5567 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005568 case BTRFS_IOC_DEFAULT_SUBVOL:
5569 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005570 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005571 return btrfs_ioctl_defrag(file, NULL);
5572 case BTRFS_IOC_DEFRAG_RANGE:
5573 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005574 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005575 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005576 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005577 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005578 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005579 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005580 case BTRFS_IOC_RM_DEV_V2:
5581 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005582 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005583 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005584 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005585 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005586 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005587 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005588 case BTRFS_IOC_TREE_SEARCH:
5589 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005590 case BTRFS_IOC_TREE_SEARCH_V2:
5591 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005592 case BTRFS_IOC_INO_LOOKUP:
5593 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005594 case BTRFS_IOC_INO_PATHS:
5595 return btrfs_ioctl_ino_to_path(root, argp);
5596 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005597 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5598 case BTRFS_IOC_LOGICAL_INO_V2:
5599 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005600 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005601 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005602 case BTRFS_IOC_SYNC: {
5603 int ret;
5604
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005605 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005606 if (ret)
5607 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005608 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005609 /*
5610 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005611 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005612 * processing uncleaned subvols.
5613 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005614 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005615 return ret;
5616 }
Sage Weil46204592010-10-29 15:41:32 -04005617 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005618 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005619 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005620 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005621 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005622 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005623 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005624 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005625 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005626 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005627 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005628 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005629 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005630 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005631 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005632 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005633 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5634 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005635#ifdef CONFIG_64BIT
5636 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5637 return btrfs_ioctl_set_received_subvol_32(file, argp);
5638#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005639 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005640 return _btrfs_ioctl_send(file, argp, false);
5641#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5642 case BTRFS_IOC_SEND_32:
5643 return _btrfs_ioctl_send(file, argp, true);
5644#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005645 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005646 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005647 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005648 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005649 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005650 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005651 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005652 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005653 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005654 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005655 case BTRFS_IOC_QUOTA_RESCAN:
5656 return btrfs_ioctl_quota_rescan(file, argp);
5657 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5658 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005659 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5660 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005661 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005662 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005663 case BTRFS_IOC_GET_FSLABEL:
5664 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005665 case BTRFS_IOC_SET_FSLABEL:
5666 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005667 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005668 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005669 case BTRFS_IOC_GET_FEATURES:
5670 return btrfs_ioctl_get_features(file, argp);
5671 case BTRFS_IOC_SET_FEATURES:
5672 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005673 case FS_IOC_FSGETXATTR:
5674 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005675 case FS_IOC_FSSETXATTR:
5676 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005677 case BTRFS_IOC_GET_SUBVOL_INFO:
5678 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005679 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5680 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005681 case BTRFS_IOC_INO_LOOKUP_USER:
5682 return btrfs_ioctl_ino_lookup_user(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005683 }
5684
5685 return -ENOTTY;
5686}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005687
5688#ifdef CONFIG_COMPAT
5689long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5690{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005691 /*
5692 * These all access 32-bit values anyway so no further
5693 * handling is necessary.
5694 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005695 switch (cmd) {
5696 case FS_IOC32_GETFLAGS:
5697 cmd = FS_IOC_GETFLAGS;
5698 break;
5699 case FS_IOC32_SETFLAGS:
5700 cmd = FS_IOC_SETFLAGS;
5701 break;
5702 case FS_IOC32_GETVERSION:
5703 cmd = FS_IOC_GETVERSION;
5704 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005705 }
5706
5707 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5708}
5709#endif