blob: be9b3f39183cb976e2fb14544888b57a77c8d40e [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
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002505/*
2506 * Return ROOT_REF information of the subvolume containing this inode
2507 * except the subvolume name.
2508 */
2509static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2510{
2511 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2512 struct btrfs_root_ref *rref;
2513 struct btrfs_root *root;
2514 struct btrfs_path *path;
2515 struct btrfs_key key;
2516 struct extent_buffer *leaf;
2517 struct inode *inode;
2518 u64 objectid;
2519 int slot;
2520 int ret;
2521 u8 found;
2522
2523 path = btrfs_alloc_path();
2524 if (!path)
2525 return -ENOMEM;
2526
2527 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2528 if (IS_ERR(rootrefs)) {
2529 btrfs_free_path(path);
2530 return PTR_ERR(rootrefs);
2531 }
2532
2533 inode = file_inode(file);
2534 root = BTRFS_I(inode)->root->fs_info->tree_root;
2535 objectid = BTRFS_I(inode)->root->root_key.objectid;
2536
2537 key.objectid = objectid;
2538 key.type = BTRFS_ROOT_REF_KEY;
2539 key.offset = rootrefs->min_treeid;
2540 found = 0;
2541
2542 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2543 if (ret < 0) {
2544 goto out;
2545 } else if (path->slots[0] >=
2546 btrfs_header_nritems(path->nodes[0])) {
2547 ret = btrfs_next_leaf(root, path);
2548 if (ret < 0) {
2549 goto out;
2550 } else if (ret > 0) {
2551 ret = -EUCLEAN;
2552 goto out;
2553 }
2554 }
2555 while (1) {
2556 leaf = path->nodes[0];
2557 slot = path->slots[0];
2558
2559 btrfs_item_key_to_cpu(leaf, &key, slot);
2560 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2561 ret = 0;
2562 goto out;
2563 }
2564
2565 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2566 ret = -EOVERFLOW;
2567 goto out;
2568 }
2569
2570 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2571 rootrefs->rootref[found].treeid = key.offset;
2572 rootrefs->rootref[found].dirid =
2573 btrfs_root_ref_dirid(leaf, rref);
2574 found++;
2575
2576 ret = btrfs_next_item(root, path);
2577 if (ret < 0) {
2578 goto out;
2579 } else if (ret > 0) {
2580 ret = -EUCLEAN;
2581 goto out;
2582 }
2583 }
2584
2585out:
2586 if (!ret || ret == -EOVERFLOW) {
2587 rootrefs->num_items = found;
2588 /* update min_treeid for next search */
2589 if (found)
2590 rootrefs->min_treeid =
2591 rootrefs->rootref[found - 1].treeid + 1;
2592 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2593 ret = -EFAULT;
2594 }
2595
2596 kfree(rootrefs);
2597 btrfs_free_path(path);
2598
2599 return ret;
2600}
2601
Yan, Zheng76dda932009-09-21 16:00:26 -04002602static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2603 void __user *arg)
2604{
Al Viro54563d42013-09-01 15:57:51 -04002605 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002606 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002607 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002608 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002609 struct inode *inode;
2610 struct btrfs_root *root = BTRFS_I(dir)->root;
2611 struct btrfs_root *dest = NULL;
2612 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002613 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002614 int err = 0;
2615
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002616 if (!S_ISDIR(dir->i_mode))
2617 return -ENOTDIR;
2618
Yan, Zheng76dda932009-09-21 16:00:26 -04002619 vol_args = memdup_user(arg, sizeof(*vol_args));
2620 if (IS_ERR(vol_args))
2621 return PTR_ERR(vol_args);
2622
2623 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2624 namelen = strlen(vol_args->name);
2625 if (strchr(vol_args->name, '/') ||
2626 strncmp(vol_args->name, "..", namelen) == 0) {
2627 err = -EINVAL;
2628 goto out;
2629 }
2630
Al Viroa561be72011-11-23 11:57:51 -05002631 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002632 if (err)
2633 goto out;
2634
David Sterba521e0542014-04-15 16:41:44 +02002635
Al Viro00235412016-05-26 00:05:12 -04002636 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2637 if (err == -EINTR)
2638 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002639 dentry = lookup_one_len(vol_args->name, parent, namelen);
2640 if (IS_ERR(dentry)) {
2641 err = PTR_ERR(dentry);
2642 goto out_unlock_dir;
2643 }
2644
David Howells2b0143b2015-03-17 22:25:59 +00002645 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002646 err = -ENOENT;
2647 goto out_dput;
2648 }
2649
David Howells2b0143b2015-03-17 22:25:59 +00002650 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002651 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302652 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002653 /*
2654 * Regular user. Only allow this with a special mount
2655 * option, when the user has write+exec access to the
2656 * subvol root, and when rmdir(2) would have been
2657 * allowed.
2658 *
2659 * Note that this is _not_ check that the subvol is
2660 * empty or doesn't contain data that we wouldn't
2661 * otherwise be able to delete.
2662 *
2663 * Users who want to delete empty subvols should try
2664 * rmdir(2).
2665 */
2666 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002667 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002668 goto out_dput;
2669
2670 /*
2671 * Do not allow deletion if the parent dir is the same
2672 * as the dir to be deleted. That means the ioctl
2673 * must be called on the dentry referencing the root
2674 * of the subvol, not a random directory contained
2675 * within it.
2676 */
2677 err = -EINVAL;
2678 if (root == dest)
2679 goto out_dput;
2680
2681 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2682 if (err)
2683 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002684 }
2685
Miao Xie5c39da52012-10-22 11:39:53 +00002686 /* check if subvolume may be deleted by a user */
2687 err = btrfs_may_delete(dir, dentry, 1);
2688 if (err)
2689 goto out_dput;
2690
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002691 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002692 err = -EINVAL;
2693 goto out_dput;
2694 }
2695
Al Viro59551022016-01-22 15:40:57 -05002696 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002697 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002698 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002699 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002700 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002701
Yan, Zheng76dda932009-09-21 16:00:26 -04002702out_dput:
2703 dput(dentry);
2704out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002705 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002706out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002707 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002708out:
2709 kfree(vol_args);
2710 return err;
2711}
2712
Chris Mason1e701a32010-03-11 09:42:04 -05002713static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002714{
Al Viro496ad9a2013-01-23 17:07:38 -05002715 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002716 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002717 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002718 int ret;
2719
Ilya Dryomov25122d12013-01-20 15:57:57 +02002720 ret = mnt_want_write_file(file);
2721 if (ret)
2722 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002723
Ilya Dryomov25122d12013-01-20 15:57:57 +02002724 if (btrfs_root_readonly(root)) {
2725 ret = -EROFS;
2726 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002727 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002728
2729 switch (inode->i_mode & S_IFMT) {
2730 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002731 if (!capable(CAP_SYS_ADMIN)) {
2732 ret = -EPERM;
2733 goto out;
2734 }
Eric Sandeende78b512013-01-31 18:21:12 +00002735 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002736 break;
2737 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002738 if (!(file->f_mode & FMODE_WRITE)) {
2739 ret = -EINVAL;
2740 goto out;
2741 }
Chris Mason1e701a32010-03-11 09:42:04 -05002742
2743 range = kzalloc(sizeof(*range), GFP_KERNEL);
2744 if (!range) {
2745 ret = -ENOMEM;
2746 goto out;
2747 }
2748
2749 if (argp) {
2750 if (copy_from_user(range, argp,
2751 sizeof(*range))) {
2752 ret = -EFAULT;
2753 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002754 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002755 }
2756 /* compression requires us to start the IO */
2757 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2758 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2759 range->extent_thresh = (u32)-1;
2760 }
2761 } else {
2762 /* the rest are all set to zero by kzalloc */
2763 range->len = (u64)-1;
2764 }
Al Viro496ad9a2013-01-23 17:07:38 -05002765 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08002766 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002767 if (ret > 0)
2768 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002769 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002770 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002771 default:
2772 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002773 }
Chris Masone441d542009-01-05 16:57:23 -05002774out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002775 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002776 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002777}
2778
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002779static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002780{
2781 struct btrfs_ioctl_vol_args *vol_args;
2782 int ret;
2783
Chris Masone441d542009-01-05 16:57:23 -05002784 if (!capable(CAP_SYS_ADMIN))
2785 return -EPERM;
2786
David Sterba171938e2017-03-28 14:44:21 +02002787 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002788 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002789
Li Zefandae7b662009-04-08 15:06:54 +08002790 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002791 if (IS_ERR(vol_args)) {
2792 ret = PTR_ERR(vol_args);
2793 goto out;
2794 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002795
Mark Fasheh5516e592008-07-24 12:20:14 -04002796 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002797 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002798
Anand Jain43d20762014-07-01 00:58:56 +08002799 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002800 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002801
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002802 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002803out:
David Sterba171938e2017-03-28 14:44:21 +02002804 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002805 return ret;
2806}
2807
Anand Jain6b526ed2016-02-13 10:01:39 +08002808static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002809{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002810 struct inode *inode = file_inode(file);
2811 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08002812 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002813 int ret;
2814
Chris Masone441d542009-01-05 16:57:23 -05002815 if (!capable(CAP_SYS_ADMIN))
2816 return -EPERM;
2817
Miao Xieda249272012-11-26 08:44:50 +00002818 ret = mnt_want_write_file(file);
2819 if (ret)
2820 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002821
Li Zefandae7b662009-04-08 15:06:54 +08002822 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002823 if (IS_ERR(vol_args)) {
2824 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002825 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002826 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002827
Anand Jain6b526ed2016-02-13 10:01:39 +08002828 /* Check for compatibility reject unknown flags */
Omar Sandovalfd4e9942018-05-22 15:44:01 -07002829 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
2830 ret = -EOPNOTSUPP;
2831 goto out;
2832 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002833
David Sterba171938e2017-03-28 14:44:21 +02002834 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00002835 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2836 goto out;
2837 }
2838
David Sterba735654e2016-02-15 18:15:21 +01002839 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002840 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08002841 } else {
2842 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002843 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08002844 }
David Sterba171938e2017-03-28 14:44:21 +02002845 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00002846
Anand Jain6b526ed2016-02-13 10:01:39 +08002847 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002848 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002849 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08002850 vol_args->devid);
2851 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002852 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08002853 vol_args->name);
2854 }
Anand Jain183860f2013-05-17 10:52:45 +00002855out:
2856 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002857err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002858 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002859 return ret;
2860}
2861
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002862static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2863{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002864 struct inode *inode = file_inode(file);
2865 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002866 struct btrfs_ioctl_vol_args *vol_args;
2867 int ret;
2868
2869 if (!capable(CAP_SYS_ADMIN))
2870 return -EPERM;
2871
2872 ret = mnt_want_write_file(file);
2873 if (ret)
2874 return ret;
2875
David Sterba171938e2017-03-28 14:44:21 +02002876 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002877 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002878 goto out_drop_write;
2879 }
2880
2881 vol_args = memdup_user(arg, sizeof(*vol_args));
2882 if (IS_ERR(vol_args)) {
2883 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002884 goto out;
2885 }
2886
David Sterba58d7bbf2016-05-04 14:10:47 +02002887 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002888 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002889
2890 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002891 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002892 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002893out:
David Sterba171938e2017-03-28 14:44:21 +02002894 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02002895out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002896 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002897
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002898 return ret;
2899}
2900
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002901static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2902 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002903{
Li Zefan027ed2f2011-06-08 08:27:56 +00002904 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002905 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002906 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002907 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002908
Li Zefan027ed2f2011-06-08 08:27:56 +00002909 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2910 if (!fi_args)
2911 return -ENOMEM;
2912
David Sterbad03262c2017-06-16 00:09:21 +02002913 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00002914 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01002915
David Sterbad03262c2017-06-16 00:09:21 +02002916 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002917 if (device->devid > fi_args->max_id)
2918 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002919 }
David Sterbad03262c2017-06-16 00:09:21 +02002920 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002921
David Sterbad03262c2017-06-16 00:09:21 +02002922 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07002923 fi_args->nodesize = fs_info->nodesize;
2924 fi_args->sectorsize = fs_info->sectorsize;
2925 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02002926
Li Zefan027ed2f2011-06-08 08:27:56 +00002927 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2928 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002929
Li Zefan027ed2f2011-06-08 08:27:56 +00002930 kfree(fi_args);
2931 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002932}
2933
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002934static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2935 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002936{
2937 struct btrfs_ioctl_dev_info_args *di_args;
2938 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01002939 int ret = 0;
2940 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002941
Jan Schmidt475f6382011-03-11 15:41:01 +01002942 di_args = memdup_user(arg, sizeof(*di_args));
2943 if (IS_ERR(di_args))
2944 return PTR_ERR(di_args);
2945
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002946 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002947 s_uuid = di_args->uuid;
2948
David Sterbac5593ca2017-06-16 00:09:21 +02002949 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002950 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002951
2952 if (!dev) {
2953 ret = -ENODEV;
2954 goto out;
2955 }
2956
2957 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002958 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2959 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002960 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002961 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002962 struct rcu_string *name;
2963
Josef Bacik606686e2012-06-04 14:03:51 -04002964 name = rcu_dereference(dev->name);
Xiongfeng Wang6670d4c2018-01-08 19:51:22 +08002965 strncpy(di_args->path, name->str, sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02002966 di_args->path[sizeof(di_args->path) - 1] = 0;
2967 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002968 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002969 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002970
2971out:
David Sterbac5593ca2017-06-16 00:09:21 +02002972 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002973 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2974 ret = -EFAULT;
2975
2976 kfree(di_args);
2977 return ret;
2978}
2979
Mark Fashehf4414602015-06-30 14:42:05 -07002980static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002981{
2982 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002983
Mark Fasheh416161d2013-08-06 11:42:51 -07002984 page = grab_cache_page(inode->i_mapping, index);
2985 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002986 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002987
2988 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002989 int ret;
2990
2991 ret = btrfs_readpage(NULL, page);
2992 if (ret)
2993 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002994 lock_page(page);
2995 if (!PageUptodate(page)) {
2996 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002997 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002998 return ERR_PTR(-EIO);
2999 }
3000 if (page->mapping != inode->i_mapping) {
3001 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003002 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00003003 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07003004 }
3005 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003006
3007 return page;
3008}
3009
Mark Fashehf4414602015-06-30 14:42:05 -07003010static int gather_extent_pages(struct inode *inode, struct page **pages,
3011 int num_pages, u64 off)
3012{
3013 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003014 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07003015
3016 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00003017again:
Mark Fashehf4414602015-06-30 14:42:05 -07003018 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00003019 if (IS_ERR(pages[i])) {
3020 int err = PTR_ERR(pages[i]);
3021
3022 if (err == -EAGAIN)
3023 goto again;
3024 pages[i] = NULL;
3025 return err;
3026 }
Mark Fashehf4414602015-06-30 14:42:05 -07003027 }
3028 return 0;
3029}
3030
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003031static int lock_extent_range(struct inode *inode, u64 off, u64 len,
3032 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003033{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003034 /*
3035 * Do any pending delalloc/csum calculations on inode, one way or
3036 * another, and lock file content.
3037 * The locking order is:
3038 *
3039 * 1) pages
3040 * 2) range in the inode's io tree
3041 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003042 while (1) {
3043 struct btrfs_ordered_extent *ordered;
3044 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
3045 ordered = btrfs_lookup_first_ordered_extent(inode,
3046 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01003047 if ((!ordered ||
3048 ordered->file_offset + ordered->len <= off ||
3049 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003050 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01003051 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
3052 if (ordered)
3053 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003054 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01003055 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003056 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
3057 if (ordered)
3058 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003059 if (!retry_range_locking)
3060 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003061 btrfs_wait_ordered_range(inode, off, len);
3062 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003063 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07003064}
3065
Mark Fashehf4414602015-06-30 14:42:05 -07003066static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07003067{
Al Viro59551022016-01-22 15:40:57 -05003068 inode_unlock(inode1);
3069 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07003070}
3071
Mark Fashehf4414602015-06-30 14:42:05 -07003072static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
3073{
3074 if (inode1 < inode2)
3075 swap(inode1, inode2);
3076
Al Viro59551022016-01-22 15:40:57 -05003077 inode_lock_nested(inode1, I_MUTEX_PARENT);
3078 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07003079}
3080
3081static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3082 struct inode *inode2, u64 loff2, u64 len)
3083{
3084 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3085 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3086}
3087
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003088static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3089 struct inode *inode2, u64 loff2, u64 len,
3090 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07003091{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003092 int ret;
3093
Mark Fasheh416161d2013-08-06 11:42:51 -07003094 if (inode1 < inode2) {
3095 swap(inode1, inode2);
3096 swap(loff1, loff2);
3097 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003098 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
3099 if (ret)
3100 return ret;
3101 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
3102 if (ret)
3103 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
3104 loff1 + len - 1);
3105 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003106}
3107
3108struct cmp_pages {
3109 int num_pages;
3110 struct page **src_pages;
3111 struct page **dst_pages;
3112};
3113
3114static void btrfs_cmp_data_free(struct cmp_pages *cmp)
3115{
3116 int i;
3117 struct page *pg;
3118
3119 for (i = 0; i < cmp->num_pages; i++) {
3120 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003121 if (pg) {
3122 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003123 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003124 }
Mark Fashehf4414602015-06-30 14:42:05 -07003125 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003126 if (pg) {
3127 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003128 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003129 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003130 }
Mark Fashehf4414602015-06-30 14:42:05 -07003131}
3132
3133static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
3134 struct inode *dst, u64 dst_loff,
3135 u64 len, struct cmp_pages *cmp)
3136{
3137 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003138 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07003139
Mark Fashehf4414602015-06-30 14:42:05 -07003140 cmp->num_pages = num_pages;
Mark Fashehf4414602015-06-30 14:42:05 -07003141
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003142 ret = gather_extent_pages(src, cmp->src_pages, num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003143 if (ret)
3144 goto out;
3145
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003146 ret = gather_extent_pages(dst, cmp->dst_pages, num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07003147
3148out:
3149 if (ret)
3150 btrfs_cmp_data_free(cmp);
Naohiro Aota78ad4ce2017-09-08 17:48:55 +09003151 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003152}
3153
David Sterba1a287cf2017-02-10 20:15:10 +01003154static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003155{
3156 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07003157 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07003158 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003159 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003160 void *addr, *dst_addr;
3161
Mark Fashehf4414602015-06-30 14:42:05 -07003162 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003163 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003164 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07003165 cmp_len = len;
3166
Mark Fashehf4414602015-06-30 14:42:05 -07003167 BUG_ON(i >= cmp->num_pages);
3168
3169 src_page = cmp->src_pages[i];
3170 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003171 ASSERT(PageLocked(src_page));
3172 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07003173
Mark Fasheh416161d2013-08-06 11:42:51 -07003174 addr = kmap_atomic(src_page);
3175 dst_addr = kmap_atomic(dst_page);
3176
3177 flush_dcache_page(src_page);
3178 flush_dcache_page(dst_page);
3179
3180 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003181 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07003182
3183 kunmap_atomic(addr);
3184 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07003185
3186 if (ret)
3187 break;
3188
Mark Fasheh416161d2013-08-06 11:42:51 -07003189 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003190 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003191 }
3192
3193 return ret;
3194}
3195
Mark Fashehe1d227a2015-06-08 15:05:25 -07003196static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3197 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003198{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003199 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003200 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3201
Mark Fashehe1d227a2015-06-08 15:05:25 -07003202 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003203 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003204
3205 /* if we extend to eof, continue to block boundary */
3206 if (off + len == inode->i_size)
3207 *plen = len = ALIGN(inode->i_size, bs) - off;
3208
Mark Fasheh416161d2013-08-06 11:42:51 -07003209 /* Check that we are block aligned - btrfs_clone() requires this */
3210 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3211 return -EINVAL;
3212
3213 return 0;
3214}
3215
Timofey Titovets39739092018-05-02 08:15:36 +03003216static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen,
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003217 struct inode *dst, u64 dst_loff,
3218 struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003219{
3220 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003221 u64 len = olen;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003222 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003223 u64 same_lock_start = 0;
3224 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003225
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003226 ret = extent_same_check_offsets(src, loff, &len, olen);
3227 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003228 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003229
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003230 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3231 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003232 return ret;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003233
3234 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003235 /*
3236 * Single inode case wants the same checks, except we
3237 * don't want our length pushed out past i_size as
3238 * comparing that data range makes no sense.
3239 *
3240 * extent_same_check_offsets() will do this for an
3241 * unaligned length at i_size, so catch it here and
3242 * reject the request.
3243 *
3244 * This effectively means we require aligned extents
3245 * for the single-inode case, whereas the other cases
3246 * allow an unaligned length so long as it ends at
3247 * i_size.
3248 */
Timofey Titovets39739092018-05-02 08:15:36 +03003249 if (len != olen)
3250 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003251
3252 /* Check for overlapping ranges */
Timofey Titovets39739092018-05-02 08:15:36 +03003253 if (dst_loff + len > loff && dst_loff < loff + len)
3254 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003255
3256 same_lock_start = min_t(u64, loff, dst_loff);
3257 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003258 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003259
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003260again:
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003261 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, cmp);
Mark Fashehf4414602015-06-30 14:42:05 -07003262 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003263 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003264
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003265 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003266 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3267 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003268 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003269 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3270 false);
3271 /*
3272 * If one of the inodes has dirty pages in the respective range or
3273 * ordered extents, we need to flush dellaloc and wait for all ordered
3274 * extents in the range. We must unlock the pages and the ranges in the
3275 * io trees to avoid deadlocks when flushing delalloc (requires locking
3276 * pages) and when waiting for ordered extents to complete (they require
3277 * range locking).
3278 */
3279 if (ret == -EAGAIN) {
3280 /*
3281 * Ranges in the io trees already unlocked. Now unlock all
3282 * pages before waiting for all IO to complete.
3283 */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003284 btrfs_cmp_data_free(cmp);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003285 if (same_inode) {
3286 btrfs_wait_ordered_range(src, same_lock_start,
3287 same_lock_len);
3288 } else {
3289 btrfs_wait_ordered_range(src, loff, len);
3290 btrfs_wait_ordered_range(dst, dst_loff, len);
3291 }
3292 goto again;
3293 }
3294 ASSERT(ret == 0);
3295 if (WARN_ON(ret)) {
3296 /* ranges in the io trees already unlocked */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003297 btrfs_cmp_data_free(cmp);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003298 return ret;
3299 }
Mark Fashehf4414602015-06-30 14:42:05 -07003300
Mark Fasheh207910d2015-06-30 14:42:04 -07003301 /* pass original length for comparison so we stay within i_size */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003302 ret = btrfs_cmp_data(olen, cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003303 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003304 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003305
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003306 if (same_inode)
3307 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3308 same_lock_start + same_lock_len - 1);
3309 else
3310 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003311
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003312 btrfs_cmp_data_free(cmp);
Timofey Titovets39739092018-05-02 08:15:36 +03003313
3314 return ret;
3315}
3316
Timofey Titovetsb6728762018-05-02 08:15:37 +03003317#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3318
Timofey Titovets39739092018-05-02 08:15:36 +03003319static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3320 struct inode *dst, u64 dst_loff)
3321{
3322 int ret;
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003323 struct cmp_pages cmp;
3324 int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT;
Timofey Titovets39739092018-05-02 08:15:36 +03003325 bool same_inode = (src == dst);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003326 u64 i, tail_len, chunk_count;
Timofey Titovets39739092018-05-02 08:15:36 +03003327
3328 if (olen == 0)
3329 return 0;
3330
3331 if (same_inode)
3332 inode_lock(src);
3333 else
3334 btrfs_double_inode_lock(src, dst);
3335
3336 /* don't make the dst file partly checksummed */
3337 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3338 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3339 ret = -EINVAL;
3340 goto out_unlock;
3341 }
3342
Timofey Titovetsb6728762018-05-02 08:15:37 +03003343 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3344 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003345 if (chunk_count == 0)
3346 num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT;
3347
3348 /*
3349 * If deduping ranges in the same inode, locking rules make it
3350 * mandatory to always lock pages in ascending order to avoid deadlocks
3351 * with concurrent tasks (such as starting writeback/delalloc).
3352 */
3353 if (same_inode && dst_loff < loff)
3354 swap(loff, dst_loff);
3355
3356 /*
3357 * We must gather up all the pages before we initiate our extent
3358 * locking. We use an array for the page pointers. Size of the array is
3359 * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
3360 */
David Sterbabf5091c2018-05-11 17:57:54 +02003361 cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3362 GFP_KERNEL | __GFP_ZERO);
3363 cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3364 GFP_KERNEL | __GFP_ZERO);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003365 if (!cmp.src_pages || !cmp.dst_pages) {
David Sterbabf5091c2018-05-11 17:57:54 +02003366 ret = -ENOMEM;
3367 goto out_free;
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003368 }
Timofey Titovetsb6728762018-05-02 08:15:37 +03003369
3370 for (i = 0; i < chunk_count; i++) {
3371 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003372 dst, dst_loff, &cmp);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003373 if (ret)
3374 goto out_unlock;
3375
3376 loff += BTRFS_MAX_DEDUPE_LEN;
3377 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3378 }
3379
3380 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003381 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
3382 dst_loff, &cmp);
Timofey Titovets39739092018-05-02 08:15:36 +03003383
Mark Fasheh416161d2013-08-06 11:42:51 -07003384out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003385 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003386 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003387 else
3388 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003389
David Sterbabf5091c2018-05-11 17:57:54 +02003390out_free:
3391 kvfree(cmp.src_pages);
3392 kvfree(cmp.dst_pages);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003393
Mark Fasheh416161d2013-08-06 11:42:51 -07003394 return ret;
3395}
3396
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003397ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3398 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003399{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003400 struct inode *src = file_inode(src_file);
3401 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003402 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003403 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003404
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003405 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003406 /*
3407 * Btrfs does not support blocksize < page_size. As a
3408 * result, btrfs_cmp_data() won't correctly handle
3409 * this situation without an update.
3410 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003411 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003412 }
3413
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003414 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3415 if (res)
3416 return res;
3417 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003418}
3419
Filipe Mananaf82a9902014-06-01 01:50:28 +01003420static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3421 struct inode *inode,
3422 u64 endoff,
3423 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003424 const u64 olen,
3425 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003426{
3427 struct btrfs_root *root = BTRFS_I(inode)->root;
3428 int ret;
3429
3430 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003431 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003432 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003433 /*
3434 * We round up to the block size at eof when determining which
3435 * extents to clone above, but shouldn't round up the file size.
3436 */
3437 if (endoff > destoff + olen)
3438 endoff = destoff + olen;
3439 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003440 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003441
3442 ret = btrfs_update_inode(trans, root, inode);
3443 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003444 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003445 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003446 goto out;
3447 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003448 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003449out:
3450 return ret;
3451}
3452
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003453static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003454 const struct btrfs_trans_handle *trans,
3455 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003456 const u64 hole_offset,
3457 const u64 hole_len)
3458{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003459 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003460 struct extent_map *em;
3461 int ret;
3462
3463 em = alloc_extent_map();
3464 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003465 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003466 return;
3467 }
3468
Filipe Manana14f59792014-06-29 21:45:40 +01003469 if (path) {
3470 struct btrfs_file_extent_item *fi;
3471
3472 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3473 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003474 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003475 em->generation = -1;
3476 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3477 BTRFS_FILE_EXTENT_INLINE)
3478 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003479 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003480 } else {
3481 em->start = hole_offset;
3482 em->len = hole_len;
3483 em->ram_bytes = em->len;
3484 em->orig_start = hole_offset;
3485 em->block_start = EXTENT_MAP_HOLE;
3486 em->block_len = 0;
3487 em->orig_block_len = 0;
3488 em->compress_type = BTRFS_COMPRESS_NONE;
3489 em->generation = trans->transid;
3490 }
3491
3492 while (1) {
3493 write_lock(&em_tree->lock);
3494 ret = add_extent_mapping(em_tree, em, 1);
3495 write_unlock(&em_tree->lock);
3496 if (ret != -EEXIST) {
3497 free_extent_map(em);
3498 break;
3499 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003500 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003501 em->start + em->len - 1, 0);
3502 }
3503
David Sterbaee39b432014-09-30 01:33:33 +02003504 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003505 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003506}
3507
Filipe Manana8039d872015-10-13 15:15:00 +01003508/*
3509 * Make sure we do not end up inserting an inline extent into a file that has
3510 * already other (non-inline) extents. If a file has an inline extent it can
3511 * not have any other extents and the (single) inline extent must start at the
3512 * file offset 0. Failing to respect these rules will lead to file corruption,
3513 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3514 *
3515 * We can have extents that have been already written to disk or we can have
3516 * dirty ranges still in delalloc, in which case the extent maps and items are
3517 * created only when we run delalloc, and the delalloc ranges might fall outside
3518 * the range we are currently locking in the inode's io tree. So we check the
3519 * inode's i_size because of that (i_size updates are done while holding the
3520 * i_mutex, which we are holding here).
3521 * We also check to see if the inode has a size not greater than "datal" but has
3522 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3523 * protected against such concurrent fallocate calls by the i_mutex).
3524 *
3525 * If the file has no extents but a size greater than datal, do not allow the
3526 * copy because we would need turn the inline extent into a non-inline one (even
3527 * with NO_HOLES enabled). If we find our destination inode only has one inline
3528 * extent, just overwrite it with the source inline extent if its size is less
3529 * than the source extent's size, or we could copy the source inline extent's
3530 * data into the destination inode's inline extent if the later is greater then
3531 * the former.
3532 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003533static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003534 struct btrfs_trans_handle *trans,
3535 struct btrfs_path *path,
3536 struct btrfs_key *new_key,
3537 const u64 drop_start,
3538 const u64 datal,
3539 const u64 skip,
3540 const u64 size,
3541 char *inline_data)
3542{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003543 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003544 struct btrfs_root *root = BTRFS_I(dst)->root;
3545 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003546 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003547 int ret;
3548 struct btrfs_key key;
3549
3550 if (new_key->offset > 0)
3551 return -EOPNOTSUPP;
3552
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003553 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003554 key.type = BTRFS_EXTENT_DATA_KEY;
3555 key.offset = 0;
3556 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3557 if (ret < 0) {
3558 return ret;
3559 } else if (ret > 0) {
3560 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3561 ret = btrfs_next_leaf(root, path);
3562 if (ret < 0)
3563 return ret;
3564 else if (ret > 0)
3565 goto copy_inline_extent;
3566 }
3567 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003568 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003569 key.type == BTRFS_EXTENT_DATA_KEY) {
3570 ASSERT(key.offset > 0);
3571 return -EOPNOTSUPP;
3572 }
3573 } else if (i_size_read(dst) <= datal) {
3574 struct btrfs_file_extent_item *ei;
3575 u64 ext_len;
3576
3577 /*
3578 * If the file size is <= datal, make sure there are no other
3579 * extents following (can happen do to an fallocate call with
3580 * the flag FALLOC_FL_KEEP_SIZE).
3581 */
3582 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3583 struct btrfs_file_extent_item);
3584 /*
3585 * If it's an inline extent, it can not have other extents
3586 * following it.
3587 */
3588 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3589 BTRFS_FILE_EXTENT_INLINE)
3590 goto copy_inline_extent;
3591
3592 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3593 if (ext_len > aligned_end)
3594 return -EOPNOTSUPP;
3595
3596 ret = btrfs_next_item(root, path);
3597 if (ret < 0) {
3598 return ret;
3599 } else if (ret == 0) {
3600 btrfs_item_key_to_cpu(path->nodes[0], &key,
3601 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003602 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003603 key.type == BTRFS_EXTENT_DATA_KEY)
3604 return -EOPNOTSUPP;
3605 }
3606 }
3607
3608copy_inline_extent:
3609 /*
3610 * We have no extent items, or we have an extent at offset 0 which may
3611 * or may not be inlined. All these cases are dealt the same way.
3612 */
3613 if (i_size_read(dst) > datal) {
3614 /*
3615 * If the destination inode has an inline extent...
3616 * This would require copying the data from the source inline
3617 * extent into the beginning of the destination's inline extent.
3618 * But this is really complex, both extents can be compressed
3619 * or just one of them, which would require decompressing and
3620 * re-compressing data (which could increase the new compressed
3621 * size, not allowing the compressed data to fit anymore in an
3622 * inline extent).
3623 * So just don't support this case for now (it should be rare,
3624 * we are not really saving space when cloning inline extents).
3625 */
3626 return -EOPNOTSUPP;
3627 }
3628
3629 btrfs_release_path(path);
3630 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3631 if (ret)
3632 return ret;
3633 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3634 if (ret)
3635 return ret;
3636
3637 if (skip) {
3638 const u32 start = btrfs_file_extent_calc_inline_size(0);
3639
3640 memmove(inline_data + start, inline_data + start + skip, datal);
3641 }
3642
3643 write_extent_buffer(path->nodes[0], inline_data,
3644 btrfs_item_ptr_offset(path->nodes[0],
3645 path->slots[0]),
3646 size);
3647 inode_add_bytes(dst, datal);
3648
3649 return 0;
3650}
3651
Mark Fasheh32b7c682013-08-06 11:42:49 -07003652/**
3653 * btrfs_clone() - clone a range from inode file to another
3654 *
3655 * @src: Inode to clone from
3656 * @inode: Inode to clone to
3657 * @off: Offset within source to start clone from
3658 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003659 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003660 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003661 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003662 */
3663static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003664 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003665 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003666{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003667 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003668 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003669 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003670 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003671 struct btrfs_trans_handle *trans;
3672 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003673 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003674 u32 nritems;
3675 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003676 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003677 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003678 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003679
3680 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003681 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3682 if (!buf)
3683 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003684
3685 path = btrfs_alloc_path();
3686 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003687 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003688 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003689 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003690
David Sterbae4058b52015-11-27 16:31:35 +01003691 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003692 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003693 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003694 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003695 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003696
3697 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003698 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003699
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003700 /*
3701 * note the key will change type as we walk through the
3702 * tree.
3703 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003704 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003705 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3706 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003707 if (ret < 0)
3708 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003709 /*
3710 * First search, if no extent item that starts at offset off was
3711 * found but the previous item is an extent item, it's possible
3712 * it might overlap our target range, therefore process it.
3713 */
3714 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3715 btrfs_item_key_to_cpu(path->nodes[0], &key,
3716 path->slots[0] - 1);
3717 if (key.type == BTRFS_EXTENT_DATA_KEY)
3718 path->slots[0]--;
3719 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003720
Yan Zhengae01a0a2008-08-04 23:23:47 -04003721 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003722process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003723 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003724 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003725 if (ret < 0)
3726 goto out;
3727 if (ret > 0)
3728 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003729 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003730 }
3731 leaf = path->nodes[0];
3732 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003733
Yan Zhengae01a0a2008-08-04 23:23:47 -04003734 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003735 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003736 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003737 break;
3738
David Sterba962a2982014-06-04 18:41:45 +02003739 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003740 struct btrfs_file_extent_item *extent;
3741 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003742 u32 size;
3743 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003744 u64 disko = 0, diskl = 0;
3745 u64 datao = 0, datal = 0;
3746 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003747 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003748
Sage Weilc5c9cd42008-11-12 14:32:25 -05003749 extent = btrfs_item_ptr(leaf, slot,
3750 struct btrfs_file_extent_item);
3751 comp = btrfs_file_extent_compression(leaf, extent);
3752 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003753 if (type == BTRFS_FILE_EXTENT_REG ||
3754 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003755 disko = btrfs_file_extent_disk_bytenr(leaf,
3756 extent);
3757 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3758 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003759 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003760 datal = btrfs_file_extent_num_bytes(leaf,
3761 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003762 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3763 /* take upper bound, may be compressed */
3764 datal = btrfs_file_extent_ram_bytes(leaf,
3765 extent);
3766 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003767
Filipe Manana2c463822014-05-31 02:31:05 +01003768 /*
3769 * The first search might have left us at an extent
3770 * item that ends before our target range's start, can
3771 * happen if we have holes and NO_HOLES feature enabled.
3772 */
3773 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003774 path->slots[0]++;
3775 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003776 } else if (key.offset >= off + len) {
3777 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003778 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003779 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003780 size = btrfs_item_size_nr(leaf, slot);
3781 read_extent_buffer(leaf, buf,
3782 btrfs_item_ptr_offset(leaf, slot),
3783 size);
3784
3785 btrfs_release_path(path);
3786 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003787
Zheng Yan31840ae2008-09-23 13:14:14 -04003788 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003789 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003790 if (off <= key.offset)
3791 new_key.offset = key.offset + destoff - off;
3792 else
3793 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003794
Sage Weilb6f34092011-09-20 14:48:51 -04003795 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003796 * Deal with a hole that doesn't have an extent item
3797 * that represents it (NO_HOLES feature enabled).
3798 * This hole is either in the middle of the cloning
3799 * range or at the beginning (fully overlaps it or
3800 * partially overlaps it).
3801 */
3802 if (new_key.offset != last_dest_end)
3803 drop_start = last_dest_end;
3804 else
3805 drop_start = new_key.offset;
3806
3807 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003808 * 1 - adjusting old extent (we may have to split it)
3809 * 1 - add new extent
3810 * 1 - inode update
3811 */
3812 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003813 if (IS_ERR(trans)) {
3814 ret = PTR_ERR(trans);
3815 goto out;
3816 }
3817
Chris Masonc8a894d2009-06-27 21:07:03 -04003818 if (type == BTRFS_FILE_EXTENT_REG ||
3819 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003820 /*
3821 * a | --- range to clone ---| b
3822 * | ------------- extent ------------- |
3823 */
3824
Antonio Ospite93915582014-06-04 14:03:48 +02003825 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003826 if (key.offset + datal > off + len)
3827 datal = off + len - key.offset;
3828
Antonio Ospite93915582014-06-04 14:03:48 +02003829 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003830 if (off > key.offset) {
3831 datao += off - key.offset;
3832 datal -= off - key.offset;
3833 }
3834
Josef Bacik5dc562c2012-08-17 13:14:17 -04003835 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003836 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003837 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003838 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003839 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003840 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003841 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003842 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003843 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003844 goto out;
3845 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003846
Sage Weilc5c9cd42008-11-12 14:32:25 -05003847 ret = btrfs_insert_empty_item(trans, root, path,
3848 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003849 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003850 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003851 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003852 goto out;
3853 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003854
3855 leaf = path->nodes[0];
3856 slot = path->slots[0];
3857 write_extent_buffer(leaf, buf,
3858 btrfs_item_ptr_offset(leaf, slot),
3859 size);
3860
3861 extent = btrfs_item_ptr(leaf, slot,
3862 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003863
Sage Weilc5c9cd42008-11-12 14:32:25 -05003864 /* disko == 0 means it's a hole */
3865 if (!disko)
3866 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003867
3868 btrfs_set_file_extent_offset(leaf, extent,
3869 datao);
3870 btrfs_set_file_extent_num_bytes(leaf, extent,
3871 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003872
Sage Weilc5c9cd42008-11-12 14:32:25 -05003873 if (disko) {
3874 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003875 ret = btrfs_inc_extent_ref(trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003876 root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003877 disko, diskl, 0,
3878 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003879 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003880 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003881 if (ret) {
3882 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003883 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003884 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003885 goto out;
3886
3887 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003888 }
3889 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3890 u64 skip = 0;
3891 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003892
Sage Weilc5c9cd42008-11-12 14:32:25 -05003893 if (off > key.offset) {
3894 skip = off - key.offset;
3895 new_key.offset += skip;
3896 }
Chris Masond3977122009-01-05 21:25:51 -05003897
Liu Boaa42ffd2012-09-18 03:52:23 -06003898 if (key.offset + datal > off + len)
3899 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003900
Sage Weilc5c9cd42008-11-12 14:32:25 -05003901 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003902 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003903 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003904 goto out;
3905 }
3906 size -= skip + trim;
3907 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003908
David Sterba4a0ab9d2017-02-10 20:18:49 +01003909 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003910 trans, path,
3911 &new_key,
3912 drop_start,
3913 datal,
3914 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003915 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003916 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003917 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003918 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003919 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003920 goto out;
3921 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003922 leaf = path->nodes[0];
3923 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003924 }
3925
Filipe Manana7ffbb592014-06-09 03:48:05 +01003926 /* If we have an implicit hole (NO_HOLES feature). */
3927 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003928 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003929 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003930 new_key.offset - drop_start);
3931
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003932 clone_update_extent_map(BTRFS_I(inode), trans,
3933 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003934
Sage Weilc5c9cd42008-11-12 14:32:25 -05003935 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003936 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003937
Filipe Manana62e23902014-08-08 02:47:06 +01003938 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003939 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003940 ret = clone_finish_inode_update(trans, inode,
3941 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003942 destoff, olen,
3943 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003944 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003945 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003946 if (new_key.offset + datal >= destoff + len)
3947 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003948 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003949 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003950 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003951
3952 if (fatal_signal_pending(current)) {
3953 ret = -EINTR;
3954 goto out;
3955 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003956 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003957 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003958
Filipe Mananaf82a9902014-06-01 01:50:28 +01003959 if (last_dest_end < destoff + len) {
3960 /*
3961 * We have an implicit hole (NO_HOLES feature is enabled) that
3962 * fully or partially overlaps our cloning range at its end.
3963 */
3964 btrfs_release_path(path);
3965
3966 /*
3967 * 1 - remove extent(s)
3968 * 1 - inode update
3969 */
3970 trans = btrfs_start_transaction(root, 2);
3971 if (IS_ERR(trans)) {
3972 ret = PTR_ERR(trans);
3973 goto out;
3974 }
3975 ret = btrfs_drop_extents(trans, root, inode,
3976 last_dest_end, destoff + len, 1);
3977 if (ret) {
3978 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003979 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003980 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003981 goto out;
3982 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003983 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3984 last_dest_end,
3985 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003986 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003987 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003988 }
3989
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003990out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003991 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003992 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003993 return ret;
3994}
3995
Zach Brown3db11b22015-11-10 16:53:32 -05003996static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3997 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003998{
Al Viro54563d42013-09-01 15:57:51 -04003999 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05004000 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004001 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004002 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004003 int ret;
4004 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004005 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05004006 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004007
4008 /*
4009 * TODO:
4010 * - split compressed inline extents. annoying: we need to
4011 * decompress into destination's address_space (the file offset
4012 * may change, so source mapping won't do), then recompress (or
4013 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08004014 *
4015 * - split destination inode's inline extents. The inline extents can
4016 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07004017 */
4018
Mark Fasheh32b7c682013-08-06 11:42:49 -07004019 if (btrfs_root_readonly(root))
4020 return -EROFS;
4021
Zach Brown3db11b22015-11-10 16:53:32 -05004022 if (file_src->f_path.mnt != file->f_path.mnt ||
4023 src->i_sb != inode->i_sb)
4024 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004025
Mark Fasheh32b7c682013-08-06 11:42:49 -07004026 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05004027 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004028
4029 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07004030 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004031 } else {
Al Viro59551022016-01-22 15:40:57 -05004032 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004033 }
4034
Omar Sandovalb5c40d52018-05-22 15:02:12 -07004035 /* don't make the dst file partly checksummed */
4036 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
4037 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
4038 ret = -EINVAL;
4039 goto out_unlock;
4040 }
4041
Mark Fasheh32b7c682013-08-06 11:42:49 -07004042 /* determine range to clone */
4043 ret = -EINVAL;
4044 if (off + len > src->i_size || off + len < off)
4045 goto out_unlock;
4046 if (len == 0)
4047 olen = len = src->i_size - off;
4048 /* if we extend to eof, continue to block boundary */
4049 if (off + len == src->i_size)
4050 len = ALIGN(src->i_size, bs) - off;
4051
Filipe Mananaccccf3d62015-03-30 18:23:59 +01004052 if (len == 0) {
4053 ret = 0;
4054 goto out_unlock;
4055 }
4056
Mark Fasheh32b7c682013-08-06 11:42:49 -07004057 /* verify the end result is block aligned */
4058 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
4059 !IS_ALIGNED(destoff, bs))
4060 goto out_unlock;
4061
4062 /* verify if ranges are overlapped within the same file */
4063 if (same_inode) {
4064 if (destoff + len > off && destoff < off + len)
4065 goto out_unlock;
4066 }
4067
4068 if (destoff > inode->i_size) {
4069 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
4070 if (ret)
4071 goto out_unlock;
4072 }
4073
Filipe Mananac125b8b2014-05-23 05:03:34 +01004074 /*
4075 * Lock the target range too. Right after we replace the file extent
4076 * items in the fs tree (which now point to the cloned data), we might
4077 * have a worker replace them with extent items relative to a write
4078 * operation that was issued before this clone operation (i.e. confront
4079 * with inode.c:btrfs_finish_ordered_io).
4080 */
4081 if (same_inode) {
4082 u64 lock_start = min_t(u64, off, destoff);
4083 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07004084
Filipe Mananae0bd70c2016-01-27 10:20:58 +00004085 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01004086 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00004087 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
4088 true);
4089 }
4090 ASSERT(ret == 0);
4091 if (WARN_ON(ret)) {
4092 /* ranges in the io trees already unlocked */
4093 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01004094 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07004095
Mark Fasheh1c919a52015-06-30 14:42:08 -07004096 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07004097
Filipe Mananac125b8b2014-05-23 05:03:34 +01004098 if (same_inode) {
4099 u64 lock_start = min_t(u64, off, destoff);
4100 u64 lock_end = max_t(u64, off, destoff) + len - 1;
4101
4102 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
4103 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07004104 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01004105 }
4106 /*
4107 * Truncate page cache pages so that future reads will see the cloned
4108 * data immediately and not the previous data.
4109 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05304110 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004111 round_down(destoff, PAGE_SIZE),
4112 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004113out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07004114 if (!same_inode)
4115 btrfs_double_inode_unlock(src, inode);
4116 else
Al Viro59551022016-01-22 15:40:57 -05004117 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05004118 return ret;
4119}
4120
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004121int btrfs_clone_file_range(struct file *src_file, loff_t off,
4122 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05004123{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01004124 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05004125}
4126
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004127static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4128{
Al Viro496ad9a2013-01-23 17:07:38 -05004129 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004130 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004131 struct btrfs_root *root = BTRFS_I(inode)->root;
4132 struct btrfs_root *new_root;
4133 struct btrfs_dir_item *di;
4134 struct btrfs_trans_handle *trans;
4135 struct btrfs_path *path;
4136 struct btrfs_key location;
4137 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004138 u64 objectid = 0;
4139 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00004140 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004141
4142 if (!capable(CAP_SYS_ADMIN))
4143 return -EPERM;
4144
Miao Xie3c04ce02012-11-26 08:43:07 +00004145 ret = mnt_want_write_file(file);
4146 if (ret)
4147 return ret;
4148
4149 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4150 ret = -EFAULT;
4151 goto out;
4152 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004153
4154 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05304155 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004156
4157 location.objectid = objectid;
4158 location.type = BTRFS_ROOT_ITEM_KEY;
4159 location.offset = (u64)-1;
4160
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004161 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00004162 if (IS_ERR(new_root)) {
4163 ret = PTR_ERR(new_root);
4164 goto out;
4165 }
satoru takeuchi6d6d2822017-09-12 22:42:52 +09004166 if (!is_fstree(new_root->objectid)) {
4167 ret = -ENOENT;
4168 goto out;
4169 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004170
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004171 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00004172 if (!path) {
4173 ret = -ENOMEM;
4174 goto out;
4175 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004176 path->leave_spinning = 1;
4177
4178 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004179 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004180 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00004181 ret = PTR_ERR(trans);
4182 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004183 }
4184
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004185 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4186 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004187 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00004188 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004189 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004190 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004191 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004192 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00004193 ret = -ENOENT;
4194 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004195 }
4196
4197 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4198 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4199 btrfs_mark_buffer_dirty(path->nodes[0]);
4200 btrfs_free_path(path);
4201
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004202 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004203 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00004204out:
4205 mnt_drop_write_file(file);
4206 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004207}
4208
Su Yuec065f5b12018-04-02 17:24:11 +08004209static void get_block_group_info(struct list_head *groups_list,
4210 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004211{
4212 struct btrfs_block_group_cache *block_group;
4213
4214 space->total_bytes = 0;
4215 space->used_bytes = 0;
4216 space->flags = 0;
4217 list_for_each_entry(block_group, groups_list, list) {
4218 space->flags = block_group->flags;
4219 space->total_bytes += block_group->key.offset;
4220 space->used_bytes +=
4221 btrfs_block_group_used(&block_group->item);
4222 }
4223}
4224
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004225static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4226 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004227{
4228 struct btrfs_ioctl_space_args space_args;
4229 struct btrfs_ioctl_space_info space;
4230 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004231 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004232 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004233 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004234 static const u64 types[] = {
4235 BTRFS_BLOCK_GROUP_DATA,
4236 BTRFS_BLOCK_GROUP_SYSTEM,
4237 BTRFS_BLOCK_GROUP_METADATA,
4238 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4239 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004240 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004241 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004242 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004243 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004244 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004245
4246 if (copy_from_user(&space_args,
4247 (struct btrfs_ioctl_space_args __user *)arg,
4248 sizeof(space_args)))
4249 return -EFAULT;
4250
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004251 for (i = 0; i < num_types; i++) {
4252 struct btrfs_space_info *tmp;
4253
4254 info = NULL;
4255 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004256 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004257 list) {
4258 if (tmp->flags == types[i]) {
4259 info = tmp;
4260 break;
4261 }
4262 }
4263 rcu_read_unlock();
4264
4265 if (!info)
4266 continue;
4267
4268 down_read(&info->groups_sem);
4269 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4270 if (!list_empty(&info->block_groups[c]))
4271 slot_count++;
4272 }
4273 up_read(&info->groups_sem);
4274 }
Josef Bacik1406e432010-01-13 18:19:06 +00004275
David Sterba36523e952014-02-07 14:34:12 +01004276 /*
4277 * Global block reserve, exported as a space_info
4278 */
4279 slot_count++;
4280
Chris Mason7fde62b2010-03-16 15:40:10 -04004281 /* space_slots == 0 means they are asking for a count */
4282 if (space_args.space_slots == 0) {
4283 space_args.total_spaces = slot_count;
4284 goto out;
4285 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004286
Dan Rosenberg51788b12011-02-14 16:04:23 -05004287 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004288
Chris Mason7fde62b2010-03-16 15:40:10 -04004289 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004290
Chris Mason7fde62b2010-03-16 15:40:10 -04004291 /* we generally have at most 6 or so space infos, one for each raid
4292 * level. So, a whole page should be more than enough for everyone
4293 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004294 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004295 return -ENOMEM;
4296
4297 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004298 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004299 if (!dest)
4300 return -ENOMEM;
4301 dest_orig = dest;
4302
4303 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004304 for (i = 0; i < num_types; i++) {
4305 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004306
Dan Rosenberg51788b12011-02-14 16:04:23 -05004307 if (!slot_count)
4308 break;
4309
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004310 info = NULL;
4311 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004312 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004313 list) {
4314 if (tmp->flags == types[i]) {
4315 info = tmp;
4316 break;
4317 }
4318 }
4319 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004320
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004321 if (!info)
4322 continue;
4323 down_read(&info->groups_sem);
4324 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4325 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004326 get_block_group_info(&info->block_groups[c],
4327 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004328 memcpy(dest, &space, sizeof(space));
4329 dest++;
4330 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004331 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004332 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004333 if (!slot_count)
4334 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004335 }
4336 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004337 }
Josef Bacik1406e432010-01-13 18:19:06 +00004338
David Sterba36523e952014-02-07 14:34:12 +01004339 /*
4340 * Add global block reserve
4341 */
4342 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004343 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004344
4345 spin_lock(&block_rsv->lock);
4346 space.total_bytes = block_rsv->size;
4347 space.used_bytes = block_rsv->size - block_rsv->reserved;
4348 spin_unlock(&block_rsv->lock);
4349 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4350 memcpy(dest, &space, sizeof(space));
4351 space_args.total_spaces++;
4352 }
4353
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004354 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004355 (arg + sizeof(struct btrfs_ioctl_space_args));
4356
4357 if (copy_to_user(user_dest, dest_orig, alloc_size))
4358 ret = -EFAULT;
4359
4360 kfree(dest_orig);
4361out:
4362 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004363 ret = -EFAULT;
4364
4365 return ret;
4366}
4367
Miao Xie9a8c28b2012-11-26 08:40:43 +00004368static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4369 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004370{
Sage Weil46204592010-10-29 15:41:32 -04004371 struct btrfs_trans_handle *trans;
4372 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004373 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004374
Miao Xied4edf392013-02-20 09:17:06 +00004375 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004376 if (IS_ERR(trans)) {
4377 if (PTR_ERR(trans) != -ENOENT)
4378 return PTR_ERR(trans);
4379
4380 /* No running transaction, don't bother */
4381 transid = root->fs_info->last_trans_committed;
4382 goto out;
4383 }
Sage Weil46204592010-10-29 15:41:32 -04004384 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004385 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004386 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004387 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004388 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004389 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004390out:
Sage Weil46204592010-10-29 15:41:32 -04004391 if (argp)
4392 if (copy_to_user(argp, &transid, sizeof(transid)))
4393 return -EFAULT;
4394 return 0;
4395}
4396
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004397static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004398 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004399{
Sage Weil46204592010-10-29 15:41:32 -04004400 u64 transid;
4401
4402 if (argp) {
4403 if (copy_from_user(&transid, argp, sizeof(transid)))
4404 return -EFAULT;
4405 } else {
4406 transid = 0; /* current trans */
4407 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004408 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004409}
4410
Miao Xieb8e95482012-11-26 08:48:01 +00004411static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004412{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004413 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004414 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004415 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004416
4417 if (!capable(CAP_SYS_ADMIN))
4418 return -EPERM;
4419
4420 sa = memdup_user(arg, sizeof(*sa));
4421 if (IS_ERR(sa))
4422 return PTR_ERR(sa);
4423
Miao Xieb8e95482012-11-26 08:48:01 +00004424 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4425 ret = mnt_want_write_file(file);
4426 if (ret)
4427 goto out;
4428 }
4429
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004430 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004431 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4432 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004433
4434 if (copy_to_user(arg, sa, sizeof(*sa)))
4435 ret = -EFAULT;
4436
Miao Xieb8e95482012-11-26 08:48:01 +00004437 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4438 mnt_drop_write_file(file);
4439out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004440 kfree(sa);
4441 return ret;
4442}
4443
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004444static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004445{
4446 if (!capable(CAP_SYS_ADMIN))
4447 return -EPERM;
4448
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004449 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004450}
4451
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004452static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004453 void __user *arg)
4454{
4455 struct btrfs_ioctl_scrub_args *sa;
4456 int ret;
4457
4458 if (!capable(CAP_SYS_ADMIN))
4459 return -EPERM;
4460
4461 sa = memdup_user(arg, sizeof(*sa));
4462 if (IS_ERR(sa))
4463 return PTR_ERR(sa);
4464
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004465 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004466
4467 if (copy_to_user(arg, sa, sizeof(*sa)))
4468 ret = -EFAULT;
4469
4470 kfree(sa);
4471 return ret;
4472}
4473
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004474static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004475 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004476{
4477 struct btrfs_ioctl_get_dev_stats *sa;
4478 int ret;
4479
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004480 sa = memdup_user(arg, sizeof(*sa));
4481 if (IS_ERR(sa))
4482 return PTR_ERR(sa);
4483
David Sterbab27f7c02012-06-22 06:30:39 -06004484 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4485 kfree(sa);
4486 return -EPERM;
4487 }
4488
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004489 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004490
4491 if (copy_to_user(arg, sa, sizeof(*sa)))
4492 ret = -EFAULT;
4493
4494 kfree(sa);
4495 return ret;
4496}
4497
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004498static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4499 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004500{
4501 struct btrfs_ioctl_dev_replace_args *p;
4502 int ret;
4503
4504 if (!capable(CAP_SYS_ADMIN))
4505 return -EPERM;
4506
4507 p = memdup_user(arg, sizeof(*p));
4508 if (IS_ERR(p))
4509 return PTR_ERR(p);
4510
4511 switch (p->cmd) {
4512 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004513 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004514 ret = -EROFS;
4515 goto out;
4516 }
David Sterba171938e2017-03-28 14:44:21 +02004517 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004518 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004519 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004520 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004521 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004522 }
4523 break;
4524 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004525 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004526 ret = 0;
4527 break;
4528 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004529 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004530 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004531 break;
4532 default:
4533 ret = -EINVAL;
4534 break;
4535 }
4536
4537 if (copy_to_user(arg, p, sizeof(*p)))
4538 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004539out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004540 kfree(p);
4541 return ret;
4542}
4543
Jan Schmidtd7728c92011-07-07 16:48:38 +02004544static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4545{
4546 int ret = 0;
4547 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004548 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004549 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004550 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004551 struct inode_fs_paths *ipath = NULL;
4552 struct btrfs_path *path;
4553
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004554 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004555 return -EPERM;
4556
4557 path = btrfs_alloc_path();
4558 if (!path) {
4559 ret = -ENOMEM;
4560 goto out;
4561 }
4562
4563 ipa = memdup_user(arg, sizeof(*ipa));
4564 if (IS_ERR(ipa)) {
4565 ret = PTR_ERR(ipa);
4566 ipa = NULL;
4567 goto out;
4568 }
4569
4570 size = min_t(u32, ipa->size, 4096);
4571 ipath = init_ipath(size, root, path);
4572 if (IS_ERR(ipath)) {
4573 ret = PTR_ERR(ipath);
4574 ipath = NULL;
4575 goto out;
4576 }
4577
4578 ret = paths_from_inode(ipa->inum, ipath);
4579 if (ret < 0)
4580 goto out;
4581
4582 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004583 rel_ptr = ipath->fspath->val[i] -
4584 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004585 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004586 }
4587
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004588 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4589 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004590 if (ret) {
4591 ret = -EFAULT;
4592 goto out;
4593 }
4594
4595out:
4596 btrfs_free_path(path);
4597 free_ipath(ipath);
4598 kfree(ipa);
4599
4600 return ret;
4601}
4602
4603static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4604{
4605 struct btrfs_data_container *inodes = ctx;
4606 const size_t c = 3 * sizeof(u64);
4607
4608 if (inodes->bytes_left >= c) {
4609 inodes->bytes_left -= c;
4610 inodes->val[inodes->elem_cnt] = inum;
4611 inodes->val[inodes->elem_cnt + 1] = offset;
4612 inodes->val[inodes->elem_cnt + 2] = root;
4613 inodes->elem_cnt += 3;
4614 } else {
4615 inodes->bytes_missing += c - inodes->bytes_left;
4616 inodes->bytes_left = 0;
4617 inodes->elem_missed += 3;
4618 }
4619
4620 return 0;
4621}
4622
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004623static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004624 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004625{
4626 int ret = 0;
4627 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004628 struct btrfs_ioctl_logical_ino_args *loi;
4629 struct btrfs_data_container *inodes = NULL;
4630 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004631 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004632
4633 if (!capable(CAP_SYS_ADMIN))
4634 return -EPERM;
4635
4636 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304637 if (IS_ERR(loi))
4638 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004639
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004640 if (version == 1) {
4641 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004642 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004643 } else {
4644 /* All reserved bits must be 0 for now */
4645 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4646 ret = -EINVAL;
4647 goto out_loi;
4648 }
4649 /* Only accept flags we have defined so far */
4650 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4651 ret = -EINVAL;
4652 goto out_loi;
4653 }
4654 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004655 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004656 }
4657
Jan Schmidtd7728c92011-07-07 16:48:38 +02004658 path = btrfs_alloc_path();
4659 if (!path) {
4660 ret = -ENOMEM;
4661 goto out;
4662 }
4663
Jan Schmidtd7728c92011-07-07 16:48:38 +02004664 inodes = init_data_container(size);
4665 if (IS_ERR(inodes)) {
4666 ret = PTR_ERR(inodes);
4667 inodes = NULL;
4668 goto out;
4669 }
4670
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004671 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004672 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004673 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004674 ret = -ENOENT;
4675 if (ret < 0)
4676 goto out;
4677
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004678 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4679 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004680 if (ret)
4681 ret = -EFAULT;
4682
4683out:
4684 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004685 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004686out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004687 kfree(loi);
4688
4689 return ret;
4690}
4691
David Sterba008ef092018-03-21 02:05:27 +01004692void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004693 struct btrfs_ioctl_balance_args *bargs)
4694{
4695 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4696
4697 bargs->flags = bctl->flags;
4698
David Sterba3009a622018-03-21 01:31:04 +01004699 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004700 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4701 if (atomic_read(&fs_info->balance_pause_req))
4702 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004703 if (atomic_read(&fs_info->balance_cancel_req))
4704 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004705
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004706 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4707 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4708 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004709
David Sterba008ef092018-03-21 02:05:27 +01004710 spin_lock(&fs_info->balance_lock);
4711 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4712 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004713}
4714
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004715static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004716{
Al Viro496ad9a2013-01-23 17:07:38 -05004717 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004718 struct btrfs_fs_info *fs_info = root->fs_info;
4719 struct btrfs_ioctl_balance_args *bargs;
4720 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004721 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004722 int ret;
4723
4724 if (!capable(CAP_SYS_ADMIN))
4725 return -EPERM;
4726
Liu Boe54bfa312012-06-29 03:58:48 -06004727 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004728 if (ret)
4729 return ret;
4730
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004731again:
David Sterba171938e2017-03-28 14:44:21 +02004732 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004733 mutex_lock(&fs_info->balance_mutex);
4734 need_unlock = true;
4735 goto locked;
4736 }
4737
4738 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004739 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004740 * (1) some other op is running
4741 * (2) balance is running
4742 * (3) balance is paused -- special case (think resume)
4743 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004744 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004745 if (fs_info->balance_ctl) {
4746 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004747 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004748 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004749 /*
4750 * Lock released to allow other waiters to continue,
4751 * we'll reexamine the status again.
4752 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004753 mutex_lock(&fs_info->balance_mutex);
4754
4755 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004756 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004757 /* this is (3) */
4758 need_unlock = false;
4759 goto locked;
4760 }
4761
4762 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004763 goto again;
4764 } else {
4765 /* this is (2) */
4766 mutex_unlock(&fs_info->balance_mutex);
4767 ret = -EINPROGRESS;
4768 goto out;
4769 }
4770 } else {
4771 /* this is (1) */
4772 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004773 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004774 goto out;
4775 }
4776
4777locked:
David Sterba171938e2017-03-28 14:44:21 +02004778 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004779
4780 if (arg) {
4781 bargs = memdup_user(arg, sizeof(*bargs));
4782 if (IS_ERR(bargs)) {
4783 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004784 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004785 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004786
4787 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4788 if (!fs_info->balance_ctl) {
4789 ret = -ENOTCONN;
4790 goto out_bargs;
4791 }
4792
4793 bctl = fs_info->balance_ctl;
4794 spin_lock(&fs_info->balance_lock);
4795 bctl->flags |= BTRFS_BALANCE_RESUME;
4796 spin_unlock(&fs_info->balance_lock);
4797
4798 goto do_balance;
4799 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004800 } else {
4801 bargs = NULL;
4802 }
4803
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004804 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004805 ret = -EINPROGRESS;
4806 goto out_bargs;
4807 }
4808
David Sterba8d2db782015-11-04 15:38:29 +01004809 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004810 if (!bctl) {
4811 ret = -ENOMEM;
4812 goto out_bargs;
4813 }
4814
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004815 if (arg) {
4816 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4817 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4818 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4819
4820 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004821 } else {
4822 /* balance everything - no filters */
4823 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004824 }
4825
David Sterba8eb93452015-10-12 16:55:54 +02004826 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4827 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004828 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004829 }
4830
Ilya Dryomovde322262012-01-16 22:04:49 +02004831do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004832 /*
David Sterba149196a2018-03-20 20:23:09 +01004833 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4834 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4835 * restriper was paused all the way until unmount, in free_fs_info.
4836 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004837 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004838 need_unlock = false;
4839
David Sterba6fcf6e22018-05-07 17:44:03 +02004840 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004841 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004842
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004843 if (arg) {
4844 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4845 ret = -EFAULT;
4846 }
4847
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004848out_bctl:
4849 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004850out_bargs:
4851 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004852out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004853 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004854 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004855 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004856out:
Liu Boe54bfa312012-06-29 03:58:48 -06004857 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004858 return ret;
4859}
4860
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004861static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004862{
4863 if (!capable(CAP_SYS_ADMIN))
4864 return -EPERM;
4865
4866 switch (cmd) {
4867 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004868 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004869 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004870 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004871 }
4872
4873 return -EINVAL;
4874}
4875
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004876static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004877 void __user *arg)
4878{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004879 struct btrfs_ioctl_balance_args *bargs;
4880 int ret = 0;
4881
4882 if (!capable(CAP_SYS_ADMIN))
4883 return -EPERM;
4884
4885 mutex_lock(&fs_info->balance_mutex);
4886 if (!fs_info->balance_ctl) {
4887 ret = -ENOTCONN;
4888 goto out;
4889 }
4890
David Sterba8d2db782015-11-04 15:38:29 +01004891 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004892 if (!bargs) {
4893 ret = -ENOMEM;
4894 goto out;
4895 }
4896
David Sterba008ef092018-03-21 02:05:27 +01004897 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004898
4899 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4900 ret = -EFAULT;
4901
4902 kfree(bargs);
4903out:
4904 mutex_unlock(&fs_info->balance_mutex);
4905 return ret;
4906}
4907
Miao Xie905b0dd2012-11-26 08:50:11 +00004908static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004909{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004910 struct inode *inode = file_inode(file);
4911 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004912 struct btrfs_ioctl_quota_ctl_args *sa;
4913 struct btrfs_trans_handle *trans = NULL;
4914 int ret;
4915 int err;
4916
4917 if (!capable(CAP_SYS_ADMIN))
4918 return -EPERM;
4919
Miao Xie905b0dd2012-11-26 08:50:11 +00004920 ret = mnt_want_write_file(file);
4921 if (ret)
4922 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004923
4924 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004925 if (IS_ERR(sa)) {
4926 ret = PTR_ERR(sa);
4927 goto drop_write;
4928 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004929
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004930 down_write(&fs_info->subvol_sem);
4931 trans = btrfs_start_transaction(fs_info->tree_root, 2);
Jan Schmidt2f232032013-04-25 16:04:51 +00004932 if (IS_ERR(trans)) {
4933 ret = PTR_ERR(trans);
4934 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004935 }
4936
4937 switch (sa->cmd) {
4938 case BTRFS_QUOTA_CTL_ENABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004939 ret = btrfs_quota_enable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004940 break;
4941 case BTRFS_QUOTA_CTL_DISABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004942 ret = btrfs_quota_disable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004943 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004944 default:
4945 ret = -EINVAL;
4946 break;
4947 }
4948
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004949 err = btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00004950 if (err && !ret)
4951 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004952out:
4953 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004954 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004955drop_write:
4956 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004957 return ret;
4958}
4959
Miao Xie905b0dd2012-11-26 08:50:11 +00004960static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004961{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004962 struct inode *inode = file_inode(file);
4963 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4964 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004965 struct btrfs_ioctl_qgroup_assign_args *sa;
4966 struct btrfs_trans_handle *trans;
4967 int ret;
4968 int err;
4969
4970 if (!capable(CAP_SYS_ADMIN))
4971 return -EPERM;
4972
Miao Xie905b0dd2012-11-26 08:50:11 +00004973 ret = mnt_want_write_file(file);
4974 if (ret)
4975 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004976
4977 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004978 if (IS_ERR(sa)) {
4979 ret = PTR_ERR(sa);
4980 goto drop_write;
4981 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004982
4983 trans = btrfs_join_transaction(root);
4984 if (IS_ERR(trans)) {
4985 ret = PTR_ERR(trans);
4986 goto out;
4987 }
4988
Arne Jansen5d13a372011-09-14 15:53:51 +02004989 if (sa->assign) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004990 ret = btrfs_add_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004991 sa->src, sa->dst);
4992 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004993 ret = btrfs_del_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004994 sa->src, sa->dst);
4995 }
4996
Qu Wenruoe082f562015-02-27 16:24:28 +08004997 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004998 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08004999 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005000 btrfs_handle_fs_error(fs_info, err,
5001 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005002 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005003 if (err && !ret)
5004 ret = err;
5005
5006out:
5007 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005008drop_write:
5009 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005010 return ret;
5011}
5012
Miao Xie905b0dd2012-11-26 08:50:11 +00005013static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005014{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005015 struct inode *inode = file_inode(file);
5016 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5017 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02005018 struct btrfs_ioctl_qgroup_create_args *sa;
5019 struct btrfs_trans_handle *trans;
5020 int ret;
5021 int err;
5022
5023 if (!capable(CAP_SYS_ADMIN))
5024 return -EPERM;
5025
Miao Xie905b0dd2012-11-26 08:50:11 +00005026 ret = mnt_want_write_file(file);
5027 if (ret)
5028 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005029
5030 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005031 if (IS_ERR(sa)) {
5032 ret = PTR_ERR(sa);
5033 goto drop_write;
5034 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005035
Miao Xied86e56c2012-11-15 11:35:41 +00005036 if (!sa->qgroupid) {
5037 ret = -EINVAL;
5038 goto out;
5039 }
5040
Arne Jansen5d13a372011-09-14 15:53:51 +02005041 trans = btrfs_join_transaction(root);
5042 if (IS_ERR(trans)) {
5043 ret = PTR_ERR(trans);
5044 goto out;
5045 }
5046
Arne Jansen5d13a372011-09-14 15:53:51 +02005047 if (sa->create) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005048 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02005049 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005050 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02005051 }
5052
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005053 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005054 if (err && !ret)
5055 ret = err;
5056
5057out:
5058 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005059drop_write:
5060 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005061 return ret;
5062}
5063
Miao Xie905b0dd2012-11-26 08:50:11 +00005064static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02005065{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005066 struct inode *inode = file_inode(file);
5067 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5068 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02005069 struct btrfs_ioctl_qgroup_limit_args *sa;
5070 struct btrfs_trans_handle *trans;
5071 int ret;
5072 int err;
5073 u64 qgroupid;
5074
5075 if (!capable(CAP_SYS_ADMIN))
5076 return -EPERM;
5077
Miao Xie905b0dd2012-11-26 08:50:11 +00005078 ret = mnt_want_write_file(file);
5079 if (ret)
5080 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02005081
5082 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00005083 if (IS_ERR(sa)) {
5084 ret = PTR_ERR(sa);
5085 goto drop_write;
5086 }
Arne Jansen5d13a372011-09-14 15:53:51 +02005087
5088 trans = btrfs_join_transaction(root);
5089 if (IS_ERR(trans)) {
5090 ret = PTR_ERR(trans);
5091 goto out;
5092 }
5093
5094 qgroupid = sa->qgroupid;
5095 if (!qgroupid) {
5096 /* take the current subvol as qgroup */
5097 qgroupid = root->root_key.objectid;
5098 }
5099
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005100 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02005101
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005102 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02005103 if (err && !ret)
5104 ret = err;
5105
5106out:
5107 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00005108drop_write:
5109 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02005110 return ret;
5111}
5112
Jan Schmidt2f232032013-04-25 16:04:51 +00005113static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5114{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005115 struct inode *inode = file_inode(file);
5116 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005117 struct btrfs_ioctl_quota_rescan_args *qsa;
5118 int ret;
5119
5120 if (!capable(CAP_SYS_ADMIN))
5121 return -EPERM;
5122
5123 ret = mnt_want_write_file(file);
5124 if (ret)
5125 return ret;
5126
5127 qsa = memdup_user(arg, sizeof(*qsa));
5128 if (IS_ERR(qsa)) {
5129 ret = PTR_ERR(qsa);
5130 goto drop_write;
5131 }
5132
5133 if (qsa->flags) {
5134 ret = -EINVAL;
5135 goto out;
5136 }
5137
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005138 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00005139
5140out:
5141 kfree(qsa);
5142drop_write:
5143 mnt_drop_write_file(file);
5144 return ret;
5145}
5146
5147static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5148{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005149 struct inode *inode = file_inode(file);
5150 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00005151 struct btrfs_ioctl_quota_rescan_args *qsa;
5152 int ret = 0;
5153
5154 if (!capable(CAP_SYS_ADMIN))
5155 return -EPERM;
5156
David Sterba8d2db782015-11-04 15:38:29 +01005157 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00005158 if (!qsa)
5159 return -ENOMEM;
5160
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005161 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00005162 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005163 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00005164 }
5165
5166 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5167 ret = -EFAULT;
5168
5169 kfree(qsa);
5170 return ret;
5171}
5172
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005173static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5174{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005175 struct inode *inode = file_inode(file);
5176 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005177
5178 if (!capable(CAP_SYS_ADMIN))
5179 return -EPERM;
5180
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005181 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005182}
5183
Hugo Millsabccd002014-01-30 20:17:00 +00005184static long _btrfs_ioctl_set_received_subvol(struct file *file,
5185 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02005186{
Al Viro496ad9a2013-01-23 17:07:38 -05005187 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005188 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02005189 struct btrfs_root *root = BTRFS_I(inode)->root;
5190 struct btrfs_root_item *root_item = &root->root_item;
5191 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005192 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02005193 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005194 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02005195
David Sterbabd60ea02014-01-16 15:50:22 +01005196 if (!inode_owner_or_capable(inode))
5197 return -EPERM;
5198
Alexander Block8ea05e32012-07-25 17:35:53 +02005199 ret = mnt_want_write_file(file);
5200 if (ret < 0)
5201 return ret;
5202
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005203 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02005204
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005205 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02005206 ret = -EINVAL;
5207 goto out;
5208 }
5209
5210 if (btrfs_root_readonly(root)) {
5211 ret = -EROFS;
5212 goto out;
5213 }
5214
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005215 /*
5216 * 1 - root item
5217 * 2 - uuid items (received uuid + subvol uuid)
5218 */
5219 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005220 if (IS_ERR(trans)) {
5221 ret = PTR_ERR(trans);
5222 trans = NULL;
5223 goto out;
5224 }
5225
5226 sa->rtransid = trans->transid;
5227 sa->rtime.sec = ct.tv_sec;
5228 sa->rtime.nsec = ct.tv_nsec;
5229
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005230 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5231 BTRFS_UUID_SIZE);
5232 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005233 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005234 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005235 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5236 root->root_key.objectid);
5237 if (ret && ret != -ENOENT) {
5238 btrfs_abort_transaction(trans, ret);
5239 btrfs_end_transaction(trans);
5240 goto out;
5241 }
5242 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005243 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5244 btrfs_set_root_stransid(root_item, sa->stransid);
5245 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005246 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5247 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5248 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5249 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005250
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005251 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005252 &root->root_key, &root->root_item);
5253 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005254 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005255 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005256 }
5257 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005258 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005259 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5260 root->root_key.objectid);
5261 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005262 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005263 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005264 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005265 }
5266 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005267 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005268out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005269 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005270 mnt_drop_write_file(file);
5271 return ret;
5272}
5273
5274#ifdef CONFIG_64BIT
5275static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5276 void __user *arg)
5277{
5278 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5279 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5280 int ret = 0;
5281
5282 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305283 if (IS_ERR(args32))
5284 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005285
David Sterba8d2db782015-11-04 15:38:29 +01005286 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005287 if (!args64) {
5288 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005289 goto out;
5290 }
5291
5292 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5293 args64->stransid = args32->stransid;
5294 args64->rtransid = args32->rtransid;
5295 args64->stime.sec = args32->stime.sec;
5296 args64->stime.nsec = args32->stime.nsec;
5297 args64->rtime.sec = args32->rtime.sec;
5298 args64->rtime.nsec = args32->rtime.nsec;
5299 args64->flags = args32->flags;
5300
5301 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5302 if (ret)
5303 goto out;
5304
5305 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5306 args32->stransid = args64->stransid;
5307 args32->rtransid = args64->rtransid;
5308 args32->stime.sec = args64->stime.sec;
5309 args32->stime.nsec = args64->stime.nsec;
5310 args32->rtime.sec = args64->rtime.sec;
5311 args32->rtime.nsec = args64->rtime.nsec;
5312 args32->flags = args64->flags;
5313
5314 ret = copy_to_user(arg, args32, sizeof(*args32));
5315 if (ret)
5316 ret = -EFAULT;
5317
5318out:
5319 kfree(args32);
5320 kfree(args64);
5321 return ret;
5322}
5323#endif
5324
5325static long btrfs_ioctl_set_received_subvol(struct file *file,
5326 void __user *arg)
5327{
5328 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5329 int ret = 0;
5330
5331 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305332 if (IS_ERR(sa))
5333 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005334
5335 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5336
5337 if (ret)
5338 goto out;
5339
Alexander Block8ea05e32012-07-25 17:35:53 +02005340 ret = copy_to_user(arg, sa, sizeof(*sa));
5341 if (ret)
5342 ret = -EFAULT;
5343
5344out:
5345 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005346 return ret;
5347}
5348
jeff.liu867ab662013-01-05 02:48:01 +00005349static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5350{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005351 struct inode *inode = file_inode(file);
5352 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005353 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005354 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005355 char label[BTRFS_LABEL_SIZE];
5356
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005357 spin_lock(&fs_info->super_lock);
5358 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5359 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005360
5361 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005362
5363 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005364 btrfs_warn(fs_info,
5365 "label is too long, return the first %zu bytes",
5366 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005367 }
5368
jeff.liu867ab662013-01-05 02:48:01 +00005369 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005370
5371 return ret ? -EFAULT : 0;
5372}
5373
jeff.liua8bfd4a2013-01-05 02:48:08 +00005374static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5375{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005376 struct inode *inode = file_inode(file);
5377 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5378 struct btrfs_root *root = BTRFS_I(inode)->root;
5379 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005380 struct btrfs_trans_handle *trans;
5381 char label[BTRFS_LABEL_SIZE];
5382 int ret;
5383
5384 if (!capable(CAP_SYS_ADMIN))
5385 return -EPERM;
5386
5387 if (copy_from_user(label, arg, sizeof(label)))
5388 return -EFAULT;
5389
5390 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005391 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005392 "unable to set label with more than %d bytes",
5393 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005394 return -EINVAL;
5395 }
5396
5397 ret = mnt_want_write_file(file);
5398 if (ret)
5399 return ret;
5400
jeff.liua8bfd4a2013-01-05 02:48:08 +00005401 trans = btrfs_start_transaction(root, 0);
5402 if (IS_ERR(trans)) {
5403 ret = PTR_ERR(trans);
5404 goto out_unlock;
5405 }
5406
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005407 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005408 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005409 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005410 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005411
5412out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005413 mnt_drop_write_file(file);
5414 return ret;
5415}
5416
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005417#define INIT_FEATURE_FLAGS(suffix) \
5418 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5419 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5420 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5421
David Sterbad5131b62016-02-17 15:26:27 +01005422int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005423{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005424 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005425 INIT_FEATURE_FLAGS(SUPP),
5426 INIT_FEATURE_FLAGS(SAFE_SET),
5427 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5428 };
5429
5430 if (copy_to_user(arg, &features, sizeof(features)))
5431 return -EFAULT;
5432
5433 return 0;
5434}
5435
5436static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5437{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005438 struct inode *inode = file_inode(file);
5439 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5440 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005441 struct btrfs_ioctl_feature_flags features;
5442
5443 features.compat_flags = btrfs_super_compat_flags(super_block);
5444 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5445 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5446
5447 if (copy_to_user(arg, &features, sizeof(features)))
5448 return -EFAULT;
5449
5450 return 0;
5451}
5452
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005453static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005454 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005455 u64 change_mask, u64 flags, u64 supported_flags,
5456 u64 safe_set, u64 safe_clear)
5457{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005458 const char *type = btrfs_feature_set_names[set];
5459 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005460 u64 disallowed, unsupported;
5461 u64 set_mask = flags & change_mask;
5462 u64 clear_mask = ~flags & change_mask;
5463
5464 unsupported = set_mask & ~supported_flags;
5465 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005466 names = btrfs_printable_features(set, unsupported);
5467 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005468 btrfs_warn(fs_info,
5469 "this kernel does not support the %s feature bit%s",
5470 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005471 kfree(names);
5472 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005473 btrfs_warn(fs_info,
5474 "this kernel does not support %s bits 0x%llx",
5475 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005476 return -EOPNOTSUPP;
5477 }
5478
5479 disallowed = set_mask & ~safe_set;
5480 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005481 names = btrfs_printable_features(set, disallowed);
5482 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005483 btrfs_warn(fs_info,
5484 "can't set the %s feature bit%s while mounted",
5485 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005486 kfree(names);
5487 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005488 btrfs_warn(fs_info,
5489 "can't set %s bits 0x%llx while mounted",
5490 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005491 return -EPERM;
5492 }
5493
5494 disallowed = clear_mask & ~safe_clear;
5495 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005496 names = btrfs_printable_features(set, disallowed);
5497 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005498 btrfs_warn(fs_info,
5499 "can't clear the %s feature bit%s while mounted",
5500 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005501 kfree(names);
5502 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005503 btrfs_warn(fs_info,
5504 "can't clear %s bits 0x%llx while mounted",
5505 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005506 return -EPERM;
5507 }
5508
5509 return 0;
5510}
5511
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005512#define check_feature(fs_info, change_mask, flags, mask_base) \
5513check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005514 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5515 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5516 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5517
5518static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5519{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005520 struct inode *inode = file_inode(file);
5521 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5522 struct btrfs_root *root = BTRFS_I(inode)->root;
5523 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005524 struct btrfs_ioctl_feature_flags flags[2];
5525 struct btrfs_trans_handle *trans;
5526 u64 newflags;
5527 int ret;
5528
5529 if (!capable(CAP_SYS_ADMIN))
5530 return -EPERM;
5531
5532 if (copy_from_user(flags, arg, sizeof(flags)))
5533 return -EFAULT;
5534
5535 /* Nothing to do */
5536 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5537 !flags[0].incompat_flags)
5538 return 0;
5539
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005540 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005541 flags[1].compat_flags, COMPAT);
5542 if (ret)
5543 return ret;
5544
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005545 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005546 flags[1].compat_ro_flags, COMPAT_RO);
5547 if (ret)
5548 return ret;
5549
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005550 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005551 flags[1].incompat_flags, INCOMPAT);
5552 if (ret)
5553 return ret;
5554
David Sterba7ab19622016-05-04 11:32:00 +02005555 ret = mnt_want_write_file(file);
5556 if (ret)
5557 return ret;
5558
David Sterba8051aa12014-02-07 14:34:04 +01005559 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005560 if (IS_ERR(trans)) {
5561 ret = PTR_ERR(trans);
5562 goto out_drop_write;
5563 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005564
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005565 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005566 newflags = btrfs_super_compat_flags(super_block);
5567 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5568 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5569 btrfs_set_super_compat_flags(super_block, newflags);
5570
5571 newflags = btrfs_super_compat_ro_flags(super_block);
5572 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5573 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5574 btrfs_set_super_compat_ro_flags(super_block, newflags);
5575
5576 newflags = btrfs_super_incompat_flags(super_block);
5577 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5578 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5579 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005580 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005581
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005582 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005583out_drop_write:
5584 mnt_drop_write_file(file);
5585
5586 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005587}
5588
Josef Bacik2351f432017-09-27 10:43:13 -04005589static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5590{
5591 struct btrfs_ioctl_send_args *arg;
5592 int ret;
5593
5594 if (compat) {
5595#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5596 struct btrfs_ioctl_send_args_32 args32;
5597
5598 ret = copy_from_user(&args32, argp, sizeof(args32));
5599 if (ret)
5600 return -EFAULT;
5601 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5602 if (!arg)
5603 return -ENOMEM;
5604 arg->send_fd = args32.send_fd;
5605 arg->clone_sources_count = args32.clone_sources_count;
5606 arg->clone_sources = compat_ptr(args32.clone_sources);
5607 arg->parent_root = args32.parent_root;
5608 arg->flags = args32.flags;
5609 memcpy(arg->reserved, args32.reserved,
5610 sizeof(args32.reserved));
5611#else
5612 return -ENOTTY;
5613#endif
5614 } else {
5615 arg = memdup_user(argp, sizeof(*arg));
5616 if (IS_ERR(arg))
5617 return PTR_ERR(arg);
5618 }
5619 ret = btrfs_ioctl_send(file, arg);
5620 kfree(arg);
5621 return ret;
5622}
5623
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005624long btrfs_ioctl(struct file *file, unsigned int
5625 cmd, unsigned long arg)
5626{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005627 struct inode *inode = file_inode(file);
5628 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5629 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005630 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005631
5632 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005633 case FS_IOC_GETFLAGS:
5634 return btrfs_ioctl_getflags(file, argp);
5635 case FS_IOC_SETFLAGS:
5636 return btrfs_ioctl_setflags(file, argp);
5637 case FS_IOC_GETVERSION:
5638 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005639 case FITRIM:
5640 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005641 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005642 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005643 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005644 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005645 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005646 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005647 case BTRFS_IOC_SUBVOL_CREATE_V2:
5648 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005649 case BTRFS_IOC_SNAP_DESTROY:
5650 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005651 case BTRFS_IOC_SUBVOL_GETFLAGS:
5652 return btrfs_ioctl_subvol_getflags(file, argp);
5653 case BTRFS_IOC_SUBVOL_SETFLAGS:
5654 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005655 case BTRFS_IOC_DEFAULT_SUBVOL:
5656 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005657 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005658 return btrfs_ioctl_defrag(file, NULL);
5659 case BTRFS_IOC_DEFRAG_RANGE:
5660 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005661 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005662 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005663 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005664 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005665 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005666 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005667 case BTRFS_IOC_RM_DEV_V2:
5668 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005669 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005670 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005671 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005672 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005673 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005674 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005675 case BTRFS_IOC_TREE_SEARCH:
5676 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005677 case BTRFS_IOC_TREE_SEARCH_V2:
5678 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005679 case BTRFS_IOC_INO_LOOKUP:
5680 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005681 case BTRFS_IOC_INO_PATHS:
5682 return btrfs_ioctl_ino_to_path(root, argp);
5683 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005684 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5685 case BTRFS_IOC_LOGICAL_INO_V2:
5686 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005687 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005688 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005689 case BTRFS_IOC_SYNC: {
5690 int ret;
5691
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005692 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005693 if (ret)
5694 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005695 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005696 /*
5697 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005698 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005699 * processing uncleaned subvols.
5700 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005701 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005702 return ret;
5703 }
Sage Weil46204592010-10-29 15:41:32 -04005704 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005705 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005706 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005707 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005708 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005709 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005710 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005711 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005712 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005713 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005714 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005715 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005716 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005717 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005718 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005719 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005720 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5721 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005722#ifdef CONFIG_64BIT
5723 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5724 return btrfs_ioctl_set_received_subvol_32(file, argp);
5725#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005726 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005727 return _btrfs_ioctl_send(file, argp, false);
5728#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5729 case BTRFS_IOC_SEND_32:
5730 return _btrfs_ioctl_send(file, argp, true);
5731#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005732 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005733 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005734 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005735 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005736 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005737 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005738 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005739 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005740 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005741 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005742 case BTRFS_IOC_QUOTA_RESCAN:
5743 return btrfs_ioctl_quota_rescan(file, argp);
5744 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5745 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005746 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5747 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005748 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005749 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005750 case BTRFS_IOC_GET_FSLABEL:
5751 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005752 case BTRFS_IOC_SET_FSLABEL:
5753 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005754 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005755 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005756 case BTRFS_IOC_GET_FEATURES:
5757 return btrfs_ioctl_get_features(file, argp);
5758 case BTRFS_IOC_SET_FEATURES:
5759 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005760 case FS_IOC_FSGETXATTR:
5761 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005762 case FS_IOC_FSSETXATTR:
5763 return btrfs_ioctl_fssetxattr(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005764 case BTRFS_IOC_GET_SUBVOL_INFO:
5765 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005766 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5767 return btrfs_ioctl_get_subvol_rootref(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005768 }
5769
5770 return -ENOTTY;
5771}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005772
5773#ifdef CONFIG_COMPAT
5774long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5775{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005776 /*
5777 * These all access 32-bit values anyway so no further
5778 * handling is necessary.
5779 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005780 switch (cmd) {
5781 case FS_IOC32_GETFLAGS:
5782 cmd = FS_IOC_GETFLAGS;
5783 break;
5784 case FS_IOC32_SETFLAGS:
5785 cmd = FS_IOC_SETFLAGS;
5786 break;
5787 case FS_IOC32_GETVERSION:
5788 cmd = FS_IOC_GETVERSION;
5789 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005790 }
5791
5792 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5793}
5794#endif