blob: a58ab372091ecb3fddc749b9e691cc060b675577 [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 Zefanf062abf2011-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 Zefanf062abf2011-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 Zefanf062abf2011-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 Zefanf062abf2011-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);
liubo2d4e6f62011-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;
Miao Xied5c12072013-02-28 10:04:33 +0000572 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200573 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400574
David Sterba49a3c4d2016-03-24 17:49:22 +0100575 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
576 if (!root_item)
577 return -ENOMEM;
578
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400579 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400580 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100581 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000582
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800583 /*
584 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400585 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800586 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100587 if (btrfs_qgroup_level(objectid)) {
588 ret = -ENOSPC;
589 goto fail_free;
590 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800591
Miao Xied5c12072013-02-28 10:04:33 +0000592 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400593 /*
Miao Xied5c12072013-02-28 10:04:33 +0000594 * The same as the snapshot creation, please see the comment
595 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400596 */
Miao Xied5c12072013-02-28 10:04:33 +0000597 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200598 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000599 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100600 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400601
Miao Xied5c12072013-02-28 10:04:33 +0000602 trans = btrfs_start_transaction(root, 0);
603 if (IS_ERR(trans)) {
604 ret = PTR_ERR(trans);
David Sterba7775c812017-02-10 19:18:18 +0100605 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100606 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000607 }
608 trans->block_rsv = &block_rsv;
609 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400610
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400611 ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200612 if (ret)
613 goto fail;
614
David Sterba4d75f8a2014-06-15 01:54:12 +0200615 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400616 if (IS_ERR(leaf)) {
617 ret = PTR_ERR(leaf);
618 goto fail;
619 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400620
David Sterbab159fa22016-11-08 18:09:03 +0100621 memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400622 btrfs_set_header_bytenr(leaf, leaf->start);
623 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400624 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400625 btrfs_set_header_owner(leaf, objectid);
626
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400627 write_extent_buffer_fsid(leaf, fs_info->fsid);
628 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400629 btrfs_mark_buffer_dirty(leaf);
630
David Sterba49a3c4d2016-03-24 17:49:22 +0100631 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800632 btrfs_set_stack_inode_generation(inode_item, 1);
633 btrfs_set_stack_inode_size(inode_item, 3);
634 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400635 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400636 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800637 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400638
David Sterba49a3c4d2016-03-24 17:49:22 +0100639 btrfs_set_root_flags(root_item, 0);
640 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800641 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000642
David Sterba49a3c4d2016-03-24 17:49:22 +0100643 btrfs_set_root_bytenr(root_item, leaf->start);
644 btrfs_set_root_generation(root_item, trans->transid);
645 btrfs_set_root_level(root_item, 0);
646 btrfs_set_root_refs(root_item, 1);
647 btrfs_set_root_used(root_item, leaf->len);
648 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400649
David Sterba49a3c4d2016-03-24 17:49:22 +0100650 btrfs_set_root_generation_v2(root_item,
651 btrfs_root_generation(root_item));
Alexander Block8ea05e32012-07-25 17:35:53 +0200652 uuid_le_gen(&new_uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100653 memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE);
654 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
655 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
656 root_item->ctime = root_item->otime;
657 btrfs_set_root_ctransid(root_item, trans->transid);
658 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400659
Chris Mason925baed2008-06-25 16:01:30 -0400660 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400661 free_extent_buffer(leaf);
662 leaf = NULL;
663
David Sterba49a3c4d2016-03-24 17:49:22 +0100664 btrfs_set_root_dirid(root_item, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400665
666 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400667 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200668 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400669 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100670 root_item);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400671 if (ret)
672 goto fail;
673
Yan, Zheng76dda932009-09-21 16:00:26 -0400674 key.offset = (u64)-1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400675 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100676 if (IS_ERR(new_root)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100677 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400678 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100679 goto fail;
680 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400681
682 btrfs_record_root_in_trans(trans, new_root);
683
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000684 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700685 if (ret) {
686 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400687 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700688 goto fail;
689 }
690
Chandan Rajendraf32e48e2016-01-07 18:56:59 +0530691 mutex_lock(&new_root->objectid_mutex);
692 new_root->highest_objectid = new_dirid;
693 mutex_unlock(&new_root->objectid_mutex);
694
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400695 /*
696 * insert the directory item
697 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200698 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100699 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400700 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100701 goto fail;
702 }
Chris Mason3de45862008-11-17 21:02:50 -0500703
704 ret = btrfs_insert_dir_item(trans, root,
Nikolay Borisov8e7611c2017-02-20 13:50:31 +0200705 name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500706 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100707 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400708 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400709 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100710 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500711
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200712 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Yan Zheng52c26172009-01-05 15:43:43 -0500713 ret = btrfs_update_inode(trans, root, dir);
714 BUG_ON(ret);
715
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400716 ret = btrfs_add_root_ref(trans, fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400717 objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200718 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500719 BUG_ON(ret);
720
Lu Fengqicdb345a2018-05-29 15:01:53 +0800721 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400722 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200723 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400724 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200725
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400726fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100727 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000728 trans->block_rsv = NULL;
729 trans->bytes_reserved = 0;
David Sterba7775c812017-02-10 19:18:18 +0100730 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800731
Sage Weil72fd0322010-10-29 15:41:32 -0400732 if (async_transid) {
733 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400734 err = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000735 if (err)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400736 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400737 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400738 err = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400739 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400740 if (err && !ret)
741 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500742
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900743 if (!ret) {
744 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800745 if (IS_ERR(inode))
746 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900747 d_instantiate(dentry, inode);
748 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400749 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100750
751fail_free:
752 kfree(root_item);
753 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400754}
755
Miao Xiee9662f72013-02-28 10:01:15 +0000756static int create_snapshot(struct btrfs_root *root, struct inode *dir,
David Sterba61d7e4c2017-02-10 19:54:06 +0100757 struct dentry *dentry,
Miao Xiee9662f72013-02-28 10:01:15 +0000758 u64 *async_transid, bool readonly,
759 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400760{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400761 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000762 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400763 struct btrfs_pending_snapshot *pending_snapshot;
764 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000765 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400766
Miao Xie27cdeb72014-04-02 19:51:05 +0800767 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400768 return -EINVAL;
769
David Sterba23269bf2017-02-13 11:03:44 +0100770 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100771 if (!pending_snapshot)
772 return -ENOMEM;
773
David Sterbab0c0ea62015-11-10 18:54:00 +0100774 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100775 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100776 pending_snapshot->path = btrfs_alloc_path();
777 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100778 ret = -ENOMEM;
779 goto free_pending;
780 }
781
David Sterbaea14b57f2017-06-22 02:19:11 +0200782 atomic_inc(&root->will_be_snapshotted);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100783 smp_mb__after_atomic();
Liu Bo45bac0f2017-09-01 16:14:29 -0600784 /* wait for no snapshot writes */
785 wait_event(root->subv_writers->wait,
786 percpu_counter_sum(&root->subv_writers->counter) == 0);
Miao Xie8257b2d2014-03-06 13:38:19 +0800787
Nikolay Borisov76f32e22018-04-23 10:54:14 +0300788 ret = btrfs_start_delalloc_inodes(root);
Miao Xie6a038432013-05-15 07:48:24 +0000789 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100790 goto dec_and_free;
Miao Xie6a038432013-05-15 07:48:24 +0000791
Chris Mason6374e57a2017-06-23 09:48:21 -0700792 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Miao Xie6a038432013-05-15 07:48:24 +0000793
Miao Xie66d8f3d2012-09-06 04:02:28 -0600794 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
795 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000796 /*
797 * 1 - parent dir inode
798 * 2 - dir entries
799 * 1 - root item
800 * 2 - root ref/backref
801 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200802 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000803 */
804 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200805 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400806 &pending_snapshot->qgroup_reserved,
807 false);
Miao Xied5c12072013-02-28 10:04:33 +0000808 if (ret)
David Sterbaa1ee7362015-11-10 18:53:56 +0100809 goto dec_and_free;
Miao Xied5c12072013-02-28 10:04:33 +0000810
Yan, Zhenga22285a2010-05-16 10:48:46 -0400811 pending_snapshot->dentry = dentry;
812 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800813 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000814 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000815 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400816
Miao Xied5c12072013-02-28 10:04:33 +0000817 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400818 if (IS_ERR(trans)) {
819 ret = PTR_ERR(trans);
820 goto fail;
821 }
822
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400823 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400824 list_add(&pending_snapshot->list,
825 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400826 spin_unlock(&fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400827 if (async_transid) {
828 *async_transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400829 ret = btrfs_commit_transaction_async(trans, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000830 if (ret)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400831 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400832 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400833 ret = btrfs_commit_transaction(trans);
Sage Weil72fd0322010-10-29 15:41:32 -0400834 }
Miao Xieaec80302013-03-04 09:44:29 +0000835 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400836 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400837
838 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400839 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000840 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400841
Chris Masond3797302014-10-15 13:50:56 -0700842 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
843 if (ret)
844 goto fail;
845
David Howells2b0143b2015-03-17 22:25:59 +0000846 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000847 if (IS_ERR(inode)) {
848 ret = PTR_ERR(inode);
849 goto fail;
850 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900851
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000852 d_instantiate(dentry, inode);
853 ret = 0;
854fail:
David Sterba7775c812017-02-10 19:18:18 +0100855 btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv);
David Sterbaa1ee7362015-11-10 18:53:56 +0100856dec_and_free:
David Sterbaea14b57f2017-06-22 02:19:11 +0200857 if (atomic_dec_and_test(&root->will_be_snapshotted))
Peter Zijlstra46259562018-03-15 11:43:08 +0100858 wake_up_var(&root->will_be_snapshotted);
David Sterbab0c0ea62015-11-10 18:54:00 +0100859free_pending:
860 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100861 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100862 kfree(pending_snapshot);
863
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400864 return ret;
865}
866
Sage Weil4260f7c2010-10-29 15:46:43 -0400867/* copy of may_delete in fs/namei.c()
868 * Check whether we can remove a link victim from directory dir, check
869 * whether the type of victim is right.
870 * 1. We can't do it if dir is read-only (done in permission())
871 * 2. We should have write and exec permissions on dir
872 * 3. We can't remove anything from append-only dir
873 * 4. We can't do anything with immutable dir (done in permission())
874 * 5. If the sticky bit on dir is set we should either
875 * a. be owner of dir, or
876 * b. be owner of victim, or
877 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400878 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400879 * links pointing to it.
880 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
881 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
882 * 9. We can't remove a root or mountpoint.
883 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
884 * nfs_async_unlink().
885 */
886
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530887static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400888{
889 int error;
890
David Howells2b0143b2015-03-17 22:25:59 +0000891 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400892 return -ENOENT;
893
David Howells2b0143b2015-03-17 22:25:59 +0000894 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400895 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400896
897 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
898 if (error)
899 return error;
900 if (IS_APPEND(dir))
901 return -EPERM;
David Howells2b0143b2015-03-17 22:25:59 +0000902 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
903 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400904 return -EPERM;
905 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000906 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400907 return -ENOTDIR;
908 if (IS_ROOT(victim))
909 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000910 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400911 return -EISDIR;
912 if (IS_DEADDIR(dir))
913 return -ENOENT;
914 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
915 return -EBUSY;
916 return 0;
917}
918
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400919/* copy of may_create in fs/namei.c() */
920static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
921{
David Howells2b0143b2015-03-17 22:25:59 +0000922 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400923 return -EEXIST;
924 if (IS_DEADDIR(dir))
925 return -ENOENT;
926 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
927}
928
929/*
930 * Create a new subvolume below @parent. This is largely modeled after
931 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
932 * inside this filesystem so it's quite a bit simpler.
933 */
Al Viro92872092016-11-20 19:34:31 -0500934static noinline int btrfs_mksubvol(const struct path *parent,
David Sterba52f75f42017-02-14 18:33:53 +0100935 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400936 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200937 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000938 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400939{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400940 struct inode *dir = d_inode(parent->dentry);
941 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400942 struct dentry *dentry;
943 int error;
944
Al Viro00235412016-05-26 00:05:12 -0400945 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
946 if (error == -EINTR)
947 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400948
949 dentry = lookup_one_len(name, parent->dentry, namelen);
950 error = PTR_ERR(dentry);
951 if (IS_ERR(dentry))
952 goto out_unlock;
953
Yan, Zheng76dda932009-09-21 16:00:26 -0400954 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400955 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600956 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400957
Chris Mason9c520572012-12-17 14:26:57 -0500958 /*
959 * even if this name doesn't exist, we may get hash collisions.
960 * check for them now when we can safely fail
961 */
962 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
963 dir->i_ino, name,
964 namelen);
965 if (error)
966 goto out_dput;
967
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400968 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400969
970 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
971 goto out_up_read;
972
Chris Mason3de45862008-11-17 21:02:50 -0500973 if (snap_src) {
David Sterba61d7e4c2017-02-10 19:54:06 +0100974 error = create_snapshot(snap_src, dir, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200975 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500976 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000977 error = create_subvol(dir, dentry, name, namelen,
978 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500979 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400980 if (!error)
981 fsnotify_mkdir(dir, dentry);
982out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400983 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400984out_dput:
985 dput(dentry);
986out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500987 inode_unlock(dir);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400988 return error;
989}
990
Chris Mason4cb53002011-05-24 15:35:30 -0400991/*
992 * When we're defragging a range, we don't want to kick it off again
993 * if it is really just waiting for delalloc to send it down.
994 * If we find a nice big extent or delalloc range for the bytes in the
995 * file you want to defrag, we return 0 to let you know to skip this
996 * part of the file
997 */
David Sterbaaab110a2014-07-29 17:32:10 +0200998static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400999{
1000 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1001 struct extent_map *em = NULL;
1002 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1003 u64 end;
1004
1005 read_lock(&em_tree->lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001006 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
Chris Mason4cb53002011-05-24 15:35:30 -04001007 read_unlock(&em_tree->lock);
1008
1009 if (em) {
1010 end = extent_map_end(em);
1011 free_extent_map(em);
1012 if (end - offset > thresh)
1013 return 0;
1014 }
1015 /* if we already have a nice delalloc here, just stop */
1016 thresh /= 2;
1017 end = count_range_bits(io_tree, &offset, offset + thresh,
1018 thresh, EXTENT_DELALLOC, 1);
1019 if (end >= thresh)
1020 return 0;
1021 return 1;
1022}
1023
1024/*
1025 * helper function to walk through a file and find extents
1026 * newer than a specific transid, and smaller than thresh.
1027 *
1028 * This is used by the defragging code to find new and small
1029 * extents
1030 */
1031static int find_new_extents(struct btrfs_root *root,
1032 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001033 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001034{
1035 struct btrfs_path *path;
1036 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001037 struct extent_buffer *leaf;
1038 struct btrfs_file_extent_item *extent;
1039 int type;
1040 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001041 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001042
1043 path = btrfs_alloc_path();
1044 if (!path)
1045 return -ENOMEM;
1046
David Sterbaa4689d22011-05-31 17:08:14 +00001047 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001048 min_key.type = BTRFS_EXTENT_DATA_KEY;
1049 min_key.offset = *off;
1050
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301051 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001052 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001053 if (ret != 0)
1054 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001055process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001056 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001057 goto none;
1058 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1059 goto none;
1060
1061 leaf = path->nodes[0];
1062 extent = btrfs_item_ptr(leaf, path->slots[0],
1063 struct btrfs_file_extent_item);
1064
1065 type = btrfs_file_extent_type(leaf, extent);
1066 if (type == BTRFS_FILE_EXTENT_REG &&
1067 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1068 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1069 *off = min_key.offset;
1070 btrfs_free_path(path);
1071 return 0;
1072 }
1073
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001074 path->slots[0]++;
1075 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1076 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1077 goto process_slot;
1078 }
1079
Chris Mason4cb53002011-05-24 15:35:30 -04001080 if (min_key.offset == (u64)-1)
1081 goto none;
1082
1083 min_key.offset++;
1084 btrfs_release_path(path);
1085 }
1086none:
1087 btrfs_free_path(path);
1088 return -ENOENT;
1089}
1090
Li Zefan6c282eb2012-06-11 16:03:35 +08001091static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001092{
1093 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001094 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001095 struct extent_map *em;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001096 u64 len = PAGE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -05001097
1098 /*
1099 * hopefully we have this extent in the tree already, try without
1100 * the full extent lock
1101 */
1102 read_lock(&em_tree->lock);
1103 em = lookup_extent_mapping(em_tree, start, len);
1104 read_unlock(&em_tree->lock);
1105
1106 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001107 struct extent_state *cached = NULL;
1108 u64 end = start + len - 1;
1109
Chris Mason940100a2010-03-10 10:52:59 -05001110 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001111 lock_extent_bits(io_tree, start, end, &cached);
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02001112 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
David Sterbae43bbe52017-12-12 21:43:52 +01001113 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001114
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001115 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001116 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001117 }
1118
Li Zefan6c282eb2012-06-11 16:03:35 +08001119 return em;
1120}
1121
1122static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1123{
1124 struct extent_map *next;
1125 bool ret = true;
1126
1127 /* this is the last extent */
1128 if (em->start + em->len >= i_size_read(inode))
1129 return false;
1130
1131 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001132 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1133 ret = false;
1134 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001135 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001136 ret = false;
1137
1138 free_extent_map(next);
1139 return ret;
1140}
1141
David Sterbaaab110a2014-07-29 17:32:10 +02001142static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001143 u64 *last_len, u64 *skip, u64 *defrag_end,
1144 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001145{
1146 struct extent_map *em;
1147 int ret = 1;
1148 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001149 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001150
1151 /*
1152 * make sure that once we start defragging an extent, we keep on
1153 * defragging it
1154 */
1155 if (start < *defrag_end)
1156 return 1;
1157
1158 *skip = 0;
1159
1160 em = defrag_lookup_extent(inode, start);
1161 if (!em)
1162 return 0;
1163
Chris Mason940100a2010-03-10 10:52:59 -05001164 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001165 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001166 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001167 goto out;
1168 }
1169
Liu Bo4a3560c2015-08-07 16:48:41 +08001170 if (!*defrag_end)
1171 prev_mergeable = false;
1172
Li Zefan6c282eb2012-06-11 16:03:35 +08001173 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001174 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001175 * we hit a real extent, if it is big or the next extent is not a
1176 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001177 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001178 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001179 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001180 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001181out:
Chris Mason940100a2010-03-10 10:52:59 -05001182 /*
1183 * last_len ends up being a counter of how many bytes we've defragged.
1184 * every time we choose not to defrag an extent, we reset *last_len
1185 * so that the next tiny extent will force a defrag.
1186 *
1187 * The end result of this is that tiny extents before a single big
1188 * extent will force at least part of that big extent to be defragged.
1189 */
1190 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001191 *defrag_end = extent_map_end(em);
1192 } else {
1193 *last_len = 0;
1194 *skip = extent_map_end(em);
1195 *defrag_end = 0;
1196 }
1197
1198 free_extent_map(em);
1199 return ret;
1200}
1201
Chris Mason4cb53002011-05-24 15:35:30 -04001202/*
1203 * it doesn't do much good to defrag one or two pages
1204 * at a time. This pulls in a nice chunk of pages
1205 * to COW and defrag.
1206 *
1207 * It also makes sure the delalloc code has enough
1208 * dirty data to avoid making new small extents as part
1209 * of the defrag
1210 *
1211 * It's a good idea to start RA on this range
1212 * before calling this.
1213 */
1214static int cluster_pages_for_defrag(struct inode *inode,
1215 struct page **pages,
1216 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001217 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001218{
Chris Mason4cb53002011-05-24 15:35:30 -04001219 unsigned long file_end;
1220 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001221 u64 page_start;
1222 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001223 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001224 int ret;
1225 int i;
1226 int i_done;
1227 struct btrfs_ordered_extent *ordered;
1228 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001229 struct extent_io_tree *tree;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001230 struct extent_changeset *data_reserved = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001231 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001232
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001233 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001234 if (!isize || start_index > file_end)
1235 return 0;
1236
1237 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001238
Qu Wenruo364ecf32017-02-27 15:10:38 +08001239 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001240 start_index << PAGE_SHIFT,
1241 page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001242 if (ret)
1243 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001244 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001245 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001246
1247 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001248 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001249 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001250again:
Josef Bacika94733d2011-07-11 10:47:06 -04001251 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001252 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001253 if (!page)
1254 break;
1255
Miao Xie600a45e2012-02-16 15:01:24 +08001256 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001257 page_end = page_start + PAGE_SIZE - 1;
Miao Xie600a45e2012-02-16 15:01:24 +08001258 while (1) {
Filipe Manana308d9802014-03-11 13:56:15 +00001259 lock_extent_bits(tree, page_start, page_end,
David Sterbaff13db42015-12-03 14:30:40 +01001260 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001261 ordered = btrfs_lookup_ordered_extent(inode,
1262 page_start);
Filipe Manana308d9802014-03-11 13:56:15 +00001263 unlock_extent_cached(tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01001264 &cached_state);
Miao Xie600a45e2012-02-16 15:01:24 +08001265 if (!ordered)
1266 break;
1267
1268 unlock_page(page);
1269 btrfs_start_ordered_extent(inode, ordered, 1);
1270 btrfs_put_ordered_extent(ordered);
1271 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001272 /*
1273 * we unlocked the page above, so we need check if
1274 * it was released or not.
1275 */
1276 if (page->mapping != inode->i_mapping) {
1277 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001278 put_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001279 goto again;
1280 }
Miao Xie600a45e2012-02-16 15:01:24 +08001281 }
1282
Chris Mason4cb53002011-05-24 15:35:30 -04001283 if (!PageUptodate(page)) {
1284 btrfs_readpage(NULL, page);
1285 lock_page(page);
1286 if (!PageUptodate(page)) {
1287 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001288 put_page(page);
Chris Mason4cb53002011-05-24 15:35:30 -04001289 ret = -EIO;
1290 break;
1291 }
1292 }
Miao Xie600a45e2012-02-16 15:01:24 +08001293
Miao Xie600a45e2012-02-16 15:01:24 +08001294 if (page->mapping != inode->i_mapping) {
1295 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001296 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001297 goto again;
1298 }
1299
Chris Mason4cb53002011-05-24 15:35:30 -04001300 pages[i] = page;
1301 i_done++;
1302 }
1303 if (!i_done || ret)
1304 goto out;
1305
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001306 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001307 goto out;
1308
1309 /*
1310 * so now we have a nice long stream of locked
1311 * and up to date pages, lets wait on them
1312 */
1313 for (i = 0; i < i_done; i++)
1314 wait_on_page_writeback(pages[i]);
1315
1316 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001317 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001318
1319 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001320 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001321 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1322 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001323 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01001324 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001325
Liu Bo1f12bd02012-03-29 09:57:44 -04001326 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001327 spin_lock(&BTRFS_I(inode)->lock);
1328 BTRFS_I(inode)->outstanding_extents++;
1329 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08001330 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001331 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001332 (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001333 }
1334
1335
Liu Bo9e8a4a82012-09-05 19:10:51 -06001336 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001337 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001338
1339 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001340 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001341
1342 for (i = 0; i < i_done; i++) {
1343 clear_page_dirty_for_io(pages[i]);
1344 ClearPageChecked(pages[i]);
1345 set_page_extent_mapped(pages[i]);
1346 set_page_dirty(pages[i]);
1347 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001348 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001349 }
Qu Wenruo43b18592017-12-12 15:34:32 +08001350 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1351 false);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001352 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001353 return i_done;
1354out:
1355 for (i = 0; i < i_done; i++) {
1356 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001357 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001358 }
Qu Wenruobc42bda2017-02-27 15:10:39 +08001359 btrfs_delalloc_release_space(inode, data_reserved,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001360 start_index << PAGE_SHIFT,
Qu Wenruo43b18592017-12-12 15:34:32 +08001361 page_cnt << PAGE_SHIFT, true);
1362 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT,
1363 true);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001364 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001365 return ret;
1366
1367}
1368
1369int btrfs_defrag_file(struct inode *inode, struct file *file,
1370 struct btrfs_ioctl_defrag_range_args *range,
1371 u64 newer_than, unsigned long max_to_defrag)
1372{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001373 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001374 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001375 struct file_ra_state *ra = NULL;
1376 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001377 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001378 u64 last_len = 0;
1379 u64 skip = 0;
1380 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001381 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001382 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001383 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001384 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001385 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001386 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001387 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001388 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001389 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001390 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001391 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001392 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Chris Mason4cb53002011-05-24 15:35:30 -04001393
Liu Bo0abd5b12013-04-16 09:20:28 +00001394 if (isize == 0)
1395 return 0;
1396
1397 if (range->start >= isize)
1398 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001399
David Sterba1e2ef462017-07-17 20:01:59 +02001400 if (do_compress) {
Li Zefan1a419d82010-10-25 15:12:50 +08001401 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1402 return -EINVAL;
1403 if (range->compress_type)
1404 compress_type = range->compress_type;
1405 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001406
Liu Bo0abd5b12013-04-16 09:20:28 +00001407 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001408 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001409
Chris Mason4cb53002011-05-24 15:35:30 -04001410 /*
David Sterba0a52d102017-06-22 03:22:58 +02001411 * If we were not given a file, allocate a readahead context. As
1412 * readahead is just an optimization, defrag will work without it so
1413 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001414 */
1415 if (!file) {
David Sterba63e727e2017-06-22 03:13:02 +02001416 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001417 if (ra)
1418 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001419 } else {
1420 ra = &file->f_ra;
1421 }
1422
David Sterba63e727e2017-06-22 03:13:02 +02001423 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001424 if (!pages) {
1425 ret = -ENOMEM;
1426 goto out_ra;
1427 }
1428
1429 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001430 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001431 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001432 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001433 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001434 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001435 }
1436
Chris Mason4cb53002011-05-24 15:35:30 -04001437 if (newer_than) {
1438 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001439 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001440 if (!ret) {
1441 range->start = newer_off;
1442 /*
1443 * we always align our defrag to help keep
1444 * the extents in the file evenly spaced
1445 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001446 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001447 } else
1448 goto out_ra;
1449 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001450 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001451 }
1452 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301453 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001454
Li Zefan2a0f7f52011-10-10 15:43:34 -04001455 /*
1456 * make writeback starts from i, so the defrag range can be
1457 * written sequentially.
1458 */
1459 if (i < inode->i_mapping->writeback_index)
1460 inode->i_mapping->writeback_index = i;
1461
Chris Masonf7f43cc2011-10-11 11:41:40 -04001462 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001463 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001464 /*
1465 * make sure we stop running if someone unmounts
1466 * the FS
1467 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001468 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001469 break;
1470
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001471 if (btrfs_defrag_cancelled(fs_info)) {
1472 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001473 ret = -EAGAIN;
1474 break;
1475 }
1476
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001477 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001478 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001479 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001480 unsigned long next;
1481 /*
1482 * the should_defrag function tells us how much to skip
1483 * bump our counter by the suggested amount
1484 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001485 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001486 i = max(i + 1, next);
1487 continue;
1488 }
Li Zefan008873e2011-09-02 15:57:07 +08001489
1490 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001491 cluster = (PAGE_ALIGN(defrag_end) >>
1492 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001493 cluster = min(cluster, max_cluster);
1494 } else {
1495 cluster = max_cluster;
1496 }
1497
Li Zefan008873e2011-09-02 15:57:07 +08001498 if (i + cluster > ra_index) {
1499 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001500 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001501 page_cache_sync_readahead(inode->i_mapping, ra,
1502 file, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301503 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001504 }
Chris Mason940100a2010-03-10 10:52:59 -05001505
Al Viro59551022016-01-22 15:40:57 -05001506 inode_lock(inode);
David Sterba1e2ef462017-07-17 20:01:59 +02001507 if (do_compress)
David Sterbaeec63c62017-07-17 19:41:31 +02001508 BTRFS_I(inode)->defrag_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001509 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001510 if (ret < 0) {
Al Viro59551022016-01-22 15:40:57 -05001511 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001512 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001513 }
Chris Mason940100a2010-03-10 10:52:59 -05001514
Chris Mason4cb53002011-05-24 15:35:30 -04001515 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001516 balance_dirty_pages_ratelimited(inode->i_mapping);
Al Viro59551022016-01-22 15:40:57 -05001517 inode_unlock(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001518
1519 if (newer_than) {
1520 if (newer_off == (u64)-1)
1521 break;
1522
Liu Boe1f041e12012-03-29 09:57:45 -04001523 if (ret > 0)
1524 i += ret;
1525
Chris Mason4cb53002011-05-24 15:35:30 -04001526 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001527 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001528
Byongho Leeee221842015-12-15 01:42:10 +09001529 ret = find_new_extents(root, inode, newer_than,
1530 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001531 if (!ret) {
1532 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001533 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001534 } else {
1535 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001536 }
Chris Mason4cb53002011-05-24 15:35:30 -04001537 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001538 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001539 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001540 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001541 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001542 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001543 last_len = 0;
1544 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001545 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001546 }
1547
Filipe Mananadec8ef92014-03-01 10:55:54 +00001548 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001549 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001550 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1551 &BTRFS_I(inode)->runtime_flags))
1552 filemap_flush(inode->i_mapping);
1553 }
Chris Mason1e701a32010-03-11 09:42:04 -05001554
Li Zefan1a419d82010-10-25 15:12:50 +08001555 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001556 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001557 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1558 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001559 }
1560
Diego Calleja60ccf822011-09-01 16:33:57 +02001561 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001562
Chris Mason4cb53002011-05-24 15:35:30 -04001563out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001564 if (do_compress) {
Al Viro59551022016-01-22 15:40:57 -05001565 inode_lock(inode);
David Sterbaeec63c62017-07-17 19:41:31 +02001566 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Al Viro59551022016-01-22 15:40:57 -05001567 inode_unlock(inode);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001568 }
Chris Mason4cb53002011-05-24 15:35:30 -04001569 if (!file)
1570 kfree(ra);
1571 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001572 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001573}
1574
Miao Xie198605a2012-11-26 08:43:45 +00001575static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001576 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001577{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001578 struct inode *inode = file_inode(file);
1579 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001580 u64 new_size;
1581 u64 old_size;
1582 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001583 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001584 struct btrfs_ioctl_vol_args *vol_args;
1585 struct btrfs_trans_handle *trans;
1586 struct btrfs_device *device = NULL;
1587 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001588 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001589 char *devstr = NULL;
1590 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001591 int mod = 0;
1592
Chris Masone441d542009-01-05 16:57:23 -05001593 if (!capable(CAP_SYS_ADMIN))
1594 return -EPERM;
1595
Miao Xie198605a2012-11-26 08:43:45 +00001596 ret = mnt_want_write_file(file);
1597 if (ret)
1598 return ret;
1599
David Sterba171938e2017-03-28 14:44:21 +02001600 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Miao Xie97547672012-12-21 10:38:50 +00001601 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001602 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001603 }
1604
Li Zefandae7b662009-04-08 15:06:54 +08001605 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001606 if (IS_ERR(vol_args)) {
1607 ret = PTR_ERR(vol_args);
1608 goto out;
1609 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001610
1611 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001612
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001613 sizestr = vol_args->name;
1614 devstr = strchr(sizestr, ':');
1615 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001616 sizestr = devstr + 1;
1617 *devstr = '\0';
1618 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001619 ret = kstrtoull(devstr, 10, &devid);
1620 if (ret)
1621 goto out_free;
Miao Xiedfd79822012-12-21 09:21:30 +00001622 if (!devid) {
1623 ret = -EINVAL;
1624 goto out_free;
1625 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001626 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001627 }
Miao Xiedba60f32012-12-21 09:19:51 +00001628
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001629 device = btrfs_find_device(fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001630 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001631 btrfs_info(fs_info, "resizer unable to find device %llu",
1632 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001633 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001634 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001635 }
Miao Xiedba60f32012-12-21 09:19:51 +00001636
Anand Jainebbede42017-12-04 12:54:52 +08001637 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001638 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001639 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001640 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001641 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001642 goto out_free;
1643 }
1644
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001645 if (!strcmp(sizestr, "max"))
1646 new_size = device->bdev->bd_inode->i_size;
1647 else {
1648 if (sizestr[0] == '-') {
1649 mod = -1;
1650 sizestr++;
1651 } else if (sizestr[0] == '+') {
1652 mod = 1;
1653 sizestr++;
1654 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001655 new_size = memparse(sizestr, &retptr);
1656 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001657 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001658 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001659 }
1660 }
1661
Anand Jain401e29c2017-12-04 12:54:55 +08001662 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001663 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001664 goto out_free;
1665 }
1666
Miao Xie7cc8e582014-09-03 21:35:38 +08001667 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001668
1669 if (mod < 0) {
1670 if (new_size > old_size) {
1671 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001672 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001673 }
1674 new_size = old_size - new_size;
1675 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001676 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001677 ret = -ERANGE;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001678 goto out_free;
1679 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001680 new_size = old_size + new_size;
1681 }
1682
Byongho Leeee221842015-12-15 01:42:10 +09001683 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001684 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001685 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001686 }
1687 if (new_size > device->bdev->bd_inode->i_size) {
1688 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001689 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001690 }
1691
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001692 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001693
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001694 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1695 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001696
1697 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001698 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001699 if (IS_ERR(trans)) {
1700 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001701 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001702 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001703 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001704 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001705 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001706 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001707 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001708
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001709out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001710 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001711out:
David Sterba171938e2017-03-28 14:44:21 +02001712 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001713 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001714 return ret;
1715}
1716
Sage Weil72fd0322010-10-29 15:41:32 -04001717static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
David Sterba52f75f42017-02-14 18:33:53 +01001718 const char *name, unsigned long fd, int subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001719 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001720 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001721{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001722 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001723 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001724
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001725 if (!S_ISDIR(file_inode(file)->i_mode))
1726 return -ENOTDIR;
1727
Liu Boa874a632012-06-29 03:58:46 -06001728 ret = mnt_want_write_file(file);
1729 if (ret)
1730 goto out;
1731
Sage Weil72fd0322010-10-29 15:41:32 -04001732 namelen = strlen(name);
1733 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001734 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001735 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001736 }
1737
Chris Mason16780ca2012-02-20 22:14:55 -05001738 if (name[0] == '.' &&
1739 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1740 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001741 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001742 }
1743
Chris Mason3de45862008-11-17 21:02:50 -05001744 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001745 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001746 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001747 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001748 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001749 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001750 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001751 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001752 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001753 }
1754
Al Viro496ad9a2013-01-23 17:07:38 -05001755 src_inode = file_inode(src.file);
1756 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001757 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001758 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001759 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001760 } else if (!inode_owner_or_capable(src_inode)) {
1761 /*
1762 * Subvolume creation is not restricted, but snapshots
1763 * are limited to own subvolumes only
1764 */
1765 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001766 } else {
1767 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1768 BTRFS_I(src_inode)->root,
1769 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001770 }
Al Viro2903ff02012-08-28 12:52:22 -04001771 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001772 }
Liu Boa874a632012-06-29 03:58:46 -06001773out_drop_write:
1774 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001775out:
Sage Weil72fd0322010-10-29 15:41:32 -04001776 return ret;
1777}
1778
1779static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001780 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001781{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001782 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001783 int ret;
1784
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001785 if (!S_ISDIR(file_inode(file)->i_mode))
1786 return -ENOTDIR;
1787
Li Zefanfa0d2b92010-12-20 15:53:28 +08001788 vol_args = memdup_user(arg, sizeof(*vol_args));
1789 if (IS_ERR(vol_args))
1790 return PTR_ERR(vol_args);
1791 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001792
Li Zefanfa0d2b92010-12-20 15:53:28 +08001793 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001794 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001795 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001796
Li Zefanfa0d2b92010-12-20 15:53:28 +08001797 kfree(vol_args);
1798 return ret;
1799}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001800
Li Zefanfa0d2b92010-12-20 15:53:28 +08001801static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1802 void __user *arg, int subvol)
1803{
1804 struct btrfs_ioctl_vol_args_v2 *vol_args;
1805 int ret;
1806 u64 transid = 0;
1807 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001808 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001809 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001810
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001811 if (!S_ISDIR(file_inode(file)->i_mode))
1812 return -ENOTDIR;
1813
Li Zefanfa0d2b92010-12-20 15:53:28 +08001814 vol_args = memdup_user(arg, sizeof(*vol_args));
1815 if (IS_ERR(vol_args))
1816 return PTR_ERR(vol_args);
1817 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001818
Li Zefanb83cc962010-12-20 16:04:08 +08001819 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001820 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1821 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001822 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001823 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001824 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001825
1826 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1827 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001828 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1829 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001830 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001831 if (vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001832 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001833 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001834 }
1835 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1836 if (IS_ERR(inherit)) {
1837 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001838 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001839 }
1840 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001841
1842 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001843 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001844 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001845 if (ret)
1846 goto free_inherit;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001847
Dan Carpenterc47ca322014-09-04 14:09:15 +03001848 if (ptr && copy_to_user(arg +
1849 offsetof(struct btrfs_ioctl_vol_args_v2,
1850 transid),
1851 ptr, sizeof(*ptr)))
Li Zefanfa0d2b92010-12-20 15:53:28 +08001852 ret = -EFAULT;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001853
1854free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001855 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001856free_args:
1857 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001858 return ret;
1859}
1860
Li Zefan0caa1022010-12-20 16:30:25 +08001861static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1862 void __user *arg)
1863{
Al Viro496ad9a2013-01-23 17:07:38 -05001864 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001865 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001866 struct btrfs_root *root = BTRFS_I(inode)->root;
1867 int ret = 0;
1868 u64 flags = 0;
1869
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001870 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001871 return -EINVAL;
1872
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001874 if (btrfs_root_readonly(root))
1875 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001876 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001877
1878 if (copy_to_user(arg, &flags, sizeof(flags)))
1879 ret = -EFAULT;
1880
1881 return ret;
1882}
1883
1884static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1885 void __user *arg)
1886{
Al Viro496ad9a2013-01-23 17:07:38 -05001887 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001888 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001889 struct btrfs_root *root = BTRFS_I(inode)->root;
1890 struct btrfs_trans_handle *trans;
1891 u64 root_flags;
1892 u64 flags;
1893 int ret = 0;
1894
David Sterbabd60ea02014-01-16 15:50:22 +01001895 if (!inode_owner_or_capable(inode))
1896 return -EPERM;
1897
Liu Bob9ca0662012-06-29 03:58:49 -06001898 ret = mnt_want_write_file(file);
1899 if (ret)
1900 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001901
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001902 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001903 ret = -EINVAL;
1904 goto out_drop_write;
1905 }
Li Zefan0caa1022010-12-20 16:30:25 +08001906
Liu Bob9ca0662012-06-29 03:58:49 -06001907 if (copy_from_user(&flags, arg, sizeof(flags))) {
1908 ret = -EFAULT;
1909 goto out_drop_write;
1910 }
Li Zefan0caa1022010-12-20 16:30:25 +08001911
Liu Bob9ca0662012-06-29 03:58:49 -06001912 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1913 ret = -EINVAL;
1914 goto out_drop_write;
1915 }
Li Zefan0caa1022010-12-20 16:30:25 +08001916
Liu Bob9ca0662012-06-29 03:58:49 -06001917 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1918 ret = -EOPNOTSUPP;
1919 goto out_drop_write;
1920 }
Li Zefan0caa1022010-12-20 16:30:25 +08001921
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001922 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001923
1924 /* nothing to do */
1925 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001926 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001927
1928 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001929 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001930 btrfs_set_root_flags(&root->root_item,
1931 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001932 } else {
1933 /*
1934 * Block RO -> RW transition if this subvolume is involved in
1935 * send
1936 */
1937 spin_lock(&root->root_item_lock);
1938 if (root->send_in_progress == 0) {
1939 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001940 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001941 spin_unlock(&root->root_item_lock);
1942 } else {
1943 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001944 btrfs_warn(fs_info,
1945 "Attempt to set subvolume %llu read-write during send",
1946 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001947 ret = -EPERM;
1948 goto out_drop_sem;
1949 }
1950 }
Li Zefan0caa1022010-12-20 16:30:25 +08001951
1952 trans = btrfs_start_transaction(root, 1);
1953 if (IS_ERR(trans)) {
1954 ret = PTR_ERR(trans);
1955 goto out_reset;
1956 }
1957
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001958 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001959 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001960 if (ret < 0) {
1961 btrfs_end_transaction(trans);
1962 goto out_reset;
1963 }
Li Zefan0caa1022010-12-20 16:30:25 +08001964
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03001965 ret = btrfs_commit_transaction(trans);
1966
Li Zefan0caa1022010-12-20 16:30:25 +08001967out_reset:
1968 if (ret)
1969 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001970out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001971 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001972out_drop_write:
1973 mnt_drop_write_file(file);
1974out:
Li Zefan0caa1022010-12-20 16:30:25 +08001975 return ret;
1976}
1977
Chris Masonac8e9812010-02-28 15:39:26 -05001978static noinline int key_in_sk(struct btrfs_key *key,
1979 struct btrfs_ioctl_search_key *sk)
1980{
Chris Masonabc6e132010-03-18 12:10:08 -04001981 struct btrfs_key test;
1982 int ret;
1983
1984 test.objectid = sk->min_objectid;
1985 test.type = sk->min_type;
1986 test.offset = sk->min_offset;
1987
1988 ret = btrfs_comp_cpu_keys(key, &test);
1989 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001990 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001991
1992 test.objectid = sk->max_objectid;
1993 test.type = sk->max_type;
1994 test.offset = sk->max_offset;
1995
1996 ret = btrfs_comp_cpu_keys(key, &test);
1997 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001998 return 0;
1999 return 1;
2000}
2001
Jeff Mahoneydf397562016-06-21 20:18:21 -04002002static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002003 struct btrfs_key *key,
2004 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002005 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002006 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002007 unsigned long *sk_offset,
2008 int *num_found)
2009{
2010 u64 found_transid;
2011 struct extent_buffer *leaf;
2012 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002013 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002014 unsigned long item_off;
2015 unsigned long item_len;
2016 int nritems;
2017 int i;
2018 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002019 int ret = 0;
2020
2021 leaf = path->nodes[0];
2022 slot = path->slots[0];
2023 nritems = btrfs_header_nritems(leaf);
2024
2025 if (btrfs_header_generation(leaf) > sk->max_transid) {
2026 i = nritems;
2027 goto advance_key;
2028 }
2029 found_transid = btrfs_header_generation(leaf);
2030
2031 for (i = slot; i < nritems; i++) {
2032 item_off = btrfs_item_ptr_offset(leaf, i);
2033 item_len = btrfs_item_size_nr(leaf, i);
2034
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002035 btrfs_item_key_to_cpu(leaf, key, i);
2036 if (!key_in_sk(key, sk))
2037 continue;
2038
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002039 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002040 if (*num_found) {
2041 ret = 1;
2042 goto out;
2043 }
Chris Masonac8e9812010-02-28 15:39:26 -05002044
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002045 /*
2046 * return one empty item back for v1, which does not
2047 * handle -EOVERFLOW
2048 */
2049
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002050 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002051 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002052 ret = -EOVERFLOW;
2053 }
Chris Masonac8e9812010-02-28 15:39:26 -05002054
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002055 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002056 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002057 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002058 }
2059
Chris Masonac8e9812010-02-28 15:39:26 -05002060 sh.objectid = key->objectid;
2061 sh.offset = key->offset;
2062 sh.type = key->type;
2063 sh.len = item_len;
2064 sh.transid = found_transid;
2065
2066 /* copy search result header */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002067 if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
2068 ret = -EFAULT;
2069 goto out;
2070 }
2071
Chris Masonac8e9812010-02-28 15:39:26 -05002072 *sk_offset += sizeof(sh);
2073
2074 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002075 char __user *up = ubuf + *sk_offset;
Chris Masonac8e9812010-02-28 15:39:26 -05002076 /* copy the item */
Gerhard Heiftba346b32014-01-30 16:24:02 +01002077 if (read_extent_buffer_to_user(leaf, up,
2078 item_off, item_len)) {
2079 ret = -EFAULT;
2080 goto out;
2081 }
2082
Chris Masonac8e9812010-02-28 15:39:26 -05002083 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002084 }
Hugo Millse2156862011-05-14 17:43:41 +00002085 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002086
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002087 if (ret) /* -EOVERFLOW from above */
2088 goto out;
2089
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002090 if (*num_found >= sk->nr_items) {
2091 ret = 1;
2092 goto out;
2093 }
Chris Masonac8e9812010-02-28 15:39:26 -05002094 }
2095advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002096 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002097 test.objectid = sk->max_objectid;
2098 test.type = sk->max_type;
2099 test.offset = sk->max_offset;
2100 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2101 ret = 1;
2102 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002103 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002104 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002105 key->offset = 0;
2106 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002107 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002108 key->offset = 0;
2109 key->type = 0;
2110 key->objectid++;
2111 } else
2112 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002113out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002114 /*
2115 * 0: all items from this leaf copied, continue with next
2116 * 1: * more items can be copied, but unused buffer is too small
2117 * * all items were found
2118 * Either way, it will stops the loop which iterates to the next
2119 * leaf
2120 * -EOVERFLOW: item was to large for buffer
2121 * -EFAULT: could not copy extent buffer back to userspace
2122 */
Chris Masonac8e9812010-02-28 15:39:26 -05002123 return ret;
2124}
2125
2126static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002127 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002128 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002129 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002130{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002131 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002132 struct btrfs_root *root;
2133 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002134 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002135 int ret;
2136 int num_found = 0;
2137 unsigned long sk_offset = 0;
2138
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002139 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2140 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002141 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002142 }
Gerhard Heift12544442014-01-30 16:23:58 +01002143
Chris Masonac8e9812010-02-28 15:39:26 -05002144 path = btrfs_alloc_path();
2145 if (!path)
2146 return -ENOMEM;
2147
2148 if (sk->tree_id == 0) {
2149 /* search the root of the inode that was passed */
2150 root = BTRFS_I(inode)->root;
2151 } else {
2152 key.objectid = sk->tree_id;
2153 key.type = BTRFS_ROOT_ITEM_KEY;
2154 key.offset = (u64)-1;
2155 root = btrfs_read_fs_root_no_name(info, &key);
2156 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002157 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002158 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002159 }
2160 }
2161
2162 key.objectid = sk->min_objectid;
2163 key.type = sk->min_type;
2164 key.offset = sk->min_offset;
2165
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302166 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002167 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002168 if (ret != 0) {
2169 if (ret > 0)
2170 ret = 0;
2171 goto err;
2172 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002173 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002174 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002175 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002176 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002177 break;
2178
2179 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002180 if (ret > 0)
2181 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002182err:
2183 sk->nr_items = num_found;
2184 btrfs_free_path(path);
2185 return ret;
2186}
2187
2188static noinline int btrfs_ioctl_tree_search(struct file *file,
2189 void __user *argp)
2190{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002191 struct btrfs_ioctl_search_args __user *uargs;
2192 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002193 struct inode *inode;
2194 int ret;
2195 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002196
2197 if (!capable(CAP_SYS_ADMIN))
2198 return -EPERM;
2199
Gerhard Heiftba346b32014-01-30 16:24:02 +01002200 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002201
Gerhard Heiftba346b32014-01-30 16:24:02 +01002202 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2203 return -EFAULT;
2204
2205 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002206
Al Viro496ad9a2013-01-23 17:07:38 -05002207 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002208 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002209
2210 /*
2211 * In the origin implementation an overflow is handled by returning a
2212 * search header with a len of zero, so reset ret.
2213 */
2214 if (ret == -EOVERFLOW)
2215 ret = 0;
2216
Gerhard Heiftba346b32014-01-30 16:24:02 +01002217 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002218 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002219 return ret;
2220}
2221
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002222static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2223 void __user *argp)
2224{
2225 struct btrfs_ioctl_search_args_v2 __user *uarg;
2226 struct btrfs_ioctl_search_args_v2 args;
2227 struct inode *inode;
2228 int ret;
2229 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002230 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002231
2232 if (!capable(CAP_SYS_ADMIN))
2233 return -EPERM;
2234
2235 /* copy search header and buffer size */
2236 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2237 if (copy_from_user(&args, uarg, sizeof(args)))
2238 return -EFAULT;
2239
2240 buf_size = args.buf_size;
2241
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002242 /* limit result size to 16MB */
2243 if (buf_size > buf_limit)
2244 buf_size = buf_limit;
2245
2246 inode = file_inode(file);
2247 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002248 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002249 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2250 ret = -EFAULT;
2251 else if (ret == -EOVERFLOW &&
2252 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2253 ret = -EFAULT;
2254
Yan, Zheng76dda932009-09-21 16:00:26 -04002255 return ret;
2256}
2257
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002258/*
Chris Masonac8e9812010-02-28 15:39:26 -05002259 * Search INODE_REFs to identify path name of 'dirid' directory
2260 * in a 'tree_id' tree. and sets path name to 'name'.
2261 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002262static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2263 u64 tree_id, u64 dirid, char *name)
2264{
2265 struct btrfs_root *root;
2266 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002267 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002268 int ret = -1;
2269 int slot;
2270 int len;
2271 int total_len = 0;
2272 struct btrfs_inode_ref *iref;
2273 struct extent_buffer *l;
2274 struct btrfs_path *path;
2275
2276 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2277 name[0]='\0';
2278 return 0;
2279 }
2280
2281 path = btrfs_alloc_path();
2282 if (!path)
2283 return -ENOMEM;
2284
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002285 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002286
2287 key.objectid = tree_id;
2288 key.type = BTRFS_ROOT_ITEM_KEY;
2289 key.offset = (u64)-1;
2290 root = btrfs_read_fs_root_no_name(info, &key);
2291 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002292 ret = PTR_ERR(root);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002293 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002294 }
2295
2296 key.objectid = dirid;
2297 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002298 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002299
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302300 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002301 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2302 if (ret < 0)
2303 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002304 else if (ret > 0) {
2305 ret = btrfs_previous_item(root, path, dirid,
2306 BTRFS_INODE_REF_KEY);
2307 if (ret < 0)
2308 goto out;
2309 else if (ret > 0) {
2310 ret = -ENOENT;
2311 goto out;
2312 }
2313 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002314
2315 l = path->nodes[0];
2316 slot = path->slots[0];
2317 btrfs_item_key_to_cpu(l, &key, slot);
2318
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002319 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2320 len = btrfs_inode_ref_name_len(l, iref);
2321 ptr -= len + 1;
2322 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002323 if (ptr < name) {
2324 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002325 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002326 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002327
2328 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302329 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002330
2331 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2332 break;
2333
David Sterbab3b4aa72011-04-21 01:20:15 +02002334 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002335 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002336 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002337 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002338 }
Li Zefan77906a502011-07-14 03:16:00 +00002339 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302340 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002341 ret = 0;
2342out:
2343 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002344 return ret;
2345}
2346
2347static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2348 void __user *argp)
2349{
2350 struct btrfs_ioctl_ino_lookup_args *args;
2351 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002352 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002353
Julia Lawall2354d082010-10-29 15:14:18 -04002354 args = memdup_user(argp, sizeof(*args));
2355 if (IS_ERR(args))
2356 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002357
Al Viro496ad9a2013-01-23 17:07:38 -05002358 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002359
David Sterba01b810b2015-05-12 19:14:49 +02002360 /*
2361 * Unprivileged query to obtain the containing subvolume root id. The
2362 * path is reset so it's consistent with btrfs_search_path_in_tree.
2363 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002364 if (args->treeid == 0)
2365 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2366
David Sterba01b810b2015-05-12 19:14:49 +02002367 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2368 args->name[0] = 0;
2369 goto out;
2370 }
2371
2372 if (!capable(CAP_SYS_ADMIN)) {
2373 ret = -EPERM;
2374 goto out;
2375 }
2376
Chris Masonac8e9812010-02-28 15:39:26 -05002377 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2378 args->treeid, args->objectid,
2379 args->name);
2380
David Sterba01b810b2015-05-12 19:14:49 +02002381out:
Chris Masonac8e9812010-02-28 15:39:26 -05002382 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2383 ret = -EFAULT;
2384
2385 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002386 return ret;
2387}
2388
Yan, Zheng76dda932009-09-21 16:00:26 -04002389static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2390 void __user *arg)
2391{
Al Viro54563d42013-09-01 15:57:51 -04002392 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002393 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002394 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002395 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002396 struct inode *inode;
2397 struct btrfs_root *root = BTRFS_I(dir)->root;
2398 struct btrfs_root *dest = NULL;
2399 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002400 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002401 int err = 0;
2402
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002403 if (!S_ISDIR(dir->i_mode))
2404 return -ENOTDIR;
2405
Yan, Zheng76dda932009-09-21 16:00:26 -04002406 vol_args = memdup_user(arg, sizeof(*vol_args));
2407 if (IS_ERR(vol_args))
2408 return PTR_ERR(vol_args);
2409
2410 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2411 namelen = strlen(vol_args->name);
2412 if (strchr(vol_args->name, '/') ||
2413 strncmp(vol_args->name, "..", namelen) == 0) {
2414 err = -EINVAL;
2415 goto out;
2416 }
2417
Al Viroa561be72011-11-23 11:57:51 -05002418 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002419 if (err)
2420 goto out;
2421
David Sterba521e0542014-04-15 16:41:44 +02002422
Al Viro00235412016-05-26 00:05:12 -04002423 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2424 if (err == -EINTR)
2425 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002426 dentry = lookup_one_len(vol_args->name, parent, namelen);
2427 if (IS_ERR(dentry)) {
2428 err = PTR_ERR(dentry);
2429 goto out_unlock_dir;
2430 }
2431
David Howells2b0143b2015-03-17 22:25:59 +00002432 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002433 err = -ENOENT;
2434 goto out_dput;
2435 }
2436
David Howells2b0143b2015-03-17 22:25:59 +00002437 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002438 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302439 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002440 /*
2441 * Regular user. Only allow this with a special mount
2442 * option, when the user has write+exec access to the
2443 * subvol root, and when rmdir(2) would have been
2444 * allowed.
2445 *
2446 * Note that this is _not_ check that the subvol is
2447 * empty or doesn't contain data that we wouldn't
2448 * otherwise be able to delete.
2449 *
2450 * Users who want to delete empty subvols should try
2451 * rmdir(2).
2452 */
2453 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002454 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002455 goto out_dput;
2456
2457 /*
2458 * Do not allow deletion if the parent dir is the same
2459 * as the dir to be deleted. That means the ioctl
2460 * must be called on the dentry referencing the root
2461 * of the subvol, not a random directory contained
2462 * within it.
2463 */
2464 err = -EINVAL;
2465 if (root == dest)
2466 goto out_dput;
2467
2468 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2469 if (err)
2470 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002471 }
2472
Miao Xie5c39da52012-10-22 11:39:53 +00002473 /* check if subvolume may be deleted by a user */
2474 err = btrfs_may_delete(dir, dentry, 1);
2475 if (err)
2476 goto out_dput;
2477
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002478 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002479 err = -EINVAL;
2480 goto out_dput;
2481 }
2482
Al Viro59551022016-01-22 15:40:57 -05002483 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002484 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002485 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002486 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002487 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002488
Yan, Zheng76dda932009-09-21 16:00:26 -04002489out_dput:
2490 dput(dentry);
2491out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002492 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002493out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002494 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002495out:
2496 kfree(vol_args);
2497 return err;
2498}
2499
Chris Mason1e701a32010-03-11 09:42:04 -05002500static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002501{
Al Viro496ad9a2013-01-23 17:07:38 -05002502 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002503 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002504 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002505 int ret;
2506
Ilya Dryomov25122d12013-01-20 15:57:57 +02002507 ret = mnt_want_write_file(file);
2508 if (ret)
2509 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002510
Ilya Dryomov25122d12013-01-20 15:57:57 +02002511 if (btrfs_root_readonly(root)) {
2512 ret = -EROFS;
2513 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002514 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002515
2516 switch (inode->i_mode & S_IFMT) {
2517 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002518 if (!capable(CAP_SYS_ADMIN)) {
2519 ret = -EPERM;
2520 goto out;
2521 }
Eric Sandeende78b512013-01-31 18:21:12 +00002522 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002523 break;
2524 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002525 if (!(file->f_mode & FMODE_WRITE)) {
2526 ret = -EINVAL;
2527 goto out;
2528 }
Chris Mason1e701a32010-03-11 09:42:04 -05002529
2530 range = kzalloc(sizeof(*range), GFP_KERNEL);
2531 if (!range) {
2532 ret = -ENOMEM;
2533 goto out;
2534 }
2535
2536 if (argp) {
2537 if (copy_from_user(range, argp,
2538 sizeof(*range))) {
2539 ret = -EFAULT;
2540 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002541 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002542 }
2543 /* compression requires us to start the IO */
2544 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2545 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2546 range->extent_thresh = (u32)-1;
2547 }
2548 } else {
2549 /* the rest are all set to zero by kzalloc */
2550 range->len = (u64)-1;
2551 }
Al Viro496ad9a2013-01-23 17:07:38 -05002552 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08002553 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002554 if (ret > 0)
2555 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002556 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002557 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002558 default:
2559 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002560 }
Chris Masone441d542009-01-05 16:57:23 -05002561out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002562 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002563 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002564}
2565
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002566static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002567{
2568 struct btrfs_ioctl_vol_args *vol_args;
2569 int ret;
2570
Chris Masone441d542009-01-05 16:57:23 -05002571 if (!capable(CAP_SYS_ADMIN))
2572 return -EPERM;
2573
David Sterba171938e2017-03-28 14:44:21 +02002574 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002575 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002576
Li Zefandae7b662009-04-08 15:06:54 +08002577 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002578 if (IS_ERR(vol_args)) {
2579 ret = PTR_ERR(vol_args);
2580 goto out;
2581 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002582
Mark Fasheh5516e592008-07-24 12:20:14 -04002583 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002584 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002585
Anand Jain43d20762014-07-01 00:58:56 +08002586 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002587 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002588
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002589 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002590out:
David Sterba171938e2017-03-28 14:44:21 +02002591 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002592 return ret;
2593}
2594
Anand Jain6b526ed2016-02-13 10:01:39 +08002595static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002596{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002597 struct inode *inode = file_inode(file);
2598 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08002599 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002600 int ret;
2601
Chris Masone441d542009-01-05 16:57:23 -05002602 if (!capable(CAP_SYS_ADMIN))
2603 return -EPERM;
2604
Miao Xieda249272012-11-26 08:44:50 +00002605 ret = mnt_want_write_file(file);
2606 if (ret)
2607 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002608
Li Zefandae7b662009-04-08 15:06:54 +08002609 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002610 if (IS_ERR(vol_args)) {
2611 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002612 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002613 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002614
Anand Jain6b526ed2016-02-13 10:01:39 +08002615 /* Check for compatibility reject unknown flags */
Omar Sandovalfd4e9942018-05-22 15:44:01 -07002616 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
2617 ret = -EOPNOTSUPP;
2618 goto out;
2619 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002620
David Sterba171938e2017-03-28 14:44:21 +02002621 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00002622 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2623 goto out;
2624 }
2625
David Sterba735654e2016-02-15 18:15:21 +01002626 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002627 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08002628 } else {
2629 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002630 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08002631 }
David Sterba171938e2017-03-28 14:44:21 +02002632 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00002633
Anand Jain6b526ed2016-02-13 10:01:39 +08002634 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002635 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002636 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08002637 vol_args->devid);
2638 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002639 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08002640 vol_args->name);
2641 }
Anand Jain183860f2013-05-17 10:52:45 +00002642out:
2643 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002644err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002645 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002646 return ret;
2647}
2648
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002649static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2650{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002651 struct inode *inode = file_inode(file);
2652 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002653 struct btrfs_ioctl_vol_args *vol_args;
2654 int ret;
2655
2656 if (!capable(CAP_SYS_ADMIN))
2657 return -EPERM;
2658
2659 ret = mnt_want_write_file(file);
2660 if (ret)
2661 return ret;
2662
David Sterba171938e2017-03-28 14:44:21 +02002663 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002664 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002665 goto out_drop_write;
2666 }
2667
2668 vol_args = memdup_user(arg, sizeof(*vol_args));
2669 if (IS_ERR(vol_args)) {
2670 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002671 goto out;
2672 }
2673
David Sterba58d7bbf2016-05-04 14:10:47 +02002674 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002675 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002676
2677 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002678 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002679 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002680out:
David Sterba171938e2017-03-28 14:44:21 +02002681 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02002682out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002683 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002684
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002685 return ret;
2686}
2687
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002688static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2689 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002690{
Li Zefan027ed2f2011-06-08 08:27:56 +00002691 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002692 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002693 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002694 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002695
Li Zefan027ed2f2011-06-08 08:27:56 +00002696 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2697 if (!fi_args)
2698 return -ENOMEM;
2699
David Sterbad03262c2017-06-16 00:09:21 +02002700 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00002701 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01002702
David Sterbad03262c2017-06-16 00:09:21 +02002703 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002704 if (device->devid > fi_args->max_id)
2705 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002706 }
David Sterbad03262c2017-06-16 00:09:21 +02002707 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002708
David Sterbad03262c2017-06-16 00:09:21 +02002709 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07002710 fi_args->nodesize = fs_info->nodesize;
2711 fi_args->sectorsize = fs_info->sectorsize;
2712 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02002713
Li Zefan027ed2f2011-06-08 08:27:56 +00002714 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2715 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002716
Li Zefan027ed2f2011-06-08 08:27:56 +00002717 kfree(fi_args);
2718 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002719}
2720
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002721static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2722 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002723{
2724 struct btrfs_ioctl_dev_info_args *di_args;
2725 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01002726 int ret = 0;
2727 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002728
Jan Schmidt475f6382011-03-11 15:41:01 +01002729 di_args = memdup_user(arg, sizeof(*di_args));
2730 if (IS_ERR(di_args))
2731 return PTR_ERR(di_args);
2732
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002733 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002734 s_uuid = di_args->uuid;
2735
David Sterbac5593ca2017-06-16 00:09:21 +02002736 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002737 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002738
2739 if (!dev) {
2740 ret = -ENODEV;
2741 goto out;
2742 }
2743
2744 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002745 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2746 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002747 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002748 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002749 struct rcu_string *name;
2750
Josef Bacik606686e2012-06-04 14:03:51 -04002751 name = rcu_dereference(dev->name);
Xiongfeng Wang6670d4c2018-01-08 19:51:22 +08002752 strncpy(di_args->path, name->str, sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02002753 di_args->path[sizeof(di_args->path) - 1] = 0;
2754 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002755 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002756 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002757
2758out:
David Sterbac5593ca2017-06-16 00:09:21 +02002759 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002760 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2761 ret = -EFAULT;
2762
2763 kfree(di_args);
2764 return ret;
2765}
2766
Mark Fashehf4414602015-06-30 14:42:05 -07002767static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002768{
2769 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002770
Mark Fasheh416161d2013-08-06 11:42:51 -07002771 page = grab_cache_page(inode->i_mapping, index);
2772 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002773 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002774
2775 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002776 int ret;
2777
2778 ret = btrfs_readpage(NULL, page);
2779 if (ret)
2780 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002781 lock_page(page);
2782 if (!PageUptodate(page)) {
2783 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002784 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002785 return ERR_PTR(-EIO);
2786 }
2787 if (page->mapping != inode->i_mapping) {
2788 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002789 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002790 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002791 }
2792 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002793
2794 return page;
2795}
2796
Mark Fashehf4414602015-06-30 14:42:05 -07002797static int gather_extent_pages(struct inode *inode, struct page **pages,
2798 int num_pages, u64 off)
2799{
2800 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002801 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002802
2803 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002804again:
Mark Fashehf4414602015-06-30 14:42:05 -07002805 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002806 if (IS_ERR(pages[i])) {
2807 int err = PTR_ERR(pages[i]);
2808
2809 if (err == -EAGAIN)
2810 goto again;
2811 pages[i] = NULL;
2812 return err;
2813 }
Mark Fashehf4414602015-06-30 14:42:05 -07002814 }
2815 return 0;
2816}
2817
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002818static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2819 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002820{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002821 /*
2822 * Do any pending delalloc/csum calculations on inode, one way or
2823 * another, and lock file content.
2824 * The locking order is:
2825 *
2826 * 1) pages
2827 * 2) range in the inode's io tree
2828 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002829 while (1) {
2830 struct btrfs_ordered_extent *ordered;
2831 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2832 ordered = btrfs_lookup_first_ordered_extent(inode,
2833 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002834 if ((!ordered ||
2835 ordered->file_offset + ordered->len <= off ||
2836 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002837 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002838 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2839 if (ordered)
2840 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002841 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002842 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002843 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2844 if (ordered)
2845 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002846 if (!retry_range_locking)
2847 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002848 btrfs_wait_ordered_range(inode, off, len);
2849 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002850 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002851}
2852
Mark Fashehf4414602015-06-30 14:42:05 -07002853static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002854{
Al Viro59551022016-01-22 15:40:57 -05002855 inode_unlock(inode1);
2856 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002857}
2858
Mark Fashehf4414602015-06-30 14:42:05 -07002859static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2860{
2861 if (inode1 < inode2)
2862 swap(inode1, inode2);
2863
Al Viro59551022016-01-22 15:40:57 -05002864 inode_lock_nested(inode1, I_MUTEX_PARENT);
2865 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002866}
2867
2868static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2869 struct inode *inode2, u64 loff2, u64 len)
2870{
2871 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2872 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2873}
2874
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002875static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2876 struct inode *inode2, u64 loff2, u64 len,
2877 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002878{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002879 int ret;
2880
Mark Fasheh416161d2013-08-06 11:42:51 -07002881 if (inode1 < inode2) {
2882 swap(inode1, inode2);
2883 swap(loff1, loff2);
2884 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002885 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
2886 if (ret)
2887 return ret;
2888 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
2889 if (ret)
2890 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
2891 loff1 + len - 1);
2892 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07002893}
2894
2895struct cmp_pages {
2896 int num_pages;
2897 struct page **src_pages;
2898 struct page **dst_pages;
2899};
2900
2901static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2902{
2903 int i;
2904 struct page *pg;
2905
2906 for (i = 0; i < cmp->num_pages; i++) {
2907 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002908 if (pg) {
2909 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002910 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002911 }
Mark Fashehf4414602015-06-30 14:42:05 -07002912 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002913 if (pg) {
2914 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002915 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002916 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002917 }
Mark Fashehf4414602015-06-30 14:42:05 -07002918}
2919
2920static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2921 struct inode *dst, u64 dst_loff,
2922 u64 len, struct cmp_pages *cmp)
2923{
2924 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002925 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002926
Mark Fashehf4414602015-06-30 14:42:05 -07002927 cmp->num_pages = num_pages;
Mark Fashehf4414602015-06-30 14:42:05 -07002928
Timofey Titovets67b07bd2018-05-02 08:15:38 +03002929 ret = gather_extent_pages(src, cmp->src_pages, num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07002930 if (ret)
2931 goto out;
2932
Timofey Titovets67b07bd2018-05-02 08:15:38 +03002933 ret = gather_extent_pages(dst, cmp->dst_pages, num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07002934
2935out:
2936 if (ret)
2937 btrfs_cmp_data_free(cmp);
Naohiro Aota78ad4ce2017-09-08 17:48:55 +09002938 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07002939}
2940
David Sterba1a287cf2017-02-10 20:15:10 +01002941static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07002942{
2943 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07002944 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07002945 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002946 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07002947 void *addr, *dst_addr;
2948
Mark Fashehf4414602015-06-30 14:42:05 -07002949 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002950 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002951 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07002952 cmp_len = len;
2953
Mark Fashehf4414602015-06-30 14:42:05 -07002954 BUG_ON(i >= cmp->num_pages);
2955
2956 src_page = cmp->src_pages[i];
2957 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002958 ASSERT(PageLocked(src_page));
2959 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07002960
Mark Fasheh416161d2013-08-06 11:42:51 -07002961 addr = kmap_atomic(src_page);
2962 dst_addr = kmap_atomic(dst_page);
2963
2964 flush_dcache_page(src_page);
2965 flush_dcache_page(dst_page);
2966
2967 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08002968 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07002969
2970 kunmap_atomic(addr);
2971 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07002972
2973 if (ret)
2974 break;
2975
Mark Fasheh416161d2013-08-06 11:42:51 -07002976 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07002977 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07002978 }
2979
2980 return ret;
2981}
2982
Mark Fashehe1d227a2015-06-08 15:05:25 -07002983static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
2984 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07002985{
Mark Fashehe1d227a2015-06-08 15:05:25 -07002986 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07002987 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2988
Mark Fashehe1d227a2015-06-08 15:05:25 -07002989 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07002990 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07002991
2992 /* if we extend to eof, continue to block boundary */
2993 if (off + len == inode->i_size)
2994 *plen = len = ALIGN(inode->i_size, bs) - off;
2995
Mark Fasheh416161d2013-08-06 11:42:51 -07002996 /* Check that we are block aligned - btrfs_clone() requires this */
2997 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2998 return -EINVAL;
2999
3000 return 0;
3001}
3002
Timofey Titovets39739092018-05-02 08:15:36 +03003003static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen,
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003004 struct inode *dst, u64 dst_loff,
3005 struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07003006{
3007 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003008 u64 len = olen;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003009 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003010 u64 same_lock_start = 0;
3011 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003012
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003013 ret = extent_same_check_offsets(src, loff, &len, olen);
3014 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003015 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003016
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003017 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3018 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003019 return ret;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003020
3021 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003022 /*
3023 * Single inode case wants the same checks, except we
3024 * don't want our length pushed out past i_size as
3025 * comparing that data range makes no sense.
3026 *
3027 * extent_same_check_offsets() will do this for an
3028 * unaligned length at i_size, so catch it here and
3029 * reject the request.
3030 *
3031 * This effectively means we require aligned extents
3032 * for the single-inode case, whereas the other cases
3033 * allow an unaligned length so long as it ends at
3034 * i_size.
3035 */
Timofey Titovets39739092018-05-02 08:15:36 +03003036 if (len != olen)
3037 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003038
3039 /* Check for overlapping ranges */
Timofey Titovets39739092018-05-02 08:15:36 +03003040 if (dst_loff + len > loff && dst_loff < loff + len)
3041 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003042
3043 same_lock_start = min_t(u64, loff, dst_loff);
3044 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003045 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003046
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003047again:
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003048 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, cmp);
Mark Fashehf4414602015-06-30 14:42:05 -07003049 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003050 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003051
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003052 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003053 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3054 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003055 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003056 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3057 false);
3058 /*
3059 * If one of the inodes has dirty pages in the respective range or
3060 * ordered extents, we need to flush dellaloc and wait for all ordered
3061 * extents in the range. We must unlock the pages and the ranges in the
3062 * io trees to avoid deadlocks when flushing delalloc (requires locking
3063 * pages) and when waiting for ordered extents to complete (they require
3064 * range locking).
3065 */
3066 if (ret == -EAGAIN) {
3067 /*
3068 * Ranges in the io trees already unlocked. Now unlock all
3069 * pages before waiting for all IO to complete.
3070 */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003071 btrfs_cmp_data_free(cmp);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003072 if (same_inode) {
3073 btrfs_wait_ordered_range(src, same_lock_start,
3074 same_lock_len);
3075 } else {
3076 btrfs_wait_ordered_range(src, loff, len);
3077 btrfs_wait_ordered_range(dst, dst_loff, len);
3078 }
3079 goto again;
3080 }
3081 ASSERT(ret == 0);
3082 if (WARN_ON(ret)) {
3083 /* ranges in the io trees already unlocked */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003084 btrfs_cmp_data_free(cmp);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003085 return ret;
3086 }
Mark Fashehf4414602015-06-30 14:42:05 -07003087
Mark Fasheh207910d2015-06-30 14:42:04 -07003088 /* pass original length for comparison so we stay within i_size */
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003089 ret = btrfs_cmp_data(olen, cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003090 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003091 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003092
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003093 if (same_inode)
3094 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3095 same_lock_start + same_lock_len - 1);
3096 else
3097 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003098
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003099 btrfs_cmp_data_free(cmp);
Timofey Titovets39739092018-05-02 08:15:36 +03003100
3101 return ret;
3102}
3103
Timofey Titovetsb6728762018-05-02 08:15:37 +03003104#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3105
Timofey Titovets39739092018-05-02 08:15:36 +03003106static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3107 struct inode *dst, u64 dst_loff)
3108{
3109 int ret;
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003110 struct cmp_pages cmp;
3111 int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT;
Timofey Titovets39739092018-05-02 08:15:36 +03003112 bool same_inode = (src == dst);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003113 u64 i, tail_len, chunk_count;
Timofey Titovets39739092018-05-02 08:15:36 +03003114
3115 if (olen == 0)
3116 return 0;
3117
3118 if (same_inode)
3119 inode_lock(src);
3120 else
3121 btrfs_double_inode_lock(src, dst);
3122
3123 /* don't make the dst file partly checksummed */
3124 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3125 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3126 ret = -EINVAL;
3127 goto out_unlock;
3128 }
3129
Timofey Titovetsb6728762018-05-02 08:15:37 +03003130 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3131 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003132 if (chunk_count == 0)
3133 num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT;
3134
3135 /*
3136 * If deduping ranges in the same inode, locking rules make it
3137 * mandatory to always lock pages in ascending order to avoid deadlocks
3138 * with concurrent tasks (such as starting writeback/delalloc).
3139 */
3140 if (same_inode && dst_loff < loff)
3141 swap(loff, dst_loff);
3142
3143 /*
3144 * We must gather up all the pages before we initiate our extent
3145 * locking. We use an array for the page pointers. Size of the array is
3146 * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
3147 */
David Sterbabf5091c2018-05-11 17:57:54 +02003148 cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3149 GFP_KERNEL | __GFP_ZERO);
3150 cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3151 GFP_KERNEL | __GFP_ZERO);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003152 if (!cmp.src_pages || !cmp.dst_pages) {
David Sterbabf5091c2018-05-11 17:57:54 +02003153 ret = -ENOMEM;
3154 goto out_free;
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003155 }
Timofey Titovetsb6728762018-05-02 08:15:37 +03003156
3157 for (i = 0; i < chunk_count; i++) {
3158 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003159 dst, dst_loff, &cmp);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003160 if (ret)
3161 goto out_unlock;
3162
3163 loff += BTRFS_MAX_DEDUPE_LEN;
3164 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3165 }
3166
3167 if (tail_len > 0)
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003168 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
3169 dst_loff, &cmp);
Timofey Titovets39739092018-05-02 08:15:36 +03003170
Mark Fasheh416161d2013-08-06 11:42:51 -07003171out_unlock:
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003172 if (same_inode)
Al Viro59551022016-01-22 15:40:57 -05003173 inode_unlock(src);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003174 else
3175 btrfs_double_inode_unlock(src, dst);
Mark Fasheh416161d2013-08-06 11:42:51 -07003176
David Sterbabf5091c2018-05-11 17:57:54 +02003177out_free:
3178 kvfree(cmp.src_pages);
3179 kvfree(cmp.dst_pages);
Timofey Titovets67b07bd2018-05-02 08:15:38 +03003180
Mark Fasheh416161d2013-08-06 11:42:51 -07003181 return ret;
3182}
3183
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003184ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3185 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003186{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003187 struct inode *src = file_inode(src_file);
3188 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003189 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003190 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003191
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003192 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003193 /*
3194 * Btrfs does not support blocksize < page_size. As a
3195 * result, btrfs_cmp_data() won't correctly handle
3196 * this situation without an update.
3197 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003198 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003199 }
3200
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003201 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3202 if (res)
3203 return res;
3204 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003205}
3206
Filipe Mananaf82a9902014-06-01 01:50:28 +01003207static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3208 struct inode *inode,
3209 u64 endoff,
3210 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003211 const u64 olen,
3212 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003213{
3214 struct btrfs_root *root = BTRFS_I(inode)->root;
3215 int ret;
3216
3217 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003218 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003219 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003220 /*
3221 * We round up to the block size at eof when determining which
3222 * extents to clone above, but shouldn't round up the file size.
3223 */
3224 if (endoff > destoff + olen)
3225 endoff = destoff + olen;
3226 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003227 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003228
3229 ret = btrfs_update_inode(trans, root, inode);
3230 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003231 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003232 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003233 goto out;
3234 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003235 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003236out:
3237 return ret;
3238}
3239
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003240static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003241 const struct btrfs_trans_handle *trans,
3242 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003243 const u64 hole_offset,
3244 const u64 hole_len)
3245{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003246 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003247 struct extent_map *em;
3248 int ret;
3249
3250 em = alloc_extent_map();
3251 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003252 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003253 return;
3254 }
3255
Filipe Manana14f59792014-06-29 21:45:40 +01003256 if (path) {
3257 struct btrfs_file_extent_item *fi;
3258
3259 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3260 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003261 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003262 em->generation = -1;
3263 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3264 BTRFS_FILE_EXTENT_INLINE)
3265 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003266 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003267 } else {
3268 em->start = hole_offset;
3269 em->len = hole_len;
3270 em->ram_bytes = em->len;
3271 em->orig_start = hole_offset;
3272 em->block_start = EXTENT_MAP_HOLE;
3273 em->block_len = 0;
3274 em->orig_block_len = 0;
3275 em->compress_type = BTRFS_COMPRESS_NONE;
3276 em->generation = trans->transid;
3277 }
3278
3279 while (1) {
3280 write_lock(&em_tree->lock);
3281 ret = add_extent_mapping(em_tree, em, 1);
3282 write_unlock(&em_tree->lock);
3283 if (ret != -EEXIST) {
3284 free_extent_map(em);
3285 break;
3286 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003287 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003288 em->start + em->len - 1, 0);
3289 }
3290
David Sterbaee39b432014-09-30 01:33:33 +02003291 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003292 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003293}
3294
Filipe Manana8039d872015-10-13 15:15:00 +01003295/*
3296 * Make sure we do not end up inserting an inline extent into a file that has
3297 * already other (non-inline) extents. If a file has an inline extent it can
3298 * not have any other extents and the (single) inline extent must start at the
3299 * file offset 0. Failing to respect these rules will lead to file corruption,
3300 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3301 *
3302 * We can have extents that have been already written to disk or we can have
3303 * dirty ranges still in delalloc, in which case the extent maps and items are
3304 * created only when we run delalloc, and the delalloc ranges might fall outside
3305 * the range we are currently locking in the inode's io tree. So we check the
3306 * inode's i_size because of that (i_size updates are done while holding the
3307 * i_mutex, which we are holding here).
3308 * We also check to see if the inode has a size not greater than "datal" but has
3309 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3310 * protected against such concurrent fallocate calls by the i_mutex).
3311 *
3312 * If the file has no extents but a size greater than datal, do not allow the
3313 * copy because we would need turn the inline extent into a non-inline one (even
3314 * with NO_HOLES enabled). If we find our destination inode only has one inline
3315 * extent, just overwrite it with the source inline extent if its size is less
3316 * than the source extent's size, or we could copy the source inline extent's
3317 * data into the destination inode's inline extent if the later is greater then
3318 * the former.
3319 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003320static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003321 struct btrfs_trans_handle *trans,
3322 struct btrfs_path *path,
3323 struct btrfs_key *new_key,
3324 const u64 drop_start,
3325 const u64 datal,
3326 const u64 skip,
3327 const u64 size,
3328 char *inline_data)
3329{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003330 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003331 struct btrfs_root *root = BTRFS_I(dst)->root;
3332 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003333 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003334 int ret;
3335 struct btrfs_key key;
3336
3337 if (new_key->offset > 0)
3338 return -EOPNOTSUPP;
3339
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003340 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003341 key.type = BTRFS_EXTENT_DATA_KEY;
3342 key.offset = 0;
3343 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3344 if (ret < 0) {
3345 return ret;
3346 } else if (ret > 0) {
3347 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3348 ret = btrfs_next_leaf(root, path);
3349 if (ret < 0)
3350 return ret;
3351 else if (ret > 0)
3352 goto copy_inline_extent;
3353 }
3354 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003355 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003356 key.type == BTRFS_EXTENT_DATA_KEY) {
3357 ASSERT(key.offset > 0);
3358 return -EOPNOTSUPP;
3359 }
3360 } else if (i_size_read(dst) <= datal) {
3361 struct btrfs_file_extent_item *ei;
3362 u64 ext_len;
3363
3364 /*
3365 * If the file size is <= datal, make sure there are no other
3366 * extents following (can happen do to an fallocate call with
3367 * the flag FALLOC_FL_KEEP_SIZE).
3368 */
3369 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3370 struct btrfs_file_extent_item);
3371 /*
3372 * If it's an inline extent, it can not have other extents
3373 * following it.
3374 */
3375 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3376 BTRFS_FILE_EXTENT_INLINE)
3377 goto copy_inline_extent;
3378
3379 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3380 if (ext_len > aligned_end)
3381 return -EOPNOTSUPP;
3382
3383 ret = btrfs_next_item(root, path);
3384 if (ret < 0) {
3385 return ret;
3386 } else if (ret == 0) {
3387 btrfs_item_key_to_cpu(path->nodes[0], &key,
3388 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003389 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003390 key.type == BTRFS_EXTENT_DATA_KEY)
3391 return -EOPNOTSUPP;
3392 }
3393 }
3394
3395copy_inline_extent:
3396 /*
3397 * We have no extent items, or we have an extent at offset 0 which may
3398 * or may not be inlined. All these cases are dealt the same way.
3399 */
3400 if (i_size_read(dst) > datal) {
3401 /*
3402 * If the destination inode has an inline extent...
3403 * This would require copying the data from the source inline
3404 * extent into the beginning of the destination's inline extent.
3405 * But this is really complex, both extents can be compressed
3406 * or just one of them, which would require decompressing and
3407 * re-compressing data (which could increase the new compressed
3408 * size, not allowing the compressed data to fit anymore in an
3409 * inline extent).
3410 * So just don't support this case for now (it should be rare,
3411 * we are not really saving space when cloning inline extents).
3412 */
3413 return -EOPNOTSUPP;
3414 }
3415
3416 btrfs_release_path(path);
3417 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3418 if (ret)
3419 return ret;
3420 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3421 if (ret)
3422 return ret;
3423
3424 if (skip) {
3425 const u32 start = btrfs_file_extent_calc_inline_size(0);
3426
3427 memmove(inline_data + start, inline_data + start + skip, datal);
3428 }
3429
3430 write_extent_buffer(path->nodes[0], inline_data,
3431 btrfs_item_ptr_offset(path->nodes[0],
3432 path->slots[0]),
3433 size);
3434 inode_add_bytes(dst, datal);
3435
3436 return 0;
3437}
3438
Mark Fasheh32b7c682013-08-06 11:42:49 -07003439/**
3440 * btrfs_clone() - clone a range from inode file to another
3441 *
3442 * @src: Inode to clone from
3443 * @inode: Inode to clone to
3444 * @off: Offset within source to start clone from
3445 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003446 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003447 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003448 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003449 */
3450static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003451 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003452 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003453{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003454 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003455 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003456 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003457 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003458 struct btrfs_trans_handle *trans;
3459 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003460 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003461 u32 nritems;
3462 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003463 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003464 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003465 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003466
3467 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003468 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3469 if (!buf)
3470 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003471
3472 path = btrfs_alloc_path();
3473 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003474 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003475 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003476 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003477
David Sterbae4058b52015-11-27 16:31:35 +01003478 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003479 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003480 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003481 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003482 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003483
3484 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003485 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003486
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003487 /*
3488 * note the key will change type as we walk through the
3489 * tree.
3490 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003491 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003492 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3493 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003494 if (ret < 0)
3495 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003496 /*
3497 * First search, if no extent item that starts at offset off was
3498 * found but the previous item is an extent item, it's possible
3499 * it might overlap our target range, therefore process it.
3500 */
3501 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3502 btrfs_item_key_to_cpu(path->nodes[0], &key,
3503 path->slots[0] - 1);
3504 if (key.type == BTRFS_EXTENT_DATA_KEY)
3505 path->slots[0]--;
3506 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003507
Yan Zhengae01a0a2008-08-04 23:23:47 -04003508 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003509process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003510 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003511 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003512 if (ret < 0)
3513 goto out;
3514 if (ret > 0)
3515 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003516 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003517 }
3518 leaf = path->nodes[0];
3519 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003520
Yan Zhengae01a0a2008-08-04 23:23:47 -04003521 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003522 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003523 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003524 break;
3525
David Sterba962a2982014-06-04 18:41:45 +02003526 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003527 struct btrfs_file_extent_item *extent;
3528 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003529 u32 size;
3530 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003531 u64 disko = 0, diskl = 0;
3532 u64 datao = 0, datal = 0;
3533 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003534 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003535
Sage Weilc5c9cd42008-11-12 14:32:25 -05003536 extent = btrfs_item_ptr(leaf, slot,
3537 struct btrfs_file_extent_item);
3538 comp = btrfs_file_extent_compression(leaf, extent);
3539 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003540 if (type == BTRFS_FILE_EXTENT_REG ||
3541 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003542 disko = btrfs_file_extent_disk_bytenr(leaf,
3543 extent);
3544 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3545 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003546 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003547 datal = btrfs_file_extent_num_bytes(leaf,
3548 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003549 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3550 /* take upper bound, may be compressed */
3551 datal = btrfs_file_extent_ram_bytes(leaf,
3552 extent);
3553 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003554
Filipe Manana2c463822014-05-31 02:31:05 +01003555 /*
3556 * The first search might have left us at an extent
3557 * item that ends before our target range's start, can
3558 * happen if we have holes and NO_HOLES feature enabled.
3559 */
3560 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003561 path->slots[0]++;
3562 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003563 } else if (key.offset >= off + len) {
3564 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003565 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003566 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003567 size = btrfs_item_size_nr(leaf, slot);
3568 read_extent_buffer(leaf, buf,
3569 btrfs_item_ptr_offset(leaf, slot),
3570 size);
3571
3572 btrfs_release_path(path);
3573 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003574
Zheng Yan31840ae2008-09-23 13:14:14 -04003575 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003576 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003577 if (off <= key.offset)
3578 new_key.offset = key.offset + destoff - off;
3579 else
3580 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003581
Sage Weilb6f34092011-09-20 14:48:51 -04003582 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003583 * Deal with a hole that doesn't have an extent item
3584 * that represents it (NO_HOLES feature enabled).
3585 * This hole is either in the middle of the cloning
3586 * range or at the beginning (fully overlaps it or
3587 * partially overlaps it).
3588 */
3589 if (new_key.offset != last_dest_end)
3590 drop_start = last_dest_end;
3591 else
3592 drop_start = new_key.offset;
3593
3594 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003595 * 1 - adjusting old extent (we may have to split it)
3596 * 1 - add new extent
3597 * 1 - inode update
3598 */
3599 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003600 if (IS_ERR(trans)) {
3601 ret = PTR_ERR(trans);
3602 goto out;
3603 }
3604
Chris Masonc8a894d2009-06-27 21:07:03 -04003605 if (type == BTRFS_FILE_EXTENT_REG ||
3606 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003607 /*
3608 * a | --- range to clone ---| b
3609 * | ------------- extent ------------- |
3610 */
3611
Antonio Ospite93915582014-06-04 14:03:48 +02003612 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003613 if (key.offset + datal > off + len)
3614 datal = off + len - key.offset;
3615
Antonio Ospite93915582014-06-04 14:03:48 +02003616 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003617 if (off > key.offset) {
3618 datao += off - key.offset;
3619 datal -= off - key.offset;
3620 }
3621
Josef Bacik5dc562c2012-08-17 13:14:17 -04003622 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003623 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003624 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003625 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003626 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003627 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003628 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003629 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003630 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003631 goto out;
3632 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003633
Sage Weilc5c9cd42008-11-12 14:32:25 -05003634 ret = btrfs_insert_empty_item(trans, root, path,
3635 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003636 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003637 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003638 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003639 goto out;
3640 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003641
3642 leaf = path->nodes[0];
3643 slot = path->slots[0];
3644 write_extent_buffer(leaf, buf,
3645 btrfs_item_ptr_offset(leaf, slot),
3646 size);
3647
3648 extent = btrfs_item_ptr(leaf, slot,
3649 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003650
Sage Weilc5c9cd42008-11-12 14:32:25 -05003651 /* disko == 0 means it's a hole */
3652 if (!disko)
3653 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003654
3655 btrfs_set_file_extent_offset(leaf, extent,
3656 datao);
3657 btrfs_set_file_extent_num_bytes(leaf, extent,
3658 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003659
Sage Weilc5c9cd42008-11-12 14:32:25 -05003660 if (disko) {
3661 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003662 ret = btrfs_inc_extent_ref(trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003663 root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003664 disko, diskl, 0,
3665 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003666 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003667 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003668 if (ret) {
3669 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003670 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003671 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003672 goto out;
3673
3674 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003675 }
3676 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3677 u64 skip = 0;
3678 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003679
Sage Weilc5c9cd42008-11-12 14:32:25 -05003680 if (off > key.offset) {
3681 skip = off - key.offset;
3682 new_key.offset += skip;
3683 }
Chris Masond3977122009-01-05 21:25:51 -05003684
Liu Boaa42ffd2012-09-18 03:52:23 -06003685 if (key.offset + datal > off + len)
3686 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003687
Sage Weilc5c9cd42008-11-12 14:32:25 -05003688 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003689 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003690 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003691 goto out;
3692 }
3693 size -= skip + trim;
3694 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003695
David Sterba4a0ab9d2017-02-10 20:18:49 +01003696 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003697 trans, path,
3698 &new_key,
3699 drop_start,
3700 datal,
3701 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003702 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003703 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003704 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003705 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003706 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003707 goto out;
3708 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003709 leaf = path->nodes[0];
3710 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003711 }
3712
Filipe Manana7ffbb592014-06-09 03:48:05 +01003713 /* If we have an implicit hole (NO_HOLES feature). */
3714 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003715 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003716 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003717 new_key.offset - drop_start);
3718
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003719 clone_update_extent_map(BTRFS_I(inode), trans,
3720 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003721
Sage Weilc5c9cd42008-11-12 14:32:25 -05003722 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003723 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003724
Filipe Manana62e23902014-08-08 02:47:06 +01003725 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003726 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003727 ret = clone_finish_inode_update(trans, inode,
3728 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003729 destoff, olen,
3730 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003731 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003732 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003733 if (new_key.offset + datal >= destoff + len)
3734 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003735 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003736 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003737 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003738
3739 if (fatal_signal_pending(current)) {
3740 ret = -EINTR;
3741 goto out;
3742 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003743 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003744 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003745
Filipe Mananaf82a9902014-06-01 01:50:28 +01003746 if (last_dest_end < destoff + len) {
3747 /*
3748 * We have an implicit hole (NO_HOLES feature is enabled) that
3749 * fully or partially overlaps our cloning range at its end.
3750 */
3751 btrfs_release_path(path);
3752
3753 /*
3754 * 1 - remove extent(s)
3755 * 1 - inode update
3756 */
3757 trans = btrfs_start_transaction(root, 2);
3758 if (IS_ERR(trans)) {
3759 ret = PTR_ERR(trans);
3760 goto out;
3761 }
3762 ret = btrfs_drop_extents(trans, root, inode,
3763 last_dest_end, destoff + len, 1);
3764 if (ret) {
3765 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003766 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003767 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003768 goto out;
3769 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003770 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3771 last_dest_end,
3772 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003773 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003774 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003775 }
3776
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003777out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003778 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003779 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003780 return ret;
3781}
3782
Zach Brown3db11b22015-11-10 16:53:32 -05003783static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3784 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003785{
Al Viro54563d42013-09-01 15:57:51 -04003786 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003787 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003788 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003789 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003790 int ret;
3791 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003792 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003793 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003794
3795 /*
3796 * TODO:
3797 * - split compressed inline extents. annoying: we need to
3798 * decompress into destination's address_space (the file offset
3799 * may change, so source mapping won't do), then recompress (or
3800 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003801 *
3802 * - split destination inode's inline extents. The inline extents can
3803 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003804 */
3805
Mark Fasheh32b7c682013-08-06 11:42:49 -07003806 if (btrfs_root_readonly(root))
3807 return -EROFS;
3808
Zach Brown3db11b22015-11-10 16:53:32 -05003809 if (file_src->f_path.mnt != file->f_path.mnt ||
3810 src->i_sb != inode->i_sb)
3811 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003812
Mark Fasheh32b7c682013-08-06 11:42:49 -07003813 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003814 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003815
3816 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003817 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003818 } else {
Al Viro59551022016-01-22 15:40:57 -05003819 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003820 }
3821
Omar Sandovalb5c40d52018-05-22 15:02:12 -07003822 /* don't make the dst file partly checksummed */
3823 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3824 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
3825 ret = -EINVAL;
3826 goto out_unlock;
3827 }
3828
Mark Fasheh32b7c682013-08-06 11:42:49 -07003829 /* determine range to clone */
3830 ret = -EINVAL;
3831 if (off + len > src->i_size || off + len < off)
3832 goto out_unlock;
3833 if (len == 0)
3834 olen = len = src->i_size - off;
3835 /* if we extend to eof, continue to block boundary */
3836 if (off + len == src->i_size)
3837 len = ALIGN(src->i_size, bs) - off;
3838
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003839 if (len == 0) {
3840 ret = 0;
3841 goto out_unlock;
3842 }
3843
Mark Fasheh32b7c682013-08-06 11:42:49 -07003844 /* verify the end result is block aligned */
3845 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3846 !IS_ALIGNED(destoff, bs))
3847 goto out_unlock;
3848
3849 /* verify if ranges are overlapped within the same file */
3850 if (same_inode) {
3851 if (destoff + len > off && destoff < off + len)
3852 goto out_unlock;
3853 }
3854
3855 if (destoff > inode->i_size) {
3856 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3857 if (ret)
3858 goto out_unlock;
3859 }
3860
Filipe Mananac125b8b2014-05-23 05:03:34 +01003861 /*
3862 * Lock the target range too. Right after we replace the file extent
3863 * items in the fs tree (which now point to the cloned data), we might
3864 * have a worker replace them with extent items relative to a write
3865 * operation that was issued before this clone operation (i.e. confront
3866 * with inode.c:btrfs_finish_ordered_io).
3867 */
3868 if (same_inode) {
3869 u64 lock_start = min_t(u64, off, destoff);
3870 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003871
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003872 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003873 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003874 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3875 true);
3876 }
3877 ASSERT(ret == 0);
3878 if (WARN_ON(ret)) {
3879 /* ranges in the io trees already unlocked */
3880 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003881 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003882
Mark Fasheh1c919a52015-06-30 14:42:08 -07003883 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003884
Filipe Mananac125b8b2014-05-23 05:03:34 +01003885 if (same_inode) {
3886 u64 lock_start = min_t(u64, off, destoff);
3887 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3888
3889 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3890 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003891 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003892 }
3893 /*
3894 * Truncate page cache pages so that future reads will see the cloned
3895 * data immediately and not the previous data.
3896 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303897 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003898 round_down(destoff, PAGE_SIZE),
3899 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003900out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003901 if (!same_inode)
3902 btrfs_double_inode_unlock(src, inode);
3903 else
Al Viro59551022016-01-22 15:40:57 -05003904 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003905 return ret;
3906}
3907
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003908int btrfs_clone_file_range(struct file *src_file, loff_t off,
3909 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05003910{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003911 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003912}
3913
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003914static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3915{
Al Viro496ad9a2013-01-23 17:07:38 -05003916 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003917 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003918 struct btrfs_root *root = BTRFS_I(inode)->root;
3919 struct btrfs_root *new_root;
3920 struct btrfs_dir_item *di;
3921 struct btrfs_trans_handle *trans;
3922 struct btrfs_path *path;
3923 struct btrfs_key location;
3924 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003925 u64 objectid = 0;
3926 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003927 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003928
3929 if (!capable(CAP_SYS_ADMIN))
3930 return -EPERM;
3931
Miao Xie3c04ce02012-11-26 08:43:07 +00003932 ret = mnt_want_write_file(file);
3933 if (ret)
3934 return ret;
3935
3936 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3937 ret = -EFAULT;
3938 goto out;
3939 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003940
3941 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303942 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003943
3944 location.objectid = objectid;
3945 location.type = BTRFS_ROOT_ITEM_KEY;
3946 location.offset = (u64)-1;
3947
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003948 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003949 if (IS_ERR(new_root)) {
3950 ret = PTR_ERR(new_root);
3951 goto out;
3952 }
satoru takeuchi6d6d2822017-09-12 22:42:52 +09003953 if (!is_fstree(new_root->objectid)) {
3954 ret = -ENOENT;
3955 goto out;
3956 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003957
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003958 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003959 if (!path) {
3960 ret = -ENOMEM;
3961 goto out;
3962 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003963 path->leave_spinning = 1;
3964
3965 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003966 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003967 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003968 ret = PTR_ERR(trans);
3969 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003970 }
3971
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003972 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3973 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003974 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003975 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003976 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003977 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003978 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003979 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003980 ret = -ENOENT;
3981 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003982 }
3983
3984 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3985 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3986 btrfs_mark_buffer_dirty(path->nodes[0]);
3987 btrfs_free_path(path);
3988
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003989 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003990 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00003991out:
3992 mnt_drop_write_file(file);
3993 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003994}
3995
Su Yuec065f5b12018-04-02 17:24:11 +08003996static void get_block_group_info(struct list_head *groups_list,
3997 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003998{
3999 struct btrfs_block_group_cache *block_group;
4000
4001 space->total_bytes = 0;
4002 space->used_bytes = 0;
4003 space->flags = 0;
4004 list_for_each_entry(block_group, groups_list, list) {
4005 space->flags = block_group->flags;
4006 space->total_bytes += block_group->key.offset;
4007 space->used_bytes +=
4008 btrfs_block_group_used(&block_group->item);
4009 }
4010}
4011
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004012static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4013 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004014{
4015 struct btrfs_ioctl_space_args space_args;
4016 struct btrfs_ioctl_space_info space;
4017 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004018 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004019 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004020 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004021 static const u64 types[] = {
4022 BTRFS_BLOCK_GROUP_DATA,
4023 BTRFS_BLOCK_GROUP_SYSTEM,
4024 BTRFS_BLOCK_GROUP_METADATA,
4025 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4026 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004027 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004028 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004029 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004030 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004031 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004032
4033 if (copy_from_user(&space_args,
4034 (struct btrfs_ioctl_space_args __user *)arg,
4035 sizeof(space_args)))
4036 return -EFAULT;
4037
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004038 for (i = 0; i < num_types; i++) {
4039 struct btrfs_space_info *tmp;
4040
4041 info = NULL;
4042 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004043 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004044 list) {
4045 if (tmp->flags == types[i]) {
4046 info = tmp;
4047 break;
4048 }
4049 }
4050 rcu_read_unlock();
4051
4052 if (!info)
4053 continue;
4054
4055 down_read(&info->groups_sem);
4056 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4057 if (!list_empty(&info->block_groups[c]))
4058 slot_count++;
4059 }
4060 up_read(&info->groups_sem);
4061 }
Josef Bacik1406e432010-01-13 18:19:06 +00004062
David Sterba36523e952014-02-07 14:34:12 +01004063 /*
4064 * Global block reserve, exported as a space_info
4065 */
4066 slot_count++;
4067
Chris Mason7fde62b2010-03-16 15:40:10 -04004068 /* space_slots == 0 means they are asking for a count */
4069 if (space_args.space_slots == 0) {
4070 space_args.total_spaces = slot_count;
4071 goto out;
4072 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004073
Dan Rosenberg51788b12011-02-14 16:04:23 -05004074 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004075
Chris Mason7fde62b2010-03-16 15:40:10 -04004076 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004077
Chris Mason7fde62b2010-03-16 15:40:10 -04004078 /* we generally have at most 6 or so space infos, one for each raid
4079 * level. So, a whole page should be more than enough for everyone
4080 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004081 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004082 return -ENOMEM;
4083
4084 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004085 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004086 if (!dest)
4087 return -ENOMEM;
4088 dest_orig = dest;
4089
4090 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004091 for (i = 0; i < num_types; i++) {
4092 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004093
Dan Rosenberg51788b12011-02-14 16:04:23 -05004094 if (!slot_count)
4095 break;
4096
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004097 info = NULL;
4098 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004099 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004100 list) {
4101 if (tmp->flags == types[i]) {
4102 info = tmp;
4103 break;
4104 }
4105 }
4106 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004107
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004108 if (!info)
4109 continue;
4110 down_read(&info->groups_sem);
4111 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4112 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004113 get_block_group_info(&info->block_groups[c],
4114 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004115 memcpy(dest, &space, sizeof(space));
4116 dest++;
4117 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004118 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004119 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004120 if (!slot_count)
4121 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004122 }
4123 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004124 }
Josef Bacik1406e432010-01-13 18:19:06 +00004125
David Sterba36523e952014-02-07 14:34:12 +01004126 /*
4127 * Add global block reserve
4128 */
4129 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004130 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004131
4132 spin_lock(&block_rsv->lock);
4133 space.total_bytes = block_rsv->size;
4134 space.used_bytes = block_rsv->size - block_rsv->reserved;
4135 spin_unlock(&block_rsv->lock);
4136 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4137 memcpy(dest, &space, sizeof(space));
4138 space_args.total_spaces++;
4139 }
4140
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004141 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004142 (arg + sizeof(struct btrfs_ioctl_space_args));
4143
4144 if (copy_to_user(user_dest, dest_orig, alloc_size))
4145 ret = -EFAULT;
4146
4147 kfree(dest_orig);
4148out:
4149 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004150 ret = -EFAULT;
4151
4152 return ret;
4153}
4154
Miao Xie9a8c28b2012-11-26 08:40:43 +00004155static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4156 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004157{
Sage Weil46204592010-10-29 15:41:32 -04004158 struct btrfs_trans_handle *trans;
4159 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004160 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004161
Miao Xied4edf392013-02-20 09:17:06 +00004162 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004163 if (IS_ERR(trans)) {
4164 if (PTR_ERR(trans) != -ENOENT)
4165 return PTR_ERR(trans);
4166
4167 /* No running transaction, don't bother */
4168 transid = root->fs_info->last_trans_committed;
4169 goto out;
4170 }
Sage Weil46204592010-10-29 15:41:32 -04004171 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004172 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004173 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004174 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004175 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004176 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004177out:
Sage Weil46204592010-10-29 15:41:32 -04004178 if (argp)
4179 if (copy_to_user(argp, &transid, sizeof(transid)))
4180 return -EFAULT;
4181 return 0;
4182}
4183
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004184static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004185 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004186{
Sage Weil46204592010-10-29 15:41:32 -04004187 u64 transid;
4188
4189 if (argp) {
4190 if (copy_from_user(&transid, argp, sizeof(transid)))
4191 return -EFAULT;
4192 } else {
4193 transid = 0; /* current trans */
4194 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004195 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004196}
4197
Miao Xieb8e95482012-11-26 08:48:01 +00004198static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004199{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004200 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004201 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004202 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004203
4204 if (!capable(CAP_SYS_ADMIN))
4205 return -EPERM;
4206
4207 sa = memdup_user(arg, sizeof(*sa));
4208 if (IS_ERR(sa))
4209 return PTR_ERR(sa);
4210
Miao Xieb8e95482012-11-26 08:48:01 +00004211 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4212 ret = mnt_want_write_file(file);
4213 if (ret)
4214 goto out;
4215 }
4216
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004217 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004218 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4219 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004220
4221 if (copy_to_user(arg, sa, sizeof(*sa)))
4222 ret = -EFAULT;
4223
Miao Xieb8e95482012-11-26 08:48:01 +00004224 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4225 mnt_drop_write_file(file);
4226out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004227 kfree(sa);
4228 return ret;
4229}
4230
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004231static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004232{
4233 if (!capable(CAP_SYS_ADMIN))
4234 return -EPERM;
4235
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004236 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004237}
4238
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004239static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004240 void __user *arg)
4241{
4242 struct btrfs_ioctl_scrub_args *sa;
4243 int ret;
4244
4245 if (!capable(CAP_SYS_ADMIN))
4246 return -EPERM;
4247
4248 sa = memdup_user(arg, sizeof(*sa));
4249 if (IS_ERR(sa))
4250 return PTR_ERR(sa);
4251
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004252 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004253
4254 if (copy_to_user(arg, sa, sizeof(*sa)))
4255 ret = -EFAULT;
4256
4257 kfree(sa);
4258 return ret;
4259}
4260
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004261static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004262 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004263{
4264 struct btrfs_ioctl_get_dev_stats *sa;
4265 int ret;
4266
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004267 sa = memdup_user(arg, sizeof(*sa));
4268 if (IS_ERR(sa))
4269 return PTR_ERR(sa);
4270
David Sterbab27f7c02012-06-22 06:30:39 -06004271 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4272 kfree(sa);
4273 return -EPERM;
4274 }
4275
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004276 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004277
4278 if (copy_to_user(arg, sa, sizeof(*sa)))
4279 ret = -EFAULT;
4280
4281 kfree(sa);
4282 return ret;
4283}
4284
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004285static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4286 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004287{
4288 struct btrfs_ioctl_dev_replace_args *p;
4289 int ret;
4290
4291 if (!capable(CAP_SYS_ADMIN))
4292 return -EPERM;
4293
4294 p = memdup_user(arg, sizeof(*p));
4295 if (IS_ERR(p))
4296 return PTR_ERR(p);
4297
4298 switch (p->cmd) {
4299 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004300 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004301 ret = -EROFS;
4302 goto out;
4303 }
David Sterba171938e2017-03-28 14:44:21 +02004304 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004305 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004306 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004307 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004308 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004309 }
4310 break;
4311 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004312 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004313 ret = 0;
4314 break;
4315 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004316 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004317 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004318 break;
4319 default:
4320 ret = -EINVAL;
4321 break;
4322 }
4323
4324 if (copy_to_user(arg, p, sizeof(*p)))
4325 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004326out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004327 kfree(p);
4328 return ret;
4329}
4330
Jan Schmidtd7728c92011-07-07 16:48:38 +02004331static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4332{
4333 int ret = 0;
4334 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004335 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004336 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004337 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004338 struct inode_fs_paths *ipath = NULL;
4339 struct btrfs_path *path;
4340
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004341 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004342 return -EPERM;
4343
4344 path = btrfs_alloc_path();
4345 if (!path) {
4346 ret = -ENOMEM;
4347 goto out;
4348 }
4349
4350 ipa = memdup_user(arg, sizeof(*ipa));
4351 if (IS_ERR(ipa)) {
4352 ret = PTR_ERR(ipa);
4353 ipa = NULL;
4354 goto out;
4355 }
4356
4357 size = min_t(u32, ipa->size, 4096);
4358 ipath = init_ipath(size, root, path);
4359 if (IS_ERR(ipath)) {
4360 ret = PTR_ERR(ipath);
4361 ipath = NULL;
4362 goto out;
4363 }
4364
4365 ret = paths_from_inode(ipa->inum, ipath);
4366 if (ret < 0)
4367 goto out;
4368
4369 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004370 rel_ptr = ipath->fspath->val[i] -
4371 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004372 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004373 }
4374
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004375 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4376 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004377 if (ret) {
4378 ret = -EFAULT;
4379 goto out;
4380 }
4381
4382out:
4383 btrfs_free_path(path);
4384 free_ipath(ipath);
4385 kfree(ipa);
4386
4387 return ret;
4388}
4389
4390static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4391{
4392 struct btrfs_data_container *inodes = ctx;
4393 const size_t c = 3 * sizeof(u64);
4394
4395 if (inodes->bytes_left >= c) {
4396 inodes->bytes_left -= c;
4397 inodes->val[inodes->elem_cnt] = inum;
4398 inodes->val[inodes->elem_cnt + 1] = offset;
4399 inodes->val[inodes->elem_cnt + 2] = root;
4400 inodes->elem_cnt += 3;
4401 } else {
4402 inodes->bytes_missing += c - inodes->bytes_left;
4403 inodes->bytes_left = 0;
4404 inodes->elem_missed += 3;
4405 }
4406
4407 return 0;
4408}
4409
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004410static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004411 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004412{
4413 int ret = 0;
4414 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004415 struct btrfs_ioctl_logical_ino_args *loi;
4416 struct btrfs_data_container *inodes = NULL;
4417 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004418 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004419
4420 if (!capable(CAP_SYS_ADMIN))
4421 return -EPERM;
4422
4423 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304424 if (IS_ERR(loi))
4425 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004426
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004427 if (version == 1) {
4428 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004429 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004430 } else {
4431 /* All reserved bits must be 0 for now */
4432 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4433 ret = -EINVAL;
4434 goto out_loi;
4435 }
4436 /* Only accept flags we have defined so far */
4437 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4438 ret = -EINVAL;
4439 goto out_loi;
4440 }
4441 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004442 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004443 }
4444
Jan Schmidtd7728c92011-07-07 16:48:38 +02004445 path = btrfs_alloc_path();
4446 if (!path) {
4447 ret = -ENOMEM;
4448 goto out;
4449 }
4450
Jan Schmidtd7728c92011-07-07 16:48:38 +02004451 inodes = init_data_container(size);
4452 if (IS_ERR(inodes)) {
4453 ret = PTR_ERR(inodes);
4454 inodes = NULL;
4455 goto out;
4456 }
4457
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004458 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004459 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004460 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004461 ret = -ENOENT;
4462 if (ret < 0)
4463 goto out;
4464
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004465 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4466 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004467 if (ret)
4468 ret = -EFAULT;
4469
4470out:
4471 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004472 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004473out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004474 kfree(loi);
4475
4476 return ret;
4477}
4478
David Sterba008ef092018-03-21 02:05:27 +01004479void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004480 struct btrfs_ioctl_balance_args *bargs)
4481{
4482 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4483
4484 bargs->flags = bctl->flags;
4485
David Sterba3009a622018-03-21 01:31:04 +01004486 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004487 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4488 if (atomic_read(&fs_info->balance_pause_req))
4489 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004490 if (atomic_read(&fs_info->balance_cancel_req))
4491 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004492
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004493 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4494 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4495 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004496
David Sterba008ef092018-03-21 02:05:27 +01004497 spin_lock(&fs_info->balance_lock);
4498 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4499 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004500}
4501
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004502static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004503{
Al Viro496ad9a2013-01-23 17:07:38 -05004504 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004505 struct btrfs_fs_info *fs_info = root->fs_info;
4506 struct btrfs_ioctl_balance_args *bargs;
4507 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004508 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004509 int ret;
4510
4511 if (!capable(CAP_SYS_ADMIN))
4512 return -EPERM;
4513
Liu Boe54bfa32012-06-29 03:58:48 -06004514 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004515 if (ret)
4516 return ret;
4517
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004518again:
David Sterba171938e2017-03-28 14:44:21 +02004519 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004520 mutex_lock(&fs_info->balance_mutex);
4521 need_unlock = true;
4522 goto locked;
4523 }
4524
4525 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004526 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004527 * (1) some other op is running
4528 * (2) balance is running
4529 * (3) balance is paused -- special case (think resume)
4530 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004531 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004532 if (fs_info->balance_ctl) {
4533 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004534 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004535 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004536 /*
4537 * Lock released to allow other waiters to continue,
4538 * we'll reexamine the status again.
4539 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004540 mutex_lock(&fs_info->balance_mutex);
4541
4542 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004543 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004544 /* this is (3) */
4545 need_unlock = false;
4546 goto locked;
4547 }
4548
4549 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004550 goto again;
4551 } else {
4552 /* this is (2) */
4553 mutex_unlock(&fs_info->balance_mutex);
4554 ret = -EINPROGRESS;
4555 goto out;
4556 }
4557 } else {
4558 /* this is (1) */
4559 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004560 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004561 goto out;
4562 }
4563
4564locked:
David Sterba171938e2017-03-28 14:44:21 +02004565 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004566
4567 if (arg) {
4568 bargs = memdup_user(arg, sizeof(*bargs));
4569 if (IS_ERR(bargs)) {
4570 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004571 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004572 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004573
4574 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4575 if (!fs_info->balance_ctl) {
4576 ret = -ENOTCONN;
4577 goto out_bargs;
4578 }
4579
4580 bctl = fs_info->balance_ctl;
4581 spin_lock(&fs_info->balance_lock);
4582 bctl->flags |= BTRFS_BALANCE_RESUME;
4583 spin_unlock(&fs_info->balance_lock);
4584
4585 goto do_balance;
4586 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004587 } else {
4588 bargs = NULL;
4589 }
4590
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004591 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004592 ret = -EINPROGRESS;
4593 goto out_bargs;
4594 }
4595
David Sterba8d2db782015-11-04 15:38:29 +01004596 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004597 if (!bctl) {
4598 ret = -ENOMEM;
4599 goto out_bargs;
4600 }
4601
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004602 if (arg) {
4603 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4604 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4605 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4606
4607 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004608 } else {
4609 /* balance everything - no filters */
4610 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004611 }
4612
David Sterba8eb93452015-10-12 16:55:54 +02004613 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4614 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004615 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004616 }
4617
Ilya Dryomovde322262012-01-16 22:04:49 +02004618do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004619 /*
David Sterba149196a2018-03-20 20:23:09 +01004620 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4621 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4622 * restriper was paused all the way until unmount, in free_fs_info.
4623 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004624 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004625 need_unlock = false;
4626
David Sterba6fcf6e22018-05-07 17:44:03 +02004627 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004628 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004629
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004630 if (arg) {
4631 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4632 ret = -EFAULT;
4633 }
4634
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004635out_bctl:
4636 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004637out_bargs:
4638 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004639out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004640 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004641 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004642 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004643out:
Liu Boe54bfa32012-06-29 03:58:48 -06004644 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004645 return ret;
4646}
4647
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004648static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004649{
4650 if (!capable(CAP_SYS_ADMIN))
4651 return -EPERM;
4652
4653 switch (cmd) {
4654 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004655 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004656 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004657 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004658 }
4659
4660 return -EINVAL;
4661}
4662
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004663static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004664 void __user *arg)
4665{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004666 struct btrfs_ioctl_balance_args *bargs;
4667 int ret = 0;
4668
4669 if (!capable(CAP_SYS_ADMIN))
4670 return -EPERM;
4671
4672 mutex_lock(&fs_info->balance_mutex);
4673 if (!fs_info->balance_ctl) {
4674 ret = -ENOTCONN;
4675 goto out;
4676 }
4677
David Sterba8d2db782015-11-04 15:38:29 +01004678 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004679 if (!bargs) {
4680 ret = -ENOMEM;
4681 goto out;
4682 }
4683
David Sterba008ef092018-03-21 02:05:27 +01004684 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004685
4686 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4687 ret = -EFAULT;
4688
4689 kfree(bargs);
4690out:
4691 mutex_unlock(&fs_info->balance_mutex);
4692 return ret;
4693}
4694
Miao Xie905b0dd2012-11-26 08:50:11 +00004695static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004696{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004697 struct inode *inode = file_inode(file);
4698 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004699 struct btrfs_ioctl_quota_ctl_args *sa;
4700 struct btrfs_trans_handle *trans = NULL;
4701 int ret;
4702 int err;
4703
4704 if (!capable(CAP_SYS_ADMIN))
4705 return -EPERM;
4706
Miao Xie905b0dd2012-11-26 08:50:11 +00004707 ret = mnt_want_write_file(file);
4708 if (ret)
4709 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004710
4711 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004712 if (IS_ERR(sa)) {
4713 ret = PTR_ERR(sa);
4714 goto drop_write;
4715 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004716
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004717 down_write(&fs_info->subvol_sem);
4718 trans = btrfs_start_transaction(fs_info->tree_root, 2);
Jan Schmidt2f232032013-04-25 16:04:51 +00004719 if (IS_ERR(trans)) {
4720 ret = PTR_ERR(trans);
4721 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004722 }
4723
4724 switch (sa->cmd) {
4725 case BTRFS_QUOTA_CTL_ENABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004726 ret = btrfs_quota_enable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004727 break;
4728 case BTRFS_QUOTA_CTL_DISABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004729 ret = btrfs_quota_disable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004730 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004731 default:
4732 ret = -EINVAL;
4733 break;
4734 }
4735
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004736 err = btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00004737 if (err && !ret)
4738 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004739out:
4740 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004741 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004742drop_write:
4743 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004744 return ret;
4745}
4746
Miao Xie905b0dd2012-11-26 08:50:11 +00004747static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004748{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004749 struct inode *inode = file_inode(file);
4750 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4751 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004752 struct btrfs_ioctl_qgroup_assign_args *sa;
4753 struct btrfs_trans_handle *trans;
4754 int ret;
4755 int err;
4756
4757 if (!capable(CAP_SYS_ADMIN))
4758 return -EPERM;
4759
Miao Xie905b0dd2012-11-26 08:50:11 +00004760 ret = mnt_want_write_file(file);
4761 if (ret)
4762 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004763
4764 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004765 if (IS_ERR(sa)) {
4766 ret = PTR_ERR(sa);
4767 goto drop_write;
4768 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004769
4770 trans = btrfs_join_transaction(root);
4771 if (IS_ERR(trans)) {
4772 ret = PTR_ERR(trans);
4773 goto out;
4774 }
4775
Arne Jansen5d13a372011-09-14 15:53:51 +02004776 if (sa->assign) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004777 ret = btrfs_add_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004778 sa->src, sa->dst);
4779 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004780 ret = btrfs_del_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004781 sa->src, sa->dst);
4782 }
4783
Qu Wenruoe082f562015-02-27 16:24:28 +08004784 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004785 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08004786 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004787 btrfs_handle_fs_error(fs_info, err,
4788 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004789 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004790 if (err && !ret)
4791 ret = err;
4792
4793out:
4794 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004795drop_write:
4796 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004797 return ret;
4798}
4799
Miao Xie905b0dd2012-11-26 08:50:11 +00004800static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004801{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004802 struct inode *inode = file_inode(file);
4803 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4804 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004805 struct btrfs_ioctl_qgroup_create_args *sa;
4806 struct btrfs_trans_handle *trans;
4807 int ret;
4808 int err;
4809
4810 if (!capable(CAP_SYS_ADMIN))
4811 return -EPERM;
4812
Miao Xie905b0dd2012-11-26 08:50:11 +00004813 ret = mnt_want_write_file(file);
4814 if (ret)
4815 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004816
4817 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004818 if (IS_ERR(sa)) {
4819 ret = PTR_ERR(sa);
4820 goto drop_write;
4821 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004822
Miao Xied86e56c2012-11-15 11:35:41 +00004823 if (!sa->qgroupid) {
4824 ret = -EINVAL;
4825 goto out;
4826 }
4827
Arne Jansen5d13a372011-09-14 15:53:51 +02004828 trans = btrfs_join_transaction(root);
4829 if (IS_ERR(trans)) {
4830 ret = PTR_ERR(trans);
4831 goto out;
4832 }
4833
Arne Jansen5d13a372011-09-14 15:53:51 +02004834 if (sa->create) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004835 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004836 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004837 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004838 }
4839
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004840 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004841 if (err && !ret)
4842 ret = err;
4843
4844out:
4845 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004846drop_write:
4847 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004848 return ret;
4849}
4850
Miao Xie905b0dd2012-11-26 08:50:11 +00004851static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004852{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004853 struct inode *inode = file_inode(file);
4854 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4855 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004856 struct btrfs_ioctl_qgroup_limit_args *sa;
4857 struct btrfs_trans_handle *trans;
4858 int ret;
4859 int err;
4860 u64 qgroupid;
4861
4862 if (!capable(CAP_SYS_ADMIN))
4863 return -EPERM;
4864
Miao Xie905b0dd2012-11-26 08:50:11 +00004865 ret = mnt_want_write_file(file);
4866 if (ret)
4867 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004868
4869 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004870 if (IS_ERR(sa)) {
4871 ret = PTR_ERR(sa);
4872 goto drop_write;
4873 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004874
4875 trans = btrfs_join_transaction(root);
4876 if (IS_ERR(trans)) {
4877 ret = PTR_ERR(trans);
4878 goto out;
4879 }
4880
4881 qgroupid = sa->qgroupid;
4882 if (!qgroupid) {
4883 /* take the current subvol as qgroup */
4884 qgroupid = root->root_key.objectid;
4885 }
4886
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004887 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004888
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004889 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004890 if (err && !ret)
4891 ret = err;
4892
4893out:
4894 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004895drop_write:
4896 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004897 return ret;
4898}
4899
Jan Schmidt2f232032013-04-25 16:04:51 +00004900static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4901{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004902 struct inode *inode = file_inode(file);
4903 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004904 struct btrfs_ioctl_quota_rescan_args *qsa;
4905 int ret;
4906
4907 if (!capable(CAP_SYS_ADMIN))
4908 return -EPERM;
4909
4910 ret = mnt_want_write_file(file);
4911 if (ret)
4912 return ret;
4913
4914 qsa = memdup_user(arg, sizeof(*qsa));
4915 if (IS_ERR(qsa)) {
4916 ret = PTR_ERR(qsa);
4917 goto drop_write;
4918 }
4919
4920 if (qsa->flags) {
4921 ret = -EINVAL;
4922 goto out;
4923 }
4924
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004925 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004926
4927out:
4928 kfree(qsa);
4929drop_write:
4930 mnt_drop_write_file(file);
4931 return ret;
4932}
4933
4934static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4935{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004936 struct inode *inode = file_inode(file);
4937 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004938 struct btrfs_ioctl_quota_rescan_args *qsa;
4939 int ret = 0;
4940
4941 if (!capable(CAP_SYS_ADMIN))
4942 return -EPERM;
4943
David Sterba8d2db782015-11-04 15:38:29 +01004944 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00004945 if (!qsa)
4946 return -ENOMEM;
4947
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004948 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00004949 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004950 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004951 }
4952
4953 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4954 ret = -EFAULT;
4955
4956 kfree(qsa);
4957 return ret;
4958}
4959
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004960static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4961{
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);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004964
4965 if (!capable(CAP_SYS_ADMIN))
4966 return -EPERM;
4967
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004968 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004969}
4970
Hugo Millsabccd002014-01-30 20:17:00 +00004971static long _btrfs_ioctl_set_received_subvol(struct file *file,
4972 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004973{
Al Viro496ad9a2013-01-23 17:07:38 -05004974 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004975 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004976 struct btrfs_root *root = BTRFS_I(inode)->root;
4977 struct btrfs_root_item *root_item = &root->root_item;
4978 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004979 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02004980 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004981 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004982
David Sterbabd60ea02014-01-16 15:50:22 +01004983 if (!inode_owner_or_capable(inode))
4984 return -EPERM;
4985
Alexander Block8ea05e32012-07-25 17:35:53 +02004986 ret = mnt_want_write_file(file);
4987 if (ret < 0)
4988 return ret;
4989
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004990 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02004991
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004992 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004993 ret = -EINVAL;
4994 goto out;
4995 }
4996
4997 if (btrfs_root_readonly(root)) {
4998 ret = -EROFS;
4999 goto out;
5000 }
5001
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005002 /*
5003 * 1 - root item
5004 * 2 - uuid items (received uuid + subvol uuid)
5005 */
5006 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005007 if (IS_ERR(trans)) {
5008 ret = PTR_ERR(trans);
5009 trans = NULL;
5010 goto out;
5011 }
5012
5013 sa->rtransid = trans->transid;
5014 sa->rtime.sec = ct.tv_sec;
5015 sa->rtime.nsec = ct.tv_nsec;
5016
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005017 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5018 BTRFS_UUID_SIZE);
5019 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005020 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08005021 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005022 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5023 root->root_key.objectid);
5024 if (ret && ret != -ENOENT) {
5025 btrfs_abort_transaction(trans, ret);
5026 btrfs_end_transaction(trans);
5027 goto out;
5028 }
5029 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005030 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5031 btrfs_set_root_stransid(root_item, sa->stransid);
5032 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005033 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5034 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5035 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5036 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005037
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005038 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005039 &root->root_key, &root->root_item);
5040 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005041 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005042 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005043 }
5044 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08005045 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005046 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5047 root->root_key.objectid);
5048 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005049 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005050 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005051 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005052 }
5053 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005054 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005055out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005056 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005057 mnt_drop_write_file(file);
5058 return ret;
5059}
5060
5061#ifdef CONFIG_64BIT
5062static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5063 void __user *arg)
5064{
5065 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5066 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5067 int ret = 0;
5068
5069 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305070 if (IS_ERR(args32))
5071 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005072
David Sterba8d2db782015-11-04 15:38:29 +01005073 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005074 if (!args64) {
5075 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005076 goto out;
5077 }
5078
5079 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5080 args64->stransid = args32->stransid;
5081 args64->rtransid = args32->rtransid;
5082 args64->stime.sec = args32->stime.sec;
5083 args64->stime.nsec = args32->stime.nsec;
5084 args64->rtime.sec = args32->rtime.sec;
5085 args64->rtime.nsec = args32->rtime.nsec;
5086 args64->flags = args32->flags;
5087
5088 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5089 if (ret)
5090 goto out;
5091
5092 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5093 args32->stransid = args64->stransid;
5094 args32->rtransid = args64->rtransid;
5095 args32->stime.sec = args64->stime.sec;
5096 args32->stime.nsec = args64->stime.nsec;
5097 args32->rtime.sec = args64->rtime.sec;
5098 args32->rtime.nsec = args64->rtime.nsec;
5099 args32->flags = args64->flags;
5100
5101 ret = copy_to_user(arg, args32, sizeof(*args32));
5102 if (ret)
5103 ret = -EFAULT;
5104
5105out:
5106 kfree(args32);
5107 kfree(args64);
5108 return ret;
5109}
5110#endif
5111
5112static long btrfs_ioctl_set_received_subvol(struct file *file,
5113 void __user *arg)
5114{
5115 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5116 int ret = 0;
5117
5118 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305119 if (IS_ERR(sa))
5120 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005121
5122 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5123
5124 if (ret)
5125 goto out;
5126
Alexander Block8ea05e32012-07-25 17:35:53 +02005127 ret = copy_to_user(arg, sa, sizeof(*sa));
5128 if (ret)
5129 ret = -EFAULT;
5130
5131out:
5132 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005133 return ret;
5134}
5135
jeff.liu867ab662013-01-05 02:48:01 +00005136static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5137{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005138 struct inode *inode = file_inode(file);
5139 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005140 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005141 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005142 char label[BTRFS_LABEL_SIZE];
5143
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005144 spin_lock(&fs_info->super_lock);
5145 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5146 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005147
5148 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005149
5150 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005151 btrfs_warn(fs_info,
5152 "label is too long, return the first %zu bytes",
5153 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005154 }
5155
jeff.liu867ab662013-01-05 02:48:01 +00005156 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005157
5158 return ret ? -EFAULT : 0;
5159}
5160
jeff.liua8bfd4a2013-01-05 02:48:08 +00005161static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5162{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005163 struct inode *inode = file_inode(file);
5164 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5165 struct btrfs_root *root = BTRFS_I(inode)->root;
5166 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005167 struct btrfs_trans_handle *trans;
5168 char label[BTRFS_LABEL_SIZE];
5169 int ret;
5170
5171 if (!capable(CAP_SYS_ADMIN))
5172 return -EPERM;
5173
5174 if (copy_from_user(label, arg, sizeof(label)))
5175 return -EFAULT;
5176
5177 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005178 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005179 "unable to set label with more than %d bytes",
5180 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005181 return -EINVAL;
5182 }
5183
5184 ret = mnt_want_write_file(file);
5185 if (ret)
5186 return ret;
5187
jeff.liua8bfd4a2013-01-05 02:48:08 +00005188 trans = btrfs_start_transaction(root, 0);
5189 if (IS_ERR(trans)) {
5190 ret = PTR_ERR(trans);
5191 goto out_unlock;
5192 }
5193
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005194 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005195 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005196 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005197 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005198
5199out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005200 mnt_drop_write_file(file);
5201 return ret;
5202}
5203
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005204#define INIT_FEATURE_FLAGS(suffix) \
5205 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5206 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5207 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5208
David Sterbad5131b62016-02-17 15:26:27 +01005209int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005210{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005211 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005212 INIT_FEATURE_FLAGS(SUPP),
5213 INIT_FEATURE_FLAGS(SAFE_SET),
5214 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5215 };
5216
5217 if (copy_to_user(arg, &features, sizeof(features)))
5218 return -EFAULT;
5219
5220 return 0;
5221}
5222
5223static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5224{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005225 struct inode *inode = file_inode(file);
5226 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5227 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005228 struct btrfs_ioctl_feature_flags features;
5229
5230 features.compat_flags = btrfs_super_compat_flags(super_block);
5231 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5232 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5233
5234 if (copy_to_user(arg, &features, sizeof(features)))
5235 return -EFAULT;
5236
5237 return 0;
5238}
5239
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005240static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005241 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005242 u64 change_mask, u64 flags, u64 supported_flags,
5243 u64 safe_set, u64 safe_clear)
5244{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005245 const char *type = btrfs_feature_set_names[set];
5246 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005247 u64 disallowed, unsupported;
5248 u64 set_mask = flags & change_mask;
5249 u64 clear_mask = ~flags & change_mask;
5250
5251 unsupported = set_mask & ~supported_flags;
5252 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005253 names = btrfs_printable_features(set, unsupported);
5254 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005255 btrfs_warn(fs_info,
5256 "this kernel does not support the %s feature bit%s",
5257 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005258 kfree(names);
5259 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005260 btrfs_warn(fs_info,
5261 "this kernel does not support %s bits 0x%llx",
5262 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005263 return -EOPNOTSUPP;
5264 }
5265
5266 disallowed = set_mask & ~safe_set;
5267 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005268 names = btrfs_printable_features(set, disallowed);
5269 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005270 btrfs_warn(fs_info,
5271 "can't set the %s feature bit%s while mounted",
5272 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005273 kfree(names);
5274 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005275 btrfs_warn(fs_info,
5276 "can't set %s bits 0x%llx while mounted",
5277 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005278 return -EPERM;
5279 }
5280
5281 disallowed = clear_mask & ~safe_clear;
5282 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005283 names = btrfs_printable_features(set, disallowed);
5284 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005285 btrfs_warn(fs_info,
5286 "can't clear the %s feature bit%s while mounted",
5287 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005288 kfree(names);
5289 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005290 btrfs_warn(fs_info,
5291 "can't clear %s bits 0x%llx while mounted",
5292 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005293 return -EPERM;
5294 }
5295
5296 return 0;
5297}
5298
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005299#define check_feature(fs_info, change_mask, flags, mask_base) \
5300check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005301 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5302 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5303 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5304
5305static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5306{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005307 struct inode *inode = file_inode(file);
5308 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5309 struct btrfs_root *root = BTRFS_I(inode)->root;
5310 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005311 struct btrfs_ioctl_feature_flags flags[2];
5312 struct btrfs_trans_handle *trans;
5313 u64 newflags;
5314 int ret;
5315
5316 if (!capable(CAP_SYS_ADMIN))
5317 return -EPERM;
5318
5319 if (copy_from_user(flags, arg, sizeof(flags)))
5320 return -EFAULT;
5321
5322 /* Nothing to do */
5323 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5324 !flags[0].incompat_flags)
5325 return 0;
5326
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005327 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005328 flags[1].compat_flags, COMPAT);
5329 if (ret)
5330 return ret;
5331
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005332 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005333 flags[1].compat_ro_flags, COMPAT_RO);
5334 if (ret)
5335 return ret;
5336
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005337 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005338 flags[1].incompat_flags, INCOMPAT);
5339 if (ret)
5340 return ret;
5341
David Sterba7ab19622016-05-04 11:32:00 +02005342 ret = mnt_want_write_file(file);
5343 if (ret)
5344 return ret;
5345
David Sterba8051aa12014-02-07 14:34:04 +01005346 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005347 if (IS_ERR(trans)) {
5348 ret = PTR_ERR(trans);
5349 goto out_drop_write;
5350 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005351
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005352 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005353 newflags = btrfs_super_compat_flags(super_block);
5354 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5355 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5356 btrfs_set_super_compat_flags(super_block, newflags);
5357
5358 newflags = btrfs_super_compat_ro_flags(super_block);
5359 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5360 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5361 btrfs_set_super_compat_ro_flags(super_block, newflags);
5362
5363 newflags = btrfs_super_incompat_flags(super_block);
5364 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5365 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5366 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005367 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005368
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005369 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005370out_drop_write:
5371 mnt_drop_write_file(file);
5372
5373 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005374}
5375
Josef Bacik2351f432017-09-27 10:43:13 -04005376static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5377{
5378 struct btrfs_ioctl_send_args *arg;
5379 int ret;
5380
5381 if (compat) {
5382#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5383 struct btrfs_ioctl_send_args_32 args32;
5384
5385 ret = copy_from_user(&args32, argp, sizeof(args32));
5386 if (ret)
5387 return -EFAULT;
5388 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5389 if (!arg)
5390 return -ENOMEM;
5391 arg->send_fd = args32.send_fd;
5392 arg->clone_sources_count = args32.clone_sources_count;
5393 arg->clone_sources = compat_ptr(args32.clone_sources);
5394 arg->parent_root = args32.parent_root;
5395 arg->flags = args32.flags;
5396 memcpy(arg->reserved, args32.reserved,
5397 sizeof(args32.reserved));
5398#else
5399 return -ENOTTY;
5400#endif
5401 } else {
5402 arg = memdup_user(argp, sizeof(*arg));
5403 if (IS_ERR(arg))
5404 return PTR_ERR(arg);
5405 }
5406 ret = btrfs_ioctl_send(file, arg);
5407 kfree(arg);
5408 return ret;
5409}
5410
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005411long btrfs_ioctl(struct file *file, unsigned int
5412 cmd, unsigned long arg)
5413{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005414 struct inode *inode = file_inode(file);
5415 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5416 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005417 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005418
5419 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005420 case FS_IOC_GETFLAGS:
5421 return btrfs_ioctl_getflags(file, argp);
5422 case FS_IOC_SETFLAGS:
5423 return btrfs_ioctl_setflags(file, argp);
5424 case FS_IOC_GETVERSION:
5425 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005426 case FITRIM:
5427 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005428 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005429 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005430 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005431 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005432 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005433 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005434 case BTRFS_IOC_SUBVOL_CREATE_V2:
5435 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005436 case BTRFS_IOC_SNAP_DESTROY:
5437 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005438 case BTRFS_IOC_SUBVOL_GETFLAGS:
5439 return btrfs_ioctl_subvol_getflags(file, argp);
5440 case BTRFS_IOC_SUBVOL_SETFLAGS:
5441 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005442 case BTRFS_IOC_DEFAULT_SUBVOL:
5443 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005444 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005445 return btrfs_ioctl_defrag(file, NULL);
5446 case BTRFS_IOC_DEFRAG_RANGE:
5447 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005448 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005449 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005450 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005451 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005452 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005453 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005454 case BTRFS_IOC_RM_DEV_V2:
5455 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005456 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005457 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005458 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005459 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005460 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005461 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005462 case BTRFS_IOC_TREE_SEARCH:
5463 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005464 case BTRFS_IOC_TREE_SEARCH_V2:
5465 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005466 case BTRFS_IOC_INO_LOOKUP:
5467 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005468 case BTRFS_IOC_INO_PATHS:
5469 return btrfs_ioctl_ino_to_path(root, argp);
5470 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005471 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5472 case BTRFS_IOC_LOGICAL_INO_V2:
5473 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005474 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005475 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005476 case BTRFS_IOC_SYNC: {
5477 int ret;
5478
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005479 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005480 if (ret)
5481 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005482 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005483 /*
5484 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005485 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005486 * processing uncleaned subvols.
5487 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005488 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005489 return ret;
5490 }
Sage Weil46204592010-10-29 15:41:32 -04005491 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005492 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005493 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005494 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005495 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005496 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005497 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005498 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005499 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005500 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005501 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005502 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005503 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005504 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005505 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005506 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005507 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5508 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005509#ifdef CONFIG_64BIT
5510 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5511 return btrfs_ioctl_set_received_subvol_32(file, argp);
5512#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005513 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005514 return _btrfs_ioctl_send(file, argp, false);
5515#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5516 case BTRFS_IOC_SEND_32:
5517 return _btrfs_ioctl_send(file, argp, true);
5518#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005519 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005520 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005521 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005522 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005523 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005524 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005525 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005526 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005527 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005528 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005529 case BTRFS_IOC_QUOTA_RESCAN:
5530 return btrfs_ioctl_quota_rescan(file, argp);
5531 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5532 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005533 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5534 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005535 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005536 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005537 case BTRFS_IOC_GET_FSLABEL:
5538 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005539 case BTRFS_IOC_SET_FSLABEL:
5540 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005541 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005542 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005543 case BTRFS_IOC_GET_FEATURES:
5544 return btrfs_ioctl_get_features(file, argp);
5545 case BTRFS_IOC_SET_FEATURES:
5546 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005547 case FS_IOC_FSGETXATTR:
5548 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005549 case FS_IOC_FSSETXATTR:
5550 return btrfs_ioctl_fssetxattr(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005551 }
5552
5553 return -ENOTTY;
5554}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005555
5556#ifdef CONFIG_COMPAT
5557long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5558{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005559 /*
5560 * These all access 32-bit values anyway so no further
5561 * handling is necessary.
5562 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005563 switch (cmd) {
5564 case FS_IOC32_GETFLAGS:
5565 cmd = FS_IOC_GETFLAGS;
5566 break;
5567 case FS_IOC32_SETFLAGS:
5568 cmd = FS_IOC_SETFLAGS;
5569 break;
5570 case FS_IOC32_GETVERSION:
5571 cmd = FS_IOC_GETVERSION;
5572 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005573 }
5574
5575 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5576}
5577#endif