blob: 42ed752288e64e91d433939c5e05b2103e0908e2 [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>
8#include <linux/buffer_head.h>
9#include <linux/file.h>
10#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040011#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040012#include <linux/pagemap.h>
13#include <linux/highmem.h>
14#include <linux/time.h>
15#include <linux/init.h>
16#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040017#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040018#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040019#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040020#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040021#include <linux/swap.h>
22#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040023#include <linux/compat.h>
24#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040025#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040026#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020027#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000029#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020030#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000031#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070032#include <linux/uaccess.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050033#include <linux/iversion.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include "ctree.h"
35#include "disk-io.h"
36#include "transaction.h"
37#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040038#include "print-tree.h"
39#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040040#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080041#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020042#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040043#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020044#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010045#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000046#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040047#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070048#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000049#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080050#include "compression.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040051
Hugo Millsabccd002014-01-30 20:17:00 +000052#ifdef CONFIG_64BIT
53/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
54 * structures are incorrect, as the timespec structure from userspace
55 * is 4 bytes too small. We define these alternatives here to teach
56 * the kernel about the 32-bit struct packing.
57 */
58struct btrfs_ioctl_timespec_32 {
59 __u64 sec;
60 __u32 nsec;
61} __attribute__ ((__packed__));
62
63struct btrfs_ioctl_received_subvol_args_32 {
64 char uuid[BTRFS_UUID_SIZE]; /* in */
65 __u64 stransid; /* in */
66 __u64 rtransid; /* out */
67 struct btrfs_ioctl_timespec_32 stime; /* in */
68 struct btrfs_ioctl_timespec_32 rtime; /* out */
69 __u64 flags; /* in */
70 __u64 reserved[16]; /* in */
71} __attribute__ ((__packed__));
72
73#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
74 struct btrfs_ioctl_received_subvol_args_32)
75#endif
76
Josef Bacik2351f432017-09-27 10:43:13 -040077#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
78struct btrfs_ioctl_send_args_32 {
79 __s64 send_fd; /* in */
80 __u64 clone_sources_count; /* in */
81 compat_uptr_t clone_sources; /* in */
82 __u64 parent_root; /* in */
83 __u64 flags; /* in */
84 __u64 reserved[4]; /* in */
85} __attribute__ ((__packed__));
86
87#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
88 struct btrfs_ioctl_send_args_32)
89#endif
Hugo Millsabccd002014-01-30 20:17:00 +000090
Mark Fasheh416161d2013-08-06 11:42:51 -070091static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070092 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
93 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070094
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020096static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
97 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020098{
David Sterba1905a0f2018-03-26 18:52:15 +020099 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200100 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +0200101 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200102 return flags & ~FS_DIRSYNC_FL;
103 else
104 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
105}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400106
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200107/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200108 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
109 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200110 */
David Sterbaa157d4f2018-03-26 19:12:25 +0200111static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200112{
113 unsigned int iflags = 0;
114
115 if (flags & BTRFS_INODE_SYNC)
116 iflags |= FS_SYNC_FL;
117 if (flags & BTRFS_INODE_IMMUTABLE)
118 iflags |= FS_IMMUTABLE_FL;
119 if (flags & BTRFS_INODE_APPEND)
120 iflags |= FS_APPEND_FL;
121 if (flags & BTRFS_INODE_NODUMP)
122 iflags |= FS_NODUMP_FL;
123 if (flags & BTRFS_INODE_NOATIME)
124 iflags |= FS_NOATIME_FL;
125 if (flags & BTRFS_INODE_DIRSYNC)
126 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000127 if (flags & BTRFS_INODE_NODATACOW)
128 iflags |= FS_NOCOW_FL;
129
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900130 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000131 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900132 else if (flags & BTRFS_INODE_COMPRESS)
133 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200134
135 return iflags;
136}
137
138/*
139 * Update inode->i_flags based on the btrfs internal flags.
140 */
David Sterba7b6a2212018-03-26 18:40:21 +0200141void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200142{
David Sterba5c57b8b2018-04-23 15:45:18 +0200143 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200145
David Sterba5c57b8b2018-04-23 15:45:18 +0200146 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100147 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200148 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100149 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200150 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100151 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200152 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100153 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200154 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100155 new_fl |= S_DIRSYNC;
156
157 set_mask_bits(&inode->i_flags,
158 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
159 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200160}
161
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200162static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
163{
David Sterba5c57b8b2018-04-23 15:45:18 +0200164 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
165 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200166
167 if (copy_to_user(arg, &flags, sizeof(flags)))
168 return -EFAULT;
169 return 0;
170}
171
David Sterba5ba76ab2018-03-26 18:52:15 +0200172/* Check if @flags are a supported and valid set of FS_*_FL flags */
173static int check_fsflags(unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000174{
175 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
176 FS_NOATIME_FL | FS_NODUMP_FL | \
177 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000178 FS_NOCOMP_FL | FS_COMPR_FL |
179 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000180 return -EOPNOTSUPP;
181
182 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
183 return -EINVAL;
184
Liu Bo75e7cb72011-03-22 10:12:20 +0000185 return 0;
186}
187
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200188static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
189{
Al Viro496ad9a2013-01-23 17:07:38 -0500190 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400191 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200192 struct btrfs_inode *binode = BTRFS_I(inode);
193 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200194 struct btrfs_trans_handle *trans;
David Sterba5c57b8b2018-04-23 15:45:18 +0200195 unsigned int fsflags, old_fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200196 int ret;
David Sterba5c57b8b2018-04-23 15:45:18 +0200197 u64 old_flags;
198 unsigned int old_i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600199 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200200
David Sterbabd60ea02014-01-16 15:50:22 +0100201 if (!inode_owner_or_capable(inode))
202 return -EPERM;
203
Li Zefanb83cc962010-12-20 16:04:08 +0800204 if (btrfs_root_readonly(root))
205 return -EROFS;
206
David Sterba5c57b8b2018-04-23 15:45:18 +0200207 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200208 return -EFAULT;
209
David Sterba5c57b8b2018-04-23 15:45:18 +0200210 ret = check_fsflags(fsflags);
Liu Bo75e7cb72011-03-22 10:12:20 +0000211 if (ret)
212 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200213
Jan Karae7848682012-06-12 16:20:32 +0200214 ret = mnt_want_write_file(file);
215 if (ret)
216 return ret;
217
Al Viro59551022016-01-22 15:40:57 -0500218 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200219
David Sterba5c57b8b2018-04-23 15:45:18 +0200220 old_flags = binode->flags;
221 old_i_flags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600222 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800223
David Sterba5c57b8b2018-04-23 15:45:18 +0200224 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
225 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
226 if ((fsflags ^ old_fsflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200227 if (!capable(CAP_LINUX_IMMUTABLE)) {
228 ret = -EPERM;
229 goto out_unlock;
230 }
231 }
232
David Sterba5c57b8b2018-04-23 15:45:18 +0200233 if (fsflags & FS_SYNC_FL)
234 binode->flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200235 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200236 binode->flags &= ~BTRFS_INODE_SYNC;
237 if (fsflags & FS_IMMUTABLE_FL)
238 binode->flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200239 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200240 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
241 if (fsflags & FS_APPEND_FL)
242 binode->flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200243 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200244 binode->flags &= ~BTRFS_INODE_APPEND;
245 if (fsflags & FS_NODUMP_FL)
246 binode->flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200247 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200248 binode->flags &= ~BTRFS_INODE_NODUMP;
249 if (fsflags & FS_NOATIME_FL)
250 binode->flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200251 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200252 binode->flags &= ~BTRFS_INODE_NOATIME;
253 if (fsflags & FS_DIRSYNC_FL)
254 binode->flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200255 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200256 binode->flags &= ~BTRFS_INODE_DIRSYNC;
257 if (fsflags & FS_NOCOW_FL) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600258 if (S_ISREG(mode)) {
259 /*
260 * It's safe to turn csums off here, no extents exist.
261 * Otherwise we want the flag to reflect the real COW
262 * status of the file and will not set it.
263 */
264 if (inode->i_size == 0)
David Sterba5c57b8b2018-04-23 15:45:18 +0200265 binode->flags |= BTRFS_INODE_NODATACOW
266 | BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600267 } else {
David Sterba5c57b8b2018-04-23 15:45:18 +0200268 binode->flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600269 }
270 } else {
271 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400272 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600273 */
274 if (S_ISREG(mode)) {
275 if (inode->i_size == 0)
David Sterba5c57b8b2018-04-23 15:45:18 +0200276 binode->flags &= ~(BTRFS_INODE_NODATACOW
David Sterba7e97b8d2012-09-07 05:56:55 -0600277 | BTRFS_INODE_NODATASUM);
278 } else {
David Sterba5c57b8b2018-04-23 15:45:18 +0200279 binode->flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600280 }
281 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200282
Liu Bo75e7cb72011-03-22 10:12:20 +0000283 /*
284 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
285 * flag may be changed automatically if compression code won't make
286 * things smaller.
287 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200288 if (fsflags & FS_NOCOMP_FL) {
289 binode->flags &= ~BTRFS_INODE_COMPRESS;
290 binode->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000291
292 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
293 if (ret && ret != -ENODATA)
294 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200295 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000296 const char *comp;
297
David Sterba5c57b8b2018-04-23 15:45:18 +0200298 binode->flags |= BTRFS_INODE_COMPRESS;
299 binode->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000300
David Sterba93370502017-10-31 17:32:41 +0100301 comp = btrfs_compress_type2str(fs_info->compress_type);
302 if (!comp || comp[0] == 0)
303 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
304
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000305 ret = btrfs_set_prop(inode, "btrfs.compression",
306 comp, strlen(comp), 0);
307 if (ret)
308 goto out_drop;
309
Li Zefanebcb9042011-04-15 03:03:17 +0000310 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100311 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
312 if (ret && ret != -ENODATA)
313 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200314 binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000315 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200316
Li Zefan4da6f1a2011-12-29 13:39:50 +0800317 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800318 if (IS_ERR(trans)) {
319 ret = PTR_ERR(trans);
320 goto out_drop;
321 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200322
David Sterba7b6a2212018-03-26 18:40:21 +0200323 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400324 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700325 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200326 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200327
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400328 btrfs_end_transaction(trans);
Li Zefanf062abf02011-12-29 13:36:45 +0800329 out_drop:
330 if (ret) {
David Sterba5c57b8b2018-04-23 15:45:18 +0200331 binode->flags = old_flags;
332 inode->i_flags = old_i_flags;
Li Zefanf062abf02011-12-29 13:36:45 +0800333 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200334
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200335 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500336 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200337 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000338 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200339}
340
David Sterba19f93b32018-03-26 19:42:05 +0200341/*
342 * Translate btrfs internal inode flags to xflags as expected by the
343 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
344 * silently dropped.
345 */
346static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
347{
348 unsigned int xflags = 0;
349
350 if (flags & BTRFS_INODE_APPEND)
351 xflags |= FS_XFLAG_APPEND;
352 if (flags & BTRFS_INODE_IMMUTABLE)
353 xflags |= FS_XFLAG_IMMUTABLE;
354 if (flags & BTRFS_INODE_NOATIME)
355 xflags |= FS_XFLAG_NOATIME;
356 if (flags & BTRFS_INODE_NODUMP)
357 xflags |= FS_XFLAG_NODUMP;
358 if (flags & BTRFS_INODE_SYNC)
359 xflags |= FS_XFLAG_SYNC;
360
361 return xflags;
362}
363
364/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
365static int check_xflags(unsigned int flags)
366{
367 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
368 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
369 return -EOPNOTSUPP;
370 return 0;
371}
372
David Sterbae4202ac2018-03-26 19:51:16 +0200373/*
374 * Set the xflags from the internal inode flags. The remaining items of fsxattr
375 * are zeroed.
376 */
377static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
378{
379 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
380 struct fsxattr fa;
381
382 memset(&fa, 0, sizeof(fa));
383 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
384
385 if (copy_to_user(arg, &fa, sizeof(fa)))
386 return -EFAULT;
387
388 return 0;
389}
390
David Sterba025f2122018-03-26 19:51:16 +0200391static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
392{
393 struct inode *inode = file_inode(file);
394 struct btrfs_inode *binode = BTRFS_I(inode);
395 struct btrfs_root *root = binode->root;
396 struct btrfs_trans_handle *trans;
397 struct fsxattr fa;
398 unsigned old_flags;
399 unsigned old_i_flags;
400 int ret = 0;
401
402 if (!inode_owner_or_capable(inode))
403 return -EPERM;
404
405 if (btrfs_root_readonly(root))
406 return -EROFS;
407
408 memset(&fa, 0, sizeof(fa));
409 if (copy_from_user(&fa, arg, sizeof(fa)))
410 return -EFAULT;
411
412 ret = check_xflags(fa.fsx_xflags);
413 if (ret)
414 return ret;
415
416 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
417 return -EOPNOTSUPP;
418
419 ret = mnt_want_write_file(file);
420 if (ret)
421 return ret;
422
423 inode_lock(inode);
424
425 old_flags = binode->flags;
426 old_i_flags = inode->i_flags;
427
428 /* We need the capabilities to change append-only or immutable inode */
429 if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
430 (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
431 !capable(CAP_LINUX_IMMUTABLE)) {
432 ret = -EPERM;
433 goto out_unlock;
434 }
435
436 if (fa.fsx_xflags & FS_XFLAG_SYNC)
437 binode->flags |= BTRFS_INODE_SYNC;
438 else
439 binode->flags &= ~BTRFS_INODE_SYNC;
440 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
441 binode->flags |= BTRFS_INODE_IMMUTABLE;
442 else
443 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
444 if (fa.fsx_xflags & FS_XFLAG_APPEND)
445 binode->flags |= BTRFS_INODE_APPEND;
446 else
447 binode->flags &= ~BTRFS_INODE_APPEND;
448 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
449 binode->flags |= BTRFS_INODE_NODUMP;
450 else
451 binode->flags &= ~BTRFS_INODE_NODUMP;
452 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
453 binode->flags |= BTRFS_INODE_NOATIME;
454 else
455 binode->flags &= ~BTRFS_INODE_NOATIME;
456
457 /* 1 item for the inode */
458 trans = btrfs_start_transaction(root, 1);
459 if (IS_ERR(trans)) {
460 ret = PTR_ERR(trans);
461 goto out_unlock;
462 }
463
464 btrfs_sync_inode_flags_to_i_flags(inode);
465 inode_inc_iversion(inode);
466 inode->i_ctime = current_time(inode);
467 ret = btrfs_update_inode(trans, root, inode);
468
469 btrfs_end_transaction(trans);
470
471out_unlock:
472 if (ret) {
473 binode->flags = old_flags;
474 inode->i_flags = old_i_flags;
475 }
476
477 inode_unlock(inode);
478 mnt_drop_write_file(file);
479
480 return ret;
481}
482
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200483static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
484{
Al Viro496ad9a2013-01-23 17:07:38 -0500485 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200486
487 return put_user(inode->i_generation, arg);
488}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400489
Li Dongyangf7039b12011-03-24 10:24:28 +0000490static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
491{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400492 struct inode *inode = file_inode(file);
493 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000494 struct btrfs_device *device;
495 struct request_queue *q;
496 struct fstrim_range range;
497 u64 minlen = ULLONG_MAX;
498 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500499 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000500 int ret;
501
502 if (!capable(CAP_SYS_ADMIN))
503 return -EPERM;
504
Xiao Guangrong1f781602011-04-20 10:09:16 +0000505 rcu_read_lock();
506 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
507 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000508 if (!device->bdev)
509 continue;
510 q = bdev_get_queue(device->bdev);
511 if (blk_queue_discard(q)) {
512 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100513 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000514 minlen);
515 }
516 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000517 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200518
Li Dongyangf7039b12011-03-24 10:24:28 +0000519 if (!num_devices)
520 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000521 if (copy_from_user(&range, arg, sizeof(range)))
522 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000523 if (range.start > total_bytes ||
524 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200525 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000526
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200527 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000528 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400529 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000530 if (ret < 0)
531 return ret;
532
533 if (copy_to_user(arg, &range, sizeof(range)))
534 return -EFAULT;
535
536 return 0;
537}
538
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200539int btrfs_is_empty_uuid(u8 *uuid)
540{
Chris Mason46e0f662013-11-15 12:14:55 +0100541 int i;
542
543 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
544 if (uuid[i])
545 return 0;
546 }
547 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200548}
549
Miao Xied5c12072013-02-28 10:04:33 +0000550static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400551 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100552 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200553 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000554 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400556 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400557 struct btrfs_trans_handle *trans;
558 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100559 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400560 struct btrfs_inode_item *inode_item;
561 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000562 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400563 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000564 struct btrfs_block_rsv block_rsv;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700565 struct timespec cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900566 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400567 int ret;
568 int err;
569 u64 objectid;
570 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500571 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200572 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400573
David Sterba49a3c4d2016-03-24 17:49:22 +0100574 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
575 if (!root_item)
576 return -ENOMEM;
577
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400578 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400579 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100580 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000581
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800582 /*
583 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400584 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800585 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100586 if (btrfs_qgroup_level(objectid)) {
587 ret = -ENOSPC;
588 goto fail_free;
589 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800590
Miao Xied5c12072013-02-28 10:04:33 +0000591 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400592 /*
Miao Xied5c12072013-02-28 10:04:33 +0000593 * The same as the snapshot creation, please see the comment
594 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400595 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800596 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000597 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100598 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400599
Miao Xied5c12072013-02-28 10:04:33 +0000600 trans = btrfs_start_transaction(root, 0);
601 if (IS_ERR(trans)) {
602 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100603 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100604 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000605 }
606 trans->block_rsv = &block_rsv;
607 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400608
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400609 ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200610 if (ret)
611 goto fail;
612
David Sterba4d75f8a2014-06-15 01:54:12 +0200613 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400614 if (IS_ERR(leaf)) {
615 ret = PTR_ERR(leaf);
616 goto fail;
617 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400618
David Sterbab159fa22016-11-08 18:09:03 +0100619 memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400620 btrfs_set_header_bytenr(leaf, leaf->start);
621 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400622 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400623 btrfs_set_header_owner(leaf, objectid);
624
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400625 write_extent_buffer_fsid(leaf, fs_info->fsid);
626 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400627 btrfs_mark_buffer_dirty(leaf);
628
David Sterba49a3c4d2016-03-24 17:49:22 +0100629 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800630 btrfs_set_stack_inode_generation(inode_item, 1);
631 btrfs_set_stack_inode_size(inode_item, 3);
632 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400633 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400634 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800635 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400636
David Sterba49a3c4d2016-03-24 17:49:22 +0100637 btrfs_set_root_flags(root_item, 0);
638 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800639 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000640
David Sterba49a3c4d2016-03-24 17:49:22 +0100641 btrfs_set_root_bytenr(root_item, leaf->start);
642 btrfs_set_root_generation(root_item, trans->transid);
643 btrfs_set_root_level(root_item, 0);
644 btrfs_set_root_refs(root_item, 1);
645 btrfs_set_root_used(root_item, leaf->len);
646 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400647
David Sterba49a3c4d2016-03-24 17:49:22 +0100648 btrfs_set_root_generation_v2(root_item,
649 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200650 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100651 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
652 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
653 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
654 root_item->ctime = root_item->otime;
655 btrfs_set_root_ctransid(root_item, trans->transid);
656 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400657
Chris Mason925baed2008-06-25 16:01:30 -0400658 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400659 free_extent_buffer(leaf);
660 leaf = NULL;
661
David Sterba49a3c4d2016-03-24 17:49:22 +0100662 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400663
664 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400665 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200666 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400667 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100668 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400669 if (ret)
670 goto fail;
671
Yan, Zheng76dda932009-09-21 16:00:26 -0400672 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400673 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100674 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100675 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400676 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100677 goto fail;
678 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400679
680 btrfs_record_root_in_trans(trans, new_root);
681
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000682 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700683 if (ret) {
684 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400685 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700686 goto fail;
687 }
688
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530689 mutex_lock(&new_root->objectid_mutex);
690 new_root->highest_objectid = new_dirid;
691 mutex_unlock(&new_root->objectid_mutex);
692
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400693 /*
694 * insert the directory item
695 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200696 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100697 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400698 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100699 goto fail;
700 }
Chris Mason3de45862008-11-17 21:02:50 -0500701
702 ret = btrfs_insert_dir_item(trans, root,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200703 name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500704 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100705 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400706 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400707 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100708 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500709
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200710 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500711 ret = btrfs_update_inode(trans, root, dir);
712 BUG_ON(ret);
713
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400714 ret = btrfs_add_root_ref(trans, fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400715 objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200716 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500717 BUG_ON(ret);
718
Lu Fengqicdb345a2018-05-29 15:01:53 +0800719 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400720 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200721 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400722 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200723
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400724fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100725 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000726 trans->block_rsv = NULL;
727 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100728 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800729
Sage Weil72fd0322010-10-29 15:41:32 -0400730 if (async_transid) {
731 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400732 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000733 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400734 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400735 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400736 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400737 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400738 if (err && !ret)
739 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500740
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900741 if (!ret) {
742 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800743 if (IS_ERR(inode))
744 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900745 d_instantiate(dentry, inode);
746 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400747 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100748
749fail_free:
750 kfree(root_item);
751 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400752}
753
Miao Xiee9662f72013-02-28 10:01:15 +0000754static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100755 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000756 u64 *async_transid, bool readonly,
757 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400758{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400759 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000760 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400761 struct btrfs_pending_snapshot *pending_snapshot;
762 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000763 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400764
Miao Xie27cdeb72014-04-02 19:51:05 +0800765 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400766 return -EINVAL;
767
David Sterba23269bf2017-02-13 11:03:44 +0100768 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100769 if (!pending_snapshot)
770 return -ENOMEM;
771
David Sterbab0c0ea62015-11-10 18:54:00 +0100772 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100773 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100774 pending_snapshot->path = btrfs_alloc_path();
775 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100776 ret = -ENOMEM;
777 goto free_pending;
778 }
779
David Sterbaea14b57f2017-06-22 02:19:11 +0200780 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100781 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600782 /* wait for no snapshot writes */
783 wait_event(root->subv_writers->wait,
784 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800785
Nikolay Borisov76f32e22018-04-23 10:54:14 +0300786 ret = btrfs_start_delalloc_inodes(root);
Miao Xie6a038432013-05-15 07:48:24 +0000787 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100788 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000789
Chris Mason6374e57a2017-06-23 09:48:21 -0700790 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000791
Miao Xie66d8f3d2012-09-06 04:02:28 -0600792 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
793 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000794 /*
795 * 1 - parent dir inode
796 * 2 - dir entries
797 * 1 - root item
798 * 2 - root ref/backref
799 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200800 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000801 */
802 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200803 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400804 false);
Miao Xied5c12072013-02-28 10:04:33 +0000805 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100806 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000807
Yan, Zhenga22285a2010-05-16 10:48:46 -0400808 pending_snapshot->dentry = dentry;
809 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800810 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000811 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000812 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400813
Miao Xied5c12072013-02-28 10:04:33 +0000814 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400815 if (IS_ERR(trans)) {
816 ret = PTR_ERR(trans);
817 goto fail;
818 }
819
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400820 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400821 list_add(&pending_snapshot->list,
822 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400823 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400824 if (async_transid) {
825 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400826 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000827 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400828 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400829 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400830 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400831 }
Miao Xieaec80302013-03-04 09:44:29 +0000832 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400833 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400834
835 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400836 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000837 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400838
Chris Masond3797302014-10-15 13:50:56 -0700839 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
840 if (ret)
841 goto fail;
842
David Howells2b0143b2015-03-17 22:25:59 +0000843 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000844 if (IS_ERR(inode)) {
845 ret = PTR_ERR(inode);
846 goto fail;
847 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900848
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000849 d_instantiate(dentry, inode);
850 ret = 0;
851fail:
David Sterba7775c812017-02-10 19:18:18 +0100852 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100853dec_and_free:
David Sterbaea14b57f2017-06-22 02:19:11 +0200854 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100855 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100856free_pending:
857 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100858 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100859 kfree(pending_snapshot);
860
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400861 return ret;
862}
863
Sage Weil4260f7c2010-10-29 15:46:43 -0400864/* copy of may_delete in fs/namei.c()
865 * Check whether we can remove a link victim from directory dir, check
866 * whether the type of victim is right.
867 * 1. We can't do it if dir is read-only (done in permission())
868 * 2. We should have write and exec permissions on dir
869 * 3. We can't remove anything from append-only dir
870 * 4. We can't do anything with immutable dir (done in permission())
871 * 5. If the sticky bit on dir is set we should either
872 * a. be owner of dir, or
873 * b. be owner of victim, or
874 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400875 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400876 * links pointing to it.
877 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
878 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
879 * 9. We can't remove a root or mountpoint.
880 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
881 * nfs_async_unlink().
882 */
883
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530884static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400885{
886 int error;
887
David Howells2b0143b2015-03-17 22:25:59 +0000888 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400889 return -ENOENT;
890
David Howells2b0143b2015-03-17 22:25:59 +0000891 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400892 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400893
894 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
895 if (error)
896 return error;
897 if (IS_APPEND(dir))
898 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000899 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
900 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400901 return -EPERM;
902 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000903 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400904 return -ENOTDIR;
905 if (IS_ROOT(victim))
906 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000907 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400908 return -EISDIR;
909 if (IS_DEADDIR(dir))
910 return -ENOENT;
911 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
912 return -EBUSY;
913 return 0;
914}
915
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400916/* copy of may_create in fs/namei.c() */
917static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
918{
David Howells2b0143b2015-03-17 22:25:59 +0000919 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400920 return -EEXIST;
921 if (IS_DEADDIR(dir))
922 return -ENOENT;
923 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
924}
925
926/*
927 * Create a new subvolume below @parent. This is largely modeled after
928 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
929 * inside this filesystem so it's quite a bit simpler.
930 */
Al Viro92872092016-11-20 19:34:31 -0500931static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100932 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400933 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200934 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000935 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400936{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400937 struct inode *dir = d_inode(parent->dentry);
938 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400939 struct dentry *dentry;
940 int error;
941
Al Viro00235412016-05-26 00:05:12 -0400942 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
943 if (error == -EINTR)
944 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400945
946 dentry = lookup_one_len(name, parent->dentry, namelen);
947 error = PTR_ERR(dentry);
948 if (IS_ERR(dentry))
949 goto out_unlock;
950
Yan, Zheng76dda932009-09-21 16:00:26 -0400951 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400952 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600953 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400954
Chris Mason9c520572012-12-17 14:26:57 -0500955 /*
956 * even if this name doesn't exist, we may get hash collisions.
957 * check for them now when we can safely fail
958 */
959 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
960 dir->i_ino, name,
961 namelen);
962 if (error)
963 goto out_dput;
964
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400965 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400966
967 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
968 goto out_up_read;
969
Chris Mason3de45862008-11-17 21:02:50 -0500970 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100971 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200972 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500973 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000974 error = create_subvol(dir, dentry, name, namelen,
975 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500976 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400977 if (!error)
978 fsnotify_mkdir(dir, dentry);
979out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400980 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400981out_dput:
982 dput(dentry);
983out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500984 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400985 return error;
986}
987
Chris Mason4cb53002011-05-24 15:35:30 -0400988/*
989 * When we're defragging a range, we don't want to kick it off again
990 * if it is really just waiting for delalloc to send it down.
991 * If we find a nice big extent or delalloc range for the bytes in the
992 * file you want to defrag, we return 0 to let you know to skip this
993 * part of the file
994 */
David Sterbaaab110a2014-07-29 17:32:10 +0200995static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400996{
997 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
998 struct extent_map *em = NULL;
999 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1000 u64 end;
1001
1002 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001003 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001004 read_unlock(&em_tree->lock);
1005
1006 if (em) {
1007 end = extent_map_end(em);
1008 free_extent_map(em);
1009 if (end - offset > thresh)
1010 return 0;
1011 }
1012 /* if we already have a nice delalloc here, just stop */
1013 thresh /= 2;
1014 end = count_range_bits(io_tree, &offset, offset + thresh,
1015 thresh, EXTENT_DELALLOC, 1);
1016 if (end >= thresh)
1017 return 0;
1018 return 1;
1019}
1020
1021/*
1022 * helper function to walk through a file and find extents
1023 * newer than a specific transid, and smaller than thresh.
1024 *
1025 * This is used by the defragging code to find new and small
1026 * extents
1027 */
1028static int find_new_extents(struct btrfs_root *root,
1029 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001030 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001031{
1032 struct btrfs_path *path;
1033 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001034 struct extent_buffer *leaf;
1035 struct btrfs_file_extent_item *extent;
1036 int type;
1037 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001038 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001039
1040 path = btrfs_alloc_path();
1041 if (!path)
1042 return -ENOMEM;
1043
David Sterbaa4689d22011-05-31 17:08:14 +00001044 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001045 min_key.type = BTRFS_EXTENT_DATA_KEY;
1046 min_key.offset = *off;
1047
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301048 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001049 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001050 if (ret != 0)
1051 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001052process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001053 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001054 goto none;
1055 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1056 goto none;
1057
1058 leaf = path->nodes[0];
1059 extent = btrfs_item_ptr(leaf, path->slots[0],
1060 struct btrfs_file_extent_item);
1061
1062 type = btrfs_file_extent_type(leaf, extent);
1063 if (type == BTRFS_FILE_EXTENT_REG &&
1064 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1065 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1066 *off = min_key.offset;
1067 btrfs_free_path(path);
1068 return 0;
1069 }
1070
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001071 path->slots[0]++;
1072 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1073 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1074 goto process_slot;
1075 }
1076
Chris Mason4cb53002011-05-24 15:35:30 -04001077 if (min_key.offset == (u64)-1)
1078 goto none;
1079
1080 min_key.offset++;
1081 btrfs_release_path(path);
1082 }
1083none:
1084 btrfs_free_path(path);
1085 return -ENOENT;
1086}
1087
Li Zefan6c282eb2012-06-11 16:03:35 +08001088static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001089{
1090 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001091 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001092 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001093 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001094
1095 /*
1096 * hopefully we have this extent in the tree already, try without
1097 * the full extent lock
1098 */
1099 read_lock(&em_tree->lock);
1100 em = lookup_extent_mapping(em_tree, start, len);
1101 read_unlock(&em_tree->lock);
1102
1103 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001104 struct extent_state *cached = NULL;
1105 u64 end = start + len - 1;
1106
Chris Mason940100a2010-03-10 10:52:59 -05001107 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001108 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02001109 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001110 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001111
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001112 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001113 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001114 }
1115
Li Zefan6c282eb2012-06-11 16:03:35 +08001116 return em;
1117}
1118
1119static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1120{
1121 struct extent_map *next;
1122 bool ret = true;
1123
1124 /* this is the last extent */
1125 if (em->start + em->len >= i_size_read(inode))
1126 return false;
1127
1128 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001129 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1130 ret = false;
1131 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001132 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001133 ret = false;
1134
1135 free_extent_map(next);
1136 return ret;
1137}
1138
David Sterbaaab110a2014-07-29 17:32:10 +02001139static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001140 u64 *last_len, u64 *skip, u64 *defrag_end,
1141 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001142{
1143 struct extent_map *em;
1144 int ret = 1;
1145 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001146 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001147
1148 /*
1149 * make sure that once we start defragging an extent, we keep on
1150 * defragging it
1151 */
1152 if (start < *defrag_end)
1153 return 1;
1154
1155 *skip = 0;
1156
1157 em = defrag_lookup_extent(inode, start);
1158 if (!em)
1159 return 0;
1160
Chris Mason940100a2010-03-10 10:52:59 -05001161 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001162 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001163 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001164 goto out;
1165 }
1166
Liu Bo4a3560c2015-08-07 16:48:41 +08001167 if (!*defrag_end)
1168 prev_mergeable = false;
1169
Li Zefan6c282eb2012-06-11 16:03:35 +08001170 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001171 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001172 * we hit a real extent, if it is big or the next extent is not a
1173 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001174 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001175 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001176 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001177 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001178out:
Chris Mason940100a2010-03-10 10:52:59 -05001179 /*
1180 * last_len ends up being a counter of how many bytes we've defragged.
1181 * every time we choose not to defrag an extent, we reset *last_len
1182 * so that the next tiny extent will force a defrag.
1183 *
1184 * The end result of this is that tiny extents before a single big
1185 * extent will force at least part of that big extent to be defragged.
1186 */
1187 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001188 *defrag_end = extent_map_end(em);
1189 } else {
1190 *last_len = 0;
1191 *skip = extent_map_end(em);
1192 *defrag_end = 0;
1193 }
1194
1195 free_extent_map(em);
1196 return ret;
1197}
1198
Chris Mason4cb53002011-05-24 15:35:30 -04001199/*
1200 * it doesn't do much good to defrag one or two pages
1201 * at a time. This pulls in a nice chunk of pages
1202 * to COW and defrag.
1203 *
1204 * It also makes sure the delalloc code has enough
1205 * dirty data to avoid making new small extents as part
1206 * of the defrag
1207 *
1208 * It's a good idea to start RA on this range
1209 * before calling this.
1210 */
1211static int cluster_pages_for_defrag(struct inode *inode,
1212 struct page **pages,
1213 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001214 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001215{
Chris Mason4cb53002011-05-24 15:35:30 -04001216 unsigned long file_end;
1217 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001218 u64 page_start;
1219 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001220 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001221 int ret;
1222 int i;
1223 int i_done;
1224 struct btrfs_ordered_extent *ordered;
1225 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001226 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001227 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001228 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001229
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001230 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001231 if (!isize || start_index > file_end)
1232 return 0;
1233
1234 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001235
Qu Wenruo364ecf32017-02-27 15:10:38 +08001236 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001237 start_index << PAGE_SHIFT,
1238 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001239 if (ret)
1240 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001241 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001242 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001243
1244 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001245 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001246 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001247again:
Josef Bacika94733d2011-07-11 10:47:06 -04001248 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001249 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001250 if (!page)
1251 break;
1252
Miao Xie600a45e2012-02-16 15:01:24 +08001253 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001254 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001255 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001256 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001257 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001258 ordered = btrfs_lookup_ordered_extent(inode,
1259 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001260 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001261 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001262 if (!ordered)
1263 break;
1264
1265 unlock_page(page);
1266 btrfs_start_ordered_extent(inode, ordered, 1);
1267 btrfs_put_ordered_extent(ordered);
1268 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001269 /*
1270 * we unlocked the page above, so we need check if
1271 * it was released or not.
1272 */
1273 if (page->mapping != inode->i_mapping) {
1274 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001275 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001276 goto again;
1277 }
Miao Xie600a45e2012-02-16 15:01:24 +08001278 }
1279
Chris Mason4cb53002011-05-24 15:35:30 -04001280 if (!PageUptodate(page)) {
1281 btrfs_readpage(NULL, page);
1282 lock_page(page);
1283 if (!PageUptodate(page)) {
1284 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001285 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001286 ret = -EIO;
1287 break;
1288 }
1289 }
Miao Xie600a45e2012-02-16 15:01:24 +08001290
Miao Xie600a45e2012-02-16 15:01:24 +08001291 if (page->mapping != inode->i_mapping) {
1292 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001293 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001294 goto again;
1295 }
1296
Chris Mason4cb53002011-05-24 15:35:30 -04001297 pages[i] = page;
1298 i_done++;
1299 }
1300 if (!i_done || ret)
1301 goto out;
1302
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001303 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001304 goto out;
1305
1306 /*
1307 * so now we have a nice long stream of locked
1308 * and up to date pages, lets wait on them
1309 */
1310 for (i = 0; i < i_done; i++)
1311 wait_on_page_writeback(pages[i]);
1312
1313 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001314 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001315
1316 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001317 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001318 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1319 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001320 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001321 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001322
Liu Bo1f12bd02012-03-29 09:57:44 -04001323 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001324 spin_lock(&BTRFS_I(inode)->lock);
1325 BTRFS_I(inode)->outstanding_extents++;
1326 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001327 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001328 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001329 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001330 }
1331
1332
Liu Bo9e8a4a82012-09-05 19:10:51 -06001333 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001334 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001335
1336 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001337 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001338
1339 for (i = 0; i < i_done; i++) {
1340 clear_page_dirty_for_io(pages[i]);
1341 ClearPageChecked(pages[i]);
1342 set_page_extent_mapped(pages[i]);
1343 set_page_dirty(pages[i]);
1344 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001345 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001346 }
Qu Wenruo43b18592017-12-12 15:34:32 +08001347 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1348 false);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001349 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001350 return i_done;
1351out:
1352 for (i = 0; i < i_done; i++) {
1353 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001354 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001355 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001356 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001357 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001358 page_cnt << PAGE_SHIFT, true);
1359 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1360 true);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001361 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001362 return ret;
1363
1364}
1365
1366int btrfs_defrag_file(struct inode *inode, struct file *file,
1367 struct btrfs_ioctl_defrag_range_args *range,
1368 u64 newer_than, unsigned long max_to_defrag)
1369{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001370 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001371 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001372 struct file_ra_state *ra = NULL;
1373 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001374 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001375 u64 last_len = 0;
1376 u64 skip = 0;
1377 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001378 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001379 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001380 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001381 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001382 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001383 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001384 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001385 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001386 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001387 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001388 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001389 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001390
Liu Bo0abd5b12013-04-16 09:20:28 +00001391 if (isize == 0)
1392 return 0;
1393
1394 if (range->start >= isize)
1395 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001396
David Sterba1e2ef462017-07-17 20:01:59 +02001397 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001398 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1399 return -EINVAL;
1400 if (range->compress_type)
1401 compress_type = range->compress_type;
1402 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001403
Liu Bo0abd5b12013-04-16 09:20:28 +00001404 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001405 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001406
Chris Mason4cb53002011-05-24 15:35:30 -04001407 /*
David Sterba0a52d102017-06-22 03:22:58 +02001408 * If we were not given a file, allocate a readahead context. As
1409 * readahead is just an optimization, defrag will work without it so
1410 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001411 */
1412 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001413 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001414 if (ra)
1415 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001416 } else {
1417 ra = &file->f_ra;
1418 }
1419
David Sterba63e727e2017-06-22 03:13:02 +02001420 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001421 if (!pages) {
1422 ret = -ENOMEM;
1423 goto out_ra;
1424 }
1425
1426 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001427 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001428 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001429 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001430 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001431 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001432 }
1433
Chris Mason4cb53002011-05-24 15:35:30 -04001434 if (newer_than) {
1435 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001436 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001437 if (!ret) {
1438 range->start = newer_off;
1439 /*
1440 * we always align our defrag to help keep
1441 * the extents in the file evenly spaced
1442 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001443 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001444 } else
1445 goto out_ra;
1446 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001447 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001448 }
1449 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301450 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001451
Li Zefan2a0f7f52011-10-10 15:43:34 -04001452 /*
1453 * make writeback starts from i, so the defrag range can be
1454 * written sequentially.
1455 */
1456 if (i < inode->i_mapping->writeback_index)
1457 inode->i_mapping->writeback_index = i;
1458
Chris Masonf7f43cc2011-10-11 11:41:40 -04001459 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001460 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001461 /*
1462 * make sure we stop running if someone unmounts
1463 * the FS
1464 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001465 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001466 break;
1467
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001468 if (btrfs_defrag_cancelled(fs_info)) {
1469 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001470 ret = -EAGAIN;
1471 break;
1472 }
1473
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001474 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001475 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001476 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001477 unsigned long next;
1478 /*
1479 * the should_defrag function tells us how much to skip
1480 * bump our counter by the suggested amount
1481 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001482 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001483 i = max(i + 1, next);
1484 continue;
1485 }
Li Zefan008873e2011-09-02 15:57:07 +08001486
1487 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001488 cluster = (PAGE_ALIGN(defrag_end) >>
1489 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001490 cluster = min(cluster, max_cluster);
1491 } else {
1492 cluster = max_cluster;
1493 }
1494
Li Zefan008873e2011-09-02 15:57:07 +08001495 if (i + cluster > ra_index) {
1496 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001497 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001498 page_cache_sync_readahead(inode->i_mapping, ra,
1499 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301500 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001501 }
Chris Mason940100a2010-03-10 10:52:59 -05001502
Al Viro59551022016-01-22 15:40:57 -05001503 inode_lock(inode);
David Sterba1e2ef462017-07-17 20:01:59 +02001504 if (do_compress)
David Sterbaeec63c62017-07-17 19:41:31 +02001505 BTRFS_I(inode)->defrag_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001506 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001507 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001508 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001509 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001510 }
Chris Mason940100a2010-03-10 10:52:59 -05001511
Chris Mason4cb53002011-05-24 15:35:30 -04001512 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001513 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001514 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001515
1516 if (newer_than) {
1517 if (newer_off == (u64)-1)
1518 break;
1519
Liu Boe1f041e2012-03-29 09:57:45 -04001520 if (ret > 0)
1521 i += ret;
1522
Chris Mason4cb53002011-05-24 15:35:30 -04001523 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001524 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001525
Byongho Leeee221842015-12-15 01:42:10 +09001526 ret = find_new_extents(root, inode, newer_than,
1527 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001528 if (!ret) {
1529 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001530 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001531 } else {
1532 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001533 }
Chris Mason4cb53002011-05-24 15:35:30 -04001534 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001535 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001536 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001537 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001538 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001539 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001540 last_len = 0;
1541 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001542 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001543 }
1544
Filipe Mananadec8ef92014-03-01 10:55:54 +00001545 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001546 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001547 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1548 &BTRFS_I(inode)->runtime_flags))
1549 filemap_flush(inode->i_mapping);
1550 }
Chris Mason1e701a32010-03-11 09:42:04 -05001551
Li Zefan1a419d82010-10-25 15:12:50 +08001552 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001553 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001554 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1555 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001556 }
1557
Diego Calleja60ccf822011-09-01 16:33:57 +02001558 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001559
Chris Mason4cb53002011-05-24 15:35:30 -04001560out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001561 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001562 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001563 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001564 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001565 }
Chris Mason4cb53002011-05-24 15:35:30 -04001566 if (!file)
1567 kfree(ra);
1568 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001569 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001570}
1571
Miao Xie198605a2012-11-26 08:43:45 +00001572static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001573 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001574{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001575 struct inode *inode = file_inode(file);
1576 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001577 u64 new_size;
1578 u64 old_size;
1579 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001580 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001581 struct btrfs_ioctl_vol_args *vol_args;
1582 struct btrfs_trans_handle *trans;
1583 struct btrfs_device *device = NULL;
1584 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001585 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001586 char *devstr = NULL;
1587 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001588 int mod = 0;
1589
Chris Masone441d542009-01-05 16:57:23 -05001590 if (!capable(CAP_SYS_ADMIN))
1591 return -EPERM;
1592
Miao Xie198605a2012-11-26 08:43:45 +00001593 ret = mnt_want_write_file(file);
1594 if (ret)
1595 return ret;
1596
David Sterba171938e2017-03-28 14:44:21 +02001597 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001598 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001599 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001600 }
1601
Li Zefandae7b662009-04-08 15:06:54 +08001602 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001603 if (IS_ERR(vol_args)) {
1604 ret = PTR_ERR(vol_args);
1605 goto out;
1606 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001607
1608 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001609
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001610 sizestr = vol_args->name;
1611 devstr = strchr(sizestr, ':');
1612 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001613 sizestr = devstr + 1;
1614 *devstr = '\0';
1615 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001616 ret = kstrtoull(devstr, 10, &devid);
1617 if (ret)
1618 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001619 if (!devid) {
1620 ret = -EINVAL;
1621 goto out_free;
1622 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001623 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001624 }
Miao Xiedba60f32012-12-21 09:19:51 +00001625
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001626 device = btrfs_find_device(fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001627 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001628 btrfs_info(fs_info, "resizer unable to find device %llu",
1629 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001630 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001631 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001632 }
Miao Xiedba60f32012-12-21 09:19:51 +00001633
Anand Jainebbede42017-12-04 12:54:52 +08001634 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001635 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001636 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001637 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001638 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001639 goto out_free;
1640 }
1641
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001642 if (!strcmp(sizestr, "max"))
1643 new_size = device->bdev->bd_inode->i_size;
1644 else {
1645 if (sizestr[0] == '-') {
1646 mod = -1;
1647 sizestr++;
1648 } else if (sizestr[0] == '+') {
1649 mod = 1;
1650 sizestr++;
1651 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001652 new_size = memparse(sizestr, &retptr);
1653 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001654 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001655 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001656 }
1657 }
1658
Anand Jain401e29c2017-12-04 12:54:55 +08001659 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001660 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001661 goto out_free;
1662 }
1663
Miao Xie7cc8e582014-09-03 21:35:38 +08001664 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001665
1666 if (mod < 0) {
1667 if (new_size > old_size) {
1668 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001669 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001670 }
1671 new_size = old_size - new_size;
1672 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001673 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001674 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001675 goto out_free;
1676 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001677 new_size = old_size + new_size;
1678 }
1679
Byongho Leeee221842015-12-15 01:42:10 +09001680 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001681 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001682 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001683 }
1684 if (new_size > device->bdev->bd_inode->i_size) {
1685 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001686 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001687 }
1688
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001689 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001690
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001691 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1692 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001693
1694 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001695 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001696 if (IS_ERR(trans)) {
1697 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001698 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001699 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001700 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001701 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001702 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001703 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001704 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001705
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001706out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001707 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001708out:
David Sterba171938e2017-03-28 14:44:21 +02001709 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001710 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001711 return ret;
1712}
1713
Sage Weil72fd0322010-10-29 15:41:32 -04001714static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001715 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001716 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001717 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001718{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001719 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001720 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001721
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001722 if (!S_ISDIR(file_inode(file)->i_mode))
1723 return -ENOTDIR;
1724
Liu Boa874a632012-06-29 03:58:46 -06001725 ret = mnt_want_write_file(file);
1726 if (ret)
1727 goto out;
1728
Sage Weil72fd0322010-10-29 15:41:32 -04001729 namelen = strlen(name);
1730 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001731 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001732 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001733 }
1734
Chris Mason16780ca2012-02-20 22:14:55 -05001735 if (name[0] == '.' &&
1736 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1737 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001738 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001739 }
1740
Chris Mason3de45862008-11-17 21:02:50 -05001741 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001742 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001743 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001744 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001745 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001746 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001747 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001748 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001749 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001750 }
1751
Al Viro496ad9a2013-01-23 17:07:38 -05001752 src_inode = file_inode(src.file);
1753 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001754 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001755 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001756 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001757 } else if (!inode_owner_or_capable(src_inode)) {
1758 /*
1759 * Subvolume creation is not restricted, but snapshots
1760 * are limited to own subvolumes only
1761 */
1762 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001763 } else {
1764 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1765 BTRFS_I(src_inode)->root,
1766 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001767 }
Al Viro2903ff02012-08-28 12:52:22 -04001768 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001769 }
Liu Boa874a632012-06-29 03:58:46 -06001770out_drop_write:
1771 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001772out:
Sage Weil72fd0322010-10-29 15:41:32 -04001773 return ret;
1774}
1775
1776static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001777 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001778{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001779 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001780 int ret;
1781
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001782 if (!S_ISDIR(file_inode(file)->i_mode))
1783 return -ENOTDIR;
1784
Li Zefanfa0d2b92010-12-20 15:53:28 +08001785 vol_args = memdup_user(arg, sizeof(*vol_args));
1786 if (IS_ERR(vol_args))
1787 return PTR_ERR(vol_args);
1788 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001789
Li Zefanfa0d2b92010-12-20 15:53:28 +08001790 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001791 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001792 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001793
Li Zefanfa0d2b92010-12-20 15:53:28 +08001794 kfree(vol_args);
1795 return ret;
1796}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001797
Li Zefanfa0d2b92010-12-20 15:53:28 +08001798static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1799 void __user *arg, int subvol)
1800{
1801 struct btrfs_ioctl_vol_args_v2 *vol_args;
1802 int ret;
1803 u64 transid = 0;
1804 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001805 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001806 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001807
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001808 if (!S_ISDIR(file_inode(file)->i_mode))
1809 return -ENOTDIR;
1810
Li Zefanfa0d2b92010-12-20 15:53:28 +08001811 vol_args = memdup_user(arg, sizeof(*vol_args));
1812 if (IS_ERR(vol_args))
1813 return PTR_ERR(vol_args);
1814 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001815
Li Zefanb83cc962010-12-20 16:04:08 +08001816 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001817 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1818 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001819 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001820 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001821 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001822
1823 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1824 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001825 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1826 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001827 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001828 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001829 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001830 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001831 }
1832 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1833 if (IS_ERR(inherit)) {
1834 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001835 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001836 }
1837 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001838
1839 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001840 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001841 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001842 if (ret)
1843 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001844
Dan Carpenterc47ca322014-09-04 14:09:15 +03001845 if (ptr && copy_to_user(arg +
1846 offsetof(struct btrfs_ioctl_vol_args_v2,
1847 transid),
1848 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001849 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001850
1851free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001852 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001853free_args:
1854 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001855 return ret;
1856}
1857
Li Zefan0caa1022010-12-20 16:30:25 +08001858static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1859 void __user *arg)
1860{
Al Viro496ad9a2013-01-23 17:07:38 -05001861 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001862 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001863 struct btrfs_root *root = BTRFS_I(inode)->root;
1864 int ret = 0;
1865 u64 flags = 0;
1866
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001867 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001868 return -EINVAL;
1869
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001870 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001871 if (btrfs_root_readonly(root))
1872 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001874
1875 if (copy_to_user(arg, &flags, sizeof(flags)))
1876 ret = -EFAULT;
1877
1878 return ret;
1879}
1880
1881static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1882 void __user *arg)
1883{
Al Viro496ad9a2013-01-23 17:07:38 -05001884 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001885 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001886 struct btrfs_root *root = BTRFS_I(inode)->root;
1887 struct btrfs_trans_handle *trans;
1888 u64 root_flags;
1889 u64 flags;
1890 int ret = 0;
1891
David Sterbabd60ea02014-01-16 15:50:22 +01001892 if (!inode_owner_or_capable(inode))
1893 return -EPERM;
1894
Liu Bob9ca0662012-06-29 03:58:49 -06001895 ret = mnt_want_write_file(file);
1896 if (ret)
1897 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001898
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001899 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001900 ret = -EINVAL;
1901 goto out_drop_write;
1902 }
Li Zefan0caa1022010-12-20 16:30:25 +08001903
Liu Bob9ca0662012-06-29 03:58:49 -06001904 if (copy_from_user(&flags, arg, sizeof(flags))) {
1905 ret = -EFAULT;
1906 goto out_drop_write;
1907 }
Li Zefan0caa1022010-12-20 16:30:25 +08001908
Liu Bob9ca0662012-06-29 03:58:49 -06001909 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1910 ret = -EINVAL;
1911 goto out_drop_write;
1912 }
Li Zefan0caa1022010-12-20 16:30:25 +08001913
Liu Bob9ca0662012-06-29 03:58:49 -06001914 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1915 ret = -EOPNOTSUPP;
1916 goto out_drop_write;
1917 }
Li Zefan0caa1022010-12-20 16:30:25 +08001918
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001919 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001920
1921 /* nothing to do */
1922 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001923 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001924
1925 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001926 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001927 btrfs_set_root_flags(&root->root_item,
1928 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001929 } else {
1930 /*
1931 * Block RO -> RW transition if this subvolume is involved in
1932 * send
1933 */
1934 spin_lock(&root->root_item_lock);
1935 if (root->send_in_progress == 0) {
1936 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001937 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001938 spin_unlock(&root->root_item_lock);
1939 } else {
1940 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001941 btrfs_warn(fs_info,
1942 "Attempt to set subvolume %llu read-write during send",
1943 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001944 ret = -EPERM;
1945 goto out_drop_sem;
1946 }
1947 }
Li Zefan0caa1022010-12-20 16:30:25 +08001948
1949 trans = btrfs_start_transaction(root, 1);
1950 if (IS_ERR(trans)) {
1951 ret = PTR_ERR(trans);
1952 goto out_reset;
1953 }
1954
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001955 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001956 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001957 if (ret < 0) {
1958 btrfs_end_transaction(trans);
1959 goto out_reset;
1960 }
Li Zefan0caa1022010-12-20 16:30:25 +08001961
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001962 ret = btrfs_commit_transaction(trans);
1963
Li Zefan0caa1022010-12-20 16:30:25 +08001964out_reset:
1965 if (ret)
1966 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001967out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001968 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001969out_drop_write:
1970 mnt_drop_write_file(file);
1971out:
Li Zefan0caa1022010-12-20 16:30:25 +08001972 return ret;
1973}
1974
Chris Masonac8e9812010-02-28 15:39:26 -05001975static noinline int key_in_sk(struct btrfs_key *key,
1976 struct btrfs_ioctl_search_key *sk)
1977{
Chris Masonabc6e132010-03-18 12:10:08 -04001978 struct btrfs_key test;
1979 int ret;
1980
1981 test.objectid = sk->min_objectid;
1982 test.type = sk->min_type;
1983 test.offset = sk->min_offset;
1984
1985 ret = btrfs_comp_cpu_keys(key, &test);
1986 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001987 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001988
1989 test.objectid = sk->max_objectid;
1990 test.type = sk->max_type;
1991 test.offset = sk->max_offset;
1992
1993 ret = btrfs_comp_cpu_keys(key, &test);
1994 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001995 return 0;
1996 return 1;
1997}
1998
Jeff Mahoneydf397562016-06-21 20:18:21 -04001999static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002000 struct btrfs_key *key,
2001 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002002 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002003 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002004 unsigned long *sk_offset,
2005 int *num_found)
2006{
2007 u64 found_transid;
2008 struct extent_buffer *leaf;
2009 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002010 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002011 unsigned long item_off;
2012 unsigned long item_len;
2013 int nritems;
2014 int i;
2015 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002016 int ret = 0;
2017
2018 leaf = path->nodes[0];
2019 slot = path->slots[0];
2020 nritems = btrfs_header_nritems(leaf);
2021
2022 if (btrfs_header_generation(leaf) > sk->max_transid) {
2023 i = nritems;
2024 goto advance_key;
2025 }
2026 found_transid = btrfs_header_generation(leaf);
2027
2028 for (i = slot; i < nritems; i++) {
2029 item_off = btrfs_item_ptr_offset(leaf, i);
2030 item_len = btrfs_item_size_nr(leaf, i);
2031
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002032 btrfs_item_key_to_cpu(leaf, key, i);
2033 if (!key_in_sk(key, sk))
2034 continue;
2035
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002036 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002037 if (*num_found) {
2038 ret = 1;
2039 goto out;
2040 }
Chris Masonac8e9812010-02-28 15:39:26 -05002041
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002042 /*
2043 * return one empty item back for v1, which does not
2044 * handle -EOVERFLOW
2045 */
2046
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002047 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002048 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002049 ret = -EOVERFLOW;
2050 }
Chris Masonac8e9812010-02-28 15:39:26 -05002051
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002052 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002053 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002054 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002055 }
2056
Chris Masonac8e9812010-02-28 15:39:26 -05002057 sh.objectid = key->objectid;
2058 sh.offset = key->offset;
2059 sh.type = key->type;
2060 sh.len = item_len;
2061 sh.transid = found_transid;
2062
2063 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002064 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2065 ret = -EFAULT;
2066 goto out;
2067 }
2068
Chris Masonac8e9812010-02-28 15:39:26 -05002069 *sk_offset += sizeof(sh);
2070
2071 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002072 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002073 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002074 if (read_extent_buffer_to_user(leaf, up,
2075 item_off, item_len)) {
2076 ret = -EFAULT;
2077 goto out;
2078 }
2079
Chris Masonac8e9812010-02-28 15:39:26 -05002080 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002081 }
Hugo Millse2156862011-05-14 17:43:41 +00002082 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002083
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002084 if (ret) /* -EOVERFLOW from above */
2085 goto out;
2086
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002087 if (*num_found >= sk->nr_items) {
2088 ret = 1;
2089 goto out;
2090 }
Chris Masonac8e9812010-02-28 15:39:26 -05002091 }
2092advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002093 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002094 test.objectid = sk->max_objectid;
2095 test.type = sk->max_type;
2096 test.offset = sk->max_offset;
2097 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2098 ret = 1;
2099 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002100 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002101 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002102 key->offset = 0;
2103 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002104 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002105 key->offset = 0;
2106 key->type = 0;
2107 key->objectid++;
2108 } else
2109 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002110out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002111 /*
2112 * 0: all items from this leaf copied, continue with next
2113 * 1: * more items can be copied, but unused buffer is too small
2114 * * all items were found
2115 * Either way, it will stops the loop which iterates to the next
2116 * leaf
2117 * -EOVERFLOW: item was to large for buffer
2118 * -EFAULT: could not copy extent buffer back to userspace
2119 */
Chris Masonac8e9812010-02-28 15:39:26 -05002120 return ret;
2121}
2122
2123static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002124 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002125 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002126 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002127{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002128 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002129 struct btrfs_root *root;
2130 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002131 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002132 int ret;
2133 int num_found = 0;
2134 unsigned long sk_offset = 0;
2135
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002136 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2137 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002138 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002139 }
Gerhard Heift12544442014-01-30 16:23:58 +01002140
Chris Masonac8e9812010-02-28 15:39:26 -05002141 path = btrfs_alloc_path();
2142 if (!path)
2143 return -ENOMEM;
2144
2145 if (sk->tree_id == 0) {
2146 /* search the root of the inode that was passed */
2147 root = BTRFS_I(inode)->root;
2148 } else {
2149 key.objectid = sk->tree_id;
2150 key.type = BTRFS_ROOT_ITEM_KEY;
2151 key.offset = (u64)-1;
2152 root = btrfs_read_fs_root_no_name(info, &key);
2153 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002154 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002155 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002156 }
2157 }
2158
2159 key.objectid = sk->min_objectid;
2160 key.type = sk->min_type;
2161 key.offset = sk->min_offset;
2162
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302163 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002164 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002165 if (ret != 0) {
2166 if (ret > 0)
2167 ret = 0;
2168 goto err;
2169 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002170 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002171 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002172 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002173 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002174 break;
2175
2176 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002177 if (ret > 0)
2178 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002179err:
2180 sk->nr_items = num_found;
2181 btrfs_free_path(path);
2182 return ret;
2183}
2184
2185static noinline int btrfs_ioctl_tree_search(struct file *file,
2186 void __user *argp)
2187{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002188 struct btrfs_ioctl_search_args __user *uargs;
2189 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002190 struct inode *inode;
2191 int ret;
2192 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002193
2194 if (!capable(CAP_SYS_ADMIN))
2195 return -EPERM;
2196
Gerhard Heiftba346b32014-01-30 16:24:02 +01002197 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002198
Gerhard Heiftba346b32014-01-30 16:24:02 +01002199 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2200 return -EFAULT;
2201
2202 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002203
Al Viro496ad9a2013-01-23 17:07:38 -05002204 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002205 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002206
2207 /*
2208 * In the origin implementation an overflow is handled by returning a
2209 * search header with a len of zero, so reset ret.
2210 */
2211 if (ret == -EOVERFLOW)
2212 ret = 0;
2213
Gerhard Heiftba346b32014-01-30 16:24:02 +01002214 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002215 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002216 return ret;
2217}
2218
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002219static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2220 void __user *argp)
2221{
2222 struct btrfs_ioctl_search_args_v2 __user *uarg;
2223 struct btrfs_ioctl_search_args_v2 args;
2224 struct inode *inode;
2225 int ret;
2226 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002227 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002228
2229 if (!capable(CAP_SYS_ADMIN))
2230 return -EPERM;
2231
2232 /* copy search header and buffer size */
2233 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2234 if (copy_from_user(&args, uarg, sizeof(args)))
2235 return -EFAULT;
2236
2237 buf_size = args.buf_size;
2238
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002239 /* limit result size to 16MB */
2240 if (buf_size > buf_limit)
2241 buf_size = buf_limit;
2242
2243 inode = file_inode(file);
2244 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002245 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002246 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2247 ret = -EFAULT;
2248 else if (ret == -EOVERFLOW &&
2249 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2250 ret = -EFAULT;
2251
Yan, Zheng76dda932009-09-21 16:00:26 -04002252 return ret;
2253}
2254
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002255/*
Chris Masonac8e9812010-02-28 15:39:26 -05002256 * Search INODE_REFs to identify path name of 'dirid' directory
2257 * in a 'tree_id' tree. and sets path name to 'name'.
2258 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002259static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2260 u64 tree_id, u64 dirid, char *name)
2261{
2262 struct btrfs_root *root;
2263 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002264 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002265 int ret = -1;
2266 int slot;
2267 int len;
2268 int total_len = 0;
2269 struct btrfs_inode_ref *iref;
2270 struct extent_buffer *l;
2271 struct btrfs_path *path;
2272
2273 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2274 name[0]='\0';
2275 return 0;
2276 }
2277
2278 path = btrfs_alloc_path();
2279 if (!path)
2280 return -ENOMEM;
2281
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002282 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002283
2284 key.objectid = tree_id;
2285 key.type = BTRFS_ROOT_ITEM_KEY;
2286 key.offset = (u64)-1;
2287 root = btrfs_read_fs_root_no_name(info, &key);
2288 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002289 ret = PTR_ERR(root);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002290 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002291 }
2292
2293 key.objectid = dirid;
2294 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002295 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002296
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302297 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002298 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2299 if (ret < 0)
2300 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002301 else if (ret > 0) {
2302 ret = btrfs_previous_item(root, path, dirid,
2303 BTRFS_INODE_REF_KEY);
2304 if (ret < 0)
2305 goto out;
2306 else if (ret > 0) {
2307 ret = -ENOENT;
2308 goto out;
2309 }
2310 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002311
2312 l = path->nodes[0];
2313 slot = path->slots[0];
2314 btrfs_item_key_to_cpu(l, &key, slot);
2315
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002316 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2317 len = btrfs_inode_ref_name_len(l, iref);
2318 ptr -= len + 1;
2319 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002320 if (ptr < name) {
2321 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002322 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002323 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002324
2325 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302326 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002327
2328 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2329 break;
2330
David Sterbab3b4aa72011-04-21 01:20:15 +02002331 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002332 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002333 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002334 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002335 }
Li Zefan77906a502011-07-14 03:16:00 +00002336 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302337 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002338 ret = 0;
2339out:
2340 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002341 return ret;
2342}
2343
2344static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2345 void __user *argp)
2346{
2347 struct btrfs_ioctl_ino_lookup_args *args;
2348 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002349 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002350
Julia Lawall2354d082010-10-29 15:14:18 -04002351 args = memdup_user(argp, sizeof(*args));
2352 if (IS_ERR(args))
2353 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002354
Al Viro496ad9a2013-01-23 17:07:38 -05002355 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002356
David Sterba01b810b2015-05-12 19:14:49 +02002357 /*
2358 * Unprivileged query to obtain the containing subvolume root id. The
2359 * path is reset so it's consistent with btrfs_search_path_in_tree.
2360 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002361 if (args->treeid == 0)
2362 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2363
David Sterba01b810b2015-05-12 19:14:49 +02002364 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2365 args->name[0] = 0;
2366 goto out;
2367 }
2368
2369 if (!capable(CAP_SYS_ADMIN)) {
2370 ret = -EPERM;
2371 goto out;
2372 }
2373
Chris Masonac8e9812010-02-28 15:39:26 -05002374 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2375 args->treeid, args->objectid,
2376 args->name);
2377
David Sterba01b810b2015-05-12 19:14:49 +02002378out:
Chris Masonac8e9812010-02-28 15:39:26 -05002379 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2380 ret = -EFAULT;
2381
2382 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002383 return ret;
2384}
2385
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002386/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2387static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2388{
2389 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2390 struct btrfs_fs_info *fs_info;
2391 struct btrfs_root *root;
2392 struct btrfs_path *path;
2393 struct btrfs_key key;
2394 struct btrfs_root_item *root_item;
2395 struct btrfs_root_ref *rref;
2396 struct extent_buffer *leaf;
2397 unsigned long item_off;
2398 unsigned long item_len;
2399 struct inode *inode;
2400 int slot;
2401 int ret = 0;
2402
2403 path = btrfs_alloc_path();
2404 if (!path)
2405 return -ENOMEM;
2406
2407 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2408 if (!subvol_info) {
2409 btrfs_free_path(path);
2410 return -ENOMEM;
2411 }
2412
2413 inode = file_inode(file);
2414 fs_info = BTRFS_I(inode)->root->fs_info;
2415
2416 /* Get root_item of inode's subvolume */
2417 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2418 key.type = BTRFS_ROOT_ITEM_KEY;
2419 key.offset = (u64)-1;
2420 root = btrfs_read_fs_root_no_name(fs_info, &key);
2421 if (IS_ERR(root)) {
2422 ret = PTR_ERR(root);
2423 goto out;
2424 }
2425 root_item = &root->root_item;
2426
2427 subvol_info->treeid = key.objectid;
2428
2429 subvol_info->generation = btrfs_root_generation(root_item);
2430 subvol_info->flags = btrfs_root_flags(root_item);
2431
2432 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2433 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2434 BTRFS_UUID_SIZE);
2435 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2436 BTRFS_UUID_SIZE);
2437
2438 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2439 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2440 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2441
2442 subvol_info->otransid = btrfs_root_otransid(root_item);
2443 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2444 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2445
2446 subvol_info->stransid = btrfs_root_stransid(root_item);
2447 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2448 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2449
2450 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2451 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2452 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2453
2454 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2455 /* Search root tree for ROOT_BACKREF of this subvolume */
2456 root = fs_info->tree_root;
2457
2458 key.type = BTRFS_ROOT_BACKREF_KEY;
2459 key.offset = 0;
2460 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2461 if (ret < 0) {
2462 goto out;
2463 } else if (path->slots[0] >=
2464 btrfs_header_nritems(path->nodes[0])) {
2465 ret = btrfs_next_leaf(root, path);
2466 if (ret < 0) {
2467 goto out;
2468 } else if (ret > 0) {
2469 ret = -EUCLEAN;
2470 goto out;
2471 }
2472 }
2473
2474 leaf = path->nodes[0];
2475 slot = path->slots[0];
2476 btrfs_item_key_to_cpu(leaf, &key, slot);
2477 if (key.objectid == subvol_info->treeid &&
2478 key.type == BTRFS_ROOT_BACKREF_KEY) {
2479 subvol_info->parent_id = key.offset;
2480
2481 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2482 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2483
2484 item_off = btrfs_item_ptr_offset(leaf, slot)
2485 + sizeof(struct btrfs_root_ref);
2486 item_len = btrfs_item_size_nr(leaf, slot)
2487 - sizeof(struct btrfs_root_ref);
2488 read_extent_buffer(leaf, subvol_info->name,
2489 item_off, item_len);
2490 } else {
2491 ret = -ENOENT;
2492 goto out;
2493 }
2494 }
2495
2496 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2497 ret = -EFAULT;
2498
2499out:
2500 btrfs_free_path(path);
2501 kzfree(subvol_info);
2502 return ret;
2503}
2504
Yan, Zheng76dda932009-09-21 16:00:26 -04002505static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2506 void __user *arg)
2507{
Al Viro54563d42013-09-01 15:57:51 -04002508 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002509 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002510 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002511 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002512 struct inode *inode;
2513 struct btrfs_root *root = BTRFS_I(dir)->root;
2514 struct btrfs_root *dest = NULL;
2515 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002516 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002517 int err = 0;
2518
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002519 if (!S_ISDIR(dir->i_mode))
2520 return -ENOTDIR;
2521
Yan, Zheng76dda932009-09-21 16:00:26 -04002522 vol_args = memdup_user(arg, sizeof(*vol_args));
2523 if (IS_ERR(vol_args))
2524 return PTR_ERR(vol_args);
2525
2526 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2527 namelen = strlen(vol_args->name);
2528 if (strchr(vol_args->name, '/') ||
2529 strncmp(vol_args->name, "..", namelen) == 0) {
2530 err = -EINVAL;
2531 goto out;
2532 }
2533
Al Viroa561be72011-11-23 11:57:51 -05002534 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002535 if (err)
2536 goto out;
2537
David Sterba521e0542014-04-15 16:41:44 +02002538
Al Viro00235412016-05-26 00:05:12 -04002539 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2540 if (err == -EINTR)
2541 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002542 dentry = lookup_one_len(vol_args->name, parent, namelen);
2543 if (IS_ERR(dentry)) {
2544 err = PTR_ERR(dentry);
2545 goto out_unlock_dir;
2546 }
2547
David Howells2b0143b2015-03-17 22:25:59 +00002548 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002549 err = -ENOENT;
2550 goto out_dput;
2551 }
2552
David Howells2b0143b2015-03-17 22:25:59 +00002553 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002554 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302555 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002556 /*
2557 * Regular user. Only allow this with a special mount
2558 * option, when the user has write+exec access to the
2559 * subvol root, and when rmdir(2) would have been
2560 * allowed.
2561 *
2562 * Note that this is _not_ check that the subvol is
2563 * empty or doesn't contain data that we wouldn't
2564 * otherwise be able to delete.
2565 *
2566 * Users who want to delete empty subvols should try
2567 * rmdir(2).
2568 */
2569 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002570 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002571 goto out_dput;
2572
2573 /*
2574 * Do not allow deletion if the parent dir is the same
2575 * as the dir to be deleted. That means the ioctl
2576 * must be called on the dentry referencing the root
2577 * of the subvol, not a random directory contained
2578 * within it.
2579 */
2580 err = -EINVAL;
2581 if (root == dest)
2582 goto out_dput;
2583
2584 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2585 if (err)
2586 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002587 }
2588
Miao Xie5c39da52012-10-22 11:39:53 +00002589 /* check if subvolume may be deleted by a user */
2590 err = btrfs_may_delete(dir, dentry, 1);
2591 if (err)
2592 goto out_dput;
2593
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002594 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002595 err = -EINVAL;
2596 goto out_dput;
2597 }
2598
Al Viro59551022016-01-22 15:40:57 -05002599 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002600 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002601 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002602 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002603 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002604
Yan, Zheng76dda932009-09-21 16:00:26 -04002605out_dput:
2606 dput(dentry);
2607out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002608 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002609out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002610 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002611out:
2612 kfree(vol_args);
2613 return err;
2614}
2615
Chris Mason1e701a32010-03-11 09:42:04 -05002616static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002617{
Al Viro496ad9a2013-01-23 17:07:38 -05002618 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002619 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002620 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002621 int ret;
2622
Ilya Dryomov25122d12013-01-20 15:57:57 +02002623 ret = mnt_want_write_file(file);
2624 if (ret)
2625 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002626
Ilya Dryomov25122d12013-01-20 15:57:57 +02002627 if (btrfs_root_readonly(root)) {
2628 ret = -EROFS;
2629 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002630 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002631
2632 switch (inode->i_mode & S_IFMT) {
2633 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002634 if (!capable(CAP_SYS_ADMIN)) {
2635 ret = -EPERM;
2636 goto out;
2637 }
Eric Sandeende78b512013-01-31 18:21:12 +00002638 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002639 break;
2640 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002641 if (!(file->f_mode & FMODE_WRITE)) {
2642 ret = -EINVAL;
2643 goto out;
2644 }
Chris Mason1e701a32010-03-11 09:42:04 -05002645
2646 range = kzalloc(sizeof(*range), GFP_KERNEL);
2647 if (!range) {
2648 ret = -ENOMEM;
2649 goto out;
2650 }
2651
2652 if (argp) {
2653 if (copy_from_user(range, argp,
2654 sizeof(*range))) {
2655 ret = -EFAULT;
2656 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002657 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002658 }
2659 /* compression requires us to start the IO */
2660 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2661 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2662 range->extent_thresh = (u32)-1;
2663 }
2664 } else {
2665 /* the rest are all set to zero by kzalloc */
2666 range->len = (u64)-1;
2667 }
Al Viro496ad9a2013-01-23 17:07:38 -05002668 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08002669 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002670 if (ret > 0)
2671 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002672 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002673 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002674 default:
2675 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002676 }
Chris Masone441d542009-01-05 16:57:23 -05002677out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002678 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002679 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002680}
2681
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002682static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002683{
2684 struct btrfs_ioctl_vol_args *vol_args;
2685 int ret;
2686
Chris Masone441d542009-01-05 16:57:23 -05002687 if (!capable(CAP_SYS_ADMIN))
2688 return -EPERM;
2689
David Sterba171938e2017-03-28 14:44:21 +02002690 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002691 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002692
Li Zefandae7b662009-04-08 15:06:54 +08002693 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002694 if (IS_ERR(vol_args)) {
2695 ret = PTR_ERR(vol_args);
2696 goto out;
2697 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002698
Mark Fasheh5516e592008-07-24 12:20:14 -04002699 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002700 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002701
Anand Jain43d20762014-07-01 00:58:56 +08002702 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002703 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002704
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002705 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002706out:
David Sterba171938e2017-03-28 14:44:21 +02002707 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002708 return ret;
2709}
2710
Anand Jain6b526ed2016-02-13 10:01:39 +08002711static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002712{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002713 struct inode *inode = file_inode(file);
2714 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08002715 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002716 int ret;
2717
Chris Masone441d542009-01-05 16:57:23 -05002718 if (!capable(CAP_SYS_ADMIN))
2719 return -EPERM;
2720
Miao Xieda249272012-11-26 08:44:50 +00002721 ret = mnt_want_write_file(file);
2722 if (ret)
2723 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002724
Li Zefandae7b662009-04-08 15:06:54 +08002725 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002726 if (IS_ERR(vol_args)) {
2727 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002728 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002729 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002730
Anand Jain6b526ed2016-02-13 10:01:39 +08002731 /* Check for compatibility reject unknown flags */
Omar Sandovalfd4e9942018-05-22 15:44:01 -07002732 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
2733 ret = -EOPNOTSUPP;
2734 goto out;
2735 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002736
David Sterba171938e2017-03-28 14:44:21 +02002737 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00002738 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2739 goto out;
2740 }
2741
David Sterba735654e2016-02-15 18:15:21 +01002742 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002743 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08002744 } else {
2745 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002746 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08002747 }
David Sterba171938e2017-03-28 14:44:21 +02002748 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00002749
Anand Jain6b526ed2016-02-13 10:01:39 +08002750 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002751 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002752 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08002753 vol_args->devid);
2754 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002755 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08002756 vol_args->name);
2757 }
Anand Jain183860f2013-05-17 10:52:45 +00002758out:
2759 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002760err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002761 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002762 return ret;
2763}
2764
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002765static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2766{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002767 struct inode *inode = file_inode(file);
2768 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002769 struct btrfs_ioctl_vol_args *vol_args;
2770 int ret;
2771
2772 if (!capable(CAP_SYS_ADMIN))
2773 return -EPERM;
2774
2775 ret = mnt_want_write_file(file);
2776 if (ret)
2777 return ret;
2778
David Sterba171938e2017-03-28 14:44:21 +02002779 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002780 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002781 goto out_drop_write;
2782 }
2783
2784 vol_args = memdup_user(arg, sizeof(*vol_args));
2785 if (IS_ERR(vol_args)) {
2786 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002787 goto out;
2788 }
2789
David Sterba58d7bbf2016-05-04 14:10:47 +02002790 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002791 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002792
2793 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002794 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002795 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002796out:
David Sterba171938e2017-03-28 14:44:21 +02002797 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02002798out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002799 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002800
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002801 return ret;
2802}
2803
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002804static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2805 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002806{
Li Zefan027ed2f2011-06-08 08:27:56 +00002807 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002808 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002809 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002810 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002811
Li Zefan027ed2f2011-06-08 08:27:56 +00002812 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2813 if (!fi_args)
2814 return -ENOMEM;
2815
David Sterbad03262c2017-06-16 00:09:21 +02002816 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00002817 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01002818
David Sterbad03262c2017-06-16 00:09:21 +02002819 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002820 if (device->devid > fi_args->max_id)
2821 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002822 }
David Sterbad03262c2017-06-16 00:09:21 +02002823 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002824
David Sterbad03262c2017-06-16 00:09:21 +02002825 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07002826 fi_args->nodesize = fs_info->nodesize;
2827 fi_args->sectorsize = fs_info->sectorsize;
2828 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02002829
Li Zefan027ed2f2011-06-08 08:27:56 +00002830 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2831 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002832
Li Zefan027ed2f2011-06-08 08:27:56 +00002833 kfree(fi_args);
2834 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002835}
2836
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002837static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2838 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002839{
2840 struct btrfs_ioctl_dev_info_args *di_args;
2841 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01002842 int ret = 0;
2843 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002844
Jan Schmidt475f6382011-03-11 15:41:01 +01002845 di_args = memdup_user(arg, sizeof(*di_args));
2846 if (IS_ERR(di_args))
2847 return PTR_ERR(di_args);
2848
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002849 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002850 s_uuid = di_args->uuid;
2851
David Sterbac5593ca2017-06-16 00:09:21 +02002852 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002853 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002854
2855 if (!dev) {
2856 ret = -ENODEV;
2857 goto out;
2858 }
2859
2860 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002861 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2862 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002863 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002864 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002865 struct rcu_string *name;
2866
Josef Bacik606686e2012-06-04 14:03:51 -04002867 name = rcu_dereference(dev->name);
Xiongfeng Wang6670d4c2018-01-08 19:51:22 +08002868 strncpy(di_args->path, name->str, sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02002869 di_args->path[sizeof(di_args->path) - 1] = 0;
2870 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002871 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002872 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002873
2874out:
David Sterbac5593ca2017-06-16 00:09:21 +02002875 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002876 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2877 ret = -EFAULT;
2878
2879 kfree(di_args);
2880 return ret;
2881}
2882
Mark Fashehf4414602015-06-30 14:42:05 -07002883static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002884{
2885 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002886
Mark Fasheh416161d2013-08-06 11:42:51 -07002887 page = grab_cache_page(inode->i_mapping, index);
2888 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002889 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002890
2891 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002892 int ret;
2893
2894 ret = btrfs_readpage(NULL, page);
2895 if (ret)
2896 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002897 lock_page(page);
2898 if (!PageUptodate(page)) {
2899 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002900 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002901 return ERR_PTR(-EIO);
2902 }
2903 if (page->mapping != inode->i_mapping) {
2904 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002905 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002906 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002907 }
2908 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002909
2910 return page;
2911}
2912
Mark Fashehf4414602015-06-30 14:42:05 -07002913static int gather_extent_pages(struct inode *inode, struct page **pages,
2914 int num_pages, u64 off)
2915{
2916 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002917 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002918
2919 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002920again:
Mark Fashehf4414602015-06-30 14:42:05 -07002921 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002922 if (IS_ERR(pages[i])) {
2923 int err = PTR_ERR(pages[i]);
2924
2925 if (err == -EAGAIN)
2926 goto again;
2927 pages[i] = NULL;
2928 return err;
2929 }
Mark Fashehf4414602015-06-30 14:42:05 -07002930 }
2931 return 0;
2932}
2933
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002934static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2935 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002936{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002937 /*
2938 * Do any pending delalloc/csum calculations on inode, one way or
2939 * another, and lock file content.
2940 * The locking order is:
2941 *
2942 * 1) pages
2943 * 2) range in the inode's io tree
2944 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002945 while (1) {
2946 struct btrfs_ordered_extent *ordered;
2947 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2948 ordered = btrfs_lookup_first_ordered_extent(inode,
2949 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002950 if ((!ordered ||
2951 ordered->file_offset + ordered->len <= off ||
2952 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002953 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002954 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2955 if (ordered)
2956 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002957 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002958 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002959 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2960 if (ordered)
2961 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002962 if (!retry_range_locking)
2963 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002964 btrfs_wait_ordered_range(inode, off, len);
2965 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002966 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002967}
2968
Mark Fashehf4414602015-06-30 14:42:05 -07002969static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002970{
Al Viro59551022016-01-22 15:40:57 -05002971 inode_unlock(inode1);
2972 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002973}
2974
Mark Fashehf4414602015-06-30 14:42:05 -07002975static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2976{
2977 if (inode1 < inode2)
2978 swap(inode1, inode2);
2979
Al Viro59551022016-01-22 15:40:57 -05002980 inode_lock_nested(inode1, I_MUTEX_PARENT);
2981 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002982}
2983
2984static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2985 struct inode *inode2, u64 loff2, u64 len)
2986{
2987 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2988 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2989}
2990
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002991static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2992 struct inode *inode2, u64 loff2, u64 len,
2993 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002994{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002995 int ret;
2996
Mark Fasheh416161d2013-08-06 11:42:51 -07002997 if (inode1 < inode2) {
2998 swap(inode1, inode2);
2999 swap(loff1, loff2);
3000 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003001 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
3002 if (ret)
3003 return ret;
3004 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
3005 if (ret)
3006 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
3007 loff1 + len - 1);
3008 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003009}
3010
3011struct cmp_pages {
3012 int num_pages;
3013 struct page **src_pages;
3014 struct page **dst_pages;
3015};
3016
3017static void btrfs_cmp_data_free(struct cmp_pages *cmp)
3018{
3019 int i;
3020 struct page *pg;
3021
3022 for (i = 0; i < cmp->num_pages; i++) {
3023 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003024 if (pg) {
3025 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003026 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003027 }
Mark Fashehf4414602015-06-30 14:42:05 -07003028 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003029 if (pg) {
3030 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003031 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003032 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003033 }
Mark Fashehf4414602015-06-30 14:42:05 -07003034}
3035
3036static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
3037 struct inode *dst, u64 dst_loff,
3038 u64 len, struct cmp_pages *cmp)
3039{
3040 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003041 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07003042
Mark Fashehf4414602015-06-30 14:42:05 -07003043 cmp->num_pages = num_pages;
Mark Fashehf4414602015-06-30 14:42:05 -07003044
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003045 ret = gather_extent_pages(src, cmp->src_pages, num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003046 if (ret)
3047 goto out;
3048
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003049 ret = gather_extent_pages(dst, cmp->dst_pages, num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003050
3051out:
3052 if (ret)
3053 btrfs_cmp_data_free(cmp);
Naohiro Aota78ad4ce2017-09-08 17:48:55 +09003054 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003055}
3056
David Sterba1a287cf2017-02-10 20:15:10 +01003057static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003058{
3059 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07003060 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07003061 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003062 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003063 void *addr, *dst_addr;
3064
Mark Fashehf4414602015-06-30 14:42:05 -07003065 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003066 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003067 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07003068 cmp_len = len;
3069
Mark Fashehf4414602015-06-30 14:42:05 -07003070 BUG_ON(i >= cmp->num_pages);
3071
3072 src_page = cmp->src_pages[i];
3073 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003074 ASSERT(PageLocked(src_page));
3075 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003076
Mark Fasheh416161d2013-08-06 11:42:51 -07003077 addr = kmap_atomic(src_page);
3078 dst_addr = kmap_atomic(dst_page);
3079
3080 flush_dcache_page(src_page);
3081 flush_dcache_page(dst_page);
3082
3083 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003084 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003085
3086 kunmap_atomic(addr);
3087 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003088
3089 if (ret)
3090 break;
3091
Mark Fasheh416161d2013-08-06 11:42:51 -07003092 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003093 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003094 }
3095
3096 return ret;
3097}
3098
Mark Fashehe1d227a2015-06-08 15:05:25 -07003099static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3100 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003101{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003102 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003103 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3104
Mark Fashehe1d227a2015-06-08 15:05:25 -07003105 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003106 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003107
3108 /* if we extend to eof, continue to block boundary */
3109 if (off + len == inode->i_size)
3110 *plen = len = ALIGN(inode->i_size, bs) - off;
3111
Mark Fasheh416161d2013-08-06 11:42:51 -07003112 /* Check that we are block aligned - btrfs_clone() requires this */
3113 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3114 return -EINVAL;
3115
3116 return 0;
3117}
3118
Timofey Titovets39739092018-05-02 08:15:36 +03003119static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen,
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003120 struct inode *dst, u64 dst_loff,
3121 struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003122{
3123 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003124 u64 len = olen;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003125 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003126 u64 same_lock_start = 0;
3127 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003128
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003129 ret = extent_same_check_offsets(src, loff, &len, olen);
3130 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003131 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003132
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003133 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3134 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003135 return ret;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003136
3137 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003138 /*
3139 * Single inode case wants the same checks, except we
3140 * don't want our length pushed out past i_size as
3141 * comparing that data range makes no sense.
3142 *
3143 * extent_same_check_offsets() will do this for an
3144 * unaligned length at i_size, so catch it here and
3145 * reject the request.
3146 *
3147 * This effectively means we require aligned extents
3148 * for the single-inode case, whereas the other cases
3149 * allow an unaligned length so long as it ends at
3150 * i_size.
3151 */
Timofey Titovets39739092018-05-02 08:15:36 +03003152 if (len != olen)
3153 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003154
3155 /* Check for overlapping ranges */
Timofey Titovets39739092018-05-02 08:15:36 +03003156 if (dst_loff + len > loff && dst_loff < loff + len)
3157 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003158
3159 same_lock_start = min_t(u64, loff, dst_loff);
3160 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003161 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003162
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003163again:
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003164 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, cmp);
Mark Fashehf4414602015-06-30 14:42:05 -07003165 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003166 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003167
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003168 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003169 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3170 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003171 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003172 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3173 false);
3174 /*
3175 * If one of the inodes has dirty pages in the respective range or
3176 * ordered extents, we need to flush dellaloc and wait for all ordered
3177 * extents in the range. We must unlock the pages and the ranges in the
3178 * io trees to avoid deadlocks when flushing delalloc (requires locking
3179 * pages) and when waiting for ordered extents to complete (they require
3180 * range locking).
3181 */
3182 if (ret == -EAGAIN) {
3183 /*
3184 * Ranges in the io trees already unlocked. Now unlock all
3185 * pages before waiting for all IO to complete.
3186 */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003187 btrfs_cmp_data_free(cmp);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003188 if (same_inode) {
3189 btrfs_wait_ordered_range(src, same_lock_start,
3190 same_lock_len);
3191 } else {
3192 btrfs_wait_ordered_range(src, loff, len);
3193 btrfs_wait_ordered_range(dst, dst_loff, len);
3194 }
3195 goto again;
3196 }
3197 ASSERT(ret == 0);
3198 if (WARN_ON(ret)) {
3199 /* ranges in the io trees already unlocked */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003200 btrfs_cmp_data_free(cmp);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003201 return ret;
3202 }
Mark Fashehf4414602015-06-30 14:42:05 -07003203
Mark Fasheh207910d2015-06-30 14:42:04 -07003204 /* pass original length for comparison so we stay within i_size */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003205 ret = btrfs_cmp_data(olen, cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003206 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003207 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003208
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003209 if (same_inode)
3210 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3211 same_lock_start + same_lock_len - 1);
3212 else
3213 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003214
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003215 btrfs_cmp_data_free(cmp);
Timofey Titovets39739092018-05-02 08:15:36 +03003216
3217 return ret;
3218}
3219
Timofey Titovetsb6728762018-05-02 08:15:37 +03003220#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3221
Timofey Titovets39739092018-05-02 08:15:36 +03003222static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3223 struct inode *dst, u64 dst_loff)
3224{
3225 int ret;
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003226 struct cmp_pages cmp;
3227 int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT;
Timofey Titovets39739092018-05-02 08:15:36 +03003228 bool same_inode = (src == dst);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003229 u64 i, tail_len, chunk_count;
Timofey Titovets39739092018-05-02 08:15:36 +03003230
3231 if (olen == 0)
3232 return 0;
3233
3234 if (same_inode)
3235 inode_lock(src);
3236 else
3237 btrfs_double_inode_lock(src, dst);
3238
3239 /* don't make the dst file partly checksummed */
3240 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3241 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3242 ret = -EINVAL;
3243 goto out_unlock;
3244 }
3245
Timofey Titovetsb6728762018-05-02 08:15:37 +03003246 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3247 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003248 if (chunk_count == 0)
3249 num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT;
3250
3251 /*
3252 * If deduping ranges in the same inode, locking rules make it
3253 * mandatory to always lock pages in ascending order to avoid deadlocks
3254 * with concurrent tasks (such as starting writeback/delalloc).
3255 */
3256 if (same_inode && dst_loff < loff)
3257 swap(loff, dst_loff);
3258
3259 /*
3260 * We must gather up all the pages before we initiate our extent
3261 * locking. We use an array for the page pointers. Size of the array is
3262 * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
3263 */
David Sterbabf5091c2018-05-11 17:57:54 +02003264 cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3265 GFP_KERNEL | __GFP_ZERO);
3266 cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3267 GFP_KERNEL | __GFP_ZERO);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003268 if (!cmp.src_pages || !cmp.dst_pages) {
David Sterbabf5091c2018-05-11 17:57:54 +02003269 ret = -ENOMEM;
3270 goto out_free;
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003271 }
Timofey Titovetsb6728762018-05-02 08:15:37 +03003272
3273 for (i = 0; i < chunk_count; i++) {
3274 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003275 dst, dst_loff, &cmp);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003276 if (ret)
3277 goto out_unlock;
3278
3279 loff += BTRFS_MAX_DEDUPE_LEN;
3280 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3281 }
3282
3283 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003284 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
3285 dst_loff, &cmp);
Timofey Titovets39739092018-05-02 08:15:36 +03003286
Mark Fasheh416161d2013-08-06 11:42:51 -07003287out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003288 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003289 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003290 else
3291 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003292
David Sterbabf5091c2018-05-11 17:57:54 +02003293out_free:
3294 kvfree(cmp.src_pages);
3295 kvfree(cmp.dst_pages);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003296
Mark Fasheh416161d2013-08-06 11:42:51 -07003297 return ret;
3298}
3299
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003300ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3301 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003302{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003303 struct inode *src = file_inode(src_file);
3304 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003305 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003306 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003307
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003308 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003309 /*
3310 * Btrfs does not support blocksize < page_size. As a
3311 * result, btrfs_cmp_data() won't correctly handle
3312 * this situation without an update.
3313 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003314 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003315 }
3316
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003317 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3318 if (res)
3319 return res;
3320 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003321}
3322
Filipe Mananaf82a9902014-06-01 01:50:28 +01003323static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3324 struct inode *inode,
3325 u64 endoff,
3326 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003327 const u64 olen,
3328 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003329{
3330 struct btrfs_root *root = BTRFS_I(inode)->root;
3331 int ret;
3332
3333 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003334 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003335 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003336 /*
3337 * We round up to the block size at eof when determining which
3338 * extents to clone above, but shouldn't round up the file size.
3339 */
3340 if (endoff > destoff + olen)
3341 endoff = destoff + olen;
3342 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003343 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003344
3345 ret = btrfs_update_inode(trans, root, inode);
3346 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003347 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003348 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003349 goto out;
3350 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003351 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003352out:
3353 return ret;
3354}
3355
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003356static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003357 const struct btrfs_trans_handle *trans,
3358 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003359 const u64 hole_offset,
3360 const u64 hole_len)
3361{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003362 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003363 struct extent_map *em;
3364 int ret;
3365
3366 em = alloc_extent_map();
3367 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003368 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003369 return;
3370 }
3371
Filipe Manana14f59792014-06-29 21:45:40 +01003372 if (path) {
3373 struct btrfs_file_extent_item *fi;
3374
3375 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3376 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003377 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003378 em->generation = -1;
3379 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3380 BTRFS_FILE_EXTENT_INLINE)
3381 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003382 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003383 } else {
3384 em->start = hole_offset;
3385 em->len = hole_len;
3386 em->ram_bytes = em->len;
3387 em->orig_start = hole_offset;
3388 em->block_start = EXTENT_MAP_HOLE;
3389 em->block_len = 0;
3390 em->orig_block_len = 0;
3391 em->compress_type = BTRFS_COMPRESS_NONE;
3392 em->generation = trans->transid;
3393 }
3394
3395 while (1) {
3396 write_lock(&em_tree->lock);
3397 ret = add_extent_mapping(em_tree, em, 1);
3398 write_unlock(&em_tree->lock);
3399 if (ret != -EEXIST) {
3400 free_extent_map(em);
3401 break;
3402 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003403 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003404 em->start + em->len - 1, 0);
3405 }
3406
David Sterbaee39b432014-09-30 01:33:33 +02003407 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003408 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003409}
3410
Filipe Manana8039d872015-10-13 15:15:00 +01003411/*
3412 * Make sure we do not end up inserting an inline extent into a file that has
3413 * already other (non-inline) extents. If a file has an inline extent it can
3414 * not have any other extents and the (single) inline extent must start at the
3415 * file offset 0. Failing to respect these rules will lead to file corruption,
3416 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3417 *
3418 * We can have extents that have been already written to disk or we can have
3419 * dirty ranges still in delalloc, in which case the extent maps and items are
3420 * created only when we run delalloc, and the delalloc ranges might fall outside
3421 * the range we are currently locking in the inode's io tree. So we check the
3422 * inode's i_size because of that (i_size updates are done while holding the
3423 * i_mutex, which we are holding here).
3424 * We also check to see if the inode has a size not greater than "datal" but has
3425 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3426 * protected against such concurrent fallocate calls by the i_mutex).
3427 *
3428 * If the file has no extents but a size greater than datal, do not allow the
3429 * copy because we would need turn the inline extent into a non-inline one (even
3430 * with NO_HOLES enabled). If we find our destination inode only has one inline
3431 * extent, just overwrite it with the source inline extent if its size is less
3432 * than the source extent's size, or we could copy the source inline extent's
3433 * data into the destination inode's inline extent if the later is greater then
3434 * the former.
3435 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003436static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003437 struct btrfs_trans_handle *trans,
3438 struct btrfs_path *path,
3439 struct btrfs_key *new_key,
3440 const u64 drop_start,
3441 const u64 datal,
3442 const u64 skip,
3443 const u64 size,
3444 char *inline_data)
3445{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003446 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003447 struct btrfs_root *root = BTRFS_I(dst)->root;
3448 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003449 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003450 int ret;
3451 struct btrfs_key key;
3452
3453 if (new_key->offset > 0)
3454 return -EOPNOTSUPP;
3455
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003456 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003457 key.type = BTRFS_EXTENT_DATA_KEY;
3458 key.offset = 0;
3459 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3460 if (ret < 0) {
3461 return ret;
3462 } else if (ret > 0) {
3463 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3464 ret = btrfs_next_leaf(root, path);
3465 if (ret < 0)
3466 return ret;
3467 else if (ret > 0)
3468 goto copy_inline_extent;
3469 }
3470 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003471 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003472 key.type == BTRFS_EXTENT_DATA_KEY) {
3473 ASSERT(key.offset > 0);
3474 return -EOPNOTSUPP;
3475 }
3476 } else if (i_size_read(dst) <= datal) {
3477 struct btrfs_file_extent_item *ei;
3478 u64 ext_len;
3479
3480 /*
3481 * If the file size is <= datal, make sure there are no other
3482 * extents following (can happen do to an fallocate call with
3483 * the flag FALLOC_FL_KEEP_SIZE).
3484 */
3485 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3486 struct btrfs_file_extent_item);
3487 /*
3488 * If it's an inline extent, it can not have other extents
3489 * following it.
3490 */
3491 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3492 BTRFS_FILE_EXTENT_INLINE)
3493 goto copy_inline_extent;
3494
3495 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3496 if (ext_len > aligned_end)
3497 return -EOPNOTSUPP;
3498
3499 ret = btrfs_next_item(root, path);
3500 if (ret < 0) {
3501 return ret;
3502 } else if (ret == 0) {
3503 btrfs_item_key_to_cpu(path->nodes[0], &key,
3504 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003505 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003506 key.type == BTRFS_EXTENT_DATA_KEY)
3507 return -EOPNOTSUPP;
3508 }
3509 }
3510
3511copy_inline_extent:
3512 /*
3513 * We have no extent items, or we have an extent at offset 0 which may
3514 * or may not be inlined. All these cases are dealt the same way.
3515 */
3516 if (i_size_read(dst) > datal) {
3517 /*
3518 * If the destination inode has an inline extent...
3519 * This would require copying the data from the source inline
3520 * extent into the beginning of the destination's inline extent.
3521 * But this is really complex, both extents can be compressed
3522 * or just one of them, which would require decompressing and
3523 * re-compressing data (which could increase the new compressed
3524 * size, not allowing the compressed data to fit anymore in an
3525 * inline extent).
3526 * So just don't support this case for now (it should be rare,
3527 * we are not really saving space when cloning inline extents).
3528 */
3529 return -EOPNOTSUPP;
3530 }
3531
3532 btrfs_release_path(path);
3533 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3534 if (ret)
3535 return ret;
3536 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3537 if (ret)
3538 return ret;
3539
3540 if (skip) {
3541 const u32 start = btrfs_file_extent_calc_inline_size(0);
3542
3543 memmove(inline_data + start, inline_data + start + skip, datal);
3544 }
3545
3546 write_extent_buffer(path->nodes[0], inline_data,
3547 btrfs_item_ptr_offset(path->nodes[0],
3548 path->slots[0]),
3549 size);
3550 inode_add_bytes(dst, datal);
3551
3552 return 0;
3553}
3554
Mark Fasheh32b7c682013-08-06 11:42:49 -07003555/**
3556 * btrfs_clone() - clone a range from inode file to another
3557 *
3558 * @src: Inode to clone from
3559 * @inode: Inode to clone to
3560 * @off: Offset within source to start clone from
3561 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003562 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003563 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003564 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003565 */
3566static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003567 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003568 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003569{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003570 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003571 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003572 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003573 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003574 struct btrfs_trans_handle *trans;
3575 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003576 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003577 u32 nritems;
3578 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003579 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003580 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003581 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003582
3583 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003584 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3585 if (!buf)
3586 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003587
3588 path = btrfs_alloc_path();
3589 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003590 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003591 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003592 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003593
David Sterbae4058b52015-11-27 16:31:35 +01003594 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003595 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003596 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003597 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003598 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003599
3600 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003601 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003602
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003603 /*
3604 * note the key will change type as we walk through the
3605 * tree.
3606 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003607 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003608 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3609 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003610 if (ret < 0)
3611 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003612 /*
3613 * First search, if no extent item that starts at offset off was
3614 * found but the previous item is an extent item, it's possible
3615 * it might overlap our target range, therefore process it.
3616 */
3617 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3618 btrfs_item_key_to_cpu(path->nodes[0], &key,
3619 path->slots[0] - 1);
3620 if (key.type == BTRFS_EXTENT_DATA_KEY)
3621 path->slots[0]--;
3622 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003623
Yan Zhengae01a0a2008-08-04 23:23:47 -04003624 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003625process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003626 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003627 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003628 if (ret < 0)
3629 goto out;
3630 if (ret > 0)
3631 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003632 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003633 }
3634 leaf = path->nodes[0];
3635 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003636
Yan Zhengae01a0a2008-08-04 23:23:47 -04003637 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003638 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003639 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003640 break;
3641
David Sterba962a2982014-06-04 18:41:45 +02003642 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003643 struct btrfs_file_extent_item *extent;
3644 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003645 u32 size;
3646 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003647 u64 disko = 0, diskl = 0;
3648 u64 datao = 0, datal = 0;
3649 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003650 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003651
Sage Weilc5c9cd42008-11-12 14:32:25 -05003652 extent = btrfs_item_ptr(leaf, slot,
3653 struct btrfs_file_extent_item);
3654 comp = btrfs_file_extent_compression(leaf, extent);
3655 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003656 if (type == BTRFS_FILE_EXTENT_REG ||
3657 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003658 disko = btrfs_file_extent_disk_bytenr(leaf,
3659 extent);
3660 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3661 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003662 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003663 datal = btrfs_file_extent_num_bytes(leaf,
3664 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003665 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3666 /* take upper bound, may be compressed */
3667 datal = btrfs_file_extent_ram_bytes(leaf,
3668 extent);
3669 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003670
Filipe Manana2c463822014-05-31 02:31:05 +01003671 /*
3672 * The first search might have left us at an extent
3673 * item that ends before our target range's start, can
3674 * happen if we have holes and NO_HOLES feature enabled.
3675 */
3676 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003677 path->slots[0]++;
3678 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003679 } else if (key.offset >= off + len) {
3680 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003681 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003682 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003683 size = btrfs_item_size_nr(leaf, slot);
3684 read_extent_buffer(leaf, buf,
3685 btrfs_item_ptr_offset(leaf, slot),
3686 size);
3687
3688 btrfs_release_path(path);
3689 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003690
Zheng Yan31840ae2008-09-23 13:14:14 -04003691 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003692 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003693 if (off <= key.offset)
3694 new_key.offset = key.offset + destoff - off;
3695 else
3696 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003697
Sage Weilb6f34092011-09-20 14:48:51 -04003698 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003699 * Deal with a hole that doesn't have an extent item
3700 * that represents it (NO_HOLES feature enabled).
3701 * This hole is either in the middle of the cloning
3702 * range or at the beginning (fully overlaps it or
3703 * partially overlaps it).
3704 */
3705 if (new_key.offset != last_dest_end)
3706 drop_start = last_dest_end;
3707 else
3708 drop_start = new_key.offset;
3709
3710 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003711 * 1 - adjusting old extent (we may have to split it)
3712 * 1 - add new extent
3713 * 1 - inode update
3714 */
3715 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003716 if (IS_ERR(trans)) {
3717 ret = PTR_ERR(trans);
3718 goto out;
3719 }
3720
Chris Masonc8a894d2009-06-27 21:07:03 -04003721 if (type == BTRFS_FILE_EXTENT_REG ||
3722 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003723 /*
3724 * a | --- range to clone ---| b
3725 * | ------------- extent ------------- |
3726 */
3727
Antonio Ospite93915582014-06-04 14:03:48 +02003728 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003729 if (key.offset + datal > off + len)
3730 datal = off + len - key.offset;
3731
Antonio Ospite93915582014-06-04 14:03:48 +02003732 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003733 if (off > key.offset) {
3734 datao += off - key.offset;
3735 datal -= off - key.offset;
3736 }
3737
Josef Bacik5dc562c2012-08-17 13:14:17 -04003738 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003739 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003740 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003741 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003742 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003743 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003744 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003745 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003746 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003747 goto out;
3748 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003749
Sage Weilc5c9cd42008-11-12 14:32:25 -05003750 ret = btrfs_insert_empty_item(trans, root, path,
3751 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003752 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003753 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003754 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003755 goto out;
3756 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003757
3758 leaf = path->nodes[0];
3759 slot = path->slots[0];
3760 write_extent_buffer(leaf, buf,
3761 btrfs_item_ptr_offset(leaf, slot),
3762 size);
3763
3764 extent = btrfs_item_ptr(leaf, slot,
3765 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003766
Sage Weilc5c9cd42008-11-12 14:32:25 -05003767 /* disko == 0 means it's a hole */
3768 if (!disko)
3769 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003770
3771 btrfs_set_file_extent_offset(leaf, extent,
3772 datao);
3773 btrfs_set_file_extent_num_bytes(leaf, extent,
3774 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003775
Sage Weilc5c9cd42008-11-12 14:32:25 -05003776 if (disko) {
3777 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003778 ret = btrfs_inc_extent_ref(trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003779 root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003780 disko, diskl, 0,
3781 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003782 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003783 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003784 if (ret) {
3785 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003786 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003787 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003788 goto out;
3789
3790 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003791 }
3792 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3793 u64 skip = 0;
3794 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003795
Sage Weilc5c9cd42008-11-12 14:32:25 -05003796 if (off > key.offset) {
3797 skip = off - key.offset;
3798 new_key.offset += skip;
3799 }
Chris Masond3977122009-01-05 21:25:51 -05003800
Liu Boaa42ffd2012-09-18 03:52:23 -06003801 if (key.offset + datal > off + len)
3802 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003803
Sage Weilc5c9cd42008-11-12 14:32:25 -05003804 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003805 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003806 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003807 goto out;
3808 }
3809 size -= skip + trim;
3810 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003811
David Sterba4a0ab9d2017-02-10 20:18:49 +01003812 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003813 trans, path,
3814 &new_key,
3815 drop_start,
3816 datal,
3817 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003818 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003819 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003820 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003821 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003822 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003823 goto out;
3824 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003825 leaf = path->nodes[0];
3826 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003827 }
3828
Filipe Manana7ffbb592014-06-09 03:48:05 +01003829 /* If we have an implicit hole (NO_HOLES feature). */
3830 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003831 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003832 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003833 new_key.offset - drop_start);
3834
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003835 clone_update_extent_map(BTRFS_I(inode), trans,
3836 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003837
Sage Weilc5c9cd42008-11-12 14:32:25 -05003838 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003839 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003840
Filipe Manana62e23902014-08-08 02:47:06 +01003841 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003842 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003843 ret = clone_finish_inode_update(trans, inode,
3844 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003845 destoff, olen,
3846 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003847 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003848 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003849 if (new_key.offset + datal >= destoff + len)
3850 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003851 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003852 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003853 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003854
3855 if (fatal_signal_pending(current)) {
3856 ret = -EINTR;
3857 goto out;
3858 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003859 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003860 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003861
Filipe Mananaf82a9902014-06-01 01:50:28 +01003862 if (last_dest_end < destoff + len) {
3863 /*
3864 * We have an implicit hole (NO_HOLES feature is enabled) that
3865 * fully or partially overlaps our cloning range at its end.
3866 */
3867 btrfs_release_path(path);
3868
3869 /*
3870 * 1 - remove extent(s)
3871 * 1 - inode update
3872 */
3873 trans = btrfs_start_transaction(root, 2);
3874 if (IS_ERR(trans)) {
3875 ret = PTR_ERR(trans);
3876 goto out;
3877 }
3878 ret = btrfs_drop_extents(trans, root, inode,
3879 last_dest_end, destoff + len, 1);
3880 if (ret) {
3881 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003882 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003883 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003884 goto out;
3885 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003886 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3887 last_dest_end,
3888 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003889 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003890 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003891 }
3892
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003893out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003894 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003895 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003896 return ret;
3897}
3898
Zach Brown3db11b22015-11-10 16:53:32 -05003899static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3900 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003901{
Al Viro54563d42013-09-01 15:57:51 -04003902 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003903 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003904 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003905 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003906 int ret;
3907 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003908 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003909 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003910
3911 /*
3912 * TODO:
3913 * - split compressed inline extents. annoying: we need to
3914 * decompress into destination's address_space (the file offset
3915 * may change, so source mapping won't do), then recompress (or
3916 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003917 *
3918 * - split destination inode's inline extents. The inline extents can
3919 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003920 */
3921
Mark Fasheh32b7c682013-08-06 11:42:49 -07003922 if (btrfs_root_readonly(root))
3923 return -EROFS;
3924
Zach Brown3db11b22015-11-10 16:53:32 -05003925 if (file_src->f_path.mnt != file->f_path.mnt ||
3926 src->i_sb != inode->i_sb)
3927 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003928
Mark Fasheh32b7c682013-08-06 11:42:49 -07003929 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003930 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003931
3932 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003933 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003934 } else {
Al Viro59551022016-01-22 15:40:57 -05003935 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003936 }
3937
Omar Sandovalb5c40d52018-05-22 15:02:12 -07003938 /* don't make the dst file partly checksummed */
3939 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3940 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
3941 ret = -EINVAL;
3942 goto out_unlock;
3943 }
3944
Mark Fasheh32b7c682013-08-06 11:42:49 -07003945 /* determine range to clone */
3946 ret = -EINVAL;
3947 if (off + len > src->i_size || off + len < off)
3948 goto out_unlock;
3949 if (len == 0)
3950 olen = len = src->i_size - off;
3951 /* if we extend to eof, continue to block boundary */
3952 if (off + len == src->i_size)
3953 len = ALIGN(src->i_size, bs) - off;
3954
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003955 if (len == 0) {
3956 ret = 0;
3957 goto out_unlock;
3958 }
3959
Mark Fasheh32b7c682013-08-06 11:42:49 -07003960 /* verify the end result is block aligned */
3961 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3962 !IS_ALIGNED(destoff, bs))
3963 goto out_unlock;
3964
3965 /* verify if ranges are overlapped within the same file */
3966 if (same_inode) {
3967 if (destoff + len > off && destoff < off + len)
3968 goto out_unlock;
3969 }
3970
3971 if (destoff > inode->i_size) {
3972 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3973 if (ret)
3974 goto out_unlock;
3975 }
3976
Filipe Mananac125b8b2014-05-23 05:03:34 +01003977 /*
3978 * Lock the target range too. Right after we replace the file extent
3979 * items in the fs tree (which now point to the cloned data), we might
3980 * have a worker replace them with extent items relative to a write
3981 * operation that was issued before this clone operation (i.e. confront
3982 * with inode.c:btrfs_finish_ordered_io).
3983 */
3984 if (same_inode) {
3985 u64 lock_start = min_t(u64, off, destoff);
3986 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003987
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003988 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003989 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003990 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3991 true);
3992 }
3993 ASSERT(ret == 0);
3994 if (WARN_ON(ret)) {
3995 /* ranges in the io trees already unlocked */
3996 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003997 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003998
Mark Fasheh1c919a52015-06-30 14:42:08 -07003999 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004000
Filipe Mananac125b8b2014-05-23 05:03:34 +01004001 if (same_inode) {
4002 u64 lock_start = min_t(u64, off, destoff);
4003 u64 lock_end = max_t(u64, off, destoff) + len - 1;
4004
4005 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
4006 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07004007 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01004008 }
4009 /*
4010 * Truncate page cache pages so that future reads will see the cloned
4011 * data immediately and not the previous data.
4012 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05304013 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004014 round_down(destoff, PAGE_SIZE),
4015 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004016out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07004017 if (!same_inode)
4018 btrfs_double_inode_unlock(src, inode);
4019 else
Al Viro59551022016-01-22 15:40:57 -05004020 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05004021 return ret;
4022}
4023
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004024int btrfs_clone_file_range(struct file *src_file, loff_t off,
4025 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05004026{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004027 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004028}
4029
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004030static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4031{
Al Viro496ad9a2013-01-23 17:07:38 -05004032 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004033 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004034 struct btrfs_root *root = BTRFS_I(inode)->root;
4035 struct btrfs_root *new_root;
4036 struct btrfs_dir_item *di;
4037 struct btrfs_trans_handle *trans;
4038 struct btrfs_path *path;
4039 struct btrfs_key location;
4040 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004041 u64 objectid = 0;
4042 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004043 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004044
4045 if (!capable(CAP_SYS_ADMIN))
4046 return -EPERM;
4047
Miao Xie3c04ce02012-11-26 08:43:07 +00004048 ret = mnt_want_write_file(file);
4049 if (ret)
4050 return ret;
4051
4052 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4053 ret = -EFAULT;
4054 goto out;
4055 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004056
4057 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304058 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004059
4060 location.objectid = objectid;
4061 location.type = BTRFS_ROOT_ITEM_KEY;
4062 location.offset = (u64)-1;
4063
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004064 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004065 if (IS_ERR(new_root)) {
4066 ret = PTR_ERR(new_root);
4067 goto out;
4068 }
satoru takeuchi6d6d2822017-09-12 22:42:52 +09004069 if (!is_fstree(new_root->objectid)) {
4070 ret = -ENOENT;
4071 goto out;
4072 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004073
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004074 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004075 if (!path) {
4076 ret = -ENOMEM;
4077 goto out;
4078 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004079 path->leave_spinning = 1;
4080
4081 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004082 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004083 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004084 ret = PTR_ERR(trans);
4085 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004086 }
4087
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004088 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4089 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004090 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004091 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004092 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004093 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004094 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004095 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004096 ret = -ENOENT;
4097 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004098 }
4099
4100 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4101 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4102 btrfs_mark_buffer_dirty(path->nodes[0]);
4103 btrfs_free_path(path);
4104
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004105 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004106 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004107out:
4108 mnt_drop_write_file(file);
4109 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004110}
4111
Su Yuec065f5b12018-04-02 17:24:11 +08004112static void get_block_group_info(struct list_head *groups_list,
4113 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004114{
4115 struct btrfs_block_group_cache *block_group;
4116
4117 space->total_bytes = 0;
4118 space->used_bytes = 0;
4119 space->flags = 0;
4120 list_for_each_entry(block_group, groups_list, list) {
4121 space->flags = block_group->flags;
4122 space->total_bytes += block_group->key.offset;
4123 space->used_bytes +=
4124 btrfs_block_group_used(&block_group->item);
4125 }
4126}
4127
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004128static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4129 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004130{
4131 struct btrfs_ioctl_space_args space_args;
4132 struct btrfs_ioctl_space_info space;
4133 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004134 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004135 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004136 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004137 static const u64 types[] = {
4138 BTRFS_BLOCK_GROUP_DATA,
4139 BTRFS_BLOCK_GROUP_SYSTEM,
4140 BTRFS_BLOCK_GROUP_METADATA,
4141 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4142 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004143 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004144 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004145 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004146 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004147 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004148
4149 if (copy_from_user(&space_args,
4150 (struct btrfs_ioctl_space_args __user *)arg,
4151 sizeof(space_args)))
4152 return -EFAULT;
4153
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004154 for (i = 0; i < num_types; i++) {
4155 struct btrfs_space_info *tmp;
4156
4157 info = NULL;
4158 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004159 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004160 list) {
4161 if (tmp->flags == types[i]) {
4162 info = tmp;
4163 break;
4164 }
4165 }
4166 rcu_read_unlock();
4167
4168 if (!info)
4169 continue;
4170
4171 down_read(&info->groups_sem);
4172 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4173 if (!list_empty(&info->block_groups[c]))
4174 slot_count++;
4175 }
4176 up_read(&info->groups_sem);
4177 }
Josef Bacik1406e432010-01-13 18:19:06 +00004178
David Sterba36523e952014-02-07 14:34:12 +01004179 /*
4180 * Global block reserve, exported as a space_info
4181 */
4182 slot_count++;
4183
Chris Mason7fde62b2010-03-16 15:40:10 -04004184 /* space_slots == 0 means they are asking for a count */
4185 if (space_args.space_slots == 0) {
4186 space_args.total_spaces = slot_count;
4187 goto out;
4188 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004189
Dan Rosenberg51788b12011-02-14 16:04:23 -05004190 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004191
Chris Mason7fde62b2010-03-16 15:40:10 -04004192 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004193
Chris Mason7fde62b2010-03-16 15:40:10 -04004194 /* we generally have at most 6 or so space infos, one for each raid
4195 * level. So, a whole page should be more than enough for everyone
4196 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004197 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004198 return -ENOMEM;
4199
4200 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004201 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004202 if (!dest)
4203 return -ENOMEM;
4204 dest_orig = dest;
4205
4206 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004207 for (i = 0; i < num_types; i++) {
4208 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004209
Dan Rosenberg51788b12011-02-14 16:04:23 -05004210 if (!slot_count)
4211 break;
4212
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004213 info = NULL;
4214 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004215 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004216 list) {
4217 if (tmp->flags == types[i]) {
4218 info = tmp;
4219 break;
4220 }
4221 }
4222 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004223
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004224 if (!info)
4225 continue;
4226 down_read(&info->groups_sem);
4227 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4228 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004229 get_block_group_info(&info->block_groups[c],
4230 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004231 memcpy(dest, &space, sizeof(space));
4232 dest++;
4233 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004234 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004235 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004236 if (!slot_count)
4237 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004238 }
4239 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004240 }
Josef Bacik1406e432010-01-13 18:19:06 +00004241
David Sterba36523e952014-02-07 14:34:12 +01004242 /*
4243 * Add global block reserve
4244 */
4245 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004246 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004247
4248 spin_lock(&block_rsv->lock);
4249 space.total_bytes = block_rsv->size;
4250 space.used_bytes = block_rsv->size - block_rsv->reserved;
4251 spin_unlock(&block_rsv->lock);
4252 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4253 memcpy(dest, &space, sizeof(space));
4254 space_args.total_spaces++;
4255 }
4256
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004257 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004258 (arg + sizeof(struct btrfs_ioctl_space_args));
4259
4260 if (copy_to_user(user_dest, dest_orig, alloc_size))
4261 ret = -EFAULT;
4262
4263 kfree(dest_orig);
4264out:
4265 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004266 ret = -EFAULT;
4267
4268 return ret;
4269}
4270
Miao Xie9a8c28b2012-11-26 08:40:43 +00004271static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4272 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004273{
Sage Weil46204592010-10-29 15:41:32 -04004274 struct btrfs_trans_handle *trans;
4275 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004276 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004277
Miao Xied4edf392013-02-20 09:17:06 +00004278 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004279 if (IS_ERR(trans)) {
4280 if (PTR_ERR(trans) != -ENOENT)
4281 return PTR_ERR(trans);
4282
4283 /* No running transaction, don't bother */
4284 transid = root->fs_info->last_trans_committed;
4285 goto out;
4286 }
Sage Weil46204592010-10-29 15:41:32 -04004287 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004288 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004289 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004290 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004291 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004292 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004293out:
Sage Weil46204592010-10-29 15:41:32 -04004294 if (argp)
4295 if (copy_to_user(argp, &transid, sizeof(transid)))
4296 return -EFAULT;
4297 return 0;
4298}
4299
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004300static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004301 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004302{
Sage Weil46204592010-10-29 15:41:32 -04004303 u64 transid;
4304
4305 if (argp) {
4306 if (copy_from_user(&transid, argp, sizeof(transid)))
4307 return -EFAULT;
4308 } else {
4309 transid = 0; /* current trans */
4310 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004311 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004312}
4313
Miao Xieb8e95482012-11-26 08:48:01 +00004314static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004315{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004316 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004317 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004318 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004319
4320 if (!capable(CAP_SYS_ADMIN))
4321 return -EPERM;
4322
4323 sa = memdup_user(arg, sizeof(*sa));
4324 if (IS_ERR(sa))
4325 return PTR_ERR(sa);
4326
Miao Xieb8e95482012-11-26 08:48:01 +00004327 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4328 ret = mnt_want_write_file(file);
4329 if (ret)
4330 goto out;
4331 }
4332
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004333 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004334 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4335 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004336
4337 if (copy_to_user(arg, sa, sizeof(*sa)))
4338 ret = -EFAULT;
4339
Miao Xieb8e95482012-11-26 08:48:01 +00004340 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4341 mnt_drop_write_file(file);
4342out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004343 kfree(sa);
4344 return ret;
4345}
4346
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004347static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004348{
4349 if (!capable(CAP_SYS_ADMIN))
4350 return -EPERM;
4351
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004352 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004353}
4354
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004355static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004356 void __user *arg)
4357{
4358 struct btrfs_ioctl_scrub_args *sa;
4359 int ret;
4360
4361 if (!capable(CAP_SYS_ADMIN))
4362 return -EPERM;
4363
4364 sa = memdup_user(arg, sizeof(*sa));
4365 if (IS_ERR(sa))
4366 return PTR_ERR(sa);
4367
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004368 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004369
4370 if (copy_to_user(arg, sa, sizeof(*sa)))
4371 ret = -EFAULT;
4372
4373 kfree(sa);
4374 return ret;
4375}
4376
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004377static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004378 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004379{
4380 struct btrfs_ioctl_get_dev_stats *sa;
4381 int ret;
4382
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004383 sa = memdup_user(arg, sizeof(*sa));
4384 if (IS_ERR(sa))
4385 return PTR_ERR(sa);
4386
David Sterbab27f7c02012-06-22 06:30:39 -06004387 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4388 kfree(sa);
4389 return -EPERM;
4390 }
4391
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004392 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004393
4394 if (copy_to_user(arg, sa, sizeof(*sa)))
4395 ret = -EFAULT;
4396
4397 kfree(sa);
4398 return ret;
4399}
4400
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004401static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4402 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004403{
4404 struct btrfs_ioctl_dev_replace_args *p;
4405 int ret;
4406
4407 if (!capable(CAP_SYS_ADMIN))
4408 return -EPERM;
4409
4410 p = memdup_user(arg, sizeof(*p));
4411 if (IS_ERR(p))
4412 return PTR_ERR(p);
4413
4414 switch (p->cmd) {
4415 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004416 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004417 ret = -EROFS;
4418 goto out;
4419 }
David Sterba171938e2017-03-28 14:44:21 +02004420 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004421 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004422 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004423 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004424 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004425 }
4426 break;
4427 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004428 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004429 ret = 0;
4430 break;
4431 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004432 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004433 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004434 break;
4435 default:
4436 ret = -EINVAL;
4437 break;
4438 }
4439
4440 if (copy_to_user(arg, p, sizeof(*p)))
4441 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004442out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004443 kfree(p);
4444 return ret;
4445}
4446
Jan Schmidtd7728c92011-07-07 16:48:38 +02004447static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4448{
4449 int ret = 0;
4450 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004451 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004452 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004453 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004454 struct inode_fs_paths *ipath = NULL;
4455 struct btrfs_path *path;
4456
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004457 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004458 return -EPERM;
4459
4460 path = btrfs_alloc_path();
4461 if (!path) {
4462 ret = -ENOMEM;
4463 goto out;
4464 }
4465
4466 ipa = memdup_user(arg, sizeof(*ipa));
4467 if (IS_ERR(ipa)) {
4468 ret = PTR_ERR(ipa);
4469 ipa = NULL;
4470 goto out;
4471 }
4472
4473 size = min_t(u32, ipa->size, 4096);
4474 ipath = init_ipath(size, root, path);
4475 if (IS_ERR(ipath)) {
4476 ret = PTR_ERR(ipath);
4477 ipath = NULL;
4478 goto out;
4479 }
4480
4481 ret = paths_from_inode(ipa->inum, ipath);
4482 if (ret < 0)
4483 goto out;
4484
4485 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004486 rel_ptr = ipath->fspath->val[i] -
4487 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004488 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004489 }
4490
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004491 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4492 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004493 if (ret) {
4494 ret = -EFAULT;
4495 goto out;
4496 }
4497
4498out:
4499 btrfs_free_path(path);
4500 free_ipath(ipath);
4501 kfree(ipa);
4502
4503 return ret;
4504}
4505
4506static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4507{
4508 struct btrfs_data_container *inodes = ctx;
4509 const size_t c = 3 * sizeof(u64);
4510
4511 if (inodes->bytes_left >= c) {
4512 inodes->bytes_left -= c;
4513 inodes->val[inodes->elem_cnt] = inum;
4514 inodes->val[inodes->elem_cnt + 1] = offset;
4515 inodes->val[inodes->elem_cnt + 2] = root;
4516 inodes->elem_cnt += 3;
4517 } else {
4518 inodes->bytes_missing += c - inodes->bytes_left;
4519 inodes->bytes_left = 0;
4520 inodes->elem_missed += 3;
4521 }
4522
4523 return 0;
4524}
4525
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004526static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004527 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004528{
4529 int ret = 0;
4530 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004531 struct btrfs_ioctl_logical_ino_args *loi;
4532 struct btrfs_data_container *inodes = NULL;
4533 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004534 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004535
4536 if (!capable(CAP_SYS_ADMIN))
4537 return -EPERM;
4538
4539 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304540 if (IS_ERR(loi))
4541 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004542
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004543 if (version == 1) {
4544 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004545 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004546 } else {
4547 /* All reserved bits must be 0 for now */
4548 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4549 ret = -EINVAL;
4550 goto out_loi;
4551 }
4552 /* Only accept flags we have defined so far */
4553 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4554 ret = -EINVAL;
4555 goto out_loi;
4556 }
4557 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004558 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004559 }
4560
Jan Schmidtd7728c92011-07-07 16:48:38 +02004561 path = btrfs_alloc_path();
4562 if (!path) {
4563 ret = -ENOMEM;
4564 goto out;
4565 }
4566
Jan Schmidtd7728c92011-07-07 16:48:38 +02004567 inodes = init_data_container(size);
4568 if (IS_ERR(inodes)) {
4569 ret = PTR_ERR(inodes);
4570 inodes = NULL;
4571 goto out;
4572 }
4573
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004574 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004575 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004576 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004577 ret = -ENOENT;
4578 if (ret < 0)
4579 goto out;
4580
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004581 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4582 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004583 if (ret)
4584 ret = -EFAULT;
4585
4586out:
4587 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004588 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004589out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004590 kfree(loi);
4591
4592 return ret;
4593}
4594
David Sterba008ef092018-03-21 02:05:27 +01004595void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004596 struct btrfs_ioctl_balance_args *bargs)
4597{
4598 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4599
4600 bargs->flags = bctl->flags;
4601
David Sterba3009a622018-03-21 01:31:04 +01004602 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004603 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4604 if (atomic_read(&fs_info->balance_pause_req))
4605 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004606 if (atomic_read(&fs_info->balance_cancel_req))
4607 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004608
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004609 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4610 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4611 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004612
David Sterba008ef092018-03-21 02:05:27 +01004613 spin_lock(&fs_info->balance_lock);
4614 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4615 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004616}
4617
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004618static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004619{
Al Viro496ad9a2013-01-23 17:07:38 -05004620 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004621 struct btrfs_fs_info *fs_info = root->fs_info;
4622 struct btrfs_ioctl_balance_args *bargs;
4623 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004624 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004625 int ret;
4626
4627 if (!capable(CAP_SYS_ADMIN))
4628 return -EPERM;
4629
Liu Boe54bfa312012-06-29 03:58:48 -06004630 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004631 if (ret)
4632 return ret;
4633
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004634again:
David Sterba171938e2017-03-28 14:44:21 +02004635 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004636 mutex_lock(&fs_info->balance_mutex);
4637 need_unlock = true;
4638 goto locked;
4639 }
4640
4641 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004642 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004643 * (1) some other op is running
4644 * (2) balance is running
4645 * (3) balance is paused -- special case (think resume)
4646 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004647 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004648 if (fs_info->balance_ctl) {
4649 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004650 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004651 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004652 /*
4653 * Lock released to allow other waiters to continue,
4654 * we'll reexamine the status again.
4655 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004656 mutex_lock(&fs_info->balance_mutex);
4657
4658 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004659 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004660 /* this is (3) */
4661 need_unlock = false;
4662 goto locked;
4663 }
4664
4665 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004666 goto again;
4667 } else {
4668 /* this is (2) */
4669 mutex_unlock(&fs_info->balance_mutex);
4670 ret = -EINPROGRESS;
4671 goto out;
4672 }
4673 } else {
4674 /* this is (1) */
4675 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004676 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004677 goto out;
4678 }
4679
4680locked:
David Sterba171938e2017-03-28 14:44:21 +02004681 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004682
4683 if (arg) {
4684 bargs = memdup_user(arg, sizeof(*bargs));
4685 if (IS_ERR(bargs)) {
4686 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004687 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004688 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004689
4690 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4691 if (!fs_info->balance_ctl) {
4692 ret = -ENOTCONN;
4693 goto out_bargs;
4694 }
4695
4696 bctl = fs_info->balance_ctl;
4697 spin_lock(&fs_info->balance_lock);
4698 bctl->flags |= BTRFS_BALANCE_RESUME;
4699 spin_unlock(&fs_info->balance_lock);
4700
4701 goto do_balance;
4702 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004703 } else {
4704 bargs = NULL;
4705 }
4706
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004707 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004708 ret = -EINPROGRESS;
4709 goto out_bargs;
4710 }
4711
David Sterba8d2db782015-11-04 15:38:29 +01004712 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004713 if (!bctl) {
4714 ret = -ENOMEM;
4715 goto out_bargs;
4716 }
4717
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004718 if (arg) {
4719 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4720 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4721 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4722
4723 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004724 } else {
4725 /* balance everything - no filters */
4726 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004727 }
4728
David Sterba8eb93452015-10-12 16:55:54 +02004729 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4730 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004731 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004732 }
4733
Ilya Dryomovde322262012-01-16 22:04:49 +02004734do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004735 /*
David Sterba149196a2018-03-20 20:23:09 +01004736 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4737 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4738 * restriper was paused all the way until unmount, in free_fs_info.
4739 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004740 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004741 need_unlock = false;
4742
David Sterba6fcf6e22018-05-07 17:44:03 +02004743 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004744 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004745
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004746 if (arg) {
4747 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4748 ret = -EFAULT;
4749 }
4750
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004751out_bctl:
4752 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004753out_bargs:
4754 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004755out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004756 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004757 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004758 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004759out:
Liu Boe54bfa312012-06-29 03:58:48 -06004760 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004761 return ret;
4762}
4763
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004764static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004765{
4766 if (!capable(CAP_SYS_ADMIN))
4767 return -EPERM;
4768
4769 switch (cmd) {
4770 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004771 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004772 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004773 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004774 }
4775
4776 return -EINVAL;
4777}
4778
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004779static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004780 void __user *arg)
4781{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004782 struct btrfs_ioctl_balance_args *bargs;
4783 int ret = 0;
4784
4785 if (!capable(CAP_SYS_ADMIN))
4786 return -EPERM;
4787
4788 mutex_lock(&fs_info->balance_mutex);
4789 if (!fs_info->balance_ctl) {
4790 ret = -ENOTCONN;
4791 goto out;
4792 }
4793
David Sterba8d2db782015-11-04 15:38:29 +01004794 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004795 if (!bargs) {
4796 ret = -ENOMEM;
4797 goto out;
4798 }
4799
David Sterba008ef092018-03-21 02:05:27 +01004800 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004801
4802 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4803 ret = -EFAULT;
4804
4805 kfree(bargs);
4806out:
4807 mutex_unlock(&fs_info->balance_mutex);
4808 return ret;
4809}
4810
Miao Xie905b0dd2012-11-26 08:50:11 +00004811static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004812{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004813 struct inode *inode = file_inode(file);
4814 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004815 struct btrfs_ioctl_quota_ctl_args *sa;
4816 struct btrfs_trans_handle *trans = NULL;
4817 int ret;
4818 int err;
4819
4820 if (!capable(CAP_SYS_ADMIN))
4821 return -EPERM;
4822
Miao Xie905b0dd2012-11-26 08:50:11 +00004823 ret = mnt_want_write_file(file);
4824 if (ret)
4825 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004826
4827 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004828 if (IS_ERR(sa)) {
4829 ret = PTR_ERR(sa);
4830 goto drop_write;
4831 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004832
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004833 down_write(&fs_info->subvol_sem);
4834 trans = btrfs_start_transaction(fs_info->tree_root, 2);
Jan Schmidt2f232032013-04-25 16:04:51 +00004835 if (IS_ERR(trans)) {
4836 ret = PTR_ERR(trans);
4837 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004838 }
4839
4840 switch (sa->cmd) {
4841 case BTRFS_QUOTA_CTL_ENABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004842 ret = btrfs_quota_enable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004843 break;
4844 case BTRFS_QUOTA_CTL_DISABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004845 ret = btrfs_quota_disable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004846 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004847 default:
4848 ret = -EINVAL;
4849 break;
4850 }
4851
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004852 err = btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00004853 if (err && !ret)
4854 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004855out:
4856 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004857 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004858drop_write:
4859 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004860 return ret;
4861}
4862
Miao Xie905b0dd2012-11-26 08:50:11 +00004863static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004864{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004865 struct inode *inode = file_inode(file);
4866 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4867 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004868 struct btrfs_ioctl_qgroup_assign_args *sa;
4869 struct btrfs_trans_handle *trans;
4870 int ret;
4871 int err;
4872
4873 if (!capable(CAP_SYS_ADMIN))
4874 return -EPERM;
4875
Miao Xie905b0dd2012-11-26 08:50:11 +00004876 ret = mnt_want_write_file(file);
4877 if (ret)
4878 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004879
4880 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004881 if (IS_ERR(sa)) {
4882 ret = PTR_ERR(sa);
4883 goto drop_write;
4884 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004885
4886 trans = btrfs_join_transaction(root);
4887 if (IS_ERR(trans)) {
4888 ret = PTR_ERR(trans);
4889 goto out;
4890 }
4891
Arne Jansen5d13a372011-09-14 15:53:51 +02004892 if (sa->assign) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004893 ret = btrfs_add_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004894 sa->src, sa->dst);
4895 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004896 ret = btrfs_del_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004897 sa->src, sa->dst);
4898 }
4899
Qu Wenruoe082f562015-02-27 16:24:28 +08004900 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004901 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08004902 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004903 btrfs_handle_fs_error(fs_info, err,
4904 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004905 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004906 if (err && !ret)
4907 ret = err;
4908
4909out:
4910 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004911drop_write:
4912 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004913 return ret;
4914}
4915
Miao Xie905b0dd2012-11-26 08:50:11 +00004916static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004917{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004918 struct inode *inode = file_inode(file);
4919 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4920 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004921 struct btrfs_ioctl_qgroup_create_args *sa;
4922 struct btrfs_trans_handle *trans;
4923 int ret;
4924 int err;
4925
4926 if (!capable(CAP_SYS_ADMIN))
4927 return -EPERM;
4928
Miao Xie905b0dd2012-11-26 08:50:11 +00004929 ret = mnt_want_write_file(file);
4930 if (ret)
4931 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004932
4933 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004934 if (IS_ERR(sa)) {
4935 ret = PTR_ERR(sa);
4936 goto drop_write;
4937 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004938
Miao Xied86e56c2012-11-15 11:35:41 +00004939 if (!sa->qgroupid) {
4940 ret = -EINVAL;
4941 goto out;
4942 }
4943
Arne Jansen5d13a372011-09-14 15:53:51 +02004944 trans = btrfs_join_transaction(root);
4945 if (IS_ERR(trans)) {
4946 ret = PTR_ERR(trans);
4947 goto out;
4948 }
4949
Arne Jansen5d13a372011-09-14 15:53:51 +02004950 if (sa->create) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004951 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004952 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004953 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004954 }
4955
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004956 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004957 if (err && !ret)
4958 ret = err;
4959
4960out:
4961 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004962drop_write:
4963 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004964 return ret;
4965}
4966
Miao Xie905b0dd2012-11-26 08:50:11 +00004967static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004968{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004969 struct inode *inode = file_inode(file);
4970 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4971 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004972 struct btrfs_ioctl_qgroup_limit_args *sa;
4973 struct btrfs_trans_handle *trans;
4974 int ret;
4975 int err;
4976 u64 qgroupid;
4977
4978 if (!capable(CAP_SYS_ADMIN))
4979 return -EPERM;
4980
Miao Xie905b0dd2012-11-26 08:50:11 +00004981 ret = mnt_want_write_file(file);
4982 if (ret)
4983 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004984
4985 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004986 if (IS_ERR(sa)) {
4987 ret = PTR_ERR(sa);
4988 goto drop_write;
4989 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004990
4991 trans = btrfs_join_transaction(root);
4992 if (IS_ERR(trans)) {
4993 ret = PTR_ERR(trans);
4994 goto out;
4995 }
4996
4997 qgroupid = sa->qgroupid;
4998 if (!qgroupid) {
4999 /* take the current subvol as qgroup */
5000 qgroupid = root->root_key.objectid;
5001 }
5002
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005003 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02005004
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005005 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005006 if (err && !ret)
5007 ret = err;
5008
5009out:
5010 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005011drop_write:
5012 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005013 return ret;
5014}
5015
Jan Schmidt2f232032013-04-25 16:04:51 +00005016static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5017{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005018 struct inode *inode = file_inode(file);
5019 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005020 struct btrfs_ioctl_quota_rescan_args *qsa;
5021 int ret;
5022
5023 if (!capable(CAP_SYS_ADMIN))
5024 return -EPERM;
5025
5026 ret = mnt_want_write_file(file);
5027 if (ret)
5028 return ret;
5029
5030 qsa = memdup_user(arg, sizeof(*qsa));
5031 if (IS_ERR(qsa)) {
5032 ret = PTR_ERR(qsa);
5033 goto drop_write;
5034 }
5035
5036 if (qsa->flags) {
5037 ret = -EINVAL;
5038 goto out;
5039 }
5040
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005041 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005042
5043out:
5044 kfree(qsa);
5045drop_write:
5046 mnt_drop_write_file(file);
5047 return ret;
5048}
5049
5050static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5051{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005052 struct inode *inode = file_inode(file);
5053 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005054 struct btrfs_ioctl_quota_rescan_args *qsa;
5055 int ret = 0;
5056
5057 if (!capable(CAP_SYS_ADMIN))
5058 return -EPERM;
5059
David Sterba8d2db782015-11-04 15:38:29 +01005060 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005061 if (!qsa)
5062 return -ENOMEM;
5063
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005064 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005065 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005066 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005067 }
5068
5069 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5070 ret = -EFAULT;
5071
5072 kfree(qsa);
5073 return ret;
5074}
5075
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005076static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5077{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005078 struct inode *inode = file_inode(file);
5079 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005080
5081 if (!capable(CAP_SYS_ADMIN))
5082 return -EPERM;
5083
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005084 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005085}
5086
Hugo Millsabccd002014-01-30 20:17:00 +00005087static long _btrfs_ioctl_set_received_subvol(struct file *file,
5088 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005089{
Al Viro496ad9a2013-01-23 17:07:38 -05005090 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005091 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005092 struct btrfs_root *root = BTRFS_I(inode)->root;
5093 struct btrfs_root_item *root_item = &root->root_item;
5094 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005095 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005096 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005097 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005098
David Sterbabd60ea02014-01-16 15:50:22 +01005099 if (!inode_owner_or_capable(inode))
5100 return -EPERM;
5101
Alexander Block8ea05e32012-07-25 17:35:53 +02005102 ret = mnt_want_write_file(file);
5103 if (ret < 0)
5104 return ret;
5105
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005106 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005107
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005108 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005109 ret = -EINVAL;
5110 goto out;
5111 }
5112
5113 if (btrfs_root_readonly(root)) {
5114 ret = -EROFS;
5115 goto out;
5116 }
5117
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005118 /*
5119 * 1 - root item
5120 * 2 - uuid items (received uuid + subvol uuid)
5121 */
5122 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005123 if (IS_ERR(trans)) {
5124 ret = PTR_ERR(trans);
5125 trans = NULL;
5126 goto out;
5127 }
5128
5129 sa->rtransid = trans->transid;
5130 sa->rtime.sec = ct.tv_sec;
5131 sa->rtime.nsec = ct.tv_nsec;
5132
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005133 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5134 BTRFS_UUID_SIZE);
5135 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005136 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005137 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005138 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5139 root->root_key.objectid);
5140 if (ret && ret != -ENOENT) {
5141 btrfs_abort_transaction(trans, ret);
5142 btrfs_end_transaction(trans);
5143 goto out;
5144 }
5145 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005146 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5147 btrfs_set_root_stransid(root_item, sa->stransid);
5148 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005149 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5150 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5151 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5152 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005153
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005154 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005155 &root->root_key, &root->root_item);
5156 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005157 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005158 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005159 }
5160 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005161 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005162 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5163 root->root_key.objectid);
5164 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005165 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005166 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005167 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005168 }
5169 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005170 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005171out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005172 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005173 mnt_drop_write_file(file);
5174 return ret;
5175}
5176
5177#ifdef CONFIG_64BIT
5178static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5179 void __user *arg)
5180{
5181 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5182 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5183 int ret = 0;
5184
5185 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305186 if (IS_ERR(args32))
5187 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005188
David Sterba8d2db782015-11-04 15:38:29 +01005189 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005190 if (!args64) {
5191 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005192 goto out;
5193 }
5194
5195 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5196 args64->stransid = args32->stransid;
5197 args64->rtransid = args32->rtransid;
5198 args64->stime.sec = args32->stime.sec;
5199 args64->stime.nsec = args32->stime.nsec;
5200 args64->rtime.sec = args32->rtime.sec;
5201 args64->rtime.nsec = args32->rtime.nsec;
5202 args64->flags = args32->flags;
5203
5204 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5205 if (ret)
5206 goto out;
5207
5208 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5209 args32->stransid = args64->stransid;
5210 args32->rtransid = args64->rtransid;
5211 args32->stime.sec = args64->stime.sec;
5212 args32->stime.nsec = args64->stime.nsec;
5213 args32->rtime.sec = args64->rtime.sec;
5214 args32->rtime.nsec = args64->rtime.nsec;
5215 args32->flags = args64->flags;
5216
5217 ret = copy_to_user(arg, args32, sizeof(*args32));
5218 if (ret)
5219 ret = -EFAULT;
5220
5221out:
5222 kfree(args32);
5223 kfree(args64);
5224 return ret;
5225}
5226#endif
5227
5228static long btrfs_ioctl_set_received_subvol(struct file *file,
5229 void __user *arg)
5230{
5231 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5232 int ret = 0;
5233
5234 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305235 if (IS_ERR(sa))
5236 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005237
5238 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5239
5240 if (ret)
5241 goto out;
5242
Alexander Block8ea05e32012-07-25 17:35:53 +02005243 ret = copy_to_user(arg, sa, sizeof(*sa));
5244 if (ret)
5245 ret = -EFAULT;
5246
5247out:
5248 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005249 return ret;
5250}
5251
jeff.liu867ab662013-01-05 02:48:01 +00005252static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5253{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005254 struct inode *inode = file_inode(file);
5255 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005256 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005257 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005258 char label[BTRFS_LABEL_SIZE];
5259
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005260 spin_lock(&fs_info->super_lock);
5261 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5262 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005263
5264 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005265
5266 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005267 btrfs_warn(fs_info,
5268 "label is too long, return the first %zu bytes",
5269 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005270 }
5271
jeff.liu867ab662013-01-05 02:48:01 +00005272 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005273
5274 return ret ? -EFAULT : 0;
5275}
5276
jeff.liua8bfd4a2013-01-05 02:48:08 +00005277static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5278{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005279 struct inode *inode = file_inode(file);
5280 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5281 struct btrfs_root *root = BTRFS_I(inode)->root;
5282 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005283 struct btrfs_trans_handle *trans;
5284 char label[BTRFS_LABEL_SIZE];
5285 int ret;
5286
5287 if (!capable(CAP_SYS_ADMIN))
5288 return -EPERM;
5289
5290 if (copy_from_user(label, arg, sizeof(label)))
5291 return -EFAULT;
5292
5293 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005294 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005295 "unable to set label with more than %d bytes",
5296 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005297 return -EINVAL;
5298 }
5299
5300 ret = mnt_want_write_file(file);
5301 if (ret)
5302 return ret;
5303
jeff.liua8bfd4a2013-01-05 02:48:08 +00005304 trans = btrfs_start_transaction(root, 0);
5305 if (IS_ERR(trans)) {
5306 ret = PTR_ERR(trans);
5307 goto out_unlock;
5308 }
5309
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005310 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005311 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005312 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005313 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005314
5315out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005316 mnt_drop_write_file(file);
5317 return ret;
5318}
5319
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005320#define INIT_FEATURE_FLAGS(suffix) \
5321 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5322 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5323 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5324
David Sterbad5131b62016-02-17 15:26:27 +01005325int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005326{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005327 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005328 INIT_FEATURE_FLAGS(SUPP),
5329 INIT_FEATURE_FLAGS(SAFE_SET),
5330 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5331 };
5332
5333 if (copy_to_user(arg, &features, sizeof(features)))
5334 return -EFAULT;
5335
5336 return 0;
5337}
5338
5339static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5340{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005341 struct inode *inode = file_inode(file);
5342 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5343 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005344 struct btrfs_ioctl_feature_flags features;
5345
5346 features.compat_flags = btrfs_super_compat_flags(super_block);
5347 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5348 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5349
5350 if (copy_to_user(arg, &features, sizeof(features)))
5351 return -EFAULT;
5352
5353 return 0;
5354}
5355
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005356static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005357 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005358 u64 change_mask, u64 flags, u64 supported_flags,
5359 u64 safe_set, u64 safe_clear)
5360{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005361 const char *type = btrfs_feature_set_names[set];
5362 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005363 u64 disallowed, unsupported;
5364 u64 set_mask = flags & change_mask;
5365 u64 clear_mask = ~flags & change_mask;
5366
5367 unsupported = set_mask & ~supported_flags;
5368 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005369 names = btrfs_printable_features(set, unsupported);
5370 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005371 btrfs_warn(fs_info,
5372 "this kernel does not support the %s feature bit%s",
5373 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005374 kfree(names);
5375 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005376 btrfs_warn(fs_info,
5377 "this kernel does not support %s bits 0x%llx",
5378 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005379 return -EOPNOTSUPP;
5380 }
5381
5382 disallowed = set_mask & ~safe_set;
5383 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005384 names = btrfs_printable_features(set, disallowed);
5385 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005386 btrfs_warn(fs_info,
5387 "can't set the %s feature bit%s while mounted",
5388 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005389 kfree(names);
5390 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005391 btrfs_warn(fs_info,
5392 "can't set %s bits 0x%llx while mounted",
5393 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005394 return -EPERM;
5395 }
5396
5397 disallowed = clear_mask & ~safe_clear;
5398 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005399 names = btrfs_printable_features(set, disallowed);
5400 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005401 btrfs_warn(fs_info,
5402 "can't clear the %s feature bit%s while mounted",
5403 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005404 kfree(names);
5405 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005406 btrfs_warn(fs_info,
5407 "can't clear %s bits 0x%llx while mounted",
5408 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005409 return -EPERM;
5410 }
5411
5412 return 0;
5413}
5414
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005415#define check_feature(fs_info, change_mask, flags, mask_base) \
5416check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005417 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5418 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5419 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5420
5421static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5422{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005423 struct inode *inode = file_inode(file);
5424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5425 struct btrfs_root *root = BTRFS_I(inode)->root;
5426 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005427 struct btrfs_ioctl_feature_flags flags[2];
5428 struct btrfs_trans_handle *trans;
5429 u64 newflags;
5430 int ret;
5431
5432 if (!capable(CAP_SYS_ADMIN))
5433 return -EPERM;
5434
5435 if (copy_from_user(flags, arg, sizeof(flags)))
5436 return -EFAULT;
5437
5438 /* Nothing to do */
5439 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5440 !flags[0].incompat_flags)
5441 return 0;
5442
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005443 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005444 flags[1].compat_flags, COMPAT);
5445 if (ret)
5446 return ret;
5447
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005448 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005449 flags[1].compat_ro_flags, COMPAT_RO);
5450 if (ret)
5451 return ret;
5452
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005453 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005454 flags[1].incompat_flags, INCOMPAT);
5455 if (ret)
5456 return ret;
5457
David Sterba7ab19622016-05-04 11:32:00 +02005458 ret = mnt_want_write_file(file);
5459 if (ret)
5460 return ret;
5461
David Sterba8051aa12014-02-07 14:34:04 +01005462 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005463 if (IS_ERR(trans)) {
5464 ret = PTR_ERR(trans);
5465 goto out_drop_write;
5466 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005467
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005468 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005469 newflags = btrfs_super_compat_flags(super_block);
5470 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5471 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5472 btrfs_set_super_compat_flags(super_block, newflags);
5473
5474 newflags = btrfs_super_compat_ro_flags(super_block);
5475 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5476 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5477 btrfs_set_super_compat_ro_flags(super_block, newflags);
5478
5479 newflags = btrfs_super_incompat_flags(super_block);
5480 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5481 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5482 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005483 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005484
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005485 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005486out_drop_write:
5487 mnt_drop_write_file(file);
5488
5489 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005490}
5491
Josef Bacik2351f432017-09-27 10:43:13 -04005492static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5493{
5494 struct btrfs_ioctl_send_args *arg;
5495 int ret;
5496
5497 if (compat) {
5498#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5499 struct btrfs_ioctl_send_args_32 args32;
5500
5501 ret = copy_from_user(&args32, argp, sizeof(args32));
5502 if (ret)
5503 return -EFAULT;
5504 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5505 if (!arg)
5506 return -ENOMEM;
5507 arg->send_fd = args32.send_fd;
5508 arg->clone_sources_count = args32.clone_sources_count;
5509 arg->clone_sources = compat_ptr(args32.clone_sources);
5510 arg->parent_root = args32.parent_root;
5511 arg->flags = args32.flags;
5512 memcpy(arg->reserved, args32.reserved,
5513 sizeof(args32.reserved));
5514#else
5515 return -ENOTTY;
5516#endif
5517 } else {
5518 arg = memdup_user(argp, sizeof(*arg));
5519 if (IS_ERR(arg))
5520 return PTR_ERR(arg);
5521 }
5522 ret = btrfs_ioctl_send(file, arg);
5523 kfree(arg);
5524 return ret;
5525}
5526
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005527long btrfs_ioctl(struct file *file, unsigned int
5528 cmd, unsigned long arg)
5529{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005530 struct inode *inode = file_inode(file);
5531 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5532 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005533 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005534
5535 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005536 case FS_IOC_GETFLAGS:
5537 return btrfs_ioctl_getflags(file, argp);
5538 case FS_IOC_SETFLAGS:
5539 return btrfs_ioctl_setflags(file, argp);
5540 case FS_IOC_GETVERSION:
5541 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005542 case FITRIM:
5543 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005544 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005545 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005546 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005547 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005548 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005549 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005550 case BTRFS_IOC_SUBVOL_CREATE_V2:
5551 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005552 case BTRFS_IOC_SNAP_DESTROY:
5553 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005554 case BTRFS_IOC_SUBVOL_GETFLAGS:
5555 return btrfs_ioctl_subvol_getflags(file, argp);
5556 case BTRFS_IOC_SUBVOL_SETFLAGS:
5557 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005558 case BTRFS_IOC_DEFAULT_SUBVOL:
5559 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005560 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005561 return btrfs_ioctl_defrag(file, NULL);
5562 case BTRFS_IOC_DEFRAG_RANGE:
5563 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005564 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005565 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005566 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005567 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005568 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005569 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005570 case BTRFS_IOC_RM_DEV_V2:
5571 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005572 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005573 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005574 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005575 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005576 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005577 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005578 case BTRFS_IOC_TREE_SEARCH:
5579 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005580 case BTRFS_IOC_TREE_SEARCH_V2:
5581 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005582 case BTRFS_IOC_INO_LOOKUP:
5583 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005584 case BTRFS_IOC_INO_PATHS:
5585 return btrfs_ioctl_ino_to_path(root, argp);
5586 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005587 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5588 case BTRFS_IOC_LOGICAL_INO_V2:
5589 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005590 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005591 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005592 case BTRFS_IOC_SYNC: {
5593 int ret;
5594
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005595 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005596 if (ret)
5597 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005598 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005599 /*
5600 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005601 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005602 * processing uncleaned subvols.
5603 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005604 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005605 return ret;
5606 }
Sage Weil46204592010-10-29 15:41:32 -04005607 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005608 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005609 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005610 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005611 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005612 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005613 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005614 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005615 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005616 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005617 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005618 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005619 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005620 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005621 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005622 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005623 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5624 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005625#ifdef CONFIG_64BIT
5626 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5627 return btrfs_ioctl_set_received_subvol_32(file, argp);
5628#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005629 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005630 return _btrfs_ioctl_send(file, argp, false);
5631#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5632 case BTRFS_IOC_SEND_32:
5633 return _btrfs_ioctl_send(file, argp, true);
5634#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005635 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005636 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005637 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005638 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005639 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005640 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005641 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005642 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005643 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005644 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005645 case BTRFS_IOC_QUOTA_RESCAN:
5646 return btrfs_ioctl_quota_rescan(file, argp);
5647 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5648 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005649 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5650 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005651 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005652 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005653 case BTRFS_IOC_GET_FSLABEL:
5654 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005655 case BTRFS_IOC_SET_FSLABEL:
5656 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005657 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005658 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005659 case BTRFS_IOC_GET_FEATURES:
5660 return btrfs_ioctl_get_features(file, argp);
5661 case BTRFS_IOC_SET_FEATURES:
5662 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005663 case FS_IOC_FSGETXATTR:
5664 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005665 case FS_IOC_FSSETXATTR:
5666 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005667 case BTRFS_IOC_GET_SUBVOL_INFO:
5668 return btrfs_ioctl_get_subvol_info(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005669 }
5670
5671 return -ENOTTY;
5672}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005673
5674#ifdef CONFIG_COMPAT
5675long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5676{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005677 /*
5678 * These all access 32-bit values anyway so no further
5679 * handling is necessary.
5680 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005681 switch (cmd) {
5682 case FS_IOC32_GETFLAGS:
5683 cmd = FS_IOC_GETFLAGS;
5684 break;
5685 case FS_IOC32_SETFLAGS:
5686 cmd = FS_IOC_SETFLAGS;
5687 break;
5688 case FS_IOC32_GETVERSION:
5689 cmd = FS_IOC_GETVERSION;
5690 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005691 }
5692
5693 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5694}
5695#endif