blob: a247991856a0c153d8fd8c420cc4f78b577e518d [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004 */
5
6#include <linux/kernel.h>
7#include <linux/bio.h>
8#include <linux/buffer_head.h>
9#include <linux/file.h>
10#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040011#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040012#include <linux/pagemap.h>
13#include <linux/highmem.h>
14#include <linux/time.h>
15#include <linux/init.h>
16#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040017#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040018#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040019#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040020#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040021#include <linux/swap.h>
22#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040023#include <linux/compat.h>
24#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040025#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040026#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020027#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000029#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020030#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000031#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070032#include <linux/uaccess.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050033#include <linux/iversion.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include "ctree.h"
35#include "disk-io.h"
36#include "transaction.h"
37#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040038#include "print-tree.h"
39#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040040#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080041#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020042#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040043#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020044#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010045#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000046#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040047#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070048#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000049#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080050#include "compression.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040051
Hugo Millsabccd002014-01-30 20:17:00 +000052#ifdef CONFIG_64BIT
53/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
54 * structures are incorrect, as the timespec structure from userspace
55 * is 4 bytes too small. We define these alternatives here to teach
56 * the kernel about the 32-bit struct packing.
57 */
58struct btrfs_ioctl_timespec_32 {
59 __u64 sec;
60 __u32 nsec;
61} __attribute__ ((__packed__));
62
63struct btrfs_ioctl_received_subvol_args_32 {
64 char uuid[BTRFS_UUID_SIZE]; /* in */
65 __u64 stransid; /* in */
66 __u64 rtransid; /* out */
67 struct btrfs_ioctl_timespec_32 stime; /* in */
68 struct btrfs_ioctl_timespec_32 rtime; /* out */
69 __u64 flags; /* in */
70 __u64 reserved[16]; /* in */
71} __attribute__ ((__packed__));
72
73#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
74 struct btrfs_ioctl_received_subvol_args_32)
75#endif
76
Josef Bacik2351f432017-09-27 10:43:13 -040077#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
78struct btrfs_ioctl_send_args_32 {
79 __s64 send_fd; /* in */
80 __u64 clone_sources_count; /* in */
81 compat_uptr_t clone_sources; /* in */
82 __u64 parent_root; /* in */
83 __u64 flags; /* in */
84 __u64 reserved[4]; /* in */
85} __attribute__ ((__packed__));
86
87#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
88 struct btrfs_ioctl_send_args_32)
89#endif
Hugo Millsabccd002014-01-30 20:17:00 +000090
Mark Fasheh416161d2013-08-06 11:42:51 -070091static int btrfs_clone(struct inode *src, struct inode *inode,
Mark Fasheh1c919a52015-06-30 14:42:08 -070092 u64 off, u64 olen, u64 olen_aligned, u64 destoff,
93 int no_time_update);
Mark Fasheh416161d2013-08-06 11:42:51 -070094
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020096static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
97 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020098{
David Sterba1905a0f2018-03-26 18:52:15 +020099 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200100 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +0200101 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200102 return flags & ~FS_DIRSYNC_FL;
103 else
104 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
105}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400106
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200107/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200108 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
109 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200110 */
David Sterbaa157d4f2018-03-26 19:12:25 +0200111static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200112{
113 unsigned int iflags = 0;
114
115 if (flags & BTRFS_INODE_SYNC)
116 iflags |= FS_SYNC_FL;
117 if (flags & BTRFS_INODE_IMMUTABLE)
118 iflags |= FS_IMMUTABLE_FL;
119 if (flags & BTRFS_INODE_APPEND)
120 iflags |= FS_APPEND_FL;
121 if (flags & BTRFS_INODE_NODUMP)
122 iflags |= FS_NODUMP_FL;
123 if (flags & BTRFS_INODE_NOATIME)
124 iflags |= FS_NOATIME_FL;
125 if (flags & BTRFS_INODE_DIRSYNC)
126 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000127 if (flags & BTRFS_INODE_NODATACOW)
128 iflags |= FS_NOCOW_FL;
129
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900130 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000131 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900132 else if (flags & BTRFS_INODE_COMPRESS)
133 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200134
135 return iflags;
136}
137
138/*
139 * Update inode->i_flags based on the btrfs internal flags.
140 */
David Sterba7b6a2212018-03-26 18:40:21 +0200141void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200142{
David Sterba5c57b8b2018-04-23 15:45:18 +0200143 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100144 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200145
David Sterba5c57b8b2018-04-23 15:45:18 +0200146 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100147 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200148 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100149 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200150 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100151 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200152 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100153 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200154 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100155 new_fl |= S_DIRSYNC;
156
157 set_mask_bits(&inode->i_flags,
158 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
159 new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200160}
161
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200162static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
163{
David Sterba5c57b8b2018-04-23 15:45:18 +0200164 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
165 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200166
167 if (copy_to_user(arg, &flags, sizeof(flags)))
168 return -EFAULT;
169 return 0;
170}
171
David Sterba5ba76ab2018-03-26 18:52:15 +0200172/* Check if @flags are a supported and valid set of FS_*_FL flags */
173static int check_fsflags(unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000174{
175 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
176 FS_NOATIME_FL | FS_NODUMP_FL | \
177 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000178 FS_NOCOMP_FL | FS_COMPR_FL |
179 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000180 return -EOPNOTSUPP;
181
182 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
183 return -EINVAL;
184
Liu Bo75e7cb72011-03-22 10:12:20 +0000185 return 0;
186}
187
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200188static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
189{
Al Viro496ad9a2013-01-23 17:07:38 -0500190 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400191 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200192 struct btrfs_inode *binode = BTRFS_I(inode);
193 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200194 struct btrfs_trans_handle *trans;
David Sterba5c57b8b2018-04-23 15:45:18 +0200195 unsigned int fsflags, old_fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200196 int ret;
David Sterba5c57b8b2018-04-23 15:45:18 +0200197 u64 old_flags;
198 unsigned int old_i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600199 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200200
David Sterbabd60ea02014-01-16 15:50:22 +0100201 if (!inode_owner_or_capable(inode))
202 return -EPERM;
203
Li Zefanb83cc962010-12-20 16:04:08 +0800204 if (btrfs_root_readonly(root))
205 return -EROFS;
206
David Sterba5c57b8b2018-04-23 15:45:18 +0200207 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200208 return -EFAULT;
209
David Sterba5c57b8b2018-04-23 15:45:18 +0200210 ret = check_fsflags(fsflags);
Liu Bo75e7cb72011-03-22 10:12:20 +0000211 if (ret)
212 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200213
Jan Karae7848682012-06-12 16:20:32 +0200214 ret = mnt_want_write_file(file);
215 if (ret)
216 return ret;
217
Al Viro59551022016-01-22 15:40:57 -0500218 inode_lock(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200219
David Sterba5c57b8b2018-04-23 15:45:18 +0200220 old_flags = binode->flags;
221 old_i_flags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600222 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800223
David Sterba5c57b8b2018-04-23 15:45:18 +0200224 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
225 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
226 if ((fsflags ^ old_fsflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200227 if (!capable(CAP_LINUX_IMMUTABLE)) {
228 ret = -EPERM;
229 goto out_unlock;
230 }
231 }
232
David Sterba5c57b8b2018-04-23 15:45:18 +0200233 if (fsflags & FS_SYNC_FL)
234 binode->flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200235 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200236 binode->flags &= ~BTRFS_INODE_SYNC;
237 if (fsflags & FS_IMMUTABLE_FL)
238 binode->flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200239 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200240 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
241 if (fsflags & FS_APPEND_FL)
242 binode->flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200243 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200244 binode->flags &= ~BTRFS_INODE_APPEND;
245 if (fsflags & FS_NODUMP_FL)
246 binode->flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200247 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200248 binode->flags &= ~BTRFS_INODE_NODUMP;
249 if (fsflags & FS_NOATIME_FL)
250 binode->flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200251 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200252 binode->flags &= ~BTRFS_INODE_NOATIME;
253 if (fsflags & FS_DIRSYNC_FL)
254 binode->flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200255 else
David Sterba5c57b8b2018-04-23 15:45:18 +0200256 binode->flags &= ~BTRFS_INODE_DIRSYNC;
257 if (fsflags & FS_NOCOW_FL) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600258 if (S_ISREG(mode)) {
259 /*
260 * It's safe to turn csums off here, no extents exist.
261 * Otherwise we want the flag to reflect the real COW
262 * status of the file and will not set it.
263 */
264 if (inode->i_size == 0)
David Sterba5c57b8b2018-04-23 15:45:18 +0200265 binode->flags |= BTRFS_INODE_NODATACOW
266 | BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600267 } else {
David Sterba5c57b8b2018-04-23 15:45:18 +0200268 binode->flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600269 }
270 } else {
271 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400272 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600273 */
274 if (S_ISREG(mode)) {
275 if (inode->i_size == 0)
David Sterba5c57b8b2018-04-23 15:45:18 +0200276 binode->flags &= ~(BTRFS_INODE_NODATACOW
David Sterba7e97b8d2012-09-07 05:56:55 -0600277 | BTRFS_INODE_NODATASUM);
278 } else {
David Sterba5c57b8b2018-04-23 15:45:18 +0200279 binode->flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600280 }
281 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200282
Liu Bo75e7cb72011-03-22 10:12:20 +0000283 /*
284 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
285 * flag may be changed automatically if compression code won't make
286 * things smaller.
287 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200288 if (fsflags & FS_NOCOMP_FL) {
289 binode->flags &= ~BTRFS_INODE_COMPRESS;
290 binode->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000291
292 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
293 if (ret && ret != -ENODATA)
294 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200295 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000296 const char *comp;
297
David Sterba5c57b8b2018-04-23 15:45:18 +0200298 binode->flags |= BTRFS_INODE_COMPRESS;
299 binode->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000300
David Sterba93370502017-10-31 17:32:41 +0100301 comp = btrfs_compress_type2str(fs_info->compress_type);
302 if (!comp || comp[0] == 0)
303 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
304
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000305 ret = btrfs_set_prop(inode, "btrfs.compression",
306 comp, strlen(comp), 0);
307 if (ret)
308 goto out_drop;
309
Li Zefanebcb9042011-04-15 03:03:17 +0000310 } else {
Filipe Manana78a017a2014-09-11 11:44:49 +0100311 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
312 if (ret && ret != -ENODATA)
313 goto out_drop;
David Sterba5c57b8b2018-04-23 15:45:18 +0200314 binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000315 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200316
Li Zefan4da6f1a2011-12-29 13:39:50 +0800317 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800318 if (IS_ERR(trans)) {
319 ret = PTR_ERR(trans);
320 goto out_drop;
321 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200322
David Sterba7b6a2212018-03-26 18:40:21 +0200323 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400324 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700325 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200326 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200327
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400328 btrfs_end_transaction(trans);
Li Zefanf062abf02011-12-29 13:36:45 +0800329 out_drop:
330 if (ret) {
David Sterba5c57b8b2018-04-23 15:45:18 +0200331 binode->flags = old_flags;
332 inode->i_flags = old_i_flags;
Li Zefanf062abf02011-12-29 13:36:45 +0800333 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200334
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200335 out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500336 inode_unlock(inode);
Jan Karae7848682012-06-12 16:20:32 +0200337 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000338 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200339}
340
David Sterba19f93b32018-03-26 19:42:05 +0200341/*
342 * Translate btrfs internal inode flags to xflags as expected by the
343 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
344 * silently dropped.
345 */
346static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
347{
348 unsigned int xflags = 0;
349
350 if (flags & BTRFS_INODE_APPEND)
351 xflags |= FS_XFLAG_APPEND;
352 if (flags & BTRFS_INODE_IMMUTABLE)
353 xflags |= FS_XFLAG_IMMUTABLE;
354 if (flags & BTRFS_INODE_NOATIME)
355 xflags |= FS_XFLAG_NOATIME;
356 if (flags & BTRFS_INODE_NODUMP)
357 xflags |= FS_XFLAG_NODUMP;
358 if (flags & BTRFS_INODE_SYNC)
359 xflags |= FS_XFLAG_SYNC;
360
361 return xflags;
362}
363
364/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
365static int check_xflags(unsigned int flags)
366{
367 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
368 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
369 return -EOPNOTSUPP;
370 return 0;
371}
372
David Sterbae4202ac2018-03-26 19:51:16 +0200373/*
374 * Set the xflags from the internal inode flags. The remaining items of fsxattr
375 * are zeroed.
376 */
377static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
378{
379 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
380 struct fsxattr fa;
381
382 memset(&fa, 0, sizeof(fa));
383 fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags);
384
385 if (copy_to_user(arg, &fa, sizeof(fa)))
386 return -EFAULT;
387
388 return 0;
389}
390
David Sterba025f2122018-03-26 19:51:16 +0200391static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
392{
393 struct inode *inode = file_inode(file);
394 struct btrfs_inode *binode = BTRFS_I(inode);
395 struct btrfs_root *root = binode->root;
396 struct btrfs_trans_handle *trans;
397 struct fsxattr fa;
398 unsigned old_flags;
399 unsigned old_i_flags;
400 int ret = 0;
401
402 if (!inode_owner_or_capable(inode))
403 return -EPERM;
404
405 if (btrfs_root_readonly(root))
406 return -EROFS;
407
408 memset(&fa, 0, sizeof(fa));
409 if (copy_from_user(&fa, arg, sizeof(fa)))
410 return -EFAULT;
411
412 ret = check_xflags(fa.fsx_xflags);
413 if (ret)
414 return ret;
415
416 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
417 return -EOPNOTSUPP;
418
419 ret = mnt_want_write_file(file);
420 if (ret)
421 return ret;
422
423 inode_lock(inode);
424
425 old_flags = binode->flags;
426 old_i_flags = inode->i_flags;
427
428 /* We need the capabilities to change append-only or immutable inode */
429 if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) ||
430 (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) &&
431 !capable(CAP_LINUX_IMMUTABLE)) {
432 ret = -EPERM;
433 goto out_unlock;
434 }
435
436 if (fa.fsx_xflags & FS_XFLAG_SYNC)
437 binode->flags |= BTRFS_INODE_SYNC;
438 else
439 binode->flags &= ~BTRFS_INODE_SYNC;
440 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
441 binode->flags |= BTRFS_INODE_IMMUTABLE;
442 else
443 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
444 if (fa.fsx_xflags & FS_XFLAG_APPEND)
445 binode->flags |= BTRFS_INODE_APPEND;
446 else
447 binode->flags &= ~BTRFS_INODE_APPEND;
448 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
449 binode->flags |= BTRFS_INODE_NODUMP;
450 else
451 binode->flags &= ~BTRFS_INODE_NODUMP;
452 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
453 binode->flags |= BTRFS_INODE_NOATIME;
454 else
455 binode->flags &= ~BTRFS_INODE_NOATIME;
456
457 /* 1 item for the inode */
458 trans = btrfs_start_transaction(root, 1);
459 if (IS_ERR(trans)) {
460 ret = PTR_ERR(trans);
461 goto out_unlock;
462 }
463
464 btrfs_sync_inode_flags_to_i_flags(inode);
465 inode_inc_iversion(inode);
466 inode->i_ctime = current_time(inode);
467 ret = btrfs_update_inode(trans, root, inode);
468
469 btrfs_end_transaction(trans);
470
471out_unlock:
472 if (ret) {
473 binode->flags = old_flags;
474 inode->i_flags = old_i_flags;
475 }
476
477 inode_unlock(inode);
478 mnt_drop_write_file(file);
479
480 return ret;
481}
482
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200483static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
484{
Al Viro496ad9a2013-01-23 17:07:38 -0500485 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200486
487 return put_user(inode->i_generation, arg);
488}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400489
Li Dongyangf7039b12011-03-24 10:24:28 +0000490static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
491{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400492 struct inode *inode = file_inode(file);
493 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000494 struct btrfs_device *device;
495 struct request_queue *q;
496 struct fstrim_range range;
497 u64 minlen = ULLONG_MAX;
498 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500499 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000500 int ret;
501
502 if (!capable(CAP_SYS_ADMIN))
503 return -EPERM;
504
Xiao Guangrong1f781602011-04-20 10:09:16 +0000505 rcu_read_lock();
506 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
507 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000508 if (!device->bdev)
509 continue;
510 q = bdev_get_queue(device->bdev);
511 if (blk_queue_discard(q)) {
512 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100513 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000514 minlen);
515 }
516 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000517 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200518
Li Dongyangf7039b12011-03-24 10:24:28 +0000519 if (!num_devices)
520 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000521 if (copy_from_user(&range, arg, sizeof(range)))
522 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000523 if (range.start > total_bytes ||
524 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200525 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000526
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200527 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000528 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400529 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000530 if (ret < 0)
531 return ret;
532
533 if (copy_to_user(arg, &range, sizeof(range)))
534 return -EFAULT;
535
536 return 0;
537}
538
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200539int btrfs_is_empty_uuid(u8 *uuid)
540{
Chris Mason46e0f662013-11-15 12:14:55 +0100541 int i;
542
543 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
544 if (uuid[i])
545 return 0;
546 }
547 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200548}
549
Miao Xied5c12072013-02-28 10:04:33 +0000550static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400551 struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100552 const char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200553 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000554 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400556 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400557 struct btrfs_trans_handle *trans;
558 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100559 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400560 struct btrfs_inode_item *inode_item;
561 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000562 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400563 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000564 struct btrfs_block_rsv block_rsv;
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700565 struct timespec cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900566 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400567 int ret;
568 int err;
569 u64 objectid;
570 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500571 u64 index = 0;
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
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400721 ret = btrfs_uuid_tree_add(trans, fs_info, 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 Boe1f041e2012-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);
2158 return -ENOENT;
2159 }
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)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002292 btrfs_err(info, "could not find root %llu", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002293 ret = -ENOENT;
2294 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002295 }
2296
2297 key.objectid = dirid;
2298 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002299 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002300
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302301 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2303 if (ret < 0)
2304 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002305 else if (ret > 0) {
2306 ret = btrfs_previous_item(root, path, dirid,
2307 BTRFS_INODE_REF_KEY);
2308 if (ret < 0)
2309 goto out;
2310 else if (ret > 0) {
2311 ret = -ENOENT;
2312 goto out;
2313 }
2314 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002315
2316 l = path->nodes[0];
2317 slot = path->slots[0];
2318 btrfs_item_key_to_cpu(l, &key, slot);
2319
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002320 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2321 len = btrfs_inode_ref_name_len(l, iref);
2322 ptr -= len + 1;
2323 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002324 if (ptr < name) {
2325 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002326 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002327 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002328
2329 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302330 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002331
2332 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2333 break;
2334
David Sterbab3b4aa72011-04-21 01:20:15 +02002335 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002336 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002337 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002338 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002339 }
Li Zefan77906a502011-07-14 03:16:00 +00002340 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302341 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002342 ret = 0;
2343out:
2344 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002345 return ret;
2346}
2347
2348static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2349 void __user *argp)
2350{
2351 struct btrfs_ioctl_ino_lookup_args *args;
2352 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002353 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002354
Julia Lawall2354d082010-10-29 15:14:18 -04002355 args = memdup_user(argp, sizeof(*args));
2356 if (IS_ERR(args))
2357 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002358
Al Viro496ad9a2013-01-23 17:07:38 -05002359 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002360
David Sterba01b810b2015-05-12 19:14:49 +02002361 /*
2362 * Unprivileged query to obtain the containing subvolume root id. The
2363 * path is reset so it's consistent with btrfs_search_path_in_tree.
2364 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002365 if (args->treeid == 0)
2366 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2367
David Sterba01b810b2015-05-12 19:14:49 +02002368 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2369 args->name[0] = 0;
2370 goto out;
2371 }
2372
2373 if (!capable(CAP_SYS_ADMIN)) {
2374 ret = -EPERM;
2375 goto out;
2376 }
2377
Chris Masonac8e9812010-02-28 15:39:26 -05002378 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2379 args->treeid, args->objectid,
2380 args->name);
2381
David Sterba01b810b2015-05-12 19:14:49 +02002382out:
Chris Masonac8e9812010-02-28 15:39:26 -05002383 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2384 ret = -EFAULT;
2385
2386 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002387 return ret;
2388}
2389
Yan, Zheng76dda932009-09-21 16:00:26 -04002390static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2391 void __user *arg)
2392{
Al Viro54563d42013-09-01 15:57:51 -04002393 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002394 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002395 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002396 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002397 struct inode *inode;
2398 struct btrfs_root *root = BTRFS_I(dir)->root;
2399 struct btrfs_root *dest = NULL;
2400 struct btrfs_ioctl_vol_args *vol_args;
Yan, Zheng76dda932009-09-21 16:00:26 -04002401 int namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002402 int err = 0;
2403
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002404 if (!S_ISDIR(dir->i_mode))
2405 return -ENOTDIR;
2406
Yan, Zheng76dda932009-09-21 16:00:26 -04002407 vol_args = memdup_user(arg, sizeof(*vol_args));
2408 if (IS_ERR(vol_args))
2409 return PTR_ERR(vol_args);
2410
2411 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2412 namelen = strlen(vol_args->name);
2413 if (strchr(vol_args->name, '/') ||
2414 strncmp(vol_args->name, "..", namelen) == 0) {
2415 err = -EINVAL;
2416 goto out;
2417 }
2418
Al Viroa561be72011-11-23 11:57:51 -05002419 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002420 if (err)
2421 goto out;
2422
David Sterba521e0542014-04-15 16:41:44 +02002423
Al Viro00235412016-05-26 00:05:12 -04002424 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2425 if (err == -EINTR)
2426 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002427 dentry = lookup_one_len(vol_args->name, parent, namelen);
2428 if (IS_ERR(dentry)) {
2429 err = PTR_ERR(dentry);
2430 goto out_unlock_dir;
2431 }
2432
David Howells2b0143b2015-03-17 22:25:59 +00002433 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002434 err = -ENOENT;
2435 goto out_dput;
2436 }
2437
David Howells2b0143b2015-03-17 22:25:59 +00002438 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04002439 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302440 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002441 /*
2442 * Regular user. Only allow this with a special mount
2443 * option, when the user has write+exec access to the
2444 * subvol root, and when rmdir(2) would have been
2445 * allowed.
2446 *
2447 * Note that this is _not_ check that the subvol is
2448 * empty or doesn't contain data that we wouldn't
2449 * otherwise be able to delete.
2450 *
2451 * Users who want to delete empty subvols should try
2452 * rmdir(2).
2453 */
2454 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002455 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04002456 goto out_dput;
2457
2458 /*
2459 * Do not allow deletion if the parent dir is the same
2460 * as the dir to be deleted. That means the ioctl
2461 * must be called on the dentry referencing the root
2462 * of the subvol, not a random directory contained
2463 * within it.
2464 */
2465 err = -EINVAL;
2466 if (root == dest)
2467 goto out_dput;
2468
2469 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2470 if (err)
2471 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002472 }
2473
Miao Xie5c39da52012-10-22 11:39:53 +00002474 /* check if subvolume may be deleted by a user */
2475 err = btrfs_may_delete(dir, dentry, 1);
2476 if (err)
2477 goto out_dput;
2478
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002479 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002480 err = -EINVAL;
2481 goto out_dput;
2482 }
2483
Al Viro59551022016-01-22 15:40:57 -05002484 inode_lock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002485 err = btrfs_delete_subvolume(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -05002486 inode_unlock(inode);
Misono Tomohirof60a2362018-04-18 11:34:52 +09002487 if (!err)
Yan, Zheng76dda932009-09-21 16:00:26 -04002488 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002489
Yan, Zheng76dda932009-09-21 16:00:26 -04002490out_dput:
2491 dput(dentry);
2492out_unlock_dir:
Al Viro59551022016-01-22 15:40:57 -05002493 inode_unlock(dir);
Al Viro00235412016-05-26 00:05:12 -04002494out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002495 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002496out:
2497 kfree(vol_args);
2498 return err;
2499}
2500
Chris Mason1e701a32010-03-11 09:42:04 -05002501static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002502{
Al Viro496ad9a2013-01-23 17:07:38 -05002503 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002504 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002505 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002506 int ret;
2507
Ilya Dryomov25122d12013-01-20 15:57:57 +02002508 ret = mnt_want_write_file(file);
2509 if (ret)
2510 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002511
Ilya Dryomov25122d12013-01-20 15:57:57 +02002512 if (btrfs_root_readonly(root)) {
2513 ret = -EROFS;
2514 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002515 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002516
2517 switch (inode->i_mode & S_IFMT) {
2518 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002519 if (!capable(CAP_SYS_ADMIN)) {
2520 ret = -EPERM;
2521 goto out;
2522 }
Eric Sandeende78b512013-01-31 18:21:12 +00002523 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002524 break;
2525 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002526 if (!(file->f_mode & FMODE_WRITE)) {
2527 ret = -EINVAL;
2528 goto out;
2529 }
Chris Mason1e701a32010-03-11 09:42:04 -05002530
2531 range = kzalloc(sizeof(*range), GFP_KERNEL);
2532 if (!range) {
2533 ret = -ENOMEM;
2534 goto out;
2535 }
2536
2537 if (argp) {
2538 if (copy_from_user(range, argp,
2539 sizeof(*range))) {
2540 ret = -EFAULT;
2541 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002542 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002543 }
2544 /* compression requires us to start the IO */
2545 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2546 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2547 range->extent_thresh = (u32)-1;
2548 }
2549 } else {
2550 /* the rest are all set to zero by kzalloc */
2551 range->len = (u64)-1;
2552 }
Al Viro496ad9a2013-01-23 17:07:38 -05002553 ret = btrfs_defrag_file(file_inode(file), file,
Anand Jain7c829b72018-03-07 17:29:18 +08002554 range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002555 if (ret > 0)
2556 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002557 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002558 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002559 default:
2560 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002561 }
Chris Masone441d542009-01-05 16:57:23 -05002562out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002563 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002564 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002565}
2566
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002567static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002568{
2569 struct btrfs_ioctl_vol_args *vol_args;
2570 int ret;
2571
Chris Masone441d542009-01-05 16:57:23 -05002572 if (!capable(CAP_SYS_ADMIN))
2573 return -EPERM;
2574
David Sterba171938e2017-03-28 14:44:21 +02002575 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
Anand Jaine57138b2013-08-21 11:44:48 +08002576 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002577
Li Zefandae7b662009-04-08 15:06:54 +08002578 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002579 if (IS_ERR(vol_args)) {
2580 ret = PTR_ERR(vol_args);
2581 goto out;
2582 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002583
Mark Fasheh5516e592008-07-24 12:20:14 -04002584 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002585 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002586
Anand Jain43d20762014-07-01 00:58:56 +08002587 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002588 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08002589
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002590 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002591out:
David Sterba171938e2017-03-28 14:44:21 +02002592 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002593 return ret;
2594}
2595
Anand Jain6b526ed2016-02-13 10:01:39 +08002596static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002597{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002598 struct inode *inode = file_inode(file);
2599 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08002600 struct btrfs_ioctl_vol_args_v2 *vol_args;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002601 int ret;
2602
Chris Masone441d542009-01-05 16:57:23 -05002603 if (!capable(CAP_SYS_ADMIN))
2604 return -EPERM;
2605
Miao Xieda249272012-11-26 08:44:50 +00002606 ret = mnt_want_write_file(file);
2607 if (ret)
2608 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002609
Li Zefandae7b662009-04-08 15:06:54 +08002610 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002611 if (IS_ERR(vol_args)) {
2612 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002613 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002614 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002615
Anand Jain6b526ed2016-02-13 10:01:39 +08002616 /* Check for compatibility reject unknown flags */
David Sterba735654e2016-02-15 18:15:21 +01002617 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED)
2618 return -EOPNOTSUPP;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002619
David Sterba171938e2017-03-28 14:44:21 +02002620 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jain183860f2013-05-17 10:52:45 +00002621 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2622 goto out;
2623 }
2624
David Sterba735654e2016-02-15 18:15:21 +01002625 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002626 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
Anand Jain6b526ed2016-02-13 10:01:39 +08002627 } else {
2628 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002629 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Anand Jain6b526ed2016-02-13 10:01:39 +08002630 }
David Sterba171938e2017-03-28 14:44:21 +02002631 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain183860f2013-05-17 10:52:45 +00002632
Anand Jain6b526ed2016-02-13 10:01:39 +08002633 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01002634 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002635 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08002636 vol_args->devid);
2637 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002638 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08002639 vol_args->name);
2640 }
Anand Jain183860f2013-05-17 10:52:45 +00002641out:
2642 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002643err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002644 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002645 return ret;
2646}
2647
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002648static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2649{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002650 struct inode *inode = file_inode(file);
2651 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002652 struct btrfs_ioctl_vol_args *vol_args;
2653 int ret;
2654
2655 if (!capable(CAP_SYS_ADMIN))
2656 return -EPERM;
2657
2658 ret = mnt_want_write_file(file);
2659 if (ret)
2660 return ret;
2661
David Sterba171938e2017-03-28 14:44:21 +02002662 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002663 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
David Sterba58d7bbf2016-05-04 14:10:47 +02002664 goto out_drop_write;
2665 }
2666
2667 vol_args = memdup_user(arg, sizeof(*vol_args));
2668 if (IS_ERR(vol_args)) {
2669 ret = PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002670 goto out;
2671 }
2672
David Sterba58d7bbf2016-05-04 14:10:47 +02002673 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002674 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002675
2676 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002677 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002678 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02002679out:
David Sterba171938e2017-03-28 14:44:21 +02002680 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
David Sterba58d7bbf2016-05-04 14:10:47 +02002681out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002682 mnt_drop_write_file(file);
David Sterba58d7bbf2016-05-04 14:10:47 +02002683
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002684 return ret;
2685}
2686
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002687static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2688 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002689{
Li Zefan027ed2f2011-06-08 08:27:56 +00002690 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002691 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002692 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002693 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002694
Li Zefan027ed2f2011-06-08 08:27:56 +00002695 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2696 if (!fi_args)
2697 return -ENOMEM;
2698
David Sterbad03262c2017-06-16 00:09:21 +02002699 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00002700 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01002701
David Sterbad03262c2017-06-16 00:09:21 +02002702 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002703 if (device->devid > fi_args->max_id)
2704 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002705 }
David Sterbad03262c2017-06-16 00:09:21 +02002706 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002707
David Sterbad03262c2017-06-16 00:09:21 +02002708 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07002709 fi_args->nodesize = fs_info->nodesize;
2710 fi_args->sectorsize = fs_info->sectorsize;
2711 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02002712
Li Zefan027ed2f2011-06-08 08:27:56 +00002713 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2714 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002715
Li Zefan027ed2f2011-06-08 08:27:56 +00002716 kfree(fi_args);
2717 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002718}
2719
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002720static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2721 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01002722{
2723 struct btrfs_ioctl_dev_info_args *di_args;
2724 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01002725 int ret = 0;
2726 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002727
Jan Schmidt475f6382011-03-11 15:41:01 +01002728 di_args = memdup_user(arg, sizeof(*di_args));
2729 if (IS_ERR(di_args))
2730 return PTR_ERR(di_args);
2731
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002732 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002733 s_uuid = di_args->uuid;
2734
David Sterbac5593ca2017-06-16 00:09:21 +02002735 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002736 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002737
2738 if (!dev) {
2739 ret = -ENODEV;
2740 goto out;
2741 }
2742
2743 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08002744 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2745 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01002746 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002747 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002748 struct rcu_string *name;
2749
Josef Bacik606686e2012-06-04 14:03:51 -04002750 name = rcu_dereference(dev->name);
Xiongfeng Wang6670d4c2018-01-08 19:51:22 +08002751 strncpy(di_args->path, name->str, sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02002752 di_args->path[sizeof(di_args->path) - 1] = 0;
2753 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002754 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002755 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002756
2757out:
David Sterbac5593ca2017-06-16 00:09:21 +02002758 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01002759 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2760 ret = -EFAULT;
2761
2762 kfree(di_args);
2763 return ret;
2764}
2765
Mark Fashehf4414602015-06-30 14:42:05 -07002766static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
Mark Fasheh416161d2013-08-06 11:42:51 -07002767{
2768 struct page *page;
Mark Fasheh416161d2013-08-06 11:42:51 -07002769
Mark Fasheh416161d2013-08-06 11:42:51 -07002770 page = grab_cache_page(inode->i_mapping, index);
2771 if (!page)
Filipe Manana31314002016-01-27 18:37:47 +00002772 return ERR_PTR(-ENOMEM);
Mark Fasheh416161d2013-08-06 11:42:51 -07002773
2774 if (!PageUptodate(page)) {
Filipe Manana31314002016-01-27 18:37:47 +00002775 int ret;
2776
2777 ret = btrfs_readpage(NULL, page);
2778 if (ret)
2779 return ERR_PTR(ret);
Mark Fasheh416161d2013-08-06 11:42:51 -07002780 lock_page(page);
2781 if (!PageUptodate(page)) {
2782 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002783 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002784 return ERR_PTR(-EIO);
2785 }
2786 if (page->mapping != inode->i_mapping) {
2787 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002788 put_page(page);
Filipe Manana31314002016-01-27 18:37:47 +00002789 return ERR_PTR(-EAGAIN);
Mark Fasheh416161d2013-08-06 11:42:51 -07002790 }
2791 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002792
2793 return page;
2794}
2795
Mark Fashehf4414602015-06-30 14:42:05 -07002796static int gather_extent_pages(struct inode *inode, struct page **pages,
2797 int num_pages, u64 off)
2798{
2799 int i;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002800 pgoff_t index = off >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002801
2802 for (i = 0; i < num_pages; i++) {
Filipe Manana31314002016-01-27 18:37:47 +00002803again:
Mark Fashehf4414602015-06-30 14:42:05 -07002804 pages[i] = extent_same_get_page(inode, index + i);
Filipe Manana31314002016-01-27 18:37:47 +00002805 if (IS_ERR(pages[i])) {
2806 int err = PTR_ERR(pages[i]);
2807
2808 if (err == -EAGAIN)
2809 goto again;
2810 pages[i] = NULL;
2811 return err;
2812 }
Mark Fashehf4414602015-06-30 14:42:05 -07002813 }
2814 return 0;
2815}
2816
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002817static int lock_extent_range(struct inode *inode, u64 off, u64 len,
2818 bool retry_range_locking)
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002819{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002820 /*
2821 * Do any pending delalloc/csum calculations on inode, one way or
2822 * another, and lock file content.
2823 * The locking order is:
2824 *
2825 * 1) pages
2826 * 2) range in the inode's io tree
2827 */
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002828 while (1) {
2829 struct btrfs_ordered_extent *ordered;
2830 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2831 ordered = btrfs_lookup_first_ordered_extent(inode,
2832 off + len - 1);
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002833 if ((!ordered ||
2834 ordered->file_offset + ordered->len <= off ||
2835 ordered->file_offset >= off + len) &&
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002836 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002837 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
2838 if (ordered)
2839 btrfs_put_ordered_extent(ordered);
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002840 break;
Filipe Mananaff5df9b2014-05-30 17:56:24 +01002841 }
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002842 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2843 if (ordered)
2844 btrfs_put_ordered_extent(ordered);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002845 if (!retry_range_locking)
2846 return -EAGAIN;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002847 btrfs_wait_ordered_range(inode, off, len);
2848 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002849 return 0;
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002850}
2851
Mark Fashehf4414602015-06-30 14:42:05 -07002852static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
Mark Fasheh416161d2013-08-06 11:42:51 -07002853{
Al Viro59551022016-01-22 15:40:57 -05002854 inode_unlock(inode1);
2855 inode_unlock(inode2);
Mark Fasheh416161d2013-08-06 11:42:51 -07002856}
2857
Mark Fashehf4414602015-06-30 14:42:05 -07002858static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
2859{
2860 if (inode1 < inode2)
2861 swap(inode1, inode2);
2862
Al Viro59551022016-01-22 15:40:57 -05002863 inode_lock_nested(inode1, I_MUTEX_PARENT);
2864 inode_lock_nested(inode2, I_MUTEX_CHILD);
Mark Fashehf4414602015-06-30 14:42:05 -07002865}
2866
2867static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
2868 struct inode *inode2, u64 loff2, u64 len)
2869{
2870 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2871 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2872}
2873
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002874static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2875 struct inode *inode2, u64 loff2, u64 len,
2876 bool retry_range_locking)
Mark Fasheh416161d2013-08-06 11:42:51 -07002877{
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002878 int ret;
2879
Mark Fasheh416161d2013-08-06 11:42:51 -07002880 if (inode1 < inode2) {
2881 swap(inode1, inode2);
2882 swap(loff1, loff2);
2883 }
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002884 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
2885 if (ret)
2886 return ret;
2887 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
2888 if (ret)
2889 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
2890 loff1 + len - 1);
2891 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07002892}
2893
2894struct cmp_pages {
2895 int num_pages;
2896 struct page **src_pages;
2897 struct page **dst_pages;
2898};
2899
2900static void btrfs_cmp_data_free(struct cmp_pages *cmp)
2901{
2902 int i;
2903 struct page *pg;
2904
2905 for (i = 0; i < cmp->num_pages; i++) {
2906 pg = cmp->src_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002907 if (pg) {
2908 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002909 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002910 }
Mark Fashehf4414602015-06-30 14:42:05 -07002911 pg = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002912 if (pg) {
2913 unlock_page(pg);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002914 put_page(pg);
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002915 }
Mark Fasheh416161d2013-08-06 11:42:51 -07002916 }
Mark Fashehf4414602015-06-30 14:42:05 -07002917 kfree(cmp->src_pages);
2918 kfree(cmp->dst_pages);
2919}
2920
2921static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
2922 struct inode *dst, u64 dst_loff,
2923 u64 len, struct cmp_pages *cmp)
2924{
2925 int ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002926 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
Mark Fashehf4414602015-06-30 14:42:05 -07002927 struct page **src_pgarr, **dst_pgarr;
2928
2929 /*
2930 * We must gather up all the pages before we initiate our
2931 * extent locking. We use an array for the page pointers. Size
2932 * of the array is bounded by len, which is in turn bounded by
2933 * BTRFS_MAX_DEDUPE_LEN.
2934 */
David Sterba66722f72016-02-11 15:01:38 +01002935 src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
2936 dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
Mark Fashehf4414602015-06-30 14:42:05 -07002937 if (!src_pgarr || !dst_pgarr) {
2938 kfree(src_pgarr);
2939 kfree(dst_pgarr);
2940 return -ENOMEM;
2941 }
2942 cmp->num_pages = num_pages;
2943 cmp->src_pages = src_pgarr;
2944 cmp->dst_pages = dst_pgarr;
2945
Filipe Mananab1517622017-02-21 17:14:52 +00002946 /*
2947 * If deduping ranges in the same inode, locking rules make it mandatory
2948 * to always lock pages in ascending order to avoid deadlocks with
2949 * concurrent tasks (such as starting writeback/delalloc).
2950 */
2951 if (src == dst && dst_loff < loff) {
2952 swap(src_pgarr, dst_pgarr);
2953 swap(loff, dst_loff);
2954 }
2955
2956 ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff);
Mark Fashehf4414602015-06-30 14:42:05 -07002957 if (ret)
2958 goto out;
2959
Filipe Mananab1517622017-02-21 17:14:52 +00002960 ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff);
Mark Fashehf4414602015-06-30 14:42:05 -07002961
2962out:
2963 if (ret)
2964 btrfs_cmp_data_free(cmp);
Naohiro Aota78ad4ce2017-09-08 17:48:55 +09002965 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07002966}
2967
David Sterba1a287cf2017-02-10 20:15:10 +01002968static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
Mark Fasheh416161d2013-08-06 11:42:51 -07002969{
2970 int ret = 0;
Mark Fashehf4414602015-06-30 14:42:05 -07002971 int i;
Mark Fasheh416161d2013-08-06 11:42:51 -07002972 struct page *src_page, *dst_page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002973 unsigned int cmp_len = PAGE_SIZE;
Mark Fasheh416161d2013-08-06 11:42:51 -07002974 void *addr, *dst_addr;
2975
Mark Fashehf4414602015-06-30 14:42:05 -07002976 i = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07002977 while (len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002978 if (len < PAGE_SIZE)
Mark Fasheh416161d2013-08-06 11:42:51 -07002979 cmp_len = len;
2980
Mark Fashehf4414602015-06-30 14:42:05 -07002981 BUG_ON(i >= cmp->num_pages);
2982
2983 src_page = cmp->src_pages[i];
2984 dst_page = cmp->dst_pages[i];
Filipe Mananae0bd70c2016-01-27 10:20:58 +00002985 ASSERT(PageLocked(src_page));
2986 ASSERT(PageLocked(dst_page));
Mark Fashehf4414602015-06-30 14:42:05 -07002987
Mark Fasheh416161d2013-08-06 11:42:51 -07002988 addr = kmap_atomic(src_page);
2989 dst_addr = kmap_atomic(dst_page);
2990
2991 flush_dcache_page(src_page);
2992 flush_dcache_page(dst_page);
2993
2994 if (memcmp(addr, dst_addr, cmp_len))
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08002995 ret = -EBADE;
Mark Fasheh416161d2013-08-06 11:42:51 -07002996
2997 kunmap_atomic(addr);
2998 kunmap_atomic(dst_addr);
Mark Fasheh416161d2013-08-06 11:42:51 -07002999
3000 if (ret)
3001 break;
3002
Mark Fasheh416161d2013-08-06 11:42:51 -07003003 len -= cmp_len;
Mark Fashehf4414602015-06-30 14:42:05 -07003004 i++;
Mark Fasheh416161d2013-08-06 11:42:51 -07003005 }
3006
3007 return ret;
3008}
3009
Mark Fashehe1d227a2015-06-08 15:05:25 -07003010static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3011 u64 olen)
Mark Fasheh416161d2013-08-06 11:42:51 -07003012{
Mark Fashehe1d227a2015-06-08 15:05:25 -07003013 u64 len = *plen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003014 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3015
Mark Fashehe1d227a2015-06-08 15:05:25 -07003016 if (off + olen > inode->i_size || off + olen < off)
Mark Fasheh416161d2013-08-06 11:42:51 -07003017 return -EINVAL;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003018
3019 /* if we extend to eof, continue to block boundary */
3020 if (off + len == inode->i_size)
3021 *plen = len = ALIGN(inode->i_size, bs) - off;
3022
Mark Fasheh416161d2013-08-06 11:42:51 -07003023 /* Check that we are block aligned - btrfs_clone() requires this */
3024 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3025 return -EINVAL;
3026
3027 return 0;
3028}
3029
Timofey Titovets39739092018-05-02 08:15:36 +03003030static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen,
3031 struct inode *dst, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003032{
3033 int ret;
Mark Fashehe1d227a2015-06-08 15:05:25 -07003034 u64 len = olen;
Mark Fashehf4414602015-06-30 14:42:05 -07003035 struct cmp_pages cmp;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003036 bool same_inode = (src == dst);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003037 u64 same_lock_start = 0;
3038 u64 same_lock_len = 0;
Mark Fasheh416161d2013-08-06 11:42:51 -07003039
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003040 ret = extent_same_check_offsets(src, loff, &len, olen);
3041 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003042 return ret;
Mark Fasheh416161d2013-08-06 11:42:51 -07003043
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003044 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3045 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003046 return ret;
Omar Sandovalfc4badd2017-01-17 23:37:38 -08003047
3048 if (same_inode) {
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003049 /*
3050 * Single inode case wants the same checks, except we
3051 * don't want our length pushed out past i_size as
3052 * comparing that data range makes no sense.
3053 *
3054 * extent_same_check_offsets() will do this for an
3055 * unaligned length at i_size, so catch it here and
3056 * reject the request.
3057 *
3058 * This effectively means we require aligned extents
3059 * for the single-inode case, whereas the other cases
3060 * allow an unaligned length so long as it ends at
3061 * i_size.
3062 */
Timofey Titovets39739092018-05-02 08:15:36 +03003063 if (len != olen)
3064 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003065
3066 /* Check for overlapping ranges */
Timofey Titovets39739092018-05-02 08:15:36 +03003067 if (dst_loff + len > loff && dst_loff < loff + len)
3068 return -EINVAL;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003069
3070 same_lock_start = min_t(u64, loff, dst_loff);
3071 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003072 }
Mark Fasheh416161d2013-08-06 11:42:51 -07003073
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003074again:
Mark Fashehf4414602015-06-30 14:42:05 -07003075 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
3076 if (ret)
Timofey Titovets39739092018-05-02 08:15:36 +03003077 return ret;
Mark Fashehf4414602015-06-30 14:42:05 -07003078
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003079 if (same_inode)
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003080 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3081 false);
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003082 else
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003083 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3084 false);
3085 /*
3086 * If one of the inodes has dirty pages in the respective range or
3087 * ordered extents, we need to flush dellaloc and wait for all ordered
3088 * extents in the range. We must unlock the pages and the ranges in the
3089 * io trees to avoid deadlocks when flushing delalloc (requires locking
3090 * pages) and when waiting for ordered extents to complete (they require
3091 * range locking).
3092 */
3093 if (ret == -EAGAIN) {
3094 /*
3095 * Ranges in the io trees already unlocked. Now unlock all
3096 * pages before waiting for all IO to complete.
3097 */
3098 btrfs_cmp_data_free(&cmp);
3099 if (same_inode) {
3100 btrfs_wait_ordered_range(src, same_lock_start,
3101 same_lock_len);
3102 } else {
3103 btrfs_wait_ordered_range(src, loff, len);
3104 btrfs_wait_ordered_range(dst, dst_loff, len);
3105 }
3106 goto again;
3107 }
3108 ASSERT(ret == 0);
3109 if (WARN_ON(ret)) {
3110 /* ranges in the io trees already unlocked */
3111 btrfs_cmp_data_free(&cmp);
3112 return ret;
3113 }
Mark Fashehf4414602015-06-30 14:42:05 -07003114
Mark Fasheh207910d2015-06-30 14:42:04 -07003115 /* pass original length for comparison so we stay within i_size */
David Sterba1a287cf2017-02-10 20:15:10 +01003116 ret = btrfs_cmp_data(olen, &cmp);
Mark Fasheh416161d2013-08-06 11:42:51 -07003117 if (ret == 0)
Mark Fasheh1c919a52015-06-30 14:42:08 -07003118 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
Mark Fasheh416161d2013-08-06 11:42:51 -07003119
Mark Fasheh0efa9f42015-06-30 14:42:07 -07003120 if (same_inode)
3121 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3122 same_lock_start + same_lock_len - 1);
3123 else
3124 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
Mark Fashehf4414602015-06-30 14:42:05 -07003125
3126 btrfs_cmp_data_free(&cmp);
Timofey Titovets39739092018-05-02 08:15:36 +03003127
3128 return ret;
3129}
3130
Timofey Titovetsb6728762018-05-02 08:15:37 +03003131#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3132
Timofey Titovets39739092018-05-02 08:15:36 +03003133static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3134 struct inode *dst, u64 dst_loff)
3135{
3136 int ret;
3137 bool same_inode = (src == dst);
Timofey Titovetsb6728762018-05-02 08:15:37 +03003138 u64 i, tail_len, chunk_count;
Timofey Titovets39739092018-05-02 08:15:36 +03003139
3140 if (olen == 0)
3141 return 0;
3142
3143 if (same_inode)
3144 inode_lock(src);
3145 else
3146 btrfs_double_inode_lock(src, dst);
3147
3148 /* don't make the dst file partly checksummed */
3149 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3150 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3151 ret = -EINVAL;
3152 goto out_unlock;
3153 }
3154
Timofey Titovetsb6728762018-05-02 08:15:37 +03003155 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3156 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
3157
3158 for (i = 0; i < chunk_count; i++) {
3159 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
3160 dst, dst_loff);
3161 if (ret)
3162 goto out_unlock;
3163
3164 loff += BTRFS_MAX_DEDUPE_LEN;
3165 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3166 }
3167
3168 if (tail_len > 0)
3169 ret = btrfs_extent_same_range(src, loff, tail_len, dst, dst_loff);
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
3177 return ret;
3178}
3179
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003180ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3181 struct file *dst_file, u64 dst_loff)
Mark Fasheh416161d2013-08-06 11:42:51 -07003182{
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003183 struct inode *src = file_inode(src_file);
3184 struct inode *dst = file_inode(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07003185 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003186 ssize_t res;
Mark Fasheh416161d2013-08-06 11:42:51 -07003187
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003188 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
Mark Fasheh416161d2013-08-06 11:42:51 -07003189 /*
3190 * Btrfs does not support blocksize < page_size. As a
3191 * result, btrfs_cmp_data() won't correctly handle
3192 * this situation without an update.
3193 */
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003194 return -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07003195 }
3196
Darrick J. Wong2b3909f2015-12-19 00:56:05 -08003197 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3198 if (res)
3199 return res;
3200 return olen;
Mark Fasheh416161d2013-08-06 11:42:51 -07003201}
3202
Filipe Mananaf82a9902014-06-01 01:50:28 +01003203static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3204 struct inode *inode,
3205 u64 endoff,
3206 const u64 destoff,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003207 const u64 olen,
3208 int no_time_update)
Filipe Mananaf82a9902014-06-01 01:50:28 +01003209{
3210 struct btrfs_root *root = BTRFS_I(inode)->root;
3211 int ret;
3212
3213 inode_inc_iversion(inode);
Mark Fasheh1c919a52015-06-30 14:42:08 -07003214 if (!no_time_update)
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003215 inode->i_mtime = inode->i_ctime = current_time(inode);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003216 /*
3217 * We round up to the block size at eof when determining which
3218 * extents to clone above, but shouldn't round up the file size.
3219 */
3220 if (endoff > destoff + olen)
3221 endoff = destoff + olen;
3222 if (endoff > inode->i_size)
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003223 btrfs_i_size_write(BTRFS_I(inode), endoff);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003224
3225 ret = btrfs_update_inode(trans, root, inode);
3226 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003227 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003228 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003229 goto out;
3230 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003231 ret = btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003232out:
3233 return ret;
3234}
3235
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003236static void clone_update_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003237 const struct btrfs_trans_handle *trans,
3238 const struct btrfs_path *path,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003239 const u64 hole_offset,
3240 const u64 hole_len)
3241{
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003242 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01003243 struct extent_map *em;
3244 int ret;
3245
3246 em = alloc_extent_map();
3247 if (!em) {
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003248 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003249 return;
3250 }
3251
Filipe Manana14f59792014-06-29 21:45:40 +01003252 if (path) {
3253 struct btrfs_file_extent_item *fi;
3254
3255 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3256 struct btrfs_file_extent_item);
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003257 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003258 em->generation = -1;
3259 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3260 BTRFS_FILE_EXTENT_INLINE)
3261 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003262 &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003263 } else {
3264 em->start = hole_offset;
3265 em->len = hole_len;
3266 em->ram_bytes = em->len;
3267 em->orig_start = hole_offset;
3268 em->block_start = EXTENT_MAP_HOLE;
3269 em->block_len = 0;
3270 em->orig_block_len = 0;
3271 em->compress_type = BTRFS_COMPRESS_NONE;
3272 em->generation = trans->transid;
3273 }
3274
3275 while (1) {
3276 write_lock(&em_tree->lock);
3277 ret = add_extent_mapping(em_tree, em, 1);
3278 write_unlock(&em_tree->lock);
3279 if (ret != -EEXIST) {
3280 free_extent_map(em);
3281 break;
3282 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003283 btrfs_drop_extent_cache(inode, em->start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003284 em->start + em->len - 1, 0);
3285 }
3286
David Sterbaee39b432014-09-30 01:33:33 +02003287 if (ret)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003288 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003289}
3290
Filipe Manana8039d872015-10-13 15:15:00 +01003291/*
3292 * Make sure we do not end up inserting an inline extent into a file that has
3293 * already other (non-inline) extents. If a file has an inline extent it can
3294 * not have any other extents and the (single) inline extent must start at the
3295 * file offset 0. Failing to respect these rules will lead to file corruption,
3296 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3297 *
3298 * We can have extents that have been already written to disk or we can have
3299 * dirty ranges still in delalloc, in which case the extent maps and items are
3300 * created only when we run delalloc, and the delalloc ranges might fall outside
3301 * the range we are currently locking in the inode's io tree. So we check the
3302 * inode's i_size because of that (i_size updates are done while holding the
3303 * i_mutex, which we are holding here).
3304 * We also check to see if the inode has a size not greater than "datal" but has
3305 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3306 * protected against such concurrent fallocate calls by the i_mutex).
3307 *
3308 * If the file has no extents but a size greater than datal, do not allow the
3309 * copy because we would need turn the inline extent into a non-inline one (even
3310 * with NO_HOLES enabled). If we find our destination inode only has one inline
3311 * extent, just overwrite it with the source inline extent if its size is less
3312 * than the source extent's size, or we could copy the source inline extent's
3313 * data into the destination inode's inline extent if the later is greater then
3314 * the former.
3315 */
David Sterba4a0ab9d2017-02-10 20:18:49 +01003316static int clone_copy_inline_extent(struct inode *dst,
Filipe Manana8039d872015-10-13 15:15:00 +01003317 struct btrfs_trans_handle *trans,
3318 struct btrfs_path *path,
3319 struct btrfs_key *new_key,
3320 const u64 drop_start,
3321 const u64 datal,
3322 const u64 skip,
3323 const u64 size,
3324 char *inline_data)
3325{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003326 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
Filipe Manana8039d872015-10-13 15:15:00 +01003327 struct btrfs_root *root = BTRFS_I(dst)->root;
3328 const u64 aligned_end = ALIGN(new_key->offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003329 fs_info->sectorsize);
Filipe Manana8039d872015-10-13 15:15:00 +01003330 int ret;
3331 struct btrfs_key key;
3332
3333 if (new_key->offset > 0)
3334 return -EOPNOTSUPP;
3335
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003336 key.objectid = btrfs_ino(BTRFS_I(dst));
Filipe Manana8039d872015-10-13 15:15:00 +01003337 key.type = BTRFS_EXTENT_DATA_KEY;
3338 key.offset = 0;
3339 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3340 if (ret < 0) {
3341 return ret;
3342 } else if (ret > 0) {
3343 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3344 ret = btrfs_next_leaf(root, path);
3345 if (ret < 0)
3346 return ret;
3347 else if (ret > 0)
3348 goto copy_inline_extent;
3349 }
3350 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003351 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003352 key.type == BTRFS_EXTENT_DATA_KEY) {
3353 ASSERT(key.offset > 0);
3354 return -EOPNOTSUPP;
3355 }
3356 } else if (i_size_read(dst) <= datal) {
3357 struct btrfs_file_extent_item *ei;
3358 u64 ext_len;
3359
3360 /*
3361 * If the file size is <= datal, make sure there are no other
3362 * extents following (can happen do to an fallocate call with
3363 * the flag FALLOC_FL_KEEP_SIZE).
3364 */
3365 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3366 struct btrfs_file_extent_item);
3367 /*
3368 * If it's an inline extent, it can not have other extents
3369 * following it.
3370 */
3371 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3372 BTRFS_FILE_EXTENT_INLINE)
3373 goto copy_inline_extent;
3374
3375 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3376 if (ext_len > aligned_end)
3377 return -EOPNOTSUPP;
3378
3379 ret = btrfs_next_item(root, path);
3380 if (ret < 0) {
3381 return ret;
3382 } else if (ret == 0) {
3383 btrfs_item_key_to_cpu(path->nodes[0], &key,
3384 path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003385 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
Filipe Manana8039d872015-10-13 15:15:00 +01003386 key.type == BTRFS_EXTENT_DATA_KEY)
3387 return -EOPNOTSUPP;
3388 }
3389 }
3390
3391copy_inline_extent:
3392 /*
3393 * We have no extent items, or we have an extent at offset 0 which may
3394 * or may not be inlined. All these cases are dealt the same way.
3395 */
3396 if (i_size_read(dst) > datal) {
3397 /*
3398 * If the destination inode has an inline extent...
3399 * This would require copying the data from the source inline
3400 * extent into the beginning of the destination's inline extent.
3401 * But this is really complex, both extents can be compressed
3402 * or just one of them, which would require decompressing and
3403 * re-compressing data (which could increase the new compressed
3404 * size, not allowing the compressed data to fit anymore in an
3405 * inline extent).
3406 * So just don't support this case for now (it should be rare,
3407 * we are not really saving space when cloning inline extents).
3408 */
3409 return -EOPNOTSUPP;
3410 }
3411
3412 btrfs_release_path(path);
3413 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3414 if (ret)
3415 return ret;
3416 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3417 if (ret)
3418 return ret;
3419
3420 if (skip) {
3421 const u32 start = btrfs_file_extent_calc_inline_size(0);
3422
3423 memmove(inline_data + start, inline_data + start + skip, datal);
3424 }
3425
3426 write_extent_buffer(path->nodes[0], inline_data,
3427 btrfs_item_ptr_offset(path->nodes[0],
3428 path->slots[0]),
3429 size);
3430 inode_add_bytes(dst, datal);
3431
3432 return 0;
3433}
3434
Mark Fasheh32b7c682013-08-06 11:42:49 -07003435/**
3436 * btrfs_clone() - clone a range from inode file to another
3437 *
3438 * @src: Inode to clone from
3439 * @inode: Inode to clone to
3440 * @off: Offset within source to start clone from
3441 * @olen: Original length, passed by user, of range to clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003442 * @olen_aligned: Block-aligned value of olen
Mark Fasheh32b7c682013-08-06 11:42:49 -07003443 * @destoff: Offset within @inode to start clone
Mark Fasheh1c919a52015-06-30 14:42:08 -07003444 * @no_time_update: Whether to update mtime/ctime on the target inode
Mark Fasheh32b7c682013-08-06 11:42:49 -07003445 */
3446static int btrfs_clone(struct inode *src, struct inode *inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003447 const u64 off, const u64 olen, const u64 olen_aligned,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003448 const u64 destoff, int no_time_update)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003449{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003450 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003451 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003452 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003453 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003454 struct btrfs_trans_handle *trans;
3455 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003456 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003457 u32 nritems;
3458 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003459 int ret;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003460 const u64 len = olen_aligned;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003461 u64 last_dest_end = destoff;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003462
3463 ret = -ENOMEM;
Michal Hocko752ade62017-05-08 15:57:27 -07003464 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3465 if (!buf)
3466 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003467
3468 path = btrfs_alloc_path();
3469 if (!path) {
David Sterba15351952016-04-11 18:40:08 +02003470 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003471 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003472 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003473
David Sterbae4058b52015-11-27 16:31:35 +01003474 path->reada = READA_FORWARD;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003475 /* clone data */
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003476 key.objectid = btrfs_ino(BTRFS_I(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -04003477 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe Manana2c463822014-05-31 02:31:05 +01003478 key.offset = off;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003479
3480 while (1) {
Chris Masonde249e62015-04-11 05:09:06 -07003481 u64 next_key_min_offset = key.offset + 1;
Filipe Mananadf858e72015-03-31 14:56:46 +01003482
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003483 /*
3484 * note the key will change type as we walk through the
3485 * tree.
3486 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003487 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02003488 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3489 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003490 if (ret < 0)
3491 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003492 /*
3493 * First search, if no extent item that starts at offset off was
3494 * found but the previous item is an extent item, it's possible
3495 * it might overlap our target range, therefore process it.
3496 */
3497 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3498 btrfs_item_key_to_cpu(path->nodes[0], &key,
3499 path->slots[0] - 1);
3500 if (key.type == BTRFS_EXTENT_DATA_KEY)
3501 path->slots[0]--;
3502 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003503
Yan Zhengae01a0a2008-08-04 23:23:47 -04003504 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003505process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04003506 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02003507 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003508 if (ret < 0)
3509 goto out;
3510 if (ret > 0)
3511 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04003512 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003513 }
3514 leaf = path->nodes[0];
3515 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003516
Yan Zhengae01a0a2008-08-04 23:23:47 -04003517 btrfs_item_key_to_cpu(leaf, &key, slot);
David Sterba962a2982014-06-04 18:41:45 +02003518 if (key.type > BTRFS_EXTENT_DATA_KEY ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003519 key.objectid != btrfs_ino(BTRFS_I(src)))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003520 break;
3521
David Sterba962a2982014-06-04 18:41:45 +02003522 if (key.type == BTRFS_EXTENT_DATA_KEY) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003523 struct btrfs_file_extent_item *extent;
3524 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04003525 u32 size;
3526 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003527 u64 disko = 0, diskl = 0;
3528 u64 datao = 0, datal = 0;
3529 u8 comp;
Filipe Mananaf82a9902014-06-01 01:50:28 +01003530 u64 drop_start;
Zheng Yan31840ae2008-09-23 13:14:14 -04003531
Sage Weilc5c9cd42008-11-12 14:32:25 -05003532 extent = btrfs_item_ptr(leaf, slot,
3533 struct btrfs_file_extent_item);
3534 comp = btrfs_file_extent_compression(leaf, extent);
3535 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04003536 if (type == BTRFS_FILE_EXTENT_REG ||
3537 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05003538 disko = btrfs_file_extent_disk_bytenr(leaf,
3539 extent);
3540 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3541 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003542 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05003543 datal = btrfs_file_extent_num_bytes(leaf,
3544 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003545 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3546 /* take upper bound, may be compressed */
3547 datal = btrfs_file_extent_ram_bytes(leaf,
3548 extent);
3549 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003550
Filipe Manana2c463822014-05-31 02:31:05 +01003551 /*
3552 * The first search might have left us at an extent
3553 * item that ends before our target range's start, can
3554 * happen if we have holes and NO_HOLES feature enabled.
3555 */
3556 if (key.offset + datal <= off) {
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003557 path->slots[0]++;
3558 goto process_slot;
Filipe Manana2c463822014-05-31 02:31:05 +01003559 } else if (key.offset >= off + len) {
3560 break;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003561 }
Filipe Mananadf858e72015-03-31 14:56:46 +01003562 next_key_min_offset = key.offset + datal;
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00003563 size = btrfs_item_size_nr(leaf, slot);
3564 read_extent_buffer(leaf, buf,
3565 btrfs_item_ptr_offset(leaf, slot),
3566 size);
3567
3568 btrfs_release_path(path);
3569 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003570
Zheng Yan31840ae2008-09-23 13:14:14 -04003571 memcpy(&new_key, &key, sizeof(new_key));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003572 new_key.objectid = btrfs_ino(BTRFS_I(inode));
Li Zefan4d728ec2011-01-26 14:10:43 +08003573 if (off <= key.offset)
3574 new_key.offset = key.offset + destoff - off;
3575 else
3576 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003577
Sage Weilb6f34092011-09-20 14:48:51 -04003578 /*
Filipe Mananaf82a9902014-06-01 01:50:28 +01003579 * Deal with a hole that doesn't have an extent item
3580 * that represents it (NO_HOLES feature enabled).
3581 * This hole is either in the middle of the cloning
3582 * range or at the beginning (fully overlaps it or
3583 * partially overlaps it).
3584 */
3585 if (new_key.offset != last_dest_end)
3586 drop_start = last_dest_end;
3587 else
3588 drop_start = new_key.offset;
3589
3590 /*
Sage Weilb6f34092011-09-20 14:48:51 -04003591 * 1 - adjusting old extent (we may have to split it)
3592 * 1 - add new extent
3593 * 1 - inode update
3594 */
3595 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003596 if (IS_ERR(trans)) {
3597 ret = PTR_ERR(trans);
3598 goto out;
3599 }
3600
Chris Masonc8a894d2009-06-27 21:07:03 -04003601 if (type == BTRFS_FILE_EXTENT_REG ||
3602 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003603 /*
3604 * a | --- range to clone ---| b
3605 * | ------------- extent ------------- |
3606 */
3607
Antonio Ospite93915582014-06-04 14:03:48 +02003608 /* subtract range b */
Li Zefand72c0842011-09-11 10:52:25 -04003609 if (key.offset + datal > off + len)
3610 datal = off + len - key.offset;
3611
Antonio Ospite93915582014-06-04 14:03:48 +02003612 /* subtract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003613 if (off > key.offset) {
3614 datao += off - key.offset;
3615 datal -= off - key.offset;
3616 }
3617
Josef Bacik5dc562c2012-08-17 13:14:17 -04003618 ret = btrfs_drop_extents(trans, root, inode,
Filipe Mananaf82a9902014-06-01 01:50:28 +01003619 drop_start,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003620 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003621 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003622 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003623 if (ret != -EOPNOTSUPP)
Liu Bo00fdf132014-03-10 18:56:07 +08003624 btrfs_abort_transaction(trans,
Jeff Mahoney66642832016-06-10 18:19:25 -04003625 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003626 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003627 goto out;
3628 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003629
Sage Weilc5c9cd42008-11-12 14:32:25 -05003630 ret = btrfs_insert_empty_item(trans, root, path,
3631 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003632 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003633 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003634 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003635 goto out;
3636 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003637
3638 leaf = path->nodes[0];
3639 slot = path->slots[0];
3640 write_extent_buffer(leaf, buf,
3641 btrfs_item_ptr_offset(leaf, slot),
3642 size);
3643
3644 extent = btrfs_item_ptr(leaf, slot,
3645 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003646
Sage Weilc5c9cd42008-11-12 14:32:25 -05003647 /* disko == 0 means it's a hole */
3648 if (!disko)
3649 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003650
3651 btrfs_set_file_extent_offset(leaf, extent,
3652 datao);
3653 btrfs_set_file_extent_num_bytes(leaf, extent,
3654 datal);
Josef Bacikfcebe452014-05-13 17:30:47 -07003655
Sage Weilc5c9cd42008-11-12 14:32:25 -05003656 if (disko) {
3657 inode_add_bytes(inode, datal);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003658 ret = btrfs_inc_extent_ref(trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003659 root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003660 disko, diskl, 0,
3661 root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003662 btrfs_ino(BTRFS_I(inode)),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003663 new_key.offset - datao);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003664 if (ret) {
3665 btrfs_abort_transaction(trans,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003666 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003667 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003668 goto out;
3669
3670 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003671 }
3672 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3673 u64 skip = 0;
3674 u64 trim = 0;
Filipe Mananaed958762015-07-14 16:09:39 +01003675
Sage Weilc5c9cd42008-11-12 14:32:25 -05003676 if (off > key.offset) {
3677 skip = off - key.offset;
3678 new_key.offset += skip;
3679 }
Chris Masond3977122009-01-05 21:25:51 -05003680
Liu Boaa42ffd2012-09-18 03:52:23 -06003681 if (key.offset + datal > off + len)
3682 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003683
Sage Weilc5c9cd42008-11-12 14:32:25 -05003684 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003685 ret = -EINVAL;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003686 btrfs_end_transaction(trans);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003687 goto out;
3688 }
3689 size -= skip + trim;
3690 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003691
David Sterba4a0ab9d2017-02-10 20:18:49 +01003692 ret = clone_copy_inline_extent(inode,
Filipe Manana8039d872015-10-13 15:15:00 +01003693 trans, path,
3694 &new_key,
3695 drop_start,
3696 datal,
3697 skip, size, buf);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003698 if (ret) {
David Sterba3f9e3df2014-04-15 18:50:17 +02003699 if (ret != -EOPNOTSUPP)
Chris Mason3a29bc02014-04-07 07:10:40 -07003700 btrfs_abort_transaction(trans,
Filipe Manana8039d872015-10-13 15:15:00 +01003701 ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003702 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003703 goto out;
3704 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003705 leaf = path->nodes[0];
3706 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05003707 }
3708
Filipe Manana7ffbb592014-06-09 03:48:05 +01003709 /* If we have an implicit hole (NO_HOLES feature). */
3710 if (drop_start < new_key.offset)
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003711 clone_update_extent_map(BTRFS_I(inode), trans,
Filipe Manana14f59792014-06-29 21:45:40 +01003712 NULL, drop_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +01003713 new_key.offset - drop_start);
3714
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003715 clone_update_extent_map(BTRFS_I(inode), trans,
3716 path, 0, 0);
Filipe Manana7ffbb592014-06-09 03:48:05 +01003717
Sage Weilc5c9cd42008-11-12 14:32:25 -05003718 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003719 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003720
Filipe Manana62e23902014-08-08 02:47:06 +01003721 last_dest_end = ALIGN(new_key.offset + datal,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003722 fs_info->sectorsize);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003723 ret = clone_finish_inode_update(trans, inode,
3724 last_dest_end,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003725 destoff, olen,
3726 no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003727 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003728 goto out;
Filipe Manana2c463822014-05-31 02:31:05 +01003729 if (new_key.offset + datal >= destoff + len)
3730 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003731 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003732 btrfs_release_path(path);
Filipe Mananadf858e72015-03-31 14:56:46 +01003733 key.offset = next_key_min_offset;
Wang Xiaoguang69ae5e42016-10-13 09:23:39 +08003734
3735 if (fatal_signal_pending(current)) {
3736 ret = -EINTR;
3737 goto out;
3738 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003739 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003740 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003741
Filipe Mananaf82a9902014-06-01 01:50:28 +01003742 if (last_dest_end < destoff + len) {
3743 /*
3744 * We have an implicit hole (NO_HOLES feature is enabled) that
3745 * fully or partially overlaps our cloning range at its end.
3746 */
3747 btrfs_release_path(path);
3748
3749 /*
3750 * 1 - remove extent(s)
3751 * 1 - inode update
3752 */
3753 trans = btrfs_start_transaction(root, 2);
3754 if (IS_ERR(trans)) {
3755 ret = PTR_ERR(trans);
3756 goto out;
3757 }
3758 ret = btrfs_drop_extents(trans, root, inode,
3759 last_dest_end, destoff + len, 1);
3760 if (ret) {
3761 if (ret != -EOPNOTSUPP)
Jeff Mahoney66642832016-06-10 18:19:25 -04003762 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003763 btrfs_end_transaction(trans);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003764 goto out;
3765 }
Nikolay Borisova2f392e2017-02-20 13:51:04 +02003766 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
3767 last_dest_end,
3768 destoff + len - last_dest_end);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003769 ret = clone_finish_inode_update(trans, inode, destoff + len,
Mark Fasheh1c919a52015-06-30 14:42:08 -07003770 destoff, olen, no_time_update);
Filipe Mananaf82a9902014-06-01 01:50:28 +01003771 }
3772
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003773out:
Mark Fasheh32b7c682013-08-06 11:42:49 -07003774 btrfs_free_path(path);
David Sterba15351952016-04-11 18:40:08 +02003775 kvfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003776 return ret;
3777}
3778
Zach Brown3db11b22015-11-10 16:53:32 -05003779static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3780 u64 off, u64 olen, u64 destoff)
Mark Fasheh32b7c682013-08-06 11:42:49 -07003781{
Al Viro54563d42013-09-01 15:57:51 -04003782 struct inode *inode = file_inode(file);
Zach Brown3db11b22015-11-10 16:53:32 -05003783 struct inode *src = file_inode(file_src);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003784 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003785 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003786 int ret;
3787 u64 len = olen;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003788 u64 bs = fs_info->sb->s_blocksize;
Zach Brown3db11b22015-11-10 16:53:32 -05003789 int same_inode = src == inode;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003790
3791 /*
3792 * TODO:
3793 * - split compressed inline extents. annoying: we need to
3794 * decompress into destination's address_space (the file offset
3795 * may change, so source mapping won't do), then recompress (or
3796 * otherwise reinsert) a subrange.
Liu Bo00fdf132014-03-10 18:56:07 +08003797 *
3798 * - split destination inode's inline extents. The inline extents can
3799 * be either compressed or non-compressed.
Mark Fasheh32b7c682013-08-06 11:42:49 -07003800 */
3801
Mark Fasheh32b7c682013-08-06 11:42:49 -07003802 if (btrfs_root_readonly(root))
3803 return -EROFS;
3804
Zach Brown3db11b22015-11-10 16:53:32 -05003805 if (file_src->f_path.mnt != file->f_path.mnt ||
3806 src->i_sb != inode->i_sb)
3807 return -EXDEV;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003808
3809 /* don't make the dst file partly checksummed */
3810 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3811 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
Zach Brown3db11b22015-11-10 16:53:32 -05003812 return -EINVAL;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003813
Mark Fasheh32b7c682013-08-06 11:42:49 -07003814 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Zach Brown3db11b22015-11-10 16:53:32 -05003815 return -EISDIR;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003816
3817 if (!same_inode) {
Mark Fasheh293a8482015-06-30 14:42:06 -07003818 btrfs_double_inode_lock(src, inode);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003819 } else {
Al Viro59551022016-01-22 15:40:57 -05003820 inode_lock(src);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003821 }
3822
3823 /* determine range to clone */
3824 ret = -EINVAL;
3825 if (off + len > src->i_size || off + len < off)
3826 goto out_unlock;
3827 if (len == 0)
3828 olen = len = src->i_size - off;
3829 /* if we extend to eof, continue to block boundary */
3830 if (off + len == src->i_size)
3831 len = ALIGN(src->i_size, bs) - off;
3832
Filipe Mananaccccf3d62015-03-30 18:23:59 +01003833 if (len == 0) {
3834 ret = 0;
3835 goto out_unlock;
3836 }
3837
Mark Fasheh32b7c682013-08-06 11:42:49 -07003838 /* verify the end result is block aligned */
3839 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3840 !IS_ALIGNED(destoff, bs))
3841 goto out_unlock;
3842
3843 /* verify if ranges are overlapped within the same file */
3844 if (same_inode) {
3845 if (destoff + len > off && destoff < off + len)
3846 goto out_unlock;
3847 }
3848
3849 if (destoff > inode->i_size) {
3850 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3851 if (ret)
3852 goto out_unlock;
3853 }
3854
Filipe Mananac125b8b2014-05-23 05:03:34 +01003855 /*
3856 * Lock the target range too. Right after we replace the file extent
3857 * items in the fs tree (which now point to the cloned data), we might
3858 * have a worker replace them with extent items relative to a write
3859 * operation that was issued before this clone operation (i.e. confront
3860 * with inode.c:btrfs_finish_ordered_io).
3861 */
3862 if (same_inode) {
3863 u64 lock_start = min_t(u64, off, destoff);
3864 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003865
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003866 ret = lock_extent_range(src, lock_start, lock_len, true);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003867 } else {
Filipe Mananae0bd70c2016-01-27 10:20:58 +00003868 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
3869 true);
3870 }
3871 ASSERT(ret == 0);
3872 if (WARN_ON(ret)) {
3873 /* ranges in the io trees already unlocked */
3874 goto out_unlock;
Filipe Mananac125b8b2014-05-23 05:03:34 +01003875 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07003876
Mark Fasheh1c919a52015-06-30 14:42:08 -07003877 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003878
Filipe Mananac125b8b2014-05-23 05:03:34 +01003879 if (same_inode) {
3880 u64 lock_start = min_t(u64, off, destoff);
3881 u64 lock_end = max_t(u64, off, destoff) + len - 1;
3882
3883 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
3884 } else {
Mark Fasheh293a8482015-06-30 14:42:06 -07003885 btrfs_double_extent_unlock(src, off, inode, destoff, len);
Filipe Mananac125b8b2014-05-23 05:03:34 +01003886 }
3887 /*
3888 * Truncate page cache pages so that future reads will see the cloned
3889 * data immediately and not the previous data.
3890 */
Chandan Rajendra65bfa652016-01-21 15:56:04 +05303891 truncate_inode_pages_range(&inode->i_data,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003892 round_down(destoff, PAGE_SIZE),
3893 round_up(destoff + len, PAGE_SIZE) - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003894out_unlock:
Mark Fasheh293a8482015-06-30 14:42:06 -07003895 if (!same_inode)
3896 btrfs_double_inode_unlock(src, inode);
3897 else
Al Viro59551022016-01-22 15:40:57 -05003898 inode_unlock(src);
Zach Brown3db11b22015-11-10 16:53:32 -05003899 return ret;
3900}
3901
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003902int btrfs_clone_file_range(struct file *src_file, loff_t off,
3903 struct file *dst_file, loff_t destoff, u64 len)
Zach Brown3db11b22015-11-10 16:53:32 -05003904{
Christoph Hellwig04b38d62015-12-03 12:59:50 +01003905 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003906}
3907
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003908static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3909{
Al Viro496ad9a2013-01-23 17:07:38 -05003910 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003911 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003912 struct btrfs_root *root = BTRFS_I(inode)->root;
3913 struct btrfs_root *new_root;
3914 struct btrfs_dir_item *di;
3915 struct btrfs_trans_handle *trans;
3916 struct btrfs_path *path;
3917 struct btrfs_key location;
3918 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003919 u64 objectid = 0;
3920 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003921 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003922
3923 if (!capable(CAP_SYS_ADMIN))
3924 return -EPERM;
3925
Miao Xie3c04ce02012-11-26 08:43:07 +00003926 ret = mnt_want_write_file(file);
3927 if (ret)
3928 return ret;
3929
3930 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3931 ret = -EFAULT;
3932 goto out;
3933 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003934
3935 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303936 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003937
3938 location.objectid = objectid;
3939 location.type = BTRFS_ROOT_ITEM_KEY;
3940 location.offset = (u64)-1;
3941
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003942 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003943 if (IS_ERR(new_root)) {
3944 ret = PTR_ERR(new_root);
3945 goto out;
3946 }
satoru takeuchi6d6d2822017-09-12 22:42:52 +09003947 if (!is_fstree(new_root->objectid)) {
3948 ret = -ENOENT;
3949 goto out;
3950 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003951
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003952 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003953 if (!path) {
3954 ret = -ENOMEM;
3955 goto out;
3956 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003957 path->leave_spinning = 1;
3958
3959 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003960 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003961 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003962 ret = PTR_ERR(trans);
3963 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003964 }
3965
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003966 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3967 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003968 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003969 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003970 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003971 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003972 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003973 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003974 ret = -ENOENT;
3975 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003976 }
3977
3978 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3979 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3980 btrfs_mark_buffer_dirty(path->nodes[0]);
3981 btrfs_free_path(path);
3982
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003983 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003984 btrfs_end_transaction(trans);
Miao Xie3c04ce02012-11-26 08:43:07 +00003985out:
3986 mnt_drop_write_file(file);
3987 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003988}
3989
Su Yuec065f5b12018-04-02 17:24:11 +08003990static void get_block_group_info(struct list_head *groups_list,
3991 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003992{
3993 struct btrfs_block_group_cache *block_group;
3994
3995 space->total_bytes = 0;
3996 space->used_bytes = 0;
3997 space->flags = 0;
3998 list_for_each_entry(block_group, groups_list, list) {
3999 space->flags = block_group->flags;
4000 space->total_bytes += block_group->key.offset;
4001 space->used_bytes +=
4002 btrfs_block_group_used(&block_group->item);
4003 }
4004}
4005
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004006static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4007 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00004008{
4009 struct btrfs_ioctl_space_args space_args;
4010 struct btrfs_ioctl_space_info space;
4011 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04004012 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00004013 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00004014 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01004015 static const u64 types[] = {
4016 BTRFS_BLOCK_GROUP_DATA,
4017 BTRFS_BLOCK_GROUP_SYSTEM,
4018 BTRFS_BLOCK_GROUP_METADATA,
4019 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4020 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004021 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04004022 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00004023 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004024 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004025 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00004026
4027 if (copy_from_user(&space_args,
4028 (struct btrfs_ioctl_space_args __user *)arg,
4029 sizeof(space_args)))
4030 return -EFAULT;
4031
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004032 for (i = 0; i < num_types; i++) {
4033 struct btrfs_space_info *tmp;
4034
4035 info = NULL;
4036 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004037 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004038 list) {
4039 if (tmp->flags == types[i]) {
4040 info = tmp;
4041 break;
4042 }
4043 }
4044 rcu_read_unlock();
4045
4046 if (!info)
4047 continue;
4048
4049 down_read(&info->groups_sem);
4050 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4051 if (!list_empty(&info->block_groups[c]))
4052 slot_count++;
4053 }
4054 up_read(&info->groups_sem);
4055 }
Josef Bacik1406e432010-01-13 18:19:06 +00004056
David Sterba36523e952014-02-07 14:34:12 +01004057 /*
4058 * Global block reserve, exported as a space_info
4059 */
4060 slot_count++;
4061
Chris Mason7fde62b2010-03-16 15:40:10 -04004062 /* space_slots == 0 means they are asking for a count */
4063 if (space_args.space_slots == 0) {
4064 space_args.total_spaces = slot_count;
4065 goto out;
4066 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004067
Dan Rosenberg51788b12011-02-14 16:04:23 -05004068 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004069
Chris Mason7fde62b2010-03-16 15:40:10 -04004070 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004071
Chris Mason7fde62b2010-03-16 15:40:10 -04004072 /* we generally have at most 6 or so space infos, one for each raid
4073 * level. So, a whole page should be more than enough for everyone
4074 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004075 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04004076 return -ENOMEM;
4077
4078 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01004079 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04004080 if (!dest)
4081 return -ENOMEM;
4082 dest_orig = dest;
4083
4084 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004085 for (i = 0; i < num_types; i++) {
4086 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04004087
Dan Rosenberg51788b12011-02-14 16:04:23 -05004088 if (!slot_count)
4089 break;
4090
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004091 info = NULL;
4092 rcu_read_lock();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004093 list_for_each_entry_rcu(tmp, &fs_info->space_info,
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004094 list) {
4095 if (tmp->flags == types[i]) {
4096 info = tmp;
4097 break;
4098 }
4099 }
4100 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04004101
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004102 if (!info)
4103 continue;
4104 down_read(&info->groups_sem);
4105 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4106 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08004107 get_block_group_info(&info->block_groups[c],
4108 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004109 memcpy(dest, &space, sizeof(space));
4110 dest++;
4111 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05004112 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004113 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05004114 if (!slot_count)
4115 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04004116 }
4117 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00004118 }
Josef Bacik1406e432010-01-13 18:19:06 +00004119
David Sterba36523e952014-02-07 14:34:12 +01004120 /*
4121 * Add global block reserve
4122 */
4123 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004124 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01004125
4126 spin_lock(&block_rsv->lock);
4127 space.total_bytes = block_rsv->size;
4128 space.used_bytes = block_rsv->size - block_rsv->reserved;
4129 spin_unlock(&block_rsv->lock);
4130 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4131 memcpy(dest, &space, sizeof(space));
4132 space_args.total_spaces++;
4133 }
4134
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08004135 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04004136 (arg + sizeof(struct btrfs_ioctl_space_args));
4137
4138 if (copy_to_user(user_dest, dest_orig, alloc_size))
4139 ret = -EFAULT;
4140
4141 kfree(dest_orig);
4142out:
4143 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00004144 ret = -EFAULT;
4145
4146 return ret;
4147}
4148
Miao Xie9a8c28b2012-11-26 08:40:43 +00004149static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4150 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004151{
Sage Weil46204592010-10-29 15:41:32 -04004152 struct btrfs_trans_handle *trans;
4153 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004154 int ret;
Sage Weil46204592010-10-29 15:41:32 -04004155
Miao Xied4edf392013-02-20 09:17:06 +00004156 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00004157 if (IS_ERR(trans)) {
4158 if (PTR_ERR(trans) != -ENOENT)
4159 return PTR_ERR(trans);
4160
4161 /* No running transaction, don't bother */
4162 transid = root->fs_info->last_trans_committed;
4163 goto out;
4164 }
Sage Weil46204592010-10-29 15:41:32 -04004165 transid = trans->transid;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004166 ret = btrfs_commit_transaction_async(trans, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004167 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004168 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004169 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00004170 }
Miao Xieff7c1d32012-11-26 08:41:29 +00004171out:
Sage Weil46204592010-10-29 15:41:32 -04004172 if (argp)
4173 if (copy_to_user(argp, &transid, sizeof(transid)))
4174 return -EFAULT;
4175 return 0;
4176}
4177
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004178static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00004179 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04004180{
Sage Weil46204592010-10-29 15:41:32 -04004181 u64 transid;
4182
4183 if (argp) {
4184 if (copy_from_user(&transid, argp, sizeof(transid)))
4185 return -EFAULT;
4186 } else {
4187 transid = 0; /* current trans */
4188 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004189 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04004190}
4191
Miao Xieb8e95482012-11-26 08:48:01 +00004192static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01004193{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004194 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01004195 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00004196 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01004197
4198 if (!capable(CAP_SYS_ADMIN))
4199 return -EPERM;
4200
4201 sa = memdup_user(arg, sizeof(*sa));
4202 if (IS_ERR(sa))
4203 return PTR_ERR(sa);
4204
Miao Xieb8e95482012-11-26 08:48:01 +00004205 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4206 ret = mnt_want_write_file(file);
4207 if (ret)
4208 goto out;
4209 }
4210
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004211 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004212 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4213 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01004214
4215 if (copy_to_user(arg, sa, sizeof(*sa)))
4216 ret = -EFAULT;
4217
Miao Xieb8e95482012-11-26 08:48:01 +00004218 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4219 mnt_drop_write_file(file);
4220out:
Jan Schmidt475f6382011-03-11 15:41:01 +01004221 kfree(sa);
4222 return ret;
4223}
4224
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004225static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01004226{
4227 if (!capable(CAP_SYS_ADMIN))
4228 return -EPERM;
4229
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004230 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004231}
4232
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004233static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01004234 void __user *arg)
4235{
4236 struct btrfs_ioctl_scrub_args *sa;
4237 int ret;
4238
4239 if (!capable(CAP_SYS_ADMIN))
4240 return -EPERM;
4241
4242 sa = memdup_user(arg, sizeof(*sa));
4243 if (IS_ERR(sa))
4244 return PTR_ERR(sa);
4245
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004246 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01004247
4248 if (copy_to_user(arg, sa, sizeof(*sa)))
4249 ret = -EFAULT;
4250
4251 kfree(sa);
4252 return ret;
4253}
4254
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004255static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06004256 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004257{
4258 struct btrfs_ioctl_get_dev_stats *sa;
4259 int ret;
4260
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004261 sa = memdup_user(arg, sizeof(*sa));
4262 if (IS_ERR(sa))
4263 return PTR_ERR(sa);
4264
David Sterbab27f7c02012-06-22 06:30:39 -06004265 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4266 kfree(sa);
4267 return -EPERM;
4268 }
4269
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004270 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004271
4272 if (copy_to_user(arg, sa, sizeof(*sa)))
4273 ret = -EFAULT;
4274
4275 kfree(sa);
4276 return ret;
4277}
4278
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004279static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4280 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004281{
4282 struct btrfs_ioctl_dev_replace_args *p;
4283 int ret;
4284
4285 if (!capable(CAP_SYS_ADMIN))
4286 return -EPERM;
4287
4288 p = memdup_user(arg, sizeof(*p));
4289 if (IS_ERR(p))
4290 return PTR_ERR(p);
4291
4292 switch (p->cmd) {
4293 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004294 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004295 ret = -EROFS;
4296 goto out;
4297 }
David Sterba171938e2017-03-28 14:44:21 +02004298 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004299 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004300 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004301 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
David Sterba171938e2017-03-28 14:44:21 +02004302 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004303 }
4304 break;
4305 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004306 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004307 ret = 0;
4308 break;
4309 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004310 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004311 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004312 break;
4313 default:
4314 ret = -EINVAL;
4315 break;
4316 }
4317
4318 if (copy_to_user(arg, p, sizeof(*p)))
4319 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004320out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004321 kfree(p);
4322 return ret;
4323}
4324
Jan Schmidtd7728c92011-07-07 16:48:38 +02004325static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4326{
4327 int ret = 0;
4328 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004329 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004330 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004331 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004332 struct inode_fs_paths *ipath = NULL;
4333 struct btrfs_path *path;
4334
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004335 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004336 return -EPERM;
4337
4338 path = btrfs_alloc_path();
4339 if (!path) {
4340 ret = -ENOMEM;
4341 goto out;
4342 }
4343
4344 ipa = memdup_user(arg, sizeof(*ipa));
4345 if (IS_ERR(ipa)) {
4346 ret = PTR_ERR(ipa);
4347 ipa = NULL;
4348 goto out;
4349 }
4350
4351 size = min_t(u32, ipa->size, 4096);
4352 ipath = init_ipath(size, root, path);
4353 if (IS_ERR(ipath)) {
4354 ret = PTR_ERR(ipath);
4355 ipath = NULL;
4356 goto out;
4357 }
4358
4359 ret = paths_from_inode(ipa->inum, ipath);
4360 if (ret < 0)
4361 goto out;
4362
4363 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004364 rel_ptr = ipath->fspath->val[i] -
4365 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004366 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004367 }
4368
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004369 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4370 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004371 if (ret) {
4372 ret = -EFAULT;
4373 goto out;
4374 }
4375
4376out:
4377 btrfs_free_path(path);
4378 free_ipath(ipath);
4379 kfree(ipa);
4380
4381 return ret;
4382}
4383
4384static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4385{
4386 struct btrfs_data_container *inodes = ctx;
4387 const size_t c = 3 * sizeof(u64);
4388
4389 if (inodes->bytes_left >= c) {
4390 inodes->bytes_left -= c;
4391 inodes->val[inodes->elem_cnt] = inum;
4392 inodes->val[inodes->elem_cnt + 1] = offset;
4393 inodes->val[inodes->elem_cnt + 2] = root;
4394 inodes->elem_cnt += 3;
4395 } else {
4396 inodes->bytes_missing += c - inodes->bytes_left;
4397 inodes->bytes_left = 0;
4398 inodes->elem_missed += 3;
4399 }
4400
4401 return 0;
4402}
4403
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004404static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004405 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004406{
4407 int ret = 0;
4408 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004409 struct btrfs_ioctl_logical_ino_args *loi;
4410 struct btrfs_data_container *inodes = NULL;
4411 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004412 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004413
4414 if (!capable(CAP_SYS_ADMIN))
4415 return -EPERM;
4416
4417 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304418 if (IS_ERR(loi))
4419 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004420
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004421 if (version == 1) {
4422 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004423 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004424 } else {
4425 /* All reserved bits must be 0 for now */
4426 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4427 ret = -EINVAL;
4428 goto out_loi;
4429 }
4430 /* Only accept flags we have defined so far */
4431 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4432 ret = -EINVAL;
4433 goto out_loi;
4434 }
4435 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004436 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004437 }
4438
Jan Schmidtd7728c92011-07-07 16:48:38 +02004439 path = btrfs_alloc_path();
4440 if (!path) {
4441 ret = -ENOMEM;
4442 goto out;
4443 }
4444
Jan Schmidtd7728c92011-07-07 16:48:38 +02004445 inodes = init_data_container(size);
4446 if (IS_ERR(inodes)) {
4447 ret = PTR_ERR(inodes);
4448 inodes = NULL;
4449 goto out;
4450 }
4451
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004452 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004453 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004454 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004455 ret = -ENOENT;
4456 if (ret < 0)
4457 goto out;
4458
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004459 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4460 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004461 if (ret)
4462 ret = -EFAULT;
4463
4464out:
4465 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004466 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004467out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004468 kfree(loi);
4469
4470 return ret;
4471}
4472
David Sterba008ef092018-03-21 02:05:27 +01004473void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004474 struct btrfs_ioctl_balance_args *bargs)
4475{
4476 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4477
4478 bargs->flags = bctl->flags;
4479
David Sterba3009a622018-03-21 01:31:04 +01004480 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004481 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4482 if (atomic_read(&fs_info->balance_pause_req))
4483 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004484 if (atomic_read(&fs_info->balance_cancel_req))
4485 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004486
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004487 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4488 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4489 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004490
David Sterba008ef092018-03-21 02:05:27 +01004491 spin_lock(&fs_info->balance_lock);
4492 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4493 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004494}
4495
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004496static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004497{
Al Viro496ad9a2013-01-23 17:07:38 -05004498 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004499 struct btrfs_fs_info *fs_info = root->fs_info;
4500 struct btrfs_ioctl_balance_args *bargs;
4501 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004502 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004503 int ret;
4504
4505 if (!capable(CAP_SYS_ADMIN))
4506 return -EPERM;
4507
Liu Boe54bfa312012-06-29 03:58:48 -06004508 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004509 if (ret)
4510 return ret;
4511
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004512again:
David Sterba171938e2017-03-28 14:44:21 +02004513 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004514 mutex_lock(&fs_info->balance_mutex);
4515 need_unlock = true;
4516 goto locked;
4517 }
4518
4519 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004520 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004521 * (1) some other op is running
4522 * (2) balance is running
4523 * (3) balance is paused -- special case (think resume)
4524 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004525 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004526 if (fs_info->balance_ctl) {
4527 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004528 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004529 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004530 /*
4531 * Lock released to allow other waiters to continue,
4532 * we'll reexamine the status again.
4533 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004534 mutex_lock(&fs_info->balance_mutex);
4535
4536 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004537 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004538 /* this is (3) */
4539 need_unlock = false;
4540 goto locked;
4541 }
4542
4543 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004544 goto again;
4545 } else {
4546 /* this is (2) */
4547 mutex_unlock(&fs_info->balance_mutex);
4548 ret = -EINPROGRESS;
4549 goto out;
4550 }
4551 } else {
4552 /* this is (1) */
4553 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004554 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004555 goto out;
4556 }
4557
4558locked:
David Sterba171938e2017-03-28 14:44:21 +02004559 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004560
4561 if (arg) {
4562 bargs = memdup_user(arg, sizeof(*bargs));
4563 if (IS_ERR(bargs)) {
4564 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004565 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004566 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004567
4568 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4569 if (!fs_info->balance_ctl) {
4570 ret = -ENOTCONN;
4571 goto out_bargs;
4572 }
4573
4574 bctl = fs_info->balance_ctl;
4575 spin_lock(&fs_info->balance_lock);
4576 bctl->flags |= BTRFS_BALANCE_RESUME;
4577 spin_unlock(&fs_info->balance_lock);
4578
4579 goto do_balance;
4580 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004581 } else {
4582 bargs = NULL;
4583 }
4584
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004585 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004586 ret = -EINPROGRESS;
4587 goto out_bargs;
4588 }
4589
David Sterba8d2db782015-11-04 15:38:29 +01004590 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004591 if (!bctl) {
4592 ret = -ENOMEM;
4593 goto out_bargs;
4594 }
4595
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004596 if (arg) {
4597 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4598 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4599 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4600
4601 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004602 } else {
4603 /* balance everything - no filters */
4604 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004605 }
4606
David Sterba8eb93452015-10-12 16:55:54 +02004607 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4608 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004609 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004610 }
4611
Ilya Dryomovde322262012-01-16 22:04:49 +02004612do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004613 /*
David Sterba149196a2018-03-20 20:23:09 +01004614 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
4615 * btrfs_balance. bctl is freed in reset_balance_state, or, if
4616 * restriper was paused all the way until unmount, in free_fs_info.
4617 * The flag should be cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004618 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004619 need_unlock = false;
4620
David Sterba6fcf6e22018-05-07 17:44:03 +02004621 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004622 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004623
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004624 if (arg) {
4625 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4626 ret = -EFAULT;
4627 }
4628
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004629out_bctl:
4630 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004631out_bargs:
4632 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004633out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004634 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004635 if (need_unlock)
David Sterba171938e2017-03-28 14:44:21 +02004636 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004637out:
Liu Boe54bfa312012-06-29 03:58:48 -06004638 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004639 return ret;
4640}
4641
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004642static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004643{
4644 if (!capable(CAP_SYS_ADMIN))
4645 return -EPERM;
4646
4647 switch (cmd) {
4648 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004649 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004650 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004651 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004652 }
4653
4654 return -EINVAL;
4655}
4656
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004657static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004658 void __user *arg)
4659{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004660 struct btrfs_ioctl_balance_args *bargs;
4661 int ret = 0;
4662
4663 if (!capable(CAP_SYS_ADMIN))
4664 return -EPERM;
4665
4666 mutex_lock(&fs_info->balance_mutex);
4667 if (!fs_info->balance_ctl) {
4668 ret = -ENOTCONN;
4669 goto out;
4670 }
4671
David Sterba8d2db782015-11-04 15:38:29 +01004672 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004673 if (!bargs) {
4674 ret = -ENOMEM;
4675 goto out;
4676 }
4677
David Sterba008ef092018-03-21 02:05:27 +01004678 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004679
4680 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4681 ret = -EFAULT;
4682
4683 kfree(bargs);
4684out:
4685 mutex_unlock(&fs_info->balance_mutex);
4686 return ret;
4687}
4688
Miao Xie905b0dd2012-11-26 08:50:11 +00004689static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004690{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004691 struct inode *inode = file_inode(file);
4692 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004693 struct btrfs_ioctl_quota_ctl_args *sa;
4694 struct btrfs_trans_handle *trans = NULL;
4695 int ret;
4696 int err;
4697
4698 if (!capable(CAP_SYS_ADMIN))
4699 return -EPERM;
4700
Miao Xie905b0dd2012-11-26 08:50:11 +00004701 ret = mnt_want_write_file(file);
4702 if (ret)
4703 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004704
4705 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004706 if (IS_ERR(sa)) {
4707 ret = PTR_ERR(sa);
4708 goto drop_write;
4709 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004710
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004711 down_write(&fs_info->subvol_sem);
4712 trans = btrfs_start_transaction(fs_info->tree_root, 2);
Jan Schmidt2f232032013-04-25 16:04:51 +00004713 if (IS_ERR(trans)) {
4714 ret = PTR_ERR(trans);
4715 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004716 }
4717
4718 switch (sa->cmd) {
4719 case BTRFS_QUOTA_CTL_ENABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004720 ret = btrfs_quota_enable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004721 break;
4722 case BTRFS_QUOTA_CTL_DISABLE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004723 ret = btrfs_quota_disable(trans, fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004724 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004725 default:
4726 ret = -EINVAL;
4727 break;
4728 }
4729
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004730 err = btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00004731 if (err && !ret)
4732 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004733out:
4734 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004735 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004736drop_write:
4737 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004738 return ret;
4739}
4740
Miao Xie905b0dd2012-11-26 08:50:11 +00004741static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004742{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004743 struct inode *inode = file_inode(file);
4744 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4745 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004746 struct btrfs_ioctl_qgroup_assign_args *sa;
4747 struct btrfs_trans_handle *trans;
4748 int ret;
4749 int err;
4750
4751 if (!capable(CAP_SYS_ADMIN))
4752 return -EPERM;
4753
Miao Xie905b0dd2012-11-26 08:50:11 +00004754 ret = mnt_want_write_file(file);
4755 if (ret)
4756 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004757
4758 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004759 if (IS_ERR(sa)) {
4760 ret = PTR_ERR(sa);
4761 goto drop_write;
4762 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004763
4764 trans = btrfs_join_transaction(root);
4765 if (IS_ERR(trans)) {
4766 ret = PTR_ERR(trans);
4767 goto out;
4768 }
4769
Arne Jansen5d13a372011-09-14 15:53:51 +02004770 if (sa->assign) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004771 ret = btrfs_add_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004772 sa->src, sa->dst);
4773 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004774 ret = btrfs_del_qgroup_relation(trans, fs_info,
Arne Jansen5d13a372011-09-14 15:53:51 +02004775 sa->src, sa->dst);
4776 }
4777
Qu Wenruoe082f562015-02-27 16:24:28 +08004778 /* update qgroup status and info */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004779 err = btrfs_run_qgroups(trans, fs_info);
Qu Wenruoe082f562015-02-27 16:24:28 +08004780 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004781 btrfs_handle_fs_error(fs_info, err,
4782 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004783 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004784 if (err && !ret)
4785 ret = err;
4786
4787out:
4788 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004789drop_write:
4790 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004791 return ret;
4792}
4793
Miao Xie905b0dd2012-11-26 08:50:11 +00004794static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004795{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004796 struct inode *inode = file_inode(file);
4797 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4798 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004799 struct btrfs_ioctl_qgroup_create_args *sa;
4800 struct btrfs_trans_handle *trans;
4801 int ret;
4802 int err;
4803
4804 if (!capable(CAP_SYS_ADMIN))
4805 return -EPERM;
4806
Miao Xie905b0dd2012-11-26 08:50:11 +00004807 ret = mnt_want_write_file(file);
4808 if (ret)
4809 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004810
4811 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004812 if (IS_ERR(sa)) {
4813 ret = PTR_ERR(sa);
4814 goto drop_write;
4815 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004816
Miao Xied86e56c2012-11-15 11:35:41 +00004817 if (!sa->qgroupid) {
4818 ret = -EINVAL;
4819 goto out;
4820 }
4821
Arne Jansen5d13a372011-09-14 15:53:51 +02004822 trans = btrfs_join_transaction(root);
4823 if (IS_ERR(trans)) {
4824 ret = PTR_ERR(trans);
4825 goto out;
4826 }
4827
Arne Jansen5d13a372011-09-14 15:53:51 +02004828 if (sa->create) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004829 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004830 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004831 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004832 }
4833
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004834 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004835 if (err && !ret)
4836 ret = err;
4837
4838out:
4839 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004840drop_write:
4841 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004842 return ret;
4843}
4844
Miao Xie905b0dd2012-11-26 08:50:11 +00004845static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004846{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004847 struct inode *inode = file_inode(file);
4848 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4849 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004850 struct btrfs_ioctl_qgroup_limit_args *sa;
4851 struct btrfs_trans_handle *trans;
4852 int ret;
4853 int err;
4854 u64 qgroupid;
4855
4856 if (!capable(CAP_SYS_ADMIN))
4857 return -EPERM;
4858
Miao Xie905b0dd2012-11-26 08:50:11 +00004859 ret = mnt_want_write_file(file);
4860 if (ret)
4861 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004862
4863 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004864 if (IS_ERR(sa)) {
4865 ret = PTR_ERR(sa);
4866 goto drop_write;
4867 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004868
4869 trans = btrfs_join_transaction(root);
4870 if (IS_ERR(trans)) {
4871 ret = PTR_ERR(trans);
4872 goto out;
4873 }
4874
4875 qgroupid = sa->qgroupid;
4876 if (!qgroupid) {
4877 /* take the current subvol as qgroup */
4878 qgroupid = root->root_key.objectid;
4879 }
4880
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004881 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004882
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004883 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004884 if (err && !ret)
4885 ret = err;
4886
4887out:
4888 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004889drop_write:
4890 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004891 return ret;
4892}
4893
Jan Schmidt2f232032013-04-25 16:04:51 +00004894static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4895{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004896 struct inode *inode = file_inode(file);
4897 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004898 struct btrfs_ioctl_quota_rescan_args *qsa;
4899 int ret;
4900
4901 if (!capable(CAP_SYS_ADMIN))
4902 return -EPERM;
4903
4904 ret = mnt_want_write_file(file);
4905 if (ret)
4906 return ret;
4907
4908 qsa = memdup_user(arg, sizeof(*qsa));
4909 if (IS_ERR(qsa)) {
4910 ret = PTR_ERR(qsa);
4911 goto drop_write;
4912 }
4913
4914 if (qsa->flags) {
4915 ret = -EINVAL;
4916 goto out;
4917 }
4918
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004919 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004920
4921out:
4922 kfree(qsa);
4923drop_write:
4924 mnt_drop_write_file(file);
4925 return ret;
4926}
4927
4928static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4929{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004930 struct inode *inode = file_inode(file);
4931 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004932 struct btrfs_ioctl_quota_rescan_args *qsa;
4933 int ret = 0;
4934
4935 if (!capable(CAP_SYS_ADMIN))
4936 return -EPERM;
4937
David Sterba8d2db782015-11-04 15:38:29 +01004938 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
Jan Schmidt2f232032013-04-25 16:04:51 +00004939 if (!qsa)
4940 return -ENOMEM;
4941
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004942 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Jan Schmidt2f232032013-04-25 16:04:51 +00004943 qsa->flags = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004944 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004945 }
4946
4947 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4948 ret = -EFAULT;
4949
4950 kfree(qsa);
4951 return ret;
4952}
4953
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004954static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4955{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004956 struct inode *inode = file_inode(file);
4957 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004958
4959 if (!capable(CAP_SYS_ADMIN))
4960 return -EPERM;
4961
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004962 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004963}
4964
Hugo Millsabccd002014-01-30 20:17:00 +00004965static long _btrfs_ioctl_set_received_subvol(struct file *file,
4966 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004967{
Al Viro496ad9a2013-01-23 17:07:38 -05004968 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004969 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004970 struct btrfs_root *root = BTRFS_I(inode)->root;
4971 struct btrfs_root_item *root_item = &root->root_item;
4972 struct btrfs_trans_handle *trans;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004973 struct timespec ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02004974 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004975 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004976
David Sterbabd60ea02014-01-16 15:50:22 +01004977 if (!inode_owner_or_capable(inode))
4978 return -EPERM;
4979
Alexander Block8ea05e32012-07-25 17:35:53 +02004980 ret = mnt_want_write_file(file);
4981 if (ret < 0)
4982 return ret;
4983
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004984 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02004985
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004986 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004987 ret = -EINVAL;
4988 goto out;
4989 }
4990
4991 if (btrfs_root_readonly(root)) {
4992 ret = -EROFS;
4993 goto out;
4994 }
4995
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004996 /*
4997 * 1 - root item
4998 * 2 - uuid items (received uuid + subvol uuid)
4999 */
5000 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02005001 if (IS_ERR(trans)) {
5002 ret = PTR_ERR(trans);
5003 trans = NULL;
5004 goto out;
5005 }
5006
5007 sa->rtransid = trans->transid;
5008 sa->rtime.sec = ct.tv_sec;
5009 sa->rtime.nsec = ct.tv_nsec;
5010
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005011 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5012 BTRFS_UUID_SIZE);
5013 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02005014 !btrfs_is_empty_uuid(root_item->received_uuid)) {
5015 ret = btrfs_uuid_tree_rem(trans, fs_info,
5016 root_item->received_uuid,
5017 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5018 root->root_key.objectid);
5019 if (ret && ret != -ENOENT) {
5020 btrfs_abort_transaction(trans, ret);
5021 btrfs_end_transaction(trans);
5022 goto out;
5023 }
5024 }
Alexander Block8ea05e32012-07-25 17:35:53 +02005025 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5026 btrfs_set_root_stransid(root_item, sa->stransid);
5027 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08005028 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5029 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5030 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5031 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02005032
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005033 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02005034 &root->root_key, &root->root_item);
5035 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005036 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005037 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005038 }
5039 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005040 ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005041 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5042 root->root_key.objectid);
5043 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04005044 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03005045 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02005046 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02005047 }
5048 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005049 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00005050out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005051 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00005052 mnt_drop_write_file(file);
5053 return ret;
5054}
5055
5056#ifdef CONFIG_64BIT
5057static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5058 void __user *arg)
5059{
5060 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5061 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5062 int ret = 0;
5063
5064 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305065 if (IS_ERR(args32))
5066 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00005067
David Sterba8d2db782015-11-04 15:38:29 +01005068 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03005069 if (!args64) {
5070 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00005071 goto out;
5072 }
5073
5074 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5075 args64->stransid = args32->stransid;
5076 args64->rtransid = args32->rtransid;
5077 args64->stime.sec = args32->stime.sec;
5078 args64->stime.nsec = args32->stime.nsec;
5079 args64->rtime.sec = args32->rtime.sec;
5080 args64->rtime.nsec = args32->rtime.nsec;
5081 args64->flags = args32->flags;
5082
5083 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5084 if (ret)
5085 goto out;
5086
5087 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5088 args32->stransid = args64->stransid;
5089 args32->rtransid = args64->rtransid;
5090 args32->stime.sec = args64->stime.sec;
5091 args32->stime.nsec = args64->stime.nsec;
5092 args32->rtime.sec = args64->rtime.sec;
5093 args32->rtime.nsec = args64->rtime.nsec;
5094 args32->flags = args64->flags;
5095
5096 ret = copy_to_user(arg, args32, sizeof(*args32));
5097 if (ret)
5098 ret = -EFAULT;
5099
5100out:
5101 kfree(args32);
5102 kfree(args64);
5103 return ret;
5104}
5105#endif
5106
5107static long btrfs_ioctl_set_received_subvol(struct file *file,
5108 void __user *arg)
5109{
5110 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5111 int ret = 0;
5112
5113 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05305114 if (IS_ERR(sa))
5115 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00005116
5117 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5118
5119 if (ret)
5120 goto out;
5121
Alexander Block8ea05e32012-07-25 17:35:53 +02005122 ret = copy_to_user(arg, sa, sizeof(*sa));
5123 if (ret)
5124 ret = -EFAULT;
5125
5126out:
5127 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02005128 return ret;
5129}
5130
jeff.liu867ab662013-01-05 02:48:01 +00005131static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5132{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005133 struct inode *inode = file_inode(file);
5134 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005135 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00005136 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08005137 char label[BTRFS_LABEL_SIZE];
5138
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005139 spin_lock(&fs_info->super_lock);
5140 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5141 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08005142
5143 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00005144
5145 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005146 btrfs_warn(fs_info,
5147 "label is too long, return the first %zu bytes",
5148 --len);
jeff.liu867ab662013-01-05 02:48:01 +00005149 }
5150
jeff.liu867ab662013-01-05 02:48:01 +00005151 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00005152
5153 return ret ? -EFAULT : 0;
5154}
5155
jeff.liua8bfd4a2013-01-05 02:48:08 +00005156static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5157{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005158 struct inode *inode = file_inode(file);
5159 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5160 struct btrfs_root *root = BTRFS_I(inode)->root;
5161 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00005162 struct btrfs_trans_handle *trans;
5163 char label[BTRFS_LABEL_SIZE];
5164 int ret;
5165
5166 if (!capable(CAP_SYS_ADMIN))
5167 return -EPERM;
5168
5169 if (copy_from_user(label, arg, sizeof(label)))
5170 return -EFAULT;
5171
5172 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005173 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005174 "unable to set label with more than %d bytes",
5175 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005176 return -EINVAL;
5177 }
5178
5179 ret = mnt_want_write_file(file);
5180 if (ret)
5181 return ret;
5182
jeff.liua8bfd4a2013-01-05 02:48:08 +00005183 trans = btrfs_start_transaction(root, 0);
5184 if (IS_ERR(trans)) {
5185 ret = PTR_ERR(trans);
5186 goto out_unlock;
5187 }
5188
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005189 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005190 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005191 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005192 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005193
5194out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00005195 mnt_drop_write_file(file);
5196 return ret;
5197}
5198
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005199#define INIT_FEATURE_FLAGS(suffix) \
5200 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5201 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5202 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5203
David Sterbad5131b62016-02-17 15:26:27 +01005204int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005205{
David Sterba4d4ab6d2015-11-19 11:42:31 +01005206 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005207 INIT_FEATURE_FLAGS(SUPP),
5208 INIT_FEATURE_FLAGS(SAFE_SET),
5209 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5210 };
5211
5212 if (copy_to_user(arg, &features, sizeof(features)))
5213 return -EFAULT;
5214
5215 return 0;
5216}
5217
5218static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5219{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005220 struct inode *inode = file_inode(file);
5221 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5222 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005223 struct btrfs_ioctl_feature_flags features;
5224
5225 features.compat_flags = btrfs_super_compat_flags(super_block);
5226 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5227 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5228
5229 if (copy_to_user(arg, &features, sizeof(features)))
5230 return -EFAULT;
5231
5232 return 0;
5233}
5234
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005235static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005236 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005237 u64 change_mask, u64 flags, u64 supported_flags,
5238 u64 safe_set, u64 safe_clear)
5239{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005240 const char *type = btrfs_feature_set_names[set];
5241 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005242 u64 disallowed, unsupported;
5243 u64 set_mask = flags & change_mask;
5244 u64 clear_mask = ~flags & change_mask;
5245
5246 unsupported = set_mask & ~supported_flags;
5247 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005248 names = btrfs_printable_features(set, unsupported);
5249 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005250 btrfs_warn(fs_info,
5251 "this kernel does not support the %s feature bit%s",
5252 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005253 kfree(names);
5254 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005255 btrfs_warn(fs_info,
5256 "this kernel does not support %s bits 0x%llx",
5257 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005258 return -EOPNOTSUPP;
5259 }
5260
5261 disallowed = set_mask & ~safe_set;
5262 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005263 names = btrfs_printable_features(set, disallowed);
5264 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005265 btrfs_warn(fs_info,
5266 "can't set the %s feature bit%s while mounted",
5267 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005268 kfree(names);
5269 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005270 btrfs_warn(fs_info,
5271 "can't set %s bits 0x%llx while mounted",
5272 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005273 return -EPERM;
5274 }
5275
5276 disallowed = clear_mask & ~safe_clear;
5277 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005278 names = btrfs_printable_features(set, disallowed);
5279 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005280 btrfs_warn(fs_info,
5281 "can't clear the %s feature bit%s while mounted",
5282 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04005283 kfree(names);
5284 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005285 btrfs_warn(fs_info,
5286 "can't clear %s bits 0x%llx while mounted",
5287 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005288 return -EPERM;
5289 }
5290
5291 return 0;
5292}
5293
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005294#define check_feature(fs_info, change_mask, flags, mask_base) \
5295check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005296 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5297 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5298 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5299
5300static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5301{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005302 struct inode *inode = file_inode(file);
5303 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5304 struct btrfs_root *root = BTRFS_I(inode)->root;
5305 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005306 struct btrfs_ioctl_feature_flags flags[2];
5307 struct btrfs_trans_handle *trans;
5308 u64 newflags;
5309 int ret;
5310
5311 if (!capable(CAP_SYS_ADMIN))
5312 return -EPERM;
5313
5314 if (copy_from_user(flags, arg, sizeof(flags)))
5315 return -EFAULT;
5316
5317 /* Nothing to do */
5318 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5319 !flags[0].incompat_flags)
5320 return 0;
5321
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005322 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005323 flags[1].compat_flags, COMPAT);
5324 if (ret)
5325 return ret;
5326
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005327 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005328 flags[1].compat_ro_flags, COMPAT_RO);
5329 if (ret)
5330 return ret;
5331
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005332 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005333 flags[1].incompat_flags, INCOMPAT);
5334 if (ret)
5335 return ret;
5336
David Sterba7ab19622016-05-04 11:32:00 +02005337 ret = mnt_want_write_file(file);
5338 if (ret)
5339 return ret;
5340
David Sterba8051aa12014-02-07 14:34:04 +01005341 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005342 if (IS_ERR(trans)) {
5343 ret = PTR_ERR(trans);
5344 goto out_drop_write;
5345 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005346
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005347 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005348 newflags = btrfs_super_compat_flags(super_block);
5349 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5350 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5351 btrfs_set_super_compat_flags(super_block, newflags);
5352
5353 newflags = btrfs_super_compat_ro_flags(super_block);
5354 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5355 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5356 btrfs_set_super_compat_ro_flags(super_block, newflags);
5357
5358 newflags = btrfs_super_incompat_flags(super_block);
5359 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5360 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5361 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005362 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005363
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005364 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005365out_drop_write:
5366 mnt_drop_write_file(file);
5367
5368 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005369}
5370
Josef Bacik2351f432017-09-27 10:43:13 -04005371static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5372{
5373 struct btrfs_ioctl_send_args *arg;
5374 int ret;
5375
5376 if (compat) {
5377#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5378 struct btrfs_ioctl_send_args_32 args32;
5379
5380 ret = copy_from_user(&args32, argp, sizeof(args32));
5381 if (ret)
5382 return -EFAULT;
5383 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5384 if (!arg)
5385 return -ENOMEM;
5386 arg->send_fd = args32.send_fd;
5387 arg->clone_sources_count = args32.clone_sources_count;
5388 arg->clone_sources = compat_ptr(args32.clone_sources);
5389 arg->parent_root = args32.parent_root;
5390 arg->flags = args32.flags;
5391 memcpy(arg->reserved, args32.reserved,
5392 sizeof(args32.reserved));
5393#else
5394 return -ENOTTY;
5395#endif
5396 } else {
5397 arg = memdup_user(argp, sizeof(*arg));
5398 if (IS_ERR(arg))
5399 return PTR_ERR(arg);
5400 }
5401 ret = btrfs_ioctl_send(file, arg);
5402 kfree(arg);
5403 return ret;
5404}
5405
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005406long btrfs_ioctl(struct file *file, unsigned int
5407 cmd, unsigned long arg)
5408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005409 struct inode *inode = file_inode(file);
5410 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5411 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005412 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005413
5414 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005415 case FS_IOC_GETFLAGS:
5416 return btrfs_ioctl_getflags(file, argp);
5417 case FS_IOC_SETFLAGS:
5418 return btrfs_ioctl_setflags(file, argp);
5419 case FS_IOC_GETVERSION:
5420 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005421 case FITRIM:
5422 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005423 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005424 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005425 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005426 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005427 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005428 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005429 case BTRFS_IOC_SUBVOL_CREATE_V2:
5430 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005431 case BTRFS_IOC_SNAP_DESTROY:
5432 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08005433 case BTRFS_IOC_SUBVOL_GETFLAGS:
5434 return btrfs_ioctl_subvol_getflags(file, argp);
5435 case BTRFS_IOC_SUBVOL_SETFLAGS:
5436 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005437 case BTRFS_IOC_DEFAULT_SUBVOL:
5438 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005439 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005440 return btrfs_ioctl_defrag(file, NULL);
5441 case BTRFS_IOC_DEFRAG_RANGE:
5442 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005443 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005444 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005445 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005446 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005447 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005448 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005449 case BTRFS_IOC_RM_DEV_V2:
5450 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005451 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005452 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005453 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005454 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005455 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005456 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005457 case BTRFS_IOC_TREE_SEARCH:
5458 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005459 case BTRFS_IOC_TREE_SEARCH_V2:
5460 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005461 case BTRFS_IOC_INO_LOOKUP:
5462 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005463 case BTRFS_IOC_INO_PATHS:
5464 return btrfs_ioctl_ino_to_path(root, argp);
5465 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005466 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5467 case BTRFS_IOC_LOGICAL_INO_V2:
5468 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005469 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005470 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005471 case BTRFS_IOC_SYNC: {
5472 int ret;
5473
Nikolay Borisov82b3e532018-04-23 10:54:13 +03005474 ret = btrfs_start_delalloc_roots(fs_info, -1);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005475 if (ret)
5476 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005477 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005478 /*
5479 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005480 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005481 * processing uncleaned subvols.
5482 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005483 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005484 return ret;
5485 }
Sage Weil46204592010-10-29 15:41:32 -04005486 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005487 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005488 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005489 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005490 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005491 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005492 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005493 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005494 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005495 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005496 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005497 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005498 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005499 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005500 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005501 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005502 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5503 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005504#ifdef CONFIG_64BIT
5505 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5506 return btrfs_ioctl_set_received_subvol_32(file, argp);
5507#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005508 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005509 return _btrfs_ioctl_send(file, argp, false);
5510#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5511 case BTRFS_IOC_SEND_32:
5512 return _btrfs_ioctl_send(file, argp, true);
5513#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005514 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005515 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005516 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005517 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005518 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005519 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005520 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005521 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005522 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005523 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005524 case BTRFS_IOC_QUOTA_RESCAN:
5525 return btrfs_ioctl_quota_rescan(file, argp);
5526 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5527 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005528 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5529 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005530 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005531 return btrfs_ioctl_dev_replace(fs_info, argp);
jeff.liu867ab662013-01-05 02:48:01 +00005532 case BTRFS_IOC_GET_FSLABEL:
5533 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00005534 case BTRFS_IOC_SET_FSLABEL:
5535 return btrfs_ioctl_set_fslabel(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005536 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005537 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005538 case BTRFS_IOC_GET_FEATURES:
5539 return btrfs_ioctl_get_features(file, argp);
5540 case BTRFS_IOC_SET_FEATURES:
5541 return btrfs_ioctl_set_features(file, argp);
David Sterbae4202ac2018-03-26 19:51:16 +02005542 case FS_IOC_FSGETXATTR:
5543 return btrfs_ioctl_fsgetxattr(file, argp);
David Sterba025f2122018-03-26 19:51:16 +02005544 case FS_IOC_FSSETXATTR:
5545 return btrfs_ioctl_fssetxattr(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005546 }
5547
5548 return -ENOTTY;
5549}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005550
5551#ifdef CONFIG_COMPAT
5552long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5553{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005554 /*
5555 * These all access 32-bit values anyway so no further
5556 * handling is necessary.
5557 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005558 switch (cmd) {
5559 case FS_IOC32_GETFLAGS:
5560 cmd = FS_IOC_GETFLAGS;
5561 break;
5562 case FS_IOC32_SETFLAGS:
5563 cmd = FS_IOC_SETFLAGS;
5564 break;
5565 case FS_IOC32_GETVERSION:
5566 cmd = FS_IOC_GETVERSION;
5567 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005568 }
5569
5570 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5571}
5572#endif